1 /*
2 * Copyright 2002 - 2006 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: AbstractAttributeMap.java 310 2007-05-18 20:26:36Z maxberger $ */
18
19 package net.sourceforge.jeuclid.elements.support.attributes;
20
21 /**
22 * Generic class for reading and parsing attributes.
23 *
24 * @author Max Berger
25 * @version $Revision: 310 $
26 */
27 public abstract class AbstractAttributeMap implements AttributeMap {
28
29 private static final String MATHMLNAMESPACE = "http://www.w3.org/1998/Math/MathML";
30
31 /**
32 * Override this method to get an attribute value without a namespace.
33 *
34 * @param attrName
35 * name of the attribute
36 * @return value of the attribute
37 */
38 protected abstract String getAttribute(String attrName);
39
40 /**
41 * Override this method to get an attribtue with a namespace.
42 *
43 * @param namespace
44 * the namespace
45 * @param attrName
46 * the attribtue name
47 * @return value of the attribute.
48 */
49 protected abstract String getAttributeNS(String namespace, String attrName);
50
51 /** {@inheritDoc} */
52 public boolean hasAttribute(final String attrName) {
53 return this.getString(attrName) != null;
54 }
55
56 /** {@inheritDoc} */
57 public String getString(final String attrName) {
58 String attrValue = this.getAttributeNS(
59 AbstractAttributeMap.MATHMLNAMESPACE, attrName);
60 if (attrValue == null) {
61 attrValue = this.getAttribute(attrName);
62 }
63 return attrValue;
64 }
65
66 /** {@inheritDoc} */
67 public String getString(final String attrName, final String defaultValue) {
68 String attrValue = this.getString(attrName);
69 if (attrValue == null) {
70 attrValue = defaultValue;
71 }
72 return attrValue;
73 }
74
75 /** {@inheritDoc} */
76 public boolean getBoolean(final String attrName,
77 final boolean defaultValue) {
78 final String strValue = this.getString(attrName, Boolean
79 .toString(defaultValue));
80 return Boolean.parseBoolean(strValue);
81 }
82
83 }