KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > state > cocoon > view > DomifyView


1 package org.sapia.soto.state.cocoon.view;
2
3 import org.apache.commons.lang.ClassUtils;
4 import org.infohazard.domify.DOMAdapter;
5
6 import org.sapia.soto.state.Result;
7 import org.sapia.soto.state.StateRuntimeException;
8 import org.sapia.soto.state.Step;
9 import org.sapia.soto.state.cocoon.CocoonContext;
10 import org.sapia.soto.state.xml.StyleStep;
11 import org.sapia.soto.state.xml.TransformState;
12 import org.sapia.soto.state.xml.XMLContext;
13
14 import org.sapia.util.xml.confix.ConfigurationException;
15
16 import org.xml.sax.ContentHandler JavaDoc;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.xml.transform.Transformer JavaDoc;
22 import javax.xml.transform.TransformerConfigurationException JavaDoc;
23 import javax.xml.transform.TransformerFactory JavaDoc;
24 import javax.xml.transform.dom.DOMSource JavaDoc;
25 import javax.xml.transform.sax.SAXResult JavaDoc;
26
27 /**
28  * This class behaves similarly to the <code>JellyView</code>, excepts that it
29  * wraps the model in a w3c <code>Node</code> implementation. The <code>Domify</code>
30  * toolkit is used for this.
31  * <p>
32  * An instance of this class must be configured with at least one XSL source.
33  *
34  * @see org.sapia.soto.state.cocoon.view.JellyView
35  * @see org.sapia.soto.state.cocoon.view.XslSource
36  *
37  * @author Yanick Duchesne
38  * <dl>
39  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
40  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
41  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
42  * </dl>
43  */

44 public class DomifyView extends TransformState implements Step{
45   private String JavaDoc _uri;
46   private String JavaDoc _name;
47   private String JavaDoc _id;
48   private TransformerFactory JavaDoc _fac;
49
50   public DomifyView() {
51     _fac = TransformerFactory.newInstance();
52   }
53   
54   /**
55    * @see org.sapia.soto.state.Step#getName()
56    */

57   public String JavaDoc getName() {
58     return ClassUtils.getShortClassName(getClass());
59   }
60
61   
62   /**
63    * @param name the XML root element name of the XML that is to be produced
64    * as the result of the model's serialization as SAX events.
65    */

66   public void setRootName(String JavaDoc name) {
67     _name = name;
68   }
69   
70   /**
71    * @see org.sapia.soto.state.xml.TransformState#process(org.sapia.soto.state.Result, org.xml.sax.ContentHandler)
72    */

73   protected void process(Result res, ContentHandler JavaDoc handler) {
74     XMLContext ctx = (XMLContext) res.getContext();
75
76     if (ctx.getContentHandler() == null) {
77       return;
78     }
79
80     Object JavaDoc model = null;
81
82     if (ctx.hasCurrentObject()) {
83       model = ctx.currentObject();
84     } else {
85       model = new NullObject();
86     }
87
88     try {
89       execute(model, ctx.getViewParams(), handler);
90     } catch (Throwable JavaDoc t) {
91       throw new StateRuntimeException("Could not execute XSL pipeline", t);
92     }
93   }
94
95   protected void execute(Object JavaDoc model, Map JavaDoc viewParams, ContentHandler JavaDoc handler)
96     throws Throwable JavaDoc {
97
98     if (_name == null) {
99       _name = CocoonContext.MODEL_KEY;
100     }
101
102     DOMSource JavaDoc src = new DOMSource JavaDoc(new DOMAdapter().adapt(model, _name));
103     SAXResult JavaDoc res = new SAXResult JavaDoc(handler);
104     Transformer JavaDoc tx = _fac.newTransformer();
105     tx.transform(src, res);
106   }
107
108   /**
109    * @see org.sapia.util.xml.confix.ObjectHandlerIF#handleObject(java.lang.String, java.lang.Object)
110    */

111   public void handleObject(String JavaDoc name, Object JavaDoc obj)
112     throws ConfigurationException {
113             if (obj instanceof XslSource) {
114                 try{
115                     XslSource xsl = (XslSource)obj;
116                     StyleStep step = new StyleStep();
117                     step.setEnv(env());
118                     step.setSrc(xsl.getUri());
119                 }catch(TransformerConfigurationException JavaDoc e){
120                     throw new ConfigurationException("Could not instantiate StyleStep", e);
121                 }catch(IOException JavaDoc e){
122                     throw new ConfigurationException("Could not inialize stylesheet", e);
123                 }
124             }
125             else{
126                 super.handleObject(name, obj);
127             }
128   }
129
130   // INNER CLASSES //////////////////////////////////////////////
131
public static class NullObject {
132   }
133 }
134
Popular Tags