Changed meta package and added junit tests

This commit is contained in:
Willem Cazander 2013-04-15 18:11:55 +02:00
parent 897fe80357
commit 226c1f0425
35 changed files with 822 additions and 383 deletions

View file

@ -8,11 +8,10 @@
- meta add more ns and jndi ref/etc'
- redo eld/default converters config
- tag aliases
- maven plugin
- lang-el-beans.xml loading
- Add XML schema tags to ELD
- Auto generate schemas from eld
- Basic write support (for editor)
- Basic write support
- Basic editor support
- Make converter config/loading better
- Single xsd output
@ -28,7 +27,6 @@
- event listeners on all DOM tree element operations
- custom and merga'ble parse phases for speed
- make element tree jdom api compatible
- (DONE) change phase enum to generic (text) phases.
- Test if possible to use threadpool for executing phases
-- TODO for version 1.0 --
@ -36,8 +34,6 @@
## NON-CODE
- Add tutorial
- doc eld and x4o lang files
- Create xslt eld to html converter
- split into multiple maven projects
##CODE
- Add (super) tag for extending tags of other namespace

View file

@ -2,14 +2,20 @@
=== X4O versions ===
Version 0.8.6:
- Changed to X4ODriver interface.
- Added write support
- Added ant and maven plugins
Version 0.8.5:
- Made module loading system.
- Added XSD generator.
- Added eld to schema generator.
- Added eld to html generator.
- Cleaned xml uri nameing.
- Cleaned language properties.
- refactored ELD tag names.
- Made elddoc ~working.
- Changed phase enum to text phases.
Version 0.8.2:
- Changed packages to org.x4o

View file

@ -50,10 +50,9 @@ public abstract class X4ODriver<T> {
public final static String DEFAULT_LANGUAGE_VERSION = "1.0";
/**
* Force public constructor and register the driver.
* Public constructor.
*/
public X4ODriver() {
X4ODriverManager.registerX4ODriver(this);
}
/**
@ -67,7 +66,6 @@ public abstract class X4ODriver<T> {
abstract public String[] getLanguageVersions();
protected X4OLanguage buildLanguage(String version) {
return X4ODriverManager.getDefaultBuildLanguage(this, version);
}
@ -81,7 +79,6 @@ public abstract class X4ODriver<T> {
}
public X4OSchemaWriter createSchemaWriter() {
return createSchemaWriter(getLanguageVersionDefault());
}

View file

@ -184,28 +184,8 @@ public final class X4ODriverManager {
if (instance.drivers.containsKey(language)) {
return instance.drivers.get(language);
}
try {
instance.lazyInit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
X4ODriver<?> result = null;
try {
result = instance.createX4ODriver(language);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
X4ODriver<?> result = instance.createX4ODriver(language);
if (result==null) {
throw new IllegalArgumentException("Can't find driver for language: "+language);
}
@ -213,15 +193,7 @@ public final class X4ODriverManager {
}
static public List<String> getX4OLanguages() {
try {
instance.lazyInit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<String> result = new ArrayList<String>(10);
result.addAll(instance.classdrivers.keySet());
result.addAll(instance.defaultDrivers.keySet());
@ -229,7 +201,7 @@ public final class X4ODriverManager {
return result;
}
private void lazyInit() throws IOException, SAXException {
private void lazyInit() {
if (reloadDrivers==false) {
return;
}
@ -237,39 +209,45 @@ public final class X4ODriverManager {
reloadDrivers = false;
}
private X4ODriver<?> createX4ODriver(String language) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
private X4ODriver<?> createX4ODriver(String language) {
String driverClassName = null;
if (classdrivers.containsKey(language)) {
String driverClassName = classdrivers.get(language);
Class<?> driverClass = X4OLanguageClassLoader.loadClass(driverClassName);
X4ODriver<?> driver = (X4ODriver<?>)driverClass.newInstance();
return driver;
}
if (defaultDrivers.containsKey(language)) {
String driverClassName = defaultDrivers.get(language);
Class<?> driverClass = X4OLanguageClassLoader.loadClass(driverClassName);
X4ODriver<?> driver = (X4ODriver<?>)driverClass.newInstance();
return driver;
driverClassName = classdrivers.get(language);
} else if (defaultDrivers.containsKey(language)) {
driverClassName = defaultDrivers.get(language);
}
if (driverClassName==null) {
return null;
}
try {
Class<?> driverClass = X4OLanguageClassLoader.loadClass(driverClassName);
X4ODriver<?> driver = (X4ODriver<?>)driverClass.newInstance();
registerX4ODriver(driver);
return driver;
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(),e);
}
}
/**
* Loads all defined language drivers in classpath.
*/
private void loadLanguageDrivers() throws IOException, SAXException {
private void loadLanguageDrivers() {
logger.finer("loading x4o drivers from: "+X4O_DRIVERS_RESOURCE);
try {
Enumeration<URL> e = Thread.currentThread().getContextClassLoader().getResources(X4O_DRIVERS_RESOURCE);
while(e.hasMoreElements()) {
URL u = e.nextElement();
loadDriversXml(u.openStream());
}
e = Thread.currentThread().getContextClassLoader().getResources("/"+X4O_DRIVERS_RESOURCE);
while(e.hasMoreElements()) {
URL u = e.nextElement();
loadDriversXml(u.openStream());
}
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(),e);
}
}
/**

View file

@ -147,11 +147,9 @@ public class ElementRefectionBindingHandler extends AbstractElementBindingHandle
if (childClass.isAssignableFrom(childObject.getClass())==false) {
return;
}
if (skipChilderenClassRegex!=null) {
if (childObject.getClass().getName().matches(skipChilderenClassRegex)) {
if (skipChilderenClassRegex!=null && childObject.getClass().getName().matches(skipChilderenClassRegex)) {
return; // skip
}
}
createChild(parentElement,childObject);
}

View file

@ -23,9 +23,7 @@
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;
@ -44,6 +42,7 @@ import javax.xml.parsers.ParserConfigurationException;
* @author Willem Cazander
* @version 1.0 Aug 11, 2005
*/
@SuppressWarnings("unchecked")
abstract public class AbstractX4OReader<T> extends AbstractX4OReaderContext<T> implements X4OReader<T> {
public AbstractX4OReader(X4OLanguageContext elementLanguage) {
@ -57,10 +56,8 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderContext<T> i
return X4OLanguagePropertyKeys.DEFAULT_X4O_READER_KEYS;
}
@SuppressWarnings("unchecked")
public T read(InputStream input, String systemId, URL basePath) throws ParserConfigurationException, SAXException, IOException {
X4OLanguageContext context = readContext(input, systemId, basePath);
return (T)context.getRootElement().getElementObject();
return (T)readContext(input, systemId, basePath).getRootElement().getElementObject();
}
/**
@ -75,10 +72,7 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderContext<T> i
* @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readFile(String fileName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException {
if (fileName==null) {
throw new NullPointerException("Can't convert null fileName to file object.");
}
return readFile(new File(fileName));
return (T)readFileContext(fileName).getRootElement().getElementObject();
}
/**
@ -93,24 +87,7 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderContext<T> i
* @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readFile(File file) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException {
if (file==null) {
throw new NullPointerException("Can't read null file.");
}
if (file.exists()==false) {
throw new FileNotFoundException("File does not exists; "+file);
}
if (file.canRead()==false) {
throw new IOException("File exists but can't read file: "+file);
}
URL basePath = new File(file.getAbsolutePath()).toURI().toURL();
InputStream inputStream = new FileInputStream(file);
try {
return read(inputStream,file.getAbsolutePath(),basePath);
} finally {
if(inputStream!=null) {
inputStream.close();
}
}
return (T)readFileContext(file).getRootElement().getElementObject();
}
/**
@ -125,29 +102,7 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderContext<T> i
* @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readResource(String resourceName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException {
if (resourceName==null) {
throw new NullPointerException("Can't read null resourceName from classpath.");
}
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) cl = getClass().getClassLoader(); // fallback
URL url = cl.getResource(resourceName);
if (url==null) {
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);
}
URL basePath = new URL(baseUrl);
InputStream inputStream = cl.getResourceAsStream(resourceName);
try {
return read(inputStream,url.toExternalForm(),basePath);
} finally {
if(inputStream!=null) {
inputStream.close();
}
}
return (T)readResourceContext(resourceName).getRootElement().getElementObject();
}
/**
@ -160,11 +115,7 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderContext<T> i
* @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readString(String xmlString) throws ParserConfigurationException,SAXException,IOException,NullPointerException {
if (xmlString==null) {
throw new NullPointerException("Can't read null xml string.");
}
URL basePath = new File(System.getProperty("user.dir")).toURI().toURL();
return read(new ByteArrayInputStream(xmlString.getBytes()),"inline-xml",basePath);
return (T)readStringContext(xmlString).getRootElement().getElementObject();
}
/**
@ -177,10 +128,6 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderContext<T> i
* @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readUrl(URL url) throws ParserConfigurationException,SAXException,IOException,NullPointerException {
if (url==null) {
throw new NullPointerException("Can't read null url.");
}
URL basePath = new URL(url.toExternalForm().substring(0,url.toExternalForm().length()-url.getFile().length()));
return read(url.openStream(),url.toExternalForm(),basePath);
return (T)readUrlContext(url).getRootElement().getElementObject();
}
}

View file

@ -24,7 +24,6 @@
package org.x4o.xml.io;
import org.x4o.xml.X4ODriver;
import org.x4o.xml.X4ODriverManager;
/**
* DefaultX4ODriver can be used to create language without code and type safty.
@ -44,7 +43,6 @@ public class DefaultX4ODriver<T> extends X4ODriver<T> {
public DefaultX4ODriver(String languageName,String languageVersion) {
this.languageName=languageName;
this.languageVersion=languageVersion;
X4ODriverManager.registerX4ODriver(this);
}
@Override

View file

@ -365,14 +365,5 @@ public class AttributeMap<K,V> implements Map<K,V> {
}
return false;
}
/**
* @see java.lang.Object#hashCode()
* @return The hashCode.
*/
@Override
public int hashCode() {
return super.hashCode();
}
}
}

View file

@ -67,9 +67,9 @@ public class DefaultX4OLanguageLoader implements X4OLanguageLoader {
* @param message The message to log to the debug output.
*/
private void logMessage(X4OLanguage language,String message) {
logger.finest(message);
logger.finest(message+" from: "+language.getLanguageName());
/*
if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
try {
language.getLanguageConfiguration().getX4ODebugWriter().debugPhaseMessage(message, this.getClass());
} catch (ElementException e) {

View file

@ -37,6 +37,24 @@ import junit.framework.TestCase;
*/
public class X4ODriverManagerTest extends TestCase {
public void testDefaultLanguageVersionSelect() throws Exception {
String version = X4ODriverManager.getDefaultLanguageVersion(new String[]{"1.0","2.0","3.0"});
assertNotNull(version);
assertEquals("3.0",version);
}
public void testDefaultLanguageVersionEmpty() throws Exception {
String version = X4ODriverManager.getDefaultLanguageVersion(new String[]{});
assertNotNull(version);
assertEquals(X4ODriver.DEFAULT_LANGUAGE_VERSION,version);
}
public void testDefaultLanguageVersionNull() throws Exception {
String version = X4ODriverManager.getDefaultLanguageVersion(null);
assertNotNull(version);
assertEquals(X4ODriver.DEFAULT_LANGUAGE_VERSION,version);
}
public void testLanguageNull() throws Exception {
String language = null;
Exception e = null;

View file

@ -0,0 +1,217 @@
/*
* Copyright (c) 2004-2012, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* 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;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import org.x4o.xml.X4ODriver;
import org.x4o.xml.io.X4OReader;
import org.x4o.xml.lang.X4OLanguageContext;
import org.x4o.xml.test.TestDriver;
import org.x4o.xml.test.models.TestBean;
import org.x4o.xml.test.models.TestObjectRoot;
import junit.framework.TestCase;
/**
* X4OAbstractReaderContextTest.
*
* @author Willem Cazander
* @version 1.0 Apr 15, 2013
*/
public class X4OAbstractReaderContextTest extends TestCase {
private File copyResourceToTempFile() throws IOException {
File tempFile = File.createTempFile("test-resource", ".xml");
tempFile.deleteOnExit();
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
X4OWriter<TestObjectRoot> writer = driver.createWriter();
try {
writer.writeFile(reader.readResource("tests/attributes/test-bean.xml"), tempFile);
} catch (Exception e) {
throw new IllegalStateException(e);
}
return tempFile;
}
public void testReadFileName() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
File xmlFile = copyResourceToTempFile();
X4OLanguageContext context = reader.readFileContext(xmlFile.getAbsolutePath());
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
}
public void testReadFileNameNull() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
Exception e = null;
try {
String nullFileName = null;
reader.readFileContext(nullFileName);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("fileName"));
}
public void testReadFile() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
File xmlFile = copyResourceToTempFile();
X4OLanguageContext context = reader.readFileContext(xmlFile);
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
}
public void testReadFileNull() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
Exception e = null;
try {
File nullFile = null;
reader.readFileContext(nullFile);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("file"));
}
public void testReadFileNotExists() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
Exception e = null;
try {
File tempFile = File.createTempFile("test-file", ".xml");
tempFile.delete();
reader.readFileContext(tempFile);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",FileNotFoundException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("exists"));
assertTrue("Wrong exception message",e.getMessage().contains("File"));
}
public void testReadResource() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
X4OLanguageContext context = reader.readResourceContext("tests/attributes/test-bean.xml");
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
assertNotNull(root);
}
public void testReadResourceNull() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
Exception e = null;
try {
reader.readResourceContext(null);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("resourceName"));
}
public void testReadString() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
X4OLanguageContext context = reader.readStringContext(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<root:root xmlns:root=\"http://test.x4o.org/xml/ns/test-root\" xmlns=\"http://test.x4o.org/xml/ns/test-lang\">"+
"<testBean privateIntegerTypeField=\"987654321\"/>"+
"</root:root>"
);
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
assertEquals("987654321", ""+bean.getPrivateIntegerTypeField());
}
public void testReadStringNull() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
Exception e = null;
try {
reader.readStringContext(null);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("string"));
}
public void testReadUrl() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
URL xmlUrl = Thread.currentThread().getContextClassLoader().getResource("tests/attributes/test-bean.xml");
X4OLanguageContext context = reader.readUrlContext(xmlUrl);
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
}
public void testReadUrlNull() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
Exception e = null;
try {
reader.readUrlContext(null);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("url"));
}
}

View file

@ -0,0 +1,193 @@
/*
* Copyright (c) 2004-2012, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* 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;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.x4o.xml.X4ODriver;
import org.x4o.xml.io.X4OReader;
import org.x4o.xml.test.TestDriver;
import org.x4o.xml.test.models.TestBean;
import org.x4o.xml.test.models.TestObjectRoot;
import junit.framework.TestCase;
/**
* X4OAbstractReaderTest.
*
* @author Willem Cazander
* @version 1.0 Apr 15, 2013
*/
public class X4OAbstractReaderTest extends TestCase {
private File copyResourceToTempFile() throws IOException {
File tempFile = File.createTempFile("test-resource", ".xml");
tempFile.deleteOnExit();
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
X4OWriter<TestObjectRoot> writer = driver.createWriter();
try {
writer.writeFile(reader.readResource("tests/attributes/test-bean.xml"), tempFile);
} catch (Exception e) {
throw new IllegalStateException(e);
}
return tempFile;
}
public void testReadResource() throws Exception {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
TestObjectRoot root = reader.readResource("tests/attributes/test-bean.xml");
assertNotNull(root);
}
public void testReadResourceNull() throws Exception {
Exception e = null;
try {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
reader.readResource(null);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("resourceName"));
}
public void testReadString() throws Exception {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
TestObjectRoot root = reader.readString(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<root:root xmlns:root=\"http://test.x4o.org/xml/ns/test-root\" xmlns=\"http://test.x4o.org/xml/ns/test-lang\">"+
"<testBean privateIntegerTypeField=\"987654321\"/>"+
"</root:root>"
);
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
assertEquals("987654321", ""+bean.getPrivateIntegerTypeField());
}
public void testReadStringNull() throws Exception {
Exception e = null;
try {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
reader.readString(null);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("string"));
}
public void testReadUrl() throws Exception {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
URL xmlUrl = Thread.currentThread().getContextClassLoader().getResource("tests/attributes/test-bean.xml");
TestObjectRoot root = reader.readUrl(xmlUrl);
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
}
public void testReadUrlNull() throws Exception {
Exception e = null;
try {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
reader.readUrl(null);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("url"));
}
public void testReadFileName() throws Exception {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
File xmlFile = copyResourceToTempFile();
TestObjectRoot root = reader.readFile(xmlFile.getAbsolutePath());
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
}
public void testReadFileNameNull() throws Exception {
Exception e = null;
try {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
String nullFileName = null;
reader.readFile(nullFileName);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("fileName"));
}
public void testReadFile() throws Exception {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
File xmlFile = copyResourceToTempFile();
TestObjectRoot root = reader.readFile(xmlFile);
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
}
public void testReadFileNull() throws Exception {
Exception e = null;
try {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
File nullFile = null;
reader.readFile(nullFile);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("file"));
}
}

View file

@ -57,7 +57,7 @@ public class TestBean {
public Character publicCharObjectField = new Character(' ');
public String publicStringObjectField = " ";
public Date publicDateObjectField = new Date(0);
//public Date publicDateObjectField = new Date(0); // TODO add date converters
// private
@ -84,7 +84,7 @@ public class TestBean {
private Character privateCharObjectField = new Character(' ');
private String privateStringObjectField = " ";
private Date privateDateObjectField = new Date(0);
//private Date privateDateObjectField = new Date(0);
// auto gen , get/set-ers
@ -269,18 +269,18 @@ public class TestBean {
public void setPublicStringObjectField(String publicStringObjectField) {
this.publicStringObjectField = publicStringObjectField;
}
/**
/*
* @return the publicDateObjectField
*/
public Date getPublicDateObjectField() {
return publicDateObjectField;
}
/**
}*/
/*
* @param publicDateObjectField the publicDateObjectField to set
*/
public void setPublicDateObjectField(Date publicDateObjectField) {
this.publicDateObjectField = publicDateObjectField;
}
}*/
/**
* @return the privateIntegerTypeField
*/
@ -461,16 +461,16 @@ public class TestBean {
public void setPrivateStringObjectField(String privateStringObjectField) {
this.privateStringObjectField = privateStringObjectField;
}
/**
/*
* @return the privateDateObjectField
*/
public Date getPrivateDateObjectField() {
return privateDateObjectField;
}
/**
}*/
/*
* @param privateDateObjectField the privateDateObjectField to set
*/
public void setPrivateDateObjectField(Date privateDateObjectField) {
this.privateDateObjectField = privateDateObjectField;
}
}*/
}

View file

@ -0,0 +1,152 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2004-2012, Willem Cazander
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided
that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
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.
-->
<root:module
xmlns:root="http://eld.x4o.org/xml/ns/eld-root"
xmlns:eld="http://eld.x4o.org/xml/ns/eld-lang"
xmlns:conv="http://eld.x4o.org/xml/ns/eld-conv"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://eld.x4o.org/xml/ns/eld-root http://eld.x4o.org/xml/ns/eld-root-1.0.xsd"
providerName="test.x4o.org"
name="Test Language"
id="test-module"
>
<eld:classBindingHandler id="Parent-Child" parentClass="org.x4o.xml.test.models.TestObjectParent" childClass="org.x4o.xml.test.models.TestObjectChild" addMethod="addTestObjectChild" getMethod="getTestObjectChilds">
<eld:description>Binds the TestObjectChild to the TestObjectParent</eld:description>
</eld:classBindingHandler>
<eld:classBindingHandler id="Root-Child" parentClass="org.x4o.xml.test.models.TestObjectRoot" childClass="org.x4o.xml.test.models.TestObjectChild" addMethod="addChild" getMethod="getTestObjectChilds">
<eld:description>Binds the TestBean to the TestObjectChild</eld:description>
</eld:classBindingHandler>
<eld:classBindingHandler id="Root-Parent" parentClass="org.x4o.xml.test.models.TestObjectRoot" childClass="org.x4o.xml.test.models.TestObjectParent" addMethod="addParent" getMethod="getTestObjectParents">
<eld:description>Binds the TestObjectParent to the TestObjectRoot</eld:description>
</eld:classBindingHandler>
<eld:classBindingHandler id="Root-Bean" parentClass="org.x4o.xml.test.models.TestObjectRoot" childClass="org.x4o.xml.test.models.TestBean" addMethod="addTestBean" getMethod="getTestBeans">
<eld:description>Binds the TestBean to the TestObjectRoot</eld:description>
</eld:classBindingHandler>
<eld:classBindingHandler id="JComponent-JComponent" parentClass="javax.swing.JComponent" childClass="javax.swing.JComponent" addMethod="addComponent" getMethod="">
<eld:description>Binds j components.</eld:description>
</eld:classBindingHandler>
<eld:classBindingHandler id="JFrame-JPanel" parentClass="javax.swing.JFrame" childClass="javax.swing.JPanel" addMethod="add" getMethod="getComponents">
<eld:description>Binds panel to frame components as unit check.</eld:description>
</eld:classBindingHandler>
<eld:elementInterface id="Component" interfaceClass="java.awt.Component">
<eld:description>Configs the Component based objects.</eld:description>
<eld:attribute name="bounds">
<conv:stringSplitConverter classTo="java.awt.Rectangle" split="," splitSize="4" singleToMethod="setRect" useNativeType="true">
<conv:stringSplitConverterStep fromMethod="getX" fromOrder="1" toOrder="1"><conv:doubleConverter/></conv:stringSplitConverterStep>
<conv:stringSplitConverterStep fromMethod="getY" fromOrder="2" toOrder="2"><conv:doubleConverter/></conv:stringSplitConverterStep>
<conv:stringSplitConverterStep fromMethod="getWidth" fromOrder="3" toOrder="3"><conv:doubleConverter/></conv:stringSplitConverterStep>
<conv:stringSplitConverterStep fromMethod="getHeight" fromOrder="4" toOrder="4"><conv:doubleConverter/></conv:stringSplitConverterStep>
</conv:stringSplitConverter>
</eld:attribute>
</eld:elementInterface>
<eld:elementInterface id="JComponent" interfaceClass="javax.swing.JComponent">
<eld:description>Configs the JComponent based objects.</eld:description>
<eld:classBindingHandler id="JComponent-JComponent" parentClass="javax.swing.JComponent" childClass="javax.swing.JComponent" addMethod="add" getMethod="getComponents">
<eld:description>Binds the JComponent to the JComponent.</eld:description>
</eld:classBindingHandler>
</eld:elementInterface>
<eld:configuratorGlobal bean.class="org.x4o.xml.test.element.TestElementConfigurator" id="testConfigGlobal">
<eld:description>Test the global element configurator.</eld:description>
</eld:configuratorGlobal>
<eld:attributeHandler attributeName="tt.attr1" bean.class="org.x4o.xml.test.element.TestElementAttributeHandler" id="attrTest1">
<eld:description>Test the global element attribute1 handler.</eld:description>
</eld:attributeHandler>
<eld:attributeHandler attributeName="tt.attr2" bean.class="org.x4o.xml.test.element.TestElementAttributeHandler" id="attrTest2">
<eld:description>Test the global element attribute2 handler.</eld:description>
<eld:attributeHandlerNextAttribute attributeName="tt.attr1"/>
</eld:attributeHandler>
<eld:namespace
uri="http://test.x4o.org/xml/ns/test-root"
schemaUri="http://test.x4o.org/xml/ns/test-root-1.0.xsd"
schemaResource="test-root-1.0.xsd"
schemaPrefix="root"
description="Root namespace to have nice namespaceing."
name="Test Root Namespace"
languageRoot="true"
id="test-root"
>
<!-- Root Element for nice namespace'ing -->
<eld:element tag="root" objectClass="org.x4o.xml.test.models.TestObjectRoot">
<eld:description>The test root element.</eld:description>
</eld:element>
</eld:namespace>
<eld:namespace
uri="http://test.x4o.org/xml/ns/test-lang"
schemaUri="http://test.x4o.org/xml/ns/test-lang-1.0.xsd"
schemaResource="test-lang-1.0.xsd"
schemaPrefix="lang"
description="Test language namespace to test some/most features"
name="Test Language Namespace"
id="test-lang"
>
<eld:element tag="testNoNSRoot" objectClass="org.x4o.xml.test.models.TestObjectRoot"/>
<eld:element tag="testBean" objectClass="org.x4o.xml.test.models.TestBean">
<eld:description>The test the bean object.</eld:description>
</eld:element>
<eld:element tag="configBean" objectClass="org.x4o.xml.test.models.TestBean">
<eld:configurator bean.class="org.x4o.xml.test.element.TestElementConfigurator" id="testConfig1">
<eld:description>The test element config.</eld:description>
</eld:configurator>
<eld:configurator bean.class="org.x4o.xml.test.element.TestElementConfigurator" id="testConfig2"/>
<eld:elementSkipPhase name="runAttributesPhase"/>
<eld:elementSkipPhase name="transformPhase"/>
</eld:element>
<eld:element tag="parent" objectClass="org.x4o.xml.test.models.TestObjectParent"/>
<eld:element tag="child" objectClass="org.x4o.xml.test.models.TestObjectChild"/>
<eld:element tag="testObjectParent" objectClass="org.x4o.xml.test.models.TestObjectParent"/>
<eld:element tag="testObjectChild" objectClass="org.x4o.xml.test.models.TestObjectChild"/>
<eld:element tag="testBeanObjectChild" objectClass="org.x4o.xml.test.models.TestObjectChild">
<!--
<eld:element tag="testBeanObjectChild" objectClass="org.x4o.xml.models.TestObjectChild" parentNamespace="http://test.x4o.org/eld/iets.eld" elementTag="superObject"/>
<eld:elementClass tag="JTextArea" objectClassName="javax.swing.JTextArea"/>
<eld:elementClassParentElementClass namespace="http://test.x4o.org/eld/iets.eld" elementTag="superObject"/>
<eld:elementClassParentElementClass namespace="http://test.x4o.org/eld/iets.eld" elementTag="superObject2"/>
-->
</eld:element>
<eld:element tag="JFrame" objectClass="javax.swing.JFrame">
<eld:elementParent tag="root" uri="http://test.x4o.org/xml/ns/test-root"/>
</eld:element>
<eld:element tag="JFrameContentPane" elementClass="org.x4o.xml.test.element.ContentPaneElement"/>
<eld:element tag="JLabel" objectClass="javax.swing.JLabel"/>
<eld:element tag="JPanel" objectClass="javax.swing.JPanel"/>
<eld:element tag="JTextField" objectClass="javax.swing.JTextField"/>
<eld:element tag="JTextArea" objectClass="javax.swing.JTextArea"/>
<eld:element tag="JScrollPane" objectClass="javax.swing.JScrollPane"/>
</eld:namespace>
</root:module>

View file

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2004-2012, Willem Cazander
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided
that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
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.
-->
<modules version="1.0"
xmlns="http://language.x4o.org/xml/ns/modules"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://language.x4o.org/xml/ns/modules http://language.x4o.org/xml/ns/modules-1.0.xsd"
>
<language version="1.0">
<eld-resource>defd-lang.eld</eld-resource>
</language>
</modules>

View file

@ -30,4 +30,5 @@
>
<driver language="test" className="org.x4o.xml.test.TestDriver"/>
<driver language="swixml" className="org.x4o.xml.test.swixml.SwiXmlDriver"/>
<defaultDriver language="junit-defp"/>
</drivers>

View file

@ -21,7 +21,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.x4o.xml.meta.lang;
package org.x4o.xml.lang.meta;
import java.util.logging.Logger;

View file

@ -21,7 +21,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.x4o.xml.meta.lang;
package org.x4o.xml.lang.meta;
import java.util.logging.Logger;

View file

@ -21,7 +21,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.x4o.xml.meta;
package org.x4o.xml.lang.meta;
import org.x4o.xml.lang.X4OLanguageModule;
import org.x4o.xml.lang.X4OLanguageModuleLoaderException;

View file

@ -21,7 +21,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.x4o.xml.meta.lang;
package org.x4o.xml.lang.meta;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

View file

@ -21,7 +21,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.x4o.xml.meta.lang;
package org.x4o.xml.lang.meta;
import org.x4o.xml.element.AbstractElement;

View file

@ -21,7 +21,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.x4o.xml.meta.lang;
package org.x4o.xml.lang.meta;
import java.lang.reflect.Method;
import java.util.Map;

View file

@ -22,10 +22,10 @@
*/
/**
* The X4O XML Meta Language Loader.
* The X4O XML Meta Language.
*
*
* @since 1.0
*/
package org.x4o.xml.meta;
package org.x4o.xml.lang.meta;

View file

@ -1,175 +0,0 @@
/*
* Copyright (c) 2004-2012, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* 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.meta.lang;
import java.util.Comparator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.x4o.xml.element.DefaultElementObjectPropertyValue;
/**
* Compares a property of a java bean.
* The property should be Comparable.
*
* @author Willem Cazander
* @version 1.0 Jan 11, 2006
*/
public class BeanPropertyComparator<T> implements Comparator<T> {
/** The propery of the bean to compare. */
private String property = null;
/** The logger to log to. */
private Logger logger = null;
/** The ascending */
private boolean ascending = true;
private DefaultElementObjectPropertyValue helper = null;
/**
* The constructor inits the logger.
*/
public BeanPropertyComparator() {
logger = Logger.getLogger(BeanPropertyComparator.class.getName());
helper = new DefaultElementObjectPropertyValue();
}
/**
* Creates an BeanPropertyComparator with an property
* @param property The property to compare to.
*/
public BeanPropertyComparator(String property) {
this();
setProperty(property);
}
/**
* Creates an BeanPropertyComparator with an property
* @param property
*/
public BeanPropertyComparator(String property,boolean ascending) {
this();
setProperty(property);
setAscending(ascending);
}
/**
* Compares 2 objects by there Comparable property.
*
* @param o1 Object 1
* @param o2 Object 2
* @return the differce between the objects.
*/
public int compare(Object o1,Object o2) throws ClassCastException {
Comparable<Object> c1 = getComparableProperty(o1);
Comparable<Object> c2 = getComparableProperty(o2);
if(c1==null && c2==null) {
return 0;
}
if(c1==null) {
if(ascending) {
return 1;
} else {
return -1;
}
}
if(c2==null) {
if(ascending) {
return 1;
} else {
return -1;
}
}
if(ascending) {
return c1.compareTo(c2);
} else {
return c2.compareTo(c1);
}
}
/**
* Returns the Comparable property of the object.
* @param object The object to get the property field of.
* @return Returns the Comparable casted object of the property field of the object.
* @throws ClassCastException
*/
@SuppressWarnings("unchecked")
private Comparable<Object> getComparableProperty(Object object) throws ClassCastException {
if(property==null) {
throw new IllegalStateException("property is not set.");
}
Object result = null;
try {
result = helper.getProperty(object,property);
} catch (Exception e) {
logger.log(Level.WARNING,"property:"+property+" is not an property of object: "+object.getClass().getName(),e);
}
try {
Comparable<Object> c = (Comparable<Object>)result;
return c;
} catch (ClassCastException e) {
logger.log(Level.WARNING,"property:"+property+" is not Comparable",e);
throw e;
}
}
/**
* @return Returns the property.
*/
public String getProperty() {
return property;
}
/**
* @param property The property to set.
*/
public void setProperty(String property) {
if(property==null) {
throw new NullPointerException("property may not be null");
}
this.property = property;
logger.finest("property="+property);
}
/**
* @return Returns the ascending.
*/
public boolean isAscending() {
return ascending;
}
/**
* @param ascending The ascending to set.
*/
public void setAscending(boolean ascending) {
this.ascending = ascending;
logger.finest("ascending="+ascending);
}
}

View file

@ -1,31 +0,0 @@
/*
* Copyright (c) 2004-2012, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* 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.
*/
/**
* The X4O XML Meta Language Classes.
*
*
* @since 1.0
*/
package org.x4o.xml.meta.lang;

View file

@ -34,7 +34,7 @@
>
<eld:description>The x4o meta language lets you do special xml tricks.</eld:description>
<eld:attributeHandler id="global-el-id" attributeName="el.id" bean.class="org.x4o.xml.meta.lang.ELIDAttributeHandler">
<eld:attributeHandler id="global-el-id" attributeName="el.id" bean.class="org.x4o.xml.lang.meta.ELIDAttributeHandler">
<eld:description>Lets you bind object into the expression language context.</eld:description>
</eld:attributeHandler>
<eld:namespace
@ -54,20 +54,20 @@
</eld:description>
</eld:element>
<eld:element tag="elReference" elementClass="org.x4o.xml.meta.lang.ELReferenceElement">
<eld:element tag="elReference" elementClass="org.x4o.xml.lang.meta.ELReferenceElement">
<eld:description>
Used to get a reference of an Object into the Element tree.
Mostly used in combination with the el.id tag.
</eld:description>
</eld:element>
<eld:element tag="parentObject" elementClass="org.x4o.xml.meta.lang.ParentObjectElement">
<eld:element tag="parentObject" elementClass="org.x4o.xml.lang.meta.ParentObjectElement">
<eld:description>
Hack in ElementTree, fills this Element with the ElementObject of his parent Element.
</eld:description>
</eld:element>
<eld:element tag="property" elementClass="org.x4o.xml.meta.lang.PropertyElement">
<eld:element tag="property" elementClass="org.x4o.xml.lang.meta.PropertyElement">
<eld:description>
Set an property value on the object of the parent Element.
@ -75,7 +75,7 @@
</eld:description>
</eld:element>
<eld:element tag="method" elementClass="org.x4o.xml.meta.lang.MethodElement">
<eld:element tag="method" elementClass="org.x4o.xml.lang.meta.MethodElement">
<eld:description>
Executes an Method of an Object.
</eld:description>

View file

@ -21,14 +21,14 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.x4o.xml.meta.test;
package org.x4o.xml.lang.meta;
import org.x4o.xml.X4ODriver;
import org.x4o.xml.X4ODriverManager;
import org.x4o.xml.io.X4OReaderContext;
import org.x4o.xml.io.X4OWriterContext;
public class MTestDriver extends X4ODriver {
public class MTestDriver extends X4ODriver<Object> {
public static final String LANGUAGE_NAME = "mtest";
public static final String[] LANGUAGE_VERSIONS = new String[]{X4ODriver.DEFAULT_LANGUAGE_VERSION};

View file

@ -0,0 +1,84 @@
/*
* Copyright (c) 2004-2012, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* 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.lang.meta;
import javax.swing.JLabel;
import org.x4o.xml.element.DefaultElement;
import org.x4o.xml.element.Element;
import org.x4o.xml.io.X4OReaderContext;
import org.x4o.xml.lang.X4OLanguageContext;
import org.x4o.xml.lang.X4OLanguagePropertyKeys;
import junit.framework.TestCase;
/**
* Tests the parent object meta element.
*
* @author Willem Cazander
* @version 1.0 Apr 15, 2012
*/
public class ParentObjectTest extends TestCase {
public void testParentElement() throws Exception {
X4OLanguageContext context = null;
MTestDriver driver = new MTestDriver();
X4OReaderContext<?> reader = driver.createReaderContext();
reader.setProperty(X4OLanguagePropertyKeys.PHASE_SKIP_RELEASE, true);
try {
context = reader.readResourceContext("junit/test-meta-parent-element.xml");
assertEquals(1,context.getRootElement().getChilderen().size());
Element childElement = context.getRootElement().getChilderen().get(0);
JLabel test = (JLabel)childElement.getElementObject();
assertEquals("parentTest",test.getText());
} finally {
reader.releaseContext(context);
}
}
public void testParentObjectElement() throws Exception {
DefaultElement ep = new DefaultElement();
ParentObjectElement e = new ParentObjectElement();
Object o;
// test non parent
o = e.getElementObject();
assertNull(o);
e.setElementObject("test");
o = e.getElementObject();
assertNull(o);
// test parent
e.setParent(ep);
o = e.getElementObject();
assertNull(o);
e.setElementObject("test");
o = e.getElementObject();
assertEquals("test",o);
o = ep.getElementObject();
assertEquals("test",o);
assertEquals(e.getElementObject(),ep.getElementObject());
}
}

View file

@ -21,14 +21,13 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.x4o.xml.meta;
package org.x4o.xml.lang.meta;
import java.util.Date;
import org.x4o.xml.io.X4OReaderContext;
import org.x4o.xml.lang.X4OLanguageContext;
import org.x4o.xml.lang.X4OLanguagePropertyKeys;
import org.x4o.xml.meta.test.MTestDriver;
import junit.framework.TestCase;
@ -46,7 +45,7 @@ public class ReferenceStoreTest extends TestCase {
X4OReaderContext<?> reader = driver.createReaderContext();
reader.setProperty(X4OLanguagePropertyKeys.PHASE_SKIP_RELEASE, true);
try {
context = reader.readResourceContext("tests/meta-generic.xml");
context = reader.readResourceContext("junit/test-meta-generic.xml");
assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(0).getElementObject().getClass().getName());
} finally {
reader.releaseContext(context);
@ -59,7 +58,7 @@ public class ReferenceStoreTest extends TestCase {
X4OReaderContext<?> reader = driver.createReaderContext();
reader.setProperty(X4OLanguagePropertyKeys.PHASE_SKIP_RELEASE, true);
try {
context = reader.readResourceContext("tests/reference/store-ref.xml");
context = reader.readResourceContext("junit/test-meta-reference.xml");
assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(0).getElementObject().getClass().getName());
} finally {
reader.releaseContext(context);
@ -72,7 +71,7 @@ public class ReferenceStoreTest extends TestCase {
X4OReaderContext<?> reader = driver.createReaderContext();
reader.setProperty(X4OLanguagePropertyKeys.PHASE_SKIP_RELEASE, true);
try {
context = reader.readResourceContext("tests/reference/store-ref.xml");
context = reader.readResourceContext("junit/test-meta-reference.xml");
assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(0).getElementObject().getClass().getName());
assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(1).getElementObject().getClass().getName());
assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(2).getElementObject().getClass().getName());

View file

@ -30,6 +30,6 @@
>
<language version="1.0">
<eld-resource>mtest-lang.eld</eld-resource>
<sibling-loader>org.x4o.xml.meta.MetaLanguageSiblingLoader</sibling-loader>
<sibling-loader>org.x4o.xml.lang.meta.MetaLanguageSiblingLoader</sibling-loader>
</language>
</modules>

View file

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2004-2012, Willem Cazander
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided
that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
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.
-->
<tree:root
xmlns:tree="http://mtest.x4o.org/xml/ns/mtest-root"
xmlns:mt="http://mtest.x4o.org/xml/ns/mtest-lang"
xmlns:x4o="http://meta.x4o.org/xml/ns/meta-lang"
>
<mt:JLabel>
<x4o:parentObject>
<x4o:property name="text" value="parentTest"/>
</x4o:parentObject>
</mt:JLabel>
</tree:root>