2
* =========================================
3
* LibXML : a free Java layouting library
4
* =========================================
6
* Project Info: http://reporting.pentaho.org/libxml/
8
* (C) Copyright 2006, by Object Refinery Ltd, Pentaho Corporation and Contributors.
10
* This library is free software; you can redistribute it and/or modify it under the terms
11
* of the GNU Lesser General Public License as published by the Free Software Foundation;
12
* either version 2.1 of the License, or (at your option) any later version.
14
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
* See the GNU Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public License along with this
19
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
* Boston, MA 02111-1307, USA.
22
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
23
* in the United States and other countries.]
27
* $Id: DefaultTagDescription.java,v 1.4 2007/06/03 17:07:08 taqua Exp $
29
* (C) Copyright 2006, by Pentaho Corporation.
32
package org.jfree.xmlns.writer;
34
import java.util.HashMap;
35
import java.util.Iterator;
37
import org.jfree.util.Configuration;
40
* A tag-description provides information about xml tags. At the moment, we
41
* simply care whether an element can contain CDATA. In such cases, we do not
42
* indent the inner elements.
44
* @author Thomas Morgner
46
public class DefaultTagDescription implements TagDescription
49
* The TagDefinitionKey is a compund key to lookup tag-specifications
50
* using a namespace and tagname.
52
private static class TagDefinitionKey
54
private String namespace;
55
private String tagName;
60
* @param namespace the namespace (can be null for undefined).
61
* @param tagName the tagname (can be null for undefined).
63
public TagDefinitionKey(String namespace, String tagName)
65
this.namespace = namespace;
66
this.tagName = tagName;
70
* Compares this key for equality with an other object.
72
* @param o the other object.
73
* @return true, if this key is the same as the given object, false otherwise.
75
public boolean equals(Object o)
81
if (o == null || getClass() != o.getClass())
86
final TagDefinitionKey that = (TagDefinitionKey) o;
88
if (namespace != null ? !namespace.equals(that.namespace) : that.namespace != null)
92
if (tagName != null ? !tagName.equals(that.tagName) : that.tagName != null)
100
public int hashCode()
103
result = (namespace != null ? namespace.hashCode() : 0);
104
result = 29 * result + (tagName != null ? tagName.hashCode() : 0);
109
* Computes the hashcode for this key.
111
* @return the hashcode.
113
public String toString()
115
return "TagDefinitionKey{" +
116
"namespace='" + namespace + '\'' +
117
", tagName='" + tagName + '\'' +
122
private HashMap defaultDefinitions;
123
private HashMap tagData;
124
private String defaultNamespace;
127
* A default-constructor.
129
public DefaultTagDescription()
131
defaultDefinitions = new HashMap();
132
tagData = new HashMap();
136
* Configures this factory from the given configuration using the speoified
139
* @param conf the configuration.
140
* @param prefix the key-prefix.
142
public void configure(Configuration conf, String prefix)
144
final HashMap knownNamespaces = new HashMap();
146
final String nsConfPrefix = prefix + "namespace.";
147
final Iterator namespaces = conf.findPropertyKeys(nsConfPrefix);
148
while (namespaces.hasNext())
150
String key = (String) namespaces.next();
151
String nsPrefix = key.substring(nsConfPrefix.length());
152
String nsUri = conf.getConfigProperty(key);
153
knownNamespaces.put(nsPrefix, nsUri);
156
defaultNamespace = (String) knownNamespaces.get
157
(conf.getConfigProperty(prefix + "namespace"));
159
final String globalDefaultKey = prefix + "default";
160
final boolean globalValue = "deny".equals(conf.getConfigProperty(globalDefaultKey));
161
defaultDefinitions.put(null, (globalValue) ? Boolean.TRUE : Boolean.FALSE);
163
final String nsDefaultPrefix = prefix + "default.";
164
final Iterator defaults = conf.findPropertyKeys(nsDefaultPrefix);
165
while (defaults.hasNext())
167
String key = (String) defaults.next();
168
String nsPrefix = key.substring(nsDefaultPrefix.length());
169
String nsUri = (String) knownNamespaces.get(nsPrefix);
175
boolean value = "deny".equals(conf.getConfigProperty(key));
176
defaultDefinitions.put(nsUri, (value) ? Boolean.TRUE : Boolean.FALSE);
179
final String nsTagsPrefix = prefix + "tag.";
180
final Iterator tags = conf.findPropertyKeys(nsTagsPrefix);
181
while (tags.hasNext())
183
String key = (String) tags.next();
184
String tagDef = key.substring(nsTagsPrefix.length());
185
final boolean value = "deny".equals(conf.getConfigProperty(key));
187
final int delim = tagDef.indexOf('.');
190
tagData.put(new TagDefinitionKey(null, tagDef), (value) ? Boolean.TRUE : Boolean.FALSE);
194
String nsPrefix = tagDef.substring(0, delim);
195
String nsUri = (String) knownNamespaces.get(nsPrefix);
201
final String tagName = tagDef.substring(delim + 1);
202
tagData.put(new TagDefinitionKey(nsUri, tagName), (value) ? Boolean.TRUE : Boolean.FALSE);
208
* Queries the defined tag-descriptions whether the given tag and namespace
209
* is defined to allow character-data.
211
* @param namespace the namespace.
212
* @param tagname the xml-tagname.
213
* @return true, if the element may contain character data, false otherwise.
215
public boolean hasCData(String namespace, String tagname)
217
if (namespace == null)
219
namespace = defaultNamespace;
222
TagDefinitionKey key = new TagDefinitionKey(namespace, tagname);
223
Object tagVal = tagData.get(key);
226
return Boolean.TRUE.equals(tagVal);
229
final Object obj = defaultDefinitions.get(namespace);
232
return Boolean.TRUE.equals(obj);
235
return Boolean.TRUE.equals(defaultDefinitions.get(null));