[FOray-commit] SF.net SVN: foray:[12116] trunk/foray
Modular XSL-FO Implementation for Java.
Status: Alpha
Brought to you by:
victormote
|
From: <vic...@us...> - 2021-11-22 23:22:04
|
Revision: 12116
http://sourceforge.net/p/foray/code/12116
Author: victormote
Date: 2021-11-22 23:22:01 +0000 (Mon, 22 Nov 2021)
Log Message:
-----------
More standardization of SAX parsers.
Modified Paths:
--------------
trunk/foray/foray-common/src/main/java/org/foray/common/xml/SaxParser.java
trunk/foray/foray-font/src/main/java/org/foray/font/FontServer4a.java
trunk/foray/foray-font/src/main/java/org/foray/font/config/FontConfigParser.java
trunk/foray/foray-orthography/src/main/java/org/foray/orthography/OrthographyServer4a.java
trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/NatLangParser.java
trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/OrthographyParser.java
trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/SpellChecker.java
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/xml/SaxParser.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/xml/SaxParser.java 2021-11-22 22:04:38 UTC (rev 12115)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/xml/SaxParser.java 2021-11-22 23:22:01 UTC (rev 12116)
@@ -59,6 +59,9 @@
/** Format string for formatting the current location. */
private static final String LOCATION_FORMAT_STRING = "(%1s:%2$d:%3$d)";
+ /** Format string for formatting the current location. */
+ private static final String SHORT_LOCATION_FORMAT_STRING = "(%1$d:%2$d)";
+
/** Constant used to initialize string builders. */
private static final int MAX_EXPECTED_TEXT_LENGTH = 4096;
@@ -79,6 +82,10 @@
/** Receives content of text nodes. */
private StringBuilder textAccumulator = new StringBuilder(MAX_EXPECTED_TEXT_LENGTH);
+ /** Flag indicating whether text parsing is active. Some elements in some parsers contain text that should not be
+ * accumulated. */
+ private boolean textParsingActive = true;
+
/**
* Parses an input document and returns the object that was created.
* @param inputSource The input document.
@@ -141,6 +148,15 @@
}
/**
+ * Returns the line and column number of the current location in the input document as a formatted string.
+ * @return The line and column number of the current location in the input document as a formatted string.
+ */
+ protected String getShortLocationString() {
+ return String.format(SHORT_LOCATION_FORMAT_STRING, this.locator.getLineNumber(),
+ this.locator.getColumnNumber());
+ }
+
+ /**
* Creates a SAX2 parser.
* @param validate Indicates whether the parser should validate the document as it parses.
* @param namespaceAware Indicates whether the parser should be namespace-aware.
@@ -261,7 +277,9 @@
@Override
public void characters(final char[] chars, final int start, final int length) throws SAXException {
- this.textAccumulator.append(chars, start, length);
+ if (this.textParsingActive) {
+ this.textAccumulator.append(chars, start, length);
+ }
}
/**
@@ -297,4 +315,13 @@
throw new SAXException(ex);
}
+ /**
+ * Sets or unsets whether text parsing is active.
+ * If not active, the text will not be accumulated.
+ * @param isActive The new value for text parsing active.
+ */
+ public void setTextParsingActive(final boolean isActive) {
+ this.textParsingActive = isActive;
+ }
+
}
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/FontServer4a.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/FontServer4a.java 2021-11-22 22:04:38 UTC (rev 12115)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/FontServer4a.java 2021-11-22 23:22:01 UTC (rev 12116)
@@ -684,7 +684,7 @@
private void readFontConfig(final InputStream inputStream) throws FontException {
final InputSource source = new InputSource(inputStream);
final FontConfigParser parser =
- new FontConfigParser(this, source);
+ new FontConfigParser(this);
try {
parser.parse(source);
} catch (IOException | ParserConfigurationException | SAXException e) {
Modified: trunk/foray/foray-font/src/main/java/org/foray/font/config/FontConfigParser.java
===================================================================
--- trunk/foray/foray-font/src/main/java/org/foray/font/config/FontConfigParser.java 2021-11-22 22:04:38 UTC (rev 12115)
+++ trunk/foray/foray-font/src/main/java/org/foray/font/config/FontConfigParser.java 2021-11-22 23:22:01 UTC (rev 12116)
@@ -41,7 +41,6 @@
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
-import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
@@ -117,9 +116,6 @@
* axsl-font-config element. */
private String parsedRootEmbed;
- /** The InputSource encapsulating the configuration file. */
- private InputSource filename;
-
/** The parent FontServer. */
private FontServer4a fontServer;
@@ -128,10 +124,6 @@
* that should be transformed to. */
private Map<String, String> parameters = new HashMap<String, String>();
- /** The XML parser's Locator instance, used to indicate line and column
- * numbers in user messages. */
- private Locator locator;
-
/** Map of GlyphList instances that have already been registered.
* The key is a String with the GlyphList name, and the value is the GlyphList instance. */
private Map<String, GlyphList> glyphListMap = new HashMap<String, GlyphList>();
@@ -144,13 +136,9 @@
/**
* Constructor.
* @param server The parent font server.
- * @param filename The file which contains the configuration information
- * to be parsed.
*/
- public FontConfigParser(final FontServer4a server,
- final InputSource filename) {
+ public FontConfigParser(final FontServer4a server) {
this.fontServer = server;
- this.filename = filename;
try {
this.currentXMLBase = UrlFactory.createURL("file", null, ".");
} catch (final MalformedURLException e) {
@@ -163,7 +151,7 @@
@Override
public Object parse(final InputSource inputSource) throws IOException, ParserConfigurationException, SAXException {
final XMLReader parser = createSax2Parser(true, true, true, AxslDtdUtil.getEntityResolver(), false);
- parser.parse(this.filename);
+ parser.parse(inputSource);
return null;
}
@@ -579,10 +567,6 @@
}
}
- /**
- * {@inheritDoc}
- * Wraps up parsing for elements that need to be finalized.
- */
@Override
public void endElement(final String uri, final String localName,
final String qName) {
@@ -809,14 +793,6 @@
}
/**
- * Sets the document locator for this parser.
- * @param locator The new locator.
- */
- public void setDocumentLocator(final Locator locator) {
- this.locator = locator;
- }
-
- /**
* Logs a warning to the logger.
* @param message The warning message.
*/
@@ -839,12 +815,7 @@
* @return The formatted context message.
*/
private String getContextMessage() {
- if (this.locator == null) {
- return null;
- }
- return " Context: " + this.locator.getSystemId() + "\n"
- + " (Line " + this.locator.getLineNumber() + ", Column "
- + this.locator.getColumnNumber() + ")";
+ return " Context: " + getLocationString();
}
/**
Modified: trunk/foray/foray-orthography/src/main/java/org/foray/orthography/OrthographyServer4a.java
===================================================================
--- trunk/foray/foray-orthography/src/main/java/org/foray/orthography/OrthographyServer4a.java 2021-11-22 22:04:38 UTC (rev 12115)
+++ trunk/foray/foray-orthography/src/main/java/org/foray/orthography/OrthographyServer4a.java 2021-11-22 23:22:01 UTC (rev 12116)
@@ -108,7 +108,7 @@
}
final InputSource inputSource = new InputSource(inputStream);
- final OrthographyParser parser = new OrthographyParser(this, inputSource);
+ final OrthographyParser parser = new OrthographyParser(this);
try {
parser.parse(inputSource);
} catch (IOException | SAXException | ParserConfigurationException e) {
Modified: trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/NatLangParser.java
===================================================================
--- trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/NatLangParser.java 2021-11-22 22:04:38 UTC (rev 12115)
+++ trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/NatLangParser.java 2021-11-22 23:22:01 UTC (rev 12116)
@@ -54,14 +54,11 @@
/** The natural language instance being parsed. */
private NaturalLanguage nl;
- /** The SAX parser. */
- private XMLReader parser;
-
@Override
public NaturalLanguage parse(final InputSource inputSource)
throws IOException, SAXException, ParserConfigurationException {
- this.parser = createSax2Parser(false, true, true, AxslDtdUtil.getEntityResolver(), false);
- this.parser.parse(inputSource);
+ final XMLReader parser = createSax2Parser(false, true, true, AxslDtdUtil.getEntityResolver(), false);
+ parser.parse(inputSource);
return this.nl;
}
Modified: trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/OrthographyParser.java
===================================================================
--- trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/OrthographyParser.java 2021-11-22 22:04:38 UTC (rev 12115)
+++ trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/OrthographyParser.java 2021-11-22 23:22:01 UTC (rev 12116)
@@ -135,9 +135,6 @@
private Map<String, HyphenationPatternsResource> hyphenationPatterns =
new HashMap<String, HyphenationPatternsResource>();
- /** The InputSource encapsulating the configuration file. */
- private InputSource filename;
-
/** The hyphenation server receiving the parsed information. */
private OrthographyServer4a hyphenationServer;
@@ -147,12 +144,9 @@
/**
* Constructor.
* @param server The hyphenation server which will capture the information from the parsed configuration.
- * @param filename The file which contains the configuration information
- * to be parsed.
*/
- public OrthographyParser(final OrthographyServer4a server, final InputSource filename) {
+ public OrthographyParser(final OrthographyServer4a server) {
this.hyphenationServer = server;
- this.filename = filename;
}
@Override
@@ -159,7 +153,7 @@
public Orthography4a parse(final InputSource inputSource)
throws IOException, SAXException, ParserConfigurationException {
final XMLReader parser = createSax2Parser(true, true, true, AxslDtdUtil.getEntityResolver(), false);
- parser.parse(this.filename);
+ parser.parse(inputSource);
return this.currentOrthographyConfig;
}
Modified: trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/SpellChecker.java
===================================================================
--- trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/SpellChecker.java 2021-11-22 22:04:38 UTC (rev 12115)
+++ trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/SpellChecker.java 2021-11-22 23:22:01 UTC (rev 12116)
@@ -33,7 +33,6 @@
import org.foray.common.i18n.Script4a;
import org.foray.common.i18n.WritingSystem4a;
import org.foray.common.primitive.ObjectUtils;
-import org.foray.common.primitive.StringUtils;
import org.foray.common.primitive.XmlUtils;
import org.foray.common.xml.SaxParser;
import org.foray.orthography.Orthography4a;
@@ -56,7 +55,6 @@
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
-import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
@@ -130,22 +128,10 @@
/** Command-line return status constant indicating that there was an error parsing the input file. */
public static final byte STATUS_PARSING_ERROR = 3;
- /** Format string for formatting the current location. */
- private static final String LOCATION_FORMAT_STRING = "(%1$d:%2$d)";
-
/** The output stream to which the output should be sent. */
private PrintStream output;
- /** The locator instance for identifying the document, line, and column
- * number of specific elements. */
- private Locator locator;
- /** Flag indicating whether text parsing is active. Some elements contain text that should not be accumulated. */
- private boolean textParsingActive;
-
- /** A reusable buffer used to accumulate the parsed text content. */
- private StringBuilder charBuffer = new StringBuilder();
-
/** The element stack. */
private Stack<Element> elementStack = new Stack<Element>();
@@ -214,7 +200,7 @@
@Override
public void startDocument() throws SAXException {
- this.textParsingActive = true;
+ setTextParsingActive(true);
}
@@ -225,11 +211,6 @@
@Override
- public void setDocumentLocator(final Locator locator) {
- this.locator = locator;
- }
-
- @Override
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes)
throws SAXException {
/* Some elements, having no content, can be placed in the middle of a word, making it look like that word is
@@ -256,7 +237,7 @@
if (languageAttr == null) {
if (this.currentOrthographyConfig == null) {
- this.output.println("Orthography not specified. " + this.locationString());
+ this.output.println("Orthography not specified. " + getLocationString());
}
} else {
/* Is there a country code? */
@@ -279,7 +260,7 @@
final String message = String.format(
"Orthography not found. Language: %1$s, Country: %2$s, Script: %3$s ", languageString,
countryString, script.getAlphaCode());
- this.output.println(message + locationString());
+ this.output.println(message + getLocationString());
} else {
final Orthography4a config = this.server.getOrthography(element.writingSystem);
if (config == null) {
@@ -286,7 +267,7 @@
final String message = String.format(
"Unconfigured orthography. Language: %1$s, Country: %2$s, Script: %3$s ",
languageString, countryString, script.getAlphaCode());
- this.output.println(message + locationString());
+ this.output.println(message + getLocationString());
} else {
element.orthographyConfig = config;
}
@@ -312,14 +293,11 @@
List<CharSequence> words = null;
if (this.currentOrthographyConfig != null) {
- words = this.currentOrthographyConfig.getLexer().tokenize(this.charBuffer);
+ words = this.currentOrthographyConfig.getLexer().tokenize(getAndClearText());
}
checkWords(words);
- /* Clear the character buffer. */
- StringUtils.clear(this.charBuffer);
-
/* This element should match the top of the element stack. Pop it. */
if (element.matches(uri, localName, qName)) {
this.elementStack.pop();
@@ -340,14 +318,6 @@
/**
- * Returns the current location in the input document as a formatted string.
- * @return The current location in the input document as a formatted string.
- */
- private String locationString() {
- return String.format(LOCATION_FORMAT_STRING, this.locator.getLineNumber(), this.locator.getColumnNumber());
- }
-
- /**
* Spell-check each word in a sequence of words.
* @param words The words to be checked.
*/
@@ -383,17 +353,10 @@
return;
}
- this.output.println("Not found: " + word + " " + locationString());
+ this.output.println("Not found: " + word + " " + getShortLocationString());
this.notFoundCounter ++;
}
- @Override
- public void characters(final char[] buffer, final int offset, final int length) {
- if (this.textParsingActive) {
- this.charBuffer.append(buffer, offset, length);
- }
- }
-
/**
* Returns the command-line options for the {@link #main(String[])} method.
* @return Command-line options.
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|