X4O: Made session equal used in read and write and added closable

This commit is contained in:
Willem Cazander 2025-11-06 18:42:35 +01:00
parent e685c27131
commit ec8711df9c
20 changed files with 220 additions and 241 deletions

View file

@ -29,6 +29,7 @@ import javax.el.ValueExpression;
import org.x4o.xml.X4ODriver;
import org.x4o.xml.X4ODriverManager;
import org.x4o.xml.io.DefaultX4OReader;
import org.x4o.xml.io.X4OConnectionException;
import org.x4o.xml.io.X4OReader;
import org.x4o.xml.lang.X4OLanguage;
@ -88,7 +89,6 @@ public class EldModuleLoader implements X4OLanguageModuleLoader {
} else {
driver = X4ODriverManager.getX4ODriver(EldDriver.LANGUAGE_NAME);
}
X4OReader<?> reader = driver.createReader();
//X4OLanguageSession eldLang = driver.createLanguageSession(driver.getLanguageVersionDefault());
@ -97,6 +97,10 @@ public class EldModuleLoader implements X4OLanguageModuleLoader {
reader.addELBeanInstance(EL_PARENT_LANGUAGE, language);
reader.addELBeanInstance(EL_PARENT_LANGUAGE_MODULE, languageModule);
//language.createLanguageSession()
reader.setProperty(DefaultX4OReader.DEBUG_OUTPUT_HANDLER, reader);
//TODO: if (language.getLanguageConfiguration().getLanguagePropertyBoolean(X4OLanguageProperty.DEBUG_OUTPUT_ELD_PARSER)) {
// eldLang.setX4ODebugWriter(elementLanguage.getLanguageConfiguration().getX4ODebugWriter());
// }

View file

@ -28,6 +28,7 @@ import java.util.List;
import org.x4o.sax3.io.SAX3PropertyConfig;
import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.X4OLanguageSession;
/**
* AbstractX4OConnection is the read/write interface for the classes.
@ -42,14 +43,23 @@ public abstract class AbstractX4OConnection extends AbstractX4OConnectionDebug {
protected List<String> phaseSkip = null;
public AbstractX4OConnection(X4OLanguage language) {
this.language=language;
if (language == null) {
throw new NullPointerException("Can't create X4O connection for null language.");
}
this.language = language;
this.phaseSkip = new ArrayList<String>(2);
}
protected X4OLanguage getLanguage() {
@Override
public final X4OLanguage getLanguage() {
return language;
}
@Override
public final X4OLanguageSession createLanguageSession() {
return language.createLanguageSession();
}
abstract SAX3PropertyConfig getPropertyConfig();
/**
@ -57,23 +67,28 @@ public abstract class AbstractX4OConnection extends AbstractX4OConnectionDebug {
* @param key The key of the property to set.
* @param value The vlue of the property to set.
*/
public void setProperty(String key,Object value) {
@Override
public final void setProperty(String key,Object value) {
getPropertyConfig().setProperty(key, value);
}
public Object getProperty(String key) {
@Override
public final Object getProperty(String key) {
return getPropertyConfig().getProperty(key);
}
public Collection<String> getPropertyKeys() {
@Override
public final Collection<String> getPropertyKeys() {
return getPropertyConfig().getPropertyKeys();
}
public void setPhaseStop(String phaseId) {
@Override
public final void setPhaseStop(String phaseId) {
phaseStop = phaseId;
}
public void addPhaseSkip(String phaseId) {
phaseSkip.add( phaseId );
@Override
public final void addPhaseSkip(String phaseId) {
phaseSkip.add(phaseId);
}
}

View file

@ -29,6 +29,7 @@ import java.io.InputStream;
import java.net.URL;
import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.X4OLanguageSession;
import org.xml.sax.SAXException;
/**
@ -45,8 +46,11 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderSession<T> i
super(language);
}
public T read(InputStream input, String systemId, URL basePath) throws X4OConnectionException,SAXException,IOException {
return (T)readSession(input, systemId, basePath).getRootElement().getElementObject();
public T read(InputStream input, String systemId, URL basePath) throws X4OConnectionException, SAXException, IOException {
try (X4OLanguageSession session = createLanguageSession()) {
readSession(session, input, systemId, basePath);
return (T)session.getRootElement().getElementObject();
}
}
/**
@ -58,8 +62,11 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderSession<T> i
* @throws IOException Is thrown after io exception.
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readFile(String fileName) throws X4OConnectionException,SAXException,IOException,FileNotFoundException {
return (T)readFileSession(fileName).getRootElement().getElementObject();
public T readFile(String fileName) throws X4OConnectionException, SAXException, IOException, FileNotFoundException {
try (X4OLanguageSession session = createLanguageSession()) {
readFileSession(session, fileName);
return (T)session.getRootElement().getElementObject();
}
}
/**
@ -71,8 +78,11 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderSession<T> i
* @throws IOException Is thrown after io exception.
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readFile(File file) throws X4OConnectionException,SAXException,IOException,FileNotFoundException {
return (T)readFileSession(file).getRootElement().getElementObject();
public T readFile(File file) throws X4OConnectionException, SAXException, IOException, FileNotFoundException {
try (X4OLanguageSession session = createLanguageSession()) {
readFileSession(session, file);
return (T)session.getRootElement().getElementObject();
}
}
/**
@ -83,8 +93,11 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderSession<T> i
* @throws IOException Is thrown after io exception.
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readResource(String resourceName) throws X4OConnectionException,SAXException,IOException {
return (T)readResourceSession(resourceName).getRootElement().getElementObject();
public T readResource(String resourceName) throws X4OConnectionException, SAXException, IOException {
try (X4OLanguageSession session = createLanguageSession()) {
readResourceSession(session, resourceName);
return (T)session.getRootElement().getElementObject();
}
}
/**
@ -95,8 +108,11 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderSession<T> i
* @throws IOException Is thrown after io exception.
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readString(String xmlString) throws X4OConnectionException,SAXException,IOException {
return (T)readStringSession(xmlString).getRootElement().getElementObject();
public T readString(String xmlString) throws X4OConnectionException, SAXException, IOException {
try (X4OLanguageSession session = createLanguageSession()) {
readStringSession(session, xmlString);
return (T)session.getRootElement().getElementObject();
}
}
/**
@ -108,6 +124,9 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderSession<T> i
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readUrl(URL url) throws X4OConnectionException,SAXException,IOException {
return (T)readUrlSession(url).getRootElement().getElementObject();
try (X4OLanguageSession session = createLanguageSession()) {
readUrlSession(session, url);
return (T)session.getRootElement().getElementObject();
}
}
}

View file

@ -48,64 +48,35 @@ abstract public class AbstractX4OReaderSession<T> extends AbstractX4OConnection
super(language);
}
/**
* Reads the file fileName and reads it as an InputStream.
* @param fileName The file name to read.
* @throws FileNotFoundException Is thrown is file is not found.
* @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io exception.
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
*/
public X4OLanguageSession readFileSession(String fileName) throws X4OConnectionException,SAXException,IOException,FileNotFoundException {
if (fileName==null) {
public void readFileSession(X4OLanguageSession session, String fileName) throws X4OConnectionException, SAXException, IOException, FileNotFoundException {
if (fileName == null) {
throw new NullPointerException("Can't convert null fileName to file object.");
}
return readFileSession(new File(fileName));
}
readFileSession(session, new File(fileName));
}
/**
* Reads the file and reads it as an InputStream.
* @param file The file to read.
* @throws FileNotFoundException Is thrown is file is not found.
* @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io exception.
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
*/
public X4OLanguageSession readFileSession(File file) throws X4OConnectionException,SAXException,IOException,FileNotFoundException {
if (file==null) {
public void readFileSession(X4OLanguageSession session, File file) throws X4OConnectionException, SAXException, IOException, FileNotFoundException {
if (file == null) {
throw new NullPointerException("Can't read null file.");
}
if (file.exists()==false) {
if (file.exists() == false) {
throw new FileNotFoundException("File does not exists; "+file);
}
if (file.canRead()==false) {
if (file.canRead() == false) {
throw new IOException("File exists but can't read file: "+file);
}
URL basePath = new File(file.getAbsolutePath()).toURI().toURL();
InputStream inputStream = new FileInputStream(file);
try {
return readSession(inputStream,file.getAbsolutePath(),basePath);
} finally {
inputStream.close();
try (InputStream inputStream = new FileInputStream(file)) {
readSession(session, inputStream,file.getAbsolutePath(),basePath);
}
}
/**
* reads an resource locaction.
* @param resourceName The resource to readr.
* @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io exception.
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
*/
public X4OLanguageSession readResourceSession(String resourceName) throws X4OConnectionException,SAXException,IOException {
if (resourceName==null) {
public void readResourceSession(X4OLanguageSession session, String resourceName) throws X4OConnectionException, SAXException, IOException {
if (resourceName == null) {
throw new NullPointerException("Can't read null resourceName from classpath.");
}
URL url = X4OLanguageClassLoader.getResource(resourceName);
if (url==null) {
if (url == null) {
throw new NullPointerException("Could not find resource on classpath: "+resourceName);
}
String baseUrl = url.toExternalForm();
@ -114,44 +85,25 @@ abstract public class AbstractX4OReaderSession<T> extends AbstractX4OConnection
baseUrl = baseUrl.substring(0,lastSlash+1);
}
URL basePath = new URL(baseUrl);
InputStream inputStream = X4OLanguageClassLoader.getResourceAsStream(resourceName);
try {
return readSession(inputStream,url.toExternalForm(),basePath);
} finally {
inputStream.close();
try (InputStream inputStream = X4OLanguageClassLoader.getResourceAsStream(resourceName)) {
readSession(session, inputStream,url.toExternalForm(),basePath);
}
}
/**
* Converts a String to a InputStream to is can me read by SAX.
* @param xmlString The xml as (UTF-8) String to read.
* @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io exception.
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
*/
public X4OLanguageSession readStringSession(String xmlString) throws X4OConnectionException,SAXException,IOException {
if (xmlString==null) {
public void readStringSession(X4OLanguageSession session, String xmlString) throws X4OConnectionException, SAXException, IOException {
if (xmlString == null) {
throw new NullPointerException("Can't read null xml string.");
}
URL basePath = new File(System.getProperty("user.dir")).toURI().toURL();
String encoding = (String)getProperty(DefaultX4OReader.INPUT_ENCODING);
return readSession(new ByteArrayInputStream(xmlString.getBytes(encoding)),"inline-xml",basePath);
readSession(session, new ByteArrayInputStream(xmlString.getBytes(encoding)),"inline-xml",basePath);
}
/**
* Fetched the data direct from remote url to a InputStream to is can me readd by SAX.
* @param url The url to read.
* @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io exception.
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
*/
public X4OLanguageSession readUrlSession(URL url) throws X4OConnectionException,SAXException,IOException {
if (url==null) {
public void readUrlSession(X4OLanguageSession session, URL url) throws X4OConnectionException, SAXException, IOException {
if (url == null) {
throw new NullPointerException("Can't read null url.");
}
URL basePath = new URL(url.toExternalForm().substring(0,url.toExternalForm().length()-url.getFile().length()));
return readSession(url.openStream(),url.toExternalForm(),basePath);
readSession(session, url.openStream(),url.toExternalForm(),basePath);
}
}

View file

@ -45,9 +45,7 @@ public abstract class AbstractX4OWriter<T> extends AbstractX4OWriterSession<T> i
super(language);
}
abstract X4OLanguageSession createLanguageSession();
private X4OLanguageSession toObjectContext(T object) throws SAXException {
protected X4OLanguageSession createObjectContext(T object) throws SAXException {
X4OLanguageSession context = createLanguageSession();
Element rootElement = null;
try {
@ -64,18 +62,26 @@ public abstract class AbstractX4OWriter<T> extends AbstractX4OWriterSession<T> i
}
public void write(T object,OutputStream output) throws X4OConnectionException,SAXException,IOException {
writeSession(toObjectContext(object), output);
try (X4OLanguageSession session = createObjectContext(object)) {
writeSession(session, output);
}
}
public void writeFile(T object,String fileName) throws X4OConnectionException,SAXException,IOException,FileNotFoundException {
writeFileSession(toObjectContext(object), fileName);
try (X4OLanguageSession session = createObjectContext(object)) {
writeFileSession(session, fileName);
}
}
public void writeFile(T object,File file) throws X4OConnectionException,SAXException,IOException,FileNotFoundException {
writeFileSession(toObjectContext(object), file);
try (X4OLanguageSession session = createObjectContext(object)) {
writeFileSession(session, file);
}
}
public String writeString(T object) throws X4OConnectionException,SAXException,IOException,FileNotFoundException {
return writeStringSession(toObjectContext(object));
try (X4OLanguageSession session = createObjectContext(object)) {
return writeStringSession(session);
}
}
}

View file

@ -37,7 +37,7 @@ import org.xml.sax.SAXException;
* AbstractX4OWriterSession.
*
* @author Willem Cazander
* @version 1.0 Apr 6, 2013\
* @version 1.0 Apr 6, 2013
* @param <T> The root element object type.
*/
public abstract class AbstractX4OWriterSession<T> extends AbstractX4OConnection implements X4OWriterSession<T> {
@ -47,21 +47,18 @@ public abstract class AbstractX4OWriterSession<T> extends AbstractX4OConnection
}
public void writeFileSession(X4OLanguageSession languageSession,String fileName) throws X4OConnectionException,SAXException,IOException {
if (fileName==null) {
if (fileName == null) {
throw new NullPointerException("Can't convert null fileName to file object.");
}
}
writeFileSession(languageSession,new File(fileName));
}
public void writeFileSession(X4OLanguageSession languageSession,File file) throws X4OConnectionException,SAXException,IOException {
if (file==null) {
if (file == null) {
throw new NullPointerException("Can't read null file.");
}
OutputStream outputStream = new FileOutputStream(file);
try {
try (OutputStream outputStream = new FileOutputStream(file)) {
writeSession(languageSession,outputStream);
} finally {
outputStream.close();
}
}

View file

@ -27,6 +27,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.el.ValueExpression;
import javax.xml.parsers.ParserConfigurationException;
@ -54,12 +56,10 @@ import org.xml.sax.SAXException;
*/
public class DefaultX4OReader<T> extends AbstractX4OReader<T> {
private X4OLanguageSession languageSession = null;
private final SAX3PropertyConfig propertyConfig;
private final Map<String, Object> elBeans = new HashMap<>();
private final static String PROPERTY_CONTEXT_PREFIX = SAX3PropertyConfig.X4O_PROPERTIES_PREFIX+"reader/x4o/";
public final static SAX3PropertyConfig DEFAULT_PROPERTY_CONFIG;
public final static String SAX_ERROR_HANDLER = PROPERTY_CONTEXT_PREFIX + "sax/error-handler";
public final static String SAX_ENTITY_RESOLVER = PROPERTY_CONTEXT_PREFIX + "sax/entity-resolver";
@ -101,7 +101,6 @@ public class DefaultX4OReader<T> extends AbstractX4OReader<T> {
public DefaultX4OReader(X4OLanguage language) {
super(language);
languageSession = language.createLanguageSession();
propertyConfig = new SAX3PropertyConfig(DEFAULT_PROPERTY_CONFIG,PROPERTY_CONTEXT_PREFIX);
}
@ -110,15 +109,20 @@ public class DefaultX4OReader<T> extends AbstractX4OReader<T> {
return propertyConfig;
}
public X4OLanguageSession readSession(InputStream input, String systemId, URL basePath) throws X4OConnectionException, SAXException, IOException {
@Override
public void readSession(X4OLanguageSession languageSession, InputStream input, String systemId, URL basePath) throws X4OConnectionException, SAXException, IOException {
setProperty(INPUT_STREAM, input);
setProperty(INPUT_SYSTEM_ID, systemId);
setProperty(INPUT_BASE_PATH, basePath);
read();
return languageSession;
for (String name : elBeans.keySet()) {
Object bean = elBeans.get(name);
ValueExpression ve = languageSession.getExpressionLanguageFactory().createValueExpression(languageSession.getExpressionLanguageContext(),"${"+name+"}", bean.getClass());
ve.setValue(languageSession.getExpressionLanguageContext(), bean);
}
readSession(languageSession);
}
public void addELBeanInstance(String name,Object bean) {
public void addELBeanInstance(String name, Object bean) {
if (name==null) {
throw new NullPointerException("Can't add null name.");
}
@ -128,14 +132,13 @@ public class DefaultX4OReader<T> extends AbstractX4OReader<T> {
if (bean==null) {
throw new NullPointerException("Can't add null bean.");
}
ValueExpression ve = languageSession.getExpressionLanguageFactory().createValueExpression(languageSession.getExpressionLanguageContext(),"${"+name+"}", bean.getClass());
ve.setValue(languageSession.getExpressionLanguageContext(), bean);
elBeans.put(name, bean);
}
/**
* Parses the input stream as a X4O document.
*/
protected void read() throws X4OConnectionException,SAXException,IOException {
protected void readSession(X4OLanguageSession languageSession) throws X4OConnectionException,SAXException,IOException {
// Extra check if we have a language
if (languageSession.getLanguage()==null) {
throw new X4OConnectionException("languageSession is broken getLanguage() returns null.");
@ -186,17 +189,4 @@ public class DefaultX4OReader<T> extends AbstractX4OReader<T> {
debugStop(languageSession);
}
}
public void releaseSession(X4OLanguageSession context) throws X4OPhaseException {
if (context==null) {
return;
}
if (context.getLanguage()==null) {
return;
}
if (context.getLanguage().getPhaseManager()==null) {
return;
}
context.getLanguage().getPhaseManager().doReleasePhaseManual(context);
}
}

View file

@ -105,10 +105,6 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
return propertyConfig;
}
X4OLanguageSession createLanguageSession() {
return getLanguage().createLanguageSession();
}
/**
* @see org.x4o.xml.io.X4OWriterSession#writeSession(org.x4o.xml.lang.X4OLanguageSession, java.io.OutputStream)
*/

View file

@ -24,26 +24,33 @@ package org.x4o.xml.io;
import java.util.Collection;
import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.X4OLanguageSession;
/**
* X4OConnection interface for config the io drivers.
* X4OConnection interface for config the io-drivers.
*
* @author Willem Cazander
* @version 1.0 Apr 6, 2013
*/
public interface X4OConnection {
X4OLanguage getLanguage();
X4OLanguageSession createLanguageSession();
/**
* Sets an X4O Language property.
* @param key The key of the property to set.
* @param value The vlue of the property to set.
* @param value The value of the property to set.
*/
void setProperty(String key,Object value);
void setProperty(String key, Object value);
public Object getProperty(String key);
Object getProperty(String key);
public Collection<String> getPropertyKeys();
Collection<String> getPropertyKeys();
public void setPhaseStop(String phaseId);
void setPhaseStop(String phaseId);
public void addPhaseSkip(String phaseId);
void addPhaseSkip(String phaseId);
}

View file

@ -29,7 +29,6 @@ import java.io.InputStream;
import java.net.URL;
import org.x4o.xml.lang.X4OLanguageSession;
import org.x4o.xml.lang.phase.X4OPhaseException;
import org.xml.sax.SAXException;
/**
@ -40,66 +39,70 @@ import org.xml.sax.SAXException;
*/
public interface X4OReaderSession<T> extends X4OReader<T> {
void releaseSession(X4OLanguageSession context) throws X4OPhaseException;
/**
* Method to parse the xml data.
* @param session The language session.
* @param input The inputStream to parse.
* @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io exception.
*/
X4OLanguageSession readSession(InputStream input,String systemId,URL basePath) throws X4OConnectionException,SAXException,IOException;
void readSession(X4OLanguageSession session, InputStream input, String systemId, URL basePath) throws X4OConnectionException, SAXException, IOException;
/**
* Reads the file fileName and parses it as an InputStream.
* @param session The language session.
* @param fileName The file name to parse.
* @throws FileNotFoundException Is thrown is file is not found.
* @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io exception.
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
* @see org.x4o.xml.io.X4OReaderSession#readSession(org.x4o.xml.lang.X4OLanguageSession,java.io.InputStream,java.lang.String,java.net.URL)
*/
X4OLanguageSession readFileSession(String fileName) throws X4OConnectionException,SAXException,IOException,FileNotFoundException;
void readFileSession(X4OLanguageSession session, String fileName) throws X4OConnectionException, SAXException, IOException, FileNotFoundException;
/**
* Reads the file and parses it as an InputStream.
* @param session The language session.
* @param file The file to parse.
* @throws FileNotFoundException Is thrown is file is not found.
* @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io exception.
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
* @see org.x4o.xml.io.X4OReaderSession#readSession(org.x4o.xml.lang.X4OLanguageSession,java.io.InputStream,java.lang.String,java.net.URL)
*/
X4OLanguageSession readFileSession(File file) throws X4OConnectionException,SAXException,IOException,FileNotFoundException;
void readFileSession(X4OLanguageSession session, File file) throws X4OConnectionException, SAXException, IOException, FileNotFoundException;
/**
* Parses an resource locaction.
* @param session The language session.
* @param resourceName The resource to parser.
* @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io exception.
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
* @see org.x4o.xml.io.X4OReaderSession#readSession(org.x4o.xml.lang.X4OLanguageSession,java.io.InputStream,java.lang.String,java.net.URL)
*/
X4OLanguageSession readResourceSession(String resourceName) throws X4OConnectionException,SAXException,IOException;
void readResourceSession(X4OLanguageSession session, String resourceName) throws X4OConnectionException, SAXException, IOException;
/**
* Converts a String to a InputStream to is can be parsed by SAX.
* @param session The language session.
* @param xmlString The xml as String to parse.
* @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io exception.
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
* @see org.x4o.xml.io.X4OReaderSession#readSession(org.x4o.xml.lang.X4OLanguageSession,java.io.InputStream,java.lang.String,java.net.URL)
*/
X4OLanguageSession readStringSession(String xmlString) throws X4OConnectionException,SAXException,IOException;
void readStringSession(X4OLanguageSession session, String xmlString) throws X4OConnectionException, SAXException, IOException;
/**
* Fetched the data direct from remote url to a InputStream to is can be parsed by SAX.
* @param session The language session.
* @param url The url to parse.
* @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io exception.
* @see org.x4o.xml.io.X4OReaderSession#readSession(java.io.InputStream,java.lang.String,java.net.URL)
* @see org.x4o.xml.io.X4OReaderSession#readSession(org.x4o.xml.lang.X4OLanguageSession,java.io.InputStream,java.lang.String,java.net.URL)
*/
X4OLanguageSession readUrlSession(URL url) throws X4OConnectionException,SAXException,IOException;
void readUrlSession(X4OLanguageSession session, URL url) throws X4OConnectionException, SAXException, IOException;
}

View file

@ -37,11 +37,11 @@ import org.xml.sax.SAXException;
*/
public interface X4OWriterSession<T> extends X4OWriter<T> {
void writeSession(X4OLanguageSession context,OutputStream out) throws X4OConnectionException,SAXException,IOException;
void writeSession(X4OLanguageSession session, OutputStream out) throws X4OConnectionException, SAXException, IOException;
void writeFileSession(X4OLanguageSession context,String fileName) throws X4OConnectionException,SAXException,IOException;
void writeFileSession(X4OLanguageSession session, String fileName) throws X4OConnectionException, SAXException, IOException;
void writeFileSession(X4OLanguageSession context,File file) throws X4OConnectionException,SAXException,IOException;
void writeFileSession(X4OLanguageSession session, File file) throws X4OConnectionException, SAXException, IOException;
String writeStringSession(X4OLanguageSession context) throws X4OConnectionException,SAXException,IOException;
String writeStringSession(X4OLanguageSession session) throws X4OConnectionException, SAXException, IOException;
}

View file

@ -38,7 +38,6 @@ import org.x4o.xml.element.ElementInterface;
import org.x4o.xml.element.ElementNamespace;
import org.x4o.xml.element.ElementNamespaceInstanceProviderException;
import org.x4o.xml.element.ElementObjectPropertyValue;
import org.x4o.xml.lang.phase.X4OPhaseException;
import org.x4o.xml.lang.phase.X4OPhaseManager;
/**
@ -135,28 +134,24 @@ public class DefaultX4OLanguage implements X4OLanguageLocal {
return result;
}
protected X4OLanguageSession buildElementLanguage(X4OLanguageSession languageSession) {
if ((languageSession instanceof X4OLanguageSessionLocal)==false) {
throw new RuntimeException("Can't init X4OLanguageSession which has not X4OLanguageSessionLocal interface obj: "+languageSession);
}
X4OLanguageSessionLocal contextInit = (X4OLanguageSessionLocal)languageSession;
protected X4OLanguageSession buildElementLanguage(X4OLanguageSessionLocal languageSession) {
try {
if (contextInit.getExpressionLanguageFactory()==null) {
contextInit.setExpressionLanguageFactory(X4OExpressionFactory.createExpressionFactory());
if (languageSession.getExpressionLanguageFactory()==null) {
languageSession.setExpressionLanguageFactory(X4OExpressionFactory.createExpressionFactory());
}
if (contextInit.getExpressionLanguageContext()==null) {
contextInit.setExpressionLanguageContext(X4OExpressionFactory.createELContext(contextInit.getLanguage().getLanguageConfiguration().getDefaultExpressionLanguageContext()));
if (languageSession.getExpressionLanguageContext()==null) {
languageSession.setExpressionLanguageContext(X4OExpressionFactory.createELContext(languageSession.getLanguage().getLanguageConfiguration().getDefaultExpressionLanguageContext()));
}
if (contextInit.getElementAttributeValueParser()==null) {
contextInit.setElementAttributeValueParser((ElementAttributeValueParser)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultElementAttributeValueParser()));
if (languageSession.getElementAttributeValueParser()==null) {
languageSession.setElementAttributeValueParser((ElementAttributeValueParser)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultElementAttributeValueParser()));
}
if (contextInit.getElementObjectPropertyValue()==null) {
contextInit.setElementObjectPropertyValue((ElementObjectPropertyValue)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultElementObjectPropertyValue()));
if (languageSession.getElementObjectPropertyValue()==null) {
languageSession.setElementObjectPropertyValue((ElementObjectPropertyValue)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultElementObjectPropertyValue()));
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage(),e);
}
return contextInit;
return languageSession;
}
/**

View file

@ -22,6 +22,8 @@
*/
package org.x4o.xml.lang;
import org.x4o.xml.io.X4OConnectionException;
import org.x4o.xml.lang.phase.X4OPhaseException;
/**
* An DefaultX4OLanguageSession.
@ -37,4 +39,16 @@ public class DefaultX4OLanguageSession extends AbstractX4OLanguageSession {
public DefaultX4OLanguageSession(X4OLanguage language) {
super(language);
}
@Override
public void close() throws X4OConnectionException {
if (getLanguage().getPhaseManager() == null) {
return;
}
try {
getLanguage().getPhaseManager().doReleasePhaseManual(this);
} catch (X4OPhaseException e) {
throw new X4OConnectionException(e);
}
}
}

View file

@ -30,6 +30,7 @@ import javax.el.ExpressionFactory;
import org.x4o.xml.element.Element;
import org.x4o.xml.element.ElementAttributeValueParser;
import org.x4o.xml.element.ElementObjectPropertyValue;
import org.x4o.xml.io.X4OConnectionException;
import org.x4o.xml.io.X4ODebugWriter;
import org.x4o.xml.lang.phase.X4OPhase;
@ -39,7 +40,7 @@ import org.x4o.xml.lang.phase.X4OPhase;
* @author Willem Cazander
* @version 1.0 Feb 14, 2007
*/
public interface X4OLanguageSession {
public interface X4OLanguageSession extends AutoCloseable {
/**
* @return Returns the language from which this session in created.
@ -112,10 +113,15 @@ public interface X4OLanguageSession {
/**
* @return Returns the phase this session will stop processing.
*/
public String getPhaseStop();
String getPhaseStop();
/**
* @return Returns a list of phases we skip while processing.
*/
public List<String> getPhaseSkip();
List<String> getPhaseSkip();
/**
* Closes this language session
*/
void close() throws X4OConnectionException;
}

View file

@ -225,7 +225,8 @@ PHASE_ORDER = { *startupX4OPhase,
releaseRequested = X4OPhaseLanguageWrite.WRITE_RELEASE;
}
if (releaseRequested==null) {
throw new IllegalStateException("No manual release requested.");
return; // No manual release requested
//throw new IllegalStateException("No manual release requested.");
}
if (languageSession.getRootElement()==null) {
return; // no root element , empty xml document ?

View file

@ -41,44 +41,35 @@ public class NamespaceUriTest {
@Test
public void testSimpleUri() throws Exception {
X4OLanguageSession context = null;
TestDriver driver = TestDriver.getInstance();
X4OReaderSession<TestObjectRoot> reader = driver.createReaderSession();
reader.addPhaseSkip(X4OPhaseLanguageRead.READ_RELEASE);
try {
context = reader.readResourceSession("tests/namespace/uri-simple.xml");
Assertions.assertEquals(true,context.getRootElement().getChilderen().size()==1);
} finally {
reader.releaseSession(context);
try (X4OLanguageSession session = reader.createLanguageSession()) {
reader.readResourceSession(session, "tests/namespace/uri-simple.xml");
Assertions.assertEquals(true,session.getRootElement().getChilderen().size()==1);
}
}
@Test
public void testEmptyUri() throws Exception {
X4OLanguageSession context = null;
TestDriver driver = TestDriver.getInstance();
X4OReaderSession<TestObjectRoot> reader = driver.createReaderSession();
reader.addPhaseSkip(X4OPhaseLanguageRead.READ_RELEASE);
reader.setProperty(DefaultX4OReader.DOC_EMPTY_NAMESPACE_URI, "http://test.junit.x4o.org/xml/ns/junit-test-lang");
try {
context = reader.readResourceSession("tests/namespace/uri-empty.xml");
Assertions.assertEquals(true,context.getRootElement().getChilderen().size()==1);
} finally {
reader.releaseSession(context);
try (X4OLanguageSession session = reader.createLanguageSession()) {
reader.readResourceSession(session, "tests/namespace/uri-empty.xml");
Assertions.assertEquals(true,session.getRootElement().getChilderen().size()==1);
}
}
@Test
public void testSchemaUri() throws Exception {
X4OLanguageSession context = null;
TestDriver driver = TestDriver.getInstance();
X4OReaderSession<TestObjectRoot> reader = driver.createReaderSession();
reader.addPhaseSkip(X4OPhaseLanguageRead.READ_RELEASE);
try {
context = reader.readResourceSession("tests/namespace/uri-schema.xml");
Assertions.assertEquals(true,context.getRootElement().getChilderen().size()==1);
} finally {
reader.releaseSession(context);
try (X4OLanguageSession session = reader.createLanguageSession()) {
reader.readResourceSession(session, "tests/namespace/uri-schema.xml");
Assertions.assertEquals(true,session.getRootElement().getChilderen().size()==1);
}
}
}

View file

@ -86,8 +86,8 @@ public class X4ODebugWriterTest {
Assertions.assertFalse(debug.length()==0, "no debug content");
Assertions.assertTrue(debug.length()>20, "debug content to small");
//System.out.println("=================== Reader Output ======================");
//System.out.println(debug);
System.out.println("=================== Reader Output ======================");
System.out.println(debug);
debugFile.delete();
}

View file

@ -62,8 +62,7 @@ public class X4OReaderSessionTest {
TestDriver driver = TestDriver.getInstance();
X4OReaderSession<TestObjectRoot> reader = driver.createReaderSession();
File xmlFile = copyResourceToTempFile();
X4OLanguageSession context = reader.readFileSession(xmlFile.getAbsolutePath());
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
TestObjectRoot root = reader.readFile(xmlFile.getAbsolutePath());
Assertions.assertNotNull(root);
Assertions.assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
@ -77,7 +76,7 @@ public class X4OReaderSessionTest {
Exception e = null;
try {
String nullFileName = null;
reader.readFileSession(nullFileName);
reader.readFile(nullFileName);
} catch (Exception catchE) {
e = catchE;
}
@ -92,8 +91,7 @@ public class X4OReaderSessionTest {
TestDriver driver = TestDriver.getInstance();
X4OReaderSession<TestObjectRoot> reader = driver.createReaderSession();
File xmlFile = copyResourceToTempFile();
X4OLanguageSession context = reader.readFileSession(xmlFile);
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
TestObjectRoot root = reader.readFile(xmlFile);
Assertions.assertNotNull(root);
Assertions.assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
@ -107,7 +105,7 @@ public class X4OReaderSessionTest {
Exception e = null;
try {
File nullFile = null;
reader.readFileSession(nullFile);
reader.readFile(nullFile);
} catch (Exception catchE) {
e = catchE;
}
@ -125,7 +123,7 @@ public class X4OReaderSessionTest {
try {
File tempFile = File.createTempFile("test-file", ".xml");
tempFile.delete();
reader.readFileSession(tempFile);
reader.readFile(tempFile);
} catch (Exception catchE) {
e = catchE;
}
@ -144,7 +142,7 @@ public class X4OReaderSessionTest {
X4OReaderSession<TestObjectRoot> reader = driver.createReaderSession();
Exception e = null;
try {
reader.readFileSession(new File("/etc/shadow"));
reader.readFile(new File("/etc/shadow"));
} catch (Exception catchE) {
e = catchE;
}
@ -158,8 +156,7 @@ public class X4OReaderSessionTest {
public void testReadResource() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderSession<TestObjectRoot> reader = driver.createReaderSession();
X4OLanguageSession context = reader.readResourceSession("tests/attributes/test-bean.xml");
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
TestObjectRoot root = reader.readResource("tests/attributes/test-bean.xml");
Assertions.assertNotNull(root);
}
@ -169,7 +166,7 @@ public class X4OReaderSessionTest {
X4OReaderSession<TestObjectRoot> reader = driver.createReaderSession();
Exception e = null;
try {
reader.readResourceSession(null);
reader.readResource(null);
} catch (Exception catchE) {
e = catchE;
}
@ -183,13 +180,12 @@ public class X4OReaderSessionTest {
public void testReadString() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderSession<TestObjectRoot> reader = driver.createReaderSession();
X4OLanguageSession context = reader.readStringSession(
TestObjectRoot root = reader.readString(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<root:root xmlns:root=\"http://test.junit.x4o.org/xml/ns/junit-test-root\" xmlns=\"http://test.junit.x4o.org/xml/ns/junit-test-lang\">"+
"<testBean privateIntegerTypeField=\"987654321\"/>"+
"</root:root>"
);
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
Assertions.assertNotNull(root);
Assertions.assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
@ -203,7 +199,7 @@ public class X4OReaderSessionTest {
X4OReaderSession<TestObjectRoot> reader = driver.createReaderSession();
Exception e = null;
try {
reader.readStringSession(null);
reader.readString(null);
} catch (Exception catchE) {
e = catchE;
}
@ -218,8 +214,7 @@ public class X4OReaderSessionTest {
TestDriver driver = TestDriver.getInstance();
X4OReaderSession<TestObjectRoot> reader = driver.createReaderSession();
URL xmlUrl = Thread.currentThread().getContextClassLoader().getResource("tests/attributes/test-bean.xml");
X4OLanguageSession context = reader.readUrlSession(xmlUrl);
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
TestObjectRoot root = reader.readUrl(xmlUrl);
Assertions.assertNotNull(root);
Assertions.assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
@ -232,7 +227,7 @@ public class X4OReaderSessionTest {
X4OReaderSession<TestObjectRoot> reader = driver.createReaderSession();
Exception e = null;
try {
reader.readUrlSession(null);
reader.readUrl(null);
} catch (Exception catchE) {
e = catchE;
}

View file

@ -42,18 +42,15 @@ public class ParentObjectTest {
@Test
public void testParentElement() throws Exception {
X4OLanguageSession context = null;
MTestDriver driver = new MTestDriver();
X4OReaderSession<?> reader = driver.createReaderSession();
reader.addPhaseSkip(X4OPhaseLanguageRead.READ_RELEASE);
try {
context = reader.readResourceSession("junit/test-meta-parent-element.xml");
Assertions.assertEquals(1,context.getRootElement().getChilderen().size());
Element childElement = context.getRootElement().getChilderen().get(0);
try (X4OLanguageSession session = reader.createLanguageSession()) {
reader.readResourceSession(session, "junit/test-meta-parent-element.xml");
Assertions.assertEquals(1,session.getRootElement().getChilderen().size());
Element childElement = session.getRootElement().getChilderen().get(0);
JLabel test = (JLabel)childElement.getElementObject();
Assertions.assertEquals("parentTest",test.getText());
} finally {
reader.releaseSession(context);
}
}

View file

@ -40,46 +40,37 @@ public class ReferenceStoreTest {
@Test
public void testMetaGeneric() throws Exception {
X4OLanguageSession context = null;
MTestDriver driver = new MTestDriver();
X4OReaderSession<?> reader = driver.createReaderSession();
reader.addPhaseSkip(X4OPhaseLanguageRead.READ_RELEASE);
try {
context = reader.readResourceSession("junit/test-meta-generic.xml");
Assertions.assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(0).getElementObject().getClass().getName());
} finally {
reader.releaseSession(context);
try (X4OLanguageSession session = reader.createLanguageSession()) {
reader.readResourceSession(session, "junit/test-meta-generic.xml");
Assertions.assertEquals(Date.class.getName(),session.getRootElement().getChilderen().get(0).getElementObject().getClass().getName());
}
}
@Test
public void testLoadClass() throws Exception {
X4OLanguageSession context = null;
MTestDriver driver = new MTestDriver();
X4OReaderSession<?> reader = driver.createReaderSession();
reader.addPhaseSkip(X4OPhaseLanguageRead.READ_RELEASE);
try {
context = reader.readResourceSession("junit/test-meta-reference.xml");
Assertions.assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(0).getElementObject().getClass().getName());
} finally {
reader.releaseSession(context);
try (X4OLanguageSession session = reader.createLanguageSession()) {
reader.readResourceSession(session, "junit/test-meta-reference.xml");
Assertions.assertEquals(Date.class.getName(),session.getRootElement().getChilderen().get(0).getElementObject().getClass().getName());
}
}
@Test
public void testStoreRef() throws Exception {
X4OLanguageSession context = null;
MTestDriver driver = new MTestDriver();
X4OReaderSession<?> reader = driver.createReaderSession();
reader.addPhaseSkip(X4OPhaseLanguageRead.READ_RELEASE);
try {
context = reader.readResourceSession("junit/test-meta-reference.xml");
Assertions.assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(0).getElementObject().getClass().getName());
Assertions.assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(1).getElementObject().getClass().getName());
Assertions.assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(2).getElementObject().getClass().getName());
Assertions.assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(3).getElementObject().getClass().getName());
} finally {
reader.releaseSession(context);
try (X4OLanguageSession session = reader.createLanguageSession()) {
reader.readResourceSession(session, "junit/test-meta-reference.xml");
Assertions.assertEquals(Date.class.getName(),session.getRootElement().getChilderen().get(0).getElementObject().getClass().getName());
Assertions.assertEquals(Date.class.getName(),session.getRootElement().getChilderen().get(1).getElementObject().getClass().getName());
Assertions.assertEquals(Date.class.getName(),session.getRootElement().getChilderen().get(2).getElementObject().getClass().getName());
Assertions.assertEquals(Date.class.getName(),session.getRootElement().getChilderen().get(3).getElementObject().getClass().getName());
}
}
}