[FOray-commit] SF.net SVN: foray:[12084] trunk/foray
Modular XSL-FO Implementation for Java.
Status: Alpha
Brought to you by:
victormote
|
From: <vic...@us...> - 2021-11-18 15:02:19
|
Revision: 12084
http://sourceforge.net/p/foray/code/12084
Author: victormote
Date: 2021-11-18 15:02:16 +0000 (Thu, 18 Nov 2021)
Log Message:
-----------
1. Move more reusable code to SaxParser. 2. Have SaxParser manage the logger.
Modified Paths:
--------------
trunk/foray/foray-common/src/main/java/org/foray/common/xml/SaxParser.java
trunk/foray/foray-fotree/src/main/java/org/foray/fotree/FoTreeBuilder.java
trunk/foray/foray-fotree/src/main/java/org/foray/fotree/FoTreeServer4a.java
trunk/foray/foray-fotree/src/test/java/org/foray/fotree/AbstractPropertyTest.java
trunk/foray/foray-orthography/src/main/java/org/foray/orthography/OrthographyServer4a.java
trunk/foray/foray-orthography/src/main/java/org/foray/orthography/PatternParser.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/OrthographyConfigParser.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-18 14:35:17 UTC (rev 12083)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/xml/SaxParser.java 2021-11-18 15:02:16 UTC (rev 12084)
@@ -28,10 +28,15 @@
package org.foray.common.xml;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.DefaultHandler2;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
@@ -40,6 +45,9 @@
*/
public abstract class SaxParser extends DefaultHandler2 {
+ /** The logger. */
+ private Logger logger = LoggerFactory.getLogger(this.getClass());
+
/**
* Returns the name of the SAX Parser class that is found in the classpath.
* @return The name of the SAX Parser class that is found in the classpath.
@@ -56,6 +64,14 @@
}
/**
+ * Returns the logger for this parser.
+ * @return The logger;
+ */
+ public final Logger getLogger() {
+ return this.logger;
+ }
+
+ /**
* Creates a SAX2 parser with a standard configuration. Specifically,
* the parser is namespace-aware and has the "namespace-prefixes" feature
* set to true.
@@ -72,4 +88,53 @@
return xmlReader;
}
+ /**
+ * Instantiates an instance of a specified class using reflection, and ensures that it is a subtype of a given type.
+ * @param className The name of the class that should be instantiated.
+ * @param expectedType The expected superclass for {@code className}.
+ * @param <T> The type of the superclass object that is being instantiated.
+ * @param parameterTypes The array of parameter types.
+ * @param parameters The array of parameters.
+ * @return The new instance of {@code className}, or null if it could not be created.
+ * @throws SAXException Wraps a number of exceptions that can be thrown during instantiation by reflection.
+ */
+ protected <T extends Object> T instantiate(final String className, final Class<T> expectedType,
+ final Class<?>[] parameterTypes, final Object[] parameters) throws SAXException {
+ Class<?> theClass = null;
+ try {
+ theClass = Class.forName(className);
+ } catch (final ClassNotFoundException e) {
+ throw new SAXException(e);
+ }
+ if (! expectedType.isAssignableFrom(theClass)) {
+ getLogger().warn("Class \"{}\" is not a {} class.", className, expectedType.getName());
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ final Class<T> factoryClass = (Class<T>) theClass;
+ Constructor<T> constructor = null;
+ try {
+ constructor = factoryClass.getConstructor(parameterTypes);
+ } catch (final SecurityException e) {
+ throw new SAXException(e);
+ } catch (final NoSuchMethodException e) {
+ throw new SAXException(e);
+ }
+
+ T newInstance = null;
+ try {
+ newInstance = constructor.newInstance(parameters);
+ } catch (final IllegalArgumentException e) {
+ throw new SAXException(e);
+ } catch (final InstantiationException e) {
+ throw new SAXException(e);
+ } catch (final IllegalAccessException e) {
+ throw new SAXException(e);
+ } catch (final InvocationTargetException e) {
+ throw new SAXException(e);
+ }
+ return newInstance;
+ }
+
}
Modified: trunk/foray/foray-fotree/src/main/java/org/foray/fotree/FoTreeBuilder.java
===================================================================
--- trunk/foray/foray-fotree/src/main/java/org/foray/fotree/FoTreeBuilder.java 2021-11-18 14:35:17 UTC (rev 12083)
+++ trunk/foray/foray-fotree/src/main/java/org/foray/fotree/FoTreeBuilder.java 2021-11-18 15:02:16 UTC (rev 12084)
@@ -47,8 +47,6 @@
import org.axsl.speech.SpeechServer;
import org.axsl.text.TextServer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
@@ -86,9 +84,6 @@
/** The parent server. */
private FoTreeServer4a server;
- /** The logger. */
- private Logger logger;
-
/** The graphic server. */
private GraphicServer graphicServer;
@@ -118,11 +113,9 @@
/**
* Constructor.
* @param server The parent server.
- * @param logger The logger.
*/
- public FoTreeBuilder(final FoTreeServer4a server, final Logger logger) {
+ public FoTreeBuilder(final FoTreeServer4a server) {
this.server = server;
- this.logger = logger;
}
@Override
@@ -296,17 +289,6 @@
}
/**
- * Returns the logger.
- * @return The logger.
- */
- public Logger getLogger() {
- if (this.logger == null) {
- this.logger = LoggerFactory.getLogger(FoTreeBuilder.class);
- }
- return this.logger;
- }
-
- /**
* Returns the line number currently showing in the SAX Locator.
* @return The line number currently showing in the SAX Locator.
*/
Modified: trunk/foray/foray-fotree/src/main/java/org/foray/fotree/FoTreeServer4a.java
===================================================================
--- trunk/foray/foray-fotree/src/main/java/org/foray/fotree/FoTreeServer4a.java 2021-11-18 14:35:17 UTC (rev 12083)
+++ trunk/foray/foray-fotree/src/main/java/org/foray/fotree/FoTreeServer4a.java 2021-11-18 15:02:16 UTC (rev 12084)
@@ -280,8 +280,7 @@
@Override
public FoTreeBuilder makeFoTree() {
- final FoTreeBuilder foTreeBuilder = new FoTreeBuilder(this,
- this.getLogger());
+ final FoTreeBuilder foTreeBuilder = new FoTreeBuilder(this);
foTreeBuilder.setGraphicServer(this.graphicServer);
foTreeBuilder.setTextServer(this.textServer);
foTreeBuilder.setOrthographyServer(this.orthographyServer);
Modified: trunk/foray/foray-fotree/src/test/java/org/foray/fotree/AbstractPropertyTest.java
===================================================================
--- trunk/foray/foray-fotree/src/test/java/org/foray/fotree/AbstractPropertyTest.java 2021-11-18 14:35:17 UTC (rev 12083)
+++ trunk/foray/foray-fotree/src/test/java/org/foray/fotree/AbstractPropertyTest.java 2021-11-18 15:02:16 UTC (rev 12084)
@@ -94,7 +94,7 @@
* @return The newly-created FoObj.
*/
protected static final FoObj makeTestFObj() {
- final FoTreeBuilder foTreeBuilder = new FoTreeBuilder(null, null);
+ final FoTreeBuilder foTreeBuilder = new FoTreeBuilder(null);
final PropertyList propertyList = new PropertyList(0);
final Root root = new Root(propertyList);
root.setFOTreeBuilder(foTreeBuilder);
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-18 14:35:17 UTC (rev 12083)
+++ trunk/foray/foray-orthography/src/main/java/org/foray/orthography/OrthographyServer4a.java 2021-11-18 15:02:16 UTC (rev 12084)
@@ -255,7 +255,7 @@
this.logger.debug("reading " + naturalLanguageDir + filePrefix
+ ".xml");
}
- final NatLangParser parser = new NatLangParser(this.logger);
+ final NatLangParser parser = new NatLangParser();
nl = parser.parse(nlFile);
return nl;
}
Modified: trunk/foray/foray-orthography/src/main/java/org/foray/orthography/PatternParser.java
===================================================================
--- trunk/foray/foray-orthography/src/main/java/org/foray/orthography/PatternParser.java 2021-11-18 14:35:17 UTC (rev 12083)
+++ trunk/foray/foray-orthography/src/main/java/org/foray/orthography/PatternParser.java 2021-11-18 15:02:16 UTC (rev 12084)
@@ -38,8 +38,6 @@
import org.axsl.orthography.OrthographyException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -86,9 +84,6 @@
* are returned with exceptions. */
private String errMsg;
- /** The logger. */
- private Logger logger = LoggerFactory.getLogger(this.getClass());
-
/**
* Constructor.
* @param consumer The pattern consumer implementation that will accept the data created by this parser.
@@ -121,7 +116,7 @@
} catch (final IOException e) {
/* This is a normal condition, and just means that the URL to the
* pattern file does not exist. */
- this.logger.error("Cannot open hyphenation pattern: "
+ getLogger().error("Cannot open hyphenation pattern: "
+ url.toString() + ForayConstants.LOG_NEWLINE
+ e.getMessage());
return;
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-18 14:35:17 UTC (rev 12083)
+++ trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/NatLangParser.java 2021-11-18 15:02:16 UTC (rev 12084)
@@ -40,8 +40,6 @@
import org.axsl.orthography.OrthographyException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
@@ -72,25 +70,16 @@
* are returned with exceptions. */
private String errMsg;
- /** The logger. */
- private Logger logger;
-
/**
* Constructor.
- * @param logger The logger for user messages.
*/
- public NatLangParser(final Logger logger) {
- if (logger == null) {
- this.logger = LoggerFactory.getLogger(NatLangParser.class);
- } else {
- this.logger = logger;
- }
+ public NatLangParser() {
try {
this.parser = createSax2Parser();
} catch (final SAXException e) {
- this.logger.error(e.getMessage(), e);
+ getLogger().error(e.getMessage(), e);
} catch (final ParserConfigurationException e) {
- this.logger.error(e.getMessage(), e);
+ getLogger().error(e.getMessage(), e);
}
this.parser.setContentHandler(this);
this.parser.setErrorHandler(this);
@@ -112,7 +101,7 @@
} catch (final IOException e) {
/* This is a normal condition, and just means that the URL to the
* pattern file does not exist. */
- this.logger.error("Cannot open hyphenation pattern: "
+ getLogger().error("Cannot open hyphenation pattern: "
+ url.toString() + ForayConstants.LOG_NEWLINE
+ e.getMessage());
return null;
Modified: trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/OrthographyConfigParser.java
===================================================================
--- trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/OrthographyConfigParser.java 2021-11-18 14:35:17 UTC (rev 12083)
+++ trunk/foray/foray-orthography/src/main/java/org/foray/orthography/util/OrthographyConfigParser.java 2021-11-18 15:02:16 UTC (rev 12084)
@@ -37,6 +37,7 @@
import org.foray.common.resource.ResourceLocation;
import org.foray.common.resource.ResourceLocationClasspath;
import org.foray.common.resource.ResourceLocationUrl;
+import org.foray.common.xml.SaxParser;
import org.foray.orthography.DerivativePattern;
import org.foray.orthography.DerivativeRule;
import org.foray.orthography.DictionaryResource;
@@ -53,8 +54,6 @@
import org.axsl.orthography.Word.PosQualifier;
import org.axsl.orthography.optional.Lexer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
@@ -66,8 +65,6 @@
import org.xml.sax.helpers.DefaultHandler;
import java.io.IOException;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
@@ -84,11 +81,8 @@
* instance.
* Normally this class doesn't need to be accessed directly.
*/
-public class OrthographyConfigParser extends DefaultHandler {
+public class OrthographyConfigParser extends SaxParser {
- /** The logger. */
- private Logger logger = LoggerFactory.getLogger(this.getClass());
-
/** Stateful variable. */
private DictionaryResource currentDictionaryResource;
@@ -192,9 +186,9 @@
try {
parser.setFeature("http://xml.org/sax/features/validation", true);
} catch (final SAXNotRecognizedException e) {
- this.logger.warn("Parser does not recognize validation.");
+ getLogger().warn("Parser does not recognize validation.");
} catch (final SAXNotSupportedException e) {
- this.logger.warn("Parser does not support validation.");
+ getLogger().warn("Parser does not support validation.");
}
parser.setContentHandler(this);
final EntityResolver resolver = AxslDtdUtil.getEntityResolver();
@@ -225,7 +219,7 @@
final XMLReader xmlReader = spf.newSAXParser().getXMLReader();
final EntityResolver entityResolver = this.hyphenationServer.getEntityResolver();
xmlReader.setEntityResolver(entityResolver);
- this.logger.debug("Orthography Configuration Parsing: Using {} as SAX2 Parser",
+ getLogger().debug("Orthography Configuration Parsing: Using {} as SAX2 Parser",
xmlReader.getClass().getName());
return xmlReader;
} catch (final javax.xml.parsers.ParserConfigurationException e) {
@@ -319,8 +313,8 @@
final String reference = attributes.getValue("reference");
final DictionaryResource resource = this.dictionaries.get(reference);
if (resource == null) {
- this.logger.error("dictionary-resource not found: {}", reference);
- this.logger.error(getContextMessage());
+ getLogger().error("dictionary-resource not found: {}", reference);
+ getLogger().error(getContextMessage());
} else {
this.currentOrthographyConfig.setDictionaryResource(resource);
}
@@ -330,8 +324,8 @@
final String reference = attributes.getValue("reference");
final HyphenationPatternsResource resource = this.hyphenationPatterns.get(reference);
if (resource == null) {
- this.logger.error("hyphenation-patterns-resource not found: {}", reference);
- this.logger.error(getContextMessage());
+ getLogger().error("hyphenation-patterns-resource not found: {}", reference);
+ getLogger().error(getContextMessage());
} else {
this.currentOrthographyConfig.setHyphenationPatternsResource(resource);
}
@@ -341,8 +335,8 @@
final String reference = attributes.getValue("reference");
final List<Pattern> patterns = this.hyphenationServer.getMatchRules(reference);
if (patterns == null) {
- this.logger.error("match-rules not found: {}", reference);
- this.logger.error(getContextMessage());
+ getLogger().error("match-rules not found: {}", reference);
+ getLogger().error(getContextMessage());
} else {
this.currentOrthographyConfig.registerMatchRuleListId(reference);
}
@@ -352,8 +346,8 @@
final String reference = attributes.getValue("reference");
final List<DerivativePattern> rules = this.hyphenationServer.getDerivativePatterns(reference);
if (rules == null) {
- this.logger.error("derivative-rules not found: {}", reference);
- this.logger.error(getContextMessage());
+ getLogger().error("derivative-rules not found: {}", reference);
+ getLogger().error(getContextMessage());
} else {
this.currentOrthographyConfig.registerDerivativeRuleListId(reference);
}
@@ -363,8 +357,8 @@
final String reference = attributes.getValue("reference");
final List<WordWrapperFactory<?>> factories = this.derivativeLists.get(reference);
if (factories == null) {
- this.logger.error("derivative-factories not found: {}", reference);
- this.logger.error(getContextMessage());
+ getLogger().error("derivative-factories not found: {}", reference);
+ getLogger().error(getContextMessage());
} else {
this.currentOrthographyConfig.setWordWrapperFactories(factories);
}
@@ -472,7 +466,7 @@
}
default: {
/* Make sure user knows about unknown tag. */
- this.logger.error("Unknown tag in orthography configuration: {}", localName);
+ getLogger().error("Unknown tag in orthography configuration: {}", localName);
}
}
}
@@ -487,76 +481,27 @@
final String scriptString = attributes.getValue("script-iso-4char");
final Language4a language = Language4a.findFrom3Char(languageString);
if (language == null) {
- this.logger.error("Unable to find language for: {}", languageString);
- this.logger.error(getContextMessage());
+ getLogger().error("Unable to find language for: {}", languageString);
+ getLogger().error(getContextMessage());
}
final Script4a script = Script4a.findFromAlpha(scriptString);
if (script == null) {
- this.logger.error("Unable to find script for: {}", scriptString);
- this.logger.error(getContextMessage());
+ getLogger().error("Unable to find script for: {}", scriptString);
+ getLogger().error(getContextMessage());
}
final Country4a country = Country4a.findFrom3Char(countryString);
if (country == null) {
- this.logger.error("Unable to find country for: {}", countryString);
- this.logger.error(getContextMessage());
+ getLogger().error("Unable to find country for: {}", countryString);
+ getLogger().error(getContextMessage());
}
final WritingSystem4a writingSystem = WritingSystem4a.find(language, script, country);
if (writingSystem == null) {
- this.logger.error("Unable to find script for: {}_{}_{}", languageString, scriptString, countryString);
- this.logger.error(getContextMessage());
+ getLogger().error("Unable to find script for: {}_{}_{}", languageString, scriptString, countryString);
+ getLogger().error(getContextMessage());
}
this.hyphenationServer.registerOrthography(writingSystem, this.currentOrthographyConfig);
}
- /**
- * Instantiates an instance of a specified class using reflection, and ensures that it is a subtype of a given type.
- * @param className The name of the class that should be instantiated.
- * @param expectedType The expected superclass for {@code className}.
- * @param <T> The type of the superclass object that is being instantiated.
- * @param parameterTypes The array of parameter types.
- * @param parameters The array of parameters.
- * @return The new instance of {@code className}, or null if it could not be created.
- * @throws SAXException Wraps a number of exceptions that can be thrown during instantiation by reflection.
- */
- private <T extends Object> T instantiate(final String className, final Class<T> expectedType,
- final Class<?>[] parameterTypes, final Object[] parameters) throws SAXException {
- Class<?> theClass = null;
- try {
- theClass = Class.forName(className);
- } catch (final ClassNotFoundException e) {
- throw new SAXException(e);
- }
- if (! expectedType.isAssignableFrom(theClass)) {
- this.logger.warn("Class \"{}\" is not a {} class.", className, WordWrapperFactory.class.getName());
- return null;
- }
-
- @SuppressWarnings("unchecked")
- final Class<T> factoryClass = (Class<T>) theClass;
- Constructor<T> constructor = null;
- try {
- constructor = factoryClass.getConstructor(parameterTypes);
- } catch (final SecurityException e) {
- throw new SAXException(e);
- } catch (final NoSuchMethodException e) {
- throw new SAXException(e);
- }
-
- T newInstance = null;
- try {
- newInstance = constructor.newInstance(parameters);
- } catch (final IllegalArgumentException e) {
- throw new SAXException(e);
- } catch (final InstantiationException e) {
- throw new SAXException(e);
- } catch (final IllegalAccessException e) {
- throw new SAXException(e);
- } catch (final InvocationTargetException e) {
- throw new SAXException(e);
- }
- return newInstance;
- }
-
@Override
public void endElement(final String uri, final String localName, final String qName) {
endElementInside(uri, localName, qName);
@@ -749,8 +694,8 @@
try {
return new URL(urlString);
} catch (final MalformedURLException e) {
- this.logger.error("Invalid URL: {}", urlString);
- this.logger.error(getContextMessage());
+ getLogger().error("Invalid URL: {}", urlString);
+ getLogger().error(getContextMessage());
return null;
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|