X4O: Some java cleanup and using new sax parser factory lookup

This commit is contained in:
Willem Cazander 2025-11-06 21:27:47 +01:00
parent ec8711df9c
commit 889e4b9eb8
24 changed files with 346 additions and 365 deletions

View file

@ -33,6 +33,10 @@ import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.x4o.xml.lang.DefaultX4OLanguage;
import org.x4o.xml.lang.DefaultX4OLanguageConfiguration;
import org.x4o.xml.lang.X4OLanguage;
@ -51,10 +55,9 @@ import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.DefaultHandler2;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* X4ODriverManager controls all the x4o driver and languages loaded in the classpath.
* X4ODriverManager controls all the x4o driver and languages loaded in the class-path.
*
* @author Willem Cazander
* @version 1.0 Apr 6, 2013
@ -91,10 +94,6 @@ public final class X4ODriverManager {
languageTasks.put(task.getId(), task);
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (InstantiationException e) {
throw new IllegalStateException(e.getMessage(),e);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e.getMessage(),e);
}
}
@ -128,12 +127,7 @@ public final class X4ODriverManager {
if (version == null) {
version = driver.getLanguageVersionDefault();
}
DefaultX4OLanguage result = new DefaultX4OLanguage(
driver.buildLanguageConfiguration(),
driver.buildPhaseManager(),
driver.getLanguageName(),
version
);
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) {
@ -149,25 +143,6 @@ public final class X4ODriverManager {
return result;
}
/*
static public void setGlobalProperty(X4ODriver<?> driver,String key,Object value) {
Map<String,Object> driverProperties = instance.globalProperties.get(driver.getLanguageName());
if (driverProperties==null) {
driverProperties = new HashMap<String,Object>(20);
instance.globalProperties.put(driver.getLanguageName(), driverProperties);
}
String keyLimits[] = driver.getGlobalPropertyKeySet();
for (int i=0;i<keyLimits.length;i++) {
String keyLimit = keyLimits[i];
if (keyLimit.equals(key)) {
driverProperties.put(key,value);
return;
}
}
throw new IllegalArgumentException("Property with key: "+key+" is protected by key limit.");
}
*/
static public void registerX4ODriver(X4ODriver<?> driver) {
if (driver == null) {
throw new NullPointerException("Can't register null driver.");
@ -246,25 +221,19 @@ public final class X4ODriverManager {
Class<?> driverClass = X4OLanguageClassLoader.loadClass(driverClassName);
// Constructor<?> markedConstructor = driverClass.getDeclaredConstructor(new Class[]{ConstructorMarker.class});
// X4ODriver<?> driver = (X4ODriver<?>)markedConstructor.newInstance(new Object[]{new ConstructorMarker()});
X4ODriver<?> driver = (X4ODriver<?>)driverClass.newInstance();
X4ODriver<?> driver = (X4ODriver<?>) X4OLanguageClassLoader.newInstance(driverClass);
registerX4ODriver(driver);
return driver;
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (InstantiationException e) {
throw new IllegalStateException(e.getMessage(),e);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e.getMessage(),e);
}
}
/*
* Class to make sure all driver instances are created by this manager instance.
final public class ConstructorMarker {
private ConstructorMarker() {
}
}*/
* TODO: Class to make sure all driver instances are created by this manager instance.
*
* final public class ConstructorMarker { private ConstructorMarker() { } }
*/
/**
* Loads all defined language drivers in classpath.
@ -289,21 +258,26 @@ public final class X4ODriverManager {
/**
* Parser xml inputstream and add into drivers and defaultDrivers lists.
*
* @param in The inputstream to parser.
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
private void loadDriversXml(InputStream in) throws IOException, SAXException {
private void loadDriversXml(InputStream in) throws IOException, SAXException, ParserConfigurationException {
if (in == null) {
throw new NullPointerException("Can't parse null input stream");
}
DriversTagHandler xth = new DriversTagHandler();
XMLReader saxParser = XMLReaderFactory.createXMLReader();
saxParser.setContentHandler(xth);
saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", xth);
saxParser.setProperty("http://xml.org/sax/properties/declaration-handler",xth);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(xth);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", xth);
reader.setProperty("http://xml.org/sax/properties/declaration-handler", xth);
try {
saxParser.parse(new InputSource(in));
reader.parse(new InputSource(in));
} finally {
in.close();
}

View file

@ -57,7 +57,7 @@ public class ByteConverter extends AbstractStringObjectConverter {
* @throws ObjectConverterException When conversion fails.
*/
public Object convertStringTo(String str, Locale locale) throws ObjectConverterException {
return new Byte(str);
return Byte.valueOf(str);
}
/**

View file

@ -57,7 +57,7 @@ public class CharacterConverter extends AbstractStringObjectConverter {
* @throws ObjectConverterException When conversion fails.
*/
public Object convertStringTo(String str, Locale locale) throws ObjectConverterException {
return new Character(str.charAt(0));
return Character.valueOf(str.charAt(0));
}
/**

View file

@ -57,7 +57,7 @@ public class DoubleConverter extends AbstractStringObjectConverter {
* @throws ObjectConverterException When conversion fails.
*/
public Object convertStringTo(String str, Locale locale) throws ObjectConverterException {
return new Double(str);
return Double.valueOf(str);
}
/**

View file

@ -57,7 +57,7 @@ public class FloatConverter extends AbstractStringObjectConverter {
* @throws ObjectConverterException When conversion fails.
*/
public Object convertStringTo(String str, Locale locale) throws ObjectConverterException {
return new Float(str);
return Float.valueOf(str);
}
/**

View file

@ -57,7 +57,7 @@ public class IntegerConverter extends AbstractStringObjectConverter {
* @throws ObjectConverterException When conversion fails.
*/
public Object convertStringTo(String str, Locale locale) throws ObjectConverterException {
return new Integer(str);
return Integer.valueOf(str);
}
/**

View file

@ -57,7 +57,7 @@ public class LongConverter extends AbstractStringObjectConverter {
* @throws ObjectConverterException When conversion fails.
*/
public Object convertStringTo(String str, Locale locale) throws ObjectConverterException {
return new Long(str);
return Long.valueOf(str);
}
/**

View file

@ -42,12 +42,12 @@ public class X4OExpressionFactory {
ExpressionFactory result = null;
try {
Class<?> expressionFactoryClass = X4OLanguageClassLoader.loadClass(EL_FACTORY_IMPL_APACHE);
result = (ExpressionFactory) expressionFactoryClass.newInstance();
} catch (Exception e) {
result = X4OLanguageClassLoader.newInstance(ExpressionFactory.class, expressionFactoryClass);
} catch (ClassNotFoundException e) {
try {
Class<?> expressionFactoryClass = X4OLanguageClassLoader.loadClass(EL_FACTORY_IMPL_ODYSSEUS);
result = (ExpressionFactory) expressionFactoryClass.newInstance();
} catch (Exception ee) {
result = X4OLanguageClassLoader.newInstance(ExpressionFactory.class, expressionFactoryClass);
} catch (ClassNotFoundException ee) {
throw new RuntimeException("Could not load ExpressionFactory tried: "+EL_FACTORY_IMPL_APACHE+" and "+EL_FACTORY_IMPL_ODYSSEUS+" but could not load one of them.");
}
}
@ -55,12 +55,10 @@ public class X4OExpressionFactory {
}
static public ELContext createELContext(Class<?> elContextClass) {
ELContext result = null;
try {
result = (ELContext)X4OLanguageClassLoader.newInstance(elContextClass);
} catch (Exception e) {
return X4OLanguageClassLoader.newInstance(ELContext.class, elContextClass);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Could not create instance of ELContext: "+e.getMessage(),e);
}
return result;
}
}

View file

@ -49,9 +49,9 @@ import org.xml.sax.SAXException;
*/
public class EldModuleLoader implements X4OLanguageModuleLoader {
private Logger logger = null;
private String eldResource = null;
private boolean isEldCore = false;
private final Logger logger;
private final String eldResource;
private final boolean isEldCore;
/** The EL key to access the parent language module. */
public static final String EL_PARENT_LANGUAGE_MODULE = "parentLanguageModule";
@ -68,7 +68,7 @@ public class EldModuleLoader implements X4OLanguageModuleLoader {
if (eldResource == null) {
throw new NullPointerException("Can't load null eld resource.");
}
logger = Logger.getLogger(EldModuleLoader.class.getName());
this.logger = Logger.getLogger(EldModuleLoader.class.getName());
this.eldResource = eldResource;
this.isEldCore = isEldCore;
}
@ -97,9 +97,7 @@ public class EldModuleLoader implements X4OLanguageModuleLoader {
reader.addELBeanInstance(EL_PARENT_LANGUAGE, language);
reader.addELBeanInstance(EL_PARENT_LANGUAGE_MODULE, languageModule);
//language.createLanguageSession()
reader.setProperty(DefaultX4OReader.DEBUG_OUTPUT_HANDLER, reader);
reader.setProperty(DefaultX4OReader.DEBUG_OUTPUT_HANDLER, null);
//TODO: if (language.getLanguageConfiguration().getLanguagePropertyBoolean(X4OLanguageProperty.DEBUG_OUTPUT_ELD_PARSER)) {
// eldLang.setX4ODebugWriter(elementLanguage.getLanguageConfiguration().getX4ODebugWriter());

View file

@ -210,19 +210,15 @@ public class EldModuleLoaderCore implements X4OLanguageModuleLoader {
logger.finer("Creating "+language.getLanguageName()+" namespace.");
ElementNamespace namespace;
try {
namespace = (ElementNamespace)X4OLanguageClassLoader.newInstance(language.getLanguageConfiguration().getDefaultElementNamespace());
} catch (InstantiationException e) {
throw new X4OLanguageModuleLoaderException(this,e.getMessage(),e);
} catch (IllegalAccessException e) {
namespace = X4OLanguageClassLoader.newInstance(ElementNamespace.class, language.getLanguageConfiguration().getDefaultElementNamespace());
} catch (ClassNotFoundException e) {
throw new X4OLanguageModuleLoaderException(this,e.getMessage(),e);
}
try {
namespace.setElementNamespaceInstanceProvider((ElementNamespaceInstanceProvider)
X4OLanguageClassLoader.newInstance(language.getLanguageConfiguration().getDefaultElementNamespaceInstanceProvider())
namespace.setElementNamespaceInstanceProvider(
X4OLanguageClassLoader.newInstance(ElementNamespaceInstanceProvider.class, language.getLanguageConfiguration().getDefaultElementNamespaceInstanceProvider())
);
} catch (InstantiationException e) {
throw new X4OLanguageModuleLoaderException(this,e.getMessage(),e);
} catch (IllegalAccessException e) {
} catch (ClassNotFoundException e) {
throw new X4OLanguageModuleLoaderException(this,e.getMessage(),e);
}
@ -236,15 +232,13 @@ public class EldModuleLoaderCore implements X4OLanguageModuleLoader {
private ElementClass createElementClass(X4OLanguage language,String tag,Class<?> objectClass,Class<?> elementClass,String description) throws X4OLanguageModuleLoaderException {
try {
ElementClass result = (ElementClass)X4OLanguageClassLoader.newInstance(language.getLanguageConfiguration().getDefaultElementClass());
ElementClass result = X4OLanguageClassLoader.newInstance(ElementClass.class, language.getLanguageConfiguration().getDefaultElementClass());
result.setId(tag);
result.setObjectClass(objectClass);
result.setElementClass(elementClass);
result.setDescription(description);
return result;
} catch (InstantiationException e) {
throw new X4OLanguageModuleLoaderException(this,e.getMessage(),e);
} catch (IllegalAccessException e) {
} catch (ClassNotFoundException e) {
throw new X4OLanguageModuleLoaderException(this,e.getMessage(),e);
}
}
@ -260,7 +254,7 @@ public class EldModuleLoaderCore implements X4OLanguageModuleLoader {
*/
private ElementClassAttribute createElementClassAttribute(X4OLanguage language,String name,boolean required,ObjectConverter converter) throws X4OLanguageModuleLoaderException {
try {
ElementClassAttribute result = (ElementClassAttribute)X4OLanguageClassLoader.newInstance(language.getLanguageConfiguration().getDefaultElementClassAttribute());
ElementClassAttribute result = X4OLanguageClassLoader.newInstance(ElementClassAttribute.class, language.getLanguageConfiguration().getDefaultElementClassAttribute());
result.setId(name);
if (required) {
result.setRequired(required);
@ -269,9 +263,7 @@ public class EldModuleLoaderCore implements X4OLanguageModuleLoader {
result.setObjectConverter(converter);
}
return result;
} catch (InstantiationException e) {
throw new X4OLanguageModuleLoaderException(this,e.getMessage(),e);
} catch (IllegalAccessException e) {
} catch (ClassNotFoundException e) {
throw new X4OLanguageModuleLoaderException(this,e.getMessage(),e);
}
}

View file

@ -59,7 +59,7 @@ public class BeanElement extends AbstractElement {
try {
Class<?> beanClass = X4OLanguageClassLoader.loadClass(className);
if (constructorArguments.isEmpty()) {
setElementObject(beanClass.newInstance());
setElementObject(X4OLanguageClassLoader.newInstance(beanClass));
} else {
Class<?>[] arguClass = new Class<?>[constructorArguments.size()];
constructorArguments.toArray(arguClass);

View file

@ -131,8 +131,8 @@ public class ElementModuleBindingHandler extends AbstractElementBindingHandler<
}
try {
elementNamespace.setElementNamespaceInstanceProvider((ElementNamespaceInstanceProvider)X4OLanguageClassLoader.newInstance(childElement.getLanguageSession().getLanguage().getLanguageConfiguration().getDefaultElementNamespaceInstanceProvider()));
} catch (Exception e) {
elementNamespace.setElementNamespaceInstanceProvider(X4OLanguageClassLoader.newInstance(ElementNamespaceInstanceProvider.class, childElement.getLanguageSession().getLanguage().getLanguageConfiguration().getDefaultElementNamespaceInstanceProvider()));
} catch (ClassNotFoundException e) {
throw new ElementBindingHandlerException("Error loading: "+e.getMessage(),e);
}
try {

View file

@ -132,10 +132,10 @@ public abstract class AbstractElement implements Element {
*/
public void doCharacters(String characters) throws ElementException {
try {
Element e = (Element)X4OLanguageClassLoader.newInstance(getLanguageSession().getLanguage().getLanguageConfiguration().getDefaultElementBodyCharacters());
Element e = X4OLanguageClassLoader.newInstance(Element.class, getLanguageSession().getLanguage().getLanguageConfiguration().getDefaultElementBodyCharacters());
e.setElementObject(characters);
addChild(e);
} catch (Exception exception) {
} catch (ClassNotFoundException exception) {
throw new ElementException(exception);
}
}
@ -145,10 +145,10 @@ public abstract class AbstractElement implements Element {
*/
public void doComment(String comment) throws ElementException {
try {
Element e = (Element)X4OLanguageClassLoader.newInstance(getLanguageSession().getLanguage().getLanguageConfiguration().getDefaultElementBodyComment());
Element e = X4OLanguageClassLoader.newInstance(Element.class, getLanguageSession().getLanguage().getLanguageConfiguration().getDefaultElementBodyComment());
e.setElementObject(comment);
addChild(e);
} catch (Exception exception) {
} catch (ClassNotFoundException exception) {
throw new ElementException(exception);
}
}
@ -158,10 +158,10 @@ public abstract class AbstractElement implements Element {
*/
public void doIgnorableWhitespace(String space) throws ElementException {
try {
Element e = (Element)X4OLanguageClassLoader.newInstance(getLanguageSession().getLanguage().getLanguageConfiguration().getDefaultElementBodyWhitespace());
Element e = X4OLanguageClassLoader.newInstance(Element.class, getLanguageSession().getLanguage().getLanguageConfiguration().getDefaultElementBodyWhitespace());
e.setElementObject(space);
addChild(e);
} catch (Exception exception) {
} catch (ClassNotFoundException exception) {
throw new ElementException(exception);
}
}

View file

@ -86,13 +86,9 @@ public class DefaultElementNamespaceInstanceProvider implements ElementNamespace
if (elementClass.getObjectClass()!=null) {
element.setElementObject(X4OLanguageClassLoader.newInstance(elementClass.getObjectClass()));
}
} catch (InstantiationException e) {
} catch (ClassNotFoundException e) {
throw new ElementNamespaceInstanceProviderException(this,"Error while providing Element: "+e.getMessage(),e);
} catch (IllegalAccessException e) {
throw new ElementNamespaceInstanceProviderException(this,"Error while providing Element: "+e.getMessage(),e);
} /*catch (ClassNotFoundException e) {
throw new ElementNamespaceInstanceProviderException(this,"Error while providing Element: "+e.getMessage(),e);
} */
}
element.setElementClass(elementClass);
element.setLanguageSession(languageSession);
return element;

View file

@ -29,6 +29,7 @@ import java.io.OutputStream;
import org.x4o.xml.element.Element;
import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.X4OLanguageClassLoader;
import org.x4o.xml.lang.X4OLanguageSession;
import org.xml.sax.SAXException;
@ -49,10 +50,8 @@ public abstract class AbstractX4OWriter<T> extends AbstractX4OWriterSession<T> i
X4OLanguageSession context = createLanguageSession();
Element rootElement = null;
try {
rootElement = (Element)context.getLanguage().getLanguageConfiguration().getDefaultElement().newInstance();
} catch (InstantiationException e) {
throw new SAXException(e);
} catch (IllegalAccessException e) {
rootElement = X4OLanguageClassLoader.newInstance(Element.class, context.getLanguage().getLanguageConfiguration().getDefaultElement());
} catch (ClassNotFoundException e) {
throw new SAXException(e);
}
rootElement.setElementObject(object);

View file

@ -40,7 +40,6 @@ import org.x4o.sax3.io.SAX3PropertyConfig.PropertyConfigItem;
import org.x4o.xml.lang.X4OLanguage;
import org.x4o.xml.lang.X4OLanguageSession;
import org.x4o.xml.lang.X4OLanguageSessionLocal;
import org.x4o.xml.lang.phase.X4OPhaseException;
import org.x4o.xml.lang.phase.X4OPhaseType;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;

View file

@ -64,11 +64,12 @@ import org.xml.sax.helpers.AttributesImpl;
public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
private final SAX3PropertyConfig propertyConfig;
private boolean schemaUriPrint;
private String schemaUriRoot;
private final static AttributeEntryComparator ATTR_ENTRY_COMPARATOR = new AttributeEntryComparator();
private final static String PROPERTY_CONTEXT_PREFIX = SAX3PropertyConfig.X4O_PROPERTIES_PREFIX+"writer/x4o/";
public final static SAX3PropertyConfig DEFAULT_PROPERTY_CONFIG;
public final static String OUTPUT_STREAM = PROPERTY_CONTEXT_PREFIX+"output/stream";
public final static String OUTPUT_XDBX = PROPERTY_CONTEXT_PREFIX+"output/xdbx";
public final static String SCHEMA_PRINT = PROPERTY_CONTEXT_PREFIX+"schema/print";
@ -120,18 +121,15 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
runWrite(languageSession);
}
private AttributeEntryComparator attributeEntryComparator = new AttributeEntryComparator();
private boolean schemaUriPrint;
private String schemaUriRoot;
private void runWrite(X4OLanguageSession languageSession) throws X4OConnectionException {
OutputStream out = (OutputStream)getProperty(OUTPUT_STREAM);
try {
String encoding = getPropertyConfig().getPropertyString(SAX3WriterXml.OUTPUT_ENCODING);
schemaUriPrint = getPropertyConfig().getPropertyBoolean(SCHEMA_PRINT);
schemaUriRoot = getPropertyConfig().getPropertyString(SCHEMA_ROOT_URI);
if (encoding==null) { encoding = SAX3XMLConstants.XML_DEFAULT_ENCODING; }
if (encoding == null) {
encoding = SAX3XMLConstants.XML_DEFAULT_ENCODING;
}
Element root = languageSession.getRootElement();
if (schemaUriRoot == null) {
String rootUri = findElementUri(root);
@ -153,7 +151,7 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
}
writer.startDocument();
Map<String,String> prefixes = new HashMap<String,String>();
Map<String, String> prefixes = new HashMap<>();
startPrefixTree(root,prefixes);
for (String uri : prefixes.keySet()) {
String prefix = prefixes.get(uri);
@ -208,20 +206,30 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
}
String name = m.getName().substring(3, 4).toLowerCase() + m.getName().substring(4);
result.add(name);
}
return result;
}
class AttributeEntry {
String id;
String value;
Integer writeOrder;
static final class AttributeEntry {
final String id;
final String value;
final Integer writeOrder;
public AttributeEntry(String id, String value, Integer writeOrder) {
this.id = id;
this.value = value;
this.writeOrder = writeOrder;
}
class AttributeEntryComparator implements Comparator<AttributeEntry> {
}
static final class AttributeEntryComparator implements Comparator<AttributeEntry> {
@Override
public int compare(AttributeEntry o1, AttributeEntry o2) {
return o1.writeOrder.compareTo(o2.writeOrder);
}
}
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) {
@ -229,15 +237,11 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
if (eca.getRunBeanValue() != null && eca.getRunBeanValue() == false) {
continue;
}
Object value = element.getLanguageSession().getElementObjectPropertyValue().getProperty(element.getElementObject(), eca.getId());
if (value == null) {
continue;
}
AttributeEntry e = new AttributeEntry();
e.id = eca.getId();
e.value = ""+value;
e.writeOrder = calcOrderNumber(e.id,eca.getWriteOrder());
AttributeEntry e = new AttributeEntry(eca.getId(), ""+value, calcOrderNumber(eca.getId(),eca.getWriteOrder()));
attr.add(e);
}
@ -274,10 +278,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();
e.id = p;
e.value = ""+value;
e.writeOrder = calcOrderNumber(e.id,writeOrder);
AttributeEntry e = new AttributeEntry(p, ""+value, calcOrderNumber(p,writeOrder));
attr.add(e);
}
}
@ -291,7 +292,7 @@ public class DefaultX4OWriter<T> extends AbstractX4OWriter<T> {
}
// Sort attributes in natural order of localName and add to attributes
Collections.sort(attr, attributeEntryComparator);
Collections.sort(attr, ATTR_ENTRY_COMPARATOR);
for (int i = 0; i < attr.size(); i++) {
AttributeEntry a = attr.get(i);
atts.addAttribute ("", a.id, "", "", a.value);

View file

@ -29,7 +29,6 @@ import org.x4o.xml.lang.X4OLanguageSession;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import java.io.IOException;
import java.io.InputStream;
@ -38,6 +37,10 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
* X4OContentParser Runs the SAX parser with the X4OContentHandler.
*
@ -56,7 +59,7 @@ public class X4OContentParser {
return propertyConfig;
}
public void parse(X4OLanguageSession languageSession) throws SAXException, IOException {
public void parse(X4OLanguageSession languageSession) throws SAXException, IOException, ParserConfigurationException {
// Group debug config property messages
if (languageSession.hasX4ODebugWriter()) {
languageSession.getX4ODebugWriter().debugSAXConfigStart();
@ -70,7 +73,7 @@ public class X4OContentParser {
}
}
private void parseSax(X4OLanguageSession languageSession) throws SAXException, IOException {
private void parseSax(X4OLanguageSession languageSession) throws SAXException, IOException, ParserConfigurationException {
// If xsd caching is needed this should be the way
//XMLParserConfiguration config = new XIncludeAwareParserConfiguration();
//config.setProperty("http://apache.org/xml/properties/internal/grammar-pool",myFullGrammarPool);
@ -78,19 +81,22 @@ public class X4OContentParser {
// Create Sax parser with x4o tag handler
X4OContentHandler xth = new X4OContentHandler(languageSession,getPropertyConfig());
XMLReader saxParser = XMLReaderFactory.createXMLReader();
saxParser.setErrorHandler(new X4OErrorHandler(languageSession,getPropertyConfig()));
saxParser.setEntityResolver(new X4OEntityResolver(languageSession,getPropertyConfig()));
saxParser.setContentHandler(xth);
saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", xth);
saxParser.setProperty("http://xml.org/sax/properties/declaration-handler",xth);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setErrorHandler(new X4OErrorHandler(languageSession,getPropertyConfig()));
reader.setEntityResolver(new X4OEntityResolver(languageSession,getPropertyConfig()));
reader.setContentHandler(xth);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", xth);
reader.setProperty("http://xml.org/sax/properties/declaration-handler",xth);
// Set properties and optional
Map<String,Object> saxParserProperties = getSAXParserProperties(languageSession);
for (Map.Entry<String,Object> entry:saxParserProperties.entrySet()) {
String name = entry.getKey();
Object value= entry.getValue();
saxParser.setProperty(name, value);
reader.setProperty(name, value);
debugMessage("property",name,value,languageSession);
}
Map<String,Object> saxParserPropertiesOptional = getSAXParserPropertiesOptional(languageSession);
@ -98,7 +104,7 @@ public class X4OContentParser {
String name = entry.getKey();
Object value= entry.getValue();
try {
saxParser.setProperty(name, value);
reader.setProperty(name, value);
debugMessage("optional-property",name,value,languageSession);
} catch (SAXException e) {
debugMessageLog("Could not set optional SAX property: "+name+" to: "+value+" error: "+e.getMessage(),languageSession);
@ -109,14 +115,14 @@ public class X4OContentParser {
Map<String, Boolean> features = getSAXParserFeatures(languageSession);
for (String key:features.keySet()) {
Boolean value=features.get(key);
saxParser.setFeature(key, value);
reader.setFeature(key, value);
debugMessage("feature",key,value,languageSession);
}
Map<String, Boolean> featuresOptional = getSAXParserFeaturesOptional(languageSession);
for (String key:featuresOptional.keySet()) {
Boolean value=featuresOptional.get(key);
try {
saxParser.setFeature(key, value);
reader.setFeature(key, value);
debugMessage("optional-feature",key,value,languageSession);
} catch (SAXException e) {
debugMessageLog("Could not set optional SAX feature: "+key+" to: "+value+" error: "+e.getMessage(),languageSession);
@ -126,7 +132,7 @@ public class X4OContentParser {
// check for required features
List<String> requiredFeatures = getSAXParserFeaturesRequired(languageSession);
for (String requiredFeature:requiredFeatures) {
if (saxParser.getFeature(requiredFeature)==false) {
if (reader.getFeature(requiredFeature)==false) {
throw new IllegalStateException("Missing required feature: "+requiredFeature);
}
debugMessage("required",requiredFeature,"true",languageSession);
@ -153,7 +159,7 @@ public class X4OContentParser {
}
try {
saxParser.parse(input);
reader.parse(input);
} finally {
if (inputStream!=null) {
inputStream.close();

View file

@ -176,7 +176,7 @@ public class X4ODebugWriter {
StringBuilder buf = new StringBuilder();
buf.append("0x");
for (char c:newline.toCharArray()) {
Integer i = new Integer(c);
Integer i = Integer.valueOf(c);
if (i<16) {
buf.append('0');
}

View file

@ -48,13 +48,13 @@ import org.x4o.xml.lang.phase.X4OPhaseManager;
*/
public class DefaultX4OLanguage implements X4OLanguageLocal {
private Logger logger = null;
private X4OLanguageConfiguration languageConfiguration = null;
private List<X4OLanguageModule> elementLanguageModules = null;
private String languageName = null;
private String languageVersion = null;
private X4OPhaseManager phaseManager = null;
private Map<String,ElementNamespace> keyedNamespaceLookup = null;
private final Logger logger;
private final X4OLanguageConfiguration languageConfiguration;
private final List<X4OLanguageModule> elementLanguageModules;
private final String languageName;
private final String languageVersion;
private final X4OPhaseManager phaseManager;
private final Map<String, ElementNamespace> keyedNamespaceLookup;
public DefaultX4OLanguage(X4OLanguageConfiguration languageConfiguration, X4OPhaseManager phaseManager, String languageName, String languageVersion) {
if (languageName == null) {
@ -63,9 +63,9 @@ public class DefaultX4OLanguage implements X4OLanguageLocal {
if (languageVersion == null) {
throw new NullPointerException("Can't define myself with null version.");
}
logger = Logger.getLogger(DefaultX4OLanguage.class.getName());
elementLanguageModules = new ArrayList<X4OLanguageModule>(20);
keyedNamespaceLookup = new HashMap<String,ElementNamespace>(20);
this.logger = Logger.getLogger(DefaultX4OLanguage.class.getName());
this.elementLanguageModules = new ArrayList<X4OLanguageModule>(20);
this.keyedNamespaceLookup = new HashMap<String, ElementNamespace>(20);
this.languageConfiguration = languageConfiguration;
this.languageName = languageName;
this.languageVersion = languageVersion;
@ -113,7 +113,6 @@ public class DefaultX4OLanguage implements X4OLanguageLocal {
}
}
elementLanguageModules.add(elementLanguageModule);
for (ElementNamespace ns : elementLanguageModule.getElementNamespaces()) {
keyedNamespaceLookup.put(ns.getUri(), ns);
}
@ -140,15 +139,18 @@ 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((ElementAttributeValueParser)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultElementAttributeValueParser()));
languageSession.setElementAttributeValueParser(X4OLanguageClassLoader.newInstance(ElementAttributeValueParser.class,
getLanguageConfiguration().getDefaultElementAttributeValueParser()));
}
if (languageSession.getElementObjectPropertyValue() == null) {
languageSession.setElementObjectPropertyValue((ElementObjectPropertyValue)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultElementObjectPropertyValue()));
languageSession.setElementObjectPropertyValue(X4OLanguageClassLoader.newInstance(ElementObjectPropertyValue.class,
getLanguageConfiguration().getDefaultElementObjectPropertyValue()));
}
} catch (Exception e) {
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
}
return languageSession;
@ -198,7 +200,8 @@ public class DefaultX4OLanguage implements X4OLanguageLocal {
return result;
}
private void findElementBindingHandlerInList(Object parent,Object child,List<ElementBindingHandler> result,List<ElementBindingHandler> checkList,boolean checkChild) {
private void findElementBindingHandlerInList(Object parent, Object child, List<ElementBindingHandler> result, List<ElementBindingHandler> checkList,
boolean checkChild) {
for (ElementBindingHandler binding : checkList) {
boolean parentBind = false;
if (parent instanceof Class) {

View file

@ -31,6 +31,10 @@ import java.util.Enumeration;
import java.util.List;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.x4o.xml.eld.EldDriver;
import org.x4o.xml.eld.EldModuleLoader;
import org.x4o.xml.lang.phase.X4OPhaseLanguageInit;
@ -40,7 +44,6 @@ import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.DefaultHandler2;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* DefaultX4OLanguageLoader loads the modules of language.
@ -101,7 +104,7 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
}
for (String value:versionedResources.moduleLoaders) {
try {
loader = (X4OLanguageModuleLoader)X4OLanguageClassLoader.loadClass(value).newInstance();
loader = (X4OLanguageModuleLoader)X4OLanguageClassLoader.newInstance(value);
} catch (Exception ee) {
throw new X4OLanguageLoaderException("Could not load class: "+value+" error: "+ee.getMessage(),ee);
}
@ -109,7 +112,7 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
}
for (String value:versionedResources.siblingLoaders) {
try {
loader = (X4OLanguageModuleLoader)X4OLanguageClassLoader.loadClass(value).newInstance();
loader = (X4OLanguageModuleLoader)X4OLanguageClassLoader.newInstance(value);
} catch (Exception ee) {
throw new X4OLanguageLoaderException("Could not load class: "+value+" error: "+ee.getMessage(),ee);
}
@ -135,10 +138,8 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
List<VersionedResources> result = new ArrayList<VersionedResources>(resources.size());
X4OLanguageVersionFilter lvf;
try {
lvf = (X4OLanguageVersionFilter)X4OLanguageClassLoader.newInstance(languageLocal.getLanguageConfiguration().getDefaultLanguageVersionFilter());
} catch (InstantiationException e) {
throw new X4OLanguageLoaderException(e);
} catch (IllegalAccessException e) {
lvf = X4OLanguageClassLoader.newInstance(X4OLanguageVersionFilter.class, languageLocal.getLanguageConfiguration().getDefaultLanguageVersionFilter());
} catch (ClassNotFoundException e) {
throw new X4OLanguageLoaderException(e);
}
for (VersionedResources versionedResources:resources) {
@ -178,10 +179,8 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
private void loadModule(X4OLanguageLocal languageLocal,X4OLanguageModuleLoader loader,String resource,VersionedResources versionedResources) throws X4OLanguageLoaderException {
X4OLanguageModuleLocal module;
try {
module = (X4OLanguageModuleLocal)X4OLanguageClassLoader.newInstance(languageLocal.getLanguageConfiguration().getDefaultElementLanguageModule());
} catch (InstantiationException e) {
throw new X4OLanguageLoaderException(e);
} catch (IllegalAccessException e) {
module = X4OLanguageClassLoader.newInstance(X4OLanguageModuleLocal.class, languageLocal.getLanguageConfiguration().getDefaultElementLanguageModule());
} catch (ClassNotFoundException e) {
throw new X4OLanguageLoaderException(e);
}
logMessage(languageLocal,"Created module: "+module);
@ -236,9 +235,7 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
result.addAll(loadLanguageModulesXml(u.openStream(),u.toString()));
}
return result;
} catch (IOException e) {
throw new X4OLanguageLoaderException(e);
} catch (SAXException e) {
} catch (IOException | SAXException | ParserConfigurationException e) {
throw new X4OLanguageLoaderException(e);
}
}
@ -248,18 +245,22 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
* @param in The inputstream to parser.
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
protected List<VersionedResources> loadLanguageModulesXml(InputStream in,String loadedFrom) throws IOException, SAXException {
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);
XMLReader saxParser = XMLReaderFactory.createXMLReader();
saxParser.setContentHandler(xth);
saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", xth);
saxParser.setProperty("http://xml.org/sax/properties/declaration-handler",xth);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(xth);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", xth);
reader.setProperty("http://xml.org/sax/properties/declaration-handler",xth);
try {
saxParser.parse(new InputSource(in));
reader.parse(new InputSource(in));
return xth.getResult();
} finally {
in.close();

View file

@ -23,6 +23,7 @@
package org.x4o.xml.lang;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
/**
@ -65,24 +66,37 @@ public final class X4OLanguageClassLoader {
/**
* Creates new instance of clazz.
* @param resultType The result type to cast to..
* @param clazz The class to make object from.
* @return The object of the clazz.
* @throws InstantiationException When className has no default constructor.
* @throws IllegalAccessException When class loading has security error.
* @throws ClassNotFoundException On any construct access or invoke error.
*/
public static Object newInstance(Class<?> clazz) throws InstantiationException, IllegalAccessException {
return clazz.newInstance();
@SuppressWarnings("unchecked")
public static <T> T newInstance(Class<T> resultType, Class<?> clazz) throws ClassNotFoundException {
return (T) newInstance(clazz);
}
/**
* Creates new instance of clazz.
* @param clazz The class to make object from.
* @return The object of the clazz.
* @throws ClassNotFoundException On any construct access or invoke error.
*/
public static Object newInstance(Class<?> clazz) throws ClassNotFoundException {
try {
return clazz.getConstructor().newInstance();
} catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException e) {
throw new ClassNotFoundException(e.getMessage(), e);
}
}
/**
* Creates new instance of className.
* @param className The className to create object from.
* @return The object of the className.
* @throws ClassNotFoundException When className is not found.
* @throws InstantiationException When className has no default constructor.
* @throws IllegalAccessException When class loading has security error.
* @throws ClassNotFoundException When className is not found or can't be created.
*/
public static Object newInstance(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
public static Object newInstance(String className) throws ClassNotFoundException {
return newInstance(loadClass(className));
}

View file

@ -103,7 +103,7 @@ public class X4OPhaseLanguageInit {
public void runPhase(X4OLanguageSession languageSession) throws X4OPhaseException {
try {
//debugPhaseMessage("Loading main language: "+elementLanguage.getLanguage(),this,elementLanguage);
X4OLanguageLoader loader = (X4OLanguageLoader)X4OLanguageClassLoader.newInstance(languageSession.getLanguage().getLanguageConfiguration().getDefaultLanguageLoader());
X4OLanguageLoader loader = X4OLanguageClassLoader.newInstance(X4OLanguageLoader.class, languageSession.getLanguage().getLanguageConfiguration().getDefaultLanguageLoader());
loader.loadLanguage((X4OLanguageLocal)languageSession.getLanguage(),languageSession.getLanguage().getLanguageName(),languageSession.getLanguage().getLanguageVersion());
if (languageSession.hasX4ODebugWriter()) {
@ -141,7 +141,7 @@ public class X4OPhaseLanguageInit {
public void runPhase(X4OLanguageSession languageSession) throws X4OPhaseException {
try {
if (siblingLoaders.isEmpty()==false) {
X4OLanguageLoader loader = (X4OLanguageLoader)X4OLanguageClassLoader.newInstance(languageSession.getLanguage().getLanguageConfiguration().getDefaultLanguageLoader());
X4OLanguageLoader loader = X4OLanguageClassLoader.newInstance(X4OLanguageLoader.class, languageSession.getLanguage().getLanguageConfiguration().getDefaultLanguageLoader());
for (X4OLanguageModuleLoaderSibling siblingLoader:siblingLoaders) {
//debugPhaseMessage("Loading sibling langauge loader: "+siblingLoader,this,elementLanguage);
siblingLoader.loadLanguageSibling((X4OLanguageLocal)languageSession.getLanguage(), loader);

View file

@ -277,7 +277,7 @@ public class X4OPhaseLanguageRead {
if (elementNamespaceAttributeComparator==null) {
try {
elementNamespaceAttributeComparator = (Comparator<ElementNamespaceAttribute>)X4OLanguageClassLoader.newInstance(element.getLanguageSession().getLanguage().getLanguageConfiguration().getDefaultElementNamespaceAttributeComparator());
} catch (Exception e) {
} catch (ClassNotFoundException e) {
throw new X4OPhaseException(this,e);
}
}