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.parser;
020:
021: import java.util.Iterator;
022: import java.util.LinkedList;
023:
024: import org.w3c.dom.svg.SVGLength;
025:
026: /**
027: * A handler class that generates an array of shorts and an array floats from
028: * parsing a length list.
029: *
030: * @author <a href="mailto:cam%40mcc%2eid%2eau">Cameron McCormack</a>
031: * @version $Id: LengthArrayProducer.java 475477 2006-11-15 22:44:28Z cam $
032: */
033: public class LengthArrayProducer extends DefaultLengthListHandler {
034:
035: /**
036: * List of <code>float[]</code> objects.
037: */
038: protected LinkedList vs;
039:
040: /**
041: * The current <code>float[]</code> object.
042: */
043: protected float[] v;
044:
045: /**
046: * List of <code>short[]</code> objects.
047: */
048: protected LinkedList us;
049:
050: /**
051: * The current <code>short[]</code> object.
052: */
053: protected short[] u;
054:
055: /**
056: * The index in which to store the next length.
057: */
058: protected int index;
059:
060: /**
061: * The total number of lengths accumulated.
062: */
063: protected int count;
064:
065: /**
066: * The unit for the current length.
067: */
068: protected short currentUnit;
069:
070: /**
071: * Returns the array of length units accumulated.
072: */
073: public short[] getLengthTypeArray() {
074: return u;
075: }
076:
077: /**
078: * Returns the array of length values accumulated.
079: */
080: public float[] getLengthValueArray() {
081: return v;
082: }
083:
084: // LengthListHandler /////////////////////////////////////////////////////
085:
086: /**
087: * Invoked when the length list attribute starts.
088: * @exception ParseException if an error occures while processing the
089: * number list.
090: */
091: public void startLengthList() throws ParseException {
092: us = new LinkedList();
093: u = new short[11];
094: vs = new LinkedList();
095: v = new float[11];
096: count = 0;
097: index = 0;
098: }
099:
100: /**
101: * Invoked when a float value has been parsed.
102: * @exception ParseException if an error occures while processing
103: * the number
104: */
105: public void numberValue(float v) throws ParseException {
106: }
107:
108: /**
109: * Implements {@link LengthHandler#lengthValue(float)}.
110: */
111: public void lengthValue(float val) throws ParseException {
112: if (index == v.length) {
113: vs.add(v);
114: v = new float[v.length * 2 + 1];
115: us.add(u);
116: u = new short[u.length * 2 + 1];
117: index = 0;
118: }
119: v[index] = val;
120: }
121:
122: /**
123: * Implements {@link LengthHandler#startLength()}.
124: */
125: public void startLength() throws ParseException {
126: currentUnit = SVGLength.SVG_LENGTHTYPE_NUMBER;
127: }
128:
129: /**
130: * Implements {@link LengthHandler#endLength()}.
131: */
132: public void endLength() throws ParseException {
133: u[index++] = currentUnit;
134: count++;
135: }
136:
137: /**
138: * Implements {@link LengthHandler#em()}.
139: */
140: public void em() throws ParseException {
141: currentUnit = SVGLength.SVG_LENGTHTYPE_EMS;
142: }
143:
144: /**
145: * Implements {@link LengthHandler#ex()}.
146: */
147: public void ex() throws ParseException {
148: currentUnit = SVGLength.SVG_LENGTHTYPE_EXS;
149: }
150:
151: /**
152: * Implements {@link LengthHandler#in()}.
153: */
154: public void in() throws ParseException {
155: currentUnit = SVGLength.SVG_LENGTHTYPE_IN;
156: }
157:
158: /**
159: * Implements {@link LengthHandler#cm()}.
160: */
161: public void cm() throws ParseException {
162: currentUnit = SVGLength.SVG_LENGTHTYPE_CM;
163: }
164:
165: /**
166: * Implements {@link LengthHandler#mm()}.
167: */
168: public void mm() throws ParseException {
169: currentUnit = SVGLength.SVG_LENGTHTYPE_MM;
170: }
171:
172: /**
173: * Implements {@link LengthHandler#pc()}.
174: */
175: public void pc() throws ParseException {
176: currentUnit = SVGLength.SVG_LENGTHTYPE_PC;
177: }
178:
179: /**
180: * Implements {@link LengthHandler#pt()}.
181: */
182: public void pt() throws ParseException {
183: currentUnit = SVGLength.SVG_LENGTHTYPE_PT;
184: }
185:
186: /**
187: * Implements {@link LengthHandler#px()}.
188: */
189: public void px() throws ParseException {
190: currentUnit = SVGLength.SVG_LENGTHTYPE_PX;
191: }
192:
193: /**
194: * Implements {@link LengthHandler#percentage()}.
195: */
196: public void percentage() throws ParseException {
197: currentUnit = SVGLength.SVG_LENGTHTYPE_PERCENTAGE;
198: }
199:
200: /**
201: * Invoked when the length list attribute ends.
202: * @exception ParseException if an error occures while processing the
203: * number list.
204: */
205: public void endLengthList() throws ParseException {
206: float[] allValues = new float[count];
207: int pos = 0;
208: Iterator it = vs.iterator();
209: while (it.hasNext()) {
210: float[] a = (float[]) it.next();
211: System.arraycopy(a, 0, allValues, pos, a.length);
212: pos += a.length;
213: }
214: System.arraycopy(v, 0, allValues, pos, index);
215: vs.clear();
216: v = allValues;
217:
218: short[] allUnits = new short[count];
219: pos = 0;
220: it = us.iterator();
221: while (it.hasNext()) {
222: short[] a = (short[]) it.next();
223: System.arraycopy(a, 0, allUnits, pos, a.length);
224: pos += a.length;
225: }
226: System.arraycopy(u, 0, allUnits, pos, index);
227: us.clear();
228: u = allUnits;
229: }
230: }
|