001    package railo.runtime.text.xml.struct;
002    
003    import java.lang.reflect.Method;
004    
005    import org.w3c.dom.Attr;
006    import org.w3c.dom.CDATASection;
007    import org.w3c.dom.Comment;
008    import org.w3c.dom.DOMConfiguration;
009    import org.w3c.dom.DOMException;
010    import org.w3c.dom.DOMImplementation;
011    import org.w3c.dom.Document;
012    import org.w3c.dom.DocumentFragment;
013    import org.w3c.dom.DocumentType;
014    import org.w3c.dom.Element;
015    import org.w3c.dom.EntityReference;
016    import org.w3c.dom.Node;
017    import org.w3c.dom.NodeList;
018    import org.w3c.dom.ProcessingInstruction;
019    import org.w3c.dom.Text;
020    
021    import railo.runtime.exp.PageRuntimeException;
022    import railo.runtime.op.Caster;
023    import railo.runtime.type.Collection;
024    import railo.runtime.type.util.ArrayUtil;
025    
026    
027    /**
028     * 
029     */
030    public final class XMLDocumentStruct extends XMLNodeStruct implements Document {
031    
032            private Document doc;
033    
034            /**
035             * @param doc
036             * @param caseSensitive
037             */
038            protected XMLDocumentStruct(Document doc, boolean caseSensitive) {
039                    super(doc, caseSensitive);
040                    this.doc=doc;
041                    
042            }
043    
044            @Override
045            public DOMImplementation getImplementation() {
046                    return doc.getImplementation();
047            }
048    
049            @Override
050            public DocumentFragment createDocumentFragment() {
051                    return doc.createDocumentFragment();
052            }
053    
054            @Override
055            public DocumentType getDoctype() {
056                    return doc.getDoctype();
057            }
058    
059            @Override
060            public Element getDocumentElement() {
061                    return doc.getDocumentElement();
062            }
063    
064            @Override
065            public Attr createAttribute(String name) throws DOMException {
066                    return doc.createAttribute(name);
067            }
068    
069            @Override
070            public CDATASection createCDATASection(String data) throws DOMException {
071                    return doc.createCDATASection(data);
072            }
073    
074            @Override
075            public Comment createComment(String data) {
076                    return doc.createComment(data);
077            }
078    
079            @Override
080            public Element createElement(String tagName) throws DOMException {
081                    return doc.createElement(tagName);
082            }
083    
084            @Override
085            public Element getElementById(String elementId) {
086                    return doc.getElementById(elementId);
087            }
088    
089            @Override
090            public EntityReference createEntityReference(String name) throws DOMException {
091                    return doc.createEntityReference(name);
092            }
093    
094            @Override
095            public Node importNode(Node importedNode, boolean deep) throws DOMException {
096                    return doc.importNode(importedNode,deep);
097            }
098    
099            @Override
100            public NodeList getElementsByTagName(String tagname) {
101                    return doc.getElementsByTagName(tagname);
102            }
103    
104            @Override
105            public Text createTextNode(String data) {
106                    return doc.createTextNode(data);
107            }
108    
109            @Override
110            public Attr createAttributeNS(String namespaceURI, String qualifiedName) throws DOMException {
111                    return doc.createAttributeNS(namespaceURI,qualifiedName);
112            }
113    
114            @Override
115            public Element createElementNS(String namespaceURI, String qualifiedName) throws DOMException {
116                    return doc.createElementNS(namespaceURI,qualifiedName);
117            }
118    
119            @Override
120            public NodeList getElementsByTagNameNS(String namespaceURI, String localName) {
121                    return doc.getElementsByTagNameNS(namespaceURI,localName);
122            }
123    
124            @Override
125            public ProcessingInstruction createProcessingInstruction(String target, String data) throws DOMException {
126                    return doc.createProcessingInstruction(target,data);
127            }
128    
129            public Node adoptNode(Node arg0) throws DOMException {
130            // dynamic load to support jre 1.4 and 1.5
131                    try {
132                            Method m = doc.getClass().getMethod("adoptNode", new Class[]{arg0.getClass()});
133                            return Caster.toNode(m.invoke(doc, new Object[]{arg0}));
134                    } 
135                    catch (Exception e) {
136                            throw new PageRuntimeException(Caster.toPageException(e));
137                    }
138            }
139    
140            public String getDocumentURI() {
141            // dynamic load to support jre 1.4 and 1.5
142                    try {
143                            Method m = doc.getClass().getMethod("getDocumentURI", new Class[]{});
144                            return Caster.toString(m.invoke(doc, ArrayUtil.OBJECT_EMPTY));
145                    } 
146                    catch (Exception e) {
147                            throw new PageRuntimeException(Caster.toPageException(e));
148                    }
149            }
150    
151            public DOMConfiguration getDomConfig() {
152            // dynamic load to support jre 1.4 and 1.5
153                    try {
154                            Method m = doc.getClass().getMethod("getDomConfig", new Class[]{});
155                            return (DOMConfiguration) m.invoke(doc, ArrayUtil.OBJECT_EMPTY);
156                    } 
157                    catch (Exception e) {
158                            throw new PageRuntimeException(Caster.toPageException(e));
159                    }
160            }
161    
162            public String getInputEncoding() {
163            // dynamic load to support jre 1.4 and 1.5
164                    try {
165                            Method m = doc.getClass().getMethod("getInputEncoding", new Class[]{});
166                            return Caster.toString(m.invoke(doc, ArrayUtil.OBJECT_EMPTY));
167                    } 
168                    catch (Exception e) {
169                            throw new PageRuntimeException(Caster.toPageException(e));
170                    }
171            }
172    
173            public boolean getStrictErrorChecking() {
174            // dynamic load to support jre 1.4 and 1.5
175                    try {
176                            Method m = doc.getClass().getMethod("getStrictErrorChecking", new Class[]{});
177                            return Caster.toBooleanValue(m.invoke(doc, ArrayUtil.OBJECT_EMPTY));
178                    } 
179                    catch (Exception e) {
180                            throw new PageRuntimeException(Caster.toPageException(e));
181                    }
182            }
183    
184            public String getXmlEncoding() {
185            // dynamic load to support jre 1.4 and 1.5
186                    try {
187                            Method m = doc.getClass().getMethod("getXmlEncoding", new Class[]{});
188                            return Caster.toString(m.invoke(doc, ArrayUtil.OBJECT_EMPTY));
189                    } 
190                    catch (Exception e) {
191                            throw new PageRuntimeException(Caster.toPageException(e));
192                    }
193            }
194    
195            public boolean getXmlStandalone() {
196            // dynamic load to support jre 1.4 and 1.5
197                    try {
198                            Method m = doc.getClass().getMethod("getXmlStandalone", new Class[]{});
199                            return Caster.toBooleanValue(m.invoke(doc, ArrayUtil.OBJECT_EMPTY));
200                    } 
201                    catch (Exception e) {
202                            throw new PageRuntimeException(Caster.toPageException(e));
203                    }
204            }
205    
206            public String getXmlVersion() {
207            // dynamic load to support jre 1.4 and 1.5
208                    try {
209                            Method m = doc.getClass().getMethod("getXmlVersion", new Class[]{});
210                            return Caster.toString(m.invoke(doc, ArrayUtil.OBJECT_EMPTY));
211                    } 
212                    catch (Exception e) {
213                            throw new PageRuntimeException(Caster.toPageException(e));
214                    }
215            }
216    
217            public void normalizeDocument() {
218            // dynamic load to support jre 1.4 and 1.5
219                    try {
220                            Method m = doc.getClass().getMethod("normalizeDocument", new Class[]{});
221                            m.invoke(doc, ArrayUtil.OBJECT_EMPTY);
222                    } 
223                    catch (Exception e) {
224                            throw new PageRuntimeException(Caster.toPageException(e));
225                    }
226            }
227    
228            public Node renameNode(Node arg0, String arg1, String arg2) throws DOMException {
229            // dynamic load to support jre 1.4 and 1.5
230                    try {
231                            Method m = doc.getClass().getMethod("renameNode", new Class[]{arg0.getClass(),arg1.getClass(),arg2.getClass()});
232                            return Caster.toNode(m.invoke(doc, new Object[]{arg0,arg1,arg2}));
233                    } 
234                    catch (Exception e) {
235                            throw new PageRuntimeException(Caster.toPageException(e));
236                    }
237            }
238    
239            public void setDocumentURI(String arg0) {
240            // dynamic load to support jre 1.4 and 1.5
241                    try {
242                            Method m = doc.getClass().getMethod("setDocumentURI", new Class[]{arg0.getClass()});
243                            m.invoke(doc, new Object[]{arg0});
244                    } 
245                    catch (Exception e) {
246                            throw new PageRuntimeException(Caster.toPageException(e));
247                    }
248                    
249            }
250    
251            public void setStrictErrorChecking(boolean arg0) {
252            // dynamic load to support jre 1.4 and 1.5
253                    try {
254                            Method m = doc.getClass().getMethod("setStrictErrorChecking", new Class[]{boolean.class});
255                            m.invoke(doc, new Object[]{Caster.toBoolean(arg0)});
256                    } 
257                    catch (Exception e) {
258                            throw new PageRuntimeException(Caster.toPageException(e));
259                    }
260                    
261            }
262    
263            public void setXmlStandalone(boolean arg0) throws DOMException {
264            // dynamic load to support jre 1.4 and 1.5
265                    try {
266                            Method m = doc.getClass().getMethod("setXmlStandalone", new Class[]{boolean.class});
267                            m.invoke(doc, new Object[]{Caster.toBoolean(arg0)});
268                    } 
269                    catch (Exception e) {
270                            throw new PageRuntimeException(Caster.toPageException(e));
271                    }
272                    
273            }
274    
275            public void setXmlVersion(String arg0) throws DOMException {
276            // dynamic load to support jre 1.4 and 1.5
277                    try {
278                            Method m = doc.getClass().getMethod("setXmlVersion", new Class[]{arg0.getClass()});
279                            m.invoke(doc, new Object[]{arg0});
280                    } 
281                    catch (Exception e) {
282                            throw new PageRuntimeException(Caster.toPageException(e));
283                    }
284            }
285            
286            @Override
287            public Collection duplicate(boolean deepCopy) {
288                    return new XMLDocumentStruct((Document)doc.cloneNode(deepCopy),caseSensitive);
289            }
290            
291    
292            @Override
293            public Node cloneNode(boolean deep) {
294                    return new XMLDocumentStruct((Document)doc.cloneNode(deep),caseSensitive);
295            }
296    }