001: /*
002: * Created on Jan 5, 2004
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.xdev.base.core;
008:
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: import org.xdev.base.core.compiler.AXCompiler;
013: import org.xdev.base.core.compiler.type.AbstractType;
014: import org.xdev.base.core.object.Configuration;
015: import org.xdev.base.log.LoggerWriter;
016:
017: import org.apache.log4j.Level;
018: import org.jdom.input.SAXBuilder;
019: import org.jdom.Document;
020: import org.jdom.Namespace;
021:
022: import java.io.StringReader;
023: import java.io.StringWriter;
024:
025: import javax.xml.transform.Transformer;
026: import javax.xml.transform.TransformerFactory;
027: import javax.xml.transform.stream.StreamResult;
028: import javax.xml.transform.stream.StreamSource;
029:
030: /**
031: * @author AYegorov
032: *
033: * To change the template for this generated type comment go to
034: * Window>Preferences>Java>Code Generation>Code and Comments
035: */
036: public class BaseSaxBuilder extends SAXBuilder {
037:
038: private static StringBuffer defaultNamespaces = new StringBuffer();
039:
040: /**
041: *
042: */
043: public BaseSaxBuilder() {
044: super ();
045: }
046:
047: /**
048: * @param arg0
049: */
050: public BaseSaxBuilder(String arg0) {
051: super (arg0);
052: // XXX Auto-generated constructor stub
053: }
054:
055: /**
056: * @param arg0
057: * @param arg1
058: */
059: public BaseSaxBuilder(String arg0, boolean arg1) {
060: super (arg0, arg1);
061: // XXX Auto-generated constructor stub
062: }
063:
064: /**
065: * @param arg0
066: */
067: public BaseSaxBuilder(boolean arg0) {
068: super (arg0);
069: // XXX Auto-generated constructor stub
070: }
071:
072: public static void addNamespace(String name, String uri) {
073: defaultNamespaces.append(" xmlns:").append(name).append("=\"")
074: .append(uri).append("\" ");
075: }
076:
077: public Document build(String xml, String qualifierXml,
078: Configuration types) throws Exception {
079:
080: Document doc = null;
081:
082: if (qualifierXml != null && !"".equals(qualifierXml)) {
083: LoggerWriter.log("Qualifying xml document: " + xml,
084: Level.INFO_INT, this .getClass());
085:
086: xml = this .build(xml, types, true);
087:
088: TransformerFactory transformerFactory = org.apache.xalan.processor.TransformerFactoryImpl
089: .newInstance();
090: Transformer transformer = transformerFactory
091: .newTransformer(new StreamSource(new StringReader(
092: xml)));
093:
094: StringWriter writer = new StringWriter();
095:
096: transformer.transform(new StreamSource(new StringReader(
097: qualifierXml)), new StreamResult(writer));
098:
099: xml = writer.toString();
100:
101: LoggerWriter.log("Qualified xml document: " + xml,
102: Level.INFO_INT, this .getClass());
103:
104: doc = super .build(new StringReader(xml));
105: } else {
106: doc = this .build(xml, types);
107: }
108:
109: return doc;
110: }
111:
112: public String build(String xml, Configuration types, boolean append) {
113: int index = xml.indexOf(">");
114:
115: StringBuffer buffer = new StringBuffer(xml);
116:
117: if (append) {
118: StringBuffer namespaces = new StringBuffer(
119: defaultNamespaces.toString());
120:
121: namespaces.append(" xmlns:").append(
122: AXCompiler.GLOBAL_IDENTIFIER).append("=\"").append(
123: AXCompiler.PREFIX_URI).append("\"");
124:
125: ArrayList typeList = new ArrayList(types.getKeys());
126:
127: int size = typeList.size();
128:
129: for (int i = 0; i < size; i++) {
130: if (xml.indexOf("<" + (String) typeList.get(i) + ":") != -1
131: && xml.indexOf("xmlns:"
132: + (String) typeList.get(i)) == -1) {
133: LoggerWriter.log("Adding Namespace prefix: "
134: + (String) typeList.get(i)
135: + " bound by following URI: "
136: + AXCompiler.PREFIX_URI, Level.DEBUG_INT,
137: this .getClass());
138: namespaces
139: .append(" xmlns:")
140: .append((String) typeList.get(i))
141: .append("=\"")
142: .append(
143: ((AbstractType) types
144: .getPropertyAsObject(typeList
145: .get(i).toString()))
146: .getType().getName())
147: .append("\"");
148: }
149: }
150:
151: if (namespaces.length() > 0) {
152:
153: LoggerWriter.log("Adding namespaces: "
154: + namespaces.toString() + " to the document.",
155: Level.DEBUG_INT, this .getClass());
156:
157: buffer.insert(index, namespaces);
158:
159: LoggerWriter.log(
160: "Document has been prepared with namespaces: "
161: + buffer.toString(), Level.DEBUG_INT,
162: this .getClass());
163: }
164: }
165:
166: return buffer.toString();
167: }
168:
169: public Document build(String xml, Configuration types)
170: throws Exception {
171:
172: LoggerWriter.log(xml, Level.DEBUG_INT, this .getClass());
173:
174: return super .build(new StringReader(this
175: .build(xml, types, true)));
176: }
177:
178: }
|