X4O: Fixed recursief debugging xml output

This commit is contained in:
Willem Cazander 2025-11-07 20:13:30 +01:00
parent 889e4b9eb8
commit 2819b36a45
37 changed files with 592 additions and 570 deletions

View file

@ -148,22 +148,6 @@ public abstract class X4ODriver<T> {
return buildLanguage(getLanguageVersionDefault());
}
/*
* Creates the X4OLanguageSession for the default language version.
* @return The created X4OLanguageSession.
*
final public X4OLanguageSession createLanguageSession() {
return createLanguageSession(getLanguageVersionDefault());
}
*
* Creates the X4OLanguageSession for the specified version.
* @param version The language version to create the context for.
* @return The created X4OLanguageSession.
*
final public X4OLanguageSession createLanguageSession(String version) {
return createLanguage(version).createLanguageSession();
}*/
// =============== Language Tasks

View file

@ -43,12 +43,10 @@ import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.X4OLanguageClassLoader;
import org.x4o.xml.lang.X4OLanguageConfiguration;
import org.x4o.xml.lang.phase.DefaultX4OPhaseManager;
import org.x4o.xml.lang.phase.X4OPhaseException;
import org.x4o.xml.lang.phase.X4OPhaseLanguageInit;
import org.x4o.xml.lang.phase.X4OPhaseLanguageRead;
import org.x4o.xml.lang.phase.X4OPhaseLanguageWrite;
import org.x4o.xml.lang.phase.X4OPhaseManager;
import org.x4o.xml.lang.phase.X4OPhaseType;
import org.x4o.xml.lang.task.X4OLanguageTask;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
@ -128,11 +126,6 @@ public final class X4ODriverManager {
version = driver.getLanguageVersionDefault();
}
DefaultX4OLanguage result = new DefaultX4OLanguage(driver.buildLanguageConfiguration(), driver.buildPhaseManager(), driver.getLanguageName(), version);
try {
result.getPhaseManager().runPhases(result.createLanguageSession(), X4OPhaseType.INIT); // TODO: fix phase to interface T
} catch (X4OPhaseException e) {
throw new RuntimeException(e); // TODO: change layer
}
return result;
}

View file

@ -75,12 +75,13 @@ public class EldModuleLoader implements X4OLanguageModuleLoader {
/**
* Loads the ELD language into the module.
* @param language The langauge to load for.
* @param languageModule The module to load it in.
* @param session The session we run in.
* @param language The local Language to load for.
* @param module The language module to load it into.
* @throws X4OLanguageModuleLoaderException When eld language could not be loaded.
* @see org.x4o.xml.lang.X4OLanguageModuleLoader#loadLanguageModule(org.x4o.xml.lang.X4OLanguageLocal, org.x4o.xml.lang.X4OLanguageModuleLocal)
*/
public void loadLanguageModule(X4OLanguageLocal language, X4OLanguageModuleLocal languageModule) throws X4OLanguageModuleLoaderException {
public void loadLanguageModule(X4OLanguageSession session, X4OLanguageLocal language, X4OLanguageModuleLocal module) throws X4OLanguageModuleLoaderException {
logger.fine("Loading name eld file from resource: " + eldResource);
try {
X4ODriver<?> driver = null;
@ -90,19 +91,11 @@ public class EldModuleLoader implements X4OLanguageModuleLoader {
driver = X4ODriverManager.getX4ODriver(EldDriver.LANGUAGE_NAME);
}
X4OReader<?> reader = driver.createReader();
//X4OLanguageSession eldLang = driver.createLanguageSession(driver.getLanguageVersionDefault());
//X4OReader<?> reader = new DefaultX4OReader<Object>(eldLang);
reader.addELBeanInstance(EL_PARENT_LANGUAGE, language);
reader.addELBeanInstance(EL_PARENT_LANGUAGE_MODULE, languageModule);
reader.setProperty(DefaultX4OReader.DEBUG_OUTPUT_HANDLER, null);
//TODO: if (language.getLanguageConfiguration().getLanguagePropertyBoolean(X4OLanguageProperty.DEBUG_OUTPUT_ELD_PARSER)) {
// eldLang.setX4ODebugWriter(elementLanguage.getLanguageConfiguration().getX4ODebugWriter());
// }
reader.addELBeanInstance(EL_PARENT_LANGUAGE_MODULE, module);
if (session.hasX4ODebugWriter()) {
reader.setProperty(DefaultX4OReader.DEBUG_OUTPUT_HANDLER, session.getX4ODebugWriter().getContentWriter());
}
reader.readResource(eldResource);
} catch (X4OConnectionException e) {
throw new X4OLanguageModuleLoaderException(this,e.getMessage()+" while parsing: "+eldResource,e);

View file

@ -48,9 +48,11 @@ import org.x4o.xml.lang.X4OLanguageClassLoader;
import org.x4o.xml.lang.X4OLanguageModuleLoader;
import org.x4o.xml.lang.X4OLanguageModuleLoaderException;
import org.x4o.xml.lang.X4OLanguageModuleLocal;
import org.x4o.xml.lang.X4OLanguageSession;
/**
* EldModuleLoaderCore provides a few basic elements for the core eld x4o language.
/**
* EldModuleLoaderCore provides a few basic elements for the core eld x4o
* language.
*
* @author Willem Cazander
* @version 1.0 Jan 11, 2009
@ -59,117 +61,122 @@ public class EldModuleLoaderCore implements X4OLanguageModuleLoader {
private Logger logger = null;
private static final String PP_CEL_PROVIDER_HOST = "cel.x4o.org";
private static final String PP_CEL_XMLNS = "http://"+PP_CEL_PROVIDER_HOST+"/xml/ns/";
private static final String PP_CEL_XMLNS = "http://" + PP_CEL_PROVIDER_HOST + "/xml/ns/";
private static final String PP_CEL_XSD_FILE = "-1.0.xsd";
private static final String CEL_CORE = "cel-core";
private static final String CEL_ROOT = "cel-root";
private static final String CEL_CORE_XSD_FILE = CEL_CORE+PP_CEL_XSD_FILE;
private static final String CEL_ROOT_XSD_FILE = CEL_ROOT+PP_CEL_XSD_FILE;
private static final String CEL_CORE_XSD_FILE = CEL_CORE + PP_CEL_XSD_FILE;
private static final String CEL_ROOT_XSD_FILE = CEL_ROOT + PP_CEL_XSD_FILE;
/** The cel core namespace uri. */
public static final String CEL_CORE_URI = PP_CEL_XMLNS+CEL_CORE;
public static final String CEL_CORE_URI = PP_CEL_XMLNS + CEL_CORE;
/** The cel root namespace uri. */
public static final String CEL_ROOT_URI = PP_CEL_XMLNS+CEL_ROOT;
public static final String CEL_ROOT_URI = PP_CEL_XMLNS + CEL_ROOT;
/** The cel core schema namespace uri. */
public static final String CEL_CORE_XSD_URI = CEL_CORE_URI+PP_CEL_XSD_FILE;
public static final String CEL_CORE_XSD_URI = CEL_CORE_URI + PP_CEL_XSD_FILE;
/** The cel root schema namespace uri. */
public static final String CEL_ROOT_XSD_URI = CEL_ROOT_URI+PP_CEL_XSD_FILE;
public static final String CEL_ROOT_XSD_URI = CEL_ROOT_URI + PP_CEL_XSD_FILE;
/**
* Creates the CEL module loader.
*/
public EldModuleLoaderCore() {
logger = Logger.getLogger(EldModuleLoaderCore.class.getName());
}
/**
* Loads the CEL language into the module.
* @param language The langauge to load for.
*
* @param session The session to run in.
* @param language The langauge to load for.
* @param languageModule The module to load it in.
* @see org.x4o.xml.lang.X4OLanguageModuleLoader#loadLanguageModule(org.x4o.xml.lang.X4OLanguageLocal, org.x4o.xml.lang.X4OLanguageModuleLocald)
* @see org.x4o.xml.lang.X4OLanguageModuleLoader#loadLanguageModule(org.x4o.xml.lang.X4OLanguageLocal,
* org.x4o.xml.lang.X4OLanguageModuleLocald)
*/
public void loadLanguageModule(X4OLanguageLocal language,X4OLanguageModuleLocal languageModule) throws X4OLanguageModuleLoaderException {
@Override
public void loadLanguageModule(X4OLanguageSession session, X4OLanguageLocal language, X4OLanguageModuleLocal languageModule) throws X4OLanguageModuleLoaderException {
// Config module meta data
configLanguageModule(languageModule);
// Config language
addBindingHandler(languageModule,new ElementClassBindingHandler(),"cel-class-bind","Binds the ElementClass childeren.");
addBindingHandler(languageModule,new ElementModuleBindingHandler(),"cel-module-bind","Binds the LanguageModule childeren.");
addBindingHandler(languageModule,new ElementClassAttributeBindingHandler(),"cel-class-attr-bind","Binds the ElementClassAttribute childeren.");
addBindingHandler(languageModule,new ElementInterfaceBindingHandler(),"cel-interface-bind","Binds the ElementInterface childeren.");
addBindingHandler(languageModule,new ElementNamespaceBindingHandler(),"cel-namespace-bind","Binds the Namespace childeren.");
addBindingHandler(languageModule, new ElementClassBindingHandler(), "cel-class-bind", "Binds the ElementClass childeren.");
addBindingHandler(languageModule, new ElementModuleBindingHandler(), "cel-module-bind", "Binds the LanguageModule childeren.");
addBindingHandler(languageModule, new ElementClassAttributeBindingHandler(), "cel-class-attr-bind", "Binds the ElementClassAttribute childeren.");
addBindingHandler(languageModule, new ElementInterfaceBindingHandler(), "cel-interface-bind", "Binds the ElementInterface childeren.");
addBindingHandler(languageModule, new ElementNamespaceBindingHandler(), "cel-namespace-bind", "Binds the Namespace childeren.");
// Create cel-lang namespace in language
ElementNamespace namespace = createNamespaceContext(language,CEL_CORE,CEL_CORE_URI,CEL_CORE_XSD_URI,CEL_CORE_XSD_FILE,CEL_CORE);
configElementClasses(language,namespace);
startAndAddNamespace(language,languageModule,namespace);
ElementNamespace namespace = createNamespaceContext(language, CEL_CORE, CEL_CORE_URI, CEL_CORE_XSD_URI, CEL_CORE_XSD_FILE, CEL_CORE);
configElementClasses(language, namespace);
startAndAddNamespace(language, languageModule, namespace);
// Create cel-root namespace in language for schema support
ElementNamespace namespaceRoot = createNamespaceContext(language,CEL_ROOT,CEL_ROOT_URI,CEL_ROOT_XSD_URI,CEL_ROOT_XSD_FILE,CEL_ROOT);
ElementNamespace namespaceRoot = createNamespaceContext(language, CEL_ROOT, CEL_ROOT_URI, CEL_ROOT_XSD_URI, CEL_ROOT_XSD_FILE, CEL_ROOT);
namespaceRoot.setLanguageRoot(true); // Only define single language root so xsd is (mostly) not cicle import.
ElementClass rootElement = createElementClass(language,"module",language.getLanguageConfiguration().getDefaultElementLanguageModule(),ModuleElement.class,"The module tag is the root xml element for ELD language.");
rootElement.addElementClassAttribute(createElementClassAttribute(language,"id",true,null));
rootElement.addElementClassAttribute(createElementClassAttribute(language,"providerHost",true,null));
ElementClass rootElement = createElementClass(language, "module", language.getLanguageConfiguration().getDefaultElementLanguageModule(), ModuleElement.class, "The module tag is the root xml element for ELD language.");
rootElement.addElementClassAttribute(createElementClassAttribute(language, "id", true, null));
rootElement.addElementClassAttribute(createElementClassAttribute(language, "providerHost", true, null));
namespaceRoot.addElementClass(rootElement);
startAndAddNamespace(language,languageModule,namespaceRoot);
startAndAddNamespace(language, languageModule, namespaceRoot);
}
/**
* Adds only Element class beans which need extra meta info for schema.
*
* @param namespace The namespace to config.
* @param language The language to config for.
* @throws X4OLanguageModuleLoaderException
* @param language The language to config for.
* @throws X4OLanguageModuleLoaderException
*/
private void configElementClasses(X4OLanguage language,ElementNamespace namespace) throws X4OLanguageModuleLoaderException {
private void configElementClasses(X4OLanguage language, ElementNamespace namespace) throws X4OLanguageModuleLoaderException {
ElementClass ec = null;
namespace.addElementClass(createElementClass(language,"attribute",language.getLanguageConfiguration().getDefaultElementClassAttribute(),null,"Defines xml element attribute."));
ec = createElementClass(language,"attributeAlias",null,AttributeAliasElement.class,"Adds an attribute alias.");
ec.addElementClassAttribute(createElementClassAttribute(language,"name",true,null));
namespace.addElementClass(createElementClass(language, "attribute", language.getLanguageConfiguration().getDefaultElementClassAttribute(), null, "Defines xml element attribute."));
ec = createElementClass(language, "attributeAlias", null, AttributeAliasElement.class, "Adds an attribute alias.");
ec.addElementClassAttribute(createElementClassAttribute(language, "name", true, null));
ec.addElementParent(CEL_CORE_URI, "attribute");
namespace.addElementClass(ec);
namespace.addElementClass(createElementClass(language,"classConverter",ClassConverter.class,null,"Converts string attribute to java class instance."));
ec = createElementClass(language,"namespace",language.getLanguageConfiguration().getDefaultElementNamespace(),null,"Defines an xml namespace.");
ec.addElementClassAttribute(createElementClassAttribute(language,"uri",true,null));
namespace.addElementClass(createElementClass(language, "classConverter", ClassConverter.class, null, "Converts string attribute to java class instance."));
ec = createElementClass(language, "namespace", language.getLanguageConfiguration().getDefaultElementNamespace(), null, "Defines an xml namespace.");
ec.addElementClassAttribute(createElementClassAttribute(language, "uri", true, null));
namespace.addElementClass(ec);
ec = createElementClass(language,"element",language.getLanguageConfiguration().getDefaultElementClass(),null,"Defines xml element tag.");
ec.addElementClassAttribute(createElementClassAttribute(language,"objectClass",false,new ClassConverter()));
ec.addElementClassAttribute(createElementClassAttribute(language,"elementClass",false,new ClassConverter()));
ec = createElementClass(language, "element", language.getLanguageConfiguration().getDefaultElementClass(), null, "Defines xml element tag.");
ec.addElementClassAttribute(createElementClassAttribute(language, "objectClass", false, new ClassConverter()));
ec.addElementClassAttribute(createElementClassAttribute(language, "elementClass", false, new ClassConverter()));
namespace.addElementClass(ec);
ec = createElementClass(language,"elementInterface",language.getLanguageConfiguration().getDefaultElementInterface(),null,"Defines element interface class.");
ec.addElementClassAttribute(createElementClassAttribute(language,"interfaceClass",false,new ClassConverter()));
ec = createElementClass(language, "elementInterface", language.getLanguageConfiguration().getDefaultElementInterface(), null, "Defines element interface class.");
ec.addElementClassAttribute(createElementClassAttribute(language, "interfaceClass", false, new ClassConverter()));
namespace.addElementClass(ec);
ec = createElementClass(language,"bindingHandler",null,BeanElement.class,"Defines generic binding handler for languge.");
ec = createElementClass(language, "bindingHandler", null, BeanElement.class, "Defines generic binding handler for languge.");
ec.addElementParent(CEL_ROOT_URI, "module");
ec.addElementClassAttribute(createElementClassAttribute(language,"id",true,null));
ec.addElementClassAttribute(createElementClassAttribute(language,"bean.class",true,null));
ec.addElementClassAttribute(createElementClassAttribute(language, "id", true, null));
ec.addElementClassAttribute(createElementClassAttribute(language, "bean.class", true, null));
namespace.addElementClass(ec);
ec = createElementClass(language,"namespaceAttribute",null,BeanElement.class,"Defines generic namespace attribute for language.");
ec = createElementClass(language, "namespaceAttribute", null, BeanElement.class, "Defines generic namespace attribute for language.");
ec.addElementParent(CEL_CORE_URI, "namespace");
ec.addElementClassAttribute(createElementClassAttribute(language,"bean.class",true,null));
ec.addElementClassAttribute(createElementClassAttribute(language, "bean.class", true, null));
namespace.addElementClass(ec);
ec = createElementClass(language,"configurator",null,BeanElement.class,"Define generic configurator for language.");
ec = createElementClass(language, "configurator", null, BeanElement.class, "Define generic configurator for language.");
ec.addElementParent(CEL_CORE_URI, "elementInterface");
ec.addElementParent(CEL_CORE_URI, "element");
ec.addElementClassAttribute(createElementClassAttribute(language,"bean.class",true,null));
ec.addElementClassAttribute(createElementClassAttribute(language,"configAction",false,null));
ec.addElementClassAttribute(createElementClassAttribute(language, "bean.class", true, null));
ec.addElementClassAttribute(createElementClassAttribute(language, "configAction", false, null));
namespace.addElementClass(ec);
ec = createElementClass(language,"configuratorGlobal",null,BeanElement.class,"Define generic global configuator for languge.");
ec = createElementClass(language, "configuratorGlobal", null, BeanElement.class, "Define generic global configuator for languge.");
ec.addElementParent(CEL_ROOT_URI, "module");
ec.addElementClassAttribute(createElementClassAttribute(language,"bean.class",true,null));
ec.addElementClassAttribute(createElementClassAttribute(language,"configAction",false,null));
ec.addElementClassAttribute(createElementClassAttribute(language, "bean.class", true, null));
ec.addElementClassAttribute(createElementClassAttribute(language, "configAction", false, null));
namespace.addElementClass(ec);
ec = createElementClass(language,"description",null,DescriptionElement.class,"Adds description as text on all eld elements.");
ec = createElementClass(language, "description", null, DescriptionElement.class, "Adds description as text on all eld elements.");
ec.setSchemaContentBase("string");
ec.addElementParent(CEL_ROOT_URI, "module");
ec.addElementParent(CEL_CORE_URI, "namespace");
@ -181,47 +188,44 @@ public class EldModuleLoaderCore implements X4OLanguageModuleLoader {
ec.addElementParent(CEL_CORE_URI, "element");
ec.addElementParent(CEL_CORE_URI, "attribute");
namespace.addElementClass(ec);
ec = createElementClass(language,"elementParent",null,ElementClassAddParentElement.class,"Added (meta) element parent.");
ec = createElementClass(language, "elementParent", null, ElementClassAddParentElement.class, "Added (meta) element parent.");
ec.addElementParent(CEL_CORE_URI, "element");
ec.addElementParent(CEL_CORE_URI, "elementInterface");
ec.addElementClassAttribute(createElementClassAttribute(language,"tag",true,null));
ec.addElementClassAttribute(createElementClassAttribute(language,"uri",false,null));
ec.addElementClassAttribute(createElementClassAttribute(language, "tag", true, null));
ec.addElementClassAttribute(createElementClassAttribute(language, "uri", false, null));
namespace.addElementClass(ec);
}
private void configLanguageModule(X4OLanguageModuleLocal languageModule) {
languageModule.setId("cel-module");
languageModule.setProviderName("Core Element Languag Module");
languageModule.setProviderHost(PP_CEL_PROVIDER_HOST);
languageModule.setDescription("Core Element Language Module Loader");
}
private void startAndAddNamespace(X4OLanguageLocal language,X4OLanguageModuleLocal languageModule,ElementNamespace namespace) throws X4OLanguageModuleLoaderException {
private void startAndAddNamespace(X4OLanguageLocal language, X4OLanguageModuleLocal languageModule, ElementNamespace namespace) throws X4OLanguageModuleLoaderException {
try {
namespace.getElementNamespaceInstanceProvider().start(language, namespace);
} catch (ElementNamespaceInstanceProviderException e) {
throw new X4OLanguageModuleLoaderException(this,"Error starting instance provider: "+e.getMessage(),e);
throw new X4OLanguageModuleLoaderException(this, "Error starting instance provider: " + e.getMessage(), e);
}
languageModule.addElementNamespace(namespace);
}
private ElementNamespace createNamespaceContext(X4OLanguageLocal language,String id,String uri,String schemaUri,String schemaResource,String schemaPrefix) throws X4OLanguageModuleLoaderException {
logger.finer("Creating "+language.getLanguageName()+" namespace.");
private ElementNamespace createNamespaceContext(X4OLanguageLocal language, String id, String uri, String schemaUri, String schemaResource, String schemaPrefix) throws X4OLanguageModuleLoaderException {
logger.finer("Creating " + language.getLanguageName() + " namespace.");
ElementNamespace namespace;
try {
namespace = X4OLanguageClassLoader.newInstance(ElementNamespace.class, language.getLanguageConfiguration().getDefaultElementNamespace());
} catch (ClassNotFoundException e) {
throw new X4OLanguageModuleLoaderException(this,e.getMessage(),e);
throw new X4OLanguageModuleLoaderException(this, e.getMessage(), e);
}
try {
namespace.setElementNamespaceInstanceProvider(
X4OLanguageClassLoader.newInstance(ElementNamespaceInstanceProvider.class, language.getLanguageConfiguration().getDefaultElementNamespaceInstanceProvider())
);
namespace.setElementNamespaceInstanceProvider(X4OLanguageClassLoader.newInstance(ElementNamespaceInstanceProvider.class, language.getLanguageConfiguration().getDefaultElementNamespaceInstanceProvider()));
} catch (ClassNotFoundException e) {
throw new X4OLanguageModuleLoaderException(this,e.getMessage(),e);
throw new X4OLanguageModuleLoaderException(this, e.getMessage(), e);
}
namespace.setId(id);
namespace.setUri(uri);
namespace.setSchemaUri(schemaUri);
@ -229,8 +233,8 @@ public class EldModuleLoaderCore implements X4OLanguageModuleLoader {
namespace.setSchemaPrefix(schemaPrefix);
return namespace;
}
private ElementClass createElementClass(X4OLanguage language,String tag,Class<?> objectClass,Class<?> elementClass,String description) throws X4OLanguageModuleLoaderException {
private ElementClass createElementClass(X4OLanguage language, String tag, Class<?> objectClass, Class<?> elementClass, String description) throws X4OLanguageModuleLoaderException {
try {
ElementClass result = X4OLanguageClassLoader.newInstance(ElementClass.class, language.getLanguageConfiguration().getDefaultElementClass());
result.setId(tag);
@ -239,43 +243,45 @@ public class EldModuleLoaderCore implements X4OLanguageModuleLoader {
result.setDescription(description);
return result;
} catch (ClassNotFoundException e) {
throw new X4OLanguageModuleLoaderException(this,e.getMessage(),e);
throw new X4OLanguageModuleLoaderException(this, e.getMessage(), e);
}
}
/**
* Creates new configed ElementClassAttribute instance.
* @param language The X4OLanguage to create from.
* @param name The name of the attribute.
* @param required Is the attribute required.
*
* @param language The X4OLanguage to create from.
* @param name The name of the attribute.
* @param required Is the attribute required.
* @param converter The converter for the attribute.
* @return The new ElementClassAttribute instance.
* @throws X4OLanguageModuleLoaderException When class could not be created.
* @return The new ElementClassAttribute instance.
* @throws X4OLanguageModuleLoaderException When class could not be created.
*/
private ElementClassAttribute createElementClassAttribute(X4OLanguage language,String name,boolean required,ObjectConverter converter) throws X4OLanguageModuleLoaderException {
private ElementClassAttribute createElementClassAttribute(X4OLanguage language, String name, boolean required, ObjectConverter converter) throws X4OLanguageModuleLoaderException {
try {
ElementClassAttribute result = X4OLanguageClassLoader.newInstance(ElementClassAttribute.class, language.getLanguageConfiguration().getDefaultElementClassAttribute());
result.setId(name);
if (required) {
result.setRequired(required);
}
if (converter!=null) {
if (converter != null) {
result.setObjectConverter(converter);
}
return result;
} catch (ClassNotFoundException e) {
throw new X4OLanguageModuleLoaderException(this,e.getMessage(),e);
throw new X4OLanguageModuleLoaderException(this, e.getMessage(), e);
}
}
/**
* Adds binding handler to module.
*
* @param languageModule The language module.
* @param handler The handler to add the the module.
* @param id The handler id.
* @param description The handler descripion.
* @param handler The handler to add the the module.
* @param id The handler id.
* @param description The handler descripion.
*/
private void addBindingHandler(X4OLanguageModuleLocal languageModule,ElementBindingHandler handler,String id,String description) {
private void addBindingHandler(X4OLanguageModuleLocal languageModule, ElementBindingHandler handler, String id, String description) {
handler.setId(id);
handler.setDescription(description);
languageModule.addElementBindingHandler(handler);

View file

@ -27,6 +27,7 @@ import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.x4o.sax3.SAX3WriterXml;
import org.x4o.sax3.io.ContentWriter;
@ -36,6 +37,7 @@ import org.x4o.xml.lang.phase.X4OPhase;
import org.x4o.xml.lang.phase.X4OPhaseException;
import org.x4o.xml.lang.phase.X4OPhaseLanguageRead;
import org.x4o.xml.lang.phase.X4OPhaseLanguageWrite;
import org.x4o.xml.lang.phase.X4OPhaseListener;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
@ -46,59 +48,52 @@ import org.xml.sax.helpers.AttributesImpl;
* @version 1.0 Mar 14, 2014
*/
public abstract class AbstractX4OConnectionDebug implements X4OConnection {
private boolean debugStarted = false;
private boolean debugAutoCloseOutputStream = true;
private OutputStream debugCloseOutputStream = null;
protected final static String ABSTRACT_DEBUG_OUTPUT_HANDLER = "debug/output-handler";
protected final static String ABSTRACT_DEBUG_OUTPUT_STREAM = "debug/output-stream";
protected final static String ABSTRACT_DEBUG_OUTPUT_STREAM_CLOSE = "debug/output-stream-close";
protected void debugStart(X4OLanguageSession languageSession,String debugHandlerKey,String debugStreamKey,String debugStreamCloseKey) throws UnsupportedEncodingException, SAXException {
debugAutoCloseOutputStream = (Boolean)getProperty(debugStreamCloseKey);
protected final static String ABSTRACT_DEBUG_OUTPUT_HANDLER = "debug/output-handler";
protected final static String ABSTRACT_DEBUG_OUTPUT_STREAM = "debug/output-stream";
protected void debugStart(X4OLanguageSession languageSession, String debugHandlerKey, String debugStreamKey) throws UnsupportedEncodingException, SAXException {
Object debugOutputHandler = getProperty(debugHandlerKey);
Object debugOutputStream = getProperty(debugStreamKey);
if (languageSession.getX4ODebugWriter()==null) {
// init debug infra
if (languageSession.getX4ODebugWriter() == null) {
ContentWriter xmlDebugWriter = null;
if (debugOutputHandler instanceof ContentWriter) {
xmlDebugWriter = (ContentWriter)debugOutputHandler;
xmlDebugWriter = (ContentWriter) debugOutputHandler;
} else if (debugOutputStream instanceof OutputStream) {
debugCloseOutputStream = (OutputStream)debugOutputStream;
xmlDebugWriter = new SAX3WriterXml(debugCloseOutputStream);
}
if (xmlDebugWriter!=null) {
xmlDebugWriter = new SAX3WriterXml((OutputStream) debugOutputStream);
xmlDebugWriter.startDocument();
xmlDebugWriter.startPrefixMapping("debug", X4ODebugWriter.DEBUG_URI);
X4ODebugWriter debugWriter = new X4ODebugWriter(xmlDebugWriter);
X4OLanguageSessionLocal local = (X4OLanguageSessionLocal)languageSession;
local.setX4ODebugWriter(debugWriter);
// We only close if we started it, this is for recursief debugging.
xmlDebugWriter.startPrefixMapping(X4ODebugWriter.DEBUG_URI_NS, X4ODebugWriter.DEBUG_URI);
// We only close if we started it, this is for recursief debugging AND debug output stream.
debugStarted = true;
}
if (xmlDebugWriter != null) {
X4ODebugWriter debugWriter = new X4ODebugWriter(xmlDebugWriter);
X4OLanguageSessionLocal local = (X4OLanguageSessionLocal) languageSession;
local.setX4ODebugWriter(debugWriter);
}
}
// debug language
// start debug language
if (languageSession.hasX4ODebugWriter()) {
try {
languageSession.getX4ODebugWriter().debugConnectionStart(languageSession, this);
} catch (IOException e) {
throw new SAXException(e);
}
// Add debug phases for all phases
for (String key:languageSession.getLanguage().getPhaseManager().getPhaseKeys()) {
X4OLanguageSessionLocal local = (X4OLanguageSessionLocal) languageSession;
for (String key : languageSession.getLanguage().getPhaseManager().getPhaseKeys()) {
X4OPhase p = languageSession.getLanguage().getPhaseManager().getPhase(key);
p.addPhaseListener(languageSession.getX4ODebugWriter().createDebugX4OPhaseListener());
List<X4OPhaseListener> listeners = local.storePhaseListeners(p.getId());
listeners.add(languageSession.getX4ODebugWriter().createDebugX4OPhaseListener());
if (shouldPrintTree(p)) {
p.addPhaseListener(languageSession.getX4ODebugWriter().createDebugPrintTreePhaseListener());
listeners.add(languageSession.getX4ODebugWriter().createDebugPrintTreePhaseListener());
}
}
}
}
private boolean shouldPrintTree(X4OPhase p) {
String phase = p.getId();
if (X4OPhaseLanguageWrite.WRITE_END.equals(phase)) {
@ -118,22 +113,22 @@ public abstract class AbstractX4OConnectionDebug implements X4OConnection {
}
return false;
}
protected void debugException(X4OLanguageSession languageSession,Exception e) throws X4OConnectionException {
protected void debugException(X4OLanguageSession languageSession, Exception e) throws X4OConnectionException {
if (!languageSession.hasX4ODebugWriter()) {
return;
}
try {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "message", "", "", e.getMessage());
atts.addAttribute("", "message", "", "", e.getMessage());
if (e instanceof X4OPhaseException) {
atts.addAttribute ("", "phase", "", "", ((X4OPhaseException)e).getX4OPhaseHandler().getId());
atts.addAttribute("", "phase", "", "", ((X4OPhaseException) e).getX4OPhaseHandler().getId());
}
languageSession.getX4ODebugWriter().getContentWriter().startElement(X4ODebugWriter.DEBUG_URI, "exceptionStackTrace", "", atts);
StringWriter writer = new StringWriter();
PrintWriter printer = new PrintWriter(writer);
printer.append('\n');
if (e.getCause()==null) {
if (e.getCause() == null) {
e.printStackTrace(printer);
} else {
e.getCause().printStackTrace(printer);
@ -142,10 +137,10 @@ public abstract class AbstractX4OConnectionDebug implements X4OConnection {
languageSession.getX4ODebugWriter().getContentWriter().characters(stack, 0, stack.length);
languageSession.getX4ODebugWriter().getContentWriter().endElement(X4ODebugWriter.DEBUG_URI, "exceptionStackTrace", "");
} catch (SAXException ee) {
throw new X4OConnectionException("Error while writing debug; "+ee.getMessage(),ee);
throw new X4OConnectionException("Error while writing debug; " + ee.getMessage(), ee);
}
}
protected void debugStop(X4OLanguageSession languageSession) throws X4OConnectionException {
if (!languageSession.hasX4ODebugWriter()) {
return;
@ -155,20 +150,12 @@ public abstract class AbstractX4OConnectionDebug implements X4OConnection {
if (!debugStarted) {
return; // not in this instance started so no stopping then.
}
languageSession.getX4ODebugWriter().getContentWriter().endPrefixMapping("debug");
languageSession.getX4ODebugWriter().getContentWriter().endPrefixMapping(X4ODebugWriter.DEBUG_URI_NS);
languageSession.getX4ODebugWriter().getContentWriter().endDocument();
if (debugCloseOutputStream==null) {
return; // we have handler
}
debugCloseOutputStream.flush();
if (!debugAutoCloseOutputStream) {
return; // no auto close
}
debugCloseOutputStream.close();
} catch (SAXException ee) {
throw new X4OConnectionException("Error while closing debug; "+ee.getMessage(),ee);
throw new X4OConnectionException("Error while closing debug; " + ee.getMessage(), ee);
} catch (IOException ee) {
throw new X4OConnectionException("Error while closing debug; "+ee.getMessage(),ee);
throw new X4OConnectionException("Error while closing debug; " + ee.getMessage(), ee);
}
}
}

View file

@ -20,7 +20,7 @@
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.x4o.xml.io;
package org.x4o.xml.io;
import java.io.File;
import java.io.FileNotFoundException;
@ -41,92 +41,98 @@ import org.xml.sax.SAXException;
*/
@SuppressWarnings("unchecked")
abstract public class AbstractX4OReader<T> extends AbstractX4OReaderSession<T> implements X4OReader<T> {
public AbstractX4OReader(X4OLanguage language) {
super(language);
}
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();
return (T) session.getRootElement().getElementObject();
}
}
/**
* 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.
*
* @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 T readFile(String fileName) throws X4OConnectionException, SAXException, IOException, FileNotFoundException {
try (X4OLanguageSession session = createLanguageSession()) {
readFileSession(session, fileName);
return (T)session.getRootElement().getElementObject();
return (T) session.getRootElement().getElementObject();
}
}
/**
* 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.
*
* @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 T readFile(File file) throws X4OConnectionException, SAXException, IOException, FileNotFoundException {
try (X4OLanguageSession session = createLanguageSession()) {
readFileSession(session, file);
return (T)session.getRootElement().getElementObject();
return (T) session.getRootElement().getElementObject();
}
}
/**
* 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.
*
* @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 T readResource(String resourceName) throws X4OConnectionException, SAXException, IOException {
try (X4OLanguageSession session = createLanguageSession()) {
readResourceSession(session, resourceName);
return (T)session.getRootElement().getElementObject();
return (T) session.getRootElement().getElementObject();
}
}
/**
* Converts a String to a InputStream to is can me readd by SAX.
* @param xmlString The xml as 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.
*
* @param xmlString The xml as 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 T readString(String xmlString) throws X4OConnectionException, SAXException, IOException {
try (X4OLanguageSession session = createLanguageSession()) {
readStringSession(session, xmlString);
return (T)session.getRootElement().getElementObject();
return (T) session.getRootElement().getElementObject();
}
}
/**
* 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.
* 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 T readUrl(URL url) throws X4OConnectionException,SAXException,IOException {
public T readUrl(URL url) throws X4OConnectionException, SAXException, IOException {
try (X4OLanguageSession session = createLanguageSession()) {
readUrlSession(session, url);
return (T)session.getRootElement().getElementObject();
return (T) session.getRootElement().getElementObject();
}
}
}

View file

@ -20,20 +20,20 @@
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.x4o.xml.io;
package org.x4o.xml.io;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.X4OLanguageClassLoader;
import org.x4o.xml.lang.X4OLanguageSession;
import org.xml.sax.SAXException;
import org.x4o.xml.lang.X4OLanguageSession;
import org.xml.sax.SAXException;
/**
* AbstractX4OReaderSession
@ -43,7 +43,7 @@ import org.xml.sax.SAXException;
* @param <T> The root element object type.
*/
abstract public class AbstractX4OReaderSession<T> extends AbstractX4OConnection implements X4OReaderSession<T> {
public AbstractX4OReaderSession(X4OLanguage language) {
super(language);
}
@ -54,56 +54,60 @@ abstract public class AbstractX4OReaderSession<T> extends AbstractX4OConnection
}
readFileSession(session, new File(fileName));
}
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) {
throw new FileNotFoundException("File does not exists; "+file);
throw new FileNotFoundException("File does not exists; " + file);
}
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();
try (InputStream inputStream = new FileInputStream(file)) {
readSession(session, inputStream,file.getAbsolutePath(),basePath);
readSession(session, inputStream, file.getAbsolutePath(), basePath);
}
}
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) {
throw new NullPointerException("Could not find resource on classpath: "+resourceName);
throw new NullPointerException("Could not find resource on classpath: " + resourceName);
}
String baseUrl = url.toExternalForm();
int lastSlash = baseUrl.lastIndexOf('/');
if (lastSlash > 0 && (lastSlash+1) < baseUrl.length()) {
baseUrl = baseUrl.substring(0,lastSlash+1);
if (lastSlash > 0 && (lastSlash + 1) < baseUrl.length()) {
baseUrl = baseUrl.substring(0, lastSlash + 1);
}
URL basePath = new URL(baseUrl);
try (InputStream inputStream = X4OLanguageClassLoader.getResourceAsStream(resourceName)) {
readSession(session, inputStream,url.toExternalForm(),basePath);
readSession(session, inputStream, url.toExternalForm(), basePath);
}
}
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);
readSession(session, new ByteArrayInputStream(xmlString.getBytes(encoding)),"inline-xml",basePath);
String encoding = (String) getProperty(DefaultX4OReader.INPUT_ENCODING);
try (InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes(encoding))) {
readSession(session, inputStream, "inline-xml", basePath);
}
}
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()));
readSession(session, url.openStream(),url.toExternalForm(),basePath);
URL basePath = new URL(url.toExternalForm().substring(0, url.toExternalForm().length() - url.getFile().length()));
try (InputStream inputStream = url.openStream()) {
readSession(session, inputStream, url.toExternalForm(), basePath);
}
}
}

View file

@ -45,7 +45,7 @@ public abstract class AbstractX4OWriter<T> extends AbstractX4OWriterSession<T> i
public AbstractX4OWriter(X4OLanguage language) {
super(language);
}
protected X4OLanguageSession createObjectContext(T object) throws SAXException {
X4OLanguageSession context = createLanguageSession();
Element rootElement = null;
@ -59,26 +59,26 @@ public abstract class AbstractX4OWriter<T> extends AbstractX4OWriterSession<T> i
context.setRootElement(rootElement);
return context;
}
public void write(T object,OutputStream output) throws X4OConnectionException,SAXException,IOException {
public void write(T object, OutputStream output) throws X4OConnectionException, SAXException, IOException {
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 {
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 {
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 {
try (X4OLanguageSession session = createObjectContext(object)) {
return writeStringSession(session);
}

View file

@ -45,27 +45,27 @@ public abstract class AbstractX4OWriterSession<T> extends AbstractX4OConnection
public AbstractX4OWriterSession(X4OLanguage language) {
super(language);
}
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) {
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) {
throw new NullPointerException("Can't read null file.");
}
try (OutputStream outputStream = new FileOutputStream(file)) {
writeSession(languageSession,outputStream);
writeSession(languageSession, outputStream);
}
}
public String writeStringSession(X4OLanguageSession languageSession) throws X4OConnectionException,SAXException,IOException {
public String writeStringSession(X4OLanguageSession languageSession) throws X4OConnectionException, SAXException, IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
writeSession(languageSession, out);
String encoding = (String)getProperty(SAX3WriterXml.OUTPUT_ENCODING);
String encoding = (String) getProperty(SAX3WriterXml.OUTPUT_ENCODING);
return out.toString(encoding);
}
}

View file

@ -38,6 +38,7 @@ import org.x4o.sax3.io.SAX3PropertyConfig;
import org.x4o.sax3.io.SAX3XMLConstants;
import org.x4o.sax3.io.SAX3PropertyConfig.PropertyConfigItem;
import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.X4OLanguageLocal;
import org.x4o.xml.lang.X4OLanguageSession;
import org.x4o.xml.lang.X4OLanguageSessionLocal;
import org.x4o.xml.lang.phase.X4OPhaseType;
@ -75,10 +76,9 @@ public class DefaultX4OReader<T> extends AbstractX4OReader<T> {
public final static String VALIDATION_INPUT_SCHEMA = PROPERTY_CONTEXT_PREFIX + "validation/input-schema";
public final static String DEBUG_OUTPUT_HANDLER = PROPERTY_CONTEXT_PREFIX + ABSTRACT_DEBUG_OUTPUT_HANDLER;
public final static String DEBUG_OUTPUT_STREAM = PROPERTY_CONTEXT_PREFIX + ABSTRACT_DEBUG_OUTPUT_STREAM;
public final static String DEBUG_OUTPUT_STREAM_CLOSE = PROPERTY_CONTEXT_PREFIX + ABSTRACT_DEBUG_OUTPUT_STREAM_CLOSE;
static {
DEFAULT_PROPERTY_CONFIG = new SAX3PropertyConfig(true,null,PROPERTY_CONTEXT_PREFIX,
DEFAULT_PROPERTY_CONFIG = new SAX3PropertyConfig(true, null, PROPERTY_CONTEXT_PREFIX,
new PropertyConfigItem(SAX_ERROR_HANDLER,ErrorHandler.class),
new PropertyConfigItem(SAX_ENTITY_RESOLVER,EntityResolver.class),
new PropertyConfigItem(DOC_EMPTY_NAMESPACE_URI,String.class),
@ -93,18 +93,20 @@ public class DefaultX4OReader<T> extends AbstractX4OReader<T> {
new PropertyConfigItem(VALIDATION_INPUT_DOC,Boolean.class,false),
new PropertyConfigItem(VALIDATION_INPUT_SCHEMA,Boolean.class,false),
new PropertyConfigItem(DEBUG_OUTPUT_HANDLER,ContentWriter.class),
new PropertyConfigItem(DEBUG_OUTPUT_STREAM,OutputStream.class),
new PropertyConfigItem(DEBUG_OUTPUT_STREAM_CLOSE,Boolean.class,true)
new PropertyConfigItem(DEBUG_OUTPUT_STREAM,OutputStream.class)
);
}
public DefaultX4OReader(X4OLanguage language) {
super(language);
propertyConfig = new SAX3PropertyConfig(DEFAULT_PROPERTY_CONFIG,PROPERTY_CONTEXT_PREFIX);
propertyConfig = new SAX3PropertyConfig(DEFAULT_PROPERTY_CONFIG, PROPERTY_CONTEXT_PREFIX);
}
/**
* @see org.x4o.xml.io.AbstractX4OConnection#getPropertyConfig()
*/
@Override
SAX3PropertyConfig getPropertyConfig() {
public SAX3PropertyConfig getPropertyConfig() {
return propertyConfig;
}
@ -122,13 +124,13 @@ public class DefaultX4OReader<T> extends AbstractX4OReader<T> {
}
public void addELBeanInstance(String name, Object bean) {
if (name==null) {
if (name == null) {
throw new NullPointerException("Can't add null name.");
}
if (name.length()==0) {
if (name.length() == 0) {
throw new NullPointerException("Can't add empty name.");
}
if (bean==null) {
if (bean == null) {
throw new NullPointerException("Can't add null bean.");
}
elBeans.put(name, bean);
@ -137,27 +139,30 @@ public class DefaultX4OReader<T> extends AbstractX4OReader<T> {
/**
* Parses the input stream as a X4O document.
*/
protected void readSession(X4OLanguageSession languageSession) throws X4OConnectionException,SAXException,IOException {
protected void readSession(X4OLanguageSession languageSession) throws X4OConnectionException, SAXException, IOException {
// Extra check if we have a language
if (languageSession.getLanguage()==null) {
if (languageSession.getLanguage() == null) {
throw new X4OConnectionException("languageSession is broken getLanguage() returns null.");
}
// Insert stop/skip phase if we allowed to. TODO: move layer ?
if (languageSession instanceof X4OLanguageSessionLocal) {
X4OLanguageSessionLocal ll = (X4OLanguageSessionLocal)languageSession;
if (phaseStop!=null) {
if (phaseStop != null) {
ll.setPhaseStop(phaseStop);
}
for (String phaseId:phaseSkip) {
for (String phaseId : phaseSkip) {
ll.addPhaseSkip(phaseId);
}
}
// init debug
debugStart(languageSession, DEBUG_OUTPUT_HANDLER, DEBUG_OUTPUT_STREAM, DEBUG_OUTPUT_STREAM_CLOSE);
debugStart(languageSession, DEBUG_OUTPUT_HANDLER, DEBUG_OUTPUT_STREAM);
try {
// Run init (auto once)
X4OLanguageLocal.class.cast(languageSession.getLanguage()).init(languageSession);
// Run document parsing
X4OContentParser parser = new X4OContentParser(propertyConfig);
parser.parse(languageSession);

View file

@ -46,6 +46,7 @@ import org.x4o.xml.element.ElementInterface;
import org.x4o.xml.element.ElementNamespace;
import org.x4o.xml.element.ElementObjectPropertyValueException;
import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.X4OLanguageLocal;
import org.x4o.xml.lang.X4OLanguageSession;
import org.x4o.xml.lang.X4OLanguageModule;
import org.x4o.xml.lang.phase.X4OPhaseException;
@ -76,17 +77,15 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
public final static String SCHEMA_ROOT_URI = PROPERTY_CONTEXT_PREFIX+"schema/root-uri";
public final static String DEBUG_OUTPUT_HANDLER = PROPERTY_CONTEXT_PREFIX + ABSTRACT_DEBUG_OUTPUT_HANDLER;
public final static String DEBUG_OUTPUT_STREAM = PROPERTY_CONTEXT_PREFIX + ABSTRACT_DEBUG_OUTPUT_STREAM;
public final static String DEBUG_OUTPUT_STREAM_CLOSE = PROPERTY_CONTEXT_PREFIX + ABSTRACT_DEBUG_OUTPUT_STREAM_CLOSE;
static {
DEFAULT_PROPERTY_CONFIG = new SAX3PropertyConfig(true,SAX3WriterXml.DEFAULT_PROPERTY_CONFIG,PROPERTY_CONTEXT_PREFIX,
DEFAULT_PROPERTY_CONFIG = new SAX3PropertyConfig(true, SAX3WriterXml.DEFAULT_PROPERTY_CONFIG, PROPERTY_CONTEXT_PREFIX,
new PropertyConfigItem(true,OUTPUT_STREAM,OutputStream.class),
new PropertyConfigItem(OUTPUT_XDBX,Boolean.class,false),
new PropertyConfigItem(SCHEMA_PRINT,Boolean.class,true),
new PropertyConfigItem(SCHEMA_ROOT_URI,String.class),
new PropertyConfigItem(DEBUG_OUTPUT_HANDLER,ContentWriter.class),
new PropertyConfigItem(DEBUG_OUTPUT_STREAM,OutputStream.class),
new PropertyConfigItem(DEBUG_OUTPUT_STREAM_CLOSE,Boolean.class,true)
new PropertyConfigItem(DEBUG_OUTPUT_STREAM,OutputStream.class)
);
}
@ -112,8 +111,9 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
public void writeSession(X4OLanguageSession languageSession, OutputStream output) throws X4OConnectionException,SAXException,IOException {
setProperty(OUTPUT_STREAM, output);
addPhaseSkip(X4OPhaseLanguageWrite.WRITE_RELEASE);
debugStart(languageSession, DEBUG_OUTPUT_HANDLER, DEBUG_OUTPUT_STREAM, DEBUG_OUTPUT_STREAM_CLOSE);
debugStart(languageSession, DEBUG_OUTPUT_HANDLER, DEBUG_OUTPUT_STREAM);
try {
X4OLanguageLocal.class.cast(languageSession.getLanguage()).init(languageSession);
languageSession.getLanguage().getPhaseManager().runPhases(languageSession, X4OPhaseType.XML_WRITE);
} catch (X4OPhaseException e) {
throw new X4OConnectionException(e);
@ -121,6 +121,7 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
runWrite(languageSession);
}
@SuppressWarnings("resource") // caller closes the resource thus writerXML and writerXDBX are not closed
private void runWrite(X4OLanguageSession languageSession) throws X4OConnectionException {
OutputStream out = (OutputStream)getProperty(OUTPUT_STREAM);
try {
@ -169,7 +170,7 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
debugException(languageSession, e);
throw new X4OConnectionException(e);
} finally {
if (out!=null) {
if (out != null) {
try {
out.close();
} catch (IOException e) {
@ -201,7 +202,7 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
if (m.getName().equals("getClass")) {
continue;
}
if (m.getName().startsWith("get")==false) {
if (m.getName().startsWith("get") == false) {
continue;
}
String name = m.getName().substring(3, 4).toLowerCase() + m.getName().substring(4);
@ -233,7 +234,7 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
private void writeTree(ContentWriter writer, Element element, boolean isRoot) throws SAXException, ElementObjectPropertyValueException {
List<AttributeEntry> attr = new ArrayList<AttributeEntry>(20);
if (element.getElementClass().getAutoAttributes() != null && element.getElementClass().getAutoAttributes() == false) {
for (ElementClassAttribute eca:element.getElementClass().getElementClassAttributes()) {
for (ElementClassAttribute eca : element.getElementClass().getElementClassAttributes()) {
if (eca.getRunBeanValue() != null && eca.getRunBeanValue() == false) {
continue;
}
@ -241,7 +242,7 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
if (value == null) {
continue;
}
AttributeEntry e = new AttributeEntry(eca.getId(), ""+value, calcOrderNumber(eca.getId(),eca.getWriteOrder()));
AttributeEntry e = new AttributeEntry(eca.getId(), "" + value, calcOrderNumber(eca.getId(), eca.getWriteOrder()));
attr.add(e);
}
@ -278,7 +279,7 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
if (value instanceof List || value instanceof Collection) {
continue; // TODO; filter on type of childeren
}
AttributeEntry e = new AttributeEntry(p, ""+value, calcOrderNumber(p,writeOrder));
AttributeEntry e = new AttributeEntry(p, "" + value, calcOrderNumber(p,writeOrder));
attr.add(e);
}
}
@ -288,7 +289,7 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
if (isRoot && schemaUriPrint) {
String rootUri = findElementUri(element);
writer.startPrefixMapping("xsi", SAX3XMLConstants.XML_SCHEMA_INSTANCE_NS_URI);
atts.addAttribute ("xsi", "schemaLocation", "", "", rootUri+" "+schemaUriRoot);
atts.addAttribute ("xsi", "schemaLocation", "", "", rootUri + " " + schemaUriRoot);
}
// Sort attributes in natural order of localName and add to attributes

View file

@ -31,7 +31,6 @@ import java.util.Map;
import org.x4o.sax3.SAX3WriterEnum;
import org.x4o.sax3.io.ContentWriter;
import org.x4o.sax3.io.SAX3XMLConstants;
import org.x4o.xml.conv.ObjectConverter;
import org.x4o.xml.element.Element;
import org.x4o.xml.element.ElementNamespaceAttribute;
@ -48,6 +47,7 @@ import org.x4o.xml.element.ElementNamespaceInstanceProvider;
import org.x4o.xml.lang.X4OLanguageModule;
import org.x4o.xml.lang.X4OLanguageModuleLoaderResult;
import org.x4o.xml.lang.X4OLanguageSession;
import org.x4o.xml.lang.X4OLanguageSessionLocal;
import org.x4o.xml.lang.X4OLanguageConfiguration;
import org.x4o.xml.lang.phase.X4OPhase;
import org.x4o.xml.lang.phase.X4OPhaseException;
@ -63,12 +63,13 @@ import org.xml.sax.helpers.AttributesImpl;
*/
public class X4ODebugWriter {
static public final String DEBUG_URI = "http://language.x4o.org/xml/ns/debug-output";
static public final String DEBUG_URI = "http://language.x4o.org/xml/ns/debug-output";
static public final String DEBUG_URI_NS = "debug";
protected SAX3WriterEnum<Tag,ContentWriter> contentWriter = null;
public X4ODebugWriter(ContentWriter debugWriter) {
this.contentWriter=new SAX3WriterEnum<Tag,ContentWriter>(debugWriter,DEBUG_URI,SAX3XMLConstants.NULL_NS_URI);
this.contentWriter = new SAX3WriterEnum<Tag,ContentWriter>(debugWriter, DEBUG_URI, DEBUG_URI_NS);
}
public ContentWriter getContentWriter() {
@ -83,6 +84,7 @@ public class X4ODebugWriter {
X4OConnection,
X4OLanguageSession,
X4OLanguageSessionSkipPhase,
X4OLanguageSessionPhaseListener,
X4OConnectionProperties,
X4OConnectionProperty,
@ -91,7 +93,6 @@ public class X4ODebugWriter {
phaseOrder,
phase,
X4OPhaseListener,
X4OPhaseDependency,
ElementLanguageModules,
@ -196,10 +197,24 @@ public class X4ODebugWriter {
atts = new AttributesImpl();
atts.addAttribute("", "phaseStop", "", "", languageSession.getPhaseStop());
atts.addAttribute("", "className", "", "", languageSession.getClass().getName());
//atts.addAttribute("", "listenersSize", "", "", phase.getPhaseListeners().size()+"");
contentWriter.printTagStart(Tag.X4OLanguageSession, atts);
for (String skipPhase:languageSession.getPhaseSkip()) {
contentWriter.printTagCharacters(Tag.X4OLanguageSessionSkipPhase, skipPhase);
}
if (languageSession instanceof X4OLanguageSessionLocal) {
X4OLanguageSessionLocal languageSessionLocal = X4OLanguageSessionLocal.class.cast(languageSession);
for (String key : languageSession.getLanguage().getPhaseManager().getPhaseKeys()) {
X4OPhase p = languageSession.getLanguage().getPhaseManager().getPhase(key);
List<X4OPhaseListener> listeners = languageSessionLocal.storePhaseListeners(p.getId());
for (X4OPhaseListener l : listeners) {
atts = new AttributesImpl();
atts.addAttribute("", "phaseId", "", "", p.getId());
atts.addAttribute("", "className", "", "", l.getClass().getName());
contentWriter.printTagStartEnd(Tag.X4OLanguageSessionPhaseListener, atts);
}
}
}
contentWriter.printTagEnd(Tag.X4OLanguageSession);
atts = new AttributesImpl();
@ -286,14 +301,8 @@ public class X4ODebugWriter {
atts.addAttribute("", "id", "", "", phase.getId());
atts.addAttribute("", "type", "", "", phase.getType().name());
atts.addAttribute("", "runOnce", "", "", phase.isRunOnce()+"");
atts.addAttribute("", "listenersSize", "", "", phase.getPhaseListeners().size()+"");
contentWriter.printTagStart(Tag.phase, atts);
for (X4OPhaseListener l:phase.getPhaseListeners()) {
atts = new AttributesImpl();
atts.addAttribute("", "className", "", "", l.getClass().getName());
contentWriter.printTagStartEnd(Tag.X4OPhaseListener, atts);
}
for (String dep:phase.getPhaseDependencies()) {
contentWriter.printTagCharacters(Tag.X4OPhaseDependency, dep);
}

View file

@ -23,7 +23,9 @@
package org.x4o.xml.lang;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.el.ELContext;
@ -34,6 +36,7 @@ import org.x4o.xml.element.ElementAttributeValueParser;
import org.x4o.xml.element.ElementObjectPropertyValue;
import org.x4o.xml.io.X4ODebugWriter;
import org.x4o.xml.lang.phase.X4OPhase;
import org.x4o.xml.lang.phase.X4OPhaseListener;
/**
* AbstractX4OLanguageSession.
@ -55,19 +58,21 @@ public abstract class AbstractX4OLanguageSession implements X4OLanguageSessionLo
private X4OPhase phaseCurrent = null;
private String phaseStop = null;
private List<String> phaseSkip = null;
private Map<String, List<X4OPhaseListener>> phaseListeners = null;
/**
* Creates a new empty language context.
*/
public AbstractX4OLanguageSession(X4OLanguage language) {
if (language==null) {
if (language == null) {
throw new NullPointerException("language may not be null");
}
logger = Logger.getLogger(AbstractX4OLanguageSession.class.getName());
logger.finest("Creating new ParsingContext");
this.language=language;
dirtyElements = new ArrayList<Element>(20);
phaseSkip = new ArrayList<String>(5);
dirtyElements = new ArrayList<>(20);
phaseSkip = new ArrayList<>(5);
phaseListeners = new HashMap<>(3);
}
public X4OLanguage getLanguage() {
@ -238,4 +243,18 @@ public abstract class AbstractX4OLanguageSession implements X4OLanguageSessionLo
public void addPhaseSkip(String phaseId) {
phaseSkip.add(phaseId);
}
/**
* Gets the phase listeners for a phase.
* @param phaseId The phaseId of for the listeners.
* @return The x4o phase listeners.
*/
public List<X4OPhaseListener> storePhaseListeners(String phaseId) {
List<X4OPhaseListener> result = phaseListeners.get(phaseId);
if (result == null) {
result = new ArrayList<>(3);
phaseListeners.put(phaseId, result);
}
return result;
}
}

View file

@ -38,7 +38,9 @@ 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;
import org.x4o.xml.lang.phase.X4OPhaseType;
/**
* DefaultX4OLanguage holds all information about the x4o xml language.
@ -55,6 +57,7 @@ public class DefaultX4OLanguage implements X4OLanguageLocal {
private final String languageVersion;
private final X4OPhaseManager phaseManager;
private final Map<String, ElementNamespace> keyedNamespaceLookup;
private boolean inited = false;
public DefaultX4OLanguage(X4OLanguageConfiguration languageConfiguration, X4OPhaseManager phaseManager, String languageName, String languageVersion) {
if (languageName == null) {
@ -72,6 +75,15 @@ public class DefaultX4OLanguage implements X4OLanguageLocal {
this.phaseManager = phaseManager;
}
@Override
public void init(X4OLanguageSession session) throws X4OPhaseException {
if (inited) {
return;
}
inited = true;
getPhaseManager().runPhases(session, X4OPhaseType.INIT);
}
/**
* @see org.x4o.xml.lang.X4OLanguage#getLanguageName()
*/
@ -139,16 +151,13 @@ public class DefaultX4OLanguage implements X4OLanguageLocal {
languageSession.setExpressionLanguageFactory(X4OExpressionFactory.createExpressionFactory());
}
if (languageSession.getExpressionLanguageContext() == null) {
languageSession.setExpressionLanguageContext(
X4OExpressionFactory.createELContext(languageSession.getLanguage().getLanguageConfiguration().getDefaultExpressionLanguageContext()));
languageSession.setExpressionLanguageContext(X4OExpressionFactory.createELContext(languageSession.getLanguage().getLanguageConfiguration().getDefaultExpressionLanguageContext()));
}
if (languageSession.getElementAttributeValueParser() == null) {
languageSession.setElementAttributeValueParser(X4OLanguageClassLoader.newInstance(ElementAttributeValueParser.class,
getLanguageConfiguration().getDefaultElementAttributeValueParser()));
languageSession.setElementAttributeValueParser(X4OLanguageClassLoader.newInstance(ElementAttributeValueParser.class, getLanguageConfiguration().getDefaultElementAttributeValueParser()));
}
if (languageSession.getElementObjectPropertyValue() == null) {
languageSession.setElementObjectPropertyValue(X4OLanguageClassLoader.newInstance(ElementObjectPropertyValue.class,
getLanguageConfiguration().getDefaultElementObjectPropertyValue()));
languageSession.setElementObjectPropertyValue(X4OLanguageClassLoader.newInstance(ElementObjectPropertyValue.class, getLanguageConfiguration().getDefaultElementObjectPropertyValue()));
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);

View file

@ -37,6 +37,7 @@ import javax.xml.parsers.SAXParserFactory;
import org.x4o.xml.eld.EldDriver;
import org.x4o.xml.eld.EldModuleLoader;
import org.x4o.xml.element.ElementException;
import org.x4o.xml.lang.phase.X4OPhaseLanguageInit;
import org.x4o.xml.lang.phase.X4OPhaseLanguageInit.X4OPhaseInitLanguageSiblings;
import org.xml.sax.Attributes;
@ -67,74 +68,70 @@ public class DefaultX4OLanguageLoader implements X4OLanguageLoader {
* @param language The X4O language we are loading.
* @param message The message to log to the debug output.
*/
private void logMessage(X4OLanguage language,String message) {
logger.finest(message+" from: "+language.getLanguageName());
/*
TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
private void logMessage(X4OLanguageSession session, String message) {
logger.finest(message + " from: " + session.getLanguage().getLanguageName());
if (session.hasX4ODebugWriter()) {
try {
language.getLanguageConfiguration().getX4ODebugWriter().debugPhaseMessage(message, this.getClass());
session.getX4ODebugWriter().debugPhaseMessage(message, this.getClass());
} catch (ElementException e) {
throw new RuntimeException(e);
}
}
*/
}
/**
* @see org.x4o.xml.lang.X4OLanguageLoader#loadLanguage(org.x4o.xml.lang.X4OLanguageLocal, java.lang.String, java.lang.String)
*/
public void loadLanguage(X4OLanguageLocal languageLocal, String language,String languageVersion) throws X4OLanguageLoaderException {
logger.finer("Loading all modules for language: "+language);
List<VersionedResources> modulesAll = loadLanguageModules(languageLocal,language);
modulesAll = filterVersionModules(modulesAll,languageLocal,languageVersion);
@Override
public void loadLanguage(X4OLanguageSession session, X4OLanguageLocal languageLocal, String language, String languageVersion) throws X4OLanguageLoaderException {
logger.finer("Loading all modules for language: " + language);
List<VersionedResources> modulesAll = loadLanguageModules(session, languageLocal, language);
modulesAll = filterVersionModules(modulesAll, languageLocal, languageVersion);
validateModules(modulesAll);
int loaded = 0;
for (VersionedResources versionedResources:modulesAll) {
for (VersionedResources versionedResources : modulesAll) {
X4OLanguageModuleLoader loader;
for (String value:versionedResources.eldResources) {
for (String value : versionedResources.eldResources) {
String languagePrefix = languageLocal.getLanguageConfiguration().getLanguageResourcePathPrefix();
String resource = languagePrefix+"/"+language+"/"+value;
String resource = languagePrefix + "/" + language + "/" + value;
if (language.equals(EldDriver.LANGUAGE_NAME)) {
loader = new EldModuleLoader(resource,true); // load cel
} else {
loader = new EldModuleLoader(resource,false); // load eld
}
loadModule(languageLocal,loader,value,versionedResources);
loadModule(session, languageLocal, loader, value, versionedResources);
}
for (String value:versionedResources.moduleLoaders) {
for (String value : versionedResources.moduleLoaders) {
try {
loader = (X4OLanguageModuleLoader)X4OLanguageClassLoader.newInstance(value);
} catch (Exception ee) {
throw new X4OLanguageLoaderException("Could not load class: "+value+" error: "+ee.getMessage(),ee);
throw new X4OLanguageLoaderException("Could not load class: " + value + " error: " + ee.getMessage(),ee);
}
loadModule(languageLocal,loader,value,versionedResources);
loadModule(session, languageLocal, loader, value, versionedResources);
}
for (String value:versionedResources.siblingLoaders) {
for (String value : versionedResources.siblingLoaders) {
try {
loader = (X4OLanguageModuleLoader)X4OLanguageClassLoader.newInstance(value);
} catch (Exception ee) {
throw new X4OLanguageLoaderException("Could not load class: "+value+" error: "+ee.getMessage(),ee);
throw new X4OLanguageLoaderException("Could not load class: " + value + " error: " + ee.getMessage(),ee);
}
loadModule(languageLocal,loader,value,versionedResources);
loadModule(session, languageLocal, loader, value, versionedResources);
if (loader instanceof X4OLanguageModuleLoaderSibling) {
// mmm
X4OPhaseInitLanguageSiblings sibPhase = (X4OPhaseInitLanguageSiblings)languageLocal.getPhaseManager().getPhase(X4OPhaseLanguageInit.INIT_LANG_SIB);
sibPhase.addLanguageModuleLoaderSibling((X4OLanguageModuleLoaderSibling)loader);
}
}
for (String value:versionedResources.elbResources) {
for (String value : versionedResources.elbResources) {
// TODO: add elb support
logger.finer("elb-resources are not done yet; "+value);
logger.finer("elb-resources are not done yet; " + value);
}
loaded++;
}
if (loaded==0) {
throw new X4OLanguageLoaderException("No modules defined for version: "+languageVersion);
if (loaded == 0) {
throw new X4OLanguageLoaderException("No modules defined for version: " + languageVersion);
}
}
private List<VersionedResources> filterVersionModules(List<VersionedResources> resources,X4OLanguageLocal languageLocal,String languageVersion) throws X4OLanguageLoaderException {
private List<VersionedResources> filterVersionModules(List<VersionedResources> resources, X4OLanguageLocal languageLocal, String languageVersion) throws X4OLanguageLoaderException {
List<VersionedResources> result = new ArrayList<VersionedResources>(resources.size());
X4OLanguageVersionFilter lvf;
try {
@ -142,11 +139,11 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
} catch (ClassNotFoundException e) {
throw new X4OLanguageLoaderException(e);
}
for (VersionedResources versionedResources:resources) {
for (VersionedResources versionedResources : resources) {
List<String> versions = new ArrayList<String>();
versions.add(versionedResources.version); // FIXME
String modulesVersion = lvf.filterVersion(languageVersion, versions);
if (modulesVersion==null) {
if (modulesVersion == null) {
continue;
}
result.add(versionedResources);
@ -159,44 +156,44 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
List<String> moduleLoaders = new ArrayList<String>(5);
List<String> elbResources = new ArrayList<String>(5);
List<String> siblingLoaders = new ArrayList<String>(5);
for (VersionedResources vr:resources) {
validateModuleList(eldResources,vr.eldResources,"eld-resource");
validateModuleList(moduleLoaders,vr.moduleLoaders,"module-loader");
validateModuleList(elbResources,vr.elbResources,"elb-resource");
validateModuleList(siblingLoaders,vr.siblingLoaders,"sibling-loader");
for (VersionedResources vr : resources) {
validateModuleList(eldResources, vr.eldResources, "eld-resource");
validateModuleList(moduleLoaders, vr.moduleLoaders, "module-loader");
validateModuleList(elbResources, vr.elbResources, "elb-resource");
validateModuleList(siblingLoaders, vr.siblingLoaders, "sibling-loader");
}
}
private void validateModuleList(List<String> data,List<String> values,String xmlTag) throws X4OLanguageLoaderException {
for (String value:values) {
private void validateModuleList(List<String> data, List<String> values, String xmlTag) throws X4OLanguageLoaderException {
for (String value : values) {
if (data.contains(value)) {
throw new X4OLanguageLoaderException("Duplicate "+xmlTag+" entry detected; "+value);
throw new X4OLanguageLoaderException("Duplicate " + xmlTag + " entry detected; " + value);
}
data.add(value);
}
}
private void loadModule(X4OLanguageLocal languageLocal,X4OLanguageModuleLoader loader,String resource,VersionedResources versionedResources) throws X4OLanguageLoaderException {
private void loadModule(X4OLanguageSession session, X4OLanguageLocal languageLocal, X4OLanguageModuleLoader loader, String resource, VersionedResources versionedResources) throws X4OLanguageLoaderException {
X4OLanguageModuleLocal module;
try {
module = X4OLanguageClassLoader.newInstance(X4OLanguageModuleLocal.class, languageLocal.getLanguageConfiguration().getDefaultElementLanguageModule());
} catch (ClassNotFoundException e) {
throw new X4OLanguageLoaderException(e);
}
logMessage(languageLocal,"Created module: "+module);
logMessage(session,"Created module: " + module);
long startTime = System.currentTimeMillis();
try {
logMessage(languageLocal,"Starting modules: "+module+" for language: "+languageLocal.getLanguageName());
loader.loadLanguageModule(languageLocal, module);
logMessage(session,"Starting modules: " + module + " for language: " + languageLocal.getLanguageName());
loader.loadLanguageModule(session, languageLocal, module);
} catch (X4OLanguageModuleLoaderException e) {
throw new X4OLanguageLoaderException(e); // FIXME info
}
long totalTime = System.currentTimeMillis() - startTime;
module.putLoaderResult(X4OLanguageModuleLoaderResult.LOAD_TIME, ""+totalTime);
module.putLoaderResult(X4OLanguageModuleLoaderResult.LOAD_TIME, "" + totalTime);
module.putLoaderResult(X4OLanguageModuleLoaderResult.LOAD_VERSION, versionedResources.version);
module.putLoaderResult(X4OLanguageModuleLoaderResult.LOAD_CLASS, loader.getClass().getName());
module.putLoaderResult(X4OLanguageModuleLoaderResult.LOAD_DATE, ""+new Date());
if (resource!=null) {
module.putLoaderResult(X4OLanguageModuleLoaderResult.LOAD_DATE, "" + new Date());
if (resource != null) {
module.putLoaderResult(X4OLanguageModuleLoaderResult.LOAD_MODULE_RESOURCE, resource);
}
module.putLoaderResult(X4OLanguageModuleLoaderResult.LOAD_FROM_RESOURCE, versionedResources.loadedFrom);
@ -209,7 +206,7 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
* @param languageLocal The ElementLanguage to load for.
* @param language The language to load.
*/
protected List<VersionedResources> loadLanguageModules(X4OLanguageLocal languageLocal,String language) throws X4OLanguageLoaderException {
protected List<VersionedResources> loadLanguageModules(X4OLanguageSession session, X4OLanguageLocal languageLocal, String language) throws X4OLanguageLoaderException {
List<VersionedResources> result = new ArrayList<VersionedResources>(15);
StringBuilder buf = new StringBuilder(150);
buf.append(languageLocal.getLanguageConfiguration().getLanguageResourcePathPrefix());
@ -219,20 +216,20 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
buf.append(language);
buf.append(languageLocal.getLanguageConfiguration().getLanguageResourceModulesFileName());
logger.finer("loading X4O language: "+language);
logger.finer("loading X4O language: " + language);
try {
Enumeration<URL> e = Thread.currentThread().getContextClassLoader().getResources(buf.toString());
while(e.hasMoreElements()) {
while (e.hasMoreElements()) {
URL u = e.nextElement();
logMessage(languageLocal,"Loading relative modules: "+u+" for: "+language);
result.addAll(loadLanguageModulesXml(u.openStream(),u.toString()));
logMessage(session, "Loading relative modules: " + u + " for: " + language);
result.addAll(loadLanguageModulesXml(u.openStream(), u.toString()));
}
e = Thread.currentThread().getContextClassLoader().getResources("/"+buf.toString());
while(e.hasMoreElements()) {
while (e.hasMoreElements()) {
URL u = e.nextElement();
logMessage(languageLocal,"Loading root modules: "+u+" for: "+language);
result.addAll(loadLanguageModulesXml(u.openStream(),u.toString()));
logMessage(session, "Loading root modules: " + u + " for: " + language);
result.addAll(loadLanguageModulesXml(u.openStream(), u.toString()));
}
return result;
} catch (IOException | SAXException | ParserConfigurationException e) {
@ -247,8 +244,8 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
* @throws SAXException
* @throws ParserConfigurationException
*/
protected List<VersionedResources> loadLanguageModulesXml(InputStream in,String loadedFrom) throws IOException, SAXException, ParserConfigurationException {
if (in==null) {
protected List<VersionedResources> loadLanguageModulesXml(InputStream in, String loadedFrom) throws IOException, SAXException, ParserConfigurationException {
if (in == null) {
throw new NullPointerException("Can't parse null input stream");
}
ModulesTagHandler xth = new ModulesTagHandler(loadedFrom);
@ -274,12 +271,11 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
private List<VersionedResources> result = null;
public ModulesTagHandler(String loadedFrom) {
this.loadedFrom=loadedFrom;
this.loadedFrom = loadedFrom;
this.result = new ArrayList<VersionedResources>(5);
}
public List<VersionedResources> getResult()
{
public List<VersionedResources> getResult() {
return result;
}
@ -288,13 +284,13 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
}
@Override
public void startElement(String namespaceUri, String tag, String qName,Attributes attr) throws SAXException {
public void startElement(String namespaceUri, String tag, String qName, Attributes attr) throws SAXException {
if ("language".equals(tag)) {
String version = attr.getValue("version");
versionedResources = new VersionedResources();
versionedResources.version=version;
versionedResources.loadedFrom=loadedFrom;
logger.finest("Version attribute: "+version);
versionedResources.version = version;
versionedResources.loadedFrom = loadedFrom;
logger.finest("Version attribute: " + version);
}
}
@ -314,7 +310,7 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
versionedResources = null;
return;
}
if (versionedResources==null) {
if (versionedResources == null) {
return;
}
@ -327,13 +323,13 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
} else if ("sibling-loader".equals(tag)) {
versionedResources.siblingLoaders.add(value);
}
logger.finest("Stored tag: "+tag+" value: "+value);
logger.finest("Stored tag: " + tag + " value: " + value);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String text = new String(ch,start,length).trim();
if (text.length()==0) {
String text = new String(ch, start, length).trim();
if (text.length() == 0) {
return;
}
buf.append(text);

View file

@ -34,7 +34,7 @@ import org.x4o.xml.lang.phase.X4OPhaseException;
public class DefaultX4OLanguageSession extends AbstractX4OLanguageSession {
/**
* Creates a new empty language context.
* Creates a new empty language session.
*/
public DefaultX4OLanguageSession(X4OLanguage language) {
super(language);

View file

@ -32,10 +32,11 @@ public interface X4OLanguageLoader {
/**
* Loads the language modules.
* @param languageLocal The elementLanguage to load the module in.
* @param session The session we run in.
* @param language The local Language to load for.
* @param languageName The language name to load.
* @param languageVersion The language version to load.
* @throws X4OLanguageLoaderException When there is an error.
*/
void loadLanguage(X4OLanguageLocal languageLocal,String languageName,String languageVersion) throws X4OLanguageLoaderException;
void loadLanguage(X4OLanguageSession session, X4OLanguageLocal language, String languageName, String languageVersion) throws X4OLanguageLoaderException;
}

View file

@ -22,6 +22,7 @@
*/
package org.x4o.xml.lang;
import org.x4o.xml.lang.phase.X4OPhaseException;
/**
* X4OLanguageLocal exposes the add method to load the language.
@ -31,11 +32,8 @@ package org.x4o.xml.lang;
*/
public interface X4OLanguageLocal extends X4OLanguage {
/*
* @param parserConfiguration The parserConfiguration to set.
void setLanguageConfiguration(X4OLanguageConfiguration parserConfiguration);
*/
// TODO: refactor a bit more a this is now called manually on magic places
void init(X4OLanguageSession session) throws X4OPhaseException;
/**
* Adds an X4OLanguageModule to this language.

View file

@ -33,9 +33,10 @@ public interface X4OLanguageModuleLoader {
/**
* Starts the ElementProvider.
* @param language The ElementLanguage to load for.
* @param module The ElementLanguageModule to load it into.
* @param session The session we run in.
* @param language The local Language to load for.
* @param module The language module to load it into.
* @throws X4OLanguageModuleLoaderException Gets thrown when modules could not be correctly loaded.
*/
void loadLanguageModule(X4OLanguageLocal language,X4OLanguageModuleLocal module) throws X4OLanguageModuleLoaderException;
void loadLanguageModule(X4OLanguageSession session, X4OLanguageLocal language, X4OLanguageModuleLocal module) throws X4OLanguageModuleLoaderException;
}

View file

@ -35,9 +35,10 @@ public interface X4OLanguageModuleLoaderSibling extends X4OLanguageModuleLoader
/**
* Loads in the sibling language.
* @param languageLocal The ElementLanguage for which we load an sibling.
* @param languageLoader The loader to use to load the x4o languages.
* @param session The session we run in.
* @param language The local Language to load for.
* @param loader The loader to use to load the x4o languages.
* @throws X4OLanguageLoaderException Gets thrown when there is an error loading the sibling language.
*/
void loadLanguageSibling(X4OLanguageLocal languageLocal,X4OLanguageLoader languageLoader) throws X4OLanguageLoaderException;
void loadLanguageSibling(X4OLanguageSession session, X4OLanguageLocal language, X4OLanguageLoader loader) throws X4OLanguageLoaderException;
}

View file

@ -22,6 +22,8 @@
*/
package org.x4o.xml.lang;
import java.util.List;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
@ -29,6 +31,7 @@ import org.x4o.xml.element.ElementAttributeValueParser;
import org.x4o.xml.element.ElementObjectPropertyValue;
import org.x4o.xml.io.X4ODebugWriter;
import org.x4o.xml.lang.phase.X4OPhase;
import org.x4o.xml.lang.phase.X4OPhaseListener;
/**
* X4OLanguageSessionLocal is the local set interface for X4OLanguageSession.
@ -37,7 +40,15 @@ import org.x4o.xml.lang.phase.X4OPhase;
* @version 1.0 Oct 28, 2009
*/
public interface X4OLanguageSessionLocal extends X4OLanguageSession {
/**
* Returns list of phase listeners which where added to it.
* TODO: bad api here
* @param phaseId The phaseId of for the listeners.
* @return All X4OPhaseListeners.
*/
List<X4OPhaseListener> storePhaseListeners(String phaseId);
/**
* Sets the EL Context.
* @param context The ELContext to set.

View file

@ -28,6 +28,7 @@ import org.x4o.xml.lang.X4OLanguageLoader;
import org.x4o.xml.lang.X4OLanguageLoaderException;
import org.x4o.xml.lang.X4OLanguageLocal;
import org.x4o.xml.lang.X4OLanguageModuleLocal;
import org.x4o.xml.lang.X4OLanguageSession;
/**
* MetaLanguageSiblingLoader loads the generic x4o meta language into defined language.
@ -49,30 +50,16 @@ public class MetaLanguageSiblingLoader implements X4OLanguageModuleLoaderSibling
/** Defines the identifier of the meta x4o language host. */
public static final String META_LANGUAGE_DESCRIPTION = "X4O Meta XML Language Module.";
/**
* Loads an ElementLanguageModule.
* @param language The ElementLanguage to load for.
* @param languageModule The ElementLanguageModule to load into.
* @throws X4OLanguageModuleLoaderException Is thrown when meta language could not be loaded.
* @see org.x4o.xml.lang.X4OLanguageModuleLoader#loadLanguageModule(org.x4o.xml.lang.X4OLanguageLocal, org.x4o.xml.lang.X4OLanguageModuleLocal)
*/
public void loadLanguageModule(X4OLanguageLocal language,X4OLanguageModuleLocal languageModule) throws X4OLanguageModuleLoaderException {
languageModule.setId(META_LANGUAGE);
languageModule.setProviderHost(META_LANGUAGE_HOST);
languageModule.setProviderName(MetaLanguageSiblingLoader.class.getSimpleName());
languageModule.setDescription(META_LANGUAGE_DESCRIPTION);
@Override
public void loadLanguageModule(X4OLanguageSession session, X4OLanguageLocal language, X4OLanguageModuleLocal module) throws X4OLanguageModuleLoaderException {
module.setId(META_LANGUAGE);
module.setProviderHost(META_LANGUAGE_HOST);
module.setProviderName(MetaLanguageSiblingLoader.class.getSimpleName());
module.setDescription(META_LANGUAGE_DESCRIPTION);
}
/**
* Loads an sibling language.
* @param language The ElementLanguage to load for.
* @param languageLoader The x4o language loader.
* @throws X4OLanguageLoaderException
* @see org.x4o.xml.lang.X4OLanguageModuleLoaderSibling#loadLanguageSibling(org.x4o.xml.lang.X4OLanguageLocal, org.x4o.xml.lang.X4OLanguageLoader)
*/
public void loadLanguageSibling(X4OLanguageLocal language,X4OLanguageLoader languageLoader) throws X4OLanguageLoaderException {
// Load the meta language.
languageLoader.loadLanguage(language, META_LANGUAGE, META_LANGUAGE_VERSION);
@Override
public void loadLanguageSibling(X4OLanguageSession session, X4OLanguageLocal language, X4OLanguageLoader loader) throws X4OLanguageLoaderException {
loader.loadLanguage(session, language, META_LANGUAGE, META_LANGUAGE_VERSION);
}
}

View file

@ -22,10 +22,8 @@
*/
package org.x4o.xml.lang.phase;
import java.util.ArrayList;
import java.util.List;
import org.x4o.xml.element.Element;
import org.x4o.xml.element.ElementException;
import org.x4o.xml.lang.X4OLanguageSession;
@ -36,14 +34,11 @@ import org.x4o.xml.lang.X4OLanguageSession;
* @version 1.0 Dec 31, 2008
*/
public abstract class AbstractX4OPhase implements X4OPhase {
protected List<X4OPhaseListener> phaseListeners = null;
/**
* Creates the AbstractX4OPhaseHandler.
*/
public AbstractX4OPhase() {
phaseListeners = new ArrayList<X4OPhaseListener>(3);
}
/**
@ -53,30 +48,6 @@ public abstract class AbstractX4OPhase implements X4OPhase {
return false;
}
/**
* Gets the phase listeners.
* @return The x4o phase listeners.
*/
public List<X4OPhaseListener> getPhaseListeners() {
return phaseListeners;
}
/**
* Adds a phase listener.
* @param listener The phase listener to add.
*/
public void addPhaseListener(X4OPhaseListener listener) {
phaseListeners.add(listener);
}
/**
* Removed a phase listener.
* @param listener The phase listener to remove.
*/
public void removePhaseListener(X4OPhaseListener listener) {
phaseListeners.remove(listener);
}
/**
* If returns true then this handler will run on all elements.
* @return defaults to true.
@ -97,6 +68,17 @@ public abstract class AbstractX4OPhase implements X4OPhase {
* @param elementLanguage The language to run phase for.
* @throws X4OPhaseException when phase has error.
*/
public void runPhase(X4OLanguageSession elementLanguage) throws X4OPhaseException {
public void runPhase(X4OLanguageSession languageSession) throws X4OPhaseException {
}
public void debugPhaseMessage(X4OLanguageSession languageSession, String message) throws X4OPhaseException {
if (!languageSession.hasX4ODebugWriter()) {
return;
}
try {
languageSession.getX4ODebugWriter().debugPhaseMessage(message,this.getClass());
} catch (ElementException ee) {
throw new X4OPhaseException(this, ee);
}
}
}

View file

@ -41,7 +41,7 @@ import org.x4o.xml.lang.X4OLanguageSessionLocal;
public class DefaultX4OPhaseManager implements X4OPhaseManagerLocal {
/** The X4OPhaseHandler */
private List<X4OPhase> x4oPhases = null;
private final List<X4OPhase> x4oPhases;
/**
* Constructor.
@ -68,7 +68,7 @@ PHASE_ORDER = { *startupX4OPhase,
*/
public X4OPhase getPhase(String phaseName) {
for (X4OPhase phase:x4oPhases) {
for (X4OPhase phase : x4oPhases) {
if (phase.getId().equals(phaseName)) {
return phase;
}
@ -78,7 +78,7 @@ PHASE_ORDER = { *startupX4OPhase,
public List<String> getPhaseKeys() {
List<String> result = new ArrayList<String>(x4oPhases.size());
for (X4OPhase phase:x4oPhases) {
for (X4OPhase phase : x4oPhases) {
result.add(phase.getId());
}
return result;
@ -97,7 +97,6 @@ PHASE_ORDER = { *startupX4OPhase,
// throw new IllegalStateException("Can't add new phases after first phase is completed.");
//}
x4oPhases.add(phase);
}
/**
@ -114,12 +113,12 @@ PHASE_ORDER = { *startupX4OPhase,
*/
public List<X4OPhase> getOrderedPhases(X4OPhaseType type) {
List<X4OPhase> result = new ArrayList<X4OPhase>(x4oPhases.size());
for (X4OPhase p:x4oPhases) {
for (X4OPhase p : x4oPhases) {
if (p.getType().equals(type)) {
result.add(p);
}
}
Collections.sort(result,new X4OPhaseComparator());
Collections.sort(result, new X4OPhaseComparator());
return result;
}
@ -127,13 +126,15 @@ PHASE_ORDER = { *startupX4OPhase,
* Runs all the phases in the right order.
* @throws X4OPhaseException When a running handlers throws one.
*/
public void runPhases(X4OLanguageSession languageSession,X4OPhaseType type) throws X4OPhaseException {
public void runPhases(X4OLanguageSession languageSession, X4OPhaseType type) throws X4OPhaseException {
// convert to local for state changes
X4OLanguageSessionLocal languageSessionLocal = X4OLanguageSessionLocal.class.cast(languageSession);
// sort for the order
List<X4OPhase> x4oPhasesOrder = getOrderedPhases(type);
// debug output
if (languageSession.getX4ODebugWriter()!=null) {
if (languageSession.getX4ODebugWriter() != null) {
languageSession.getX4ODebugWriter().debugPhaseOrder(x4oPhasesOrder);
}
@ -141,17 +142,17 @@ PHASE_ORDER = { *startupX4OPhase,
String phaseStop = languageSession.getPhaseStop();
// run the phases in ordered order
for (X4OPhase phase:x4oPhasesOrder) {
for (X4OPhase phase : x4oPhasesOrder) {
if (phaseSkip.contains(phase.getId())) {
continue; // skip phase when requested by context
}
// debug output
((X4OLanguageSessionLocal)languageSession).setPhaseCurrent(phase);
languageSessionLocal.setPhaseCurrent(phase);
// run listeners
for (X4OPhaseListener l:phase.getPhaseListeners()) {
for (X4OPhaseListener l : languageSessionLocal.storePhaseListeners(phase.getId())) {
l.preRunPhase(phase, languageSession);
}
@ -161,15 +162,15 @@ PHASE_ORDER = { *startupX4OPhase,
phase.runPhase(languageSession);
// run the element phase if possible
executePhaseRoot(languageSession,phase);
executePhaseRoot(languageSession, phase);
} finally {
// run the listeners again
for (X4OPhaseListener l:phase.getPhaseListeners()) {
for (X4OPhaseListener l : languageSessionLocal.storePhaseListeners(phase.getId())) {
l.endRunPhase(phase, languageSession);
}
}
if (phaseStop!=null && phaseStop.equals(phase.getId())) {
if (phaseStop != null && phaseStop.equals(phase.getId())) {
return; // we are done
}
}
@ -188,8 +189,8 @@ PHASE_ORDER = { *startupX4OPhase,
// sort for the order
List<X4OPhase> x4oPhasesOrder = getOrderedPhases(type);
for (X4OPhase phase:x4oPhasesOrder) {
if (phase.getId().equals(p.getId())==false) {
for (X4OPhase phase : x4oPhasesOrder) {
if (phase.getId().equals(p.getId()) == false) {
continue; // we start running all phases from specified phase
}
if (phaseSkip.contains(phase.getId())) {
@ -203,9 +204,9 @@ PHASE_ORDER = { *startupX4OPhase,
phase.runPhase(languageSession);
// run the element phase if possible
executePhaseRoot(languageSession,phase);
executePhaseRoot(languageSession, phase);
if (phaseStop!=null && phaseStop.equals(phase.getId())) {
if (phaseStop != null && phaseStop.equals(phase.getId())) {
return; // we are done
}
}
@ -224,25 +225,25 @@ PHASE_ORDER = { *startupX4OPhase,
if (phaseSkip.contains(X4OPhaseLanguageWrite.WRITE_RELEASE)) {
releaseRequested = X4OPhaseLanguageWrite.WRITE_RELEASE;
}
if (releaseRequested==null) {
if (releaseRequested == null) {
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 ?
}
if (languageSession.getRootElement().getElementClass()==null) {
if (languageSession.getRootElement().getElementClass() == null) {
throw new IllegalStateException("Release phase has already been runned.");
}
X4OPhase h = null;
for (X4OPhase phase:x4oPhases) {
for (X4OPhase phase : x4oPhases) {
if (phase.getId().equals(releaseRequested)) {
h = phase;
break;
}
}
if (h==null) {
if (h == null) {
throw new IllegalStateException("No release phase found in manager to run.");
}
@ -253,7 +254,7 @@ PHASE_ORDER = { *startupX4OPhase,
h.runPhase(languageSession);
// run the element phase if possible
executePhaseRoot(languageSession,h);
executePhaseRoot(languageSession, h);
}
class X4OPhaseComparator implements Comparator<X4OPhase> {
@ -265,7 +266,7 @@ PHASE_ORDER = { *startupX4OPhase,
String pid = e1.getId();
String[] dpids = e2.getPhaseDependencies();
for (int i=0;i<dpids.length;i++) {
for (int i = 0; i < dpids.length; i++) {
String dpid = dpids[i];
if (pid.equals(dpid)) {
return -1;
@ -315,8 +316,8 @@ PHASE_ORDER = { *startupX4OPhase,
* @param phase The phase to run.
* @throws X4OPhaseException When a running handlers throws one.
*/
private void executePhaseRoot(X4OLanguageSession elementLanguage,X4OPhase phase) throws X4OPhaseException {
if (elementLanguage.getRootElement()==null) {
private void executePhaseRoot(X4OLanguageSession elementLanguage, X4OPhase phase) throws X4OPhaseException {
if (elementLanguage.getRootElement() == null) {
return;
}
executePhaseTree(elementLanguage.getRootElement(),phase);
@ -329,8 +330,8 @@ PHASE_ORDER = { *startupX4OPhase,
* @param phase The phase to run.
* @throws X4OPhaseException
*/
private void executePhaseTree(Element element,X4OPhase phase) throws X4OPhaseException {
if (element.getElementClass()!=null && element.getElementClass().getSkipPhases().contains(phase.getId())==false) {
private void executePhaseTree(Element element, X4OPhase phase) throws X4OPhaseException {
if (element.getElementClass() != null && element.getElementClass().getSkipPhases().contains(phase.getId()) == false) {
phase.runElementPhase(element);
}
for (Element e:element.getChilderen()) {

View file

@ -22,12 +22,9 @@
*/
package org.x4o.xml.lang.phase;
import java.util.List;
import org.x4o.xml.element.Element;
import org.x4o.xml.lang.X4OLanguageSession;
/**
* X4OPhase is one small step in the read or write process of the language.
*
@ -59,24 +56,6 @@ public interface X4OPhase {
*/
void runPhase(X4OLanguageSession elementLanguage) throws X4OPhaseException;
/**
* Returns all phase listeners which where added.
* @return All X4OPhaseListeners.
*/
List<X4OPhaseListener> getPhaseListeners();
/**
* Adds an X4OPhaseListener.
* @param listener The listener to add.
*/
void addPhaseListener(X4OPhaseListener listener);
/**
* Removes an X4OPhaseListener.
* @param listener The listener to remove.
*/
void removePhaseListener(X4OPhaseListener listener);
/**
* runPhase is called but should do nothig.
* When elementPhase is enables x4o tries to merge all element phases so

View file

@ -76,7 +76,7 @@ public class X4OPhaseLanguageInit {
}
public void runElementPhase(Element element) throws X4OPhaseException {
}
public void runPhase(X4OLanguageSession languageSession) throws X4OPhaseException {
public void runPhase(X4OLanguageSession languageSession) throws X4OPhaseException {
logger.finest("Run init start phase");
}
};
@ -100,11 +100,12 @@ public class X4OPhaseLanguageInit {
}
public void runElementPhase(Element element) throws X4OPhaseException {
}
public void runPhase(X4OLanguageSession languageSession) throws X4OPhaseException {
public void runPhase(X4OLanguageSession languageSession) throws X4OPhaseException {
try {
//debugPhaseMessage("Loading main language: "+elementLanguage.getLanguage(),this,elementLanguage);
debugPhaseMessage(languageSession, "Load main language: " + languageSession.getLanguage().getLanguageName());
X4OLanguageLoader loader = X4OLanguageClassLoader.newInstance(X4OLanguageLoader.class, languageSession.getLanguage().getLanguageConfiguration().getDefaultLanguageLoader());
loader.loadLanguage((X4OLanguageLocal)languageSession.getLanguage(),languageSession.getLanguage().getLanguageName(),languageSession.getLanguage().getLanguageVersion());
X4OLanguageLocal language = (X4OLanguageLocal)languageSession.getLanguage();
loader.loadLanguage(languageSession, language, language.getLanguageName(), language.getLanguageVersion());
if (languageSession.hasX4ODebugWriter()) {
languageSession.getX4ODebugWriter().debugElementLanguageModules(languageSession);
@ -142,9 +143,10 @@ public class X4OPhaseLanguageInit {
try {
if (siblingLoaders.isEmpty()==false) {
X4OLanguageLoader loader = X4OLanguageClassLoader.newInstance(X4OLanguageLoader.class, languageSession.getLanguage().getLanguageConfiguration().getDefaultLanguageLoader());
X4OLanguageLocal language = (X4OLanguageLocal)languageSession.getLanguage();
for (X4OLanguageModuleLoaderSibling siblingLoader:siblingLoaders) {
//debugPhaseMessage("Loading sibling langauge loader: "+siblingLoader,this,elementLanguage);
siblingLoader.loadLanguageSibling((X4OLanguageLocal)languageSession.getLanguage(), loader);
debugPhaseMessage(languageSession, "Loading sibling langauge loader: " + siblingLoader);
siblingLoader.loadLanguageSibling(languageSession, language, loader);
}
if (languageSession.hasX4ODebugWriter()) {
languageSession.getX4ODebugWriter().debugElementLanguageModules(languageSession);
@ -175,7 +177,7 @@ public class X4OPhaseLanguageInit {
}
public void runElementPhase(Element element) throws X4OPhaseException {
}
public void runPhase(X4OLanguageSession languageSession) throws X4OPhaseException {
public void runPhase(X4OLanguageSession languageSession) throws X4OPhaseException {
logger.finest("Run init end phase");
}
};

View file

@ -32,7 +32,6 @@ import java.util.logging.Logger;
import org.x4o.xml.conv.ObjectConverterException;
import org.x4o.xml.element.Element;
import org.x4o.xml.element.ElementNamespaceAttribute;
import org.x4o.xml.io.X4ODebugWriter;
import org.x4o.xml.element.ElementAttributeValueParser;
import org.x4o.xml.element.ElementBindingHandler;
import org.x4o.xml.element.ElementClassAttribute;
@ -44,8 +43,6 @@ import org.x4o.xml.element.ElementNamespace;
import org.x4o.xml.lang.X4OLanguageModule;
import org.x4o.xml.lang.X4OLanguageSession;
import org.x4o.xml.lang.X4OLanguageClassLoader;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
/**
* Factory which can create X4OPhaseHandlers for all the predefined phases used in default x4o language parsing.
@ -113,16 +110,6 @@ public class X4OPhaseLanguageRead {
// We are done
}
private void debugPhaseMessage(String message,X4OPhase phaseHandler,X4OLanguageSession languageSession) throws X4OPhaseException {
if (languageSession.hasX4ODebugWriter()) {
try {
languageSession.getX4ODebugWriter().debugPhaseMessage(message,phaseHandler.getClass());
} catch (ElementException ee) {
throw new X4OPhaseException(phaseHandler,ee);
}
}
}
/**
* Creates the startX4OPhase which is a empty meta phase.
*/
@ -431,7 +418,7 @@ public class X4OPhaseLanguageRead {
class X4OPhaseReadRunDirty extends AbstractX4OPhase {
private X4OPhaseManager phaseManager = null;
public X4OPhaseReadRunDirty(X4OPhaseManager phaseManager) {
this.phaseManager=phaseManager;
this.phaseManager = phaseManager;
}
public X4OPhaseType getType() {
return X4OPhaseType.XML_READ;
@ -447,9 +434,9 @@ public class X4OPhaseLanguageRead {
if (dirtyElements.isEmpty()) {
return;
}
debugPhaseMessage("Dirty elements: "+dirtyElements.size(), this,element.getLanguageSession());
debugPhaseMessage(element.getLanguageSession(), "Dirty elements: " + dirtyElements.size());
X4OPhase p = phaseManager.getPhase(READ_BEGIN);
for (Element e:dirtyElements) {
for (Element e : dirtyElements) {
phaseManager.runPhasesForElement(e,getType(), p);
}
element.getLanguageSession().getDirtyElements().clear();
@ -462,7 +449,7 @@ public class X4OPhaseLanguageRead {
class X4OPhaseReadRunDirtyLast extends AbstractX4OPhase {
private X4OPhaseManager phaseManager = null;
public X4OPhaseReadRunDirtyLast(X4OPhaseManager phaseManager) {
this.phaseManager=phaseManager;
this.phaseManager = phaseManager;
}
public X4OPhaseType getType() {
return X4OPhaseType.XML_READ;
@ -478,9 +465,9 @@ public class X4OPhaseLanguageRead {
if (dirtyElements.isEmpty()) {
return;
}
debugPhaseMessage("Dirty elements last: "+dirtyElements.size(), this,element.getLanguageSession());
debugPhaseMessage(element.getLanguageSession(), "Dirty elements last: " + dirtyElements.size());
X4OPhase p = phaseManager.getPhase(READ_BEGIN);
for (Element e:dirtyElements) {
for (Element e : dirtyElements) {
phaseManager.runPhasesForElement(e,getType(), p);
}
element.getLanguageSession().getDirtyElements().clear();
@ -644,7 +631,6 @@ public class X4OPhaseLanguageRead {
// print the properties and classes for this language/config
if (languageSession.hasX4ODebugWriter()) {
try {
//languageSession.getX4ODebugWriter().debugLanguageProperties(languageSession);
languageSession.getX4ODebugWriter().debugLanguageDefaultClasses(languageSession);
} catch (ElementException e) {
throw new X4OPhaseException(this,e);
@ -659,18 +645,13 @@ public class X4OPhaseLanguageRead {
*/
public X4OPhase releasePhase() {
// for debug output
// for debug output TODO: redo release counters
/*
class ReleasePhaseListener implements X4OPhaseListener {
private int elementsReleased = 0;
/**
* @see org.x4o.xml.lang.phase.X4OPhaseListener#preRunPhase(org.x4o.xml.lang.phase.X4OPhase, org.x4o.xml.lang.X4OLanguageSession)
*/
public void preRunPhase(X4OPhase phase,X4OLanguageSession languageSession) throws X4OPhaseException {
elementsReleased=0;
}
/**
* @see org.x4o.xml.lang.phase.X4OPhaseListener#endRunPhase(org.x4o.xml.lang.phase.X4OPhase, org.x4o.xml.lang.X4OLanguageSession)
*/
public void endRunPhase(X4OPhase phase,X4OLanguageSession languageSession) throws X4OPhaseException {
if (languageSession.hasX4ODebugWriter()==false) {
return;
@ -689,8 +670,9 @@ public class X4OPhaseLanguageRead {
elementsReleased++;
}
}
*/
final ReleasePhaseListener releaseCounter = new ReleasePhaseListener();
//final ReleasePhaseListener releaseCounter = new ReleasePhaseListener();
X4OPhase result = new AbstractX4OPhase() {
public X4OPhaseType getType() {
return X4OPhaseType.XML_READ;
@ -708,12 +690,12 @@ public class X4OPhaseLanguageRead {
element.release();
} catch (ElementException e) {
throw new X4OPhaseException(this,e);
} finally {
releaseCounter.addReleasedElement();
}
}// finally {
// releaseCounter.addReleasedElement();
//}
}
};
result.addPhaseListener(releaseCounter);
//result.addPhaseListener(releaseCounter);
return result;
}
}

View file

@ -38,7 +38,6 @@ import org.x4o.xml.X4ODriverManager;
import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.task.X4OLanguageTask;
import org.x4o.xml.lang.task.X4OLanguageTaskException;
import org.x4o.xml.lang.task.X4OLanguageTaskExecutor;
/**
* X4OLanguageTaskCommandLine runs a language task from the command line.
@ -87,9 +86,8 @@ public class X4OTaskCommandLine {
}
private void executeLanguageTask() throws X4OLanguageTaskException {
X4OLanguageTaskExecutor taskExecutor = task.createTaskExecutor(config);
X4OLanguage language = driver.createLanguage();
taskExecutor.execute(language);
X4OLanguage language = driver.createLanguage(); // TODO: also add support for version select
X4OTaskRunner.runTaskLanguage(task, language, config);
}
private void systemErrExit(String message) {

View file

@ -27,38 +27,53 @@ import java.util.List;
import org.x4o.sax3.io.SAX3PropertyConfig;
import org.x4o.xml.X4ODriver;
import org.x4o.xml.X4ODriverManager;
import org.x4o.xml.io.X4OConnectionException;
import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.X4OLanguageLocal;
import org.x4o.xml.lang.X4OLanguageSession;
import org.x4o.xml.lang.phase.X4OPhaseException;
import org.x4o.xml.lang.task.X4OLanguageTask;
import org.x4o.xml.lang.task.X4OLanguageTaskException;
import org.x4o.xml.lang.task.X4OLanguageTaskExecutor;
/**
* X4OTaskRunner finds all x4o objects and configs and then run the x4o langauge task.
* X4OTaskRunner finds all x4o objects and configs and then run the x4o langauge
* task.
*
* @author Willem Cazander
* @version 1.0 Aug 30, 2013
*/
public final class X4OTaskRunner {
static public void runTask(String languageName,String languageVersion,String taskId,List<X4OTaskProperty> props) throws X4OLanguageTaskException {
static public void runTask(String languageName, String languageVersion, String taskId, List<X4OTaskProperty> props) throws X4OLanguageTaskException {
X4ODriver<?> driver = X4ODriverManager.getX4ODriver(languageName);
X4OLanguageTask task = driver.getLanguageTask(taskId);
if (task==null) {
throw new NullPointerException("Could not find x4o task with id; "+taskId);
if (task == null) {
throw new NullPointerException("Could not find x4o task with id; " + taskId);
}
SAX3PropertyConfig config = task.createTaskConfig();
for (X4OTaskProperty prop:props) {
for (X4OTaskProperty prop : props) {
String key = prop.getKey();
String value = prop.getValue();
config.setPropertyParsedValue(key, value);
}
X4OLanguageTaskExecutor taskExecutor = task.createTaskExecutor(config);
X4OLanguage language = null;
if (languageVersion==null) {
if (languageVersion == null) {
language = driver.createLanguage();
} else {
language = driver.createLanguage(languageVersion);
}
runTaskLanguage(task, language, config);
}
static public void runTaskLanguage(X4OLanguageTask task, X4OLanguage language, SAX3PropertyConfig config) throws X4OLanguageTaskException {
X4OLanguageTaskExecutor taskExecutor = task.createTaskExecutor(config);
// Run init to load the language, which is 'normally' in read or write for deep recursief debugging support.
try (X4OLanguageSession session = language.createLanguageSession()) {
X4OLanguageLocal.class.cast(language).init(session);
} catch (X4OConnectionException | X4OPhaseException e) {
throw new X4OLanguageTaskException(config, e.getMessage(), e);
}
taskExecutor.execute(language);
}
}

View file

@ -26,6 +26,10 @@ import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.X4OLanguageLocal;
import org.x4o.xml.lang.X4OLanguageSession;
import org.x4o.xml.lang.X4OLanguageSessionLocal;
/**
* X4ODriverManager
@ -86,12 +90,13 @@ public class X4ODriverManagerTest {
@Test
public void testLanguageVersionNonExcisting() throws Exception {
String language = "test";
String version = "99.9";
String languageTest = "test";
String versionTest = "99.9";
Throwable e = null;
try {
X4ODriver<?> driver = X4ODriverManager.getX4ODriver(language);
driver.createLanguage(version).createLanguageSession();
X4ODriver<?> driver = X4ODriverManager.getX4ODriver(languageTest);
X4OLanguage language = driver.createLanguage(versionTest);
X4OLanguageLocal.class.cast(language).init(language.createLanguageSession());
} catch (Throwable catchE) {
e = catchE;
}

View file

@ -28,13 +28,18 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.Charset;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.x4o.sax3.SAX3WriterXml;
import org.x4o.sax3.io.ContentCloseable;
import org.x4o.sax3.io.ContentWriter;
import org.x4o.xml.X4ODriver;
import org.x4o.xml.io.DefaultX4OReader;
import org.x4o.xml.io.DefaultX4OWriter;
import org.x4o.xml.io.X4ODebugWriter;
import org.x4o.xml.io.X4OReader;
import org.x4o.xml.io.X4OWriter;
import org.x4o.xml.test.TestDriver;
@ -77,8 +82,13 @@ public class X4ODebugWriterTest {
File debugFile = createDebugFile();
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
reader.setProperty(DefaultX4OReader.DEBUG_OUTPUT_STREAM, new FileOutputStream(debugFile));
reader.readResource("tests/attributes/test-bean.xml");
try (ContentWriter debugHandler = new SAX3WriterXml(new FileOutputStream(debugFile))) {
try (ContentCloseable docClosure = debugHandler.startDocumentClosure()) {
debugHandler.startPrefixMapping(X4ODebugWriter.DEBUG_URI_NS, X4ODebugWriter.DEBUG_URI);
reader.setProperty(DefaultX4OReader.DEBUG_OUTPUT_HANDLER, debugHandler);
reader.readResource("tests/attributes/test-bean.xml");
}
}
Assertions.assertTrue(debugFile.exists(), "Debug file does not exists.");
String debug = readFile(debugFile);
@ -86,8 +96,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();
}
@ -100,8 +110,10 @@ public class X4ODebugWriterTest {
X4OWriter<TestObjectRoot> writer = driver.createWriter();
TestObjectRoot object = reader.readResource("tests/attributes/test-bean.xml");
writer.setProperty(DefaultX4OWriter.DEBUG_OUTPUT_STREAM, new FileOutputStream(debugFile));
writer.writeFile(object, writeFile);
try (OutputStream out = new FileOutputStream(debugFile)) {
writer.setProperty(DefaultX4OWriter.DEBUG_OUTPUT_STREAM, out);
writer.writeFile(object, writeFile);
}
Assertions.assertTrue(debugFile.exists(), "Debug file does not exists.");
String debug = readFile(debugFile);

View file

@ -31,6 +31,8 @@ import org.x4o.xml.X4ODriver;
import org.x4o.xml.eld.CelDriver;
import org.x4o.xml.io.DefaultX4OReader;
import org.x4o.xml.io.X4OEntityResolver;
import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.X4OLanguageLocal;
import org.x4o.xml.lang.X4OLanguageSession;
import org.x4o.xml.test.TestDriver;
import org.x4o.xml.test.models.TestObjectRoot;
@ -62,14 +64,19 @@ public class X4OEntityResolverTest {
@Test
public void testResolve() throws Exception {
X4ODriver<?> driver = new CelDriver();
X4OEntityResolver resolver = new X4OEntityResolver(driver.createLanguage().createLanguageSession(),DefaultX4OReader.DEFAULT_PROPERTY_CONFIG);
InputSource input = resolver.resolveEntity("","http://cel.x4o.org/xml/ns/cel-root-1.0.xsd");
Assertions.assertNotNull(input);
X4OLanguage language = driver.createLanguage();
try (X4OLanguageSession session = language.createLanguageSession()) {
X4OLanguageLocal.class.cast(language).init(session);
X4OEntityResolver resolver = new X4OEntityResolver(session, DefaultX4OReader.DEFAULT_PROPERTY_CONFIG);
InputSource input = resolver.resolveEntity("","http://cel.x4o.org/xml/ns/cel-root-1.0.xsd");
Assertions.assertNotNull(input);
}
}
@Test
public void testResolveMissing() throws Exception {
X4ODriver<TestObjectRoot> driver = new TestDriver();
// NOTE: this session is not inited, see fix code above.
X4OEntityResolver resolver = new X4OEntityResolver(driver.createLanguage().createLanguageSession(),DefaultX4OReader.DEFAULT_PROPERTY_CONFIG);
Exception e = null;
try {

View file

@ -32,6 +32,7 @@ import org.x4o.xml.X4ODriverManager;
import org.x4o.xml.eld.CelDriver;
import org.x4o.xml.eld.EldDriver;
import org.x4o.xml.lang.task.X4OLanguageTask;
import org.x4o.xml.lang.task.run.X4OTaskRunner;
import org.x4o.xml.test.TestDriver;
/**
@ -56,7 +57,7 @@ public class X4OWriteLanguageDocExecutorTest {
SAX3PropertyConfig config = task.createTaskConfig();
File outputPath = createOutputPath(outputPostfix);
config.setProperty(EldDocWriter.OUTPUT_PATH,outputPath);
task.createTaskExecutor(config).execute(driver.createLanguage());
X4OTaskRunner.runTaskLanguage(task, driver.createLanguage(), config);
Assertions.assertTrue(outputPath.exists());
Assertions.assertTrue(outputPath.list()!=null);
Assertions.assertTrue(outputPath.list().length>2);

View file

@ -37,6 +37,7 @@ import org.x4o.xml.eld.EldDriver;
import org.x4o.xml.eld.EldModuleLoaderCore;
import org.x4o.xml.io.X4OWriterTest;
import org.x4o.xml.lang.task.X4OLanguageTask;
import org.x4o.xml.lang.task.run.X4OTaskRunner;
import org.x4o.xml.test.swixml.SwiXmlDriver;
/**
@ -71,7 +72,7 @@ public class EldXsdLanguageTaskTest {
config.setProperty(key, value);
}
}
task.createTaskExecutor(config).execute(driver.createLanguage());
X4OTaskRunner.runTaskLanguage(task, driver.createLanguage(), config);
Assertions.assertTrue(outputPath.exists());
Assertions.assertTrue(outputPath.list()!=null);
return outputPath;

View file

@ -42,13 +42,16 @@ import org.x4o.xml.test.models.TestObjectRoot;
public class DefaultX4OLanguageLoaderTest {
static X4ODriver<TestObjectRoot> driver;
static X4OLanguage language;
static X4OLanguageLocal language;
static X4OLanguageSession session;
static DefaultX4OLanguageLoader loader;
@BeforeAll
public static void setUp() throws Exception {
driver = TestDriver.getInstance();
language = driver.createLanguage();
language = (X4OLanguageLocal)driver.createLanguage();
session = language.createLanguageSession();
language.init(session);
loader = (DefaultX4OLanguageLoader)language.getLanguageConfiguration().getDefaultLanguageLoader().newInstance();
}
@ -56,7 +59,7 @@ public class DefaultX4OLanguageLoaderTest {
public void testLoadingDuplicate() throws Exception {
Exception e = null;
try {
loader.loadLanguage((X4OLanguageLocal)language, "test", "1.0");
loader.loadLanguage(session, language, "test", "1.0");
} catch (Exception ee) {
e = ee;
}