View Javadoc

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