View Javadoc

1   /*
2    * $Id: ProxyNodeAdapter.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 org.w3c.dom.DOMException;
25  import org.w3c.dom.NamedNodeMap;
26  import org.w3c.dom.Node;
27  
28  import com.opensymphony.xwork2.util.logging.Logger;
29  import com.opensymphony.xwork2.util.logging.LoggerFactory;
30  
31  /***
32   * ProxyNodeAdapter is a read-only delegating adapter for objects which already
33   * implement the Node interface.  All methods are proxied to the underlying
34   * Node except getParent(), getNextSibling() and getPreviousSibling(), which
35   * are implemented by the abstract adapter node to work with the parent adapter.
36   */
37  public abstract class ProxyNodeAdapter extends AbstractAdapterNode {
38  
39      private Logger log = LoggerFactory.getLogger(this.getClass());
40  
41      public ProxyNodeAdapter(AdapterFactory factory, AdapterNode parent, Node value) {
42          setContext(factory, parent, "document"/*propname unused*/, value);
43          log.debug("proxied node is: " + value);
44          log.debug("node class is: " + value.getClass());
45          log.debug("node type is: " + value.getNodeType());
46          log.debug("node name is: " + value.getNodeName());
47      }
48  
49      /***
50       * Get the proxied Node value
51       */
52      protected Node node() {
53          return (Node) getPropertyValue();
54      }
55  
56      /***
57       * Get and adapter to wrap the proxied node.
58       *
59       * @param node
60       */
61      protected Node wrap(Node node) {
62          return getAdapterFactory().proxyNode(this, node);
63      }
64  
65      protected NamedNodeMap wrap(NamedNodeMap nnm) {
66          return getAdapterFactory().proxyNamedNodeMap(this, nnm);
67      }
68      //protected NodeList wrap( NodeList nl ) { }
69  
70      //protected Node unwrap( Node child ) {
71      //  return ((ProxyNodeAdapter)child).node();
72      //}
73  
74      // Proxied Node methods
75  
76      public String getNodeName() {
77          log.trace("getNodeName");
78          return node().getNodeName();
79      }
80  
81      public String getNodeValue() throws DOMException {
82          log.trace("getNodeValue");
83          return node().getNodeValue();
84      }
85  
86      public short getNodeType() {
87          if (log.isTraceEnabled())
88              log.trace("getNodeType: " + getNodeName() + ": " + node().getNodeType());
89          return node().getNodeType();
90      }
91  
92      public NamedNodeMap getAttributes() {
93          NamedNodeMap nnm = wrap(node().getAttributes());
94          if (log.isTraceEnabled())
95              log.trace("getAttributes: " + nnm);
96          return nnm;
97      }
98  
99      public boolean hasChildNodes() {
100         log.trace("hasChildNodes");
101         return node().hasChildNodes();
102     }
103 
104     public boolean isSupported(String s, String s1) {
105         log.trace("isSupported");
106         // Is this ok?  What kind of features are they asking about?
107         return node().isSupported(s, s1);
108     }
109 
110     public String getNamespaceURI() {
111         log.trace("getNamespaceURI");
112         return node().getNamespaceURI();
113     }
114 
115     public String getPrefix() {
116         log.trace("getPrefix");
117         return node().getPrefix();
118     }
119 
120     public String getLocalName() {
121         log.trace("getLocalName");
122         return node().getLocalName();
123     }
124 
125     public boolean hasAttributes() {
126         log.trace("hasAttributes");
127         return node().hasAttributes();
128     }
129 
130     // End proxied Node methods
131 
132     public String toString() {
133         return "ProxyNode for: " + node();
134     }
135 }
136