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: /**
025: * A handler class that generates an array of floats from parsing a
026: * number list or a point list.
027: *
028: * @author <a href="mailto:cam%40mcc%2eid%2eau">Cameron McCormack</a>
029: * @version $Id: FloatArrayProducer.java 475477 2006-11-15 22:44:28Z cam $
030: */
031: public class FloatArrayProducer extends DefaultNumberListHandler
032: implements PointsHandler {
033:
034: /**
035: * List of <code>float[]</code> objects.
036: */
037: protected LinkedList as;
038:
039: /**
040: * The current <code>float[]</code> object.
041: */
042: protected float[] a;
043:
044: /**
045: * The index in which to store the next number.
046: */
047: protected int index;
048:
049: /**
050: * The total number of floats accumulated.
051: */
052: protected int count;
053:
054: /**
055: * Returns the array of floats accumulated.
056: */
057: public float[] getFloatArray() {
058: return a;
059: }
060:
061: // NumberListHandler /////////////////////////////////////////////////////
062:
063: /**
064: * Invoked when the number list attribute starts.
065: * @exception ParseException if an error occures while processing the
066: * number list.
067: */
068: public void startNumberList() throws ParseException {
069: as = new LinkedList();
070: a = new float[11];
071: count = 0;
072: index = 0;
073: }
074:
075: /**
076: * Invoked when a float value has been parsed.
077: * @exception ParseException if an error occures while processing
078: * the number
079: */
080: public void numberValue(float v) throws ParseException {
081: if (index == a.length) {
082: as.add(a);
083: a = new float[a.length * 2 + 1];
084: index = 0;
085: }
086: a[index++] = v;
087: count++;
088: }
089:
090: /**
091: * Invoked when the number list attribute ends.
092: * @exception ParseException if an error occures while processing the
093: * number list.
094: */
095: public void endNumberList() throws ParseException {
096: float[] all = new float[count];
097: int pos = 0;
098: Iterator it = as.iterator();
099: while (it.hasNext()) {
100: float[] b = (float[]) it.next();
101: System.arraycopy(b, 0, all, pos, b.length);
102: pos += b.length;
103: }
104: System.arraycopy(a, 0, all, pos, index);
105: as.clear();
106: a = all;
107: }
108:
109: // PointsHandler /////////////////////////////////////////////////////////
110:
111: /**
112: * Implements {@link PointsHandler#startPoints()}.
113: */
114: public void startPoints() throws ParseException {
115: startNumberList();
116: }
117:
118: /**
119: * Implements {@link PointsHandler#point(float,float)}.
120: */
121: public void point(float x, float y) throws ParseException {
122: numberValue(x);
123: numberValue(y);
124: }
125:
126: /**
127: * Implements {@link PointsHandler#endPoints()}.
128: */
129: public void endPoints() throws ParseException {
130: endNumberList();
131: }
132: }
|