001: package org.geotools.xml.impl;
002:
003: import java.util.logging.Level;
004: import java.util.logging.Logger;
005:
006: import org.eclipse.xsd.XSDElementDeclaration;
007: import org.geotools.util.Converters;
008: import org.geotools.xml.Binding;
009: import org.geotools.xml.ComplexBinding;
010: import org.geotools.xml.SimpleBinding;
011: import org.w3c.dom.Document;
012: import org.w3c.dom.Element;
013: import org.w3c.dom.Node;
014: import org.w3c.dom.Text;
015:
016: public class ElementEncodeExecutor implements BindingWalker.Visitor {
017:
018: /** the object being encoded **/
019: Object object;
020:
021: /** the element being encoded **/
022: XSDElementDeclaration element;
023:
024: /** the encoded value **/
025: Element encoding;
026:
027: /** the document / factory **/
028: Document document;
029:
030: /** logger */
031: Logger logger;
032:
033: public ElementEncodeExecutor(Object object,
034: XSDElementDeclaration element, Document document,
035: Logger logger) {
036: this .object = object;
037: this .element = element;
038: this .document = document;
039: this .logger = logger;
040:
041: // if ( element.getTargetNamespace() != null ) {
042: encoding = document.createElementNS(element
043: .getTargetNamespace(), element.getName());
044: // }
045: // else {
046: // encoding = document.createElementNS(
047: // element.getSchema().getTargetNamespace(), element.getName()
048: // );
049: // }
050:
051: }
052:
053: public Element getEncodedElement() {
054: return encoding;
055: }
056:
057: public void visit(Binding binding) {
058: //ensure that the type of the object being encoded matches the type
059: // of the binding
060: if (binding.getType() == null) {
061: if (logger.isLoggable(Level.FINE))
062: logger.fine("Binding: " + binding.getTarget()
063: + " does not declare a target type");
064: return;
065: }
066:
067: if (!binding.getType().isAssignableFrom(object.getClass())) {
068: //try to convert
069: Object converted = Converters.convert(object, binding
070: .getType());
071: if (converted != null) {
072: object = converted;
073: } else {
074: // do not log the object itself, it could be a multi megabyte feature collection
075: if (logger.isLoggable(Level.FINE))
076: logger.fine("[ " + object.getClass()
077: + " ] is not of type " + binding.getType());
078: return;
079: }
080: }
081:
082: if (binding instanceof ComplexBinding) {
083: ComplexBinding complex = (ComplexBinding) binding;
084:
085: try {
086: Element element = complex.encode(object, document,
087: encoding);
088: if (element != null) {
089: encoding = element;
090: }
091: } catch (Throwable t) {
092: String msg = "Encode failed for " + element.getName()
093: + ". Cause: " + t.getLocalizedMessage();
094: throw new RuntimeException(msg, t);
095: }
096: } else {
097: SimpleBinding simple = (SimpleBinding) binding;
098:
099: //figure out if the node has any text
100: Text text = null;
101: for (int i = 0; i < encoding.getChildNodes().getLength(); i++) {
102: Node node = (Node) encoding.getChildNodes().item(i);
103: if (node instanceof Text) {
104: text = (Text) node;
105: break;
106: }
107: }
108:
109: try {
110: String value = simple.encode(object,
111: text != null ? text.getData() : null);
112:
113: if (value != null) {
114: //set the text of the node
115: if (text == null) {
116: text = document.createTextNode(value);
117: encoding.appendChild(text);
118: } else {
119: text.setData(value);
120: }
121: }
122: } catch (Throwable t) {
123: String msg = "Encode failed for " + element.getName()
124: + ". Cause: " + t.getLocalizedMessage();
125: throw new RuntimeException(msg, t);
126: }
127:
128: }
129: }
130:
131: }
|