KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > taskdefs > LocalizeXmlcUtilsImpl


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: LocalizeXmlcUtilsImpl.java,v 1.7 2004/02/01 05:16:32 christianc Exp $
19  */

20 package org.enhydra.barracuda.taskdefs;
21
22 import java.io.File JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import org.enhydra.xml.xmlc.taskdef.*;
26
27
28 /**
29  * Implementation of the XmlcUtils class for use when localizing files
30  *
31  * @author Robert Leftwich
32  * @version 1.0
33  */

34 public class LocalizeXmlcUtilsImpl extends DefaultXmlcUtilsImpl {
35
36   //===========================================================================
37
// CONSTRUCTORS
38
//===========================================================================
39

40   /**
41    * Construct default LocalizeXmlcUtilsImpl. This was formerly protected to
42    * force the use of XmlcUtils.create() factory method. However, since
43    * XmlcUtils is now in a separate package (org.enhydra.xml.xmlc.taskdef), it
44    * is necessary to make this constructor public. It should be understood,
45    * however, that one should use XmlcUtils.create() rather than using this
46    * constructor directly.
47    */

48   public LocalizeXmlcUtilsImpl() {
49     super();
50   }
51
52   //===========================================================================
53
// QUERY METHODS
54
//===========================================================================
55

56   /**
57    * Build a Java class name out of the specified components.
58    * This implementation checks to see if the file name is
59    * localized (i.e. it has a _xx suffix - where xx is a country
60    * code). If it does, it adds the modifier prior to the _xx
61    * suffix but after the file name.
62    * e.g. config_en becomes configHTML_en if the modifier is HTML
63    *
64    * @param theFullBaseFileName
65    * The full base file name (specified with '/' separators)
66    * and no file type. e.g. "org.enhydra.test"
67    * @param theModifier
68    * The file modifier to append to the class name
69    * e.g. HTML
70    * @return A Java class name string
71    */

72   public String JavaDoc buildClassName(String JavaDoc theFullBaseFileName,
73                                 String JavaDoc theModifier) {
74     // Synthesise class name based on path and filename based on filename
75
String JavaDoc baseFileName = theFullBaseFileName;
76     String JavaDoc localeStr = "";
77
78     //first and foremost, figure out if we're looking at an
79
//existing localized template file. If so, split it into
80
//baseFileName and localeStr segments.
81
if (theFullBaseFileName.indexOf("_")>0) {
82       int offset = 0;
83       String JavaDoc tfile = theFullBaseFileName;
84       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(tfile, "_");
85       while (st.hasMoreTokens()) {
86         String JavaDoc token = st.nextToken();
87         int pos = Arrays.binarySearch(Localize.countryCodes, token);
88         if (pos>-1 && pos<Localize.countryCodes.length && Localize.countryCodes[pos].equals(token)) break;
89         else offset += token.length()+1;
90       }
91       if (offset<baseFileName.length()) {
92         baseFileName = theFullBaseFileName.substring(0,offset-1);
93         localeStr = theFullBaseFileName.substring(offset-1);
94       }
95       //System.out.println("base filename:"+baseFileName+" localeStr:"+localeStr+" offset:"+offset);
96
}
97
98     String JavaDoc className = baseFileName.replace('/', '.');
99     className = className.replace(File.separatorChar, '.') + theModifier + localeStr;
100     return className;
101   }
102
103 }
104
Popular Tags