001: /*
002: * Created on Mar 9, 2004
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.xdev.base.xssl.text.xml;
008:
009: import java.io.StringReader;
010: import java.util.HashMap;
011: import java.util.List;
012:
013: import org.xdev.base.util.Tools;
014: import org.xdev.base.xssl.XSSLReturn;
015:
016: import org.jaxen.jdom.JDOMXPath;
017: import org.jdom.*;
018: import org.jdom.input.*;
019:
020: /**
021: * @author AYegorov
022: *
023: * To change the template for this generated type comment go to
024: * Window>Preferences>Java>Code Generation>Code and Comments
025: */
026: public class XmlJoin extends XSSLReturn {
027:
028: private List list = null;
029:
030: private Document document = new Document();
031:
032: private Element joinRoot = null;
033:
034: /**
035: * @param id
036: */
037: public XmlJoin(String id) {
038: super (id);
039: // XXX Auto-generated constructor stub
040: }
041:
042: /**
043: * @param id
044: * @param properties
045: */
046: public XmlJoin(String id, HashMap properties) {
047: super (id, properties);
048: // XXX Auto-generated constructor stub
049: }
050:
051: /* (non-Javadoc)
052: * @see org.xdev.base.xssl.XSSLReturn#getValue()
053: */
054: public Object getObjectValue() {
055: return this .joinRoot;
056: }
057:
058: /* (non-Javadoc)
059: * @see org.xdev.base.xssl.XSSLAction#set()
060: */
061: protected void set() throws Exception {
062:
063: Document doc = new Document();
064:
065: this .joinRoot = new Element(this .getProperty("table"));
066:
067: doc.setRootElement(this .joinRoot);
068:
069: XSSLReturn sourceAction = (XSSLReturn) this .getElement(0);
070: XSSLReturn targetAction = (XSSLReturn) this .getElement(1);
071:
072: if (sourceAction.execute(this ) && targetAction.execute(this )) {
073: Object source = sourceAction.getObjectValue();
074: Object target = targetAction.getObjectValue();
075:
076: Element sourceRoot = this .getXmlRoot(source);
077: Element targetRoot = this .getXmlRoot(target);
078:
079: JDOMXPath sourceXpath = new JDOMXPath(this
080: .getProperty("source"));
081: JDOMXPath targetXpath = null;
082:
083: List sourceNodes = sourceXpath.selectNodes(sourceRoot);
084: List targetNodes = null;
085:
086: Object sourceObject = null;
087: Object targetObject = null;
088:
089: String sourceValue = null;
090:
091: Element sourceElement = null;
092: Element targetElement = null;
093:
094: int size = sourceNodes.size();
095:
096: Element rowJoin = null;
097:
098: String targetXpathString = null;
099:
100: for (int i = 0; i < size; i++) {
101: sourceObject = sourceNodes.get(i);
102:
103: sourceElement = (Element) this .getXmlObjectElement(
104: sourceObject).getParent();
105: sourceValue = this .getXmlObjectValue(sourceObject);
106:
107: sourceElement = (Element) sourceElement.detach();
108:
109: targetXpathString = Tools.replaceString(this
110: .getProperty("target"), "@value@", sourceValue);
111:
112: this .logDebug("Target XPath String: "
113: + targetXpathString);
114:
115: targetXpath = new JDOMXPath(targetXpathString);
116:
117: targetNodes = targetXpath.selectNodes(targetRoot);
118:
119: int sz = targetNodes.size();
120:
121: if (sz > 0 || this .getBooleanProperty("outer-join")) {
122:
123: for (int j = 0; j < sz; j++) {
124: targetObject = targetNodes.get(j);
125:
126: targetElement = this
127: .getXmlObjectElement(targetObject);
128:
129: sourceElement
130: .addContent(targetElement.detach());
131:
132: }
133:
134: this .joinRoot.addContent(sourceElement);
135: }
136: }
137: }
138:
139: this .logDebug("Join created: " + this .joinRoot);
140: }
141:
142: private String getXmlObjectValue(Object sourceObject)
143: throws Exception {
144: String sourceValue = null;
145:
146: if (sourceObject instanceof Element) {
147: sourceValue = ((Element) sourceObject).getTextTrim();
148: } else if (sourceObject instanceof Attribute) {
149: sourceValue = ((Attribute) sourceObject).getValue();
150: } else if (sourceObject instanceof Text) {
151: sourceValue = ((Text) sourceObject).getTextTrim();
152: }
153:
154: return sourceValue;
155: }
156:
157: private Element getXmlObjectElement(Object sourceObject) {
158: Element sourceElement = null;
159:
160: if (sourceObject instanceof Element) {
161: sourceElement = (Element) sourceObject;
162: } else if (sourceObject instanceof Attribute) {
163: sourceElement = ((Attribute) sourceObject).getParent();
164: } else if (sourceObject instanceof Text) {
165: sourceElement = (Element) ((Text) sourceObject).getParent();
166: }
167:
168: return sourceElement;
169: }
170:
171: private Element getXmlRoot(Object obj) throws Exception {
172: Element root = null;
173:
174: if (obj instanceof String) {
175: SAXBuilder builder = new SAXBuilder();
176:
177: Document doc = builder.build(new StringReader(obj
178: .toString()));
179:
180: root = doc.getRootElement();
181: } else if (obj instanceof Document) {
182: root = ((Document) obj).getRootElement();
183: } else if (obj instanceof Element) {
184: root = (Element) obj;
185: }
186:
187: return root;
188: }
189: }
|