001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.dom.svg;
020:
021: import org.apache.batik.dom.util.DoublyIndexedTable;
022:
023: /**
024: * This class is used by elements to initialize and reset their attributes.
025: *
026: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
027: * @version $Id: AttributeInitializer.java 476924 2006-11-19 21:13:26Z dvholten $
028: */
029: public class AttributeInitializer {
030:
031: /**
032: * The list of namespaces, prefixes and names.
033: */
034: protected String[] keys;
035:
036: /**
037: * The length of keys.
038: */
039: protected int length;
040:
041: /**
042: * The attribute values table.
043: */
044: protected DoublyIndexedTable values = new DoublyIndexedTable();
045:
046: /**
047: * Creates a new AttributeInitializer.
048: */
049: public AttributeInitializer(int capacity) {
050: keys = new String[capacity * 3];
051: }
052:
053: /**
054: * Adds a default attribute value to the initializer.
055: * @param ns The attribute namespace URI.
056: * @param prefix The attribute's name prefix, or null.
057: * @param ln The attribute's local name.
058: * @param val The attribute's default value.
059: */
060: public void addAttribute(String ns, String prefix, String ln,
061: String val) {
062: int len = keys.length;
063: if (length == len) {
064: String[] t = new String[len * 2];
065: System.arraycopy(keys, 0, t, 0, len);
066: keys = t;
067: }
068: keys[length++] = ns;
069: keys[length++] = prefix;
070: keys[length++] = ln;
071: values.put(ns, ln, val);
072: }
073:
074: /**
075: * Initializes the attributes of the given element.
076: */
077: public void initializeAttributes(AbstractElement elt) {
078: for (int i = length - 1; i >= 2; i -= 3) {
079: resetAttribute(elt, keys[i - 2], keys[i - 1], keys[i]);
080: }
081: }
082:
083: /**
084: * Resets an attribute of the given element to its default value.
085: * @param elt The element to modify.
086: * @param ns The attribute namespace URI.
087: * @param prefix The attribute's name prefix.
088: * @param ln The attribute's local name.
089: * @return true if a default value is known for the given attribute and
090: * if it was resetted.
091: */
092: public boolean resetAttribute(AbstractElement elt, String ns,
093: String prefix, String ln) {
094: String val = (String) values.get(ns, ln);
095: if (val == null) {
096: return false;
097: }
098: if (prefix != null) {
099: ln = prefix + ':' + ln;
100: }
101: elt.setUnspecifiedAttribute(ns, ln, val);
102: return true;
103: }
104: }
|