001    /*
002     * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id: AbstractPartialElementImpl.java 356 2007-06-28 21:07:20Z maxberger $ */
018    
019    package net.sourceforge.jeuclid.dom;
020    
021    import java.util.HashMap;
022    import java.util.Iterator;
023    import java.util.Map;
024    import java.util.Map.Entry;
025    
026    import org.w3c.dom.Attr;
027    import org.w3c.dom.Element;
028    import org.w3c.dom.NamedNodeMap;
029    import org.w3c.dom.Node;
030    import org.w3c.dom.TypeInfo;
031    
032    /**
033     * Partial implementation of org.w3c.dom.Node.
034     * <p>
035     * This implements only the functions necessary for MathElements. Feel free to
036     * implement whatever functions you need.
037     * 
038     * @author Max Berger
039     * @version $Revision: 356 $
040     */
041    public abstract class AbstractPartialElementImpl extends
042            AbstractPartialNodeImpl implements Element {
043    
044        private final Map<String, String> attributes = new HashMap<String, String>();
045    
046        /** Partial implementation of Attr. */
047        public static class AttrImpl extends AbstractPartialNodeImpl implements
048                Attr {
049    
050            private final String name;
051    
052            private final String value;
053    
054            private final Element owner;
055    
056            /**
057             * Create a new AttrImpl Element.
058             * 
059             * @param nam
060             *            name of the Attribute.
061             * @param val
062             *            value of the attribute.
063             * @param own
064             *            owner for the attribute.
065             */
066            protected AttrImpl(final String nam, final String val,
067                    final Element own) {
068                this.name = nam;
069                this.value = val;
070                this.owner = own;
071            }
072    
073            /** {@inheritDoc} */
074            public String getName() {
075                return this.name;
076            }
077    
078            /** {@inheritDoc} */
079            public Element getOwnerElement() {
080                return this.owner;
081            }
082    
083            /** {@inheritDoc} */
084            public TypeInfo getSchemaTypeInfo() {
085                throw new UnsupportedOperationException();
086            }
087    
088            /** {@inheritDoc} */
089            public boolean getSpecified() {
090                throw new UnsupportedOperationException();
091            }
092    
093            /** {@inheritDoc} */
094            public String getValue() {
095                return this.value;
096            }
097    
098            /** {@inheritDoc} */
099            @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    }