如何获取应用程序中所有.properties文件的列表

时间:2012-11-21 14:51:00

标签: xpages

我正在寻找一种方法来使用java获取应用程序中所有属性文件的文件名列表。

目标是动态构建语言选择下拉列表的内容。

1 个答案:

答案 0 :(得分:4)

此类提供方法 getPropertFileList(),它将当前数据库中的所有属性文件(语言资源文件)作为 java..util.Vector

package ch.hasselba.xpages;

import java.util.Vector;
import javax.faces.context.FacesContext;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NoteCollection;
import lotus.domino.NotesException;

public class DesignElements {

    private final String EMPTY_STRING = "";
    private final String FLAG_PROPERTY = "gC~4K2P";
    private final String FIELD_$FLAGS = "$Flags";
    private final String FIELD_TITLE = "$TITLE";

    /**
     * returns Vector containing all property files 
     * of a database
     * 
     * No error handling included!
     * 
     * @category Domino
     * @author Sven Hasselbach
     * @category Tools
     * @version 0.1
     */
    public Vector getPropertFileList() {

        FacesContext fc = FacesContext.getCurrentInstance();

        Vector data = new Vector();
        try {

            // get DB
            Database db = (Database) fc.getApplication().getVariableResolver()
                    .resolveVariable(fc, "database");

            // get all design docs
            NoteCollection nc = db.createNoteCollection(false);
            nc.selectAllDesignElements(true);
            nc.buildCollection();

            // process all notes 
            String noteId = "";
            noteId = nc.getFirstNoteID();

            Document doc = null;
            // 
            while (!(EMPTY_STRING.equals(noteId))) {

                // get design doc
                doc = db.getDocumentByID(noteId);

                // check if its a property file
                if (FLAG_PROPERTY.equals(doc.getItemValueString(FIELD_$FLAGS))) {
                    // add to Vector
                    data.add(doc.getItemValueString(FIELD_TITLE));
                }

                // next one
                noteId = nc.getNextNoteID(noteId);

                // recycle doc
                recycleObject( doc );
            }

        } catch (NotesException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return data;

    }
    /**
     * recycles a domino document instance
     *
     * @param lotus.domino.Base
     *           obj to recycle
     * @category Domino
     * @author Sven Hasselbach
     * @category Tools
     * @version 1.1
     */
    public static void recycleObject(lotus.domino.Base obj) {
        if (obj != null) {
           try {
              obj.recycle();
           } catch (Exception e) {}
       }
    }
}

要在XPage中使用它,请按以下方式调用它:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">

   <xp:comboBox id="comboBox1">
      <xp:selectItems>
         <xp:this.value>
            <![CDATA[#{javascript:
               importPackage(ch.hasselba.xpages);
               DesignElements().getPropertFileList()
            }]]>
         </xp:this.value>
      </xp:selectItems>
   </xp:comboBox>
</xp:view>