001: /*
002: * Created on Dec 9, 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.vm;
008:
009: import java.io.IOException;
010: import java.io.StringReader;
011: import java.util.ArrayList;
012:
013: import org.apache.log4j.Level;
014: import org.apache.log4j.Logger;
015: import org.jdom.Document;
016: import org.jdom.Element;
017: import org.jdom.JDOMException;
018: import org.jdom.Namespace;
019: import org.jdom.input.SAXBuilder;
020: import org.xdev.base.core.BASE;
021: import org.xdev.base.core.IPage;
022: import org.xdev.base.core.compiler.AXCompiler;
023: import org.xdev.base.core.compiler.type.AbstractType;
024: import org.xdev.base.core.object.Configuration;
025: import org.xdev.base.log.LoggerWriter;
026: import org.xdev.base.xssl.XSSLAction;
027: import org.xdev.base.xssl.XSSLReturn;
028:
029: /**
030: * @author ayegorov
031: *
032: * TODO To change the template for this generated type comment go to
033: * Window - Preferences - Java - Code Style - Code Templates
034: */
035: public class ActiveSAXBuilder extends SAXBuilder implements IPage {
036:
037: private String qualifierXml = null;
038:
039: /**
040: *
041: */
042: public ActiveSAXBuilder() {
043: super ();
044: }
045:
046: /**
047: * @param arg0
048: */
049: public ActiveSAXBuilder(String arg0) {
050: super (arg0);
051: }
052:
053: /**
054: * @param arg0
055: * @param arg1
056: */
057: public ActiveSAXBuilder(String arg0, boolean arg1) {
058: super (arg0, arg1);
059: }
060:
061: /**
062: * @param arg0
063: */
064: public ActiveSAXBuilder(boolean arg0) {
065: super (arg0);
066: }
067:
068: public Document build(java.io.File file) throws JDOMException,
069: IOException {
070:
071: return this .build(new SAXBuilder().build(file));
072: }
073:
074: public Document build(org.xml.sax.InputSource in)
075: throws JDOMException, IOException {
076:
077: return this .build(new SAXBuilder().build(in));
078: }
079:
080: public Document build(java.io.InputStream in) throws JDOMException,
081: IOException {
082:
083: return this .build(new SAXBuilder().build(in));
084: }
085:
086: public Document build(java.io.InputStream in,
087: java.lang.String systemId) throws JDOMException,
088: IOException {
089:
090: return this .build(new SAXBuilder().build(in));
091: }
092:
093: public Document build(java.io.Reader characterStream)
094: throws JDOMException, IOException {
095:
096: return this .build(new SAXBuilder().build(characterStream));
097: }
098:
099: public Document build(java.io.Reader characterStream,
100: java.lang.String systemId) throws JDOMException,
101: IOException {
102:
103: return this .build(new SAXBuilder().build(characterStream,
104: systemId));
105: }
106:
107: public Document build(java.lang.String systemId)
108: throws JDOMException, IOException {
109:
110: return this .build(new SAXBuilder().build(BASE.getFile(systemId,
111: this .getClass())));
112: }
113:
114: public Document build(java.net.URL url) throws JDOMException,
115: IOException {
116:
117: return this .build(new SAXBuilder().build(url));
118: }
119:
120: public Document buildXML(String xml) throws JDOMException,
121: IOException {
122:
123: return this
124: .build(new SAXBuilder().build(new StringReader(xml)));
125: }
126:
127: protected Document build(Document jdomDocument)
128: throws JDOMException, IOException {
129:
130: return this .build(jdomDocument, this .qualifierXml);
131: }
132:
133: protected Document build(Document jdomDocument, String qualifierXml)
134: throws JDOMException {
135:
136: Document doc = null;
137:
138: try {
139:
140: AXCompiler compiler = AXCompiler.getInstance(System
141: .getProperty(AXCompiler.BASE_CONFIG), this );
142:
143: Configuration types = AXCompiler.getTypes();
144:
145: ArrayList typeList = new ArrayList(types.getKeys());
146:
147: int size = typeList.size();
148:
149: Element jdomRoot = jdomDocument.getRootElement();
150:
151: jdomRoot.addNamespaceDeclaration(Namespace
152: .getNamespace(AXCompiler.GLOBAL_IDENTIFIER,
153: AXCompiler.PREFIX_URI));
154:
155: for (int i = 0; i < size; i++) {
156:
157: jdomRoot.addNamespaceDeclaration(Namespace
158: .getNamespace(typeList.get(i).toString(),
159: ((AbstractType) types
160: .getPropertyAsObject(typeList
161: .get(i).toString()))
162: .getType().getName()));
163: }
164:
165: XSSLAction root = compiler.compileSingleSource(this ,
166: jdomDocument, "/parser/build/jdom/"
167: + jdomDocument.getRootElement().getName(),
168: qualifierXml);
169:
170: Document txDoc = new Document();
171:
172: txDoc.setRootElement(root);
173:
174: if (root.execute()) {
175:
176: if (!(root instanceof XSSLReturn)) {
177:
178: throw new JDOMException(
179: "XDD root element "
180: + root.getName()
181: + " is not of type "
182: + XSSLReturn.class.getName()
183: + " and therefore does not return anything.");
184: }
185:
186: Object returnObject = ((XSSLReturn) root).getValue();
187:
188: if (returnObject == null) {
189:
190: throw new JDOMException(
191: "XDD does not return anything.");
192: }
193:
194: doc = new SAXBuilder().build(new StringReader(
195: returnObject.toString()));
196:
197: Element xmlRoot = doc.getRootElement();
198:
199: jdomRoot.removeNamespaceDeclaration(Namespace
200: .getNamespace(AXCompiler.GLOBAL_IDENTIFIER,
201: AXCompiler.PREFIX_URI));
202:
203: for (int i = 0; i < size; i++) {
204:
205: xmlRoot
206: .removeNamespaceDeclaration(Namespace
207: .getNamespace(
208: typeList.get(i).toString(),
209: ((AbstractType) types
210: .getPropertyAsObject(typeList
211: .get(i)
212: .toString()))
213: .getType()
214: .getName()));
215: }
216: }
217:
218: } catch (Exception e) {
219:
220: LoggerWriter.log(BASE.getValueFromObject(e),
221: Level.ERROR_INT, this .getClass());
222:
223: if (e instanceof JDOMException) {
224: throw ((JDOMException) e);
225: } else {
226: throw new JDOMException(e.getMessage());
227: }
228: }
229:
230: return doc;
231: }
232:
233: /**
234: * @return Returns the qualifierXml.
235: */
236: public String getQualifierXml() {
237: return qualifierXml;
238: }
239:
240: /**
241: * @param qualifierXml The qualifierXml to set.
242: */
243: public void setQualifierXml(String qualifierXml) {
244: this .qualifierXml = qualifierXml;
245: }
246:
247: /* (non-Javadoc)
248: * @see org.xdev.base.core.IPage#evaluate()
249: */
250: public Object evaluate() throws Exception {
251:
252: throw new Exception(
253: "This method is not available for Active XML Parser implementation. Please use any of the applicable "
254: + SAXBuilder.class.getName()
255: + " methods (e.g. build(...) ).");
256: }
257:
258: /* (non-Javadoc)
259: * @see org.xdev.base.core.IPage#getType(java.lang.String)
260: */
261: public String getType(String xmlTag) {
262: return AXCompiler.getInstance().getType(xmlTag).getType()
263: .getName();
264: }
265:
266: /* (non-Javadoc)
267: * @see org.xdev.base.core.IPage#getLogger()
268: */
269: public Logger getLogger() {
270:
271: return Logger.getLogger(this .getLoggerId());
272: }
273:
274: /* (non-Javadoc)
275: * @see org.xdev.base.core.IPage#getLoggerId()
276: */
277: public String getLoggerId() {
278: return this .getClass().getName();
279: }
280:
281: /* (non-Javadoc)
282: * @see org.xdev.base.core.IPage#getName()
283: */
284: public String getName() {
285: return this .getLoggerId();
286: }
287:
288: /* (non-Javadoc)
289: * @see org.xdev.base.core.IPage#getWorkingDirectory()
290: */
291: public String getWorkingDirectory() {
292: return System.getProperty("user.dir");
293: }
294:
295: /* (non-Javadoc)
296: * @see org.xdev.base.core.IPage#getTransactionController()
297: */
298: public Object getTransactionController() {
299: return this ;
300: }
301:
302: /* (non-Javadoc)
303: * @see org.xdev.base.core.IPage#getTransactionRequest()
304: */
305: public Object getTransactionRequest() {
306: return null;
307: }
308:
309: /* (non-Javadoc)
310: * @see org.xdev.base.core.IPage#getTransactionResponse()
311: */
312: public Object getTransactionResponse() {
313: return null;
314: }
315:
316: /* (non-Javadoc)
317: * @see org.xdev.base.core.IPage#getTransactionConfig()
318: */
319: public Object getTransactionConfig() {
320: return null;
321: }
322:
323: /* (non-Javadoc)
324: * @see org.xdev.base.core.IPage#getTransactionAction()
325: */
326: public Object getTransactionAction() {
327: return null;
328: }
329:
330: /* (non-Javadoc)
331: * @see org.xdev.base.core.IPage#getTransactionSession()
332: */
333: public Object getTransactionSession() {
334: return null;
335: }
336:
337: /* (non-Javadoc)
338: * @see org.xdev.base.core.IPage#getParentTransaction()
339: */
340: public Object getParentTransaction() {
341: return null;
342: }
343: }
|