View Javadoc

1   /*
2    * $Id: SimpleAdapterDocument.java 440597 2006-09-06 03:34:39Z wsmoak $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.views.xslt;
19  
20  import java.util.Arrays;
21  import java.util.List;
22  
23  import org.apache.struts2.StrutsException;
24  import org.w3c.dom.Attr;
25  import org.w3c.dom.CDATASection;
26  import org.w3c.dom.Comment;
27  import org.w3c.dom.DOMConfiguration;
28  import org.w3c.dom.DOMException;
29  import org.w3c.dom.DOMImplementation;
30  import org.w3c.dom.Document;
31  import org.w3c.dom.DocumentFragment;
32  import org.w3c.dom.DocumentType;
33  import org.w3c.dom.Element;
34  import org.w3c.dom.EntityReference;
35  import org.w3c.dom.Node;
36  import org.w3c.dom.NodeList;
37  import org.w3c.dom.ProcessingInstruction;
38  import org.w3c.dom.Text;
39  
40  /***
41   * SimpleAdapterDocument adapted a Java object and presents it as
42   * a Document.  This class represents the Document container and uses
43   * the AdapterFactory to produce a child adapter for the wrapped object.
44   * The adapter produced must be of an Element type or an exception is thrown.
45   *
46   * Note: in theory we could base this on AbstractAdapterElement and then allow
47   * the wrapped object to be a more general Node type.  We would just use
48   * ourselves as the root element.  However I don't think this is an issue as
49   * people expect Documents to wrap Elements.
50   */
51  public class SimpleAdapterDocument extends AbstractAdapterNode implements Document {
52  
53      private Element rootElement;
54  
55      public SimpleAdapterDocument(
56              AdapterFactory adapterFactory, AdapterNode parent, String propertyName, Object value) {
57          setContext(adapterFactory, parent, propertyName, value);
58  
59      }
60  
61      public void setPropertyValue(Object prop) {
62          super.setPropertyValue(prop);
63          rootElement = null; // recreate the root element
64      }
65  
66      /***
67       * Lazily construct the root element adapter from the value object.
68       */
69      private Element getRootElement() {
70          if (rootElement != null)
71              return rootElement;
72  
73          Node node = getAdapterFactory().adaptNode(
74                  this, getPropertyName(), getPropertyValue());
75          if (node instanceof Element)
76              rootElement = (Element) node;
77          else
78              throw new StrutsException(
79                      "Document adapter expected to wrap an Element type.  Node is not an element:" + node);
80  
81          return rootElement;
82      }
83  
84      protected List<Node> getChildAdapters() {
85          return Arrays.asList(new Node[]{getRootElement()});
86      }
87  
88      public NodeList getChildNodes() {
89          return new NodeList() {
90              public Node item(int i) {
91                  return getRootElement();
92              }
93  
94              public int getLength() {
95                  return 1;
96              }
97          };
98      }
99  
100     public DocumentType getDoctype() {
101         return null;
102     }
103 
104     public Element getDocumentElement() {
105         return getRootElement();
106     }
107 
108     public Element getElementById(String string) {
109         return null;
110     }
111 
112     public NodeList getElementsByTagName(String string) {
113         return null;
114     }
115 
116     public NodeList getElementsByTagNameNS(String string, String string1) {
117         return null;
118     }
119 
120     public Node getFirstChild() {
121         return getRootElement();
122     }
123 
124     public DOMImplementation getImplementation() {
125         return null;
126     }
127 
128     public Node getLastChild() {
129         return getRootElement();
130     }
131 
132     public String getNodeName() {
133         return "#document";
134     }
135 
136     public short getNodeType() {
137         return Node.DOCUMENT_NODE;
138     }
139 
140     public Attr createAttribute(String string) throws DOMException {
141         return null;
142     }
143 
144     public Attr createAttributeNS(String string, String string1) throws DOMException {
145         return null;
146     }
147 
148     public CDATASection createCDATASection(String string) throws DOMException {
149         return null;
150     }
151 
152     public Comment createComment(String string) {
153         return null;
154     }
155 
156     public DocumentFragment createDocumentFragment() {
157         return null;
158     }
159 
160     public Element createElement(String string) throws DOMException {
161         return null;
162     }
163 
164     public Element createElementNS(String string, String string1) throws DOMException {
165         return null;
166     }
167 
168     public EntityReference createEntityReference(String string) throws DOMException {
169         return null;
170     }
171 
172     public ProcessingInstruction createProcessingInstruction(String string, String string1) throws DOMException {
173         return null;
174     }
175 
176     public Text createTextNode(String string) {
177         return null;
178     }
179 
180     public boolean hasChildNodes() {
181         return true;
182     }
183 
184     public Node importNode(Node node, boolean b) throws DOMException {
185         return null;
186     }
187 
188     public Node getChildAfter(Node child) {
189         return null;
190     }
191 
192     public Node getChildBefore(Node child) {
193         return null;
194     }
195 
196     // DOM level 3
197 
198     public String getInputEncoding() {
199         throw operationNotSupported();
200     }
201 
202     public String getXmlEncoding() {
203         throw operationNotSupported();
204     }
205 
206     public boolean getXmlStandalone() {
207         throw operationNotSupported();
208     }
209 
210     public void setXmlStandalone(boolean b) throws DOMException {
211         throw operationNotSupported();
212     }
213 
214     public String getXmlVersion() {
215         throw operationNotSupported();
216     }
217 
218     public void setXmlVersion(String string) throws DOMException {
219         throw operationNotSupported();
220     }
221 
222     public boolean getStrictErrorChecking() {
223         throw operationNotSupported();
224     }
225 
226     public void setStrictErrorChecking(boolean b) {
227         throw operationNotSupported();
228     }
229 
230     public String getDocumentURI() {
231         throw operationNotSupported();
232     }
233 
234     public void setDocumentURI(String string) {
235         throw operationNotSupported();
236     }
237 
238     public Node adoptNode(Node node) throws DOMException {
239         throw operationNotSupported();
240     }
241 
242     public DOMConfiguration getDomConfig() {
243         throw operationNotSupported();
244     }
245 
246     public void normalizeDocument() {
247         throw operationNotSupported();
248     }
249 
250     public Node renameNode(Node node, String string, String string1) throws DOMException {
251         return null;
252     }
253     // end DOM level 3
254 }