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