KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > svg > AttributeInitializer


1 /*
2
3    Copyright 2001 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.dom.svg;
19
20 import org.apache.batik.dom.util.DoublyIndexedTable;
21
22 /**
23  * This class is used by elements to initialize and reset their attributes.
24  *
25  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
26  * @version $Id: AttributeInitializer.java,v 1.3 2004/08/18 07:13:13 vhardy Exp $
27  */

28 public class AttributeInitializer {
29
30     /**
31      * The list of namespaces, prefixes and names.
32      */

33     protected String JavaDoc[] keys;
34
35     /**
36      * The length of keys.
37      */

38     protected int length;
39
40     /**
41      * The attribute values table.
42      */

43     protected DoublyIndexedTable values = new DoublyIndexedTable();
44     
45     /**
46      * Creates a new AttributeInitializer.
47      */

48     public AttributeInitializer(int capacity) {
49         keys = new String JavaDoc[capacity * 3];
50     }
51
52     /**
53      * Adds a default attribute value to the initializer.
54      * @param ns The attribute namespace URI.
55      * @param prefix The attribute's name prefix, or null.
56      * @param ln The attribute's local name.
57      * @param val The attribute's default value.
58      */

59     public void addAttribute(String JavaDoc ns, String JavaDoc prefix, String JavaDoc ln, String JavaDoc val) {
60         int len = keys.length;
61         if (length == len) {
62             String JavaDoc[] t = new String JavaDoc[len * 2];
63             for (int i = len - 1; i >= 0; --i) {
64                 t[i] = keys[i];
65             }
66             keys = t;
67         }
68         keys[length++] = ns;
69         keys[length++] = prefix;
70         keys[length++] = ln;
71         values.put(ns, ln, val);
72     }
73
74     /**
75      * Initializes the attributes of the given element.
76      */

77     public void initializeAttributes(AbstractElement elt) {
78         for (int i = length - 1; i >= 2; i -= 3) {
79             resetAttribute(elt, keys[i - 2], keys[i - 1], keys[i]);
80         }
81     }
82
83     /**
84      * Resets an attribute of the given element to its default value.
85      * @param elt The element to modify.
86      * @param ns The attribute namespace URI.
87      * @param prefix The attribute's name prefix.
88      * @param ln The attribute's local name.
89      * @return true if a default value is known for the given attribute and
90      * if it was resetted.
91      */

92     public boolean resetAttribute(AbstractElement elt,
93                                   String JavaDoc ns, String JavaDoc prefix, String JavaDoc ln) {
94         String JavaDoc val = (String JavaDoc)values.get(ns, ln);
95         if (val == null) {
96             return false;
97         }
98         if (prefix != null) {
99             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(prefix.length() + ln.length() + 1);
100             sb.append(prefix).append(':').append(ln);
101             ln = sb.toString();
102         }
103         elt.setUnspecifiedAttribute(ns, ln, val);
104         return true;
105     }
106 }
107
Popular Tags