001: /*
002: * $Id: SimpleAdapterDocument.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.xslt;
022:
023: import java.util.Arrays;
024: import java.util.List;
025:
026: import org.apache.struts2.StrutsException;
027: import org.w3c.dom.Attr;
028: import org.w3c.dom.CDATASection;
029: import org.w3c.dom.Comment;
030: import org.w3c.dom.DOMConfiguration;
031: import org.w3c.dom.DOMException;
032: import org.w3c.dom.DOMImplementation;
033: import org.w3c.dom.Document;
034: import org.w3c.dom.DocumentFragment;
035: import org.w3c.dom.DocumentType;
036: import org.w3c.dom.Element;
037: import org.w3c.dom.EntityReference;
038: import org.w3c.dom.Node;
039: import org.w3c.dom.NodeList;
040: import org.w3c.dom.ProcessingInstruction;
041: import org.w3c.dom.Text;
042:
043: /**
044: * SimpleAdapterDocument adapted a Java object and presents it as
045: * a Document. This class represents the Document container and uses
046: * the AdapterFactory to produce a child adapter for the wrapped object.
047: * The adapter produced must be of an Element type or an exception is thrown.
048: *
049: * Note: in theory we could base this on AbstractAdapterElement and then allow
050: * the wrapped object to be a more general Node type. We would just use
051: * ourselves as the root element. However I don't think this is an issue as
052: * people expect Documents to wrap Elements.
053: */
054: public class SimpleAdapterDocument extends AbstractAdapterNode
055: implements Document {
056:
057: private Element rootElement;
058:
059: public SimpleAdapterDocument(AdapterFactory adapterFactory,
060: AdapterNode parent, String propertyName, Object value) {
061: setContext(adapterFactory, parent, propertyName, value);
062:
063: }
064:
065: public void setPropertyValue(Object prop) {
066: super .setPropertyValue(prop);
067: rootElement = null; // recreate the root element
068: }
069:
070: /**
071: * Lazily construct the root element adapter from the value object.
072: */
073: private Element getRootElement() {
074: if (rootElement != null)
075: return rootElement;
076:
077: Node node = getAdapterFactory().adaptNode(this ,
078: getPropertyName(), getPropertyValue());
079: if (node instanceof Element)
080: rootElement = (Element) node;
081: else
082: throw new StrutsException(
083: "Document adapter expected to wrap an Element type. Node is not an element:"
084: + node);
085:
086: return rootElement;
087: }
088:
089: protected List<Node> getChildAdapters() {
090: return Arrays.asList(new Node[] { getRootElement() });
091: }
092:
093: public NodeList getChildNodes() {
094: return new NodeList() {
095: public Node item(int i) {
096: return getRootElement();
097: }
098:
099: public int getLength() {
100: return 1;
101: }
102: };
103: }
104:
105: public DocumentType getDoctype() {
106: return null;
107: }
108:
109: public Element getDocumentElement() {
110: return getRootElement();
111: }
112:
113: public Element getElementById(String string) {
114: return null;
115: }
116:
117: public NodeList getElementsByTagName(String string) {
118: return null;
119: }
120:
121: public NodeList getElementsByTagNameNS(String string, String string1) {
122: return null;
123: }
124:
125: public Node getFirstChild() {
126: return getRootElement();
127: }
128:
129: public DOMImplementation getImplementation() {
130: return null;
131: }
132:
133: public Node getLastChild() {
134: return getRootElement();
135: }
136:
137: public String getNodeName() {
138: return "#document";
139: }
140:
141: public short getNodeType() {
142: return Node.DOCUMENT_NODE;
143: }
144:
145: public Attr createAttribute(String string) throws DOMException {
146: return null;
147: }
148:
149: public Attr createAttributeNS(String string, String string1)
150: throws DOMException {
151: return null;
152: }
153:
154: public CDATASection createCDATASection(String string)
155: throws DOMException {
156: return null;
157: }
158:
159: public Comment createComment(String string) {
160: return null;
161: }
162:
163: public DocumentFragment createDocumentFragment() {
164: return null;
165: }
166:
167: public Element createElement(String string) throws DOMException {
168: return null;
169: }
170:
171: public Element createElementNS(String string, String string1)
172: throws DOMException {
173: return null;
174: }
175:
176: public EntityReference createEntityReference(String string)
177: throws DOMException {
178: return null;
179: }
180:
181: public ProcessingInstruction createProcessingInstruction(
182: String string, String string1) throws DOMException {
183: return null;
184: }
185:
186: public Text createTextNode(String string) {
187: return null;
188: }
189:
190: public boolean hasChildNodes() {
191: return true;
192: }
193:
194: public Node importNode(Node node, boolean b) throws DOMException {
195: return null;
196: }
197:
198: public Node getChildAfter(Node child) {
199: return null;
200: }
201:
202: public Node getChildBefore(Node child) {
203: return null;
204: }
205:
206: // DOM level 3
207:
208: public String getInputEncoding() {
209: throw operationNotSupported();
210: }
211:
212: public String getXmlEncoding() {
213: throw operationNotSupported();
214: }
215:
216: public boolean getXmlStandalone() {
217: throw operationNotSupported();
218: }
219:
220: public void setXmlStandalone(boolean b) throws DOMException {
221: throw operationNotSupported();
222: }
223:
224: public String getXmlVersion() {
225: throw operationNotSupported();
226: }
227:
228: public void setXmlVersion(String string) throws DOMException {
229: throw operationNotSupported();
230: }
231:
232: public boolean getStrictErrorChecking() {
233: throw operationNotSupported();
234: }
235:
236: public void setStrictErrorChecking(boolean b) {
237: throw operationNotSupported();
238: }
239:
240: public String getDocumentURI() {
241: throw operationNotSupported();
242: }
243:
244: public void setDocumentURI(String string) {
245: throw operationNotSupported();
246: }
247:
248: public Node adoptNode(Node node) throws DOMException {
249: throw operationNotSupported();
250: }
251:
252: public DOMConfiguration getDomConfig() {
253: throw operationNotSupported();
254: }
255:
256: public void normalizeDocument() {
257: throw operationNotSupported();
258: }
259:
260: public Node renameNode(Node node, String string, String string1)
261: throws DOMException {
262: return null;
263: }
264: // end DOM level 3
265: }
|