1 package com.icl.saxon.output; 2 import com.icl.saxon.*; 3 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 ; 9 10 14 15 public class DTDEmitter extends ProxyEmitter 16 { 17 private String current = null; 18 private boolean openSquare = false; 19 private StringBuffer buffer = null; 20 21 24 25 public void startElement (int nameCode, Attributes attributes, 26 int[] namespaces, int nscount) throws TransformerException 27 { 28 String uri = namePool.getURI(nameCode); 29 String localname = namePool.getLocalName(nameCode); 30 31 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 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 (); 56 if (current!=null) { 57 throw new TransformerException ("dtd:doctype can only appear at top level of DTD"); 58 } 59 String name = attributes.getValue("name"); 60 String system = attributes.getValue("system"); 61 String publicid = attributes.getValue("public"); 62 if (name==null) { 63 throw new TransformerException ("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 ("dtd:element can only appear as child of dtd:doctype"); 78 } 79 String name = attributes.getValue("name"); 80 String content = attributes.getValue("content"); 81 if (name==null) { 82 throw new TransformerException ("dtd:element must have a name attribute"); 83 } 84 if (content==null) { 85 throw new TransformerException ("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 ("dtd:attlist can only appear as child of dtd:doctype"); 92 } 93 String name = attributes.getValue("element"); 94 if (name==null) { 95 throw new TransformerException ("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 ("dtd:attribute can only appear as child of dtd:attlist"); 102 } 103 String name = attributes.getValue("name"); 104 String type = attributes.getValue("type"); 105 String value = attributes.getValue("value"); 106 if (name==null) { 107 throw new TransformerException ("dtd:attribute must have a name attribute"); 108 } 109 if (type==null) { 110 throw new TransformerException ("dtd:attribute must have a type attribute"); 111 } 112 if (value==null) { 113 throw new TransformerException ("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 ("dtd:entity can only appear as child of dtd:doctype"); 120 } 121 String name = attributes.getValue("name"); 122 String parameter = attributes.getValue("parameter"); 123 String system = attributes.getValue("system"); 124 String publicid = attributes.getValue("public"); 125 String notation = attributes.getValue("notation"); 126 127 if (name==null) { 128 throw new TransformerException ("dtd:entity must have a name attribute"); 129 } 130 131 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 ("dtd:notation can only appear as a child of dtd:doctype"); 152 } 153 String name = attributes.getValue("name"); 154 String system = attributes.getValue("system"); 155 String publicid = attributes.getValue("public"); 156 if (name==null) { 157 throw new TransformerException ("dtd:notation must have a name attribute"); 158 } 159 if ((system==null) && (publicid==null)) { 160 throw new TransformerException ("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 ("Unrecognized element " + localname + " in DTD output"); 173 } 174 175 } else { 176 if (!(current.equals("entity"))) { 177 throw new TransformerException ("Unrecognized element " + localname + " in DTD output"); 178 } 179 super.startElement(nameCode, attributes, namespaces, nscount); 180 } 181 current = localname; 182 183 } 184 185 186 189 190 public void endElement (int nameCode) throws TransformerException 191 { 192 String uri = namePool.getURI(nameCode); 193 194 if (uri.equals(Namespace.DTD)) { 196 String 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 235 } 236 237 240 241 public void characters(char[] chars, int start, int len) throws TransformerException { 242 if (buffer!=null) { 243 buffer.append(chars, start, len); 244 } else { 245 super.characters(chars, start, len); 246 } 247 } 248 249 252 253 private void write(String s) { 254 buffer.append(s); 255 } 256 257 260 261 private void flush() throws TransformerException { 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 274 275 279 } 280 281 | Popular Tags |