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

View file

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

View file

@ -29,6 +29,7 @@ import java.io.InputStream;
import java.net.URL; import java.net.URL;
import org.x4o.xml.lang.X4OLanguage; import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.X4OLanguageSession;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
/** /**
@ -45,8 +46,11 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderSession<T> i
super(language); super(language);
} }
public T read(InputStream input, String systemId, URL basePath) throws X4OConnectionException,SAXException,IOException { public T read(InputStream input, String systemId, URL basePath) throws X4OConnectionException, SAXException, IOException {
return (T)readSession(input, systemId, basePath).getRootElement().getElementObject(); 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. * @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(java.io.InputStream,java.lang.String,java.net.URL)
*/ */
public T readFile(String fileName) throws X4OConnectionException,SAXException,IOException,FileNotFoundException { public T readFile(String fileName) throws X4OConnectionException, SAXException, IOException, FileNotFoundException {
return (T)readFileSession(fileName).getRootElement().getElementObject(); 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. * @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(java.io.InputStream,java.lang.String,java.net.URL)
*/ */
public T readFile(File file) throws X4OConnectionException,SAXException,IOException,FileNotFoundException { public T readFile(File file) throws X4OConnectionException, SAXException, IOException, FileNotFoundException {
return (T)readFileSession(file).getRootElement().getElementObject(); 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. * @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(java.io.InputStream,java.lang.String,java.net.URL)
*/ */
public T readResource(String resourceName) throws X4OConnectionException,SAXException,IOException { public T readResource(String resourceName) throws X4OConnectionException, SAXException, IOException {
return (T)readResourceSession(resourceName).getRootElement().getElementObject(); 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. * @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(java.io.InputStream,java.lang.String,java.net.URL)
*/ */
public T readString(String xmlString) throws X4OConnectionException,SAXException,IOException { public T readString(String xmlString) throws X4OConnectionException, SAXException, IOException {
return (T)readStringSession(xmlString).getRootElement().getElementObject(); 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) * @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 { 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); super(language);
} }
/** public void readFileSession(X4OLanguageSession session, String fileName) throws X4OConnectionException, SAXException, IOException, FileNotFoundException {
* Reads the file fileName and reads it as an InputStream. if (fileName == null) {
* @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) {
throw new NullPointerException("Can't convert null fileName to file object."); throw new NullPointerException("Can't convert null fileName to file object.");
} }
return readFileSession(new File(fileName)); readFileSession(session, new File(fileName));
} }
/** public void readFileSession(X4OLanguageSession session, File file) throws X4OConnectionException, SAXException, IOException, FileNotFoundException {
* Reads the file and reads it as an InputStream. if (file == null) {
* @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) {
throw new NullPointerException("Can't read null file."); throw new NullPointerException("Can't read null file.");
} }
if (file.exists()==false) { if (file.exists() == false) {
throw new FileNotFoundException("File does not exists; "+file); 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); throw new IOException("File exists but can't read file: "+file);
} }
URL basePath = new File(file.getAbsolutePath()).toURI().toURL(); URL basePath = new File(file.getAbsolutePath()).toURI().toURL();
InputStream inputStream = new FileInputStream(file); try (InputStream inputStream = new FileInputStream(file)) {
try { readSession(session, inputStream,file.getAbsolutePath(),basePath);
return readSession(inputStream,file.getAbsolutePath(),basePath);
} finally {
inputStream.close();
} }
} }
/** public void readResourceSession(X4OLanguageSession session, String resourceName) throws X4OConnectionException, SAXException, IOException {
* reads an resource locaction. if (resourceName == null) {
* @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) {
throw new NullPointerException("Can't read null resourceName from classpath."); throw new NullPointerException("Can't read null resourceName from classpath.");
} }
URL url = X4OLanguageClassLoader.getResource(resourceName); URL url = X4OLanguageClassLoader.getResource(resourceName);
if (url==null) { if (url == null) {
throw new NullPointerException("Could not find resource on classpath: "+resourceName); throw new NullPointerException("Could not find resource on classpath: "+resourceName);
} }
String baseUrl = url.toExternalForm(); String baseUrl = url.toExternalForm();
@ -114,44 +85,25 @@ abstract public class AbstractX4OReaderSession<T> extends AbstractX4OConnection
baseUrl = baseUrl.substring(0,lastSlash+1); baseUrl = baseUrl.substring(0,lastSlash+1);
} }
URL basePath = new URL(baseUrl); URL basePath = new URL(baseUrl);
InputStream inputStream = X4OLanguageClassLoader.getResourceAsStream(resourceName); try (InputStream inputStream = X4OLanguageClassLoader.getResourceAsStream(resourceName)) {
try { readSession(session, inputStream,url.toExternalForm(),basePath);
return readSession(inputStream,url.toExternalForm(),basePath);
} finally {
inputStream.close();
} }
} }
/** public void readStringSession(X4OLanguageSession session, String xmlString) throws X4OConnectionException, SAXException, IOException {
* Converts a String to a InputStream to is can me read by SAX. if (xmlString == null) {
* @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) {
throw new NullPointerException("Can't read null xml string."); throw new NullPointerException("Can't read null xml string.");
} }
URL basePath = new File(System.getProperty("user.dir")).toURI().toURL(); URL basePath = new File(System.getProperty("user.dir")).toURI().toURL();
String encoding = (String)getProperty(DefaultX4OReader.INPUT_ENCODING); 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);
} }
/** public void readUrlSession(X4OLanguageSession session, URL url) throws X4OConnectionException, SAXException, IOException {
* Fetched the data direct from remote url to a InputStream to is can me readd by SAX. if (url == null) {
* @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) {
throw new NullPointerException("Can't read null url."); throw new NullPointerException("Can't read null url.");
} }
URL basePath = new URL(url.toExternalForm().substring(0,url.toExternalForm().length()-url.getFile().length())); 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); super(language);
} }
abstract X4OLanguageSession createLanguageSession(); protected X4OLanguageSession createObjectContext(T object) throws SAXException {
private X4OLanguageSession toObjectContext(T object) throws SAXException {
X4OLanguageSession context = createLanguageSession(); X4OLanguageSession context = createLanguageSession();
Element rootElement = null; Element rootElement = null;
try { 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 { 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 { 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 { 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 { 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. * AbstractX4OWriterSession.
* *
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Apr 6, 2013\ * @version 1.0 Apr 6, 2013
* @param <T> The root element object type. * @param <T> The root element object type.
*/ */
public abstract class AbstractX4OWriterSession<T> extends AbstractX4OConnection implements X4OWriterSession<T> { 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 { 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."); throw new NullPointerException("Can't convert null fileName to file object.");
} }
writeFileSession(languageSession,new File(fileName)); writeFileSession(languageSession,new File(fileName));
} }
public void writeFileSession(X4OLanguageSession languageSession,File file) throws X4OConnectionException,SAXException,IOException { 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."); throw new NullPointerException("Can't read null file.");
} }
OutputStream outputStream = new FileOutputStream(file); try (OutputStream outputStream = new FileOutputStream(file)) {
try {
writeSession(languageSession,outputStream); writeSession(languageSession,outputStream);
} finally {
outputStream.close();
} }
} }

View file

@ -27,6 +27,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URL; import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.el.ValueExpression; import javax.el.ValueExpression;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
@ -54,12 +56,10 @@ import org.xml.sax.SAXException;
*/ */
public class DefaultX4OReader<T> extends AbstractX4OReader<T> { public class DefaultX4OReader<T> extends AbstractX4OReader<T> {
private X4OLanguageSession languageSession = null;
private final SAX3PropertyConfig propertyConfig; 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/"; private final static String PROPERTY_CONTEXT_PREFIX = SAX3PropertyConfig.X4O_PROPERTIES_PREFIX+"reader/x4o/";
public final static SAX3PropertyConfig DEFAULT_PROPERTY_CONFIG; 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_ERROR_HANDLER = PROPERTY_CONTEXT_PREFIX + "sax/error-handler";
public final static String SAX_ENTITY_RESOLVER = PROPERTY_CONTEXT_PREFIX + "sax/entity-resolver"; 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) { public DefaultX4OReader(X4OLanguage language) {
super(language); super(language);
languageSession = language.createLanguageSession();
propertyConfig = new SAX3PropertyConfig(DEFAULT_PROPERTY_CONFIG,PROPERTY_CONTEXT_PREFIX); propertyConfig = new SAX3PropertyConfig(DEFAULT_PROPERTY_CONFIG,PROPERTY_CONTEXT_PREFIX);
} }
@ -110,15 +109,20 @@ public class DefaultX4OReader<T> extends AbstractX4OReader<T> {
return propertyConfig; 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_STREAM, input);
setProperty(INPUT_SYSTEM_ID, systemId); setProperty(INPUT_SYSTEM_ID, systemId);
setProperty(INPUT_BASE_PATH, basePath); setProperty(INPUT_BASE_PATH, basePath);
read(); for (String name : elBeans.keySet()) {
return languageSession; 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) { if (name==null) {
throw new NullPointerException("Can't add null name."); throw new NullPointerException("Can't add null name.");
} }
@ -128,14 +132,13 @@ public class DefaultX4OReader<T> extends AbstractX4OReader<T> {
if (bean==null) { if (bean==null) {
throw new NullPointerException("Can't add null bean."); throw new NullPointerException("Can't add null bean.");
} }
ValueExpression ve = languageSession.getExpressionLanguageFactory().createValueExpression(languageSession.getExpressionLanguageContext(),"${"+name+"}", bean.getClass()); elBeans.put(name, bean);
ve.setValue(languageSession.getExpressionLanguageContext(), bean);
} }
/** /**
* Parses the input stream as a X4O document. * 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 // Extra check if we have a language
if (languageSession.getLanguage()==null) { if (languageSession.getLanguage()==null) {
throw new X4OConnectionException("languageSession is broken getLanguage() returns null."); throw new X4OConnectionException("languageSession is broken getLanguage() returns null.");
@ -186,17 +189,4 @@ public class DefaultX4OReader<T> extends AbstractX4OReader<T> {
debugStop(languageSession); 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; return propertyConfig;
} }
X4OLanguageSession createLanguageSession() {
return getLanguage().createLanguageSession();
}
/** /**
* @see org.x4o.xml.io.X4OWriterSession#writeSession(org.x4o.xml.lang.X4OLanguageSession, java.io.OutputStream) * @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 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 * @author Willem Cazander
* @version 1.0 Apr 6, 2013 * @version 1.0 Apr 6, 2013
*/ */
public interface X4OConnection { public interface X4OConnection {
X4OLanguage getLanguage();
X4OLanguageSession createLanguageSession();
/** /**
* Sets an X4O Language property. * Sets an X4O Language property.
* @param key The key of the property to set. * @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 java.net.URL;
import org.x4o.xml.lang.X4OLanguageSession; import org.x4o.xml.lang.X4OLanguageSession;
import org.x4o.xml.lang.phase.X4OPhaseException;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
/** /**
@ -40,66 +39,70 @@ import org.xml.sax.SAXException;
*/ */
public interface X4OReaderSession<T> extends X4OReader<T> { public interface X4OReaderSession<T> extends X4OReader<T> {
void releaseSession(X4OLanguageSession context) throws X4OPhaseException;
/** /**
* Method to parse the xml data. * Method to parse the xml data.
* @param session The language session.
* @param input The inputStream to parse. * @param input The inputStream to parse.
* @throws X4OConnectionException Is thrown after x4o exception. * @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception. * @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io 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. * Reads the file fileName and parses it as an InputStream.
* @param session The language session.
* @param fileName The file name to parse. * @param fileName The file name to parse.
* @throws FileNotFoundException Is thrown is file is not found. * @throws FileNotFoundException Is thrown is file is not found.
* @throws X4OConnectionException Is thrown after x4o exception. * @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception. * @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io 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. * Reads the file and parses it as an InputStream.
* @param session The language session.
* @param file The file to parse. * @param file The file to parse.
* @throws FileNotFoundException Is thrown is file is not found. * @throws FileNotFoundException Is thrown is file is not found.
* @throws X4OConnectionException Is thrown after x4o exception. * @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception. * @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io 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. * Parses an resource locaction.
* @param session The language session.
* @param resourceName The resource to parser. * @param resourceName The resource to parser.
* @throws X4OConnectionException Is thrown after x4o exception. * @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception. * @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io 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. * 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. * @param xmlString The xml as String to parse.
* @throws X4OConnectionException Is thrown after x4o exception. * @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception. * @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io 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. * 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. * @param url The url to parse.
* @throws X4OConnectionException Is thrown after x4o exception. * @throws X4OConnectionException Is thrown after x4o exception.
* @throws SAXException Is thrown after sax xml exception. * @throws SAXException Is thrown after sax xml exception.
* @throws IOException Is thrown after io 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> { 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.ElementNamespace;
import org.x4o.xml.element.ElementNamespaceInstanceProviderException; import org.x4o.xml.element.ElementNamespaceInstanceProviderException;
import org.x4o.xml.element.ElementObjectPropertyValue; import org.x4o.xml.element.ElementObjectPropertyValue;
import org.x4o.xml.lang.phase.X4OPhaseException;
import org.x4o.xml.lang.phase.X4OPhaseManager; import org.x4o.xml.lang.phase.X4OPhaseManager;
/** /**
@ -135,28 +134,24 @@ public class DefaultX4OLanguage implements X4OLanguageLocal {
return result; return result;
} }
protected X4OLanguageSession buildElementLanguage(X4OLanguageSession languageSession) { protected X4OLanguageSession buildElementLanguage(X4OLanguageSessionLocal languageSession) {
if ((languageSession instanceof X4OLanguageSessionLocal)==false) {
throw new RuntimeException("Can't init X4OLanguageSession which has not X4OLanguageSessionLocal interface obj: "+languageSession);
}
X4OLanguageSessionLocal contextInit = (X4OLanguageSessionLocal)languageSession;
try { try {
if (contextInit.getExpressionLanguageFactory()==null) { if (languageSession.getExpressionLanguageFactory()==null) {
contextInit.setExpressionLanguageFactory(X4OExpressionFactory.createExpressionFactory()); languageSession.setExpressionLanguageFactory(X4OExpressionFactory.createExpressionFactory());
} }
if (contextInit.getExpressionLanguageContext()==null) { if (languageSession.getExpressionLanguageContext()==null) {
contextInit.setExpressionLanguageContext(X4OExpressionFactory.createELContext(contextInit.getLanguage().getLanguageConfiguration().getDefaultExpressionLanguageContext())); languageSession.setExpressionLanguageContext(X4OExpressionFactory.createELContext(languageSession.getLanguage().getLanguageConfiguration().getDefaultExpressionLanguageContext()));
} }
if (contextInit.getElementAttributeValueParser()==null) { if (languageSession.getElementAttributeValueParser()==null) {
contextInit.setElementAttributeValueParser((ElementAttributeValueParser)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultElementAttributeValueParser())); languageSession.setElementAttributeValueParser((ElementAttributeValueParser)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultElementAttributeValueParser()));
} }
if (contextInit.getElementObjectPropertyValue()==null) { if (languageSession.getElementObjectPropertyValue()==null) {
contextInit.setElementObjectPropertyValue((ElementObjectPropertyValue)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultElementObjectPropertyValue())); languageSession.setElementObjectPropertyValue((ElementObjectPropertyValue)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultElementObjectPropertyValue()));
} }
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e.getMessage(),e); throw new RuntimeException(e.getMessage(),e);
} }
return contextInit; return languageSession;
} }
/** /**

View file

@ -22,6 +22,8 @@
*/ */
package org.x4o.xml.lang; package org.x4o.xml.lang;
import org.x4o.xml.io.X4OConnectionException;
import org.x4o.xml.lang.phase.X4OPhaseException;
/** /**
* An DefaultX4OLanguageSession. * An DefaultX4OLanguageSession.
@ -37,4 +39,16 @@ public class DefaultX4OLanguageSession extends AbstractX4OLanguageSession {
public DefaultX4OLanguageSession(X4OLanguage language) { public DefaultX4OLanguageSession(X4OLanguage language) {
super(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.Element;
import org.x4o.xml.element.ElementAttributeValueParser; import org.x4o.xml.element.ElementAttributeValueParser;
import org.x4o.xml.element.ElementObjectPropertyValue; import org.x4o.xml.element.ElementObjectPropertyValue;
import org.x4o.xml.io.X4OConnectionException;
import org.x4o.xml.io.X4ODebugWriter; import org.x4o.xml.io.X4ODebugWriter;
import org.x4o.xml.lang.phase.X4OPhase; import org.x4o.xml.lang.phase.X4OPhase;
@ -39,7 +40,7 @@ import org.x4o.xml.lang.phase.X4OPhase;
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Feb 14, 2007 * @version 1.0 Feb 14, 2007
*/ */
public interface X4OLanguageSession { public interface X4OLanguageSession extends AutoCloseable {
/** /**
* @return Returns the language from which this session in created. * @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. * @return Returns the phase this session will stop processing.
*/ */
public String getPhaseStop(); String getPhaseStop();
/** /**
* @return Returns a list of phases we skip while processing. * @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; releaseRequested = X4OPhaseLanguageWrite.WRITE_RELEASE;
} }
if (releaseRequested==null) { 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) { if (languageSession.getRootElement()==null) {
return; // no root element , empty xml document ? return; // no root element , empty xml document ?

View file

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

View file

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

View file

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

View file

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

View file

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