001: /*
002: * Created on Jul 5, 2004
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package org.xdev.base.core.compiler;
008:
009: import java.io.File;
010: import java.io.StringReader;
011: import java.io.StringWriter;
012: import java.util.HashMap;
013: import java.util.List;
014:
015: import javax.xml.transform.Transformer;
016: import javax.xml.transform.TransformerFactory;
017: import javax.xml.transform.stream.StreamResult;
018: import javax.xml.transform.stream.StreamSource;
019:
020: import org.apache.log4j.Level;
021: import org.jaxen.jdom.JDOMXPath;
022: import org.jdom.Attribute;
023: import org.jdom.Document;
024: import org.jdom.Element;
025: import org.jdom.Namespace;
026: import org.jdom.input.SAXBuilder;
027: import org.jdom.output.SAXOutputter;
028: import org.jdom.output.XMLOutputter;
029: import org.jdom.transform.JDOMSource;
030: import org.xdev.base.core.BASE;
031: import org.xdev.base.core.BaseSaxBuilder;
032: import org.xdev.base.core.object.Configuration;
033: import org.xdev.base.log.LoggerWriter;
034:
035: /**
036: * @author Administrator
037: *
038: * TODO To change the template for this generated type comment go to
039: * Window - Preferences - Java - Code Style - Code Templates
040: */
041: public class JDOMCompiler extends AXCompiler {
042:
043: private TransformerFactory transformerFactory = null;
044: private Transformer transformer = null;
045:
046: public JDOMCompiler(String configPath, Object invoker)
047: throws Exception {
048: super (configPath, invoker);
049:
050: String xsl = BASE.getValueFromObject(BASE.getFile(
051: AXCompiler.BASE_ROOT.getAttributeValue("xsl"), this
052: .getClass()));
053:
054: LoggerWriter.log(xsl, Level.DEBUG_INT, AXCompiler.class);
055:
056: try {
057:
058: this .transformerFactory = org.apache.xalan.processor.TransformerFactoryImpl
059: .newInstance();
060: this .transformer = transformerFactory
061: .newTransformer(new StreamSource(new StringReader(
062: xsl)));
063:
064: } catch (Exception ex) {
065: LoggerWriter.log(BASE.getValueFromObject(ex),
066: Level.DEBUG_INT, AXCompiler.class);
067:
068: this .transformer = transformerFactory
069: .newTransformer(new StreamSource(
070: new StringReader(
071: "<?xml version=\"1.0\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:output method=\"html\" encoding=\"UTF-8\"/><xsl:template match=\"/\"><compiler-validation></compiler-validation></xsl:template></xsl:stylesheet>")));
072: }
073: }
074:
075: /* (non-Javadoc)
076: * @see org.xdev.base.core.compiler.AXCompiler#compileFile(java.io.File, java.lang.String, java.lang.String)
077: */
078: protected Object compileSource(Object xddSource, String rulePath,
079: String qualifierXml) throws Exception {
080:
081: LoggerWriter.log("Starting compilation of " + xddSource,
082: Level.INFO_INT, this .getClass());
083:
084: Document doc = null;
085:
086: String xml = null;
087:
088: if (xddSource instanceof File) {
089:
090: xml = BASE.getValueFromObject((File) xddSource);
091:
092: BaseSaxBuilder builder = new BaseSaxBuilder();
093:
094: doc = builder.build(xml, qualifierXml, AXCompiler
095: .getTypes());
096: } else if (xddSource instanceof Document) {
097:
098: doc = (Document) xddSource;
099:
100: try {
101: TransformerFactory transformerFactory = org.apache.xalan.processor.TransformerFactoryImpl
102: .newInstance();
103: Transformer transformer = transformerFactory
104: .newTransformer(new JDOMSource(doc));
105:
106: StringWriter writer = new StringWriter();
107:
108: transformer.transform(new StreamSource(
109: new StringReader(qualifierXml)),
110: new StreamResult(writer));
111:
112: xml = writer.toString();
113:
114: doc = new SAXBuilder().build(new StringReader(xml));
115:
116: } catch (Exception ex) {
117:
118: LoggerWriter.log(BASE.getValueFromObject(ex),
119: Level.DEBUG_INT, this .getClass());
120: }
121:
122: if (xml == null) {
123: XMLOutputter outputter = new XMLOutputter();
124:
125: xml = outputter.outputString(doc);
126: }
127: }
128:
129: Element ruleRoot = doc.getRootElement();
130:
131: ruleRoot.setAttribute("vrulepath", rulePath);
132:
133: try {
134: StringWriter writer = new StringWriter();
135:
136: this .transformer.transform(new JDOMSource(doc),
137: new StreamResult(writer));
138:
139: xml = writer.toString();
140:
141: SAXBuilder b = new SAXBuilder();
142:
143: Document d = b.build(new StringReader(xml));
144:
145: Element valRoot = d.getRootElement();
146:
147: JDOMXPath xpath = new JDOMXPath("//validation");
148:
149: List vals = xpath.selectNodes(valRoot);
150:
151: int sz = vals.size();
152:
153: Element val = null;
154:
155: String valLevel = null;
156:
157: StringBuffer validationError = null;
158:
159: for (int i = 0; i < sz; i++) {
160: val = (Element) vals.get(i);
161:
162: valLevel = val.getAttributeValue("level");
163:
164: if ("info".equalsIgnoreCase(valLevel)
165: || valLevel == null) {
166: LoggerWriter.log(val.getTextTrim(), Level.INFO_INT,
167: AXCompiler.class);
168: } else if ("warn".equalsIgnoreCase(valLevel)) {
169: LoggerWriter.log(val.getTextTrim(), Level.WARN_INT,
170: AXCompiler.class);
171: } else if ("debug".equalsIgnoreCase(valLevel)) {
172: LoggerWriter.log(val.getTextTrim(),
173: Level.DEBUG_INT, AXCompiler.class);
174: } else if ("error".equalsIgnoreCase(valLevel)) {
175: LoggerWriter.log(val.getTextTrim(),
176: Level.ERROR_INT, AXCompiler.class);
177: } else if ("fatal".equalsIgnoreCase(valLevel)) {
178: if (validationError == null) {
179: validationError = new StringBuffer();
180: }
181:
182: validationError.append("\nERROR - ").append(
183: val.getAttributeValue("code"))
184: .append(":\n").append(val.getTextTrim())
185: .append("\n");
186: }
187: }
188:
189: if (validationError != null) {
190: try {
191: throw new CompilerException(validationError
192: .toString());
193: } finally {
194: validationError = null;
195: }
196: }
197:
198: } catch (Exception ex) {
199:
200: LoggerWriter.log(BASE.getValueFromObject(ex),
201: Level.WARN_INT, this .getClass());
202: } finally {
203: ruleRoot.removeAttribute("vrulepath");
204: }
205:
206: LoggerWriter.log("Compiling underlyining XSSL code: " + doc,
207: Level.DEBUG_INT, AXCompiler.class);
208:
209: return ruleRoot;
210: }
211:
212: /* (non-Javadoc)
213: * @see org.xdev.base.core.compiler.AXCompiler#compileProperties(java.lang.Object, org.xdev.base.core.object.Configuration)
214: */
215: public HashMap compileProperties(Object elm, Configuration stemplate)
216: throws Exception {
217: HashMap map = new HashMap();
218:
219: LoggerWriter
220: .log(
221: "[START ADD_PROPERTIES] Adding Properties [ADD_PROPERTIES START] ",
222: Level.DEBUG_INT, AXCompiler.class);
223:
224: LoggerWriter
225: .log("[ADD PROPERTY] Property Element: " + elm
226: + " [PROPERTY ADD] ", Level.DEBUG_INT,
227: AXCompiler.class);
228:
229: if (elm != null) {
230:
231: Element element = (Element) elm;
232:
233: List attrs = element.getAttributes();
234:
235: Attribute attr = null;
236:
237: int size = attrs.size();
238:
239: String id = null;
240:
241: for (int i = 0; i < size; i++) {
242:
243: attr = (Attribute) attrs.get(i);
244:
245: if (attr != null) {
246: id = attr.getName();
247:
248: if (id != null && !"".equals(id)) {
249: map.put(id, attr.getValue());
250:
251: LoggerWriter.log("[ADD PROPERTY] Key: "
252: + attr.getName() + " Value: "
253: + attr.getValue() + " [PROPERTY ADD]",
254: Level.DEBUG_INT, AXCompiler.class);
255: }
256:
257: id = null;
258: }
259: attr = null;
260: }
261:
262: List ns = element.getAdditionalNamespaces();
263:
264: size = ns.size();
265:
266: Namespace nms = null;
267:
268: for (int i = 0; i < size; i++) {
269:
270: nms = (Namespace) ns.get(i);
271:
272: if (nms != null) {
273:
274: String nmsPrefix = nms.getPrefix();
275:
276: if (!nmsPrefix.equals(AXCompiler.GLOBAL_IDENTIFIER)) {
277:
278: map.put(AXCompiler.NAMESPACE_DECL + nmsPrefix,
279: nms.getURI());
280:
281: }
282: }
283: }
284:
285: attrs = null;
286: }
287: LoggerWriter
288: .log(
289: "[END ADD_PROPERTIES] Returning Compiled Map [ADD_PROPERTIES END] ",
290: Level.DEBUG_INT, AXCompiler.class);
291:
292: return map;
293: }
294:
295: }
|