View Javadoc

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