View Javadoc

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