001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019: package de.schlund.pfixcore.util;
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.OutputStreamWriter;
026: import java.io.Writer;
027: import java.util.HashSet;
028: import java.util.Properties;
029:
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Element;
032: import org.w3c.dom.NodeList;
033: import org.xml.sax.SAXException;
034:
035: import de.schlund.pfixxml.config.GlobalConfigurator;
036: import de.schlund.pfixxml.config.XMLPropertiesUtil;
037: import de.schlund.pfixxml.resources.DocrootResource;
038: import de.schlund.pfixxml.resources.FileResource;
039: import de.schlund.pfixxml.resources.ResourceUtil;
040: import de.schlund.pfixxml.util.Xml;
041: import de.schlund.util.statuscodes.StatusCode;
042:
043: public class GenerateSCodes {
044:
045: private static final String SCODEFILES = "partindex.scodefile";
046:
047: public static void main(String[] args) throws Exception {
048: if (args.length != 3) {
049: throw new IOException("expected 3 arguments, got "
050: + args.length);
051: }
052:
053: //System.out.println("**** prop " + args[1]);
054: // Initialize PathFactory as it is needed by XMLPropertiesUtil
055: GlobalConfigurator.setDocroot(args[2]);
056: Properties tmp = new Properties();
057: if (args[1].endsWith(".prop")
058: || args[1].endsWith(".properties")) {
059: tmp.load(new FileInputStream(args[1]));
060: } else {
061: XMLPropertiesUtil.loadPropertiesFromXMLFile(new File(
062: args[1]), tmp);
063: }
064:
065: new GenerateSCodes(new File(args[0]), tmp, args[2]).run();
066: }
067:
068: File dest;
069: HashSet<DocrootResource> scfiles;
070: String docroot;
071:
072: public GenerateSCodes(File dest, Properties prop, String docroot)
073: throws IOException {
074: this .dest = dest;
075: this .docroot = docroot;
076:
077: scfiles = new HashSet<DocrootResource>();
078:
079: HashSet<String> propfiles = new HashSet<String>(PropertiesUtils
080: .selectProperties(prop, SCODEFILES).values());
081:
082: for (String tmp : propfiles) {
083: //System.out.println("**** scfile: " + tmp);
084: scfiles.add(ResourceUtil.getFileResourceFromDocroot(tmp));
085: }
086: }
087:
088: public void run() throws IOException, SAXException {
089: boolean dogen = false;
090: long targetmodtime = -1;
091:
092: //System.out.println("**** " + dest.getCanonicalPath());
093:
094: if (dest.exists()) {
095: targetmodtime = dest.lastModified();
096: } else {
097: if (!dest.getParentFile().exists()) {
098: dest.getParentFile().mkdirs();
099: }
100: }
101:
102: for (FileResource path : scfiles) {
103: //System.out.println("**** look at " + tmp.getCanonicalPath());
104: if (path.exists() && path.lastModified() > targetmodtime) {
105: dogen = true;
106: break;
107: }
108: }
109:
110: //System.out.println("**** dogen is " + dogen);
111:
112: if (dogen) {
113:
114: Writer writer = new OutputStreamWriter(
115: new FileOutputStream(dest), "ascii");
116: createHeader(writer);
117:
118: for (DocrootResource input : scfiles) {
119: Document doc = Xml.parseMutable(input);
120: NodeList list = doc.getElementsByTagName("part");
121: for (int i = 0; i < list.getLength(); i++) {
122: Element node = (Element) list.item(i);
123: String name = node.getAttribute("name");
124: String classname = StatusCode
125: .convertToFieldName(name);
126: writer
127: .write(" public static final StatusCode "
128: + classname
129: + " = new StatusCode(\""
130: + name
131: + "\", ResourceUtil.getFileResourceFromDocroot(\""
132: + input.getRelativePath()
133: + "\"));\n");
134: }
135: }
136:
137: writer.write("}\n");
138: writer.flush();
139: writer.close();
140: }
141: }
142:
143: private void createHeader(Writer writer) throws IOException {
144: writer.write("/*\n");
145: writer
146: .write(" * This file is AUTOGENERATED. Do not change by hand.\n");
147: writer.write(" */\n");
148: writer.write("\n");
149: writer.write("\n");
150: writer.write("package de.schlund.util.statuscodes;\n");
151: writer
152: .write("import de.schlund.pfixxml.resources.ResourceUtil;\n");
153: writer.write("import java.lang.reflect.Field;\n");
154: writer.write("\n");
155: writer.write("public class StatusCodeLib {\n");
156: writer
157: .write(" public static StatusCode getStatusCodeByName(String name) throws StatusCodeException {\n");
158: writer
159: .write(" return getStatusCodeByName(name, false);\n");
160: writer.write(" }\n");
161: writer.write("\n");
162: writer
163: .write(" public static StatusCode getStatusCodeByName(String name, boolean optional) throws StatusCodeException {\n");
164: writer
165: .write(" String fieldname = StatusCode.convertToFieldName(name);\n");
166: writer.write(" StatusCode scode = null;\n");
167: writer.write(" try {\n");
168: writer
169: .write(" Field field = StatusCodeLib.class.getField(fieldname);\n");
170: writer
171: .write(" scode = (StatusCode) field.get(null);\n");
172: writer.write(" } catch (NoSuchFieldException e) {\n");
173: writer.write(" //\n");
174: writer.write(" } catch (SecurityException e) {\n");
175: writer.write(" //\n");
176: writer.write(" } catch (IllegalAccessException e) {\n");
177: writer.write(" //\n");
178: writer.write(" }\n");
179: writer
180: .write(" if (scode == null && optional == false) {\n");
181: writer
182: .write(" throw new StatusCodeException(\"StatusCode \" + name + \" is not defined.\");\n");
183: writer.write(" }\n");
184: writer.write(" return scode;\n");
185: writer.write(" }\n");
186: }
187:
188: }
|