001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.xslt;
006:
007: import org.w3c.dom.*;
008:
009: import java.util.List;
010: import java.util.Arrays;
011:
012: import com.opensymphony.webwork.WebWorkException;
013:
014: /**
015: * SimpleAdapterDocument adapted a Java object and presents it as
016: * a Document. This class represents the Document container and uses
017: * the AdapterFactory to produce a child adapter for the wrapped object.
018: * The adapter produced must be of an Element type or an exception is thrown.
019: *
020: * @author <a href="mailto:meier@meisterbohne.de">Philipp Meier</a>
021: * @author Pat Niemeyer (pat@pat.net)
022: */
023: /*
024: Note: in theory we could base this on AbstractAdapterElement and then allow
025: the wrapped object to be a more general Node type. We would just use
026: ourselves as the root element. However I don't think this is an issue as
027: people expect Documents to wrap Elements.
028: */
029: public class SimpleAdapterDocument extends AbstractAdapterNode
030: implements Document {
031: //~ Instance fields ////////////////////////////////////////////////////////
032:
033: private Element rootElement;
034:
035: //~ Constructors ///////////////////////////////////////////////////////////
036:
037: public SimpleAdapterDocument(AdapterFactory adapterFactory,
038: AdapterNode parent, String propertyName, Object value) {
039: setContext(adapterFactory, parent, propertyName, value);
040:
041: }
042:
043: public void setPropertyValue(Object prop) {
044: super .setPropertyValue(prop);
045: rootElement = null; // recreate the root element
046: }
047:
048: /**
049: * Lazily construct the root element adapter from the value object.
050: */
051: private Element getRootElement() {
052: if (rootElement != null)
053: return rootElement;
054:
055: Node node = getAdapterFactory().adaptNode(this ,
056: getPropertyName(), getPropertyValue());
057: if (node instanceof Element)
058: rootElement = (Element) node;
059: else
060: throw new WebWorkException(
061: "Document adapter expected to wrap an Element type. Node is not an element:"
062: + node);
063:
064: return rootElement;
065: }
066:
067: //~ Methods ////////////////////////////////////////////////////////////////
068:
069: protected List getChildAdapters() {
070: return Arrays.asList(new Object[] { getRootElement() });
071: }
072:
073: public NodeList getChildNodes() {
074: return new NodeList() {
075: public Node item(int i) {
076: return getRootElement();
077: }
078:
079: public int getLength() {
080: return 1;
081: }
082: };
083: }
084:
085: public DocumentType getDoctype() {
086: return null;
087: }
088:
089: public Element getDocumentElement() {
090: return getRootElement();
091: }
092:
093: public Element getElementById(String string) {
094: return null;
095: }
096:
097: public NodeList getElementsByTagName(String string) {
098: return null;
099: }
100:
101: public NodeList getElementsByTagNameNS(String string, String string1) {
102: return null;
103: }
104:
105: public Node getFirstChild() {
106: return getRootElement();
107: }
108:
109: public DOMImplementation getImplementation() {
110: return null;
111: }
112:
113: public Node getLastChild() {
114: return getRootElement();
115: }
116:
117: public String getNodeName() {
118: return "#document";
119: }
120:
121: public short getNodeType() {
122: return Node.DOCUMENT_NODE;
123: }
124:
125: public Attr createAttribute(String string) throws DOMException {
126: return null;
127: }
128:
129: public Attr createAttributeNS(String string, String string1)
130: throws DOMException {
131: return null;
132: }
133:
134: public CDATASection createCDATASection(String string)
135: throws DOMException {
136: return null;
137: }
138:
139: public Comment createComment(String string) {
140: return null;
141: }
142:
143: public DocumentFragment createDocumentFragment() {
144: return null;
145: }
146:
147: public Element createElement(String string) throws DOMException {
148: return null;
149: }
150:
151: public Element createElementNS(String string, String string1)
152: throws DOMException {
153: return null;
154: }
155:
156: public EntityReference createEntityReference(String string)
157: throws DOMException {
158: return null;
159: }
160:
161: public ProcessingInstruction createProcessingInstruction(
162: String string, String string1) throws DOMException {
163: return null;
164: }
165:
166: public Text createTextNode(String string) {
167: return null;
168: }
169:
170: public boolean hasChildNodes() {
171: return true;
172: }
173:
174: public Node importNode(Node node, boolean b) throws DOMException {
175: return null;
176: }
177:
178: public Node getChildAfter(Node child) {
179: return null;
180: }
181:
182: public Node getChildBefore(Node child) {
183: return null;
184: }
185:
186: // DOM level 3
187:
188: public String getInputEncoding() {
189: throw operationNotSupported();
190: }
191:
192: public String getXmlEncoding() {
193: throw operationNotSupported();
194: }
195:
196: public boolean getXmlStandalone() {
197: throw operationNotSupported();
198: }
199:
200: public void setXmlStandalone(boolean b) throws DOMException {
201: throw operationNotSupported();
202: }
203:
204: public String getXmlVersion() {
205: throw operationNotSupported();
206: }
207:
208: public void setXmlVersion(String string) throws DOMException {
209: throw operationNotSupported();
210: }
211:
212: public boolean getStrictErrorChecking() {
213: throw operationNotSupported();
214: }
215:
216: public void setStrictErrorChecking(boolean b) {
217: throw operationNotSupported();
218: }
219:
220: public String getDocumentURI() {
221: throw operationNotSupported();
222: }
223:
224: public void setDocumentURI(String string) {
225: throw operationNotSupported();
226: }
227:
228: public Node adoptNode(Node node) throws DOMException {
229: throw operationNotSupported();
230: }
231:
232: public DOMConfiguration getDomConfig() {
233: throw operationNotSupported();
234: }
235:
236: public void normalizeDocument() {
237: throw operationNotSupported();
238: }
239:
240: public Node renameNode(Node node, String string, String string1)
241: throws DOMException {
242: return null;
243: }
244: // end DOM level 3
245: }
|