
net.sf.xolite.expressions.jaxb_abstr.RecursiveExpressionExample_abstr Maven / Gradle / Ivy
/*-------------------------------------------------------------------------
Copyright 2006 Olivier Berlanger
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-------------------------------------------------------------------------*/
package net.sf.xolite.expressions.jaxb_abstr;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import net.sf.xolite.XMLParseException;
import org.apache.log4j.xml.DOMConfigurator;
import org.xml.sax.SAXException;
public class RecursiveExpressionExample_abstr {
public void run(boolean useSchema) throws Exception {
System.out.println("Expressions mapped with JAXB using abtract type");
System.out.println("");
testSimpleHolder(useSchema);
testExpressionHolder(useSchema);
}
private void testSimpleHolder(boolean useSchema) throws Exception {
System.out.println("------------------------------------------------------------------------");
String packageName = getClass().getPackage().getName();
JAXBContext jc = JAXBContext.newInstance(packageName);
Unmarshaller u = jc.createUnmarshaller();
u.setSchema(getSchema());
JAXBElement> element = (JAXBElement>) u.unmarshal(getInputStream("SimpleExpression.xml"));
BooleanExpression expr = (BooleanExpression) element.getValue();
// display result
System.out.println("Got expression: " + expr);
System.out.println("");
}
private void testExpressionHolder(boolean useSchema) throws Exception {
System.out.println("------------------------------------------------------------------------");
String packageName = getClass().getPackage().getName();
JAXBContext jc = JAXBContext.newInstance(packageName);
Unmarshaller u = jc.createUnmarshaller();
u.setSchema(getSchema());
ExpressionHolder holder = (ExpressionHolder) u.unmarshal(getInputStream("BooleanExpression.xml"));
// display result
System.out.println("Loaded: " + holder);
System.out.println("Expression " + holder.getExpression() + " evaluated to " + holder.evaluate());
// serialize back in the console
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
QName expr_QNAME = new QName("xo-lite.sf.net/examples/expression/jaxb_abstr", "expr");
JAXBElement elementToMarshall = new JAXBElement(expr_QNAME, ExpressionHolder.class,
null, holder);
m.marshal(elementToMarshall, System.out);
// Also write it in a temporary file rather than console
File outputFile = File.createTempFile("booleanExpressionExample_", ".xml");
OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
m.marshal(elementToMarshall, out);
System.out.println("Written in temporary file: " + outputFile.getAbsolutePath());
}
private InputStream getInputStream(String resourceName) throws XMLParseException {
InputStream is = getClass().getResourceAsStream(resourceName);
if (is == null) throw new XMLParseException("Resource '" + resourceName + "' not found");
return is;
}
/**
* Load the schema for boolean expressions.
*/
private Schema getSchema() {
Schema mySchema;
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
mySchema = sf.newSchema(getClass().getResource("BooleanExpression_abstr.xsd"));
} catch (SAXException saxe) {
throw new IllegalStateException("Schema not found", saxe);
}
return mySchema;
}
/**
* Configure log4j using the "log4j.xml" file from the default directory.
*/
public static void setupLogging() {
try {
DOMConfigurator.configure(new File("log4j.xml").toURI().toURL());
} catch (MalformedURLException mue) {
throw new IllegalStateException("Invalid log4j configuration file URL", mue);
}
}
public static void main(String[] args) throws Exception {
setupLogging();
new RecursiveExpressionExample_abstr().run(true);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy