Java源码示例:org.simpleframework.xml.stream.InputNode
示例1
/**
* This method checks to see if there are any <code>Label</code>
* objects remaining in the provided map that are required. This is
* used when deserialization is performed to ensure the the XML
* element deserialized contains sufficient details to satisfy the
* XML schema class annotations. If there is a required label that
* remains it is reported within the exception thrown.
*
* @param node this is the node that contains the contact value
* @param map this is the map to check for remaining labels
* @param source this is the object that has been deserialized
*/
private void validate(InputNode node, LabelMap map, Object source) throws Exception {
Class expect = context.getType(type, source);
Position line = node.getPosition();
for(Label label : map) {
if(label.isRequired() && revision.isEqual()) {
throw new ValueRequiredException("Unable to satisfy %s for %s at %s", label, expect, line);
}
Object value = label.getEmpty(context);
if(value != null) {
criteria.set(label, value);
}
}
}
示例2
@Override
public T read(InputNode node) throws Exception {
Map<String, Field> entityFields = getEntityFields();
T entity = entityClass.newInstance();
List<org.w3c.dom.Element> anyElements = entity.getAny();
InputNode childNode;
while((childNode = node.getNext()) != null) {
if (entityFields.containsKey(childNode.getName())) {
Field field = entityFields.get(childNode.getName());
getSetterForField(field).invoke(entity, serializer.read(field.getType(), childNode));
} else if (childNode.getPrefix() != null && !childNode.getPrefix().isEmpty()) {
org.w3c.dom.Element element = ElementConverter.read(childNode);
anyElements.add(element);
} else {
// Probably a WebDAV field we don't support yet
skipChildrenOfNode(childNode);
}
}
return entity;
}
示例3
@Override
public T read(InputNode node) throws Exception {
Map<String, Field> entityFields = getEntityFields();
T entity = entityClass.newInstance();
List<org.w3c.dom.Element> anyElements = entity.getAny();
InputNode childNode;
while((childNode = node.getNext()) != null) {
if (entityFields.containsKey(childNode.getName())) {
Field field = entityFields.get(childNode.getName());
getSetterForField(field).invoke(entity, serializer.read(field.getType(), childNode));
} else if (childNode.getPrefix() != null && !childNode.getPrefix().isEmpty()) {
org.w3c.dom.Element element = ElementConverter.read(childNode);
anyElements.add(element);
} else {
// Probably a WebDAV field we don't support yet
skipChildrenOfNode(childNode);
}
}
return entity;
}
示例4
public void testTwo() throws Exception {
Context context = new Source(new TreeStrategy(), new Support(), new Session());
PrimitiveList primitive = new PrimitiveList(context, new ClassType(List.class), new ClassType(String.class), "entry");
InputNode node = NodeBuilder.read(new StringReader(TWO));
Object value = primitive.read(node);
assertEquals(value.getClass(), Vector.class);
Vector vector = (Vector) value;
assertEquals(vector.get(0), "one");
assertEquals(vector.get(1), "two");
InputNode newNode = NodeBuilder.read(new StringReader(TWO));
assertTrue(primitive.validate(newNode));
}
示例5
/**
* This is used to read the <code>Value</code> which will be used
* to represent the deserialized object. If there is an annotation
* present then the value will contain an object instance. If it
* does not then it is up to the internal strategy to determine
* what the returned value contains.
*
* @param type this is the type that represents a method or field
* @param node this is the node representing the XML element
* @param value this is the value from the internal strategy
*
* @return the value representing the deserialized value
*/
private Value read(Type type, NodeMap<InputNode> node, Value value) throws Exception {
Converter converter = scanner.getConverter(type, value);
InputNode parent = node.getNode();
if(converter != null) {
Object data = converter.read(parent);
Class actual = type.getType();
if(value != null) {
value.setValue(data);
}
return new Reference(value, data, actual);
}
return value;
}
示例6
public Something read(InputNode node) throws Exception {
Object source = node.getSource();
Element element = (Element)source;
NodeList elements = element.getChildNodes();
String child1 = null;
String child2 = null;
String text = "";
for(int i = 0; i < elements.getLength(); i++) {
Node next = elements.item(i);
if(next.getNodeType() == Node.TEXT_NODE) {
text += next.getNodeValue();
}
if(next.getNodeType() == Node.ELEMENT_NODE) {
if(next.getNodeName().equals("child1")) {
child1 = next.getTextContent();
}
if(next.getNodeName().equals("child2")) {
child2 = next.getTextContent();
}
}
}
return new Something(child1, child2, text.trim());
}
示例7
/**
* This method is used to read the version from the provided input
* node. Once the version has been read it is used to determine how
* to deserialize the object. If the version is not the initial
* version then it is read in a manner that ignores excessive XML
* elements and attributes. Also none of the annotated fields or
* methods are required if the version is not the initial version.
*
* @param node the XML element contact values are deserialized from
* @param source this object whose contacts are to be deserialized
* @param schema this object visits the objects contacts
*/
private void readVersion(InputNode node, Object source, Schema schema) throws Exception {
Label label = schema.getVersion();
Class expect = type.getType();
if(label != null) {
String name = label.getName();
NodeMap<InputNode> map = node.getAttributes();
InputNode value = map.remove(name);
if(value != null) {
readVersion(value, source, label);
} else {
Version version = context.getVersion(expect);
Double start = revision.getDefault();
Double expected = version.revision();
criteria.set(label, start);
revision.compare(expected, start);
}
}
}
示例8
/**
* This <code>populate</code> method will read the XML element map
* from the provided node and deserialize its children as entry types.
* Each entry type must contain a key and value so that the entry
* can be inserted in to the map as a pair. If either the key or
* value is composite it is read as a root object, which means its
* <code>Root</code> annotation must be present and the name of the
* object element must match that root element name.
*
* @param node this is the XML element that is to be deserialized
* @param result this is the map object that is to be populated
*
* @return this returns the item to attach to the object contact
*/
private Object populate(InputNode node, Object result) throws Exception {
Map map = (Map) result;
while(true) {
InputNode next = node.getNext();
if(next == null) {
return map;
}
Object index = key.read(next);
Object item = value.read(next);
map.put(index, item);
}
}
示例9
/**
* This <code>readElement</code> method is used for deserialization
* of the provided node object using a delegate converter. This is
* typically another <code>Composite</code> converter, or if the
* node is an attribute a <code>Primitive</code> converter. When
* the delegate converter has completed the deserialized value is
* assigned to the contact.
*
* @param node this is the node that contains the contact value
* @param source the type of the object that is being deserialized
* @param section this is the section to read the element from
* @param map this is the map that contains the label objects
*/
private void readElement(InputNode node, Object source, Section section, LabelMap map) throws Exception {
String name = node.getName();
String path = section.getPath(name);
Label label = map.getLabel(path);
if(label == null) {
label = criteria.resolve(path);
}
if(label == null) {
Position line = node.getPosition();
Class expect = context.getType(type, source);
if(map.isStrict(context) && revision.isEqual()) {
throw new ElementException("Element '%s' does not have a match in %s at %s", path, expect, line);
} else {
node.skip();
}
} else {
readUnion(node, source, map, label);
}
}
示例10
/**
* This <code>readElements</code> method reads the elements from
* the provided XML element. This will iterate over all elements
* within the element and convert those elements to primitives or
* composite objects depending on the contact annotation.
* <p>
* Once all elements within the XML element have been evaluated
* the <code>Schema</code> is checked to ensure that there are no
* required contacts annotated with the <code>Element</code> that
* remain. If any required element remains an exception is thrown.
*
* @param node this is the XML element to be evaluated
* @param source the type of the object that is being deserialized
* @param section the XML section that contains the structure
*/
private void readElements(InputNode node, Object source, Section section) throws Exception {
LabelMap map = section.getElements();
InputNode child = node.getNext();
while(child != null) {
String name = child.getName();
Section block = section.getSection(name);
if(block != null) {
readSection(child, source, block);
} else {
readElement(child, source, section, map);
}
child = node.getNext();
}
validate(node, map, source);
}
示例11
/**
* This <code>read</code> method will read the XML element list from
* the provided node and deserialize its children as entry types.
* This ensures each entry type is deserialized as a root type, that
* is, its <code>Root</code> annotation must be present and the
* name of the entry element must match that root element name.
*
* @param node this is the XML element that is to be deserialized
* @param list this is the array that is to be deserialized
*
* @return this returns the item to attach to the object contact
*/
public Object read(InputNode node, Object list) throws Exception{
int length = Array.getLength(list);
for(int pos = 0; true; pos++) {
Position line = node.getPosition();
InputNode next = node.getNext();
if(next == null) {
return list;
}
if(pos >= length){
throw new ElementException("Array length missing or incorrect for %s at %s", type, line);
}
read(next, list, pos);
}
}
示例12
/**
* This <code>populate</code> method wll read the XML element list
* from the provided node and deserialize its children as entry types.
* This will deserialize each entry type as a primitive value. In
* order to do this the parent string provided forms the element.
*
* @param node this is the XML element that is to be deserialized
* @param result this is the collection that is to be populated
*
* @return this returns the item to attach to the object contact
*/
private Object populate(InputNode node, Object result) throws Exception {
Collection list = (Collection) result;
while(true) {
InputNode next = node.getNext();
if(next == null) {
return list;
}
list.add(root.read(next));
}
}
示例13
@Override
public Integer read(InputNode node) throws Exception {
try {
String value = node.getValue();
return Integer.valueOf(node.getValue());
}catch (Exception e){
return 0;
}
}
示例14
private boolean isProtected(InputNode node) throws Exception {
InputNode isProtectedNode = node.getAttribute("Protected");
if (isProtectedNode == null) {
return false;
}
String value = isProtectedNode.getValue();
return value.equalsIgnoreCase("true");
}
示例15
@Override
public Date read(InputNode inputNode) throws Exception {
String value = inputNode.getValue();
if (value.equals("${creationDate}")) {
return new Date();
}
return Helpers.toDate(value);
}
示例16
public void testZero() throws Exception {
Context context = new Source(new TreeStrategy(), new Support(), new Session());
PrimitiveArray primitive = new PrimitiveArray(context, new ClassType(String[].class), new ClassType(String.class), "entry");
InputNode node = NodeBuilder.read(new StringReader(ZERO));
Object value = primitive.read(node);
assertEquals(value.getClass(), String[].class);
InputNode newNode = NodeBuilder.read(new StringReader(ZERO));
assertTrue(primitive.validate(newNode));
}
示例17
public void testPrimitiveCycle() throws Exception {
Context context = new Source(new CycleStrategy(), new Support(), new Session());
Primitive primitive = new Primitive(context, new ClassType(String.class));
InputNode node = NodeBuilder.read(new StringReader(CYCLE_1));
Object value = primitive.read(node);
assertEquals("some text", value);
// Need to use a different id for validate as reading has created the object
// and an exception is thrown that the value already exists if id=1 is used
InputNode newNode = NodeBuilder.read(new StringReader(CYCLE_2));
assertTrue(primitive.validate(newNode));
}
示例18
/**
* This <code>validate</code> method wll validate the XML element
* list against the provided node and validate its children as entry
* types. This ensures each entry type is validated as a root type,
* that is, its <code>Root</code> annotation must be present and the
* name of the entry element must match that root element name.
*
* @param node this is the XML element that is to be validated
* @param type this is the array type used to create the array
*
* @return true if the element matches the XML schema class given
*/
private boolean validate(InputNode node, Class type) throws Exception{
while(true) {
InputNode next = node.getNext();
if(next == null) {
return true;
}
if(!next.isEmpty()) {
root.validate(next, type);
}
}
}
示例19
public Cat read(InputNode source) throws Exception{
int age = 0;
String name = null;
while(true) {
InputNode node = source.getNext();
if(node == null) {
break;
}else if(node.getName().equals(ELEMENT_NAME)) {
name = node.getValue();
}else if(node.getName().equals(ELEMENT_AGE)){
age = Integer.parseInt(node.getValue().trim());
}
}
return new Cat(name, age);
}
示例20
/**
* This <code>validate</code> method is used to perform validation
* of the provided node object using a delegate converter. This is
* typically another <code>Composite</code> converter, or if the
* node is an attribute a <code>Primitive</code> converter. If this
* fails validation then an exception is thrown to report the issue.
*
* @param node this is the node that contains the contact value
* @param label this is the label used to create the converter
*/
private void validate(InputNode node, Label label) throws Exception {
Converter reader = label.getConverter(context);
Position line = node.getPosition();
Class expect = type.getType();
boolean valid = reader.validate(node);
if(valid == false) {
throw new PersistenceException("Invalid value for %s in %s at %s", label, expect, line);
}
criteria.set(label, null);
}
示例21
/**
* This method will instantiate an object of the field type, or if
* the <code>Strategy</code> object can resolve a class from the
* XML element then this is used instead. If the resulting type is
* abstract or an interface then this method throws an exception.
*
* @param node this is the node to check for the override
*
* @return this returns an instance of the resulting type
*/
@Override
public Instance getInstance(InputNode node) throws Exception {
Value value = getOverride(node);
Class expect = getType();
if(value == null) {
if(!isInstantiable(expect)) {
throw new InstantiationException("Cannot instantiate %s for %s", expect, type);
}
return context.getInstance(expect);
}
return new ObjectInstance(context, value);
}
示例22
@Override
public Property read(InputNode node) throws Exception {
Property property = new Property();
InputNode childNode = node.getNext();
if (childNode != null) {
property.setProperty(ElementConverter.read(childNode));
}
return property;
}
示例23
/**
* The <code>readResolve</code> method is used to determine if there
* is a resolution method which can be used to substitute the object
* deserialized. The resolve method is used when an object wishes
* to provide a substitute within the deserialized object graph.
* This acts as an equivalent to the Java Object Serialization
* <code>readResolve</code> method for the object deserialization.
*
* @param node the XML element object provided as a replacement
* @param source the type of the object that is being deserialized
* @param caller this is used to invoke the callback methods
*
* @return this returns a replacement for the deserialized object
*/
private Object readResolve(InputNode node, Object source, Caller caller) throws Exception {
if(source != null) {
Position line = node.getPosition();
Object value = caller.resolve(source);
Class expect = type.getType();
Class real = value.getClass();
if(!expect.isAssignableFrom(real)) {
throw new ElementException("Type %s does not match %s at %s", real, expect, line);
}
return value;
}
return source;
}
示例24
/**
* This method is used to read the value value from the node. The
* value read from the node is resolved using the template filter.
* If the value value can not be found according to the annotation
* attributes then an exception is thrown.
*
* @param node this is the node to read the value object from
*
* @return this returns the value deserialized from the node
*/
public Object read(InputNode node) throws Exception {
Class expect = type.getType();
String name = entry.getValue();
if(!entry.isInline()) {
if(name == null) {
name = context.getName(expect);
}
return readElement(node, name);
}
return readAttribute(node, name);
}
示例25
/**
* This <code>read</code> method will perform a read using the
* provided object with the repeater. Reading with this method
* ensures that any additional XML elements within the source
* will be added to the value.
*
* @param node this is the node that contains the extra data
*
* @return this will return the original deserialized object
*/
public boolean validate(InputNode node) throws Exception {
Position line = node.getPosition();
String name = node.getName();
if(reader instanceof Repeater) {
Repeater repeat = (Repeater) reader;
return repeat.validate(node);
}
throw new PersistenceException("Element '%s' declared twice at %s", name, line);
}
示例26
public void read(Type type, NodeMap<InputNode> node){
InputNode element = node.getNode();
if(element.isRoot()) {
Object source = element.getSource();
Class sourceType = source.getClass();
Class itemType = type.getType();
System.out.printf(">>>>> ELEMENT=[%s]%n>>>>> TYPE=[%s]%n>>>>> SOURCE=[%s]%n", element, itemType, sourceType);
}
}
示例27
/**
* This method is used to read the key value from the node. The
* value read from the node is resolved using the template filter.
* If the key value can not be found according to the annotation
* attributes then the node is considered as null and is valid.
*
* @param node this is the node to read the key value from
*
* @return this returns the value deserialized from the node
*/
public boolean validate(InputNode node) throws Exception {
Class expect = type.getType();
String name = entry.getKey();
if(name == null) {
name = context.getName(expect);
}
if(!entry.isAttribute()) {
return validateElement(node, name);
}
return validateAttribute(node, name);
}
示例28
/**
* This <code>populate</code> method wll read the XML element list
* from the provided node and deserialize its children as entry types.
* This will each entry type is deserialized as a root type, that
* is, its <code>Root</code> annotation must be present and the
* name of the entry element must match that root element name.
*
* @param node this is the XML element that is to be deserialized
* @param result this is the collection that is to be populated
*
* @return this returns the item to attach to the object contact
*/
private Object populate(InputNode node, Object result) throws Exception {
Collection list = (Collection) result;
while(true) {
InputNode next = node.getNext();
Class expect = entry.getType();
if(next == null) {
return list;
}
list.add(root.read(next, expect));
}
}
示例29
/**
* This <code>validate</code> method will validate the XML element
* list from the provided node and deserialize its children as entry
* types. This takes each entry type and validates it as a root type,
* that is, its <code>Root</code> annotation must be present and the
* name of the entry element must match that root element name.
*
* @param node this is the XML element that is to be validated
*
* @return true if the element matches the XML schema class given
*/
public boolean validate(InputNode node) throws Exception{
Instance value = factory.getInstance(node);
if(!value.isReference()) {
Object result = value.setInstance(null);
Class type = value.getType();
return validate(node, type);
}
return true;
}
示例30
public void testSimpleList() throws Exception {
Context context = getContext();
Group group = getGroup();
Type type = new ClassType(CompositeListUnionTest.class);
List<Shape> list = new ArrayList<Shape>();
Expression expression = new PathParser("some/path", type, new Format());
CompositeListUnion union = new CompositeListUnion(
context,
group,
expression,
type);
InputNode node = NodeBuilder.read(new StringReader(SOURCE));
InputNode circle = node.getNext();
union.read(circle, list);
InputNode square = node.getNext();
union.read(square, list);
InputNode triangle = node.getNext();
union.read(triangle, list);
assertEquals(list.get(0).getClass(), Circle.class);
assertEquals(list.get(1).getClass(), Square.class);
assertEquals(list.get(2).getClass(), Triangle.class);
OutputNode output = NodeBuilder.write(new PrintWriter(System.out), new Format(3));
OutputNode parent = output.getChild("parent");
union.write(parent, list);
}