[FOray-commit] SF.net SVN: foray:[12298] trunk/foray
Modular XSL-FO Implementation for Java.
Status: Alpha
Brought to you by:
victormote
|
From: <vic...@us...> - 2021-12-29 23:20:51
|
Revision: 12298
http://sourceforge.net/p/foray/code/12298
Author: victormote
Date: 2021-12-29 23:20:48 +0000 (Wed, 29 Dec 2021)
Log Message:
-----------
Use ForayEntityResolver to resolve FOray as well as aXSL DTDs.
Modified Paths:
--------------
trunk/foray/foray-common/src/main/java/org/foray/common/ForayEntityResolver.java
trunk/foray/foray-core/src/main/java/org/foray/core/ForayConfigParser.java
Modified: trunk/foray/foray-common/src/main/java/org/foray/common/ForayEntityResolver.java
===================================================================
--- trunk/foray/foray-common/src/main/java/org/foray/common/ForayEntityResolver.java 2021-12-29 22:31:05 UTC (rev 12297)
+++ trunk/foray/foray-common/src/main/java/org/foray/common/ForayEntityResolver.java 2021-12-29 23:20:48 UTC (rev 12298)
@@ -28,10 +28,13 @@
package org.foray.common;
+import org.slf4j.LoggerFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -59,8 +62,11 @@
public static final String AREA_TREE_PUBLIC_ID = "-//aXSL//DTD Area Tree V0.1//EN";
/** The public id of the "parts of speech" DTD. */
- public static final String PARTS_OF_SPEECH = "-//aXSL//DTD Parts of Speech V0.1//EN";
+ public static final String PARTS_OF_SPEECH_PUBLIC_ID = "-//aXSL//DTD Parts of Speech V0.1//EN";
+ /** The public id of the "foray config" DTD. */
+ public static final String FORAY_CONFIG_DTD_PUBLIC_ID = "-//FOray//DTD FOray Configuration V0.1//EN";
+
/** The singleton instance. */
private static final ForayEntityResolver INSTANCE = new ForayEntityResolver();
@@ -80,40 +86,63 @@
}
/**
- * Returns an aXSL DTD as an InputStream.
+ * Returns an aXSL DTD as an InputSource.
* @param dtdName The name of the DTD to be retrieved.
* @return The appropriate aXSL DTD, or null if not found.
*/
- public InputStream getAxslDtdAsInputStream(final String dtdName) {
- final InputStream theStream =
- ForayEntityResolver.class.getResourceAsStream("/resources/org/axsl/dtds/" + dtdName);
- return theStream;
+ private InputStream getAxslDtdAsInputStream(final String dtdName) {
+ return ForayEntityResolver.class.getResourceAsStream("/resources/org/axsl/dtds/" + dtdName);
}
+ /**
+ * Returns a FOray DTD as an InputStream.
+ * @param dtdName The name of the DTD to be retrieved.
+ * @return The appropriate FOray DTD, or null if not found.
+ */
+ private InputStream getForayDtdAsInputStream(final String dtdName) throws IOException {
+ InputStream inputStream = this.getClass().getResourceAsStream("/resources/org/foray/dtds/" + dtdName);
+
+ if (inputStream == null) {
+ /* The aXSL DTDs should all be found in jar files resolved by the build system.
+ * For FOray DTDs, the user could be a developer running in an IDE, and, because the DTD files live in the
+ * website directory in the "master" project, the IDE does not know about them.
+ * See build.gradle where the file is copied into the jar file.
+ * Therefore, we look for the file on the local file system. */
+ final File file = new File("../master/doc/web/dtds/0.1/en/" + dtdName);
+ if (file.exists()) {
+ inputStream = new FileInputStream(file);
+ }
+ }
+
+ return inputStream;
+ }
+
+ public InputStream getInputStream(final String publicId) throws IOException {
+ switch(publicId) {
+ /* aXSL DTDs. */
+ case NATURAL_LANGUAGE_PUBLIC_ID: return getAxslDtdAsInputStream("axsl-natural-language.dtd");
+ case HYPHENATION_PUBLIC_ID: return getAxslDtdAsInputStream("axsl-hyphenation.dtd");
+ case DICTIONARY_PUBLIC_ID: return getAxslDtdAsInputStream("axsl-dictionary.dtd");
+ case FONT_CONFIG_PUBLIC_ID: return getAxslDtdAsInputStream("axsl-font-config.dtd");
+ case ORTHOGRAPHY_CONFIG_PUBLIC_ID: return getAxslDtdAsInputStream("axsl-orthography-config.dtd");
+ case AREA_TREE_PUBLIC_ID: return getAxslDtdAsInputStream("axsl-area-tree.dtd");
+ case PARTS_OF_SPEECH_PUBLIC_ID: return getAxslDtdAsInputStream("axsl-parts-of-speech.dtd");
+
+ /* FOray DTDs. */
+ case FORAY_CONFIG_DTD_PUBLIC_ID: return getForayDtdAsInputStream("foray-config.dtd");
+
+ default: return null;
+ }
+
+ }
+
@Override
- public InputSource resolveEntity(final String publicId, final String systemId) throws SAXException,
- IOException {
- InputStream inputStream = null;
- if (ForayEntityResolver.NATURAL_LANGUAGE_PUBLIC_ID.equals(publicId)) {
- inputStream = getAxslDtdAsInputStream("axsl-natural-language.dtd");
- } else if (ForayEntityResolver.HYPHENATION_PUBLIC_ID.equals(publicId)) {
- inputStream = getAxslDtdAsInputStream("axsl-hyphenation.dtd");
- } else if (ForayEntityResolver.DICTIONARY_PUBLIC_ID.equals(publicId)) {
- inputStream = getAxslDtdAsInputStream("axsl-dictionary.dtd");
- } else if (ForayEntityResolver.FONT_CONFIG_PUBLIC_ID.equals(publicId)) {
- inputStream = getAxslDtdAsInputStream("axsl-font-config.dtd");
- } else if (ForayEntityResolver.ORTHOGRAPHY_CONFIG_PUBLIC_ID.equals(publicId)) {
- inputStream = getAxslDtdAsInputStream("axsl-orthography-config.dtd");
- } else if (ForayEntityResolver.AREA_TREE_PUBLIC_ID.equals(publicId)) {
- inputStream = getAxslDtdAsInputStream("axsl-area-tree.dtd");
- } else if (ForayEntityResolver.PARTS_OF_SPEECH.equals(publicId)) {
- inputStream = getAxslDtdAsInputStream("axsl-parts-of-speech.dtd");
- }
+ public InputSource resolveEntity(final String publicId, final String systemId) throws SAXException, IOException {
+ final InputStream inputStream = getInputStream(publicId);
if (inputStream == null) {
- return null;
+ LoggerFactory.getLogger(this.getClass()).warn("Unable to resolve resource locally: " + publicId);
}
- final InputSource inputSource = new InputSource(inputStream);
- return inputSource;
+ return inputStream == null ? null : new InputSource(inputStream);
}
}
Modified: trunk/foray/foray-core/src/main/java/org/foray/core/ForayConfigParser.java
===================================================================
--- trunk/foray/foray-core/src/main/java/org/foray/core/ForayConfigParser.java 2021-12-29 22:31:05 UTC (rev 12297)
+++ trunk/foray/foray-core/src/main/java/org/foray/core/ForayConfigParser.java 2021-12-29 23:20:48 UTC (rev 12298)
@@ -30,6 +30,7 @@
import org.foray.common.Configuration;
import org.foray.common.ConfigurationException;
+import org.foray.common.ForayEntityResolver;
import org.foray.common.xml.SaxParser;
import org.xml.sax.Attributes;
@@ -38,9 +39,7 @@
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
-import java.io.FileInputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.ParserConfigurationException;
@@ -52,9 +51,6 @@
*/
public class ForayConfigParser extends SaxParser<Object> {
- /** Public ID of the FOray configuration DTD. */
- public static final String FORAY_CONFIG_DTD_PUBLIC_ID = "-//FOray//DTD FOray Configuration V0.1//EN";
-
/** Constant indicating that the current status of the parser is outside
* of either key or value. */
private static final int OUT = 0;
@@ -117,24 +113,7 @@
@Override
public Object parse(final InputSource inputSource) throws IOException, ParserConfigurationException, SAXException {
- final EntityResolver resolver = new EntityResolver() {
- public InputSource resolveEntity(final String publicId, final String systemId) throws SAXException,
- IOException {
- if (ForayConfigParser.FORAY_CONFIG_DTD_PUBLIC_ID.equals(publicId)) {
- InputStream inputStream =
- ForayConfigParser.class.getResourceAsStream("/resources/org/foray/dtds/foray-config.dtd");
- /* If null, the user could be a developer running in an IDE.
- * See build.gradle where the file is copied into the jar file.
- * However, the IDE doesn't know how to find it because it lives with the website files.
- * Therefore look for it on the file system instead. */
- if (inputStream == null) {
- inputStream = new FileInputStream("../master/doc/web/dtds/0.1/en/foray-config.dtd");
- }
- return new InputSource(inputStream);
- }
- return null;
- }
- };
+ final EntityResolver resolver = ForayEntityResolver.getInstance();
final XMLReader parser = createSax2Parser(true, true, true, resolver, false);
parser.parse(inputSource);
return null;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|