[FOray-commit] SF.net SVN: foray:[10831] trunk/foray
Modular XSL-FO Implementation for Java.
Status: Alpha
Brought to you by:
victormote
|
From: <vic...@us...> - 2009-04-30 15:17:47
|
Revision: 10831
http://foray.svn.sourceforge.net/foray/?rev=10831&view=rev
Author: victormote
Date: 2009-04-30 15:17:46 +0000 (Thu, 30 Apr 2009)
Log Message:
-----------
Use lists instead of arrays to avoid needing to read the file twice.
Modified Paths:
--------------
trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingParser.java
Added Paths:
-----------
trunk/foray/foray-common/src/java/org/foray/common/CharacterUtil.java
Added: trunk/foray/foray-common/src/java/org/foray/common/CharacterUtil.java
===================================================================
--- trunk/foray/foray-common/src/java/org/foray/common/CharacterUtil.java (rev 0)
+++ trunk/foray/foray-common/src/java/org/foray/common/CharacterUtil.java 2009-04-30 15:17:46 UTC (rev 10831)
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2009 The FOray Project.
+ * http://www.foray.org
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * This work is in part derived from the following work(s), used with the
+ * permission of the licensor:
+ * Apache FOP, licensed by the Apache Software Foundation
+ *
+ */
+
+/*
+ * $LastChangedRevision$
+ * $LastChangedDate$
+ * $LastChangedBy$
+ */
+
+package org.foray.common;
+
+import java.util.List;
+
+/**
+ * Utilities related to char and Character.
+ */
+public final class CharacterUtil {
+
+ /**
+ * Private constructor. This is a utility class, and should never be instantiated.
+ */
+ private CharacterUtil() { }
+
+ /**
+ * Converts a List of Character instances into an array of chars.
+ * @param list The List of Character instances to be converted to an array.
+ * @return The array of chars.
+ */
+ public static char[] toArray(final List<Character> list) {
+ if (list == null) {
+ return new char[0];
+ }
+ final char[] returnArray = new char[list.size()];
+ for (int i = 0; i < list.size(); i++) {
+ returnArray[i] = list.get(i);
+ }
+ return returnArray;
+ }
+
+}
Property changes on: trunk/foray/foray-common/src/java/org/foray/common/CharacterUtil.java
___________________________________________________________________
Added: svn:keywords
+ "Author Id Rev Date URL"
Added: svn:eol-style
+ native
Modified: trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingParser.java
===================================================================
--- trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingParser.java 2009-04-30 14:23:39 UTC (rev 10830)
+++ trunk/foray/foray-ps/src/java/org/foray/ps/encode/EncodingParser.java 2009-04-30 15:17:46 UTC (rev 10831)
@@ -28,6 +28,7 @@
package org.foray.ps.encode;
+import org.foray.common.CharacterUtil;
import org.foray.common.Logging;
import org.foray.common.RandomReader;
import org.foray.common.StringUtil;
@@ -42,6 +43,8 @@
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
import java.util.StringTokenizer;
/**
@@ -134,21 +137,21 @@
/** The array of glyph lists that can be used to map names to codepoints. */
private GlyphList4a[] glyphListsToCheck = null;
- /** The names of the glyphs parsed from the encoding. */
- private String[] glyphNames;
-
/** The radix which should be used for parsing. For base-10 numbers, this
* value should be 10. For octal numbers, it should be 8. */
private int radix;
+ /** The names of the glyphs parsed from the encoding. */
+ private List<String> glyphNames = new ArrayList<String>();
+
/** The parsed glyph indexes. */
- private char[] glyphIndexes;
+ private List<Character> glyphIndexes = new ArrayList<Character>();
/** The parsed Unicode codepoints. */
- private char[] codePoints;
+ private List<Character> codePoints = new ArrayList<Character>();
/** The parsed encoded indexes. */
- private char[] codePointIndexes;
+ private List<Character> codePointIndexes = new ArrayList<Character>();
/**
* Create a new EncodingParser instance.
@@ -179,42 +182,13 @@
* @throws IOException For I/O Errors.
*/
public void parseList() throws IOException {
- final int numberOfLines = countLines();
- if (numberOfLines < 1) {
- return;
- }
- this.glyphNames = new String[numberOfLines];
- this.glyphIndexes = new char[numberOfLines];
parseLines();
sortGlyphNames();
createCodePoints();
- EncodingVector4a.sortCodePoints(this.codePoints, this.codePointIndexes);
+ EncodingVector4a.sortCodePoints(this.getCodePoints(), this.getCodePointIndexes());
}
/**
- * Counts the number of content lines in the input file.
- * @return The number of content lines in the input file.
- * @throws IOException For I/O Exception.
- */
- private int countLines() throws IOException {
- int count = 0;
- this.reader.seek(0);
- boolean endOfFile = false;
- while (! endOfFile) {
- final String currentLine = this.reader.readLine();
- if (currentLine == null) {
- endOfFile = true;
- break;
- }
- if (lineHasContent(currentLine)
- && lineAppliesToEncoding(currentLine)) {
- count++;
- }
- }
- return count;
- }
-
- /**
* Parses the lines of the file.
* @throws IOException For I/O Error.
*/
@@ -286,8 +260,13 @@
* data.
*/
private void processCurrentLine(final String line, final int arrayIndex) {
+ this.glyphNames.add(StringUtil.EMPTY_STRING);
+ this.glyphIndexes.add(Character.MIN_VALUE);
+ this.codePoints.add(Character.MIN_VALUE);
+ this.codePointIndexes.add(Character.MIN_VALUE);
+
final String[] tokens = tokenizeLine(line);
- this.glyphNames[arrayIndex] = tokens[0];
+ this.glyphNames.set(arrayIndex, tokens[0]);
final String numericString = tokens[this.columnNum];
int index = -1;
try {
@@ -303,7 +282,7 @@
if (index < 0) {
index = 0;
}
- this.glyphIndexes[arrayIndex] = (char) index;
+ this.glyphIndexes.set(arrayIndex, (char) index);
}
/**
@@ -331,18 +310,17 @@
boolean anyChanges = true;
while (anyChanges) {
anyChanges = false;
- for (int i = 0; i < this.glyphNames.length - 1; i++) {
- final int compareValue = this.glyphNames[i].compareTo(
- this.glyphNames[i + 1]);
+ for (int i = 0; i < this.glyphNames.size() - 1; i++) {
+ final int compareValue = this.glyphNames.get(i).compareTo(this.glyphNames.get(i + 1));
if (compareValue > 0) {
/* Item at [i + 1] is less than item at [i]. Switch them
* and the same elements in codePointsForGlyphNames. */
- final String storeString = this.glyphNames[i];
- this.glyphNames[i] = this.glyphNames[i + 1];
- this.glyphNames[i + 1] = storeString;
- final char storeChar = this.glyphIndexes[i];
- this.glyphIndexes[i] = this.glyphIndexes[i + 1];
- this.glyphIndexes[i + 1] = storeChar;
+ final String storeString = this.glyphNames.get(i);
+ this.glyphNames.set(i, this.glyphNames.get(i + 1));
+ this.glyphNames.set(i + 1, storeString);
+ final Character storeChar = this.glyphIndexes.get(i);
+ this.glyphIndexes.set(i, this.glyphIndexes.get(i + 1));
+ this.glyphIndexes.set(i + 1, storeChar);
anyChanges = true;
}
}
@@ -373,22 +351,20 @@
}
}
this.glyphListsToCheck[index] = GlyphList4a.getGlyphList("AGL");
- this.codePoints = new char[this.glyphNames.length];
- this.codePointIndexes = new char[this.codePoints.length];
- for (int i = 0; i < this.glyphNames.length; i++) {
- final String glyphName = this.glyphNames[i];
+ for (int i = 0; i < this.glyphNames.size(); i++) {
+ final String glyphName = this.glyphNames.get(i);
for (int j = 0; j < this.glyphListsToCheck.length; j++) {
glyphList = this.glyphListsToCheck[j];
final char codePoint = glyphList.mapGlyphNameToCodePoint(
glyphName);
if (codePoint != Character.MAX_VALUE) {
- this.codePoints[i] = codePoint;
- final char glyphIndex = this.glyphIndexes[i];
- this.codePointIndexes[i] = glyphIndex;
+ this.codePoints.set(i, codePoint);
+ final char glyphIndex = this.glyphIndexes.get(i);
+ this.codePointIndexes.set(i, glyphIndex);
break;
}
}
- if (this.codePointIndexes[i] == 0
+ if (this.codePointIndexes.get(i) == 0
&& ! glyphName.equals(EncodingVector4a.NOTDEF)) {
this.logger.error("Glyph name not found: " + glyphName);
}
@@ -419,20 +395,20 @@
// Write the glyph names.
s = " public static final String[] sortedGlyphNames = {\n";
out.write(s.getBytes());
- for (int i = 0; i < this.glyphNames.length; i++) {
+ for (int i = 0; i < this.glyphNames.size(); i++) {
s = " \"";
- s = s + this.glyphNames[i];
+ s = s + this.glyphNames.get(i);
s = s + "\"";
- if (i < this.glyphNames.length - 1) {
+ if (i < this.glyphNames.size() - 1) {
s = s + ",";
}
s = padSpaces(s, EncodingParser.GLYPH_NAMES_COLUMN_1, 2);
s = s + "// " + formatArrayIndex(i);
s = padSpaces(s, EncodingParser.GLYPH_NAMES_COLUMN_2, 1);
s = s + " glyph-index: 0x"
- + StringUtil.charToHexString(this.glyphIndexes[i], true,
+ + StringUtil.charToHexString(this.glyphIndexes.get(i), true,
EncodingParser.HEX_STRING_SIZE);
- s = s + " \\" + StringUtil.charToOctalString(this.glyphIndexes[i],
+ s = s + " \\" + StringUtil.charToOctalString(this.glyphIndexes.get(i),
EncodingParser.OCTAL_STRING_SIZE);
s = s + "\n";
out.write(s.getBytes());
@@ -445,13 +421,13 @@
out.write(s.getBytes());
s = " public static final short[] glyphIndexes = {\n";
out.write(s.getBytes());
- for (int i = 0; i < this.glyphIndexes.length; i++) {
- final char index = this.glyphIndexes[i];
+ for (int i = 0; i < this.glyphIndexes.size(); i++) {
+ final char index = this.glyphIndexes.get(i);
s = " ";
s = s + "0x";
s = s + StringUtil.charToHexString(index, true,
EncodingParser.HEX_STRING_SIZE);
- if (i < this.glyphIndexes.length - 1) {
+ if (i < this.glyphIndexes.size() - 1) {
s = s + ",";
}
s = padSpaces(s, EncodingParser.GLYPH_NAME_CODE_POINTS_COLUMN_1, 2);
@@ -460,7 +436,7 @@
s = s + "\\" + StringUtil.charToOctalString(index,
EncodingParser.OCTAL_STRING_SIZE);
s = padSpaces(s, EncodingParser.GLYPH_NAME_CODE_POINTS_COLUMN_3, 1);
- s = s + this.glyphNames[i];
+ s = s + this.glyphNames.get(i);
s = s + "\n";
out.write(s.getBytes());
}
@@ -476,23 +452,23 @@
out.write(s.getBytes());
s = " public static final char[] codePoints = {\n";
out.write(s.getBytes());
- for (int i = 0; i < this.codePoints.length; i++) {
- final char index = this.codePoints[i];
+ for (int i = 0; i < this.codePoints.size(); i++) {
+ final char index = this.codePoints.get(i);
s = " ";
s = s + "0x";
s = s + StringUtil.charToHexString(index, true,
EncodingParser.HEX_STRING_SIZE);
- if (i < this.codePoints.length - 1) {
+ if (i < this.codePoints.size() - 1) {
s = s + ",";
}
s = padSpaces(s, EncodingParser.CODE_POINTS_COLUMN_1, 2);
s = s + "// " + formatArrayIndex(i);
s = padSpaces(s, EncodingParser.CODE_POINTS_COLUMN_2, 2);
s = s + " glyph: 0x" + StringUtil.charToHexString(
- this.codePointIndexes[i], true,
+ this.codePointIndexes.get(i), true,
EncodingParser.HEX_STRING_SIZE);
s = s + " \\" + StringUtil.charToOctalString(
- this.codePointIndexes[i], EncodingParser.HEX_STRING_SIZE);
+ this.codePointIndexes.get(i), EncodingParser.HEX_STRING_SIZE);
s = s + "\n";
out.write(s.getBytes());
}
@@ -504,13 +480,13 @@
out.write(s.getBytes());
s = " public static final char[] codePointIndexes = {\n";
out.write(s.getBytes());
- for (int i = 0; i < this.codePointIndexes.length; i++) {
- final char index = this.codePointIndexes[i];
+ for (int i = 0; i < this.codePointIndexes.size(); i++) {
+ final char index = this.codePointIndexes.get(i);
s = " ";
s = s + "0x";
s = s + StringUtil.charToHexString(index, true,
EncodingParser.HEX_STRING_SIZE);
- if (i < this.codePointIndexes.length - 1) {
+ if (i < this.codePointIndexes.size() - 1) {
s = s + ",";
}
s = padSpaces(s, EncodingParser.CODE_POINTS_INDEX_1, 2);
@@ -558,7 +534,7 @@
* @return The codepoint indexes.
*/
public char[] getCodePointIndexes() {
- return this.codePointIndexes;
+ return CharacterUtil.toArray(this.codePointIndexes);
}
/**
@@ -566,7 +542,7 @@
* @return The codepoints.
*/
public char[] getCodePoints() {
- return this.codePoints;
+ return CharacterUtil.toArray(this.codePoints);
}
/**
@@ -574,7 +550,7 @@
* @return The glyph indexes.
*/
public char[] getGlyphIndexes() {
- return this.glyphIndexes;
+ return CharacterUtil.toArray(this.glyphIndexes);
}
/**
@@ -582,7 +558,8 @@
* @return The glyph names.
*/
public String[] getGlyphNames() {
- return this.glyphNames;
+ final String[] returnArray = new String[this.glyphNames.size()];
+ return this.glyphNames.toArray(returnArray);
}
/**
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|