Multilingual packages use the review of Resource, all features are defined in resource.
There is a main method in the XMLResource class, which can be run directly.
Execute the XMLResource.Import method before calling.
The software has enabled a resource file as an XML file, and the format is finally shown.
The following files are compiled and run
======================= rouce.java =================================================================================================================================================================================
Package org.fswan;
Import java.util.locale;
Import java.util.properties;
/ **
* @Author Swan Fong (Fang Zhiwen)
* e-mail: fswan@yah.net
* Use the interface to read the resource file, get different strings with different languages
* If you get the same incoming area code for the resource is null, use the default area code.
* /
Public interface resource {
/ **
* Get the content of resources corresponding to the name
* @Param Name Name
* @Param locale language
* @return resources content
* /
Public String GetResource (String Name, Locale Locale);
/ **
* Get the content of resources with parameters
* @Param Name resource name
* @Param Property resource parameter value
* @Param locale language
* @Return's content
* /
Public String getResource (String Name, String [] Property, Locale Locale;
/ **
* Get a group of resources
* If the directory is named Common.title, it is displayed in all the resources under Title under Common.
* Property is the parameter name, corresponding to the length of the resource under Category, the second dimension length. If the length is less than the length of Category
* The last one as the parameters of all resources thereafter, if all resources are not parameters if NULL.
* @Param category directory name
* @Param Property parameters
* @Param locale language
* @return resource array
* /
Public Properties GetResource (String Category, String [] Property, Locale Locale;
/ **
* Get the name of all resources under the directory
* @Param category directory name
* @Param locale language
* @Return's arrays of all children
* /
PUBLIC STRING [] GetResourceNames (String Category, Locale Locale);
/ **
* Get all resources or subdirectories under the directory
* @Param category directory name
* @Param iscategory returns a subdirectory or resources
* @Param locale language
* @return returned content
* /
Public String [] GetResourceNames (String Category, Boolean ISCategory, Locale Locale);
================================================
============== XmlResource.java ======================
Package org.fswan;
Import java.io.file;
Import java.io.ioException;
Import java.util.arraylist;
Import java.util.hashtable;
Import java.util.locale;
Import java.util.properties;
Import java.util.StringTokenizer;
Import javax.xml.parsers.documentbuilder;
Import javax.xml.parsers.documentBuilderFactory;
Import javax.xml.parsers.FactoryConfigurationError;
Import javax.xml.parsers.ParserConfigurationException;
Import org.w3c.dom.document;
Import Org.w3c.dom.Element;
Import org.w3c.dom.nodeelist;
Import org.xml.sax.saxexception;
/ **
* @Author Swan Fong (Fang Zhiwen)
* E-mail: fswan@yeah.net
* Get the class from the XML file
* The location of the resource is separated from the tree layer.
* If the resource file content is the following internal space
*
*
* form1>
* resource>
* Get the contents of resource form1.title1 to Title1
* Get the contents of resource Form1 as Title1 and Title2
*
* You can also replace% 1, such as title of% 1
* When you use GetResource ("XXXX", "BOOK"), you will become Title of Book.
*
* /
Public Class XmlResource Implements Resource
{
/ **
* Used to store resources, Hashtable's key is Locale type, the value is ELEMENT, corresponding to different locale has different Document
* /
Private static hashtable resources;
/ **
* Default language
* /
Private Static Locale DefaultLocale = New Locale ("EN", "CN"); static {
ArrayList List = ResourceLib.getxmlResource ();
For (int i = 0; i { ImportXML (list.get (i) .tostring ()); } } Public Static Void ImportXML (String FileName) { Try { DocumentBuilderFactory Builderfac = DocumentBuilderFactory.newInstance (); DocumentBuilder Builder = builderfac.newdocumentbuilder (); Document Doc = NULL; IF (New File (filename) .exists ()) DOC = Builder.Parse (New File (file (filename)); Else Doc = builder.Parse (XmlResource.class.getResourceASstream (FileName)); Element root = doc.getdocumentelement (); Nodelist nl = root.getLetelementsBytagname ("resource"); Resources = new hashtable (); For (int i = 0; i { ELEMENT EL = (Element) NL.Item (i); Locale Locale = New Locale (El.getaTribute ("Language"), El.getaTribute ("Country")); Resources.Put (Locale, EL); } } catch (FactoryConfigurationError E) { E.PrintStackTrace (); } catch (ParserConfigurationException E) { E.PrintStackTrace (); } catch (SAXEXCEPTION E) { E.PrintStackTrace (); } catch (ioException e) { E.PrintStackTrace (); } } / * (non-javadoc) * @see org.fswan.db.resource # getResource (java.lang.string, java.util.locale) * / Public String GetResource (String Name, Locale Locale) { IF (locale == null) Locale = defaultlocale; ELEMENT EL = NULL; IF (resources.get (locale)! = null) EL = (element) resources.get (locale); Else EL = (Element) resources.get (defaultlocale); StringTokenizer Stk = New StringTokenizer (Name, "."); While (STK.hasmoreToKens ()) { Nodelist nl = el.GtelementsBytagname (STK.NEXTTOKEN ()); if (nl.getlength () <1) Return NULL; EL = (element) nl.Item (0); } Return el.getchildNodes (). Item (0) .GetnodeValue (); } / * (non-javadoc) * @see org.fswan.db.resource # getResource (java.lang.string, java.lang.string [], java.util.locale) * / Public string getresource (string name, string [] Property, Locale Locale) { IF (Property == Null || Property.Length == 0) Property = new string [] {""}; String Tmpstr = getResource (name, local); StringBuffer SB = New StringBuffer (TMPSTR); INT i = 1; INT POS = getPosition (SB, I); While (POS! = -1) { Sb.replace POS, POS ("%" i) .length (), Property [(i - 1> Property.length)? Property.Length - 1: i - 1]); i ; POS = getPosition (SB, I); } Return sb.toString (); } / ** * Get the location of% i in the StringBuffer, if you are / wrong, if you can't find it, return -1 * @Param Str To find the string * @Param i to find the content * @return location * / Private int getPosition (StringBuffer Str, INT i) { INT POS = Str.indexof ("%" i); While (POS! = -1 && str.charat (POS - 1) == '//') { POS = Str.Indexof ("%" i, pOS); } Return POS; } / * (non-javadoc) * @see org.fswan.db.resource # getResource (java.lang.string, java.lang.string [] [], java.util.locale) * / Public Properties GetResource (String Category, String "[] Property, Locale Locale { IF (Property == Null || Property.Length == 0) { Property = New String [1] [1]; Property [0] [0] = "" } String [] Names = GetResourceNames (Category, False, Locale); String [] Values = new string [names.length]; Properties P = New Properties (); for (int i = 0; i { String [] Pro = Property [(i> Property.Length - 1)? Property.Length - 1: i]; VALUES [I] = GetResource (Category "." Names [i], pro, local); P.SetProperty (Names [i], values [i]); } Return P; } / * (non-javadoc) * @see org.fswan.db.resource # getResourceNames (java.lang.string, boolean, java.util.locale) * / Public String [] GetResourceNames (String Category, Boolean ISCategory, Locale Locale) { IF (locale == null) Locale = defaultlocale; ELEMENT EL = NULL; IF (resources.get (locale)! = null) EL = (element) resources.get (locale); Else EL = (Element) resources.get (defaultlocale); StringTokenizer Stk = New StringTokenizer (category, "."); While (STK.hasmoreToKens ()) { Nodelist NL = El.GtelementsBytagname (STK.NEXTTOKEN ()); IF (nl.getlength () <1) Return NULL; EL = (element) nl.Item (0); } Nodelist NL = El.getElementsBytagname ("*"); IF (nl == null || nl.getLength () == 0) Return new string [] { } String [] resources = new string [nl.getLength ()]; For (int i = 0; i { Resources [i] = nl.Item (i) .Getnodename (); } Return resources; } / * (non-javadoc) * @see org.fswan.db.resource # getResourceNames (java.lang.string, java.util.locale) * / Public string [] getResourceNames (String Category, Locale Locale) { IF (locale == null) Locale = defaultlocale; ELEMENT EL = NULL; IF (resources.get (locale)! = null) EL = (element) resources.get (locale); Else EL = (Element) resources.get (defaultlocale); StringTokenizer Stk = New StringTokenizer (category, "."); While (STK.hasmoreToKens ()) { Nodelist NL = El.GtelementsBytagname (STK.NEXTTOKEN ()); IF (nl.getlength () <1) Return NULL; EL = (element) nl.Item (0); } Nodelist NL = El.getElementsBytagname ("*"); IF (nl == null || nl.getLength () == 0) Return new string [] { } String [] resources = new string [nl.getLength ()]; For (int i = 0; i { Resources [i] = nl.Item (i) .Getnodename (); } Return resources; } Public static void main (string [] args) { XmlResource.ImportXML ("COM / FSWAN / Application / AddressBook / Resource.xml"); Properties first = new XmlResource (). Getresource ("addressbook", (String [] []) null, null; Object [] s = first.keyset (). Toarray (); For (int i = 0; i { System.out.println (s [i] " first.getproperty (s [i] .tostring ())))); } } } ========================== ========================================================================================================================================================================================================================== ========== ======================================================================================================================================================================================================== ===== XML Version = "1.0" encoding = "UTF-8"?> menu> mainframe> resource> <中文> English eNGLish> menu> mainframe> resource> root> ========================= End ====================================================================================================================================================== ===========