KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > output > DTDEmitter


1 package com.icl.saxon.output;
2 import com.icl.saxon.*;
3 //import com.icl.saxon.om.Name;
4
import com.icl.saxon.om.Namespace;
5 import org.xml.sax.*;
6 import java.io.*;
7 import java.util.*;
8 import javax.xml.transform.TransformerException JavaDoc;
9
10 /**
11   * DTDEmitter is an Emitter that generates output in DTD format from special elements
12   * such as dtd:doctype and dtd:element.
13   */

14   
15 public class DTDEmitter extends ProxyEmitter
16 {
17     private String JavaDoc current = null;
18     private boolean openSquare = false;
19     private StringBuffer JavaDoc buffer = null;
20
21     /**
22     * Start of an element.
23     */

24     
25     public void startElement (int nameCode, Attributes attributes,
26                               int[] namespaces, int nscount) throws TransformerException JavaDoc
27     {
28         String JavaDoc uri = namePool.getURI(nameCode);
29         String JavaDoc localname = namePool.getLocalName(nameCode);
30         
31         // Suppress the DTD namespace
32

33         int dtdpos = -1;
34         for (int n=0; n<nscount; n++) {
35             if (namePool.getURIFromNamespaceCode(namespaces[n]).equals(Namespace.DTD)) {
36                 dtdpos = n;
37                 break;
38             }
39         }
40         
41         if (dtdpos>0) {
42             // remove the entry at position dtdpos
43
namespaces[dtdpos] = namespaces[nscount-1];
44             nscount--;
45         }
46         
47         if (uri.equals(Namespace.DTD)) {
48             
49             if ("doctype".equals(current) && !openSquare) {
50                 write(" [");
51                 openSquare = true;
52             }
53
54             if (localname.equals("doctype")) {
55                 buffer = new StringBuffer JavaDoc();
56                 if (current!=null) {
57                     throw new TransformerException JavaDoc("dtd:doctype can only appear at top level of DTD");
58                 }
59                 String JavaDoc name = attributes.getValue("name");
60                 String JavaDoc system = attributes.getValue("system");
61                 String JavaDoc publicid = attributes.getValue("public");
62                 if (name==null) {
63                     throw new TransformerException JavaDoc("dtd:doctype must have a name attribute");
64                 }
65
66                 write("<!DOCTYPE " + name + " ");
67                 if (system!=null) {
68                     if (publicid!=null) {
69                         write("PUBLIC \"" + publicid + "\" \"" + system + "\"");
70                     } else {
71                         write("SYSTEM \"" + system + "\"");
72                     }
73                 }
74                     
75             } else if (localname.equals("element")) {
76                 if (!("doctype".equals(current))) {
77                     throw new TransformerException JavaDoc("dtd:element can only appear as child of dtd:doctype");
78                 }
79                 String JavaDoc name = attributes.getValue("name");
80                 String JavaDoc content = attributes.getValue("content");
81                 if (name==null) {
82                     throw new TransformerException JavaDoc("dtd:element must have a name attribute");
83                 }
84                 if (content==null) {
85                     throw new TransformerException JavaDoc("dtd:element must have a content attribute");
86                 }
87                 write("\n <!ELEMENT " + name + " " + content + " ");
88
89             } else if (localname.equals("attlist")) {
90                 if (!("doctype".equals(current))) {
91                     throw new TransformerException JavaDoc("dtd:attlist can only appear as child of dtd:doctype");
92                 }
93                 String JavaDoc name = attributes.getValue("element");
94                 if (name==null) {
95                     throw new TransformerException JavaDoc("dtd:attlist must have an attribute named 'element'");
96                 }
97                 write("\n <!ATTLIST " + name + " " );
98
99             } else if (localname.equals("attribute")) {
100                 if (!("attlist".equals(current))) {
101                     throw new TransformerException JavaDoc("dtd:attribute can only appear as child of dtd:attlist");
102                 }
103                 String JavaDoc name = attributes.getValue("name");
104                 String JavaDoc type = attributes.getValue("type");
105                 String JavaDoc value = attributes.getValue("value");
106                 if (name==null) {
107                     throw new TransformerException JavaDoc("dtd:attribute must have a name attribute");
108                 }
109                 if (type==null) {
110                     throw new TransformerException JavaDoc("dtd:attribute must have a type attribute");
111                 }
112                 if (value==null) {
113                     throw new TransformerException JavaDoc("dtd:attribute must have a value attribute");
114                 }
115                 write("\n " + name + " " + type + " " + value);
116
117             } else if (localname.equals("entity")) {
118                 if (!("doctype".equals(current))) {
119                     throw new TransformerException JavaDoc("dtd:entity can only appear as child of dtd:doctype");
120                 }
121                 String JavaDoc name = attributes.getValue("name");
122                 String JavaDoc parameter = attributes.getValue("parameter");
123                 String JavaDoc system = attributes.getValue("system");
124                 String JavaDoc publicid = attributes.getValue("public");
125                 String JavaDoc notation = attributes.getValue("notation");
126
127                 if (name==null) {
128                     throw new TransformerException JavaDoc("dtd:entity must have a name attribute");
129                 }
130
131                 // we could do a lot more checking now...
132

133                 write("\n <!ENTITY ");
134                 if ("yes".equals(parameter)) {
135                     write("% ");
136                 }
137                 write(name + " ");
138                 if (system!=null) {
139                     if (publicid!=null) {
140                         write("PUBLIC \"" + publicid + "\" \"" + system + "\" ");
141                     } else {
142                         write("SYSTEM \"" + system + "\" ");
143                     }
144                 }
145                 if (notation!=null) {
146                     write("NDATA " + notation + " ");
147                 }
148
149             } else if (localname.equals("notation")) {
150                 if (!("doctype".equals(current))) {
151                     throw new TransformerException JavaDoc("dtd:notation can only appear as a child of dtd:doctype");
152                 }
153                 String JavaDoc name = attributes.getValue("name");
154                 String JavaDoc system = attributes.getValue("system");
155                 String JavaDoc publicid = attributes.getValue("public");
156                 if (name==null) {
157                     throw new TransformerException JavaDoc("dtd:notation must have a name attribute");
158                 }
159                 if ((system==null) && (publicid==null)) {
160                     throw new TransformerException JavaDoc("dtd:notation must have a system attribute or a public attribute");
161                 }
162                 write("\n <!NOTATION " + name);
163                 if (publicid!=null) {
164                     write(" PUBLIC \"" + publicid + "\" ");
165                     if (system!=null) {
166                         write("\"" + system + "\" ");
167                     }
168                 } else {
169                     write(" SYSTEM \"" + system + "\" ");
170                 }
171             } else {
172                 throw new TransformerException JavaDoc("Unrecognized element " + localname + " in DTD output");
173             }
174
175         } else {
176             if (!(current.equals("entity"))) {
177                 throw new TransformerException JavaDoc("Unrecognized element " + localname + " in DTD output");
178             }
179             super.startElement(nameCode, attributes, namespaces, nscount);
180         }
181         current = localname;
182         
183     }
184
185
186     /**
187     * End of an element.
188     */

189
190     public void endElement (int nameCode) throws TransformerException JavaDoc
191     {
192         String JavaDoc uri = namePool.getURI(nameCode);
193
194         //try {
195
if (uri.equals(Namespace.DTD)) {
196                 String JavaDoc localname = namePool.getLocalName(nameCode);
197                 
198                 if (localname.equals("doctype")) {
199                     if (openSquare) {
200                         write("\n]");
201                         openSquare = false;
202                     }
203                     write(">\n");
204                     current=null;
205                     flush();
206                     
207                 } else if (localname.equals("element")) {
208                     write(">");
209                     current="doctype";
210
211                 } else if (localname.equals("attlist")) {
212                     write(">");
213                     current="doctype";
214
215                 } else if (localname.equals("attribute")) {
216                     current="attlist";
217                     
218                 } else if (localname.equals("entity")) {
219                     write(">");
220                     current="doctype";
221
222                 } else if (localname.equals("notation")) {
223                     write(">");
224                     current="doctype";
225                 }
226             } else {
227                 super.endElement(nameCode);
228             }
229             
230
231         //} catch (java.io.IOException err) {
232
// throw new TransformerException(err);
233
//}
234

235     }
236
237     /**
238     * Write character data (normally the value of an entity)
239     */

240     
241     public void characters(char[] chars, int start, int len) throws TransformerException JavaDoc {
242         if (buffer!=null) {
243             buffer.append(chars, start, len);
244         } else {
245             super.characters(chars, start, len);
246         }
247     }
248     
249     /**
250     * Write content to the buffer
251     */

252     
253     private void write(String JavaDoc s) {
254         buffer.append(s);
255     }
256     
257     /**
258     * Flush the finished DTD to the underlying XML emitter
259     */

260     
261     private void flush() throws TransformerException JavaDoc {
262         int len = buffer.length();
263         char[] chars = new char[len];
264         buffer.getChars(0, len, chars, 0);
265         buffer = null;
266         setEscaping(false);
267         characters(chars, 0, len);
268         setEscaping(true);
269     }
270
271     /**
272     * On completion, get the finished DTD
273     */

274
275     //public String getDTD() {
276
// return ((StringWriter)writer).toString();
277
//}
278

279 }
280
281 //
282
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
283
// you may not use this file except in compliance with the License. You may obtain a copy of the
284
// License at http://www.mozilla.org/MPL/
285
//
286
// Software distributed under the License is distributed on an "AS IS" basis,
287
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
288
// See the License for the specific language governing rights and limitations under the License.
289
//
290
// The Original Code is: all this file.
291
//
292
// The Initial Developer of the Original Code is
293
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
294
//
295
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
296
//
297
// Contributor(s): none.
298
//
299
Popular Tags