KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > XmlSubTask


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet;
6
7 import java.net.URL JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.Iterator JavaDoc;
11
12 import org.apache.commons.logging.Log;
13
14 import xdoclet.tagshandler.IdTagsHandler;
15 import xdoclet.util.LogUtil;
16 import xdoclet.util.XmlValidator;
17
18 /**
19  * Generic subtask for processing a user-supplied template, to generate an XML document.
20  *
21  * @author Ara Abrahamian (ara_e@email.com)
22  * @created Oct 13, 2001
23  * @ant.element name="xml" parent="xdoclet.DocletTask" display-name="Standard Subtask for XML generation"
24  * @version $Revision: 1.25 $
25  */

26 public class XmlSubTask extends TemplateSubTask
27 {
28     private boolean useIds = false;
29
30     private String JavaDoc xmlEncoding = "UTF-8";
31
32     /**
33      * Flag that indicates whether validation of generated XML should occur.
34      */

35     private boolean validateXML = false;
36
37     private String JavaDoc publicId = null;
38
39     private String JavaDoc systemId = null;
40
41     private URL JavaDoc dtdURL = null;
42
43     private String JavaDoc schema = null;
44
45     private URL JavaDoc schemaURL = null;
46
47     /**
48      * Gets the UseIds attribute of the XmlSubTask object.
49      *
50      * @return The UseIds value
51      */

52     public boolean getUseIds()
53     {
54         return useIds;
55     }
56
57     /**
58      * Gets the Xmlencoding attribute of the XmlSubTask object.
59      *
60      * @return The Xmlencoding value
61      */

62     public String JavaDoc getXmlencoding()
63     {
64         return xmlEncoding;
65     }
66
67     /**
68      * Gets the DtdURL attribute of the XmlSubTask object.
69      *
70      * @return The DtdURL value
71      */

72     public URL JavaDoc getDtdURL()
73     {
74         return dtdURL;
75     }
76
77     /**
78      * Gets the PublicId attribute of the XmlSubTask object.
79      *
80      * @return The PublicId value
81      */

82     public String JavaDoc getPublicId()
83     {
84         return publicId;
85     }
86
87     /**
88      * Gets the SystemId attribute of the XmlSubTask object.
89      *
90      * @return The SystemId value
91      */

92     public String JavaDoc getSystemId()
93     {
94         return systemId;
95     }
96
97     /**
98      * Gets the Schema attribute of the XmlSubTask object.
99      *
100      * @return The Schema value
101      */

102     public String JavaDoc getSchema()
103     {
104         return schema;
105     }
106
107     /**
108      * Gets the ValidateXML attribute of the XmlSubTask object.
109      *
110      * @return The ValidateXML value
111      */

112     public boolean isValidateXML()
113     {
114         return validateXML;
115     }
116
117     /**
118      * Gets the SchemaURL attribute of the XmlSubTask object.
119      *
120      * @return The SchemaURL value
121      */

122     public URL JavaDoc getSchemaURL()
123     {
124         return schemaURL;
125     }
126
127     /**
128      * If this attribute is set to true, XDoclet will generate id attributes in the XML document. Note that this is only
129      * available in some subtasks.
130      *
131      * @param useIds The new UseIds value
132      * @ant.not-required No. Default is "false"
133      */

134     public void setUseIds(boolean useIds)
135     {
136         this.useIds = useIds;
137     }
138
139     /**
140      * The encoding of the produced xml file. If your XML file uses international characters, you might want to set this
141      * to "ISO-8859-1".
142      *
143      * @param xmlEncoding The new Xmlencoding value
144      * @ant.not-required No, default is "UTF-8"
145      */

146     public void setXmlencoding(String JavaDoc xmlEncoding)
147     {
148         this.xmlEncoding = xmlEncoding;
149     }
150
151     /**
152      * The XML Schema to which the generated document should conform.
153      *
154      * @param schema The new Schema value
155      */

156     public void setSchema(String JavaDoc schema)
157     {
158         this.schema = schema;
159     }
160
161     /**
162      * If this is set to true, the generated XML will be validated against its DTD or XML Schema.
163      *
164      * @param flag The new Validatexml value
165      * @ant.not-required No, default is false.
166      */

167     public void setValidateXML(boolean flag)
168     {
169         validateXML = flag;
170     }
171
172     /**
173      * Describe what the method does
174      *
175      * @param templateSrc Describe what the parameter does
176      */

177     public void copyAttributesFrom(TemplateSubTask templateSrc)
178     {
179         super.copyAttributesFrom(templateSrc);
180
181         XmlSubTask src = (XmlSubTask) templateSrc;
182
183         setValidateXML(src.isValidateXML());
184         setPublicId(src.getPublicId());
185         setSystemId(src.getSystemId());
186         setDtdURL(src.getDtdURL());
187         setSchema(src.getSchema());
188         setSchemaURL(src.getSchemaURL());
189     }
190
191     /**
192      * Describe what the method does
193      *
194      * @exception XDocletException Describe the exception
195      */

196     public void startProcess() throws XDocletException
197     {
198         Log log = LogUtil.getLog(XmlSubTask.class, "startProcess");
199
200         super.getXJavaDoc().setDocEncoding(xmlEncoding);
201
202         if (log.isDebugEnabled()) {
203             log.debug("isValidateXML()=" + isValidateXML());
204             log.debug("getPublicId()=" + getPublicId());
205             log.debug("getSystemId()=" + getSystemId());
206             log.debug("getDtdURL()=" + getDtdURL());
207             log.debug("getSchema()=" + getSchema());
208             log.debug("getSchemaURL()=" + getSchemaURL());
209         }
210
211         if (shouldValidate()) {
212             XmlValidator validator = XmlValidator.getInstance();
213
214             validator.reset();
215
216             if (getPublicId() != null) {
217                 validator.registerDTD(getPublicId(), getDtdURL());
218             }
219             if (getSchemaURL() != null) {
220                 validator.registerSchema(getSchemaURL());
221             }
222         }
223
224         // reset ids counter
225
IdTagsHandler.reset();
226
227         super.startProcess();
228     }
229
230     /**
231      * Sets the SchemaURL attribute of the XmlSubTask object.
232      *
233      * @param url The new SchemaURL value
234      */

235     protected void setSchemaURL(URL JavaDoc url)
236     {
237         this.schemaURL = url;
238     }
239
240     /**
241      * Sets the DtdURL attribute of the XmlSubTask object.
242      *
243      * @param dtdURL The new DtdURL value
244      */

245     protected void setDtdURL(URL JavaDoc dtdURL)
246     {
247         this.dtdURL = dtdURL;
248     }
249
250     /**
251      * The PUBLIC ID of the DTD to which the generated document should conform.
252      *
253      * @param publicId The new PublicId value
254      */

255     protected void setPublicId(String JavaDoc publicId)
256     {
257         this.publicId = publicId;
258     }
259
260     /**
261      * The SYSTEM ID of the DTD to which the generated document should conform.
262      *
263      * @param systemId The new SystemId value
264      */

265     protected void setSystemId(String JavaDoc systemId)
266     {
267         this.systemId = systemId;
268     }
269
270     /**
271      * Describe what the method does
272      *
273      * @exception XDocletException Describe the exception
274      */

275     protected void engineFinished() throws XDocletException
276     {
277         Log log = LogUtil.getLog(XmlSubTask.class, "engineFinished");
278
279         log.debug("isValidateXML()=" + isValidateXML());
280         if (shouldValidate()) {
281             XmlValidator.getInstance().validate(getEngine().getOutput());
282         }
283     }
284
285     /**
286      * Describe what the method does
287      *
288      * @return Describe the return value
289      */

290     private boolean shouldValidate()
291     {
292         return isValidateXML() && ((getPublicId() != null && getDtdURL() != null) || getSchemaURL() != null);
293     }
294 }
295
Popular Tags