| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.instruct; |
| 9 |
|
| 10 |
import net.sf.saxon.Controller; |
| 11 |
import net.sf.saxon.event.ComplexContentOutputter; |
| 12 |
import net.sf.saxon.event.PipelineConfiguration; |
| 13 |
import net.sf.saxon.event.Receiver; |
| 14 |
import net.sf.saxon.event.ReceiverOptions; |
| 15 |
import net.sf.saxon.expr.Expression; |
| 16 |
import net.sf.saxon.expr.Operand; |
| 17 |
import net.sf.saxon.expr.OperandRole; |
| 18 |
import net.sf.saxon.expr.XPathContext; |
| 19 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 20 |
import net.sf.saxon.om.AxisInfo; |
| 21 |
import net.sf.saxon.om.NodeInfo; |
| 22 |
import net.sf.saxon.om.SequenceIterator; |
| 23 |
import net.sf.saxon.om.StandardNames; |
| 24 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 25 |
import net.sf.saxon.trans.XPathException; |
| 26 |
import net.sf.saxon.tree.tiny.TinyBuilder; |
| 27 |
import net.sf.saxon.type.Type; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class Doctype extends Instruction { |
| 34 |
|
| 35 |
private Operand contentOp; |
| 36 |
|
| 37 |
public Doctype(Expression content) { |
| 38 |
contentOp = new Operand(this, content, OperandRole.SINGLE_ATOMIC); |
| 39 |
} |
| 40 |
|
| 41 |
public Expression getContent() { |
| 42 |
return contentOp.getChildExpression(); |
| 43 |
} |
| 44 |
|
| 45 |
public void setContent(Expression content) { |
| 46 |
contentOp.setChildExpression(content); |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public Iterable<Operand> operands() { |
| 51 |
return contentOp; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public Expression copy(RebindingMap rebindings) { |
| 63 |
throw new UnsupportedOperationException("Doctype.copy()"); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public final boolean mayCreateNewNodes() { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public int getInstructionNameCode() { |
| 81 |
return StandardNames.SAXON_DOCTYPE; |
| 82 |
} |
| 83 |
|
| 84 |
public TailCall processLeavingTail(XPathContext context) throws XPathException { |
| 85 |
Controller controller = context.getController(); |
| 86 |
|
| 87 |
Receiver out = context.getReceiver(); |
| 88 |
PipelineConfiguration pipe = controller.makePipelineConfiguration(); |
| 89 |
pipe.setXPathContext(context); |
| 90 |
pipe.setHostLanguage(getPackageData().getHostLanguage()); |
| 91 |
TinyBuilder builder = new TinyBuilder(pipe); |
| 92 |
builder.setStatistics(pipe.getConfiguration().getTreeStatistics().RESULT_TREE_STATISTICS); |
| 93 |
builder.open(); |
| 94 |
builder.startDocument(0); |
| 95 |
context.setReceiver(ComplexContentOutputter.makeComplexContentReceiver(builder, null)); |
| 96 |
getContent().process(context); |
| 97 |
builder.endDocument(); |
| 98 |
builder.close(); |
| 99 |
context.setReceiver(out); |
| 100 |
NodeInfo dtdRoot = builder.getCurrentRoot(); |
| 101 |
|
| 102 |
SequenceIterator children = dtdRoot.iterateAxis(AxisInfo.CHILD); |
| 103 |
NodeInfo docType = (NodeInfo) children.next(); |
| 104 |
if (docType == null || !"doctype".equals(docType.getLocalPart())) { |
| 105 |
XPathException e = new XPathException("saxon:doctype instruction must contain dtd:doctype"); |
| 106 |
e.setXPathContext(context); |
| 107 |
throw e; |
| 108 |
} |
| 109 |
String name = docType.getAttributeValue("", "name"); |
| 110 |
String system = docType.getAttributeValue("", "system"); |
| 111 |
String publicid = docType.getAttributeValue("", "public"); |
| 112 |
|
| 113 |
if (name == null) { |
| 114 |
XPathException e = new XPathException("dtd:doctype must have a name attribute"); |
| 115 |
e.setXPathContext(context); |
| 116 |
throw e; |
| 117 |
} |
| 118 |
|
| 119 |
write(out, "<!DOCTYPE " + name + ' '); |
| 120 |
if (system != null) { |
| 121 |
if (publicid != null) { |
| 122 |
write(out, "PUBLIC \"" + publicid + "\" \"" + system + '\"'); |
| 123 |
} else { |
| 124 |
write(out, "SYSTEM \"" + system + '\"'); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
boolean openSquare = false; |
| 129 |
children = docType.iterateAxis(AxisInfo.CHILD); |
| 130 |
|
| 131 |
NodeInfo child = (NodeInfo) children.next(); |
| 132 |
if (child != null) { |
| 133 |
write(out, " ["); |
| 134 |
openSquare = true; |
| 135 |
} |
| 136 |
|
| 137 |
while (child != null) { |
| 138 |
String localname = child.getLocalPart(); |
| 139 |
|
| 140 |
if ("element".equals(localname)) { |
| 141 |
String elname = child.getAttributeValue("", "name"); |
| 142 |
String content = child.getAttributeValue("", "content"); |
| 143 |
if (elname == null) { |
| 144 |
XPathException e = new XPathException("dtd:element must have a name attribute"); |
| 145 |
e.setXPathContext(context); |
| 146 |
throw e; |
| 147 |
} |
| 148 |
if (content == null) { |
| 149 |
XPathException e = new XPathException("dtd:element must have a content attribute"); |
| 150 |
e.setXPathContext(context); |
| 151 |
throw e; |
| 152 |
} |
| 153 |
write(out, "\n <!ELEMENT " + elname + ' ' + content + '>'); |
| 154 |
|
| 155 |
} else if (localname.equals("attlist")) { |
| 156 |
String elname = child.getAttributeValue("", "element"); |
| 157 |
if (elname == null) { |
| 158 |
XPathException e = new XPathException("dtd:attlist must have an attribute named 'element'"); |
| 159 |
e.setXPathContext(context); |
| 160 |
throw e; |
| 161 |
} |
| 162 |
write(out, "\n <!ATTLIST " + elname + ' '); |
| 163 |
|
| 164 |
SequenceIterator attributes = child.iterateAxis(AxisInfo.CHILD); |
| 165 |
while (true) { |
| 166 |
NodeInfo attDef = (NodeInfo) attributes.next(); |
| 167 |
if (attDef == null) { |
| 168 |
break; |
| 169 |
} |
| 170 |
|
| 171 |
if ("attribute".equals(attDef.getLocalPart())) { |
| 172 |
|
| 173 |
String atname = attDef.getAttributeValue("", "name"); |
| 174 |
String type = attDef.getAttributeValue("", "type"); |
| 175 |
String value = attDef.getAttributeValue("", "value"); |
| 176 |
if (atname == null) { |
| 177 |
XPathException e = new XPathException("dtd:attribute must have a name attribute"); |
| 178 |
e.setXPathContext(context); |
| 179 |
throw e; |
| 180 |
} |
| 181 |
if (type == null) { |
| 182 |
XPathException e = new XPathException("dtd:attribute must have a type attribute"); |
| 183 |
e.setXPathContext(context); |
| 184 |
throw e; |
| 185 |
} |
| 186 |
if (value == null) { |
| 187 |
XPathException e = new XPathException("dtd:attribute must have a value attribute"); |
| 188 |
e.setXPathContext(context); |
| 189 |
throw e; |
| 190 |
} |
| 191 |
write(out, "\n " + atname + ' ' + type + ' ' + value); |
| 192 |
} else { |
| 193 |
XPathException e = new XPathException("Unrecognized element within dtd:attlist"); |
| 194 |
e.setXPathContext(context); |
| 195 |
throw e; |
| 196 |
} |
| 197 |
} |
| 198 |
write(out, ">"); |
| 199 |
|
| 200 |
} else if (localname.equals("entity")) { |
| 201 |
|
| 202 |
String entname = child.getAttributeValue("", "name"); |
| 203 |
String parameter = child.getAttributeValue("", "parameter"); |
| 204 |
String esystem = child.getAttributeValue("", "system"); |
| 205 |
String epublicid = child.getAttributeValue("", "public"); |
| 206 |
String notation = child.getAttributeValue("", "notation"); |
| 207 |
|
| 208 |
if (entname == null) { |
| 209 |
XPathException e = new XPathException("dtd:entity must have a name attribute"); |
| 210 |
e.setXPathContext(context); |
| 211 |
throw e; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
write(out, "\n <!ENTITY "); |
| 217 |
if ("yes".equals(parameter)) { |
| 218 |
write(out, "% "); |
| 219 |
} |
| 220 |
write(out, entname + ' '); |
| 221 |
if (esystem != null) { |
| 222 |
if (epublicid != null) { |
| 223 |
write(out, "PUBLIC \"" + epublicid + "\" \"" + esystem + "\" "); |
| 224 |
} else { |
| 225 |
write(out, "SYSTEM \"" + esystem + "\" "); |
| 226 |
} |
| 227 |
} |
| 228 |
if (notation != null) { |
| 229 |
write(out, "NDATA " + notation + ' '); |
| 230 |
} |
| 231 |
|
| 232 |
SequenceIterator contents = child.iterateAxis(AxisInfo.CHILD); |
| 233 |
while (true) { |
| 234 |
NodeInfo content = (NodeInfo) contents.next(); |
| 235 |
if (content == null) { |
| 236 |
break; |
| 237 |
} |
| 238 |
content.copy(out, 0, getLocation()); |
| 239 |
} |
| 240 |
write(out, ">"); |
| 241 |
|
| 242 |
} else if (localname.equals("notation")) { |
| 243 |
String notname = child.getAttributeValue("", "name"); |
| 244 |
String nsystem = child.getAttributeValue("", "system"); |
| 245 |
String npublicid = child.getAttributeValue("", "public"); |
| 246 |
if (notname == null) { |
| 247 |
XPathException e = new XPathException("dtd:notation must have a name attribute"); |
| 248 |
e.setXPathContext(context); |
| 249 |
throw e; |
| 250 |
} |
| 251 |
if ((nsystem == null) && (npublicid == null)) { |
| 252 |
XPathException e = new XPathException("dtd:notation must have a system attribute or a public attribute"); |
| 253 |
e.setXPathContext(context); |
| 254 |
throw e; |
| 255 |
} |
| 256 |
write(out, "\n <!NOTATION " + notname); |
| 257 |
if (npublicid != null) { |
| 258 |
write(out, " PUBLIC \"" + npublicid + "\" "); |
| 259 |
if (nsystem != null) { |
| 260 |
write(out, '\"' + nsystem + "\" "); |
| 261 |
} |
| 262 |
} else { |
| 263 |
write(out, " SYSTEM \"" + nsystem + "\" "); |
| 264 |
} |
| 265 |
write(out, ">"); |
| 266 |
} else if (child.getNodeKind() == Type.TEXT) { |
| 267 |
write(out, child.getStringValue()); |
| 268 |
} else { |
| 269 |
XPathException e = new XPathException("Unrecognized element " + localname + " in DTD output"); |
| 270 |
e.setXPathContext(context); |
| 271 |
throw e; |
| 272 |
} |
| 273 |
child = (NodeInfo) children.next(); |
| 274 |
} |
| 275 |
|
| 276 |
if (openSquare) { |
| 277 |
write(out, "\n]"); |
| 278 |
} |
| 279 |
write(out, ">\n"); |
| 280 |
|
| 281 |
return null; |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
private void write(Receiver out, String s) throws XPathException { |
| 286 |
out.characters(s, getLocation(), ReceiverOptions.DISABLE_ESCAPING); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public void export(ExpressionPresenter out) throws XPathException { |
| 296 |
out.startElement("saxonDoctype", this); |
| 297 |
getContent().export(out); |
| 298 |
out.endElement(); |
| 299 |
} |
| 300 |
|
| 301 |
} |
| 302 |
|