KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mc > formgenerator > ggf > GGF


1 /*
2  * Created on 22 avr. 2004 by the Message Center Team
3  *
4  */

5
6 package mc.formgenerator.ggf;
7
8 import java.io.File JavaDoc;
9 import java.io.FileNotFoundException JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import javax.xml.transform.TransformerConfigurationException JavaDoc;
15 import javax.xml.transform.TransformerException JavaDoc;
16
17 import mc.formgenerator.ggf.xslprocessing.XalanExecutor;
18
19 import org.w3c.dom.Document JavaDoc;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import org.apache.xml.serialize.XMLSerializer;
23 import org.apache.xml.serialize.OutputFormat;
24
25
26 /**
27  * Implementation of the generic form generator
28  * This class will be call by the servletGF
29  * @author sempereb
30  */

31 public class GGF{
32
33     //Xalan executor instance for xsl processing
34
public XalanExecutor xalanExe;
35     
36     //Context for the webapp
37
public String JavaDoc contextPath = null;
38     
39     //Action URL
40
public String JavaDoc actionURL = null;
41     
42     //Parameters names
43
private Map JavaDoc parameterNames = null;
44     
45
46     /**
47      * Default constructor for our class
48      * @param contextPath Context path for uri resolution
49      * @param actionURL URL for submission
50      * @throws TransformerConfigurationException
51      */

52     public GGF(String JavaDoc contextPath, String JavaDoc actionURL, String JavaDoc userLanguage) throws TransformerConfigurationException JavaDoc{
53         
54         //Setting the context path attribute
55
this.contextPath = contextPath;
56         
57         //Setting the actionURL attribute
58
this.actionURL = actionURL;
59         
60         // get the file path to XForms display
61
String JavaDoc fileXformsBonita = contextPath + File.separator + "web" + File.separator + "xml" + File.separator + "xformsBonita.xml";
62
63         
64         //Instanciation of Xalan executor variable
65
this.xalanExe = new XalanExecutor( this.contextPath + File.separator + "web" + File.separator + "xslt" + File.separator + "generator.xsl",
66             userLanguage, fileXformsBonita);
67         
68         //Parameters names
69
this.parameterNames = new HashMap JavaDoc();
70         
71     }
72      
73     public GGF(String JavaDoc contextPath, String JavaDoc actionURL, String JavaDoc xslURI, String JavaDoc userLanguage)
74     throws TransformerConfigurationException JavaDoc{
75         
76         //Setting the context path attribute
77
this.contextPath = contextPath;
78         
79         //Setting the actionURL attribute
80
this.actionURL = actionURL;
81         
82         // get the file path to XForms display
83
String JavaDoc fileXformsBonita = contextPath + File.separator + "web" + File.separator + "xml" + File.separator + "xformsBonita.xml";
84         
85         //Instanciation of Xalan executor variable
86
this.xalanExe = XalanExecutor.xalanExecutorFormURL( xslURI , userLanguage, fileXformsBonita);
87         
88         //Parameters names
89
this.parameterNames = new HashMap JavaDoc();
90     }
91     
92     // Retourne un Document en String
93
public static String JavaDoc toString(Document JavaDoc doc) throws Exception JavaDoc{
94     
95         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
96         OutputFormat outputFormat = new OutputFormat(doc);
97         outputFormat.setPreserveSpace(true);
98         outputFormat.setIndenting(true);
99         //outputFormat.setLineWidth(0);
100
XMLSerializer serializer = new XMLSerializer(os, outputFormat);
101         serializer.serialize(doc);
102     
103         return new String JavaDoc(os.toByteArray());
104      }
105
106     
107     /**
108      * Generates a xhtml dom thanks to the xsl file and the xml dom in input
109      * @param xmlInput: The xml file that will be submit to the xsl transformation
110      * @return a Document, a dom that represents the generated document
111      * @throws XFormsException
112      * @throws TransformerConfigurationException
113      * @throws FileNotFoundException
114      * @throws TransformerException
115      * @throws IOException
116      */

117     private Document JavaDoc generateXforms (Document JavaDoc instance, String JavaDoc xmlBase) throws TransformerConfigurationException JavaDoc, FileNotFoundException JavaDoc, TransformerException JavaDoc, IOException JavaDoc{
118         //Output Document
119
Document JavaDoc xformsOutput;
120         
121         try {
122             }
123             catch(Exception JavaDoc e){
124                 System.out.println(e + " " + e.getMessage());
125             }
126         
127         //Dom tranformation processing
128
xformsOutput = this.xalanExe.XalanExecutionDOM(instance,xmlBase);
129     
130         try {
131         }
132         catch(Exception JavaDoc e){
133         }
134         
135     
136         //Return the Xforms document
137
return xformsOutput;
138      }
139      
140      
141     /**
142      * Generate XForm document
143      * @param xmlInput: The document send to the xsl engine
144      * @throws TransformerConfigurationException
145      * @throws FileNotFoundException
146      * @throws TransformerException
147      * @throws IOException
148      */

149     public Document JavaDoc runGGF(Document JavaDoc instance, String JavaDoc xmlBase) throws TransformerConfigurationException JavaDoc, FileNotFoundException JavaDoc, TransformerException JavaDoc, IOException JavaDoc{
150         
151         try {
152         }
153         catch(Exception JavaDoc e){
154         }
155         
156         //xforms generation by xsl file and xsl engine
157
Document JavaDoc xformsDocument = this.generateXforms(instance,xmlBase);
158         
159         if(xformsDocument == null){
160         }
161         return xformsDocument;
162     }
163
164
165     /**
166      * @param prefix
167      * @return
168      */

169     public String JavaDoc getUniqueParameterName(String JavaDoc prefix) {
170         String JavaDoc id = prefix + Integer.toHexString((int) (Math.random() * 10000));
171
172         while (this.parameterNames.get(id) != null) {
173             id = prefix + Integer.toHexString((int) (Math.random() * 10000));
174         }
175
176         return id;
177     }
178
179 }
180
Popular Tags