001: /*
002: * Created on Mar 22, 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.util.HashMap;
010:
011: import org.jdom.Document;
012: import org.jdom.Element;
013: import org.jdom.transform.JDOMSource;
014: import org.xdev.base.xssl.XSSLAction;
015: import org.xdev.base.xssl.XSSLReturn;
016: import org.xdev.base.xssl.XSSLActionIterator;
017:
018: import java.io.StringReader;
019: import java.io.StringWriter;
020:
021: import javax.xml.transform.Transformer;
022: import javax.xml.transform.TransformerFactory;
023: import javax.xml.transform.stream.StreamResult;
024: import javax.xml.transform.stream.StreamSource;
025:
026: /**
027: * @author AYegorov
028: *
029: * To change the template for this generated type comment go to
030: * Window>Preferences>Java>Code Generation>Code and Comments
031: */
032: public class XmlTransform extends XSSLActionIterator {
033:
034: private Object xml = null;
035:
036: /**
037: * @param id
038: */
039: public XmlTransform(String id) {
040: super (id);
041: // XXX Auto-generated constructor stub
042: }
043:
044: /**
045: * @param id
046: * @param properties
047: */
048: public XmlTransform(String id, HashMap properties) {
049: super (id, properties);
050: // XXX Auto-generated constructor stub
051: }
052:
053: /* (non-Javadoc)
054: * @see org.xdev.base.xssl.XSSLActionIterator#invoke(org.xdev.base.xssl.XSSLAction)
055: */
056: protected void invoke(XSSLAction component) throws Exception {
057: if (component instanceof XSSLReturn) {
058: String xsl = ((XSSLReturn) component).getObjectValue()
059: .toString();
060:
061: this .logDebug("Transformation XSL: " + xsl);
062:
063: TransformerFactory transformerFactory = org.apache.xalan.processor.TransformerFactoryImpl
064: .newInstance();
065: Transformer transformer = transformerFactory
066: .newTransformer(new StreamSource(new StringReader(
067: xsl)));
068:
069: StringWriter writer = new StringWriter();
070:
071: if (this .xml instanceof Document) {
072: transformer.transform(new JDOMSource(
073: (Document) this .xml), new StreamResult(writer));
074: } else if (this .xml instanceof Element) {
075: transformer.transform(new JDOMSource(
076: ((Element) this .xml).getDocument()),
077: new StreamResult(writer));
078: } else {
079: transformer.transform(new StreamSource(
080: new StringReader(this .xml.toString())),
081: new StreamResult(writer));
082: }
083:
084: this .xml = writer.toString();
085: }
086: }
087:
088: public Object getObjectValue() {
089: return this .xml;
090: }
091:
092: /* (non-Javadoc)
093: * @see org.xdev.base.xssl.XSSLAction#set()
094: */
095: protected void set() throws Exception {
096: XSSLReturn xmlReturn = (XSSLReturn) this .getElement(0);
097:
098: this .iterationIndex = 1;
099:
100: if (xmlReturn.execute(this )) {
101: this .xml = xmlReturn.getObjectValue().toString();
102: }
103: }
104:
105: protected int setIterationIndex() {
106: return 1;
107: }
108:
109: }
|