mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Removed old XML code that never got into main code
This commit is contained in:
@@ -1,100 +0,0 @@
|
||||
package com.avaje.tests.xml;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import com.avaje.tests.xml.build.XbContextBuilder;
|
||||
import com.avaje.tests.xml.oxm.OxmContext;
|
||||
import com.avaje.tests.xml.oxm.OxmContextBuilder;
|
||||
import com.avaje.tests.xml.oxm.OxmNode;
|
||||
|
||||
public class TestXmlSimpleOutput extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer) Ebean.getServer(null);
|
||||
|
||||
OxmContextBuilder builder = new XbContextBuilder(server, null);
|
||||
|
||||
OxmNode node = builder.addRootElement("header", XHeader.class);
|
||||
node.addElement("requestedBy");
|
||||
// node.addElement("requestedSystem");
|
||||
OxmNode horders = node.addElement("orders");
|
||||
horders.addElement("id");
|
||||
horders.addElement("cretime");
|
||||
horders.addElement("orderDate");
|
||||
|
||||
// OxmNode orderRoot = builder.addRootElement("order", Order.class);
|
||||
//
|
||||
// orderRoot.addElement("id").addAttribute("status");
|
||||
// orderRoot.addElement("cretime","created-ts");
|
||||
// orderRoot.addElement("orderDate","ship-date");
|
||||
// OxmNode cust = orderRoot.addElement("customer","cust");
|
||||
// cust.addAttribute("id");
|
||||
// cust.addElement("name");
|
||||
//
|
||||
// OxmNode orderDetails = orderRoot.addElement("details","order-details");
|
||||
// OxmNode line = orderDetails.addWrapperElement("line");
|
||||
// line.addElement("id");
|
||||
// line.addElement("orderQty","order-quantity");
|
||||
// line.addElement("unitPrice","unit-price");
|
||||
//
|
||||
// OxmNode product = line.addElement("product");
|
||||
// product.addElement("name","prodname");
|
||||
// product.addElement("sku","sku");
|
||||
|
||||
OxmContext oxmContext = builder.createContext();
|
||||
|
||||
List<Order> list = Ebean.find(Order.class).fetch("details")
|
||||
.fetch("details.product", "sku,name").findList();
|
||||
|
||||
XHeader header = new XHeader();
|
||||
header.setRequestedBy("blah");
|
||||
header.setRequestedSystem("twoSystem");
|
||||
header.setOrders(list);
|
||||
//
|
||||
// Order bean = list.get(0);
|
||||
//
|
||||
// Writer writer = new StringWriter();
|
||||
// oxmContext.writeBean(bean, writer);
|
||||
// System.out.println(writer);
|
||||
//
|
||||
Writer writer = new StringWriter();
|
||||
oxmContext.writeBean(header, writer);
|
||||
System.out.println(writer);
|
||||
//
|
||||
// DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
|
||||
// DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
|
||||
// Document doc = docBuilder.newDocument();
|
||||
//
|
||||
//
|
||||
//
|
||||
// oxmContext.writeBean(bean, doc);
|
||||
//
|
||||
// System.out.println(doc.getDocumentElement());
|
||||
//
|
||||
// NodeList childNodes = doc.getChildNodes();
|
||||
// int len = childNodes.getLength();
|
||||
// for (int i = 0; i < len; i++) {
|
||||
//
|
||||
// Node childNode = childNodes.item(i);
|
||||
// Order o = (Order)oxmContext.readBean(childNode);
|
||||
// System.out.println(o);
|
||||
// System.out.println(o.getDetails());
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.avaje.tests.xml;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
|
||||
@XmlRootElement
|
||||
public class XHeader {
|
||||
|
||||
String requestedBy;
|
||||
|
||||
String requestedSystem;
|
||||
|
||||
List<Order> orders;
|
||||
|
||||
public String getRequestedBy() {
|
||||
return requestedBy;
|
||||
}
|
||||
|
||||
public void setRequestedBy(String requestedBy) {
|
||||
this.requestedBy = requestedBy;
|
||||
}
|
||||
|
||||
public String getRequestedSystem() {
|
||||
return requestedSystem;
|
||||
}
|
||||
|
||||
public void setRequestedSystem(String requestedSystem) {
|
||||
this.requestedSystem = requestedSystem;
|
||||
}
|
||||
|
||||
public List<Order> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setOrders(List<Order> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.avaje.tests.xml.build;
|
||||
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
import com.avaje.tests.xml.oxm.OxmNode;
|
||||
import com.avaje.tests.xml.runtime.XoiAttribute;
|
||||
import com.avaje.tests.xml.runtime.XrAttribute;
|
||||
|
||||
public class XbAttribute extends XbBase {
|
||||
|
||||
|
||||
public XbAttribute(OxmNode rootNode, String nodeName, String propertyName){
|
||||
super(rootNode, nodeName, propertyName);
|
||||
}
|
||||
|
||||
public XoiAttribute create(BeanDescriptor<?> descriptor, boolean parentAssocBean) {
|
||||
|
||||
ElPropertyValue prop = descriptor.getElGetValue(propertyName);
|
||||
return new XrAttribute(nodeName, prop, descriptor, parentAssocBean, formatter, parser);
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package com.avaje.tests.xml.build;
|
||||
|
||||
import com.avaje.ebean.text.StringFormatter;
|
||||
import com.avaje.ebean.text.StringParser;
|
||||
import com.avaje.tests.xml.oxm.OxmNode;
|
||||
|
||||
public abstract class XbBase {
|
||||
|
||||
protected OxmNode rootNode;
|
||||
protected final String nodeName;
|
||||
protected final String propertyName;
|
||||
protected StringParser parser;
|
||||
protected StringFormatter formatter;
|
||||
protected boolean requiresXmlEncoding;
|
||||
|
||||
public XbBase(String nodeName) {
|
||||
this.nodeName = nodeName;
|
||||
this.propertyName = null;
|
||||
}
|
||||
|
||||
public XbBase(OxmNode rootNode, String nodeName, String propertyName) {
|
||||
this.rootNode = rootNode;
|
||||
this.nodeName = nodeName;
|
||||
this.propertyName = propertyName;
|
||||
}
|
||||
|
||||
public String getNamingConventionNodeName(String propertyName) {
|
||||
// if (rootNode != null){
|
||||
// return rootNode.getNamingConventionNodeName(propertyName);
|
||||
// }
|
||||
return propertyName;
|
||||
}
|
||||
|
||||
public String getNodeName() {
|
||||
return nodeName;
|
||||
}
|
||||
|
||||
public String getPropertyName() {
|
||||
return propertyName;
|
||||
}
|
||||
|
||||
public StringFormatter getFormatter() {
|
||||
return formatter;
|
||||
}
|
||||
|
||||
public void setFormatter(StringFormatter formatter) {
|
||||
this.formatter = formatter;
|
||||
}
|
||||
|
||||
public StringParser getParser() {
|
||||
return parser;
|
||||
}
|
||||
|
||||
public void setParser(StringParser parser) {
|
||||
this.parser = parser;
|
||||
}
|
||||
|
||||
public boolean isRequiresXmlEncoding() {
|
||||
return requiresXmlEncoding;
|
||||
}
|
||||
|
||||
public void setRequiresXmlEncoding(boolean requiresXmlEncoding) {
|
||||
this.requiresXmlEncoding = requiresXmlEncoding;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package com.avaje.tests.xml.build;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.tests.xml.oxm.OxmContext;
|
||||
import com.avaje.tests.xml.oxm.OxmContextBuilder;
|
||||
import com.avaje.tests.xml.oxm.OxmNamingConvention;
|
||||
import com.avaje.tests.xml.runtime.XoiNode;
|
||||
import com.avaje.tests.xml.runtime.XrContext;
|
||||
|
||||
public class XbContextBuilder implements OxmContextBuilder {
|
||||
|
||||
private final SpiEbeanServer ebeanServer;
|
||||
|
||||
private final OxmNamingConvention namingConvention;
|
||||
|
||||
String rootNodeName;
|
||||
|
||||
Map<String,XbNode> rootNodes = new HashMap<String,XbNode>();
|
||||
|
||||
|
||||
public XbContextBuilder(SpiEbeanServer ebeanServer, OxmNamingConvention namingConvention){
|
||||
this.ebeanServer = ebeanServer;
|
||||
this.namingConvention = namingConvention;
|
||||
}
|
||||
|
||||
protected String getNamingConventionNodeName(String propertyName) {
|
||||
|
||||
if (namingConvention != null){
|
||||
return namingConvention.getNodeName(propertyName);
|
||||
} else {
|
||||
return propertyName;
|
||||
}
|
||||
}
|
||||
|
||||
public XbNode addRootElement(String rootElementName, Class<?> rootType) {
|
||||
|
||||
BeanDescriptor<?> descriptor = ebeanServer.getBeanDescriptor(rootType);
|
||||
XbNode rootNode = new XbNode(rootElementName, descriptor);
|
||||
rootNodes.put(rootElementName, rootNode);
|
||||
|
||||
return rootNode;
|
||||
}
|
||||
|
||||
public OxmContext createContext() {
|
||||
|
||||
Map<Class<?>,XoiNode> classNodeMap = new HashMap<Class<?>, XoiNode>(rootNodes.size()+4);
|
||||
Map<String,XoiNode> tagNodeMap = new HashMap<String, XoiNode>(rootNodes.size()+4);
|
||||
|
||||
Iterator<XbNode> it = rootNodes.values().iterator();
|
||||
while (it.hasNext()) {
|
||||
XbNode xbNode = it.next();
|
||||
XoiNode xoiNode = xbNode.createNode();
|
||||
|
||||
classNodeMap.put(xbNode.getRootBeanType(), xoiNode);
|
||||
tagNodeMap.put(xbNode.getRootElementName(), xoiNode);
|
||||
}
|
||||
|
||||
return new XrContext(tagNodeMap, classNodeMap);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,144 +0,0 @@
|
||||
package com.avaje.tests.xml.build;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor.EntityType;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
import com.avaje.tests.xml.oxm.OxmNode;
|
||||
import com.avaje.tests.xml.runtime.XoiAttribute;
|
||||
import com.avaje.tests.xml.runtime.XoiNode;
|
||||
import com.avaje.tests.xml.runtime.XrCollection;
|
||||
import com.avaje.tests.xml.runtime.XrNode;
|
||||
|
||||
public class XbNode extends XbBase implements OxmNode {
|
||||
|
||||
private Map<String,XbAttribute> attributes = new LinkedHashMap<String, XbAttribute>();
|
||||
|
||||
private List<XbNode> childNodes = new ArrayList<XbNode>();
|
||||
|
||||
private final String rootElementName;
|
||||
private final BeanDescriptor<?> rootDescriptor;
|
||||
|
||||
public XbNode(String rootElementName, BeanDescriptor<?> rootDescriptor) {
|
||||
super(rootElementName);
|
||||
this.rootElementName = rootElementName;
|
||||
this.rootDescriptor = rootDescriptor;
|
||||
this.rootNode = this;
|
||||
}
|
||||
|
||||
public XbNode(OxmNode rootNode, String propertyName, String nodeName) {
|
||||
super(rootNode, nodeName, propertyName);
|
||||
this.rootElementName = null;
|
||||
this.rootDescriptor = null;
|
||||
}
|
||||
|
||||
public Class<?> getRootBeanType() {
|
||||
return rootDescriptor.getBeanType();
|
||||
}
|
||||
|
||||
public String getRootElementName() {
|
||||
return rootElementName;
|
||||
}
|
||||
|
||||
public OxmNode addWrapperElement(String elementName){
|
||||
return addElement(null, elementName);
|
||||
}
|
||||
|
||||
public OxmNode addElement(String propertyName){
|
||||
String elementName = getNamingConventionNodeName(propertyName);
|
||||
return addElement(propertyName, elementName);
|
||||
}
|
||||
|
||||
public OxmNode addElement(String propertyName, String elementName){
|
||||
|
||||
XbNode n = new XbNode(rootNode, propertyName, elementName);
|
||||
childNodes.add(n);
|
||||
return n;
|
||||
}
|
||||
|
||||
public OxmNode addAttribute(String propertyName){
|
||||
String attrName = getNamingConventionNodeName(propertyName);
|
||||
return addAttribute(propertyName, attrName);
|
||||
}
|
||||
|
||||
public OxmNode addAttribute(String propertyName, String attrName){
|
||||
XbAttribute xbAttribute = attributes.get(attrName);
|
||||
if (xbAttribute != null){
|
||||
throw new RuntimeException("Attribute with this name ["+attrName+"]already exists");
|
||||
}
|
||||
xbAttribute = new XbAttribute(rootNode, attrName, propertyName);
|
||||
return this;
|
||||
}
|
||||
|
||||
public XoiNode createNode() {
|
||||
return createNode(rootDescriptor);
|
||||
}
|
||||
|
||||
protected XoiNode createNode(BeanDescriptor<?> d) {
|
||||
|
||||
XoiAttribute[] attrs = new XoiAttribute[attributes.size()];
|
||||
|
||||
boolean assocManyProperty = false;
|
||||
boolean assocOneProperty = false;
|
||||
|
||||
ElPropertyValue prop = null;
|
||||
|
||||
if (propertyName != null){
|
||||
prop = d.getElGetValue(propertyName);
|
||||
if (prop == null){
|
||||
throw new RuntimeException("Property "+propertyName+" not found from "+d.getFullName());
|
||||
}
|
||||
BeanProperty p = prop.getBeanProperty();
|
||||
if (p instanceof BeanPropertyAssoc<?>){
|
||||
// move to the relative/target descriptor before
|
||||
// evaluating the attributes and children for this node
|
||||
BeanDescriptor<?> targetDescriptor = ((BeanPropertyAssoc<?>)p).getTargetDescriptor();
|
||||
if (targetDescriptor == null) {
|
||||
if (EntityType.XMLELEMENT.equals(d.getEntityType())){
|
||||
Class<?> targetType = p.getPropertyType();
|
||||
targetDescriptor = d.getBeanDescriptor(targetType);
|
||||
}
|
||||
}
|
||||
|
||||
if (targetDescriptor != null){
|
||||
d = targetDescriptor;
|
||||
}
|
||||
assocManyProperty = (p instanceof BeanPropertyAssocMany<?>);
|
||||
assocOneProperty = (p instanceof BeanPropertyAssocOne<?>);
|
||||
}
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (XbAttribute attrBuilder : attributes.values()) {
|
||||
XoiAttribute xoAttr = attrBuilder.create(d, assocOneProperty);
|
||||
attrs[i++] = xoAttr;
|
||||
}
|
||||
|
||||
XoiNode[] children = null;
|
||||
if (!childNodes.isEmpty()){
|
||||
children = new XoiNode[childNodes.size()];
|
||||
for (int j = 0; j < children.length; j++) {
|
||||
XbNode xbNode = childNodes.get(j);
|
||||
children[j] = xbNode.createNode(d);
|
||||
}
|
||||
}
|
||||
|
||||
if (assocManyProperty){
|
||||
boolean invokeFetch = false;
|
||||
return new XrCollection(nodeName, prop, children, invokeFetch);
|
||||
}
|
||||
if (assocOneProperty){
|
||||
return new XrNode(nodeName, prop, d, children, attrs);
|
||||
}
|
||||
|
||||
return new XrNode(nodeName, prop, d, formatter, parser, children, attrs);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.avaje.tests.xml.oxm;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
public interface OxmContext {
|
||||
|
||||
public void writeBean(Object bean, Document document) throws IOException;
|
||||
|
||||
public void writeBean(Object bean, Writer writer) throws IOException;
|
||||
|
||||
public Object readBean(Node node);
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.avaje.tests.xml.oxm;
|
||||
|
||||
|
||||
public interface OxmContextBuilder {
|
||||
|
||||
public OxmNode addRootElement(String rootElementName, Class<?> rootType);
|
||||
|
||||
public OxmContext createContext();
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.avaje.tests.xml.oxm;
|
||||
|
||||
/**
|
||||
* Naming convention used in XML to Object mapping.
|
||||
*
|
||||
* @author rbygrave
|
||||
*/
|
||||
public interface OxmNamingConvention {
|
||||
|
||||
/**
|
||||
* Returns the default node/element/attribute name for the given property name.
|
||||
*/
|
||||
public String getNodeName(String propertyName);
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.avaje.tests.xml.oxm;
|
||||
|
||||
public interface OxmNode {
|
||||
|
||||
public OxmNode addWrapperElement(String elementName);
|
||||
|
||||
public OxmNode addElement(String propertyName);
|
||||
|
||||
public OxmNode addElement(String propertyName, String elementName);
|
||||
|
||||
public OxmNode addAttribute(String propertyName);
|
||||
|
||||
public OxmNode addAttribute(String propertyName, String attrName);
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.avaje.tests.xml.runtime;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
|
||||
public interface XoiAttribute {
|
||||
|
||||
public void writeAttribute(XrOutputWriter o, Object bean, Object value) throws IOException;
|
||||
|
||||
public void writeAttribute(XrOutputDocument out, Element e, Object bean, Object value) throws IOException;
|
||||
|
||||
public void readNode(Node node, NamedNodeMap attributes, XrReadContext ctx);
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.avaje.tests.xml.runtime;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
|
||||
public interface XoiNode {
|
||||
|
||||
public String getNodeName();
|
||||
|
||||
public Object createBean();
|
||||
|
||||
public void writeNode(XrOutputDocument out, Node node, Object bean) throws IOException;
|
||||
|
||||
public void writeNode(XrOutputWriter o, Object bean) throws IOException;
|
||||
|
||||
public void readNode(Node node, XrReadContext ctx);
|
||||
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package com.avaje.tests.xml.runtime;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import com.avaje.ebean.text.StringFormatter;
|
||||
import com.avaje.ebean.text.StringParser;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
|
||||
public class XrAttribute extends XrBase implements XoiAttribute {
|
||||
|
||||
private boolean parentAssocBean;
|
||||
|
||||
/**
|
||||
* Create as an property based attribute.
|
||||
*/
|
||||
public XrAttribute(String attrName, ElPropertyValue prop, BeanDescriptor<?> parentDesc, boolean parentAssocBean, StringFormatter formatter, StringParser parser) {
|
||||
super(attrName, prop, parentDesc, formatter, parser);
|
||||
this.parentAssocBean = parentAssocBean;
|
||||
}
|
||||
|
||||
public XrAttribute(String attrName, ElPropertyValue prop, BeanDescriptor<?> parentDesc, boolean parentAssocBean) {
|
||||
this(attrName, prop, parentDesc, parentAssocBean, null, null);
|
||||
}
|
||||
|
||||
public void writeAttribute(XrOutputDocument out, Element e, Object bean, Object value) throws IOException {
|
||||
|
||||
Object beanToUse = parentAssocBean ? value : bean;
|
||||
|
||||
Object v = getObjectValue(beanToUse);
|
||||
String sv = getFormattedValue(v);
|
||||
Attr attr = out.getDocument().createAttribute(nodeName);
|
||||
attr.setValue(sv);
|
||||
|
||||
e.setAttributeNode(attr);
|
||||
}
|
||||
|
||||
public void writeAttribute(XrOutputWriter o, Object bean, Object value) throws IOException {
|
||||
|
||||
Object beanToUse = parentAssocBean ? value : bean;
|
||||
Object v = getObjectValue(beanToUse);
|
||||
String sv = getFormattedValue(v);
|
||||
|
||||
o.write(" ");
|
||||
o.write(nodeName);
|
||||
o.write("=\"");
|
||||
o.write(sv);
|
||||
o.write("\"");
|
||||
}
|
||||
|
||||
public void readNode(Node node, NamedNodeMap attributes, XrReadContext ctx) {
|
||||
|
||||
Node namedItem = attributes.getNamedItem(nodeName);
|
||||
if (namedItem != null){
|
||||
String c = namedItem.getTextContent();
|
||||
Object v = getParsedValue(c);
|
||||
setObjectValue(ctx.getBean(), v);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package com.avaje.tests.xml.runtime;
|
||||
|
||||
import com.avaje.ebean.text.StringFormatter;
|
||||
import com.avaje.ebean.text.StringParser;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
|
||||
public abstract class XrBase {
|
||||
|
||||
protected final String nodeName;
|
||||
protected final ElPropertyValue prop;
|
||||
protected final StringFormatter stringFormatter;
|
||||
protected final StringParser stringParser;
|
||||
|
||||
protected final boolean encode;
|
||||
|
||||
protected boolean decreaseDepth = false;
|
||||
|
||||
protected BeanDescriptor<?> beanDescriptor;
|
||||
|
||||
protected XrBase(String nodeName, ElPropertyValue prop, BeanDescriptor<?> parentDesc, StringFormatter formatter, StringParser parser) {
|
||||
|
||||
if (formatter == null && prop != null){
|
||||
formatter = prop.getStringFormatter();
|
||||
}
|
||||
if (parser == null && prop != null){
|
||||
parser = prop.getStringParser();
|
||||
}
|
||||
|
||||
this.nodeName = nodeName;
|
||||
this.prop = prop;
|
||||
this.stringFormatter = formatter;
|
||||
this.stringParser = parser;
|
||||
this.encode = false;
|
||||
|
||||
this.beanDescriptor = getBeanDescriptor(parentDesc, prop);
|
||||
}
|
||||
|
||||
private BeanDescriptor<?> getBeanDescriptor(BeanDescriptor<?> parent, ElPropertyValue prop){
|
||||
if (prop != null){
|
||||
BeanProperty beanProperty = prop.getBeanProperty();
|
||||
if (beanProperty instanceof BeanPropertyAssoc<?>){
|
||||
return ((BeanPropertyAssoc<?>)beanProperty).getTargetDescriptor();
|
||||
}
|
||||
}
|
||||
return parent;
|
||||
}
|
||||
|
||||
protected void setObjectValue(Object bean, Object value){
|
||||
prop.elSetValue(bean, value, true, false);
|
||||
}
|
||||
|
||||
protected Object getObjectValue(Object bean) {
|
||||
return prop.elGetValue(bean);
|
||||
}
|
||||
|
||||
protected Object getParsedValue(String value) {
|
||||
if (value == null || value.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
//FIXME xml decode the string
|
||||
if (stringParser != null){
|
||||
return stringParser.parse(value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
protected String getFormattedValue(Object value) {
|
||||
|
||||
if (value == null) {
|
||||
return "";
|
||||
}
|
||||
String s;
|
||||
if (stringFormatter == null){
|
||||
s = value.toString();
|
||||
} else {
|
||||
s = stringFormatter.format(value);
|
||||
}
|
||||
|
||||
if (encode){
|
||||
//FIXME xml encode the string
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
}
|
||||
@@ -1,176 +0,0 @@
|
||||
package com.avaje.tests.xml.runtime;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import com.avaje.ebean.bean.BeanCollection;
|
||||
import com.avaje.ebean.bean.BeanCollectionAdd;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
|
||||
public class XrCollection extends XrNode implements XoiNode {
|
||||
|
||||
private final boolean invokeFetch;
|
||||
|
||||
private final BeanPropertyAssocMany<?> manyProp;
|
||||
|
||||
private final String mapKey;
|
||||
|
||||
private final BeanDescriptor<?> targetDescriptor;
|
||||
|
||||
/**
|
||||
* Construct with no mapKey and no attributes.
|
||||
*/
|
||||
public XrCollection(String name, ElPropertyValue prop, XoiNode[] children, boolean invokeFetch) {
|
||||
this(name, prop, children, null, invokeFetch, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct with no attributes.
|
||||
*/
|
||||
public XrCollection(String name, ElPropertyValue prop, XoiNode[] children, boolean invokeFetch, String mapKey) {
|
||||
this(name, prop, children, null, invokeFetch, mapKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct with all options.
|
||||
*/
|
||||
public XrCollection(String name, ElPropertyValue prop, XoiNode[] children, XoiAttribute[] attributes, boolean invokeFetch, String mapKey) {
|
||||
|
||||
super(name, prop, null, null, null, children, attributes);
|
||||
this.invokeFetch = invokeFetch;
|
||||
this.decreaseDepth = true;
|
||||
this.mapKey = mapKey;
|
||||
this.manyProp = (BeanPropertyAssocMany<?>)prop.getBeanProperty();
|
||||
this.targetDescriptor = manyProp.getTargetDescriptor();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void readNode(Node node, XrReadContext ctx) {
|
||||
|
||||
Object parentBean = ctx.getBean();
|
||||
|
||||
// create a List/Set/Map to hold the details
|
||||
Object details = manyProp.createEmpty(false);
|
||||
|
||||
// Wrapper used to add to the collection
|
||||
BeanCollectionAdd bcAdd = manyProp.getBeanCollectionAdd(details, mapKey);
|
||||
|
||||
Node detailNode = nextElement(node.getFirstChild());
|
||||
do {
|
||||
Object detailBean = targetDescriptor.createBean();
|
||||
ctx.setBean(detailBean);
|
||||
|
||||
for (int i = 0; i < childNodes.length; i++) {
|
||||
childNodes[i].readNode(detailNode, ctx);
|
||||
}
|
||||
|
||||
bcAdd.addBean(detailBean);
|
||||
|
||||
detailNode = nextElement(detailNode.getNextSibling());
|
||||
|
||||
} while(detailNode != null);
|
||||
|
||||
setObjectValue(parentBean, details);
|
||||
ctx.setBean(parentBean);
|
||||
}
|
||||
|
||||
private Node nextElement(Node node) {
|
||||
|
||||
while (node != null) {
|
||||
int type = node.getNodeType();
|
||||
if (type == Node.ELEMENT_NODE){
|
||||
return node;
|
||||
}
|
||||
node = node.getNextSibling();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeContent(XrOutputWriter o, Object bean, Object val) throws IOException {
|
||||
|
||||
Collection<?> collection = (Collection<?>)val;
|
||||
Iterator<?> it = collection.iterator();
|
||||
while (it.hasNext()) {
|
||||
Object childBean = it.next();
|
||||
for (int i = 0; i < childNodes.length; i++) {
|
||||
childNodes[i].writeNode(o, childBean);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeContent(XrOutputDocument out, Node e, Object bean, Object value) throws IOException {
|
||||
|
||||
Collection<?> collection = (Collection<?>)value;
|
||||
Iterator<?> it = collection.iterator();
|
||||
while (it.hasNext()) {
|
||||
Object childBean = it.next();
|
||||
for (int i = 0; i < childNodes.length; i++) {
|
||||
childNodes[i].writeNode(out, e, childBean);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeNode(XrOutputDocument out, Node node, Object bean) throws IOException {
|
||||
super.writeNode(out, node, bean);
|
||||
}
|
||||
|
||||
// protected void writeNodeChildren(XmlOutputDocument out, Node childNode, Object bean, Object val) throws IOException {
|
||||
//
|
||||
// if (hasChildNodes) {
|
||||
// for (int i = 0; i < childNodes.length; i++) {
|
||||
// childNodes[i].writeNode(out, childNode, bean);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
@Override
|
||||
protected Object getObjectValue(Object bean) {
|
||||
|
||||
Object o = prop.elGetValue(bean);
|
||||
Collection<?> collection = getDetailsIterator(o);
|
||||
return collection;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the details of the collection or map taking care to avoid
|
||||
* unnecessary fetching of the data.
|
||||
*/
|
||||
private Collection<?> getDetailsIterator(Object o) {
|
||||
if (o == null) {
|
||||
return null;
|
||||
}
|
||||
if (o instanceof BeanCollection<?>) {
|
||||
BeanCollection<?> bc = (BeanCollection<?>) o;
|
||||
if (!invokeFetch && !bc.isPopulated()) {
|
||||
return null;
|
||||
}
|
||||
bc.size();
|
||||
return bc.getActualDetails();
|
||||
}
|
||||
|
||||
if (o instanceof Map<?,?>) {
|
||||
// yes, we want the entrySet (to set the keys)
|
||||
return ((Map<?, ?>) o).entrySet();
|
||||
|
||||
} else if (o instanceof Collection<?>) {
|
||||
return ((Collection<?>) o);
|
||||
}
|
||||
String m = "expecting a Map or Collection but got [" + o.getClass().getName() + "]";
|
||||
throw new PersistenceException(m);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
package com.avaje.tests.xml.runtime;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.Map;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import com.avaje.tests.xml.oxm.OxmContext;
|
||||
|
||||
public class XrContext implements OxmContext {
|
||||
|
||||
Map<String,XoiNode> tagToNodeMap;
|
||||
Map<Class<?>,XoiNode> classToNodeMap;
|
||||
|
||||
public XrContext(Map<String,XoiNode> tagToNodeMap, Map<Class<?>,XoiNode> classToNodeMap) {
|
||||
this.tagToNodeMap = tagToNodeMap;
|
||||
this.classToNodeMap = classToNodeMap;
|
||||
}
|
||||
|
||||
public void writeBean(Object bean, Document document) throws IOException {
|
||||
|
||||
XoiNode xoiNode = getXoiNode(bean);
|
||||
|
||||
XrOutputDocument out = new XrOutputDocument(document);
|
||||
xoiNode.writeNode(out, null, bean);
|
||||
}
|
||||
|
||||
public void writeBean(Object bean, Writer writer) throws IOException {
|
||||
XoiNode xoiNode = getXoiNode(bean);
|
||||
|
||||
XrOutputWriter o = new XrOutputWriter(writer);
|
||||
xoiNode.writeNode(o, bean);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Object readBean(Node node) {
|
||||
|
||||
XoiNode xoiNode = getXoiNode(node.getNodeName());
|
||||
|
||||
Object bean = xoiNode.createBean();
|
||||
|
||||
XrReadContext ctx = new XrReadContext(bean);
|
||||
|
||||
xoiNode.readNode(node, ctx);
|
||||
return ctx.getBean();
|
||||
}
|
||||
|
||||
private XoiNode getXoiNode(Object bean) {
|
||||
Class<?> cls = bean.getClass();
|
||||
XoiNode xoiNode = classToNodeMap.get(cls);
|
||||
if (xoiNode == null){
|
||||
throw new RuntimeException("No handler for type "+cls);
|
||||
} else {
|
||||
return xoiNode;
|
||||
}
|
||||
}
|
||||
|
||||
private XoiNode getXoiNode(String nodeName) {
|
||||
|
||||
XoiNode xoiNode = tagToNodeMap.get(nodeName);
|
||||
if (xoiNode == null){
|
||||
throw new RuntimeException("No handler for node "+nodeName);
|
||||
} else {
|
||||
return xoiNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,210 +0,0 @@
|
||||
package com.avaje.tests.xml.runtime;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import com.avaje.ebean.text.StringFormatter;
|
||||
import com.avaje.ebean.text.StringParser;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
|
||||
public class XrNode extends XrBase implements XoiNode {
|
||||
|
||||
protected final boolean assocBeanValue;
|
||||
|
||||
protected final XoiAttribute[] attributes;
|
||||
protected final boolean hasAttributes;
|
||||
|
||||
protected final String beginTag;
|
||||
protected final String beginTagEnd;
|
||||
protected final String endTag;
|
||||
|
||||
protected final XoiNode[] childNodes;
|
||||
protected final boolean hasChildNodes;
|
||||
|
||||
public XrNode(String nodeName, ElPropertyValue prop, BeanDescriptor<?> parentDesc, XoiNode[] childNodes, XoiAttribute[] attributes) {
|
||||
this(nodeName, prop, parentDesc, null, null, childNodes, attributes, true);
|
||||
}
|
||||
|
||||
public XrNode(String nodeName, ElPropertyValue prop, BeanDescriptor<?> parentDesc, StringFormatter formatter,
|
||||
StringParser parser, XoiNode[] childNodes, XoiAttribute[] attributes) {
|
||||
|
||||
this(nodeName, prop, parentDesc, formatter,parser, childNodes, attributes, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create as a property based node.
|
||||
*/
|
||||
private XrNode(String nodeName, ElPropertyValue prop, BeanDescriptor<?> parentDesc, StringFormatter formatter,
|
||||
StringParser parser, XoiNode[] childNodes, XoiAttribute[] attributes, boolean assocBeanValue) {
|
||||
|
||||
super(nodeName, prop, parentDesc, formatter, parser);
|
||||
|
||||
this.assocBeanValue = assocBeanValue;
|
||||
this.attributes = attributes;
|
||||
this.hasAttributes = (attributes != null && attributes.length > 0);
|
||||
|
||||
this.childNodes = childNodes;
|
||||
this.hasChildNodes = (childNodes!= null && childNodes.length > 0);
|
||||
|
||||
boolean selfClosing = false;
|
||||
|
||||
this.beginTagEnd = selfClosing ? "/>" : ">";
|
||||
this.beginTag = "<" + nodeName;
|
||||
this.endTag = selfClosing ? "" : "</" + nodeName + ">";
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return nodeName;
|
||||
}
|
||||
|
||||
public Object createBean() {
|
||||
return beanDescriptor.createBean();
|
||||
}
|
||||
|
||||
public String getNodeName() {
|
||||
return nodeName;
|
||||
}
|
||||
|
||||
public void readNode(Node node, XrReadContext ctx) {
|
||||
|
||||
Object parentBean = null;
|
||||
if (assocBeanValue){
|
||||
parentBean = ctx.getBean();
|
||||
Object childBean = beanDescriptor.createBean();
|
||||
setObjectValue(parentBean, childBean);
|
||||
|
||||
ctx.setBean(childBean);
|
||||
|
||||
} else if (prop != null){
|
||||
|
||||
String c = node.getTextContent();
|
||||
Object v = getParsedValue(c);
|
||||
setObjectValue(ctx.getBean(), v);
|
||||
}
|
||||
|
||||
if (hasAttributes){
|
||||
NamedNodeMap attrs = node.getAttributes();
|
||||
for (int i = 0; i < attributes.length; i++) {
|
||||
attributes[i].readNode(node, attrs, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasChildNodes){
|
||||
readChildNodes(node, ctx);
|
||||
}
|
||||
|
||||
if (parentBean != null){
|
||||
ctx.setBean(parentBean);
|
||||
}
|
||||
}
|
||||
|
||||
private void readChildNodes(Node node, XrReadContext ctx) {
|
||||
|
||||
Node childNode = node.getFirstChild();
|
||||
int pos = -1;
|
||||
|
||||
do {
|
||||
for (; ++pos < childNodes.length;) {
|
||||
if (childNode.getNodeName().equals(childNodes[pos].getNodeName())) {
|
||||
childNodes[pos].readNode(childNode, ctx);
|
||||
break;
|
||||
} else {
|
||||
System.out.println("no element for " + childNodes[pos].getNodeName());
|
||||
}
|
||||
}
|
||||
|
||||
childNode = childNode.getNextSibling();
|
||||
|
||||
} while (childNode != null);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void writeNode(XrOutputDocument out, Node node, Object bean) throws IOException {
|
||||
|
||||
Object val = prop == null ? null : getObjectValue(bean);
|
||||
|
||||
Node childNode;
|
||||
if (nodeName == null){
|
||||
childNode = node;
|
||||
|
||||
} else {
|
||||
Element element = out.getDocument().createElement(nodeName);
|
||||
childNode = element;
|
||||
out.appendChild(node, element);
|
||||
|
||||
if (hasAttributes) {
|
||||
for (int i = 0; i < attributes.length; i++) {
|
||||
attributes[i].writeAttribute(out, element, bean, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writeContent(out, childNode, bean, val);
|
||||
}
|
||||
|
||||
public void writeNode(XrOutputWriter o, Object bean) throws IOException {
|
||||
|
||||
Object val = null;
|
||||
if (prop != null){
|
||||
val = getObjectValue(bean);
|
||||
if (val == null && !hasAttributes && !hasChildNodes){
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
o.increaseDepth();
|
||||
o.write(beginTag);
|
||||
if (hasAttributes) {
|
||||
for (int i = 0; i < attributes.length; i++) {
|
||||
attributes[i].writeAttribute(o, bean, val);
|
||||
}
|
||||
}
|
||||
o.write(beginTagEnd);
|
||||
|
||||
writeContent(o, bean, val);
|
||||
|
||||
if (hasChildNodes){
|
||||
o.decreaseDepth(true);
|
||||
} else {
|
||||
o.decreaseDepth(decreaseDepth);
|
||||
}
|
||||
|
||||
o.write(endTag);
|
||||
}
|
||||
|
||||
public void writeContent(XrOutputDocument out, Node e, Object bean, Object value) throws IOException {
|
||||
|
||||
if (!assocBeanValue && value != null){
|
||||
String sv = getFormattedValue(value);
|
||||
//TODO encode string
|
||||
e.setTextContent(sv);
|
||||
}
|
||||
|
||||
if (hasChildNodes) {
|
||||
Object beanToPass = assocBeanValue ? value : bean;
|
||||
for (int i = 0; i < childNodes.length; i++) {
|
||||
childNodes[i].writeNode(out, e, beanToPass);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void writeContent(XrOutputWriter o, Object bean, Object value) throws IOException {
|
||||
|
||||
if (!assocBeanValue && value != null){
|
||||
String sv = getFormattedValue(value);
|
||||
o.writeEncoded(sv);
|
||||
}
|
||||
if (hasChildNodes) {
|
||||
Object beanToPass = assocBeanValue ? value : bean;
|
||||
for (int i = 0; i < childNodes.length; i++) {
|
||||
childNodes[i].writeNode(o, beanToPass);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.avaje.tests.xml.runtime;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
public class XrOutputDocument {
|
||||
|
||||
private final Document document;
|
||||
|
||||
public XrOutputDocument(Document document) {
|
||||
this.document = document;
|
||||
}
|
||||
|
||||
public Document getDocument() {
|
||||
return document;
|
||||
}
|
||||
|
||||
|
||||
public void appendChild(Node node, Node newChild) {
|
||||
if (node != null){
|
||||
node.appendChild(newChild);
|
||||
} else {
|
||||
node = document.getDocumentElement();
|
||||
if (node == null){
|
||||
document.appendChild(newChild);
|
||||
} else {
|
||||
node.appendChild(newChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package com.avaje.tests.xml.runtime;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
public class XrOutputWriter {
|
||||
|
||||
final Writer writer;
|
||||
|
||||
public XrOutputWriter(Writer writer) {
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
public void writeXml(Object o){
|
||||
|
||||
}
|
||||
|
||||
public void write(String s) throws IOException {
|
||||
writer.write(s);
|
||||
}
|
||||
|
||||
public void writeEncoded(String s) throws IOException {
|
||||
writer.write(s);
|
||||
}
|
||||
|
||||
int depth = -1;
|
||||
|
||||
public void increaseDepth() throws IOException {
|
||||
depth += 1;
|
||||
indent();
|
||||
}
|
||||
|
||||
public void decreaseDepth(boolean indent) throws IOException {
|
||||
|
||||
if (indent) {
|
||||
indent();
|
||||
}
|
||||
depth -= 1;
|
||||
}
|
||||
|
||||
private void indent() throws IOException {
|
||||
writer.write("\n");
|
||||
for (int j = 0; j < depth; j++) {
|
||||
writer.write(" ");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.avaje.tests.xml.runtime;
|
||||
|
||||
public class XrReadContext {
|
||||
|
||||
private Object bean;
|
||||
|
||||
public XrReadContext(Object bean) {
|
||||
this.bean = bean;
|
||||
}
|
||||
|
||||
public Object getBean() {
|
||||
return bean;
|
||||
}
|
||||
|
||||
public void setBean(Object bean) {
|
||||
this.bean = bean;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user