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.DOMException;
030import org.w3c.dom.Element;
031import org.w3c.dom.Node;
032import org.w3c.dom.NodeList;
033import org.w3c.dom.TypeInfo;
034
035
036/**
037 * 
038 */
039public class XMLElementStruct extends XMLNodeStruct implements Element {
040        
041        
042        private Element element;
043    
044        /**
045         * constructor of the class
046         * @param element
047         * @param caseSensitive
048         */
049        protected XMLElementStruct(Element element, boolean caseSensitive) {
050                super(element instanceof XMLElementStruct?element=((XMLElementStruct)element).getElement():element, caseSensitive);
051        this.element=element;
052        }
053        
054        @Override
055        public String getTagName() {
056                return element.getTagName();
057        }
058        @Override
059        public void removeAttribute(String name) throws DOMException {
060                element.removeAttribute(name);
061        }
062        @Override
063        public boolean hasAttribute(String name) {
064                return element.hasAttribute(name);
065        }
066        @Override
067        public String getAttribute(String name) {
068                return element.getAttribute(name);
069        }
070        @Override
071        public void removeAttributeNS(String namespaceURI, String localName) throws DOMException {
072                element.removeAttributeNS(namespaceURI,localName);
073        }
074        @Override
075        public void setAttribute(String name, String value) throws DOMException {
076                element.setAttribute(name,value);
077        }
078        @Override
079        public boolean hasAttributeNS(String namespaceURI, String localName) {
080                return element.hasAttributeNS(namespaceURI,localName);
081        }
082        @Override
083        public Attr getAttributeNode(String name) {
084                return element.getAttributeNode(name);
085        }
086        @Override
087        public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
088                return element.removeAttributeNode(oldAttr);
089        }
090        @Override
091        public Attr setAttributeNode(Attr newAttr) throws DOMException {
092                return element.setAttributeNode(newAttr);
093        }
094        @Override
095        public Attr setAttributeNodeNS(Attr newAttr) throws DOMException {
096                return element.setAttributeNodeNS(newAttr);
097        }
098        @Override
099        public NodeList getElementsByTagName(String name) {
100                return element.getElementsByTagName(name);
101        }
102        @Override
103        public String getAttributeNS(String namespaceURI, String localName) {
104                return element.getAttributeNS(namespaceURI,localName);
105        }
106        @Override
107        public void setAttributeNS(String namespaceURI, String qualifiedName,String value) throws DOMException {
108                element.setAttributeNS(namespaceURI,qualifiedName,value);
109        }
110        @Override
111        public Attr getAttributeNodeNS(String namespaceURI, String localName) {
112                return element.getAttributeNodeNS(namespaceURI,localName);
113        }
114        @Override
115        public NodeList getElementsByTagNameNS(String namespaceURI, String localName) {
116                return element.getElementsByTagNameNS(namespaceURI,localName);
117        }
118        
119    public void setIdAttribute(String name, boolean isId) throws DOMException {
120        // dynamic load to support jre 1.4 and 1.5
121                try {
122                        Method m = element.getClass().getMethod("setIdAttribute", new Class[]{name.getClass(),boolean.class});
123                        m.invoke(element, new Object[]{name,Caster.toBoolean(isId)});
124                } 
125                catch (Exception e) {
126                        throw new PageRuntimeException(Caster.toPageException(e));
127                }
128    }
129    
130    public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException {
131        // dynamic load to support jre 1.4 and 1.5
132                try {
133                        Method m = element.getClass().getMethod("setIdAttributeNS", new Class[]{namespaceURI.getClass(),localName.getClass(),boolean.class});
134                        m.invoke(element, new Object[]{namespaceURI,localName,Caster.toBoolean(isId)});
135                } 
136                catch (Exception e) {
137                        throw new PageRuntimeException(Caster.toPageException(e));
138                }
139    }
140    
141    public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException {
142        // dynamic load to support jre 1.4 and 1.5
143                try {
144                        Method m = element.getClass().getMethod("setIdAttributeNode", new Class[]{idAttr.getClass(),boolean.class});
145                        m.invoke(element, new Object[]{idAttr,Caster.toBoolean(isId)});
146                } 
147                catch (Exception e) {
148                element.setAttributeNodeNS(idAttr);
149                }
150    }
151    
152        public TypeInfo getSchemaTypeInfo() {
153        // dynamic load to support jre 1.4 and 1.5
154                try {
155                        Method m = element.getClass().getMethod("getSchemaTypeInfo", new Class[]{});
156                        return (TypeInfo) m.invoke(element, ArrayUtil.OBJECT_EMPTY);
157                } 
158                catch (Exception e) {
159                        throw new PageRuntimeException(Caster.toPageException(e));
160                }
161        }
162        /**
163         * @return the element
164         */
165        public Element getElement() {
166                return element;
167        }
168        
169        @Override
170        public Collection duplicate(boolean deepCopy) {
171                return new XMLElementStruct((Element)element.cloneNode(deepCopy),caseSensitive);
172        }
173        
174
175        @Override
176        public Node cloneNode(boolean deep) {
177                return new XMLElementStruct((Element)element.cloneNode(deep),caseSensitive);
178        }
179}