View Javadoc

1   /*
2    * $Id: ProxyElementAdapter.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.ArrayList;
21  import java.util.List;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.w3c.dom.Attr;
26  import org.w3c.dom.DOMException;
27  import org.w3c.dom.Element;
28  import org.w3c.dom.Node;
29  import org.w3c.dom.NodeList;
30  import org.w3c.dom.TypeInfo;
31  
32  /***
33   * ProxyElementAdapter is a pass-through adapter for objects which already
34   * implement the Element interface.  All methods are proxied to the underlying
35   * Node except getParent(), getNextSibling() and getPreviousSibling(), which
36   * are implemented by the abstract adapter node to work with the parent adapter.
37   *
38   * Note: this class wants to be (extend) both an AbstractElementAdapter
39   * and ProxyElementAdapter, but its proxy-ness is winning right now.
40   */
41  public class ProxyElementAdapter extends ProxyNodeAdapter implements Element {
42  
43      private Log log = LogFactory.getLog(this.getClass());
44  
45      public ProxyElementAdapter(AdapterFactory factory, AdapterNode parent, Element value) {
46          super(factory, parent, value);
47      }
48  
49      /***
50       * Get the proxied Element
51       */
52      protected Element element() {
53          return (Element) getPropertyValue();
54      }
55  
56      protected List<Node> buildChildAdapters() {
57          List<Node> adapters = new ArrayList<Node>();
58          NodeList children = node().getChildNodes();
59          for (int i = 0; i < children.getLength(); i++) {
60              Node child = children.item(i);
61              Node adapter = wrap(child);
62              if (adapter != null) {
63                  log.debug("wrapped child node: " + child.getNodeName());
64                  adapters.add(adapter);
65              }
66          }
67          return adapters;
68      }
69  
70      // Proxied Element methods
71  
72      public String getTagName() {
73          return element().getTagName();
74      }
75  
76      public boolean hasAttribute(String name) {
77          return element().hasAttribute(name);
78      }
79  
80      public String getAttribute(String name) {
81          return element().getAttribute(name);
82      }
83  
84      public boolean hasAttributeNS(String namespaceURI, String localName) {
85          return element().hasAttributeNS(namespaceURI, localName);
86      }
87  
88      public Attr getAttributeNode(String name) {
89          log.debug("wrapping attribute");
90          return (Attr) wrap(element().getAttributeNode(name));
91      }
92  
93      // I'm overriding this just for clarity.  The base impl is correct.
94      public NodeList getElementsByTagName(String name) {
95          return super.getElementsByTagName(name);
96      }
97  
98      public String getAttributeNS(String namespaceURI, String localName) {
99          return element().getAttributeNS(namespaceURI, localName);
100     }
101 
102     public Attr getAttributeNodeNS(String namespaceURI, String localName) {
103         return (Attr) wrap(element().getAttributeNodeNS(namespaceURI, localName));
104     }
105 
106     public NodeList getElementsByTagNameNS(String namespaceURI, String localName) {
107         return super.getElementsByTagNameNS(namespaceURI, localName);
108     }
109 
110     // Unsupported mutators of Element
111 
112     public void removeAttribute(String name) throws DOMException {
113         throw new UnsupportedOperationException();
114     }
115 
116     public void removeAttributeNS(String namespaceURI, String localName) throws DOMException {
117         throw new UnsupportedOperationException();
118     }
119 
120     public void setAttribute(String name, String value) throws DOMException {
121         throw new UnsupportedOperationException();
122     }
123 
124     public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
125         throw new UnsupportedOperationException();
126     }
127 
128     public Attr setAttributeNode(Attr newAttr) throws DOMException {
129         throw new UnsupportedOperationException();
130     }
131 
132     public Attr setAttributeNodeNS(Attr newAttr) throws DOMException {
133         throw new UnsupportedOperationException();
134     }
135 
136     public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException {
137         throw new UnsupportedOperationException();
138     }
139 
140     // end proxied Element methods
141 
142     // unsupported DOM level 3 methods
143 
144     public TypeInfo getSchemaTypeInfo() {
145         throw operationNotSupported();
146     }
147 
148     public void setIdAttribute(String string, boolean b) throws DOMException {
149         throw operationNotSupported();
150     }
151 
152     public void setIdAttributeNS(String string, String string1, boolean b) throws DOMException {
153         throw operationNotSupported();
154     }
155 
156     public void setIdAttributeNode(Attr attr, boolean b) throws DOMException {
157         throw operationNotSupported();
158     }
159 
160     // end DOM level 3 methods
161 
162     public String toString() {
163         return "ProxyElement for: " + element();
164     }
165 }