View Javadoc

1   /*
2    * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: AbstractPartialElementImpl.java 356 2007-06-28 21:07:20Z maxberger $ */
18  
19  package net.sourceforge.jeuclid.dom;
20  
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  import java.util.Map.Entry;
25  
26  import org.w3c.dom.Attr;
27  import org.w3c.dom.Element;
28  import org.w3c.dom.NamedNodeMap;
29  import org.w3c.dom.Node;
30  import org.w3c.dom.TypeInfo;
31  
32  /**
33   * Partial implementation of org.w3c.dom.Node.
34   * <p>
35   * This implements only the functions necessary for MathElements. Feel free to
36   * implement whatever functions you need.
37   * 
38   * @author Max Berger
39   * @version $Revision: 356 $
40   */
41  public abstract class AbstractPartialElementImpl extends
42          AbstractPartialNodeImpl implements Element {
43  
44      private final Map<String, String> attributes = new HashMap<String, String>();
45  
46      /** Partial implementation of Attr. */
47      public static class AttrImpl extends AbstractPartialNodeImpl implements
48              Attr {
49  
50          private final String name;
51  
52          private final String value;
53  
54          private final Element owner;
55  
56          /**
57           * Create a new AttrImpl Element.
58           * 
59           * @param nam
60           *            name of the Attribute.
61           * @param val
62           *            value of the attribute.
63           * @param own
64           *            owner for the attribute.
65           */
66          protected AttrImpl(final String nam, final String val,
67                  final Element own) {
68              this.name = nam;
69              this.value = val;
70              this.owner = own;
71          }
72  
73          /** {@inheritDoc} */
74          public String getName() {
75              return this.name;
76          }
77  
78          /** {@inheritDoc} */
79          public Element getOwnerElement() {
80              return this.owner;
81          }
82  
83          /** {@inheritDoc} */
84          public TypeInfo getSchemaTypeInfo() {
85              throw new UnsupportedOperationException();
86          }
87  
88          /** {@inheritDoc} */
89          public boolean getSpecified() {
90              throw new UnsupportedOperationException();
91          }
92  
93          /** {@inheritDoc} */
94          public String getValue() {
95              return this.value;
96          }
97  
98          /** {@inheritDoc} */
99          @Override
100         public final String getNodeValue() {
101             return this.value;
102         }
103 
104         /** {@inheritDoc} */
105         public boolean isId() {
106             throw new UnsupportedOperationException();
107         }
108 
109         /** {@inheritDoc} */
110         public void setValue(final String val) {
111             throw new UnsupportedOperationException();
112         }
113 
114         /** {@inheritDoc} */
115         public String getNodeName() {
116             return this.name;
117         }
118 
119         /** {@inheritDoc} */
120         public short getNodeType() {
121             return Node.ATTRIBUTE_NODE;
122         }
123     }
124 
125     /** Partial Implementation for an NodeMap describing Attributes. */
126     public static class AttributeNodeMap implements NamedNodeMap {
127 
128         private final Map<String, String> attributes;
129 
130         private final Element owner;
131 
132         /**
133          * Creates a new AttributeNodeMap.
134          * 
135          * @param attrs
136          *            A Map containing attributes and values.
137          * @param parent
138          *            the node these attributes belong to.
139          */
140         protected AttributeNodeMap(final Map<String, String> attrs,
141                 final Element parent) {
142             this.attributes = attrs;
143             this.owner = parent;
144         }
145 
146         /** {@inheritDoc} */
147         public int getLength() {
148             return this.attributes.size();
149         }
150 
151         /** {@inheritDoc} */
152         public Node getNamedItem(final String name) {
153             final String value = this.attributes.get(name);
154             if (value == null) {
155                 return null;
156             } else {
157                 return new AttrImpl(name, value, this.owner);
158             }
159         }
160 
161         /** {@inheritDoc} */
162         public Node getNamedItemNS(final String namespaceURI,
163                 final String localName) {
164             return this.getNamedItem(localName);
165         }
166 
167         /** {@inheritDoc} */
168         public Node item(final int index) {
169 
170             final Iterator<Entry<String, String>> it = this.attributes
171                     .entrySet().iterator();
172             for (int i = 0; i < index; i++) {
173                 it.next();
174             }
175             final Entry<String, String> found = it.next();
176             return new AttrImpl(found.getKey(), found.getValue(), this.owner);
177         }
178 
179         /** {@inheritDoc} */
180         public Node removeNamedItem(final String name) {
181             throw new UnsupportedOperationException();
182         }
183 
184         /** {@inheritDoc} */
185         public Node removeNamedItemNS(final String namespaceURI,
186                 final String localName) {
187             throw new UnsupportedOperationException();
188         }
189 
190         /** {@inheritDoc} */
191         public Node setNamedItem(final Node arg) {
192             throw new UnsupportedOperationException();
193         }
194 
195         /** {@inheritDoc} */
196         public Node setNamedItemNS(final Node arg) {
197             throw new UnsupportedOperationException();
198         }
199     }
200 
201     /** {@inheritDoc} */
202     public final String getAttribute(final String name) {
203         return this.attributes.get(name);
204     }
205 
206     /** {@inheritDoc} */
207     public final String getAttributeNS(final String namespaceURI,
208             final String localName) {
209         return this.attributes.get(localName);
210     }
211 
212     /** {@inheritDoc} */
213     public void setAttribute(final String name, final String value) {
214         this.attributes.put(name, value);
215     }
216 
217     /** {@inheritDoc} */
218     public final Attr getAttributeNode(final String name) {
219         throw new UnsupportedOperationException("getAttributeNode");
220     }
221 
222     /** {@inheritDoc} */
223     public final Attr getAttributeNodeNS(final String namespaceURI,
224             final String localName) {
225         throw new UnsupportedOperationException("getAttributeNodeNS");
226     }
227 
228     /** {@inheritDoc} */
229     public final org.w3c.dom.NodeList getElementsByTagName(final String name) {
230         throw new UnsupportedOperationException("getElementsByTagName");
231     }
232 
233     /** {@inheritDoc} */
234     public final org.w3c.dom.NodeList getElementsByTagNameNS(
235             final String namespaceURI, final String localName) {
236         throw new UnsupportedOperationException("getElementsByTagNameNS");
237     }
238 
239     /** {@inheritDoc} */
240     public final TypeInfo getSchemaTypeInfo() {
241         throw new UnsupportedOperationException("getSchemaTypeInfo");
242     }
243 
244     /** {@inheritDoc} */
245     public final boolean hasAttribute(final String name) {
246         throw new UnsupportedOperationException("hasAttribute");
247     }
248 
249     /** {@inheritDoc} */
250     public final boolean hasAttributeNS(final String namespaceURI,
251             final String localName) {
252         throw new UnsupportedOperationException("hasAttributeNS");
253     }
254 
255     /** {@inheritDoc} */
256     public final void removeAttribute(final String name) {
257         throw new UnsupportedOperationException("removeAttribute");
258     }
259 
260     /** {@inheritDoc} */
261     public final void removeAttributeNS(final String namespaceURI,
262             final String localName) {
263         throw new UnsupportedOperationException("removeAttributeNS");
264     }
265 
266     /** {@inheritDoc} */
267     public final Attr removeAttributeNode(final Attr oldAttr) {
268         throw new UnsupportedOperationException("removeAttributeNode");
269     }
270 
271     /** {@inheritDoc} */
272     public final void setAttributeNS(final String namespaceURI,
273             final String qualifiedName, final String value) {
274         throw new UnsupportedOperationException("setAttributeNS");
275     }
276 
277     /** {@inheritDoc} */
278     public final Attr setAttributeNode(final Attr newAttr) {
279         throw new UnsupportedOperationException("setAttributeNode");
280     }
281 
282     /** {@inheritDoc} */
283     public final Attr setAttributeNodeNS(final Attr newAttr) {
284         throw new UnsupportedOperationException("setAttributeNodeNS");
285     }
286 
287     /** {@inheritDoc} */
288     public final void setIdAttribute(final String name, final boolean isId) {
289         throw new UnsupportedOperationException("setIdAttribute");
290     }
291 
292     /** {@inheritDoc} */
293     public final void setIdAttributeNS(final String namespaceURI,
294             final String localName, final boolean isId) {
295         throw new UnsupportedOperationException("setIdAttributeNS");
296     }
297 
298     /** {@inheritDoc} */
299     public final void setIdAttributeNode(final Attr idAttr, final boolean isId) {
300         throw new UnsupportedOperationException("setIdAttributeNode");
301     }
302 
303     /** {@inheritDoc} */
304     public final String getNodeName() {
305         return this.getTagName();
306     }
307 
308     /** {@inheritDoc} */
309     @Override
310     public NamedNodeMap getAttributes() {
311         return new AttributeNodeMap(this.attributes, this);
312     }
313 
314     /** {@inheritDoc} */
315     @Override
316     public final String getLocalName() {
317         return this.getTagName();
318     }
319 
320     /** {@inheritDoc} */
321     public final short getNodeType() {
322         return Node.ELEMENT_NODE;
323     }
324 
325     /** {@inheritDoc} */
326     @Override
327     public final String toString() {
328         final StringBuilder builder = new StringBuilder(this.getTagName());
329         for (final Map.Entry<String, String> attrs : this.attributes
330                 .entrySet()) {
331             builder.append(" " + attrs.getKey() + "='" + attrs.getValue()
332                     + "'");
333         }
334         return builder.toString();
335     }
336 
337 }