~ubuntu-branches/ubuntu/oneiric/libxml-java/oneiric

« back to all changes in this revision

Viewing changes to source/org/jfree/xmlns/writer/DefaultTagDescription.java

  • Committer: Bazaar Package Importer
  • Author(s): Rene Engelhard
  • Date: 2007-08-21 00:19:22 UTC
  • Revision ID: james.westby@ubuntu.com-20070821001922-9khxrso03tk3c5i2
Tags: upstream-0.9.5
ImportĀ upstreamĀ versionĀ 0.9.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * =========================================
 
3
 * LibXML : a free Java layouting library
 
4
 * =========================================
 
5
 *
 
6
 * Project Info:  http://reporting.pentaho.org/libxml/
 
7
 *
 
8
 * (C) Copyright 2006, by Object Refinery Ltd, Pentaho Corporation and Contributors.
 
9
 *
 
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.
 
13
 *
 
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.
 
17
 *
 
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.
 
21
 *
 
22
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 
23
 * in the United States and other countries.]
 
24
 *
 
25
 *
 
26
 * ------------
 
27
 * $Id: DefaultTagDescription.java,v 1.4 2007/06/03 17:07:08 taqua Exp $
 
28
 * ------------
 
29
 * (C) Copyright 2006, by Pentaho Corporation.
 
30
 */
 
31
 
 
32
package org.jfree.xmlns.writer;
 
33
 
 
34
import java.util.HashMap;
 
35
import java.util.Iterator;
 
36
 
 
37
import org.jfree.util.Configuration;
 
38
 
 
39
/**
 
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.
 
43
 *
 
44
 * @author Thomas Morgner
 
45
 */
 
46
public class DefaultTagDescription implements TagDescription
 
47
{
 
48
  /**
 
49
   * The TagDefinitionKey is a compund key to lookup tag-specifications
 
50
   * using a namespace and tagname.
 
51
   */
 
52
  private static class TagDefinitionKey
 
53
  {
 
54
    private String namespace;
 
55
    private String tagName;
 
56
 
 
57
    /**
 
58
     * Creates a new key.
 
59
     *
 
60
     * @param namespace the namespace (can be null for undefined).
 
61
     * @param tagName the tagname (can be null for undefined).
 
62
     */
 
63
    public TagDefinitionKey(String namespace, String tagName)
 
64
    {
 
65
      this.namespace = namespace;
 
66
      this.tagName = tagName;
 
67
    }
 
68
 
 
69
    /**
 
70
     * Compares this key for equality with an other object.
 
71
     *
 
72
     * @param o the other object.
 
73
     * @return true, if this key is the same as the given object, false otherwise.
 
74
     */
 
75
    public boolean equals(Object o)
 
76
    {
 
77
      if (this == o)
 
78
      {
 
79
        return true;
 
80
      }
 
81
      if (o == null || getClass() != o.getClass())
 
82
      {
 
83
        return false;
 
84
      }
 
85
 
 
86
      final TagDefinitionKey that = (TagDefinitionKey) o;
 
87
 
 
88
      if (namespace != null ? !namespace.equals(that.namespace) : that.namespace != null)
 
89
      {
 
90
        return false;
 
91
      }
 
92
      if (tagName != null ? !tagName.equals(that.tagName) : that.tagName != null)
 
93
      {
 
94
        return false;
 
95
      }
 
96
 
 
97
      return true;
 
98
    }
 
99
 
 
100
    public int hashCode()
 
101
    {
 
102
      int result;
 
103
      result = (namespace != null ? namespace.hashCode() : 0);
 
104
      result = 29 * result + (tagName != null ? tagName.hashCode() : 0);
 
105
      return result;
 
106
    }
 
107
 
 
108
    /**
 
109
     * Computes the hashcode for this key.
 
110
     *
 
111
     * @return the hashcode.
 
112
     */
 
113
    public String toString()
 
114
    {
 
115
      return "TagDefinitionKey{" +
 
116
          "namespace='" + namespace + '\'' +
 
117
          ", tagName='" + tagName + '\'' +
 
118
          '}';
 
119
    }
 
120
  }
 
121
 
 
122
  private HashMap defaultDefinitions;
 
123
  private HashMap tagData;
 
124
  private String defaultNamespace;
 
125
 
 
126
  /**
 
127
   * A default-constructor.
 
128
   */
 
129
  public DefaultTagDescription()
 
130
  {
 
131
    defaultDefinitions = new HashMap();
 
132
    tagData = new HashMap();
 
133
  }
 
134
 
 
135
  /**
 
136
   * Configures this factory from the given configuration using the speoified
 
137
   * prefix as filter.
 
138
   *
 
139
   * @param conf   the configuration.
 
140
   * @param prefix the key-prefix.
 
141
   */
 
142
  public void configure(Configuration conf, String prefix)
 
143
  {
 
144
    final HashMap knownNamespaces = new HashMap();
 
145
 
 
146
    final String nsConfPrefix = prefix + "namespace.";
 
147
    final Iterator namespaces = conf.findPropertyKeys(nsConfPrefix);
 
148
    while (namespaces.hasNext())
 
149
    {
 
150
      String key = (String) namespaces.next();
 
151
      String nsPrefix = key.substring(nsConfPrefix.length());
 
152
      String nsUri = conf.getConfigProperty(key);
 
153
      knownNamespaces.put(nsPrefix, nsUri);
 
154
    }
 
155
 
 
156
    defaultNamespace = (String) knownNamespaces.get
 
157
        (conf.getConfigProperty(prefix + "namespace"));
 
158
 
 
159
    final String globalDefaultKey = prefix + "default";
 
160
    final boolean globalValue = "deny".equals(conf.getConfigProperty(globalDefaultKey));
 
161
    defaultDefinitions.put(null, (globalValue) ? Boolean.TRUE : Boolean.FALSE);
 
162
 
 
163
    final String nsDefaultPrefix = prefix + "default.";
 
164
    final Iterator defaults = conf.findPropertyKeys(nsDefaultPrefix);
 
165
    while (defaults.hasNext())
 
166
    {
 
167
      String key = (String) defaults.next();
 
168
      String nsPrefix = key.substring(nsDefaultPrefix.length());
 
169
      String nsUri = (String) knownNamespaces.get(nsPrefix);
 
170
      if (nsUri == null)
 
171
      {
 
172
        continue;
 
173
      }
 
174
 
 
175
      boolean value = "deny".equals(conf.getConfigProperty(key));
 
176
      defaultDefinitions.put(nsUri, (value) ? Boolean.TRUE : Boolean.FALSE);
 
177
    }
 
178
 
 
179
    final String nsTagsPrefix = prefix + "tag.";
 
180
    final Iterator tags = conf.findPropertyKeys(nsTagsPrefix);
 
181
    while (tags.hasNext())
 
182
    {
 
183
      String key = (String) tags.next();
 
184
      String tagDef = key.substring(nsTagsPrefix.length());
 
185
      final boolean value = "deny".equals(conf.getConfigProperty(key));
 
186
 
 
187
      final int delim = tagDef.indexOf('.');
 
188
      if (delim == -1)
 
189
      {
 
190
        tagData.put(new TagDefinitionKey(null, tagDef), (value) ? Boolean.TRUE : Boolean.FALSE);
 
191
      }
 
192
      else
 
193
      {
 
194
        String nsPrefix = tagDef.substring(0, delim);
 
195
        String nsUri = (String) knownNamespaces.get(nsPrefix);
 
196
        if (nsUri == null)
 
197
        {
 
198
          continue;
 
199
        }
 
200
 
 
201
        final String tagName = tagDef.substring(delim + 1);
 
202
        tagData.put(new TagDefinitionKey(nsUri, tagName), (value) ? Boolean.TRUE : Boolean.FALSE);
 
203
      }
 
204
    }
 
205
  }
 
206
 
 
207
  /**
 
208
   * Queries the defined tag-descriptions whether the given tag and namespace
 
209
   * is defined to allow character-data.
 
210
   *
 
211
   * @param namespace the namespace.
 
212
   * @param tagname the xml-tagname.
 
213
   * @return true, if the element may contain character data, false otherwise.
 
214
   */
 
215
  public boolean hasCData(String namespace, String tagname)
 
216
  {
 
217
    if (namespace == null)
 
218
    {
 
219
      namespace = defaultNamespace;
 
220
    }
 
221
 
 
222
    TagDefinitionKey key = new TagDefinitionKey(namespace, tagname);
 
223
    Object tagVal = tagData.get(key);
 
224
    if (tagVal != null)
 
225
    {
 
226
      return Boolean.TRUE.equals(tagVal);
 
227
    }
 
228
 
 
229
    final Object obj = defaultDefinitions.get(namespace);
 
230
    if (obj != null)
 
231
    {
 
232
      return Boolean.TRUE.equals(obj);
 
233
    }
 
234
 
 
235
    return Boolean.TRUE.equals(defaultDefinitions.get(null));
 
236
  }
 
237
}