From c71f8c0a8217d92e5162dc2d3a86d5fa21469ccd Mon Sep 17 00:00:00 2001 From: Willem Date: Sat, 8 Nov 2025 14:27:09 +0100 Subject: [PATCH] X4O: Deleted INPUT_SOURCE property support --- .../java/org/x4o/xml/io/DefaultX4OReader.java | 10 ++-- .../java/org/x4o/xml/io/X4OContentParser.java | 49 ++++++------------- 2 files changed, 21 insertions(+), 38 deletions(-) diff --git a/nx01-x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OReader.java b/nx01-x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OReader.java index 9604d0e..41cf9c9 100644 --- a/nx01-x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OReader.java +++ b/nx01-x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OReader.java @@ -29,6 +29,7 @@ import java.io.OutputStream; import java.net.URL; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import javax.el.ValueExpression; import javax.xml.parsers.ParserConfigurationException; @@ -44,7 +45,6 @@ import org.x4o.xml.lang.X4OLanguageSessionLocal; import org.x4o.xml.lang.phase.X4OPhaseType; import org.xml.sax.EntityResolver; import org.xml.sax.ErrorHandler; -import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** @@ -68,7 +68,6 @@ public class DefaultX4OReader extends AbstractX4OReader { public final static String DOC_BUFFER_SIZE = PROPERTY_CONTEXT_PREFIX + "doc/buffer-size"; public final static String INPUT_STREAM = PROPERTY_CONTEXT_PREFIX + "input/stream"; public final static String INPUT_ENCODING = PROPERTY_CONTEXT_PREFIX + "input/encoding"; - public final static String INPUT_SOURCE = PROPERTY_CONTEXT_PREFIX + "input/source"; public final static String INPUT_SYSTEM_ID = PROPERTY_CONTEXT_PREFIX + "input/system-id"; public final static String INPUT_BASE_PATH = PROPERTY_CONTEXT_PREFIX + "input/base-path"; public final static String VALIDATION_SCHEMA_AUTO_WRITE = PROPERTY_CONTEXT_PREFIX + "validation/schema-auto-write"; @@ -84,9 +83,8 @@ public class DefaultX4OReader extends AbstractX4OReader { new PropertyConfigItem(SAX_ENTITY_RESOLVER,EntityResolver.class), new PropertyConfigItem(DOC_EMPTY_NAMESPACE_URI,String.class), new PropertyConfigItem(DOC_BUFFER_SIZE,Integer.class,4096*2), - new PropertyConfigItem(INPUT_STREAM,InputStream.class), + new PropertyConfigItem(true,INPUT_STREAM,InputStream.class), new PropertyConfigItem(INPUT_ENCODING,String.class,SAX3XMLConstants.XML_DEFAULT_ENCODING), - new PropertyConfigItem(INPUT_SOURCE,InputSource.class), new PropertyConfigItem(true,INPUT_SYSTEM_ID,String.class), new PropertyConfigItem(true,INPUT_BASE_PATH,URL.class), new PropertyConfigItem(VALIDATION_SCHEMA_AUTO_WRITE,Boolean.class,true), @@ -115,6 +113,10 @@ public class DefaultX4OReader extends AbstractX4OReader { @Override public void readSession(X4OLanguageSession languageSession, InputStream input, String systemId, URL basePath) throws X4OConnectionException, SAXException, IOException { + Objects.requireNonNull(languageSession, "Can't read into null language session"); + Objects.requireNonNull(input, "Can't read a null input stream"); + Objects.requireNonNull(systemId, "No system-id provided for the input streamn"); + Objects.requireNonNull(basePath, "No base-path provided for the input streamn"); // TODO; make optional, use default working folder setProperty(INPUT_STREAM, input); setProperty(INPUT_SYSTEM_ID, systemId); setProperty(INPUT_BASE_PATH, basePath); diff --git a/nx01-x4o-driver/src/main/java/org/x4o/xml/io/X4OContentParser.java b/nx01-x4o-driver/src/main/java/org/x4o/xml/io/X4OContentParser.java index ed01a05..fd9fe8c 100644 --- a/nx01-x4o-driver/src/main/java/org/x4o/xml/io/X4OContentParser.java +++ b/nx01-x4o-driver/src/main/java/org/x4o/xml/io/X4OContentParser.java @@ -36,6 +36,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; @@ -65,20 +66,25 @@ public class X4OContentParser { languageSession.getX4ODebugWriter().debugSAXConfigStart(); } try { - parseSax(languageSession); + // Validate required input + InputStream inputStream = Objects.requireNonNull((InputStream) getPropertyConfig().getProperty(DefaultX4OReader.INPUT_STREAM), "Missing property: " + DefaultX4OReader.INPUT_STREAM); + String inputEncoding = Objects.requireNonNull((String) getPropertyConfig().getProperty(DefaultX4OReader.INPUT_ENCODING), "Missing property: " + DefaultX4OReader.INPUT_ENCODING); + String inputSystemId = Objects.requireNonNull((String) getPropertyConfig().getProperty(DefaultX4OReader.INPUT_SYSTEM_ID), "Missing property: " + DefaultX4OReader.INPUT_SYSTEM_ID); + // Wrap input stream to source with added meta info + InputSource inputSource = new InputSource(inputStream); + inputSource.setEncoding(inputEncoding); + inputSource.setSystemId(inputSystemId); + // Start the SAX bases parsing + parseSax(languageSession, inputSource); } finally { + // Close the debug parse group tag if (languageSession.hasX4ODebugWriter()) { languageSession.getX4ODebugWriter().debugSAXConfigEnd(); } } } - private void parseSax(X4OLanguageSession languageSession) throws SAXException, IOException, ParserConfigurationException { - // If xsd caching is needed this should be the way - // XMLParserConfiguration config = new XIncludeAwareParserConfiguration(); - // config.setProperty("http://apache.org/xml/properties/internal/grammar-pool",myFullGrammarPool); - // SAXParser parser = new SAXParser(config); - + private void parseSax(X4OLanguageSession languageSession, InputSource inputSource) throws SAXException, IOException, ParserConfigurationException { // Create Sax parser with x4o tag handler X4OContentHandler xth = new X4OContentHandler(languageSession, getPropertyConfig()); SAXParserFactory factory = SAXParserFactory.newInstance(); @@ -129,7 +135,7 @@ public class X4OContentParser { } } - // check for required features + // Check for required features List requiredFeatures = getSAXParserFeaturesRequired(languageSession); for (String requiredFeature : requiredFeatures) { if (reader.getFeature(requiredFeature) == false) { @@ -139,32 +145,7 @@ public class X4OContentParser { } // Finally start parsing the xml input stream - Object requestInputSource = getPropertyConfig().getProperty(DefaultX4OReader.INPUT_SOURCE); - InputSource input = null; - InputStream inputStream = null; - if (requestInputSource instanceof InputSource) { - input = (InputSource) requestInputSource; - } else { - inputStream = (InputStream) getPropertyConfig().getProperty(DefaultX4OReader.INPUT_STREAM); - input = new InputSource(inputStream); - } - - Object requestInputEncoding = getPropertyConfig().getProperty(DefaultX4OReader.INPUT_ENCODING); - if (requestInputEncoding != null && requestInputEncoding instanceof String) { - input.setEncoding(requestInputEncoding.toString()); - } - Object requestSystemId = getPropertyConfig().getProperty(DefaultX4OReader.INPUT_SYSTEM_ID); - if (requestSystemId != null && requestSystemId instanceof String) { - input.setSystemId(requestSystemId.toString()); - } - - try { - reader.parse(input); - } finally { - if (inputStream != null) { - inputStream.close(); - } - } + reader.parse(inputSource); } private void debugMessageLog(String message, X4OLanguageSession languageSession) throws SAXException {