View Javadoc

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