summaryrefslogtreecommitdiff
path: root/libjava/classpath/gnu/xml
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/classpath/gnu/xml')
-rw-r--r--libjava/classpath/gnu/xml/aelfred2/JAXPFactory.java230
-rw-r--r--libjava/classpath/gnu/xml/aelfred2/SAXDriver.java1609
-rw-r--r--libjava/classpath/gnu/xml/aelfred2/XmlParser.java5831
-rw-r--r--libjava/classpath/gnu/xml/aelfred2/XmlReader.java373
-rw-r--r--libjava/classpath/gnu/xml/aelfred2/package.html506
-rw-r--r--libjava/classpath/gnu/xml/dom/Consumer.java352
-rw-r--r--libjava/classpath/gnu/xml/dom/DTDAttributeTypeInfo.java83
-rw-r--r--libjava/classpath/gnu/xml/dom/DTDElementTypeInfo.java111
-rw-r--r--libjava/classpath/gnu/xml/dom/DomAttr.java411
-rw-r--r--libjava/classpath/gnu/xml/dom/DomCDATASection.java90
-rw-r--r--libjava/classpath/gnu/xml/dom/DomCharacterData.java344
-rw-r--r--libjava/classpath/gnu/xml/dom/DomComment.java80
-rw-r--r--libjava/classpath/gnu/xml/dom/DomDOMException.java174
-rw-r--r--libjava/classpath/gnu/xml/dom/DomDoctype.java455
-rw-r--r--libjava/classpath/gnu/xml/dom/DomDocument.java1555
-rw-r--r--libjava/classpath/gnu/xml/dom/DomDocumentBuilder.java227
-rw-r--r--libjava/classpath/gnu/xml/dom/DomDocumentBuilderFactory.java181
-rw-r--r--libjava/classpath/gnu/xml/dom/DomDocumentConfiguration.java260
-rw-r--r--libjava/classpath/gnu/xml/dom/DomDocumentFragment.java75
-rw-r--r--libjava/classpath/gnu/xml/dom/DomElement.java582
-rw-r--r--libjava/classpath/gnu/xml/dom/DomEntity.java146
-rw-r--r--libjava/classpath/gnu/xml/dom/DomEntityReference.java130
-rw-r--r--libjava/classpath/gnu/xml/dom/DomEvent.java351
-rw-r--r--libjava/classpath/gnu/xml/dom/DomExtern.java116
-rw-r--r--libjava/classpath/gnu/xml/dom/DomImpl.java277
-rw-r--r--libjava/classpath/gnu/xml/dom/DomIterator.java380
-rw-r--r--libjava/classpath/gnu/xml/dom/DomNSResolverContext.java90
-rw-r--r--libjava/classpath/gnu/xml/dom/DomNamedNodeMap.java421
-rw-r--r--libjava/classpath/gnu/xml/dom/DomNode.java2222
-rw-r--r--libjava/classpath/gnu/xml/dom/DomNodeIterator.java328
-rw-r--r--libjava/classpath/gnu/xml/dom/DomNotation.java102
-rw-r--r--libjava/classpath/gnu/xml/dom/DomNsNode.java226
-rw-r--r--libjava/classpath/gnu/xml/dom/DomProcessingInstruction.java146
-rw-r--r--libjava/classpath/gnu/xml/dom/DomText.java222
-rw-r--r--libjava/classpath/gnu/xml/dom/DomXPathExpression.java147
-rw-r--r--libjava/classpath/gnu/xml/dom/DomXPathNSResolver.java64
-rw-r--r--libjava/classpath/gnu/xml/dom/DomXPathResult.java233
-rw-r--r--libjava/classpath/gnu/xml/dom/ImplementationList.java70
-rw-r--r--libjava/classpath/gnu/xml/dom/ImplementationSource.java167
-rw-r--r--libjava/classpath/gnu/xml/dom/JAXPFactory.java308
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLAnchorElement.java188
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLAppletElement.java187
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLAreaElement.java138
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLBRElement.java68
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseElement.java78
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseFontElement.java88
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLBodyElement.java118
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLButtonElement.java119
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLCollection.java225
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLDListElement.java68
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLDirectoryElement.java68
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLDivElement.java68
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLDocument.java426
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLElement.java286
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLEmbedElement.java129
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLFieldSetElement.java64
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLFontElement.java88
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLFormElement.java149
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameElement.java145
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameSetElement.java78
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLHRElement.java98
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadElement.java68
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadingElement.java68
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLHtmlElement.java68
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLIFrameElement.java165
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLImageElement.java178
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLImpl.java66
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLInputElement.java265
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLIsIndexElement.java74
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLLIElement.java78
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLLabelElement.java84
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLLegendElement.java84
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLLinkElement.java148
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLMapElement.java78
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLMenuElement.java68
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLMetaElement.java98
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLModElement.java78
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLOListElement.java88
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLObjectElement.java320
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLOptGroupElement.java78
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLOptionElement.java130
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLParagraphElement.java68
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLParamElement.java98
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLParser.java264
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLPreElement.java68
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLQuoteElement.java68
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLScriptElement.java128
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLSelectElement.java210
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLStyleElement.java88
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCaptionElement.java69
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCellElement.java204
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLTableColElement.java119
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLTableElement.java397
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLTableRowElement.java228
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLTableSectionElement.java162
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLTextAreaElement.java181
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLTitleElement.java68
-rw-r--r--libjava/classpath/gnu/xml/dom/html2/DomHTMLUListElement.java78
-rw-r--r--libjava/classpath/gnu/xml/dom/ls/DomLSException.java57
-rw-r--r--libjava/classpath/gnu/xml/dom/ls/DomLSInput.java159
-rw-r--r--libjava/classpath/gnu/xml/dom/ls/DomLSOutput.java98
-rw-r--r--libjava/classpath/gnu/xml/dom/ls/DomLSParser.java567
-rw-r--r--libjava/classpath/gnu/xml/dom/ls/DomLSSerializer.java353
-rw-r--r--libjava/classpath/gnu/xml/dom/ls/FilteredSAXEventSink.java353
-rw-r--r--libjava/classpath/gnu/xml/dom/ls/ReaderInputStream.java236
-rw-r--r--libjava/classpath/gnu/xml/dom/ls/SAXEventSink.java603
-rw-r--r--libjava/classpath/gnu/xml/dom/ls/WriterOutputStream.java97
-rw-r--r--libjava/classpath/gnu/xml/dom/package.html273
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeAttr.java117
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeCDATASection.java57
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeCharacterData.java119
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeComment.java57
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMException.java98
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMStringList.java84
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocument.java562
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilder.java326
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilderFactory.java118
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentFragment.java57
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentType.java98
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeElement.java184
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntity.java104
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntityReference.java57
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeNamedNodeMap.java92
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeNode.java501
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeNodeList.java66
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeNotation.java80
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeProcessingInstruction.java79
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeText.java132
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeTypeInfo.java65
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathExpression.java86
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNSResolver.java64
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNodeList.java73
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathResult.java134
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/sax/GnomeLocator.java99
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParser.java105
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParserFactory.java92
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/sax/GnomeXMLReader.java1065
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/sax/Namespaces.java122
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/sax/StringArrayAttributes.java170
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/sax/XMLName.java92
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/transform/ErrorListenerErrorHandler.java111
-rwxr-xr-xlibjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformer.java572
-rwxr-xr-xlibjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformerFactory.java349
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/transform/URIResolverEntityResolver.java87
-rwxr-xr-xlibjava/classpath/gnu/xml/libxmlj/transform/package.html14
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/util/EmptyNodeList.java62
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/util/NamedInputStream.java99
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/util/StandaloneDocumentType.java294
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/util/StandaloneLocator.java89
-rw-r--r--libjava/classpath/gnu/xml/libxmlj/util/XMLJ.java280
-rw-r--r--libjava/classpath/gnu/xml/pipeline/CallFilter.java257
-rw-r--r--libjava/classpath/gnu/xml/pipeline/DomConsumer.java967
-rw-r--r--libjava/classpath/gnu/xml/pipeline/EventConsumer.java95
-rw-r--r--libjava/classpath/gnu/xml/pipeline/EventFilter.java796
-rw-r--r--libjava/classpath/gnu/xml/pipeline/LinkFilter.java242
-rw-r--r--libjava/classpath/gnu/xml/pipeline/NSFilter.java341
-rw-r--r--libjava/classpath/gnu/xml/pipeline/PipelineFactory.java723
-rw-r--r--libjava/classpath/gnu/xml/pipeline/TeeConsumer.java417
-rw-r--r--libjava/classpath/gnu/xml/pipeline/TextConsumer.java117
-rw-r--r--libjava/classpath/gnu/xml/pipeline/ValidationConsumer.java1928
-rw-r--r--libjava/classpath/gnu/xml/pipeline/WellFormednessFilter.java363
-rw-r--r--libjava/classpath/gnu/xml/pipeline/XIncludeFilter.java579
-rw-r--r--libjava/classpath/gnu/xml/pipeline/XsltFilter.java130
-rw-r--r--libjava/classpath/gnu/xml/pipeline/package.html255
-rw-r--r--libjava/classpath/gnu/xml/stream/AttributeImpl.java123
-rw-r--r--libjava/classpath/gnu/xml/stream/BufferedReader.java198
-rw-r--r--libjava/classpath/gnu/xml/stream/CRLFReader.java180
-rw-r--r--libjava/classpath/gnu/xml/stream/CharactersImpl.java119
-rw-r--r--libjava/classpath/gnu/xml/stream/CommentImpl.java91
-rw-r--r--libjava/classpath/gnu/xml/stream/DTDImpl.java114
-rw-r--r--libjava/classpath/gnu/xml/stream/EndDocumentImpl.java70
-rw-r--r--libjava/classpath/gnu/xml/stream/EndElementImpl.java107
-rw-r--r--libjava/classpath/gnu/xml/stream/EntityDeclarationImpl.java163
-rw-r--r--libjava/classpath/gnu/xml/stream/EntityReferenceImpl.java101
-rw-r--r--libjava/classpath/gnu/xml/stream/FilteredEventReader.java114
-rw-r--r--libjava/classpath/gnu/xml/stream/FilteredStreamReader.java90
-rw-r--r--libjava/classpath/gnu/xml/stream/NamespaceImpl.java137
-rw-r--r--libjava/classpath/gnu/xml/stream/NotationDeclarationImpl.java125
-rw-r--r--libjava/classpath/gnu/xml/stream/ProcessingInstructionImpl.java104
-rw-r--r--libjava/classpath/gnu/xml/stream/SAXParser.java1041
-rw-r--r--libjava/classpath/gnu/xml/stream/SAXParserFactory.java104
-rw-r--r--libjava/classpath/gnu/xml/stream/StartDocumentImpl.java143
-rw-r--r--libjava/classpath/gnu/xml/stream/StartElementImpl.java152
-rw-r--r--libjava/classpath/gnu/xml/stream/UnicodeReader.java205
-rw-r--r--libjava/classpath/gnu/xml/stream/XIncludeFilter.java931
-rw-r--r--libjava/classpath/gnu/xml/stream/XMLEventAllocatorImpl.java204
-rw-r--r--libjava/classpath/gnu/xml/stream/XMLEventFactoryImpl.java269
-rw-r--r--libjava/classpath/gnu/xml/stream/XMLEventImpl.java198
-rw-r--r--libjava/classpath/gnu/xml/stream/XMLEventReaderImpl.java157
-rw-r--r--libjava/classpath/gnu/xml/stream/XMLEventWriterImpl.java190
-rw-r--r--libjava/classpath/gnu/xml/stream/XMLInputFactoryImpl.java397
-rw-r--r--libjava/classpath/gnu/xml/stream/XMLOutputFactoryImpl.java187
-rw-r--r--libjava/classpath/gnu/xml/stream/XMLParser.java5434
-rw-r--r--libjava/classpath/gnu/xml/stream/XMLStreamWriterImpl.java1016
-rw-r--r--libjava/classpath/gnu/xml/transform/AbstractNumberNode.java330
-rw-r--r--libjava/classpath/gnu/xml/transform/ApplyImportsNode.java82
-rw-r--r--libjava/classpath/gnu/xml/transform/ApplyTemplatesNode.java219
-rw-r--r--libjava/classpath/gnu/xml/transform/AttributeNode.java244
-rw-r--r--libjava/classpath/gnu/xml/transform/AttributeSet.java66
-rw-r--r--libjava/classpath/gnu/xml/transform/Bindings.java346
-rw-r--r--libjava/classpath/gnu/xml/transform/CallTemplateNode.java156
-rw-r--r--libjava/classpath/gnu/xml/transform/ChooseNode.java88
-rw-r--r--libjava/classpath/gnu/xml/transform/CommentNode.java103
-rw-r--r--libjava/classpath/gnu/xml/transform/CopyNode.java168
-rw-r--r--libjava/classpath/gnu/xml/transform/CopyOfNode.java174
-rw-r--r--libjava/classpath/gnu/xml/transform/CurrentFunction.java103
-rw-r--r--libjava/classpath/gnu/xml/transform/DOMSourceLocator.java84
-rw-r--r--libjava/classpath/gnu/xml/transform/DocumentFunction.java244
-rw-r--r--libjava/classpath/gnu/xml/transform/ElementAvailableFunction.java181
-rw-r--r--libjava/classpath/gnu/xml/transform/ElementNode.java261
-rw-r--r--libjava/classpath/gnu/xml/transform/ErrorListenerErrorHandler.java101
-rw-r--r--libjava/classpath/gnu/xml/transform/ForEachNode.java159
-rw-r--r--libjava/classpath/gnu/xml/transform/FormatNumberFunction.java145
-rw-r--r--libjava/classpath/gnu/xml/transform/FunctionAvailableFunction.java192
-rw-r--r--libjava/classpath/gnu/xml/transform/GenerateIdFunction.java139
-rw-r--r--libjava/classpath/gnu/xml/transform/IfNode.java112
-rw-r--r--libjava/classpath/gnu/xml/transform/Key.java70
-rw-r--r--libjava/classpath/gnu/xml/transform/KeyFunction.java227
-rw-r--r--libjava/classpath/gnu/xml/transform/LiteralNode.java202
-rw-r--r--libjava/classpath/gnu/xml/transform/MessageNode.java110
-rw-r--r--libjava/classpath/gnu/xml/transform/NamespaceProxy.java77
-rw-r--r--libjava/classpath/gnu/xml/transform/NodeNumberNode.java269
-rw-r--r--libjava/classpath/gnu/xml/transform/NumberNode.java88
-rw-r--r--libjava/classpath/gnu/xml/transform/OtherwiseNode.java83
-rw-r--r--libjava/classpath/gnu/xml/transform/ParameterNode.java172
-rw-r--r--libjava/classpath/gnu/xml/transform/ProcessingInstructionNode.java118
-rw-r--r--libjava/classpath/gnu/xml/transform/SAXSerializer.java305
-rw-r--r--libjava/classpath/gnu/xml/transform/SAXTemplatesHandler.java97
-rw-r--r--libjava/classpath/gnu/xml/transform/SAXTransformerHandler.java111
-rw-r--r--libjava/classpath/gnu/xml/transform/SortKey.java239
-rw-r--r--libjava/classpath/gnu/xml/transform/StreamSerializer.java854
-rw-r--r--libjava/classpath/gnu/xml/transform/StrippingInstruction.java73
-rw-r--r--libjava/classpath/gnu/xml/transform/Stylesheet.java1772
-rw-r--r--libjava/classpath/gnu/xml/transform/SystemPropertyFunction.java140
-rw-r--r--libjava/classpath/gnu/xml/transform/Template.java263
-rw-r--r--libjava/classpath/gnu/xml/transform/TemplateNode.java130
-rw-r--r--libjava/classpath/gnu/xml/transform/TemplatesImpl.java95
-rw-r--r--libjava/classpath/gnu/xml/transform/TextNode.java121
-rw-r--r--libjava/classpath/gnu/xml/transform/TransformerFactoryImpl.java438
-rw-r--r--libjava/classpath/gnu/xml/transform/TransformerImpl.java805
-rw-r--r--libjava/classpath/gnu/xml/transform/TransformerOutputProperties.java186
-rw-r--r--libjava/classpath/gnu/xml/transform/URIResolverEntityResolver.java83
-rw-r--r--libjava/classpath/gnu/xml/transform/UnparsedEntityUriFunction.java132
-rw-r--r--libjava/classpath/gnu/xml/transform/ValueOfNode.java141
-rw-r--r--libjava/classpath/gnu/xml/transform/WhenNode.java115
-rw-r--r--libjava/classpath/gnu/xml/transform/WithParam.java127
-rw-r--r--libjava/classpath/gnu/xml/transform/XSLComparator.java118
-rw-r--r--libjava/classpath/gnu/xml/transform/XSLURIResolver.java321
-rw-r--r--libjava/classpath/gnu/xml/transform/package.html77
-rw-r--r--libjava/classpath/gnu/xml/util/DoParse.java302
-rw-r--r--libjava/classpath/gnu/xml/util/DomParser.java804
-rw-r--r--libjava/classpath/gnu/xml/util/Resolver.java263
-rw-r--r--libjava/classpath/gnu/xml/util/SAXNullTransformerFactory.java676
-rw-r--r--libjava/classpath/gnu/xml/util/XCat.java1611
-rw-r--r--libjava/classpath/gnu/xml/util/XHTMLWriter.java114
-rw-r--r--libjava/classpath/gnu/xml/util/XMLWriter.java1931
-rw-r--r--libjava/classpath/gnu/xml/util/package.html20
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/Annotation.java60
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/AnySimpleType.java58
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/AnyType.java57
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/AnyURIType.java93
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/AtomicSimpleType.java77
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/Base64BinaryType.java128
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/BooleanType.java89
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/ByteType.java132
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/DateTimeType.java334
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/DateType.java221
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/DecimalType.java118
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/DoubleType.java110
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/DurationType.java238
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/EntitiesType.java108
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/EntityType.java87
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/EnumerationFacet.java68
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/Facet.java77
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/FloatType.java110
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/FractionDigitsFacet.java71
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/GDayType.java174
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/GMonthDayType.java183
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/GMonthType.java163
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/GYearMonthType.java176
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/GYearType.java151
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/HexBinaryType.java89
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/IDRefType.java86
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/IDRefsType.java86
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/IDType.java86
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/IntType.java132
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/IntegerType.java109
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/LanguageType.java86
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/LengthFacet.java71
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/ListSimpleType.java82
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/LongType.java132
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/MaxExclusiveFacet.java110
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/MaxInclusiveFacet.java111
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/MaxLengthFacet.java71
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/MinExclusiveFacet.java110
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/MinInclusiveFacet.java111
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/MinLengthFacet.java71
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/NCNameType.java110
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/NMTokenType.java101
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/NMTokensType.java125
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/NameType.java103
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/NegativeIntegerType.java110
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/NonNegativeIntegerType.java120
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/NonPositiveIntegerType.java120
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/NormalizedStringType.java87
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/NotationType.java87
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/PatternFacet.java70
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/PositiveIntegerType.java110
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/QNameType.java121
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/ShortType.java132
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/SimpleType.java255
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/StringType.java72
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/TimeType.java302
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/TokenType.java95
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/TotalDigitsFacet.java71
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/Type.java62
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/TypeBuilder.java278
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/TypeLibrary.java173
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/TypeLibraryFactory.java60
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/UnionSimpleType.java82
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/UnsignedByteType.java120
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/UnsignedIntType.java120
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/UnsignedLongType.java120
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/UnsignedShortType.java121
-rw-r--r--libjava/classpath/gnu/xml/validation/datatype/WhiteSpaceFacet.java74
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/AnyNameNameClass.java57
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/AttributePattern.java52
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/ChoiceNameClass.java58
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/ChoicePattern.java52
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/DataPattern.java59
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/Define.java51
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/ElementPattern.java52
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/EmptyPattern.java51
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/FullSyntaxBuilder.java1650
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/Grammar.java69
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/GrammarException.java56
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/GrammarValidator.java97
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/GroupPattern.java52
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/InterleavePattern.java52
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/ListPattern.java51
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/NSNameNameClass.java60
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/NameClass.java50
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/NameNameClass.java59
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/NotAllowedPattern.java51
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/OneOrMorePattern.java51
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/Param.java51
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/Pattern.java47
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/RELAXNGSchemaFactory.java163
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/RefPattern.java51
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/TextPattern.java51
-rw-r--r--libjava/classpath/gnu/xml/validation/relaxng/ValuePattern.java57
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/AnyAttribute.java66
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/AttributeDeclaration.java98
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/AttributeUse.java78
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/ComplexType.java101
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/ElementDeclaration.java124
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/Particle.java60
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/ValidationException.java57
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/XMLSchema.java133
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaAttributeTypeInfo.java98
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaBuilder.java839
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaElementTypeInfo.java91
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaSchemaFactory.java171
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaTypeInfo.java76
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaTypeInfoProvider.java81
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaValidator.java97
-rw-r--r--libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaValidatorHandler.java393
-rw-r--r--libjava/classpath/gnu/xml/xpath/AndExpr.java87
-rw-r--r--libjava/classpath/gnu/xml/xpath/ArithmeticExpr.java170
-rw-r--r--libjava/classpath/gnu/xml/xpath/BooleanFunction.java95
-rw-r--r--libjava/classpath/gnu/xml/xpath/CeilingFunction.java89
-rw-r--r--libjava/classpath/gnu/xml/xpath/ConcatFunction.java115
-rw-r--r--libjava/classpath/gnu/xml/xpath/Constant.java98
-rw-r--r--libjava/classpath/gnu/xml/xpath/ContainsFunction.java93
-rw-r--r--libjava/classpath/gnu/xml/xpath/CountFunction.java89
-rw-r--r--libjava/classpath/gnu/xml/xpath/DocumentOrderComparator.java57
-rw-r--r--libjava/classpath/gnu/xml/xpath/EqualityExpr.java274
-rw-r--r--libjava/classpath/gnu/xml/xpath/Expr.java549
-rw-r--r--libjava/classpath/gnu/xml/xpath/FalseFunction.java72
-rw-r--r--libjava/classpath/gnu/xml/xpath/FloorFunction.java90
-rw-r--r--libjava/classpath/gnu/xml/xpath/Function.java56
-rw-r--r--libjava/classpath/gnu/xml/xpath/FunctionCall.java169
-rw-r--r--libjava/classpath/gnu/xml/xpath/IdFunction.java103
-rw-r--r--libjava/classpath/gnu/xml/xpath/LangFunction.java123
-rw-r--r--libjava/classpath/gnu/xml/xpath/LastFunction.java73
-rw-r--r--libjava/classpath/gnu/xml/xpath/LocalNameFunction.java98
-rw-r--r--libjava/classpath/gnu/xml/xpath/NameFunction.java109
-rw-r--r--libjava/classpath/gnu/xml/xpath/NameTest.java143
-rw-r--r--libjava/classpath/gnu/xml/xpath/NamespaceTest.java120
-rw-r--r--libjava/classpath/gnu/xml/xpath/NamespaceUriFunction.java98
-rw-r--r--libjava/classpath/gnu/xml/xpath/NegativeExpr.java81
-rw-r--r--libjava/classpath/gnu/xml/xpath/NodeTypeTest.java141
-rw-r--r--libjava/classpath/gnu/xml/xpath/NormalizeSpaceFunction.java108
-rw-r--r--libjava/classpath/gnu/xml/xpath/NotFunction.java88
-rw-r--r--libjava/classpath/gnu/xml/xpath/NumberFunction.java103
-rw-r--r--libjava/classpath/gnu/xml/xpath/OrExpr.java87
-rw-r--r--libjava/classpath/gnu/xml/xpath/ParenthesizedExpr.java95
-rw-r--r--libjava/classpath/gnu/xml/xpath/Path.java54
-rw-r--r--libjava/classpath/gnu/xml/xpath/Pattern.java53
-rw-r--r--libjava/classpath/gnu/xml/xpath/PositionFunction.java73
-rw-r--r--libjava/classpath/gnu/xml/xpath/Predicate.java85
-rw-r--r--libjava/classpath/gnu/xml/xpath/RelationalExpr.java107
-rw-r--r--libjava/classpath/gnu/xml/xpath/Root.java91
-rw-r--r--libjava/classpath/gnu/xml/xpath/RoundFunction.java97
-rw-r--r--libjava/classpath/gnu/xml/xpath/Selector.java503
-rw-r--r--libjava/classpath/gnu/xml/xpath/StartsWithFunction.java92
-rw-r--r--libjava/classpath/gnu/xml/xpath/Steps.java260
-rw-r--r--libjava/classpath/gnu/xml/xpath/StringFunction.java119
-rw-r--r--libjava/classpath/gnu/xml/xpath/StringLengthFunction.java92
-rw-r--r--libjava/classpath/gnu/xml/xpath/SubstringAfterFunction.java98
-rw-r--r--libjava/classpath/gnu/xml/xpath/SubstringBeforeFunction.java97
-rw-r--r--libjava/classpath/gnu/xml/xpath/SubstringFunction.java119
-rw-r--r--libjava/classpath/gnu/xml/xpath/SumFunction.java104
-rw-r--r--libjava/classpath/gnu/xml/xpath/Test.java58
-rw-r--r--libjava/classpath/gnu/xml/xpath/TranslateFunction.java135
-rw-r--r--libjava/classpath/gnu/xml/xpath/TrueFunction.java72
-rw-r--r--libjava/classpath/gnu/xml/xpath/UnionExpr.java117
-rw-r--r--libjava/classpath/gnu/xml/xpath/VariableReference.java102
-rw-r--r--libjava/classpath/gnu/xml/xpath/XPathFactoryImpl.java90
-rw-r--r--libjava/classpath/gnu/xml/xpath/XPathImpl.java164
-rw-r--r--libjava/classpath/gnu/xml/xpath/XPathParser.java1475
-rw-r--r--libjava/classpath/gnu/xml/xpath/XPathParser.y794
-rw-r--r--libjava/classpath/gnu/xml/xpath/XPathTokenizer.java594
423 files changed, 97550 insertions, 0 deletions
diff --git a/libjava/classpath/gnu/xml/aelfred2/JAXPFactory.java b/libjava/classpath/gnu/xml/aelfred2/JAXPFactory.java
new file mode 100644
index 000000000..094442765
--- /dev/null
+++ b/libjava/classpath/gnu/xml/aelfred2/JAXPFactory.java
@@ -0,0 +1,230 @@
+/* JAXPFactory.java --
+ Copyright (C) 2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.aelfred2;
+
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+import org.xml.sax.Parser;
+import org.xml.sax.XMLReader;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.helpers.XMLReaderAdapter;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+
+/**
+ * Configurable factory to create an Ælfred2 JAXP parser; required
+ * to bootstrap using JAXP. You should use SAX2 directly where possible,
+ * rather than through JAXP, since that gives you better control.
+ * This class would normally be configured as a platform default factory.
+ *
+ * @author David Brownell
+ */
+public final class JAXPFactory
+ extends SAXParserFactory
+{
+
+ private Hashtable flags = new Hashtable();
+
+ /**
+ * Constructs a factory which normally returns a non-validating
+ * parser.
+ */
+ public JAXPFactory()
+ {
+ }
+
+ public SAXParser newSAXParser()
+ throws ParserConfigurationException, SAXException
+ {
+ JaxpParser jaxp = new JaxpParser();
+ Enumeration e = flags.keys();
+ XMLReader parser = jaxp.getXMLReader();
+
+ parser.setFeature(SAXDriver.FEATURE + "namespaces",
+ isNamespaceAware());
+ parser.setFeature(SAXDriver.FEATURE + "validation",
+ isValidating());
+ // that makes SAX2 feature flags trump JAXP
+
+ while (e.hasMoreElements())
+ {
+ String uri = (String) e.nextElement();
+ Boolean value = (Boolean) flags.get(uri);
+ parser.setFeature(uri, value.booleanValue());
+ }
+
+ return jaxp;
+ }
+
+ // yes, this "feature transfer" mechanism doesn't play well
+
+ public void setFeature(String name, boolean value)
+ throws ParserConfigurationException, SAXNotRecognizedException,
+ SAXNotSupportedException
+ {
+ try
+ {
+ // force "early" detection of errors where possible
+ // (flags can't necessarily be set before parsing)
+ new JaxpParser().getXMLReader().setFeature(name, value);
+
+ flags.put(name, Boolean.valueOf(value));
+ }
+ catch (SAXNotRecognizedException e)
+ {
+ throw new SAXNotRecognizedException(name);
+ }
+ catch (SAXNotSupportedException e)
+ {
+ throw new SAXNotSupportedException(name);
+ }
+ catch (Exception e)
+ {
+ throw new ParserConfigurationException(e.getClass().getName()
+ + ": "
+ + e.getMessage());
+ }
+ }
+
+ public boolean getFeature(String name)
+ throws ParserConfigurationException, SAXNotRecognizedException,
+ SAXNotSupportedException
+ {
+ Boolean value = (Boolean) flags.get(name);
+
+ if (value != null)
+ {
+ return value.booleanValue();
+ }
+ else
+ {
+ try
+ {
+ return new JaxpParser().getXMLReader().getFeature(name);
+ }
+ catch (SAXNotRecognizedException e)
+ {
+ throw new SAXNotRecognizedException(name);
+ }
+ catch (SAXNotSupportedException e)
+ {
+ throw new SAXNotSupportedException(name);
+ }
+ catch (SAXException e)
+ {
+ throw new ParserConfigurationException(e.getClass().getName()
+ + ": "
+ + e.getMessage());
+ }
+ }
+ }
+
+ private static class JaxpParser
+ extends SAXParser
+ {
+
+ private XmlReader ae2 = new XmlReader();
+ private XMLReaderAdapter parser = null;
+
+ JaxpParser()
+ {
+ }
+
+ public void setProperty(String id, Object value)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ ae2.setProperty(id, value);
+ }
+
+ public Object getProperty(String id)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ return ae2.getProperty(id);
+ }
+
+ public Parser getParser()
+ throws SAXException
+ {
+ if (parser == null)
+ {
+ parser = new XMLReaderAdapter(ae2);
+ }
+ return parser;
+ }
+
+ public XMLReader getXMLReader ()
+ throws SAXException
+ {
+ return ae2;
+ }
+
+ public boolean isNamespaceAware()
+ {
+ try
+ {
+ return ae2.getFeature(SAXDriver.FEATURE + "namespaces");
+ }
+ catch (Exception e)
+ {
+ throw new Error();
+ }
+ }
+
+ public boolean isValidating()
+ {
+ try
+ {
+ return ae2.getFeature(SAXDriver.FEATURE + "validation");
+ }
+ catch (Exception e)
+ {
+ throw new Error();
+ }
+ }
+
+ // TODO isXIncludeAware()
+
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/aelfred2/SAXDriver.java b/libjava/classpath/gnu/xml/aelfred2/SAXDriver.java
new file mode 100644
index 000000000..6ce14707b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/aelfred2/SAXDriver.java
@@ -0,0 +1,1609 @@
+/* SAXDriver.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version.
+
+Portions derived from code which carried the following notice:
+
+ Copyright (c) 1997, 1998 by Microstar Software Ltd.
+
+ AElfred is free for both commercial and non-commercial use and
+ redistribution, provided that Microstar's copyright and disclaimer are
+ retained intact. You are free to modify AElfred for your own use and
+ to redistribute AElfred with your modifications, provided that the
+ modifications are clearly documented.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ merchantability or fitness for a particular purpose. Please use it AT
+ YOUR OWN RISK.
+*/
+
+package gnu.xml.aelfred2;
+
+import java.io.*;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Locale;
+import java.util.Stack;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
+
+import org.xml.sax.*;
+import org.xml.sax.ext.*;
+import org.xml.sax.helpers.NamespaceSupport;
+
+
+/**
+ * An enhanced SAX2 version of Microstar's Ælfred XML parser.
+ * The enhancements primarily relate to significant improvements in
+ * conformance to the XML specification, and SAX2 support. Performance
+ * has been improved. See the package level documentation for more
+ * information.
+ *
+ * <table border="1" width='100%' cellpadding='3' cellspacing='0'>
+ * <tr bgcolor='#ccccff'>
+ * <th><font size='+1'>Name</font></th>
+ * <th><font size='+1'>Notes</font></th></tr>
+ *
+ * <tr><td colspan=2><center><em>Features ... URL prefix is
+ * <b>http://xml.org/sax/features/</b></em></center></td></tr>
+ *
+ * <tr><td>(URL)/external-general-entities</td>
+ * <td>Value defaults to <em>true</em></td></tr>
+ * <tr><td>(URL)/external-parameter-entities</td>
+ * <td>Value defaults to <em>true</em></td></tr>
+ * <tr><td>(URL)/is-standalone</td>
+ * <td>(PRELIMINARY) Returns true iff the document's parsing
+ * has started (some non-error event after <em>startDocument()</em>
+ * was reported) and the document's standalone flag is set.</td></tr>
+ * <tr><td>(URL)/namespace-prefixes</td>
+ * <td>Value defaults to <em>false</em> (but XML 1.0 names are
+ * always reported)</td></tr>
+ * <tr><td>(URL)/lexical-handler/parameter-entities</td>
+ * <td>Value is fixed at <em>true</em></td></tr>
+ * <tr><td>(URL)/namespaces</td>
+ * <td>Value defaults to <em>true</em></td></tr>
+ * <tr><td>(URL)/resolve-dtd-uris</td>
+ * <td>(PRELIMINARY) Value defaults to <em>true</em></td></tr>
+ * <tr><td>(URL)/string-interning</td>
+ * <td>Value is fixed at <em>true</em></td></tr>
+ * <tr><td>(URL)/use-attributes2</td>
+ * <td>(PRELIMINARY) Value is fixed at <em>true</em></td></tr>
+ * <tr><td>(URL)/use-entity-resolver2</td>
+ * <td>(PRELIMINARY) Value defaults to <em>true</em></td></tr>
+ * <tr><td>(URL)/validation</td>
+ * <td>Value is fixed at <em>false</em></td></tr>
+ *
+ * <tr><td colspan=2><center><em>Handler Properties ... URL prefix is
+ * <b>http://xml.org/sax/properties/</b></em></center></td></tr>
+ *
+ * <tr><td>(URL)/declaration-handler</td>
+ * <td>A declaration handler may be provided. </td></tr>
+ * <tr><td>(URL)/lexical-handler</td>
+ * <td>A lexical handler may be provided. </td></tr>
+ * </table>
+ *
+ * <p>This parser currently implements the SAX1 Parser API, but
+ * it may not continue to do so in the future.
+ *
+ * @author Written by David Megginson (version 1.2a from Microstar)
+ * @author Updated by David Brownell &lt;dbrownell@users.sourceforge.net&gt;
+ * @see org.xml.sax.Parser
+ */
+final public class SAXDriver
+ implements Locator, Attributes2, XMLReader, Parser, AttributeList
+{
+
+ private final DefaultHandler2 base = new DefaultHandler2();
+ private XmlParser parser;
+
+ private EntityResolver entityResolver = base;
+ private EntityResolver2 resolver2 = null;
+ private ContentHandler contentHandler = base;
+ private DTDHandler dtdHandler = base;
+ private ErrorHandler errorHandler = base;
+ private DeclHandler declHandler = base;
+ private LexicalHandler lexicalHandler = base;
+
+ private String elementName;
+ private Stack entityStack;
+
+ // one vector (of object/struct): faster, smaller
+ private List attributesList;
+
+ private boolean namespaces = true;
+ private boolean xmlNames = false;
+ private boolean extGE = true;
+ private boolean extPE = true;
+ private boolean resolveAll = true;
+ private boolean useResolver2 = true;
+
+ // package private to allow (read-only) access in XmlParser
+ boolean stringInterning = true;
+
+ private int attributeCount;
+ private boolean attributes;
+ private String[] nsTemp;
+ private NamespaceSupport prefixStack;
+
+ //
+ // Constructor.
+ //
+
+ /**
+ * Constructs a SAX Parser.
+ */
+ public SAXDriver()
+ {
+ reset();
+ }
+
+ private void reset()
+ {
+ elementName = null;
+ entityStack = new Stack();
+ attributesList = Collections.synchronizedList(new ArrayList());
+ attributeCount = 0;
+ attributes = false;
+ nsTemp = new String[3];
+ prefixStack = null;
+ }
+
+
+ //
+ // Implementation of org.xml.sax.Parser.
+ //
+
+ /**
+ * <b>SAX1</b>: Sets the locale used for diagnostics; currently,
+ * only locales using the English language are supported.
+ * @param locale The locale for which diagnostics will be generated
+ */
+ public void setLocale(Locale locale)
+ throws SAXException
+ {
+ if ("en".equals(locale.getLanguage()))
+ {
+ return;
+ }
+ throw new SAXException ("AElfred2 only supports English locales.");
+ }
+
+ /**
+ * <b>SAX2</b>: Returns the object used when resolving external
+ * entities during parsing (both general and parameter entities).
+ */
+ public EntityResolver getEntityResolver()
+ {
+ return (entityResolver == base) ? null : entityResolver;
+ }
+
+ /**
+ * <b>SAX1, SAX2</b>: Set the entity resolver for this parser.
+ * @param handler The object to receive entity events.
+ */
+ public void setEntityResolver(EntityResolver resolver)
+ {
+ if (resolver instanceof EntityResolver2)
+ {
+ resolver2 = (EntityResolver2) resolver;
+ }
+ else
+ {
+ resolver2 = null;
+ }
+ if (resolver == null)
+ {
+ resolver = base;
+ }
+ entityResolver = resolver;
+ }
+
+ /**
+ * <b>SAX2</b>: Returns the object used to process declarations related
+ * to notations and unparsed entities.
+ */
+ public DTDHandler getDTDHandler()
+ {
+ return (dtdHandler == base) ? null : dtdHandler;
+ }
+
+ /**
+ * <b>SAX1, SAX2</b>: Set the DTD handler for this parser.
+ * @param handler The object to receive DTD events.
+ */
+ public void setDTDHandler(DTDHandler handler)
+ {
+ if (handler == null)
+ {
+ handler = base;
+ }
+ this.dtdHandler = handler;
+ }
+
+
+ /**
+ * <b>SAX1</b>: Set the document handler for this parser. If a
+ * content handler was set, this document handler will supplant it.
+ * The parser is set to report all XML 1.0 names rather than to
+ * filter out "xmlns" attributes (the "namespace-prefixes" feature
+ * is set to true).
+ *
+ * @deprecated SAX2 programs should use the XMLReader interface
+ * and a ContentHandler.
+ *
+ * @param handler The object to receive document events.
+ */
+ public void setDocumentHandler(DocumentHandler handler)
+ {
+ contentHandler = new Adapter(handler);
+ xmlNames = true;
+ }
+
+ /**
+ * <b>SAX2</b>: Returns the object used to report the logical
+ * content of an XML document.
+ */
+ public ContentHandler getContentHandler()
+ {
+ return (contentHandler == base) ? null : contentHandler;
+ }
+
+ /**
+ * <b>SAX2</b>: Assigns the object used to report the logical
+ * content of an XML document. If a document handler was set,
+ * this content handler will supplant it (but XML 1.0 style name
+ * reporting may remain enabled).
+ */
+ public void setContentHandler(ContentHandler handler)
+ {
+ if (handler == null)
+ {
+ handler = base;
+ }
+ contentHandler = handler;
+ }
+
+ /**
+ * <b>SAX1, SAX2</b>: Set the error handler for this parser.
+ * @param handler The object to receive error events.
+ */
+ public void setErrorHandler(ErrorHandler handler)
+ {
+ if (handler == null)
+ {
+ handler = base;
+ }
+ this.errorHandler = handler;
+ }
+
+ /**
+ * <b>SAX2</b>: Returns the object used to receive callbacks for XML
+ * errors of all levels (fatal, nonfatal, warning); this is never null;
+ */
+ public ErrorHandler getErrorHandler()
+ {
+ return (errorHandler == base) ? null : errorHandler;
+ }
+
+ /**
+ * <b>SAX1, SAX2</b>: Auxiliary API to parse an XML document, used mostly
+ * when no URI is available.
+ * If you want anything useful to happen, you should set
+ * at least one type of handler.
+ * @param source The XML input source. Don't set 'encoding' unless
+ * you know for a fact that it's correct.
+ * @see #setEntityResolver
+ * @see #setDTDHandler
+ * @see #setContentHandler
+ * @see #setErrorHandler
+ * @exception SAXException The handlers may throw any SAXException,
+ * and the parser normally throws SAXParseException objects.
+ * @exception IOException IOExceptions are normally through through
+ * the parser if there are problems reading the source document.
+ */
+ public void parse(InputSource source)
+ throws SAXException, IOException
+ {
+ synchronized (base)
+ {
+ parser = new XmlParser();
+ if (namespaces)
+ {
+ prefixStack = new NamespaceSupport();
+ }
+ else if (!xmlNames)
+ {
+ throw new IllegalStateException();
+ }
+ parser.setHandler(this);
+
+ try
+ {
+ Reader r = source.getCharacterStream();
+ InputStream in = source.getByteStream();
+
+ parser.doParse(source.getSystemId(),
+ source.getPublicId(),
+ r,
+ in,
+ source.getEncoding());
+ }
+ catch (SAXException e)
+ {
+ throw e;
+ }
+ catch (IOException e)
+ {
+ throw e;
+ }
+ catch (RuntimeException e)
+ {
+ throw e;
+ }
+ catch (Exception e)
+ {
+ throw new SAXParseException(e.getMessage(), this, e);
+ }
+ finally
+ {
+ contentHandler.endDocument();
+ reset();
+ }
+ }
+ }
+
+ /**
+ * <b>SAX1, SAX2</b>: Preferred API to parse an XML document, using a
+ * system identifier (URI).
+ */
+ public void parse(String systemId)
+ throws SAXException, IOException
+ {
+ parse(new InputSource(systemId));
+ }
+
+ //
+ // Implementation of SAX2 "XMLReader" interface
+ //
+ static final String FEATURE = "http://xml.org/sax/features/";
+ static final String PROPERTY = "http://xml.org/sax/properties/";
+
+ /**
+ * <b>SAX2</b>: Tells the value of the specified feature flag.
+ *
+ * @exception SAXNotRecognizedException thrown if the feature flag
+ * is neither built in, nor yet assigned.
+ */
+ public boolean getFeature(String featureId)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ if ((FEATURE + "validation").equals(featureId))
+ {
+ return false;
+ }
+
+ // external entities (both types) are optionally included
+ if ((FEATURE + "external-general-entities").equals(featureId))
+ {
+ return extGE;
+ }
+ if ((FEATURE + "external-parameter-entities").equals(featureId))
+ {
+ return extPE;
+ }
+
+ // element/attribute names are as written in document; no mangling
+ if ((FEATURE + "namespace-prefixes").equals(featureId))
+ {
+ return xmlNames;
+ }
+
+ // report element/attribute namespaces?
+ if ((FEATURE + "namespaces").equals(featureId))
+ {
+ return namespaces;
+ }
+
+ // all PEs and GEs are reported
+ if ((FEATURE + "lexical-handler/parameter-entities").equals(featureId))
+ {
+ return true;
+ }
+
+ // default is true
+ if ((FEATURE + "string-interning").equals(featureId))
+ {
+ return stringInterning;
+ }
+
+ // EXTENSIONS 1.1
+
+ // always returns isSpecified info
+ if ((FEATURE + "use-attributes2").equals(featureId))
+ {
+ return true;
+ }
+
+ // meaningful between startDocument/endDocument
+ if ((FEATURE + "is-standalone").equals(featureId))
+ {
+ if (parser == null)
+ {
+ throw new SAXNotSupportedException(featureId);
+ }
+ return parser.isStandalone();
+ }
+
+ // optionally don't absolutize URIs in declarations
+ if ((FEATURE + "resolve-dtd-uris").equals(featureId))
+ {
+ return resolveAll;
+ }
+
+ // optionally use resolver2 interface methods, if possible
+ if ((FEATURE + "use-entity-resolver2").equals(featureId))
+ {
+ return useResolver2;
+ }
+
+ throw new SAXNotRecognizedException(featureId);
+ }
+
+ // package private
+ DeclHandler getDeclHandler()
+ {
+ return declHandler;
+ }
+
+ // package private
+ boolean resolveURIs()
+ {
+ return resolveAll;
+ }
+
+ /**
+ * <b>SAX2</b>: Returns the specified property.
+ *
+ * @exception SAXNotRecognizedException thrown if the property value
+ * is neither built in, nor yet stored.
+ */
+ public Object getProperty(String propertyId)
+ throws SAXNotRecognizedException
+ {
+ if ((PROPERTY + "declaration-handler").equals(propertyId))
+ {
+ return (declHandler == base) ? null : declHandler;
+ }
+
+ if ((PROPERTY + "lexical-handler").equals(propertyId))
+ {
+ return (lexicalHandler == base) ? null : lexicalHandler;
+ }
+
+ // unknown properties
+ throw new SAXNotRecognizedException(propertyId);
+ }
+
+ /**
+ * <b>SAX2</b>: Sets the state of feature flags in this parser. Some
+ * built-in feature flags are mutable.
+ */
+ public void setFeature(String featureId, boolean value)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ boolean state;
+
+ // Features with a defined value, we just change it if we can.
+ state = getFeature (featureId);
+
+ if (state == value)
+ {
+ return;
+ }
+ if (parser != null)
+ {
+ throw new SAXNotSupportedException("not while parsing");
+ }
+
+ if ((FEATURE + "namespace-prefixes").equals(featureId))
+ {
+ // in this implementation, this only affects xmlns reporting
+ xmlNames = value;
+ // forcibly prevent illegal parser state
+ if (!xmlNames)
+ {
+ namespaces = true;
+ }
+ return;
+ }
+
+ if ((FEATURE + "namespaces").equals(featureId))
+ {
+ namespaces = value;
+ // forcibly prevent illegal parser state
+ if (!namespaces)
+ {
+ xmlNames = true;
+ }
+ return;
+ }
+
+ if ((FEATURE + "external-general-entities").equals(featureId))
+ {
+ extGE = value;
+ return;
+ }
+ if ((FEATURE + "external-parameter-entities").equals(featureId))
+ {
+ extPE = value;
+ return;
+ }
+ if ((FEATURE + "resolve-dtd-uris").equals(featureId))
+ {
+ resolveAll = value;
+ return;
+ }
+
+ if ((FEATURE + "use-entity-resolver2").equals(featureId))
+ {
+ useResolver2 = value;
+ return;
+ }
+
+ throw new SAXNotRecognizedException(featureId);
+ }
+
+ /**
+ * <b>SAX2</b>: Assigns the specified property. Like SAX1 handlers,
+ * these may be changed at any time.
+ */
+ public void setProperty(String propertyId, Object value)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ // see if the property is recognized
+ getProperty(propertyId);
+
+ // Properties with a defined value, we just change it if we can.
+
+ if ((PROPERTY + "declaration-handler").equals(propertyId))
+ {
+ if (value == null)
+ {
+ declHandler = base;
+ }
+ else if (!(value instanceof DeclHandler))
+ {
+ throw new SAXNotSupportedException(propertyId);
+ }
+ else
+ {
+ declHandler = (DeclHandler) value;
+ }
+ return ;
+ }
+
+ if ((PROPERTY + "lexical-handler").equals(propertyId))
+ {
+ if (value == null)
+ {
+ lexicalHandler = base;
+ }
+ else if (!(value instanceof LexicalHandler))
+ {
+ throw new SAXNotSupportedException(propertyId);
+ }
+ else
+ {
+ lexicalHandler = (LexicalHandler) value;
+ }
+ return;
+ }
+
+ throw new SAXNotSupportedException(propertyId);
+ }
+
+ //
+ // This is where the driver receives XmlParser callbacks and translates
+ // them into SAX callbacks. Some more callbacks have been added for
+ // SAX2 support.
+ //
+
+ void startDocument()
+ throws SAXException
+ {
+ contentHandler.setDocumentLocator(this);
+ contentHandler.startDocument();
+ attributesList.clear();
+ }
+
+ void skippedEntity(String name)
+ throws SAXException
+ {
+ contentHandler.skippedEntity(name);
+ }
+
+ InputSource getExternalSubset(String name, String baseURI)
+ throws SAXException, IOException
+ {
+ if (resolver2 == null || !useResolver2 || !extPE)
+ {
+ return null;
+ }
+ return resolver2.getExternalSubset(name, baseURI);
+ }
+
+ InputSource resolveEntity(boolean isPE, String name,
+ InputSource in, String baseURI)
+ throws SAXException, IOException
+ {
+ InputSource source;
+
+ // external entities might be skipped
+ if (isPE && !extPE)
+ {
+ return null;
+ }
+ if (!isPE && !extGE)
+ {
+ return null;
+ }
+
+ // ... or not
+ lexicalHandler.startEntity(name);
+ if (resolver2 != null && useResolver2)
+ {
+ source = resolver2.resolveEntity(name, in.getPublicId(),
+ baseURI, in.getSystemId());
+ if (source == null)
+ {
+ in.setSystemId(absolutize(baseURI,
+ in.getSystemId(), false));
+ source = in;
+ }
+ }
+ else
+ {
+ in.setSystemId(absolutize(baseURI,
+ in.getSystemId(),
+ entityResolver != base));
+ source = entityResolver.resolveEntity(in.getPublicId(),
+ in.getSystemId());
+ if (source == null)
+ {
+ source = in;
+ }
+ }
+ startExternalEntity(name, source.getSystemId(), true);
+ return source;
+ }
+
+ // absolutize a system ID relative to the specified base URI
+ // (temporarily) package-visible for external entity decls
+ String absolutize(String baseURI, String systemId, boolean nice)
+ throws MalformedURLException, SAXException
+ {
+ // FIXME normalize system IDs -- when?
+ // - Convert to UTF-8
+ // - Map reserved and non-ASCII characters to %HH
+
+ try
+ {
+ if (baseURI == null)
+ {
+ if (XmlParser.uriWarnings)
+ {
+ warn ("No base URI; hope this SYSTEM id is absolute: "
+ + systemId);
+ }
+ return new URL(systemId).toString();
+ }
+ else
+ {
+ return new URL(new URL(baseURI), systemId).toString();
+ }
+ }
+ catch (MalformedURLException e)
+ {
+ // Let unknown URI schemes pass through unless we need
+ // the JVM to map them to i/o streams for us...
+ if (!nice)
+ {
+ throw e;
+ }
+
+ // sometimes sysids for notations or unparsed entities
+ // aren't really URIs...
+ warn("Can't absolutize SYSTEM id: " + e.getMessage());
+ return systemId;
+ }
+ }
+
+ void startExternalEntity(String name, String systemId, boolean stackOnly)
+ throws SAXException
+ {
+ // The following warning was deleted because the application has the
+ // option of not setting systemId. Sun's JAXP or Xerces seems to
+ // ignore this case.
+ /*
+ if (systemId == null)
+ warn ("URI was not reported to parser for entity " + name);
+ */
+ if (!stackOnly) // spliced [dtd] needs startEntity
+ {
+ lexicalHandler.startEntity(name);
+ }
+ entityStack.push(systemId);
+ }
+
+ void endExternalEntity(String name)
+ throws SAXException
+ {
+ if (!"[document]".equals(name))
+ {
+ lexicalHandler.endEntity(name);
+ }
+ entityStack.pop();
+ }
+
+ void startInternalEntity(String name)
+ throws SAXException
+ {
+ lexicalHandler.startEntity(name);
+ }
+
+ void endInternalEntity(String name)
+ throws SAXException
+ {
+ lexicalHandler.endEntity(name);
+ }
+
+ void doctypeDecl(String name, String publicId, String systemId)
+ throws SAXException
+ {
+ lexicalHandler.startDTD(name, publicId, systemId);
+
+ // ... the "name" is a declaration and should be given
+ // to the DeclHandler (but sax2 doesn't).
+
+ // the IDs for the external subset are lexical details,
+ // as are the contents of the internal subset; but sax2
+ // doesn't provide the internal subset "pre-parse"
+ }
+
+ void notationDecl(String name, String publicId, String systemId,
+ String baseUri)
+ throws SAXException
+ {
+ try
+ {
+ dtdHandler.notationDecl(name, publicId,
+ (resolveAll && systemId != null)
+ ? absolutize(baseUri, systemId, true)
+ : systemId);
+ }
+ catch (IOException e)
+ {
+ // "can't happen"
+ throw new SAXParseException(e.getMessage(), this, e);
+ }
+ }
+
+ void unparsedEntityDecl(String name, String publicId, String systemId,
+ String baseUri, String notation)
+ throws SAXException
+ {
+ try
+ {
+ dtdHandler.unparsedEntityDecl(name, publicId,
+ resolveAll
+ ? absolutize(baseUri, systemId, true)
+ : systemId,
+ notation);
+ }
+ catch (IOException e)
+ {
+ // "can't happen"
+ throw new SAXParseException(e.getMessage(), this, e);
+ }
+ }
+
+ void endDoctype()
+ throws SAXException
+ {
+ lexicalHandler.endDTD();
+ }
+
+ private void declarePrefix(String prefix, String uri)
+ throws SAXException
+ {
+ int index = uri.indexOf(':');
+
+ // many versions of nwalsh docbook stylesheets
+ // have bogus URLs; so this can't be an error...
+ if (index < 1 && uri.length() != 0)
+ {
+ warn("relative URI for namespace: " + uri);
+ }
+
+ // FIXME: char [0] must be ascii alpha; chars [1..index]
+ // must be ascii alphanumeric or in "+-." [RFC 2396]
+
+ //Namespace Constraints
+ //name for xml prefix must be http://www.w3.org/XML/1998/namespace
+ boolean prefixEquality = prefix.equals("xml");
+ boolean uriEquality = uri.equals("http://www.w3.org/XML/1998/namespace");
+ if ((prefixEquality || uriEquality) && !(prefixEquality && uriEquality))
+ {
+ fatal("xml is by definition bound to the namespace name " +
+ "http://www.w3.org/XML/1998/namespace");
+ }
+
+ //xmlns prefix declaration is illegal but xml prefix declaration is llegal...
+ if (prefixEquality && uriEquality)
+ {
+ return;
+ }
+
+ //name for xmlns prefix must be http://www.w3.org/2000/xmlns/
+ prefixEquality = prefix.equals("xmlns");
+ uriEquality = uri.equals("http://www.w3.org/2000/xmlns/");
+ if ((prefixEquality || uriEquality) && !(prefixEquality && uriEquality))
+ {
+ fatal("http://www.w3.org/2000/xmlns/ is by definition bound" +
+ " to prefix xmlns");
+ }
+
+ //even if the uri is http://www.w3.org/2000/xmlns/
+ // it is illegal to declare it
+ if (prefixEquality && uriEquality)
+ {
+ fatal ("declaring the xmlns prefix is illegal");
+ }
+
+ uri = uri.intern();
+ prefixStack.declarePrefix(prefix, uri);
+ contentHandler.startPrefixMapping(prefix, uri);
+ }
+
+ void attribute(String qname, String value, boolean isSpecified)
+ throws SAXException
+ {
+ if (!attributes)
+ {
+ attributes = true;
+ if (namespaces)
+ {
+ prefixStack.pushContext();
+ }
+ }
+
+ // process namespace decls immediately;
+ // then maybe forget this as an attribute
+ if (namespaces)
+ {
+ int index;
+
+ // default NS declaration?
+ if (stringInterning)
+ {
+ if ("xmlns" == qname)
+ {
+ declarePrefix("", value);
+ if (!xmlNames)
+ {
+ return;
+ }
+ }
+ // NS prefix declaration?
+ else if ((index = qname.indexOf(':')) == 5
+ && qname.startsWith("xmlns"))
+ {
+ String prefix = qname.substring(6);
+
+ if (prefix.equals(""))
+ {
+ fatal("missing prefix " +
+ "in namespace declaration attribute");
+ }
+ if (value.length() == 0)
+ {
+ verror("missing URI in namespace declaration attribute: "
+ + qname);
+ }
+ else
+ {
+ declarePrefix(prefix, value);
+ }
+ if (!xmlNames)
+ {
+ return;
+ }
+ }
+ }
+ else
+ {
+ if ("xmlns".equals(qname))
+ {
+ declarePrefix("", value);
+ if (!xmlNames)
+ {
+ return;
+ }
+ }
+ // NS prefix declaration?
+ else if ((index = qname.indexOf(':')) == 5
+ && qname.startsWith("xmlns"))
+ {
+ String prefix = qname.substring(6);
+
+ if (value.length() == 0)
+ {
+ verror("missing URI in namespace decl attribute: "
+ + qname);
+ }
+ else
+ {
+ declarePrefix(prefix, value);
+ }
+ if (!xmlNames)
+ {
+ return;
+ }
+ }
+ }
+ }
+ // remember this attribute ...
+ attributeCount++;
+
+ // attribute type comes from querying parser's DTD records
+ attributesList.add(new Attribute(qname, value, isSpecified));
+
+ }
+
+ void startElement(String elname)
+ throws SAXException
+ {
+ ContentHandler handler = contentHandler;
+
+ //
+ // NOTE: this implementation of namespace support adds something
+ // like six percent to parsing CPU time, in a large (~50 MB)
+ // document that doesn't use namespaces at all. (Measured by PC
+ // sampling, with a bug where endElement processing was omitted.)
+ // [Measurement referred to older implementation, older JVM ...]
+ //
+ // It ought to become notably faster in such cases. Most
+ // costs are the prefix stack calling Hashtable.get() (2%),
+ // String.hashCode() (1.5%) and about 1.3% each for pushing
+ // the context, and two chunks of name processing.
+ //
+
+ if (!attributes)
+ {
+ if (namespaces)
+ {
+ prefixStack.pushContext();
+ }
+ }
+ else if (namespaces)
+ {
+
+ // now we can patch up namespace refs; we saw all the
+ // declarations, so now we'll do the Right Thing
+ Iterator itt = attributesList.iterator();
+ while (itt.hasNext())
+ {
+ Attribute attribute = (Attribute) itt.next();
+ String qname = attribute.name;
+ int index;
+
+ // default NS declaration?
+ if (stringInterning)
+ {
+ if ("xmlns" == qname)
+ {
+ continue;
+ }
+ }
+ else
+ {
+ if ("xmlns".equals(qname))
+ {
+ continue;
+ }
+ }
+ //Illegal in the new Namespaces Draft
+ //should it be only in 1.1 docs??
+ if (qname.equals (":"))
+ {
+ fatal("namespace names consisting of a single colon " +
+ "character are invalid");
+ }
+ index = qname.indexOf(':');
+
+ // NS prefix declaration?
+ if (index == 5 && qname.startsWith("xmlns"))
+ {
+ continue;
+ }
+
+ // it's not a NS decl; patch namespace info items
+ if (prefixStack.processName(qname, nsTemp, true) == null)
+ {
+ fatal("undeclared attribute prefix in: " + qname);
+ }
+ else
+ {
+ attribute.nameSpace = nsTemp[0];
+ attribute.localName = nsTemp[1];
+ }
+ }
+ }
+
+ // save element name so attribute callbacks work
+ elementName = elname;
+ if (namespaces)
+ {
+ if (prefixStack.processName(elname, nsTemp, false) == null)
+ {
+ fatal("undeclared element prefix in: " + elname);
+ nsTemp[0] = nsTemp[1] = "";
+ }
+ handler.startElement(nsTemp[0], nsTemp[1], elname, this);
+ }
+ else
+ {
+ handler.startElement("", "", elname, this);
+ }
+ // elementName = null;
+
+ // elements with no attributes are pretty common!
+ if (attributes)
+ {
+ attributesList.clear();
+ attributeCount = 0;
+ attributes = false;
+ }
+ }
+
+ void endElement(String elname)
+ throws SAXException
+ {
+ ContentHandler handler = contentHandler;
+
+ if (!namespaces)
+ {
+ handler.endElement("", "", elname);
+ return;
+ }
+ prefixStack.processName(elname, nsTemp, false);
+ handler.endElement(nsTemp[0], nsTemp[1], elname);
+
+ Enumeration prefixes = prefixStack.getDeclaredPrefixes();
+
+ while (prefixes.hasMoreElements())
+ {
+ handler.endPrefixMapping((String) prefixes.nextElement());
+ }
+ prefixStack.popContext();
+ }
+
+ void startCDATA()
+ throws SAXException
+ {
+ lexicalHandler.startCDATA();
+ }
+
+ void charData(char[] ch, int start, int length)
+ throws SAXException
+ {
+ contentHandler.characters(ch, start, length);
+ }
+
+ void endCDATA()
+ throws SAXException
+ {
+ lexicalHandler.endCDATA();
+ }
+
+ void ignorableWhitespace(char[] ch, int start, int length)
+ throws SAXException
+ {
+ contentHandler.ignorableWhitespace(ch, start, length);
+ }
+
+ void processingInstruction(String target, String data)
+ throws SAXException
+ {
+ contentHandler.processingInstruction(target, data);
+ }
+
+ void comment(char[] ch, int start, int length)
+ throws SAXException
+ {
+ if (lexicalHandler != base)
+ {
+ lexicalHandler.comment(ch, start, length);
+ }
+ }
+
+ void fatal(String message)
+ throws SAXException
+ {
+ SAXParseException fatal;
+
+ fatal = new SAXParseException(message, this);
+ errorHandler.fatalError(fatal);
+
+ // Even if the application can continue ... we can't!
+ throw fatal;
+ }
+
+ // We can safely report a few validity errors that
+ // make layered SAX2 DTD validation more conformant
+ void verror(String message)
+ throws SAXException
+ {
+ SAXParseException err;
+
+ err = new SAXParseException(message, this);
+ errorHandler.error(err);
+ }
+
+ void warn(String message)
+ throws SAXException
+ {
+ SAXParseException err;
+
+ err = new SAXParseException(message, this);
+ errorHandler.warning(err);
+ }
+
+ //
+ // Implementation of org.xml.sax.Attributes.
+ //
+
+ /**
+ * <b>SAX1 AttributeList, SAX2 Attributes</b> method
+ * (don't invoke on parser);
+ */
+ public int getLength()
+ {
+ return attributesList.size();
+ }
+
+ /**
+ * <b>SAX2 Attributes</b> method (don't invoke on parser);
+ */
+ public String getURI(int index)
+ {
+ if (index < 0 || index >= attributesList.size())
+ {
+ return null;
+ }
+ return ((Attribute) attributesList.get(index)).nameSpace;
+ }
+
+ /**
+ * <b>SAX2 Attributes</b> method (don't invoke on parser);
+ */
+ public String getLocalName(int index)
+ {
+ if (index < 0 || index >= attributesList.size())
+ {
+ return null;
+ }
+ Attribute attr = (Attribute) attributesList.get(index);
+ // FIXME attr.localName is sometimes null, why?
+ if (namespaces && attr.localName == null)
+ {
+ // XXX fix this here for now
+ int ci = attr.name.indexOf(':');
+ attr.localName = (ci == -1) ? attr.name :
+ attr.name.substring(ci + 1);
+ }
+ return (attr.localName == null) ? "" : attr.localName;
+ }
+
+ /**
+ * <b>SAX2 Attributes</b> method (don't invoke on parser);
+ */
+ public String getQName(int index)
+ {
+ if (index < 0 || index >= attributesList.size())
+ {
+ return null;
+ }
+ Attribute attr = (Attribute) attributesList.get(index);
+ return (attr.name == null) ? "" : attr.name;
+ }
+
+ /**
+ * <b>SAX1 AttributeList</b> method (don't invoke on parser);
+ */
+ public String getName(int index)
+ {
+ return getQName(index);
+ }
+
+ /**
+ * <b>SAX1 AttributeList, SAX2 Attributes</b> method
+ * (don't invoke on parser);
+ */
+ public String getType(int index)
+ {
+ if (index < 0 || index >= attributesList.size())
+ {
+ return null;
+ }
+ String type = parser.getAttributeType(elementName, getQName(index));
+ if (type == null)
+ {
+ return "CDATA";
+ }
+ // ... use DeclHandler.attributeDecl to see enumerations
+ if (type == "ENUMERATION")
+ {
+ return "NMTOKEN";
+ }
+ return type;
+ }
+
+ /**
+ * <b>SAX1 AttributeList, SAX2 Attributes</b> method
+ * (don't invoke on parser);
+ */
+ public String getValue(int index)
+ {
+ if (index < 0 || index >= attributesList.size())
+ {
+ return null;
+ }
+ return ((Attribute) attributesList.get(index)).value;
+ }
+
+ /**
+ * <b>SAX2 Attributes</b> method (don't invoke on parser);
+ */
+ public int getIndex(String uri, String local)
+ {
+ int length = getLength();
+
+ for (int i = 0; i < length; i++)
+ {
+ if (!getURI(i).equals(uri))
+ {
+ continue;
+ }
+ if (getLocalName(i).equals(local))
+ {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * <b>SAX2 Attributes</b> method (don't invoke on parser);
+ */
+ public int getIndex(String xmlName)
+ {
+ int length = getLength();
+
+ for (int i = 0; i < length; i++)
+ {
+ if (getQName(i).equals(xmlName))
+ {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * <b>SAX2 Attributes</b> method (don't invoke on parser);
+ */
+ public String getType(String uri, String local)
+ {
+ int index = getIndex(uri, local);
+
+ if (index < 0)
+ {
+ return null;
+ }
+ return getType(index);
+ }
+
+ /**
+ * <b>SAX1 AttributeList, SAX2 Attributes</b> method
+ * (don't invoke on parser);
+ */
+ public String getType(String xmlName)
+ {
+ int index = getIndex(xmlName);
+
+ if (index < 0)
+ {
+ return null;
+ }
+ return getType(index);
+ }
+
+ /**
+ * <b>SAX Attributes</b> method (don't invoke on parser);
+ */
+ public String getValue(String uri, String local)
+ {
+ int index = getIndex(uri, local);
+
+ if (index < 0)
+ {
+ return null;
+ }
+ return getValue(index);
+ }
+
+ /**
+ * <b>SAX1 AttributeList, SAX2 Attributes</b> method
+ * (don't invoke on parser);
+ */
+ public String getValue(String xmlName)
+ {
+ int index = getIndex(xmlName);
+
+ if (index < 0)
+ {
+ return null;
+ }
+ return getValue(index);
+ }
+
+ //
+ // Implementation of org.xml.sax.ext.Attributes2
+ //
+
+ /** @return false unless the attribute was declared in the DTD.
+ * @throws java.lang.ArrayIndexOutOfBoundsException
+ * When the supplied index does not identify an attribute.
+ */
+ public boolean isDeclared(int index)
+ {
+ if (index < 0 || index >= attributeCount)
+ {
+ throw new ArrayIndexOutOfBoundsException();
+ }
+ String type = parser.getAttributeType(elementName, getQName(index));
+ return (type != null);
+ }
+
+ /** @return false unless the attribute was declared in the DTD.
+ * @throws java.lang.IllegalArgumentException
+ * When the supplied names do not identify an attribute.
+ */
+ public boolean isDeclared(String qName)
+ {
+ int index = getIndex(qName);
+ if (index < 0)
+ {
+ throw new IllegalArgumentException();
+ }
+ String type = parser.getAttributeType(elementName, qName);
+ return (type != null);
+ }
+
+ /** @return false unless the attribute was declared in the DTD.
+ * @throws java.lang.IllegalArgumentException
+ * When the supplied names do not identify an attribute.
+ */
+ public boolean isDeclared(String uri, String localName)
+ {
+ int index = getIndex(uri, localName);
+ return isDeclared(index);
+ }
+
+ /**
+ * <b>SAX-ext Attributes2</b> method (don't invoke on parser);
+ */
+ public boolean isSpecified(int index)
+ {
+ return ((Attribute) attributesList.get(index)).specified;
+ }
+
+ /**
+ * <b>SAX-ext Attributes2</b> method (don't invoke on parser);
+ */
+ public boolean isSpecified(String uri, String local)
+ {
+ int index = getIndex (uri, local);
+ return isSpecified(index);
+ }
+
+ /**
+ * <b>SAX-ext Attributes2</b> method (don't invoke on parser);
+ */
+ public boolean isSpecified(String xmlName)
+ {
+ int index = getIndex (xmlName);
+ return isSpecified(index);
+ }
+
+ //
+ // Implementation of org.xml.sax.Locator.
+ //
+
+ /**
+ * <b>SAX Locator</b> method (don't invoke on parser);
+ */
+ public String getPublicId()
+ {
+ return null; // FIXME track public IDs too
+ }
+
+ /**
+ * <b>SAX Locator</b> method (don't invoke on parser);
+ */
+ public String getSystemId()
+ {
+ if (entityStack.empty())
+ {
+ return null;
+ }
+ else
+ {
+ return (String) entityStack.peek();
+ }
+ }
+
+ /**
+ * <b>SAX Locator</b> method (don't invoke on parser);
+ */
+ public int getLineNumber()
+ {
+ return parser.getLineNumber();
+ }
+
+ /**
+ * <b>SAX Locator</b> method (don't invoke on parser);
+ */
+ public int getColumnNumber()
+ {
+ return parser.getColumnNumber();
+ }
+
+ // adapter between SAX2 content handler and SAX1 document handler callbacks
+ private static class Adapter
+ implements ContentHandler
+ {
+
+ private DocumentHandler docHandler;
+
+ Adapter(DocumentHandler dh)
+ {
+ docHandler = dh;
+ }
+
+ public void setDocumentLocator(Locator l)
+ {
+ docHandler.setDocumentLocator(l);
+ }
+
+ public void startDocument()
+ throws SAXException
+ {
+ docHandler.startDocument();
+ }
+
+ public void processingInstruction(String target, String data)
+ throws SAXException
+ {
+ docHandler.processingInstruction(target, data);
+ }
+
+ public void startPrefixMapping(String prefix, String uri)
+ {
+ /* ignored */
+ }
+
+ public void startElement(String namespace,
+ String local,
+ String name,
+ Attributes attrs)
+ throws SAXException
+ {
+ docHandler.startElement(name, (AttributeList) attrs);
+ }
+
+ public void characters(char[] buf, int offset, int len)
+ throws SAXException
+ {
+ docHandler.characters(buf, offset, len);
+ }
+
+ public void ignorableWhitespace(char[] buf, int offset, int len)
+ throws SAXException
+ {
+ docHandler.ignorableWhitespace(buf, offset, len);
+ }
+
+ public void skippedEntity(String name)
+ {
+ /* ignored */
+ }
+
+ public void endElement(String u, String l, String name)
+ throws SAXException
+ {
+ docHandler.endElement(name);
+ }
+
+ public void endPrefixMapping(String prefix)
+ {
+ /* ignored */
+ }
+
+ public void endDocument()
+ throws SAXException
+ {
+ docHandler.endDocument();
+ }
+ }
+
+ private static class Attribute
+ {
+
+ String name;
+ String value;
+ String nameSpace;
+ String localName;
+ boolean specified;
+
+ Attribute(String name, String value, boolean specified)
+ {
+ this.name = name;
+ this.value = value;
+ this.nameSpace = "";
+ this.specified = specified;
+ }
+
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/aelfred2/XmlParser.java b/libjava/classpath/gnu/xml/aelfred2/XmlParser.java
new file mode 100644
index 000000000..813593d93
--- /dev/null
+++ b/libjava/classpath/gnu/xml/aelfred2/XmlParser.java
@@ -0,0 +1,5831 @@
+/* XmlParser.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version.
+
+Partly derived from code which carried the following notice:
+
+ Copyright (c) 1997, 1998 by Microstar Software Ltd.
+
+ AElfred is free for both commercial and non-commercial use and
+ redistribution, provided that Microstar's copyright and disclaimer are
+ retained intact. You are free to modify AElfred for your own use and
+ to redistribute AElfred with your modifications, provided that the
+ modifications are clearly documented.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ merchantability or fitness for a particular purpose. Please use it AT
+ YOUR OWN RISK.
+*/
+
+package gnu.xml.aelfred2;
+
+import gnu.java.security.action.GetPropertyAction;
+
+import java.io.BufferedInputStream;
+import java.io.CharConversionException;
+import java.io.EOFException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.UnsupportedEncodingException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.security.AccessController;
+
+import java.util.Iterator;
+import java.util.HashMap;
+import java.util.LinkedList;
+
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+
+/**
+ * Parse XML documents and return parse events through call-backs.
+ * Use the <code>SAXDriver</code> class as your entry point, as all
+ * internal parser interfaces are subject to change.
+ *
+ * @author Written by David Megginson &lt;dmeggins@microstar.com&gt;
+ * (version 1.2a with bugfixes)
+ * @author Updated by David Brownell &lt;dbrownell@users.sourceforge.net&gt;
+ * @see SAXDriver
+ */
+final class XmlParser
+{
+
+ // avoid slow per-character readCh()
+ private final static boolean USE_CHEATS = true;
+
+ ////////////////////////////////////////////////////////////////////////
+ // Constants.
+ ////////////////////////////////////////////////////////////////////////
+
+ //
+ // Constants for element content type.
+ //
+
+ /**
+ * Constant: an element has not been declared.
+ * @see #getElementContentType
+ */
+ public final static int CONTENT_UNDECLARED = 0;
+
+ /**
+ * Constant: the element has a content model of ANY.
+ * @see #getElementContentType
+ */
+ public final static int CONTENT_ANY = 1;
+
+ /**
+ * Constant: the element has declared content of EMPTY.
+ * @see #getElementContentType
+ */
+ public final static int CONTENT_EMPTY = 2;
+
+ /**
+ * Constant: the element has mixed content.
+ * @see #getElementContentType
+ */
+ public final static int CONTENT_MIXED = 3;
+
+ /**
+ * Constant: the element has element content.
+ * @see #getElementContentType
+ */
+ public final static int CONTENT_ELEMENTS = 4;
+
+
+ //
+ // Constants for the entity type.
+ //
+
+ /**
+ * Constant: the entity has not been declared.
+ * @see #getEntityType
+ */
+ public final static int ENTITY_UNDECLARED = 0;
+
+ /**
+ * Constant: the entity is internal.
+ * @see #getEntityType
+ */
+ public final static int ENTITY_INTERNAL = 1;
+
+ /**
+ * Constant: the entity is external, non-parsable data.
+ * @see #getEntityType
+ */
+ public final static int ENTITY_NDATA = 2;
+
+ /**
+ * Constant: the entity is external XML data.
+ * @see #getEntityType
+ */
+ public final static int ENTITY_TEXT = 3;
+
+ //
+ // Attribute type constants are interned literal strings.
+ //
+
+ //
+ // Constants for supported encodings. "external" is just a flag.
+ //
+ private final static int ENCODING_EXTERNAL = 0;
+ private final static int ENCODING_UTF_8 = 1;
+ private final static int ENCODING_ISO_8859_1 = 2;
+ private final static int ENCODING_UCS_2_12 = 3;
+ private final static int ENCODING_UCS_2_21 = 4;
+ private final static int ENCODING_UCS_4_1234 = 5;
+ private final static int ENCODING_UCS_4_4321 = 6;
+ private final static int ENCODING_UCS_4_2143 = 7;
+ private final static int ENCODING_UCS_4_3412 = 8;
+ private final static int ENCODING_ASCII = 9;
+
+ //
+ // Constants for attribute default value.
+ //
+
+ /**
+ * Constant: the attribute is not declared.
+ * @see #getAttributeDefaultValueType
+ */
+ public final static int ATTRIBUTE_DEFAULT_UNDECLARED = 30;
+
+ /**
+ * Constant: the attribute has a literal default value specified.
+ * @see #getAttributeDefaultValueType
+ * @see #getAttributeDefaultValue
+ */
+ public final static int ATTRIBUTE_DEFAULT_SPECIFIED = 31;
+
+ /**
+ * Constant: the attribute was declared #IMPLIED.
+ * @see #getAttributeDefaultValueType
+ */
+ public final static int ATTRIBUTE_DEFAULT_IMPLIED = 32;
+
+ /**
+ * Constant: the attribute was declared #REQUIRED.
+ * @see #getAttributeDefaultValueType
+ */
+ public final static int ATTRIBUTE_DEFAULT_REQUIRED = 33;
+
+ /**
+ * Constant: the attribute was declared #FIXED.
+ * @see #getAttributeDefaultValueType
+ * @see #getAttributeDefaultValue
+ */
+ public final static int ATTRIBUTE_DEFAULT_FIXED = 34;
+
+ //
+ // Constants for input.
+ //
+ private final static int INPUT_NONE = 0;
+ private final static int INPUT_INTERNAL = 1;
+ private final static int INPUT_STREAM = 3;
+ private final static int INPUT_READER = 5;
+
+ //
+ // Flags for reading literals.
+ //
+ // expand general entity refs (attribute values in dtd and content)
+ private final static int LIT_ENTITY_REF = 2;
+ // normalize this value (space chars) (attributes, public ids)
+ private final static int LIT_NORMALIZE = 4;
+ // literal is an attribute value
+ private final static int LIT_ATTRIBUTE = 8;
+ // don't expand parameter entities
+ private final static int LIT_DISABLE_PE = 16;
+ // don't expand [or parse] character refs
+ private final static int LIT_DISABLE_CREF = 32;
+ // don't parse general entity refs
+ private final static int LIT_DISABLE_EREF = 64;
+ // literal is a public ID value
+ private final static int LIT_PUBID = 256;
+
+ //
+ // Flags affecting PE handling in DTDs (if expandPE is true).
+ // PEs expand with space padding, except inside literals.
+ //
+ private final static int CONTEXT_NORMAL = 0;
+ private final static int CONTEXT_LITERAL = 1;
+
+ // Emit warnings for relative URIs with no base URI.
+ static boolean uriWarnings;
+ static
+ {
+ String key = "gnu.xml.aelfred2.XmlParser.uriWarnings";
+ GetPropertyAction a = new GetPropertyAction(key);
+ uriWarnings = "true".equals(AccessController.doPrivileged(a));
+ }
+
+ //
+ // The current XML handler interface.
+ //
+ private SAXDriver handler;
+
+ //
+ // I/O information.
+ //
+ private Reader reader; // current reader
+ private InputStream is; // current input stream
+ private int line; // current line number
+ private int column; // current column number
+ private int sourceType; // type of input source
+ private LinkedList inputStack; // stack of input soruces
+ private URLConnection externalEntity; // current external entity
+ private int encoding; // current character encoding
+ private int currentByteCount; // bytes read from current source
+ private InputSource scratch; // temporary
+
+ //
+ // Buffers for decoded but unparsed character input.
+ //
+ private char[] readBuffer;
+ private int readBufferPos;
+ private int readBufferLength;
+ private int readBufferOverflow; // overflow from last data chunk.
+
+ //
+ // Buffer for undecoded raw byte input.
+ //
+ private final static int READ_BUFFER_MAX = 16384;
+ private byte[] rawReadBuffer;
+
+
+ //
+ // Buffer for attribute values, char refs, DTD stuff.
+ //
+ private static int DATA_BUFFER_INITIAL = 4096;
+ private char[] dataBuffer;
+ private int dataBufferPos;
+
+ //
+ // Buffer for parsed names.
+ //
+ private static int NAME_BUFFER_INITIAL = 1024;
+ private char[] nameBuffer;
+ private int nameBufferPos;
+
+ //
+ // Save any standalone flag
+ //
+ private boolean docIsStandalone;
+
+ //
+ // Hashtables for DTD information on elements, entities, and notations.
+ // Populated until we start ignoring decls (because of skipping a PE)
+ //
+ private HashMap elementInfo;
+ private HashMap entityInfo;
+ private HashMap notationInfo;
+ private boolean skippedPE;
+
+ //
+ // Element type currently in force.
+ //
+ private String currentElement;
+ private int currentElementContent;
+
+ //
+ // Stack of entity names, to detect recursion.
+ //
+ private LinkedList entityStack;
+
+ //
+ // PE expansion is enabled in most chunks of the DTD, not all.
+ // When it's enabled, literals are treated differently.
+ //
+ private boolean inLiteral;
+ private boolean expandPE;
+ private boolean peIsError;
+
+ //
+ // can't report entity expansion inside two constructs:
+ // - attribute expansions (internal entities only)
+ // - markup declarations (parameter entities only)
+ //
+ private boolean doReport;
+
+ //
+ // Symbol table, for caching interned names.
+ //
+ // These show up wherever XML names or nmtokens are used: naming elements,
+ // attributes, PIs, notations, entities, and enumerated attribute values.
+ //
+ // NOTE: This hashtable doesn't grow. The default size is intended to be
+ // rather large for most documents. Example: one snapshot of the DocBook
+ // XML 4.1 DTD used only about 350 such names. As a rule, only pathological
+ // documents (ones that don't reuse names) should ever see much collision.
+ //
+ // Be sure that SYMBOL_TABLE_LENGTH always stays prime, for best hashing.
+ // "2039" keeps the hash table size at about two memory pages on typical
+ // 32 bit hardware.
+ //
+ private final static int SYMBOL_TABLE_LENGTH = 2039;
+
+ private Object[][] symbolTable;
+
+ //
+ // Hash table of attributes found in current start tag.
+ //
+ private String[] tagAttributes;
+ private int tagAttributePos;
+
+ //
+ // Utility flag: have we noticed a CR while reading the last
+ // data chunk? If so, we will have to go back and normalise
+ // CR or CR/LF line ends.
+ //
+ private boolean sawCR;
+
+ //
+ // Utility flag: are we in CDATA? If so, whitespace isn't ignorable.
+ //
+ private boolean inCDATA;
+
+ //
+ // Xml version.
+ //
+ private static final int XML_10 = 0;
+ private static final int XML_11 = 1;
+ private int xmlVersion = XML_10;
+
+ //////////////////////////////////////////////////////////////////////
+ // Constructors.
+ ////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Construct a new parser with no associated handler.
+ * @see #setHandler
+ * @see #parse
+ */
+ // package private
+ XmlParser()
+ {
+ }
+
+ /**
+ * Set the handler that will receive parsing events.
+ * @param handler The handler to receive callback events.
+ * @see #parse
+ */
+ // package private
+ void setHandler(SAXDriver handler)
+ {
+ this.handler = handler;
+ }
+
+ /**
+ * Parse an XML document from the character stream, byte stream, or URI
+ * that you provide (in that order of preference). Any URI that you
+ * supply will become the base URI for resolving relative URI, and may
+ * be used to acquire a reader or byte stream.
+ *
+ * <p> Only one thread at a time may use this parser; since it is
+ * private to this package, post-parse cleanup is done by the caller,
+ * which MUST NOT REUSE the parser (just null it).
+ *
+ * @param systemId Absolute URI of the document; should never be null,
+ * but may be so iff a reader <em>or</em> a stream is provided.
+ * @param publicId The public identifier of the document, or null.
+ * @param reader A character stream; must be null if stream isn't.
+ * @param stream A byte input stream; must be null if reader isn't.
+ * @param encoding The suggested encoding, or null if unknown.
+ * @exception java.lang.Exception Basically SAXException or IOException
+ */
+ // package private
+ void doParse(String systemId, String publicId, Reader reader,
+ InputStream stream, String encoding)
+ throws Exception
+ {
+ if (handler == null)
+ {
+ throw new IllegalStateException("no callback handler");
+ }
+
+ initializeVariables();
+
+ // predeclare the built-in entities here (replacement texts)
+ // we don't need to intern(), since we're guaranteed literals
+ // are always (globally) interned.
+ setInternalEntity("amp", "&#38;");
+ setInternalEntity("lt", "&#60;");
+ setInternalEntity("gt", "&#62;");
+ setInternalEntity("apos", "&#39;");
+ setInternalEntity("quot", "&#34;");
+
+ try
+ {
+ // pushURL first to ensure locator is correct in startDocument
+ // ... it might report an IO or encoding exception.
+ handler.startDocument();
+ pushURL(false, "[document]",
+ // default baseURI: null
+ new ExternalIdentifiers(publicId, systemId, null),
+ reader, stream, encoding, false);
+
+ parseDocument();
+ }
+ catch (EOFException e)
+ {
+ //empty input
+ error("empty document, with no root element.");
+ }
+ finally
+ {
+ if (reader != null)
+ {
+ try
+ {
+ reader.close();
+ }
+ catch (IOException e)
+ {
+ /* ignore */
+ }
+ }
+ if (stream != null)
+ {
+ try
+ {
+ stream.close();
+ }
+ catch (IOException e)
+ {
+ /* ignore */
+ }
+ }
+ if (is != null)
+ {
+ try
+ {
+ is.close();
+ }
+ catch (IOException e)
+ {
+ /* ignore */
+ }
+ }
+ scratch = null;
+ }
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // Error reporting.
+ //////////////////////////////////////////////////////////////////////
+
+ /**
+ * Report an error.
+ * @param message The error message.
+ * @param textFound The text that caused the error (or null).
+ * @see SAXDriver#error
+ * @see #line
+ */
+ private void error(String message, String textFound, String textExpected)
+ throws SAXException
+ {
+ if (textFound != null)
+ {
+ message = message + " (found \"" + textFound + "\")";
+ }
+ if (textExpected != null)
+ {
+ message = message + " (expected \"" + textExpected + "\")";
+ }
+ handler.fatal(message);
+
+ // "can't happen"
+ throw new SAXException(message);
+ }
+
+ /**
+ * Report a serious error.
+ * @param message The error message.
+ * @param textFound The text that caused the error (or null).
+ */
+ private void error(String message, char textFound, String textExpected)
+ throws SAXException
+ {
+ error(message, Character.toString(textFound), textExpected);
+ }
+
+ /**
+ * Report typical case fatal errors.
+ */
+ private void error(String message)
+ throws SAXException
+ {
+ handler.fatal(message);
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // Major syntactic productions.
+ //////////////////////////////////////////////////////////////////////
+
+ /**
+ * Parse an XML document.
+ * <pre>
+ * [1] document ::= prolog element Misc*
+ * </pre>
+ * <p>This is the top-level parsing function for a single XML
+ * document. As a minimum, a well-formed document must have
+ * a document element, and a valid document must have a prolog
+ * (one with doctype) as well.
+ */
+ private void parseDocument()
+ throws Exception
+ {
+ try
+ { // added by MHK
+ boolean sawDTD = parseProlog();
+ require('<');
+ parseElement(!sawDTD);
+ }
+ catch (EOFException ee)
+ { // added by MHK
+ error("premature end of file", "[EOF]", null);
+ }
+
+ try
+ {
+ parseMisc(); //skip all white, PIs, and comments
+ char c = readCh(); //if this doesn't throw an exception...
+ error("unexpected characters after document end", c, null);
+ }
+ catch (EOFException e)
+ {
+ return;
+ }
+ }
+
+ static final char[] startDelimComment = { '<', '!', '-', '-' };
+ static final char[] endDelimComment = { '-', '-' };
+
+ /**
+ * Skip a comment.
+ * <pre>
+ * [15] Comment ::= '&lt;!--' ((Char - '-') | ('-' (Char - '-')))* "-->"
+ * </pre>
+ * <p> (The <code>&lt;!--</code> has already been read.)
+ */
+ private void parseComment()
+ throws Exception
+ {
+ char c;
+ boolean saved = expandPE;
+
+ expandPE = false;
+ parseUntil(endDelimComment);
+ require('>');
+ expandPE = saved;
+ handler.comment(dataBuffer, 0, dataBufferPos);
+ dataBufferPos = 0;
+ }
+
+ static final char[] startDelimPI = { '<', '?' };
+ static final char[] endDelimPI = { '?', '>' };
+
+ /**
+ * Parse a processing instruction and do a call-back.
+ * <pre>
+ * [16] PI ::= '&lt;?' PITarget
+ * (S (Char* - (Char* '?&gt;' Char*)))?
+ * '?&gt;'
+ * [17] PITarget ::= Name - ( ('X'|'x') ('M'|m') ('L'|l') )
+ * </pre>
+ * <p> (The <code>&lt;?</code> has already been read.)
+ */
+ private void parsePI()
+ throws SAXException, IOException
+ {
+ String name;
+ boolean saved = expandPE;
+
+ expandPE = false;
+ name = readNmtoken(true);
+ //NE08
+ if (name.indexOf(':') >= 0)
+ {
+ error("Illegal character(':') in processing instruction name ",
+ name, null);
+ }
+ if ("xml".equalsIgnoreCase(name))
+ {
+ error("Illegal processing instruction target", name, null);
+ }
+ if (!tryRead(endDelimPI))
+ {
+ requireWhitespace();
+ parseUntil(endDelimPI);
+ }
+ expandPE = saved;
+ handler.processingInstruction(name, dataBufferToString());
+ }
+
+ static final char[] endDelimCDATA = { ']', ']', '>' };
+
+ private boolean isDirtyCurrentElement;
+
+ /**
+ * Parse a CDATA section.
+ * <pre>
+ * [18] CDSect ::= CDStart CData CDEnd
+ * [19] CDStart ::= '&lt;![CDATA['
+ * [20] CData ::= (Char* - (Char* ']]&gt;' Char*))
+ * [21] CDEnd ::= ']]&gt;'
+ * </pre>
+ * <p> (The '&lt;![CDATA[' has already been read.)
+ */
+ private void parseCDSect()
+ throws Exception
+ {
+ parseUntil(endDelimCDATA);
+ dataBufferFlush();
+ }
+
+ /**
+ * Parse the prolog of an XML document.
+ * <pre>
+ * [22] prolog ::= XMLDecl? Misc* (Doctypedecl Misc*)?
+ * </pre>
+ * <p>We do not look for the XML declaration here, because it was
+ * handled by pushURL ().
+ * @see pushURL
+ * @return true if a DTD was read.
+ */
+ private boolean parseProlog()
+ throws Exception
+ {
+ parseMisc();
+
+ if (tryRead("<!DOCTYPE"))
+ {
+ parseDoctypedecl();
+ parseMisc();
+ return true;
+ }
+ return false;
+ }
+
+ private void checkLegalVersion(String version)
+ throws SAXException
+ {
+ int len = version.length();
+ for (int i = 0; i < len; i++)
+ {
+ char c = version.charAt(i);
+ if ('0' <= c && c <= '9')
+ {
+ continue;
+ }
+ if (c == '_' || c == '.' || c == ':' || c == '-')
+ {
+ continue;
+ }
+ if ('a' <= c && c <= 'z')
+ {
+ continue;
+ }
+ if ('A' <= c && c <= 'Z')
+ {
+ continue;
+ }
+ error ("illegal character in version", version, "1.0");
+ }
+ }
+
+ /**
+ * Parse the XML declaration.
+ * <pre>
+ * [23] XMLDecl ::= '&lt;?xml' VersionInfo EncodingDecl? SDDecl? S? '?&gt;'
+ * [24] VersionInfo ::= S 'version' Eq
+ * ("'" VersionNum "'" | '"' VersionNum '"' )
+ * [26] VersionNum ::= ([a-zA-Z0-9_.:] | '-')*
+ * [32] SDDecl ::= S 'standalone' Eq
+ * ( "'"" ('yes' | 'no') "'"" | '"' ("yes" | "no") '"' )
+ * [80] EncodingDecl ::= S 'encoding' Eq
+ * ( "'" EncName "'" | "'" EncName "'" )
+ * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
+ * </pre>
+ * <p> (The <code>&lt;?xml</code> and whitespace have already been read.)
+ * @return the encoding in the declaration, uppercased; or null
+ * @see #parseTextDecl
+ * @see #setupDecoding
+ */
+ private String parseXMLDecl(boolean ignoreEncoding)
+ throws SAXException, IOException
+ {
+ String version;
+ String encodingName = null;
+ String standalone = null;
+ int flags = LIT_DISABLE_CREF | LIT_DISABLE_PE | LIT_DISABLE_EREF;
+ String inputEncoding = null;
+
+ switch (this.encoding)
+ {
+ case ENCODING_EXTERNAL:
+ case ENCODING_UTF_8:
+ inputEncoding = "UTF-8";
+ break;
+ case ENCODING_ISO_8859_1:
+ inputEncoding = "ISO-8859-1";
+ break;
+ case ENCODING_UCS_2_12:
+ inputEncoding = "UTF-16BE";
+ break;
+ case ENCODING_UCS_2_21:
+ inputEncoding = "UTF-16LE";
+ break;
+ }
+
+ // Read the version.
+ require("version");
+ parseEq();
+ checkLegalVersion(version = readLiteral(flags));
+ if (!version.equals("1.0"))
+ {
+ if (version.equals("1.1"))
+ {
+ handler.warn("expected XML version 1.0, not: " + version);
+ xmlVersion = XML_11;
+ }
+ else
+ {
+ error("illegal XML version", version, "1.0 or 1.1");
+ }
+ }
+ else
+ {
+ xmlVersion = XML_10;
+ }
+ // Try reading an encoding declaration.
+ boolean white = tryWhitespace();
+
+ if (tryRead("encoding"))
+ {
+ if (!white)
+ {
+ error("whitespace required before 'encoding='");
+ }
+ parseEq();
+ encodingName = readLiteral(flags);
+ if (!ignoreEncoding)
+ {
+ setupDecoding(encodingName);
+ }
+ }
+
+ // Try reading a standalone declaration
+ if (encodingName != null)
+ {
+ white = tryWhitespace();
+ }
+ if (tryRead("standalone"))
+ {
+ if (!white)
+ {
+ error("whitespace required before 'standalone='");
+ }
+ parseEq();
+ standalone = readLiteral(flags);
+ if ("yes".equals(standalone))
+ {
+ docIsStandalone = true;
+ }
+ else if (!"no".equals(standalone))
+ {
+ error("standalone flag must be 'yes' or 'no'");
+ }
+ }
+
+ skipWhitespace();
+ require("?>");
+
+ if (inputEncoding == null)
+ {
+ inputEncoding = encodingName;
+ }
+ return encodingName;
+ }
+
+ /**
+ * Parse a text declaration.
+ * <pre>
+ * [79] TextDecl ::= '&lt;?xml' VersionInfo? EncodingDecl S? '?&gt;'
+ * [80] EncodingDecl ::= S 'encoding' Eq
+ * ( '"' EncName '"' | "'" EncName "'" )
+ * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
+ * </pre>
+ * <p> (The <code>&lt;?xml</code>' and whitespace have already been read.)
+ * @return the encoding in the declaration, uppercased; or null
+ * @see #parseXMLDecl
+ * @see #setupDecoding
+ */
+ private String parseTextDecl(boolean ignoreEncoding)
+ throws SAXException, IOException
+ {
+ String encodingName = null;
+ int flags = LIT_DISABLE_CREF | LIT_DISABLE_PE | LIT_DISABLE_EREF;
+
+ // Read an optional version.
+ if (tryRead ("version"))
+ {
+ String version;
+ parseEq();
+ checkLegalVersion(version = readLiteral(flags));
+
+ if (version.equals("1.1"))
+ {
+ if (xmlVersion == XML_10)
+ {
+ error("external subset has later version number.", "1.0",
+ version);
+ }
+ handler.warn("expected XML version 1.0, not: " + version);
+ xmlVersion = XML_11;
+ }
+ else if (!version.equals("1.0"))
+ {
+ error("illegal XML version", version, "1.0 or 1.1");
+ }
+ requireWhitespace();
+ }
+
+ // Read the encoding.
+ require("encoding");
+ parseEq();
+ encodingName = readLiteral(flags);
+ if (!ignoreEncoding)
+ {
+ setupDecoding(encodingName);
+ }
+ skipWhitespace();
+ require("?>");
+
+ return encodingName;
+ }
+
+ /**
+ * Sets up internal state so that we can decode an entity using the
+ * specified encoding. This is used when we start to read an entity
+ * and we have been given knowledge of its encoding before we start to
+ * read any data (e.g. from a SAX input source or from a MIME type).
+ *
+ * <p> It is also used after autodetection, at which point only very
+ * limited adjustments to the encoding may be used (switching between
+ * related builtin decoders).
+ *
+ * @param encodingName The name of the encoding specified by the user.
+ * @exception IOException if the encoding isn't supported either
+ * internally to this parser, or by the hosting JVM.
+ * @see #parseXMLDecl
+ * @see #parseTextDecl
+ */
+ private void setupDecoding(String encodingName)
+ throws SAXException, IOException
+ {
+ encodingName = encodingName.toUpperCase();
+
+ // ENCODING_EXTERNAL indicates an encoding that wasn't
+ // autodetected ... we can use builtin decoders, or
+ // ones from the JVM (InputStreamReader).
+
+ // Otherwise we can only tweak what was autodetected, and
+ // only for single byte (ASCII derived) builtin encodings.
+
+ // ASCII-derived encodings
+ if (encoding == ENCODING_UTF_8 || encoding == ENCODING_EXTERNAL)
+ {
+ if (encodingName.equals("ISO-8859-1")
+ || encodingName.equals("8859_1")
+ || encodingName.equals("ISO8859_1"))
+ {
+ encoding = ENCODING_ISO_8859_1;
+ return;
+ }
+ else if (encodingName.equals("US-ASCII")
+ || encodingName.equals("ASCII"))
+ {
+ encoding = ENCODING_ASCII;
+ return;
+ }
+ else if (encodingName.equals("UTF-8")
+ || encodingName.equals("UTF8"))
+ {
+ encoding = ENCODING_UTF_8;
+ return;
+ }
+ else if (encoding != ENCODING_EXTERNAL)
+ {
+ // used to start with a new reader ...
+ throw new UnsupportedEncodingException(encodingName);
+ }
+ // else fallthrough ...
+ // it's ASCII-ish and something other than a builtin
+ }
+
+ // Unicode and such
+ if (encoding == ENCODING_UCS_2_12 || encoding == ENCODING_UCS_2_21)
+ {
+ if (!(encodingName.equals("ISO-10646-UCS-2")
+ || encodingName.equals("UTF-16")
+ || encodingName.equals("UTF-16BE")
+ || encodingName.equals("UTF-16LE")))
+ {
+ error("unsupported Unicode encoding", encodingName, "UTF-16");
+ }
+ return;
+ }
+
+ // four byte encodings
+ if (encoding == ENCODING_UCS_4_1234
+ || encoding == ENCODING_UCS_4_4321
+ || encoding == ENCODING_UCS_4_2143
+ || encoding == ENCODING_UCS_4_3412)
+ {
+ // Strictly: "UCS-4" == "UTF-32BE"; also, "UTF-32LE" exists
+ if (!encodingName.equals("ISO-10646-UCS-4"))
+ {
+ error("unsupported 32-bit encoding", encodingName,
+ "ISO-10646-UCS-4");
+ }
+ return;
+ }
+
+ // assert encoding == ENCODING_EXTERNAL
+ // if (encoding != ENCODING_EXTERNAL)
+ // throw new RuntimeException ("encoding = " + encoding);
+
+ if (encodingName.equals("UTF-16BE"))
+ {
+ encoding = ENCODING_UCS_2_12;
+ return;
+ }
+ if (encodingName.equals("UTF-16LE"))
+ {
+ encoding = ENCODING_UCS_2_21;
+ return;
+ }
+
+ // We couldn't use the builtin decoders at all. But we can try to
+ // create a reader, since we haven't messed up buffering. Tweak
+ // the encoding name if necessary.
+
+ if (encodingName.equals("UTF-16")
+ || encodingName.equals("ISO-10646-UCS-2"))
+ {
+ encodingName = "Unicode";
+ }
+ // Ignoring all the EBCDIC aliases here
+
+ reader = new InputStreamReader(is, encodingName);
+ sourceType = INPUT_READER;
+ }
+
+ /**
+ * Parse miscellaneous markup outside the document element and DOCTYPE
+ * declaration.
+ * <pre>
+ * [27] Misc ::= Comment | PI | S
+ * </pre>
+ */
+ private void parseMisc()
+ throws Exception
+ {
+ while (true)
+ {
+ skipWhitespace();
+ if (tryRead(startDelimPI))
+ {
+ parsePI();
+ }
+ else if (tryRead(startDelimComment))
+ {
+ parseComment();
+ }
+ else
+ {
+ return;
+ }
+ }
+ }
+
+ /**
+ * Parse a document type declaration.
+ * <pre>
+ * [28] doctypedecl ::= '&lt;!DOCTYPE' S Name (S ExternalID)? S?
+ * ('[' (markupdecl | PEReference | S)* ']' S?)? '&gt;'
+ * </pre>
+ * <p> (The <code>&lt;!DOCTYPE</code> has already been read.)
+ */
+ private void parseDoctypedecl()
+ throws Exception
+ {
+ String rootName;
+ ExternalIdentifiers ids;
+
+ // Read the document type name.
+ requireWhitespace();
+ rootName = readNmtoken(true);
+
+ // Read the External subset's IDs
+ skipWhitespace();
+ ids = readExternalIds(false, true);
+
+ // report (a) declaration of name, (b) lexical info (ids)
+ handler.doctypeDecl(rootName, ids.publicId, ids.systemId);
+
+ // Internal subset is parsed first, if present
+ skipWhitespace();
+ if (tryRead('['))
+ {
+
+ // loop until the subset ends
+ while (true)
+ {
+ doReport = expandPE = true;
+ skipWhitespace();
+ doReport = expandPE = false;
+ if (tryRead(']'))
+ {
+ break; // end of subset
+ }
+ else
+ {
+ // WFC, PEs in internal subset (only between decls)
+ peIsError = expandPE = true;
+ parseMarkupdecl();
+ peIsError = expandPE = false;
+ }
+ }
+ }
+ skipWhitespace();
+ require('>');
+
+ // Read the external subset, if any
+ InputSource subset;
+
+ if (ids.systemId == null)
+ {
+ subset = handler.getExternalSubset(rootName,
+ handler.getSystemId());
+ }
+ else
+ {
+ subset = null;
+ }
+ if (ids.systemId != null || subset != null)
+ {
+ pushString(null, ">");
+
+ // NOTE: [dtd] is so we say what SAX2 expects,
+ // though it's misleading (subset, not entire dtd)
+ if (ids.systemId != null)
+ {
+ pushURL(true, "[dtd]", ids, null, null, null, true);
+ }
+ else
+ {
+ handler.warn("modifying document by adding external subset");
+ pushURL(true, "[dtd]",
+ new ExternalIdentifiers(subset.getPublicId(),
+ subset.getSystemId(),
+ null),
+ subset.getCharacterStream(),
+ subset.getByteStream(),
+ subset.getEncoding(),
+ false);
+ }
+
+ // Loop until we end up back at '>'
+ while (true)
+ {
+ doReport = expandPE = true;
+ skipWhitespace();
+ doReport = expandPE = false;
+ if (tryRead('>'))
+ {
+ break;
+ }
+ else
+ {
+ expandPE = true;
+ parseMarkupdecl();
+ expandPE = false;
+ }
+ }
+
+ // the ">" string isn't popped yet
+ if (inputStack.size() != 1)
+ {
+ error("external subset has unmatched '>'");
+ }
+ }
+
+ // done dtd
+ handler.endDoctype();
+ expandPE = false;
+ doReport = true;
+ }
+
+ /**
+ * Parse a markup declaration in the internal or external DTD subset.
+ * <pre>
+ * [29] markupdecl ::= elementdecl | Attlistdecl | EntityDecl
+ * | NotationDecl | PI | Comment
+ * [30] extSubsetDecl ::= (markupdecl | conditionalSect
+ * | PEReference | S) *
+ * </pre>
+ * <p> Reading toplevel PE references is handled as a lexical issue
+ * by the caller, as is whitespace.
+ */
+ private void parseMarkupdecl()
+ throws Exception
+ {
+ char[] saved = null;
+ boolean savedPE = expandPE;
+
+ // prevent "<%foo;" and ensures saved entity is right
+ require('<');
+ unread('<');
+ expandPE = false;
+
+ if (tryRead("<!ELEMENT"))
+ {
+ saved = readBuffer;
+ expandPE = savedPE;
+ parseElementDecl();
+ }
+ else if (tryRead("<!ATTLIST"))
+ {
+ saved = readBuffer;
+ expandPE = savedPE;
+ parseAttlistDecl();
+ }
+ else if (tryRead("<!ENTITY"))
+ {
+ saved = readBuffer;
+ expandPE = savedPE;
+ parseEntityDecl();
+ }
+ else if (tryRead("<!NOTATION"))
+ {
+ saved = readBuffer;
+ expandPE = savedPE;
+ parseNotationDecl();
+ }
+ else if (tryRead(startDelimPI))
+ {
+ saved = readBuffer;
+ expandPE = savedPE;
+ parsePI();
+ }
+ else if (tryRead(startDelimComment))
+ {
+ saved = readBuffer;
+ expandPE = savedPE;
+ parseComment();
+ }
+ else if (tryRead("<!["))
+ {
+ saved = readBuffer;
+ expandPE = savedPE;
+ if (inputStack.size() > 0)
+ {
+ parseConditionalSect(saved);
+ }
+ else
+ {
+ error("conditional sections illegal in internal subset");
+ }
+ }
+ else
+ {
+ error("expected markup declaration");
+ }
+
+ // VC: Proper Decl/PE Nesting
+ if (readBuffer != saved)
+ {
+ handler.verror("Illegal Declaration/PE nesting");
+ }
+ }
+
+ /**
+ * Parse an element, with its tags.
+ * <pre>
+ * [39] element ::= EmptyElementTag | STag content ETag
+ * [40] STag ::= '&lt;' Name (S Attribute)* S? '&gt;'
+ * [44] EmptyElementTag ::= '&lt;' Name (S Attribute)* S? '/&gt;'
+ * </pre>
+ * <p> (The '&lt;' has already been read.)
+ * <p>NOTE: this method actually chains onto parseContent (), if necessary,
+ * and parseContent () will take care of calling parseETag ().
+ */
+ private void parseElement(boolean maybeGetSubset)
+ throws Exception
+ {
+ String gi;
+ char c;
+ int oldElementContent = currentElementContent;
+ String oldElement = currentElement;
+ ElementDecl element;
+
+ // This is the (global) counter for the
+ // array of specified attributes.
+ tagAttributePos = 0;
+
+ // Read the element type name.
+ gi = readNmtoken(true);
+
+ // If we saw no DTD, and this is the document root element,
+ // let the application modify the input stream by providing one.
+ if (maybeGetSubset)
+ {
+ InputSource subset = handler.getExternalSubset(gi,
+ handler.getSystemId());
+ if (subset != null)
+ {
+ String publicId = subset.getPublicId();
+ String systemId = subset.getSystemId();
+
+ handler.warn("modifying document by adding DTD");
+ handler.doctypeDecl(gi, publicId, systemId);
+ pushString(null, ">");
+
+ // NOTE: [dtd] is so we say what SAX2 expects,
+ // though it's misleading (subset, not entire dtd)
+ pushURL(true, "[dtd]",
+ new ExternalIdentifiers(publicId, systemId, null),
+ subset.getCharacterStream(),
+ subset.getByteStream(),
+ subset.getEncoding(),
+ false);
+
+ // Loop until we end up back at '>'
+ while (true)
+ {
+ doReport = expandPE = true;
+ skipWhitespace();
+ doReport = expandPE = false;
+ if (tryRead('>'))
+ {
+ break;
+ }
+ else
+ {
+ expandPE = true;
+ parseMarkupdecl();
+ expandPE = false;
+ }
+ }
+
+ // the ">" string isn't popped yet
+ if (inputStack.size() != 1)
+ {
+ error("external subset has unmatched '>'");
+ }
+
+ handler.endDoctype();
+ }
+ }
+
+ // Determine the current content type.
+ currentElement = gi;
+ element = (ElementDecl) elementInfo.get(gi);
+ currentElementContent = getContentType(element, CONTENT_ANY);
+
+ // Read the attributes, if any.
+ // After this loop, "c" is the closing delimiter.
+ boolean white = tryWhitespace();
+ c = readCh();
+ while (c != '/' && c != '>')
+ {
+ unread(c);
+ if (!white)
+ {
+ error("need whitespace between attributes");
+ }
+ parseAttribute(gi);
+ white = tryWhitespace();
+ c = readCh();
+ }
+
+ // Supply any defaulted attributes.
+ Iterator atts = declaredAttributes(element);
+ if (atts != null)
+ {
+ String aname;
+loop:
+ while (atts.hasNext())
+ {
+ aname = (String) atts.next();
+ // See if it was specified.
+ for (int i = 0; i < tagAttributePos; i++)
+ {
+ if (tagAttributes[i] == aname)
+ {
+ continue loop;
+ }
+ }
+ // ... or has a default
+ String value = getAttributeDefaultValue(gi, aname);
+
+ if (value == null)
+ {
+ continue;
+ }
+ handler.attribute(aname, value, false);
+ }
+ }
+
+ // Figure out if this is a start tag
+ // or an empty element, and dispatch an
+ // event accordingly.
+ switch (c)
+ {
+ case '>':
+ handler.startElement(gi);
+ parseContent();
+ break;
+ case '/':
+ require('>');
+ handler.startElement(gi);
+ handler.endElement(gi);
+ break;
+ }
+
+ // Restore the previous state.
+ currentElement = oldElement;
+ currentElementContent = oldElementContent;
+ }
+
+ /**
+ * Parse an attribute assignment.
+ * <pre>
+ * [41] Attribute ::= Name Eq AttValue
+ * </pre>
+ * @param name The name of the attribute's element.
+ * @see SAXDriver#attribute
+ */
+ private void parseAttribute(String name)
+ throws Exception
+ {
+ String aname;
+ String type;
+ String value;
+ int flags = LIT_ATTRIBUTE | LIT_ENTITY_REF;
+
+ // Read the attribute name.
+ aname = readNmtoken(true);
+ type = getAttributeType(name, aname);
+
+ // Parse '='
+ parseEq();
+
+ // Read the value, normalizing whitespace
+ // unless it is CDATA.
+ if (handler.stringInterning)
+ {
+ if (type == "CDATA" || type == null)
+ {
+ value = readLiteral(flags);
+ }
+ else
+ {
+ value = readLiteral(flags | LIT_NORMALIZE);
+ }
+ }
+ else
+ {
+ if (type == null || type.equals("CDATA"))
+ {
+ value = readLiteral(flags);
+ }
+ else
+ {
+ value = readLiteral(flags | LIT_NORMALIZE);
+ }
+ }
+
+ // WFC: no duplicate attributes
+ for (int i = 0; i < tagAttributePos; i++)
+ {
+ if (aname.equals(tagAttributes [i]))
+ {
+ error("duplicate attribute", aname, null);
+ }
+ }
+
+ // Inform the handler about the
+ // attribute.
+ handler.attribute(aname, value, true);
+ dataBufferPos = 0;
+
+ // Note that the attribute has been
+ // specified.
+ if (tagAttributePos == tagAttributes.length)
+ {
+ String newAttrib[] = new String[tagAttributes.length * 2];
+ System.arraycopy(tagAttributes, 0, newAttrib, 0, tagAttributePos);
+ tagAttributes = newAttrib;
+ }
+ tagAttributes[tagAttributePos++] = aname;
+ }
+
+ /**
+ * Parse an equals sign surrounded by optional whitespace.
+ * <pre>
+ * [25] Eq ::= S? '=' S?
+ * </pre>
+ */
+ private void parseEq()
+ throws SAXException, IOException
+ {
+ skipWhitespace();
+ require('=');
+ skipWhitespace();
+ }
+
+ /**
+ * Parse an end tag.
+ * <pre>
+ * [42] ETag ::= '</' Name S? '>'
+ * </pre>
+ * <p>NOTE: parseContent () chains to here, we already read the
+ * "&lt;/".
+ */
+ private void parseETag()
+ throws Exception
+ {
+ require(currentElement);
+ skipWhitespace();
+ require('>');
+ handler.endElement(currentElement);
+ // not re-reporting any SAXException re bogus end tags,
+ // even though that diagnostic might be clearer ...
+ }
+
+ /**
+ * Parse the content of an element.
+ * <pre>
+ * [43] content ::= (element | CharData | Reference
+ * | CDSect | PI | Comment)*
+ * [67] Reference ::= EntityRef | CharRef
+ * </pre>
+ * <p> NOTE: consumes ETtag.
+ */
+ private void parseContent()
+ throws Exception
+ {
+ char c;
+
+ while (true)
+ {
+ // consume characters (or ignorable whitspace) until delimiter
+ parseCharData();
+
+ // Handle delimiters
+ c = readCh();
+ switch (c)
+ {
+ case '&': // Found "&"
+ c = readCh();
+ if (c == '#')
+ {
+ parseCharRef();
+ }
+ else
+ {
+ unread(c);
+ parseEntityRef(true);
+ }
+ isDirtyCurrentElement = true;
+ break;
+
+ case '<': // Found "<"
+ dataBufferFlush();
+ c = readCh();
+ switch (c)
+ {
+ case '!': // Found "<!"
+ c = readCh();
+ switch (c)
+ {
+ case '-': // Found "<!-"
+ require('-');
+ isDirtyCurrentElement = false;
+ parseComment();
+ break;
+ case '[': // Found "<!["
+ isDirtyCurrentElement = false;
+ require("CDATA[");
+ handler.startCDATA();
+ inCDATA = true;
+ parseCDSect();
+ inCDATA = false;
+ handler.endCDATA();
+ break;
+ default:
+ error("expected comment or CDATA section", c, null);
+ break;
+ }
+ break;
+
+ case '?': // Found "<?"
+ isDirtyCurrentElement = false;
+ parsePI();
+ break;
+
+ case '/': // Found "</"
+ isDirtyCurrentElement = false;
+ parseETag();
+ return;
+
+ default: // Found "<" followed by something else
+ isDirtyCurrentElement = false;
+ unread(c);
+ parseElement(false);
+ break;
+ }
+ }
+ }
+ }
+
+ /**
+ * Parse an element type declaration.
+ * <pre>
+ * [45] elementdecl ::= '&lt;!ELEMENT' S Name S contentspec S? '&gt;'
+ * </pre>
+ * <p> NOTE: the '&lt;!ELEMENT' has already been read.
+ */
+ private void parseElementDecl()
+ throws Exception
+ {
+ String name;
+
+ requireWhitespace();
+ // Read the element type name.
+ name = readNmtoken(true);
+
+ requireWhitespace();
+ // Read the content model.
+ parseContentspec(name);
+
+ skipWhitespace();
+ require('>');
+ }
+
+ /**
+ * Content specification.
+ * <pre>
+ * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | elements
+ * </pre>
+ */
+ private void parseContentspec(String name)
+ throws Exception
+ {
+ // FIXME: move elementDecl() into setElement(), pass EMTPY/ANY ...
+ if (tryRead("EMPTY"))
+ {
+ setElement(name, CONTENT_EMPTY, null, null);
+ if (!skippedPE)
+ {
+ handler.getDeclHandler().elementDecl(name, "EMPTY");
+ }
+ return;
+ }
+ else if (tryRead("ANY"))
+ {
+ setElement(name, CONTENT_ANY, null, null);
+ if (!skippedPE)
+ {
+ handler.getDeclHandler().elementDecl(name, "ANY");
+ }
+ return;
+ }
+ else
+ {
+ String model;
+ char[] saved;
+
+ require('(');
+ saved = readBuffer;
+ dataBufferAppend('(');
+ skipWhitespace();
+ if (tryRead("#PCDATA"))
+ {
+ dataBufferAppend("#PCDATA");
+ parseMixed(saved);
+ model = dataBufferToString();
+ setElement(name, CONTENT_MIXED, model, null);
+ }
+ else
+ {
+ parseElements(saved);
+ model = dataBufferToString();
+ setElement(name, CONTENT_ELEMENTS, model, null);
+ }
+ if (!skippedPE)
+ {
+ handler.getDeclHandler().elementDecl(name, model);
+ }
+ }
+ }
+
+ /**
+ * Parse an element-content model.
+ * <pre>
+ * [47] elements ::= (choice | seq) ('?' | '*' | '+')?
+ * [49] choice ::= '(' S? cp (S? '|' S? cp)+ S? ')'
+ * [50] seq ::= '(' S? cp (S? ',' S? cp)* S? ')'
+ * </pre>
+ *
+ * <p> NOTE: the opening '(' and S have already been read.
+ *
+ * @param saved Buffer for entity that should have the terminal ')'
+ */
+ private void parseElements(char[] saved)
+ throws Exception
+ {
+ char c;
+ char sep;
+
+ // Parse the first content particle
+ skipWhitespace();
+ parseCp();
+
+ // Check for end or for a separator.
+ skipWhitespace();
+ c = readCh();
+ switch (c)
+ {
+ case ')':
+ // VC: Proper Group/PE Nesting
+ if (readBuffer != saved)
+ {
+ handler.verror("Illegal Group/PE nesting");
+ }
+
+ dataBufferAppend(')');
+ c = readCh();
+ switch (c)
+ {
+ case '*':
+ case '+':
+ case '?':
+ dataBufferAppend(c);
+ break;
+ default:
+ unread(c);
+ }
+ return;
+ case ',': // Register the separator.
+ case '|':
+ sep = c;
+ dataBufferAppend(c);
+ break;
+ default:
+ error("bad separator in content model", c, null);
+ return;
+ }
+
+ // Parse the rest of the content model.
+ while (true)
+ {
+ skipWhitespace();
+ parseCp();
+ skipWhitespace();
+ c = readCh();
+ if (c == ')')
+ {
+ // VC: Proper Group/PE Nesting
+ if (readBuffer != saved)
+ {
+ handler.verror("Illegal Group/PE nesting");
+ }
+
+ dataBufferAppend(')');
+ break;
+ }
+ else if (c != sep)
+ {
+ error("bad separator in content model", c, null);
+ return;
+ }
+ else
+ {
+ dataBufferAppend(c);
+ }
+ }
+
+ // Check for the occurrence indicator.
+ c = readCh();
+ switch (c)
+ {
+ case '?':
+ case '*':
+ case '+':
+ dataBufferAppend(c);
+ return;
+ default:
+ unread(c);
+ return;
+ }
+ }
+
+ /**
+ * Parse a content particle.
+ * <pre>
+ * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
+ * </pre>
+ */
+ private void parseCp()
+ throws Exception
+ {
+ if (tryRead('('))
+ {
+ dataBufferAppend('(');
+ parseElements(readBuffer);
+ }
+ else
+ {
+ dataBufferAppend(readNmtoken(true));
+ char c = readCh();
+ switch (c)
+ {
+ case '?':
+ case '*':
+ case '+':
+ dataBufferAppend(c);
+ break;
+ default:
+ unread(c);
+ break;
+ }
+ }
+ }
+
+ /**
+ * Parse mixed content.
+ * <pre>
+ * [51] Mixed ::= '(' S? ( '#PCDATA' (S? '|' S? Name)*) S? ')*'
+ * | '(' S? ('#PCDATA') S? ')'
+ * </pre>
+ *
+ * @param saved Buffer for entity that should have the terminal ')'
+ */
+ private void parseMixed(char[] saved)
+ throws Exception
+ {
+ // Check for PCDATA alone.
+ skipWhitespace();
+ if (tryRead(')'))
+ {
+ // VC: Proper Group/PE Nesting
+ if (readBuffer != saved)
+ {
+ handler.verror("Illegal Group/PE nesting");
+ }
+
+ dataBufferAppend(")*");
+ tryRead('*');
+ return;
+ }
+
+ // Parse mixed content.
+ skipWhitespace();
+ while (!tryRead(")"))
+ {
+ require('|');
+ dataBufferAppend('|');
+ skipWhitespace();
+ dataBufferAppend(readNmtoken(true));
+ skipWhitespace();
+ }
+
+ // VC: Proper Group/PE Nesting
+ if (readBuffer != saved)
+ {
+ handler.verror("Illegal Group/PE nesting");
+ }
+
+ require('*');
+ dataBufferAppend(")*");
+ }
+
+ /**
+ * Parse an attribute list declaration.
+ * <pre>
+ * [52] AttlistDecl ::= '&lt;!ATTLIST' S Name AttDef* S? '&gt;'
+ * </pre>
+ * <p>NOTE: the '&lt;!ATTLIST' has already been read.
+ */
+ private void parseAttlistDecl()
+ throws Exception
+ {
+ String elementName;
+
+ requireWhitespace();
+ elementName = readNmtoken(true);
+ boolean white = tryWhitespace();
+ while (!tryRead('>'))
+ {
+ if (!white)
+ {
+ error("whitespace required before attribute definition");
+ }
+ parseAttDef(elementName);
+ white = tryWhitespace();
+ }
+ }
+
+ /**
+ * Parse a single attribute definition.
+ * <pre>
+ * [53] AttDef ::= S Name S AttType S DefaultDecl
+ * </pre>
+ */
+ private void parseAttDef(String elementName)
+ throws Exception
+ {
+ String name;
+ String type;
+ String enumer = null;
+
+ // Read the attribute name.
+ name = readNmtoken(true);
+
+ // Read the attribute type.
+ requireWhitespace();
+ type = readAttType();
+
+ // Get the string of enumerated values if necessary.
+ if (handler.stringInterning)
+ {
+ if ("ENUMERATION" == type || "NOTATION" == type)
+ {
+ enumer = dataBufferToString();
+ }
+ }
+ else
+ {
+ if ("ENUMERATION".equals(type) || "NOTATION".equals(type))
+ {
+ enumer = dataBufferToString();
+ }
+ }
+
+ // Read the default value.
+ requireWhitespace();
+ parseDefault(elementName, name, type, enumer);
+ }
+
+ /**
+ * Parse the attribute type.
+ * <pre>
+ * [54] AttType ::= StringType | TokenizedType | EnumeratedType
+ * [55] StringType ::= 'CDATA'
+ * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY'
+ * | 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS'
+ * [57] EnumeratedType ::= NotationType | Enumeration
+ * </pre>
+ */
+ private String readAttType()
+ throws Exception
+ {
+ if (tryRead('('))
+ {
+ parseEnumeration(false);
+ return "ENUMERATION";
+ }
+ else
+ {
+ String typeString = readNmtoken(true);
+ if (handler.stringInterning)
+ {
+ if ("NOTATION" == typeString)
+ {
+ parseNotationType();
+ return typeString;
+ }
+ else if ("CDATA" == typeString
+ || "ID" == typeString
+ || "IDREF" == typeString
+ || "IDREFS" == typeString
+ || "ENTITY" == typeString
+ || "ENTITIES" == typeString
+ || "NMTOKEN" == typeString
+ || "NMTOKENS" == typeString)
+ {
+ return typeString;
+ }
+ }
+ else
+ {
+ if ("NOTATION".equals(typeString))
+ {
+ parseNotationType();
+ return typeString;
+ }
+ else if ("CDATA".equals(typeString)
+ || "ID".equals(typeString)
+ || "IDREF".equals(typeString)
+ || "IDREFS".equals(typeString)
+ || "ENTITY".equals(typeString)
+ || "ENTITIES".equals(typeString)
+ || "NMTOKEN".equals(typeString)
+ || "NMTOKENS".equals(typeString))
+ {
+ return typeString;
+ }
+ }
+ error("illegal attribute type", typeString, null);
+ return null;
+ }
+ }
+
+ /**
+ * Parse an enumeration.
+ * <pre>
+ * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
+ * </pre>
+ * <p>NOTE: the '(' has already been read.
+ */
+ private void parseEnumeration(boolean isNames)
+ throws Exception
+ {
+ dataBufferAppend('(');
+
+ // Read the first token.
+ skipWhitespace();
+ dataBufferAppend(readNmtoken(isNames));
+ // Read the remaining tokens.
+ skipWhitespace();
+ while (!tryRead(')'))
+ {
+ require('|');
+ dataBufferAppend('|');
+ skipWhitespace();
+ dataBufferAppend(readNmtoken (isNames));
+ skipWhitespace();
+ }
+ dataBufferAppend(')');
+ }
+
+ /**
+ * Parse a notation type for an attribute.
+ * <pre>
+ * [58] NotationType ::= 'NOTATION' S '(' S? NameNtoks
+ * (S? '|' S? name)* S? ')'
+ * </pre>
+ * <p>NOTE: the 'NOTATION' has already been read
+ */
+ private void parseNotationType()
+ throws Exception
+ {
+ requireWhitespace();
+ require('(');
+
+ parseEnumeration(true);
+ }
+
+ /**
+ * Parse the default value for an attribute.
+ * <pre>
+ * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED'
+ * | (('#FIXED' S)? AttValue)
+ * </pre>
+ */
+ private void parseDefault(String elementName, String name,
+ String type, String enumer)
+ throws Exception
+ {
+ int valueType = ATTRIBUTE_DEFAULT_SPECIFIED;
+ String value = null;
+ int flags = LIT_ATTRIBUTE;
+ boolean saved = expandPE;
+ String defaultType = null;
+
+ // LIT_ATTRIBUTE forces '<' checks now (ASAP) and turns whitespace
+ // chars to spaces (doesn't matter when that's done if it doesn't
+ // interfere with char refs expanding to whitespace).
+
+ if (!skippedPE)
+ {
+ flags |= LIT_ENTITY_REF;
+ if (handler.stringInterning)
+ {
+ if ("CDATA" != type)
+ {
+ flags |= LIT_NORMALIZE;
+ }
+ }
+ else
+ {
+ if (!"CDATA".equals(type))
+ {
+ flags |= LIT_NORMALIZE;
+ }
+ }
+ }
+
+ expandPE = false;
+ if (tryRead('#'))
+ {
+ if (tryRead("FIXED"))
+ {
+ defaultType = "#FIXED";
+ valueType = ATTRIBUTE_DEFAULT_FIXED;
+ requireWhitespace();
+ value = readLiteral(flags);
+ }
+ else if (tryRead("REQUIRED"))
+ {
+ defaultType = "#REQUIRED";
+ valueType = ATTRIBUTE_DEFAULT_REQUIRED;
+ }
+ else if (tryRead("IMPLIED"))
+ {
+ defaultType = "#IMPLIED";
+ valueType = ATTRIBUTE_DEFAULT_IMPLIED;
+ }
+ else
+ {
+ error("illegal keyword for attribute default value");
+ }
+ }
+ else
+ {
+ value = readLiteral(flags);
+ }
+ expandPE = saved;
+ setAttribute(elementName, name, type, enumer, value, valueType);
+ if (handler.stringInterning)
+ {
+ if ("ENUMERATION" == type)
+ {
+ type = enumer;
+ }
+ else if ("NOTATION" == type)
+ {
+ type = "NOTATION " + enumer;
+ }
+ }
+ else
+ {
+ if ("ENUMERATION".equals(type))
+ {
+ type = enumer;
+ }
+ else if ("NOTATION".equals(type))
+ {
+ type = "NOTATION " + enumer;
+ }
+ }
+ if (!skippedPE)
+ {
+ handler.getDeclHandler().attributeDecl(elementName, name, type,
+ defaultType, value);
+ }
+ }
+
+ /**
+ * Parse a conditional section.
+ * <pre>
+ * [61] conditionalSect ::= includeSect || ignoreSect
+ * [62] includeSect ::= '&lt;![' S? 'INCLUDE' S? '['
+ * extSubsetDecl ']]&gt;'
+ * [63] ignoreSect ::= '&lt;![' S? 'IGNORE' S? '['
+ * ignoreSectContents* ']]&gt;'
+ * [64] ignoreSectContents ::= Ignore
+ * ('&lt;![' ignoreSectContents* ']]&gt;' Ignore )*
+ * [65] Ignore ::= Char* - (Char* ( '&lt;![' | ']]&gt;') Char* )
+ * </pre>
+ * <p> NOTE: the '&gt;![' has already been read.
+ */
+ private void parseConditionalSect(char[] saved)
+ throws Exception
+ {
+ skipWhitespace();
+ if (tryRead("INCLUDE"))
+ {
+ skipWhitespace();
+ require('[');
+ // VC: Proper Conditional Section/PE Nesting
+ if (readBuffer != saved)
+ {
+ handler.verror("Illegal Conditional Section/PE nesting");
+ }
+ skipWhitespace();
+ while (!tryRead("]]>"))
+ {
+ parseMarkupdecl();
+ skipWhitespace();
+ }
+ }
+ else if (tryRead("IGNORE"))
+ {
+ skipWhitespace();
+ require('[');
+ // VC: Proper Conditional Section/PE Nesting
+ if (readBuffer != saved)
+ {
+ handler.verror("Illegal Conditional Section/PE nesting");
+ }
+ int nesting = 1;
+ char c;
+ expandPE = false;
+ for (int nest = 1; nest > 0; )
+ {
+ c = readCh();
+ switch (c)
+ {
+ case '<':
+ if (tryRead("!["))
+ {
+ nest++;
+ }
+ break;
+ case ']':
+ if (tryRead("]>"))
+ {
+ nest--;
+ }
+ }
+ }
+ expandPE = true;
+ }
+ else
+ {
+ error("conditional section must begin with INCLUDE or IGNORE");
+ }
+ }
+
+ private void parseCharRef()
+ throws SAXException, IOException
+ {
+ parseCharRef(true /* do flushDataBuffer by default */);
+ }
+
+ /**
+ * Try to read a character reference without consuming data from buffer.
+ * <pre>
+ * [66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
+ * </pre>
+ * <p>NOTE: the '&#' has already been read.
+ */
+ private void tryReadCharRef()
+ throws SAXException, IOException
+ {
+ int value = 0;
+ char c;
+
+ if (tryRead('x'))
+ {
+loop1:
+ while (true)
+ {
+ c = readCh();
+ if (c == ';')
+ {
+ break loop1;
+ }
+ else
+ {
+ int n = Character.digit(c, 16);
+ if (n == -1)
+ {
+ error("illegal character in character reference", c, null);
+ break loop1;
+ }
+ value *= 16;
+ value += n;
+ }
+ }
+ }
+ else
+ {
+loop2:
+ while (true)
+ {
+ c = readCh();
+ if (c == ';')
+ {
+ break loop2;
+ }
+ else
+ {
+ int n = Character.digit(c, 10);
+ if (n == -1)
+ {
+ error("illegal character in character reference", c, null);
+ break loop2;
+ }
+ value *= 10;
+ value += n;
+ }
+ }
+ }
+
+ // check for character refs being legal XML
+ if ((value < 0x0020
+ && ! (value == '\n' || value == '\t' || value == '\r'))
+ || (value >= 0xD800 && value <= 0xDFFF)
+ || value == 0xFFFE || value == 0xFFFF
+ || value > 0x0010ffff)
+ {
+ error("illegal XML character reference U+"
+ + Integer.toHexString(value));
+ }
+
+ // Check for surrogates: 00000000 0000xxxx yyyyyyyy zzzzzzzz
+ // (1101|10xx|xxyy|yyyy + 1101|11yy|zzzz|zzzz:
+ if (value > 0x0010ffff)
+ {
+ // too big for surrogate
+ error("character reference " + value + " is too large for UTF-16",
+ Integer.toString(value), null);
+ }
+
+ }
+
+ /**
+ * Read and interpret a character reference.
+ * <pre>
+ * [66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
+ * </pre>
+ * <p>NOTE: the '&#' has already been read.
+ */
+ private void parseCharRef(boolean doFlush)
+ throws SAXException, IOException
+ {
+ int value = 0;
+ char c;
+
+ if (tryRead('x'))
+ {
+loop1:
+ while (true)
+ {
+ c = readCh();
+ if (c == ';')
+ {
+ break loop1;
+ }
+ else
+ {
+ int n = Character.digit(c, 16);
+ if (n == -1)
+ {
+ error("illegal character in character reference", c, null);
+ break loop1;
+ }
+ value *= 16;
+ value += n;
+ }
+ }
+ }
+ else
+ {
+loop2:
+ while (true)
+ {
+ c = readCh();
+ if (c == ';')
+ {
+ break loop2;
+ }
+ else
+ {
+ int n = Character.digit(c, 10);
+ if (n == -1)
+ {
+ error("illegal character in character reference", c, null);
+ break loop2;
+ }
+ value *= 10;
+ value += c - '0';
+ }
+ }
+ }
+
+ // check for character refs being legal XML
+ if ((value < 0x0020
+ && ! (value == '\n' || value == '\t' || value == '\r'))
+ || (value >= 0xD800 && value <= 0xDFFF)
+ || value == 0xFFFE || value == 0xFFFF
+ || value > 0x0010ffff)
+ {
+ error("illegal XML character reference U+"
+ + Integer.toHexString(value));
+ }
+
+ // Check for surrogates: 00000000 0000xxxx yyyyyyyy zzzzzzzz
+ // (1101|10xx|xxyy|yyyy + 1101|11yy|zzzz|zzzz:
+ if (value <= 0x0000ffff)
+ {
+ // no surrogates needed
+ dataBufferAppend((char) value);
+ }
+ else if (value <= 0x0010ffff)
+ {
+ value -= 0x10000;
+ // > 16 bits, surrogate needed
+ dataBufferAppend((char) (0xd800 | (value >> 10)));
+ dataBufferAppend((char) (0xdc00 | (value & 0x0003ff)));
+ }
+ else
+ {
+ // too big for surrogate
+ error("character reference " + value + " is too large for UTF-16",
+ Integer.toString(value), null);
+ }
+ if (doFlush)
+ {
+ dataBufferFlush();
+ }
+ }
+
+ /**
+ * Parse and expand an entity reference.
+ * <pre>
+ * [68] EntityRef ::= '&' Name ';'
+ * </pre>
+ * <p>NOTE: the '&amp;' has already been read.
+ * @param externalAllowed External entities are allowed here.
+ */
+ private void parseEntityRef(boolean externalAllowed)
+ throws SAXException, IOException
+ {
+ String name;
+
+ name = readNmtoken(true);
+ require(';');
+ switch (getEntityType(name))
+ {
+ case ENTITY_UNDECLARED:
+ // NOTE: XML REC describes amazingly convoluted handling for
+ // this case. Nothing as meaningful as being a WFness error
+ // unless the processor might _legitimately_ not have seen a
+ // declaration ... which is what this implements.
+ String message;
+
+ message = "reference to undeclared general entity " + name;
+ if (skippedPE && !docIsStandalone)
+ {
+ handler.verror(message);
+ // we don't know this entity, and it might be external...
+ if (externalAllowed)
+ {
+ handler.skippedEntity(name);
+ }
+ }
+ else
+ {
+ error(message);
+ }
+ break;
+ case ENTITY_INTERNAL:
+ pushString(name, getEntityValue(name));
+
+ //workaround for possible input pop before marking
+ //the buffer reading position
+ char t = readCh();
+ unread(t);
+ int bufferPosMark = readBufferPos;
+
+ int end = readBufferPos + getEntityValue(name).length();
+ for (int k = readBufferPos; k < end; k++)
+ {
+ t = readCh();
+ if (t == '&')
+ {
+ t = readCh();
+ if (t == '#')
+ {
+ //try to match a character ref
+ tryReadCharRef();
+
+ //everything has been read
+ if (readBufferPos >= end)
+ {
+ break;
+ }
+ k = readBufferPos;
+ continue;
+ }
+ else if (Character.isLetter(t))
+ {
+ //looks like an entity ref
+ unread(t);
+ readNmtoken(true);
+ require(';');
+
+ //everything has been read
+ if (readBufferPos >= end)
+ {
+ break;
+ }
+ k = readBufferPos;
+ continue;
+ }
+ error(" malformed entity reference");
+ }
+
+ }
+ readBufferPos = bufferPosMark;
+ break;
+ case ENTITY_TEXT:
+ if (externalAllowed)
+ {
+ pushURL(false, name, getEntityIds(name),
+ null, null, null, true);
+ }
+ else
+ {
+ error("reference to external entity in attribute value.",
+ name, null);
+ }
+ break;
+ case ENTITY_NDATA:
+ if (externalAllowed)
+ {
+ error("unparsed entity reference in content", name, null);
+ }
+ else
+ {
+ error("reference to external entity in attribute value.",
+ name, null);
+ }
+ break;
+ default:
+ throw new RuntimeException();
+ }
+ }
+
+ /**
+ * Parse and expand a parameter entity reference.
+ * <pre>
+ * [69] PEReference ::= '%' Name ';'
+ * </pre>
+ * <p>NOTE: the '%' has already been read.
+ */
+ private void parsePEReference()
+ throws SAXException, IOException
+ {
+ String name;
+
+ name = "%" + readNmtoken(true);
+ require(';');
+ switch (getEntityType(name))
+ {
+ case ENTITY_UNDECLARED:
+ // VC: Entity Declared
+ handler.verror("reference to undeclared parameter entity " + name);
+
+ // we should disable handling of all subsequent declarations
+ // unless this is a standalone document (info discarded)
+ break;
+ case ENTITY_INTERNAL:
+ if (inLiteral)
+ {
+ pushString(name, getEntityValue(name));
+ }
+ else
+ {
+ pushString(name, ' ' + getEntityValue(name) + ' ');
+ }
+ break;
+ case ENTITY_TEXT:
+ if (!inLiteral)
+ {
+ pushString(null, " ");
+ }
+ pushURL(true, name, getEntityIds(name), null, null, null, true);
+ if (!inLiteral)
+ {
+ pushString(null, " ");
+ }
+ break;
+ }
+ }
+
+ /**
+ * Parse an entity declaration.
+ * <pre>
+ * [70] EntityDecl ::= GEDecl | PEDecl
+ * [71] GEDecl ::= '&lt;!ENTITY' S Name S EntityDef S? '&gt;'
+ * [72] PEDecl ::= '&lt;!ENTITY' S '%' S Name S PEDef S? '&gt;'
+ * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
+ * [74] PEDef ::= EntityValue | ExternalID
+ * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
+ * | 'PUBLIC' S PubidLiteral S SystemLiteral
+ * [76] NDataDecl ::= S 'NDATA' S Name
+ * </pre>
+ * <p>NOTE: the '&lt;!ENTITY' has already been read.
+ */
+ private void parseEntityDecl()
+ throws Exception
+ {
+ boolean peFlag = false;
+ int flags = 0;
+
+ // Check for a parameter entity.
+ expandPE = false;
+ requireWhitespace();
+ if (tryRead('%'))
+ {
+ peFlag = true;
+ requireWhitespace();
+ }
+ expandPE = true;
+
+ // Read the entity name, and prepend
+ // '%' if necessary.
+ String name = readNmtoken(true);
+ //NE08
+ if (name.indexOf(':') >= 0)
+ {
+ error("Illegal character(':') in entity name ", name, null);
+ }
+ if (peFlag)
+ {
+ name = "%" + name;
+ }
+
+ // Read the entity value.
+ requireWhitespace();
+ char c = readCh();
+ unread (c);
+ if (c == '"' || c == '\'')
+ {
+ // Internal entity ... replacement text has expanded refs
+ // to characters and PEs, but not to general entities
+ String value = readLiteral(flags);
+ setInternalEntity(name, value);
+ }
+ else
+ {
+ // Read the external IDs
+ ExternalIdentifiers ids = readExternalIds(false, false);
+
+ // Check for NDATA declaration.
+ boolean white = tryWhitespace();
+ if (!peFlag && tryRead("NDATA"))
+ {
+ if (!white)
+ {
+ error("whitespace required before NDATA");
+ }
+ requireWhitespace();
+ String notationName = readNmtoken(true);
+ if (!skippedPE)
+ {
+ setExternalEntity(name, ENTITY_NDATA, ids, notationName);
+ handler.unparsedEntityDecl(name, ids.publicId, ids.systemId,
+ ids.baseUri, notationName);
+ }
+ }
+ else if (!skippedPE)
+ {
+ setExternalEntity(name, ENTITY_TEXT, ids, null);
+ handler.getDeclHandler()
+ .externalEntityDecl(name, ids.publicId,
+ handler.resolveURIs()
+ // FIXME: ASSUMES not skipped
+ // "false" forces error on bad URI
+ ? handler.absolutize(ids.baseUri,
+ ids.systemId,
+ false)
+ : ids.systemId);
+ }
+ }
+
+ // Finish the declaration.
+ skipWhitespace();
+ require('>');
+ }
+
+ /**
+ * Parse a notation declaration.
+ * <pre>
+ * [82] NotationDecl ::= '&lt;!NOTATION' S Name S
+ * (ExternalID | PublicID) S? '&gt;'
+ * [83] PublicID ::= 'PUBLIC' S PubidLiteral
+ * </pre>
+ * <P>NOTE: the '&lt;!NOTATION' has already been read.
+ */
+ private void parseNotationDecl()
+ throws Exception
+ {
+ String nname;
+ ExternalIdentifiers ids;
+
+ requireWhitespace();
+ nname = readNmtoken(true);
+ //NE08
+ if (nname.indexOf(':') >= 0)
+ {
+ error("Illegal character(':') in notation name ", nname, null);
+ }
+ requireWhitespace();
+
+ // Read the external identifiers.
+ ids = readExternalIds(true, false);
+
+ // Register the notation.
+ setNotation(nname, ids);
+
+ skipWhitespace();
+ require('>');
+ }
+
+ /**
+ * Parse character data.
+ * <pre>
+ * [14] CharData ::= [^&lt;&amp;]* - ([^&lt;&amp;]* ']]&gt;' [^&lt;&amp;]*)
+ * </pre>
+ */
+ private void parseCharData()
+ throws Exception
+ {
+ char c;
+ int state = 0;
+ boolean pureWhite = false;
+
+ // assert (dataBufferPos == 0);
+
+ // are we expecting pure whitespace? it might be dirty...
+ if ((currentElementContent == CONTENT_ELEMENTS) && !isDirtyCurrentElement)
+ {
+ pureWhite = true;
+ }
+
+ // always report right out of readBuffer
+ // to minimize (pointless) buffer copies
+ while (true)
+ {
+ int lineAugment = 0;
+ int columnAugment = 0;
+ int i;
+
+loop:
+ for (i = readBufferPos; i < readBufferLength; i++)
+ {
+ switch (c = readBuffer[i])
+ {
+ case '\n':
+ lineAugment++;
+ columnAugment = 0;
+ // pureWhite unmodified
+ break;
+ case '\r': // should not happen!!
+ case '\t':
+ case ' ':
+ // pureWhite unmodified
+ columnAugment++;
+ break;
+ case '&':
+ case '<':
+ columnAugment++;
+ // pureWhite unmodified
+ // CLEAN end of text sequence
+ state = 1;
+ break loop;
+ case ']':
+ // that's not a whitespace char, and
+ // can not terminate pure whitespace either
+ pureWhite = false;
+ if ((i + 2) < readBufferLength)
+ {
+ if (readBuffer [i + 1] == ']'
+ && readBuffer [i + 2] == '>')
+ {
+ // ERROR end of text sequence
+ state = 2;
+ break loop;
+ }
+ }
+ else
+ {
+ // FIXME missing two end-of-buffer cases
+ }
+ columnAugment++;
+ break;
+ default:
+ if ((c < 0x0020 || c > 0xFFFD)
+ || ((c >= 0x007f) && (c <= 0x009f) && (c != 0x0085)
+ && xmlVersion == XML_11))
+ {
+ error("illegal XML character U+"
+ + Integer.toHexString(c));
+ }
+ // that's not a whitespace char
+ pureWhite = false;
+ columnAugment++;
+ }
+ }
+
+ // report text thus far
+ if (lineAugment > 0)
+ {
+ line += lineAugment;
+ column = columnAugment;
+ }
+ else
+ {
+ column += columnAugment;
+ }
+
+ // report characters/whitspace
+ int length = i - readBufferPos;
+
+ if (length != 0)
+ {
+ if (pureWhite)
+ {
+ handler.ignorableWhitespace(readBuffer,
+ readBufferPos, length);
+ }
+ else
+ {
+ handler.charData(readBuffer, readBufferPos, length);
+ }
+ readBufferPos = i;
+ }
+
+ if (state != 0)
+ {
+ break;
+ }
+
+ // fill next buffer from this entity, or
+ // pop stack and continue with previous entity
+ unread(readCh());
+ }
+ if (!pureWhite)
+ {
+ isDirtyCurrentElement = true;
+ }
+ // finish, maybe with error
+ if (state != 1) // finish, no error
+ {
+ error("character data may not contain ']]>'");
+ }
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // High-level reading and scanning methods.
+ //////////////////////////////////////////////////////////////////////
+
+ /**
+ * Require whitespace characters.
+ */
+ private void requireWhitespace()
+ throws SAXException, IOException
+ {
+ char c = readCh();
+ if (isWhitespace(c))
+ {
+ skipWhitespace();
+ }
+ else
+ {
+ error("whitespace required", c, null);
+ }
+ }
+
+ /**
+ * Skip whitespace characters.
+ * <pre>
+ * [3] S ::= (#x20 | #x9 | #xd | #xa)+
+ * </pre>
+ */
+ private void skipWhitespace()
+ throws SAXException, IOException
+ {
+ // Start with a little cheat. Most of
+ // the time, the white space will fall
+ // within the current read buffer; if
+ // not, then fall through.
+ if (USE_CHEATS)
+ {
+ int lineAugment = 0;
+ int columnAugment = 0;
+
+loop:
+ for (int i = readBufferPos; i < readBufferLength; i++)
+ {
+ switch (readBuffer[i])
+ {
+ case ' ':
+ case '\t':
+ case '\r':
+ columnAugment++;
+ break;
+ case '\n':
+ lineAugment++;
+ columnAugment = 0;
+ break;
+ case '%':
+ if (expandPE)
+ {
+ break loop;
+ }
+ // else fall through...
+ default:
+ readBufferPos = i;
+ if (lineAugment > 0)
+ {
+ line += lineAugment;
+ column = columnAugment;
+ }
+ else
+ {
+ column += columnAugment;
+ }
+ return;
+ }
+ }
+ }
+
+ // OK, do it the slow way.
+ char c = readCh ();
+ while (isWhitespace(c))
+ {
+ c = readCh();
+ }
+ unread(c);
+ }
+
+ /**
+ * Read a name or (when parsing an enumeration) name token.
+ * <pre>
+ * [5] Name ::= (Letter | '_' | ':') (NameChar)*
+ * [7] Nmtoken ::= (NameChar)+
+ * </pre>
+ */
+ private String readNmtoken(boolean isName)
+ throws SAXException, IOException
+ {
+ char c;
+
+ if (USE_CHEATS)
+ {
+loop:
+ for (int i = readBufferPos; i < readBufferLength; i++)
+ {
+ c = readBuffer[i];
+ switch (c)
+ {
+ case '%':
+ if (expandPE)
+ {
+ break loop;
+ }
+ // else fall through...
+
+ // What may legitimately come AFTER a name/nmtoken?
+ case '<': case '>': case '&':
+ case ',': case '|': case '*': case '+': case '?':
+ case ')':
+ case '=':
+ case '\'': case '"':
+ case '[':
+ case ' ': case '\t': case '\r': case '\n':
+ case ';':
+ case '/':
+ int start = readBufferPos;
+ if (i == start)
+ {
+ error("name expected", readBuffer[i], null);
+ }
+ readBufferPos = i;
+ return intern(readBuffer, start, i - start);
+
+ default:
+ // FIXME ... per IBM's OASIS test submission, these:
+ // ? U+06dd
+ // Combining U+309B
+ //these switches are kind of ugly but at least we won't
+ //have to go over the whole lits for each char
+ if (isName && i == readBufferPos)
+ {
+ char c2 = (char) (c & 0x00f0);
+ switch (c & 0xff00)
+ {
+ //starting with 01
+ case 0x0100:
+ switch (c2)
+ {
+ case 0x0030:
+ if (c == 0x0132 || c == 0x0133 || c == 0x013f)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x0040:
+ if (c == 0x0140 || c == 0x0149)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x00c0:
+ if (c == 0x01c4 || c == 0x01cc)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x00f0:
+ if (c == 0x01f1 || c == 0x01f3)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x00b0:
+ if (c == 0x01f1 || c == 0x01f3)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ default:
+ if (c == 0x017f)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ }
+
+ break;
+ //starting with 11
+ case 0x1100:
+ switch (c2)
+ {
+ case 0x0000:
+ if (c == 0x1104 || c == 0x1108 ||
+ c == 0x110a || c == 0x110d)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x0030:
+ if (c == 0x113b || c == 0x113f)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x0040:
+ if (c == 0x1141 || c == 0x114d
+ || c == 0x114f )
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x0050:
+ if (c == 0x1151 || c == 0x1156)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x0060:
+ if (c == 0x1162 || c == 0x1164
+ || c == 0x1166 || c == 0x116b
+ || c == 0x116f)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x00b0:
+ if (c == 0x11b6 || c == 0x11b9
+ || c == 0x11bb || c == 0x116f)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ default:
+ if (c == 0x1174 || c == 0x119f
+ || c == 0x11ac || c == 0x11c3
+ || c == 0x11f1)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ }
+ break;
+ default:
+ if (c == 0x0e46 || c == 0x1011
+ || c == 0x212f || c == 0x0587
+ || c == 0x0230 )
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ }
+ }
+ // punt on exact tests from Appendix A; approximate
+ // them using the Unicode ID start/part rules
+ if (i == readBufferPos && isName)
+ {
+ if (!Character.isUnicodeIdentifierStart(c)
+ && c != ':' && c != '_')
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ }
+ else if (!Character.isUnicodeIdentifierPart(c)
+ && c != '-' && c != ':' && c != '_' && c != '.'
+ && !isExtender(c))
+ {
+ error("Not a name character, U+"
+ + Integer.toHexString(c));
+ }
+ }
+ }
+ }
+
+ nameBufferPos = 0;
+
+ // Read the first character.
+ while (true)
+ {
+ c = readCh();
+ switch (c)
+ {
+ case '%':
+ case '<': case '>': case '&':
+ case ',': case '|': case '*': case '+': case '?':
+ case ')':
+ case '=':
+ case '\'': case '"':
+ case '[':
+ case ' ': case '\t': case '\n': case '\r':
+ case ';':
+ case '/':
+ unread(c);
+ if (nameBufferPos == 0)
+ {
+ error ("name expected");
+ }
+ // punt on exact tests from Appendix A, but approximate them
+ if (isName
+ && !Character.isUnicodeIdentifierStart(nameBuffer[0])
+ && ":_".indexOf(nameBuffer[0]) == -1)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(nameBuffer[0]));
+ }
+ String s = intern(nameBuffer, 0, nameBufferPos);
+ nameBufferPos = 0;
+ return s;
+ default:
+ // punt on exact tests from Appendix A, but approximate them
+
+ if ((nameBufferPos != 0 || !isName)
+ && !Character.isUnicodeIdentifierPart(c)
+ && ":-_.".indexOf(c) == -1
+ && !isExtender(c))
+ {
+ error("Not a name character, U+"
+ + Integer.toHexString(c));
+ }
+ if (nameBufferPos >= nameBuffer.length)
+ {
+ nameBuffer =
+ (char[]) extendArray(nameBuffer,
+ nameBuffer.length, nameBufferPos);
+ }
+ nameBuffer[nameBufferPos++] = c;
+ }
+ }
+ }
+
+ private static boolean isExtender(char c)
+ {
+ // [88] Extender ::= ...
+ return c == 0x00b7 || c == 0x02d0 || c == 0x02d1 || c == 0x0387
+ || c == 0x0640 || c == 0x0e46 || c == 0x0ec6 || c == 0x3005
+ || (c >= 0x3031 && c <= 0x3035)
+ || (c >= 0x309d && c <= 0x309e)
+ || (c >= 0x30fc && c <= 0x30fe);
+ }
+
+ /**
+ * Read a literal. With matching single or double quotes as
+ * delimiters (and not embedded!) this is used to parse:
+ * <pre>
+ * [9] EntityValue ::= ... ([^%&amp;] | PEReference | Reference)* ...
+ * [10] AttValue ::= ... ([^<&] | Reference)* ...
+ * [11] SystemLiteral ::= ... (URLchar - "'")* ...
+ * [12] PubidLiteral ::= ... (PubidChar - "'")* ...
+ * </pre>
+ * as well as the quoted strings in XML and text declarations
+ * (for version, encoding, and standalone) which have their
+ * own constraints.
+ */
+ private String readLiteral(int flags)
+ throws SAXException, IOException
+ {
+ char delim, c;
+ int startLine = line;
+ boolean saved = expandPE;
+ boolean savedReport = doReport;
+
+ // Find the first delimiter.
+ delim = readCh();
+ if (delim != '"' && delim != '\'')
+ {
+ error("expected '\"' or \"'\"", delim, null);
+ return null;
+ }
+ inLiteral = true;
+ if ((flags & LIT_DISABLE_PE) != 0)
+ {
+ expandPE = false;
+ }
+ doReport = false;
+
+ // Each level of input source has its own buffer; remember
+ // ours, so we won't read the ending delimiter from any
+ // other input source, regardless of entity processing.
+ char[] ourBuf = readBuffer;
+
+ // Read the literal.
+ try
+ {
+ c = readCh();
+ boolean ampRead = false;
+loop:
+ while (! (c == delim && readBuffer == ourBuf))
+ {
+ switch (c)
+ {
+ // attributes and public ids are normalized
+ // in almost the same ways
+ case '\n':
+ case '\r':
+ if ((flags & (LIT_ATTRIBUTE | LIT_PUBID)) != 0)
+ {
+ c = ' ';
+ }
+ break;
+ case '\t':
+ if ((flags & LIT_ATTRIBUTE) != 0)
+ {
+ c = ' ';
+ }
+ break;
+ case '&':
+ c = readCh();
+ // Char refs are expanded immediately, except for
+ // all the cases where it's deferred.
+ if (c == '#')
+ {
+ if ((flags & LIT_DISABLE_CREF) != 0)
+ {
+ dataBufferAppend('&');
+ break;
+ }
+ parseCharRef(false /* Do not do flushDataBuffer */);
+
+ // exotic WFness risk: this is an entity literal,
+ // dataBuffer [dataBufferPos - 1] == '&', and
+ // following chars are a _partial_ entity/char ref
+
+ // It looks like an entity ref ...
+ }
+ else
+ {
+ unread(c);
+ // Expand it?
+ if ((flags & LIT_ENTITY_REF) > 0)
+ {
+ parseEntityRef(false);
+ if (String.valueOf(readBuffer).equals("&#38;"))
+ {
+ ampRead = true;
+ }
+ //Is it just data?
+ }
+ else if ((flags & LIT_DISABLE_EREF) != 0)
+ {
+ dataBufferAppend('&');
+
+ // OK, it will be an entity ref -- expanded later.
+ }
+ else
+ {
+ String name = readNmtoken(true);
+ require(';');
+ dataBufferAppend('&');
+ dataBufferAppend(name);
+ dataBufferAppend(';');
+ }
+ }
+ c = readCh();
+ continue loop;
+
+ case '<':
+ // and why? Perhaps so "&foo;" expands the same
+ // inside and outside an attribute?
+ if ((flags & LIT_ATTRIBUTE) != 0)
+ {
+ error("attribute values may not contain '<'");
+ }
+ break;
+
+ // We don't worry about case '%' and PE refs, readCh does.
+
+ default:
+ break;
+ }
+ dataBufferAppend(c);
+ c = readCh();
+ }
+ }
+ catch (EOFException e)
+ {
+ error("end of input while looking for delimiter (started on line "
+ + startLine + ')', null, Character.toString(delim));
+ }
+ inLiteral = false;
+ expandPE = saved;
+ doReport = savedReport;
+
+ // Normalise whitespace if necessary.
+ if ((flags & LIT_NORMALIZE) > 0)
+ {
+ dataBufferNormalize();
+ }
+
+ // Return the value.
+ return dataBufferToString();
+ }
+
+ /**
+ * Try reading external identifiers.
+ * A system identifier is not required for notations.
+ * @param inNotation Are we parsing a notation decl?
+ * @param isSubset Parsing external subset decl (may be omitted)?
+ * @return A three-member String array containing the identifiers,
+ * or nulls. Order: public, system, baseURI.
+ */
+ private ExternalIdentifiers readExternalIds(boolean inNotation,
+ boolean isSubset)
+ throws Exception
+ {
+ char c;
+ ExternalIdentifiers ids = new ExternalIdentifiers();
+ int flags = LIT_DISABLE_CREF | LIT_DISABLE_PE | LIT_DISABLE_EREF;
+
+ if (tryRead("PUBLIC"))
+ {
+ requireWhitespace();
+ ids.publicId = readLiteral(LIT_NORMALIZE | LIT_PUBID | flags);
+ if (inNotation)
+ {
+ skipWhitespace();
+ c = readCh();
+ unread(c);
+ if (c == '"' || c == '\'')
+ {
+ ids.systemId = readLiteral(flags);
+ }
+ }
+ else
+ {
+ requireWhitespace();
+ ids.systemId = readLiteral(flags);
+ }
+
+ for (int i = 0; i < ids.publicId.length(); i++)
+ {
+ c = ids.publicId.charAt(i);
+ if (c >= 'a' && c <= 'z')
+ {
+ continue;
+ }
+ if (c >= 'A' && c <= 'Z')
+ {
+ continue;
+ }
+ if (" \r\n0123456789-' ()+,./:=?;!*#@$_%".indexOf(c) != -1)
+ {
+ continue;
+ }
+ error("illegal PUBLIC id character U+"
+ + Integer.toHexString(c));
+ }
+ }
+ else if (tryRead("SYSTEM"))
+ {
+ requireWhitespace();
+ ids.systemId = readLiteral(flags);
+ }
+ else if (!isSubset)
+ {
+ error("missing SYSTEM or PUBLIC keyword");
+ }
+
+ if (ids.systemId != null)
+ {
+ if (ids.systemId.indexOf('#') != -1)
+ {
+ handler.verror("SYSTEM id has a URI fragment: " + ids.systemId);
+ }
+ ids.baseUri = handler.getSystemId();
+ if (ids.baseUri == null && uriWarnings)
+ {
+ handler.warn("No base URI; hope URI is absolute: "
+ + ids.systemId);
+ }
+ }
+
+ return ids;
+ }
+
+ /**
+ * Test if a character is whitespace.
+ * <pre>
+ * [3] S ::= (#x20 | #x9 | #xd | #xa)+
+ * </pre>
+ * @param c The character to test.
+ * @return true if the character is whitespace.
+ */
+ private final boolean isWhitespace(char c)
+ {
+ if (c > 0x20)
+ {
+ return false;
+ }
+ if (c == 0x20 || c == 0x0a || c == 0x09 || c == 0x0d)
+ {
+ return true;
+ }
+ return false; // illegal ...
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // Utility routines.
+ //////////////////////////////////////////////////////////////////////
+
+ /**
+ * Add a character to the data buffer.
+ */
+ private void dataBufferAppend(char c)
+ {
+ // Expand buffer if necessary.
+ if (dataBufferPos >= dataBuffer.length)
+ {
+ dataBuffer = (char[]) extendArray(dataBuffer,
+ dataBuffer.length, dataBufferPos);
+ }
+ dataBuffer[dataBufferPos++] = c;
+ }
+
+ /**
+ * Add a string to the data buffer.
+ */
+ private void dataBufferAppend(String s)
+ {
+ dataBufferAppend(s.toCharArray(), 0, s.length());
+ }
+
+ /**
+ * Append (part of) a character array to the data buffer.
+ */
+ private void dataBufferAppend(char[] ch, int start, int length)
+ {
+ dataBuffer = (char[]) extendArray(dataBuffer, dataBuffer.length,
+ dataBufferPos + length);
+
+ System.arraycopy(ch, start, dataBuffer, dataBufferPos, length);
+ dataBufferPos += length;
+ }
+
+ /**
+ * Normalise space characters in the data buffer.
+ */
+ private void dataBufferNormalize()
+ {
+ int i = 0;
+ int j = 0;
+ int end = dataBufferPos;
+
+ // Skip spaces at the start.
+ while (j < end && dataBuffer[j] == ' ')
+ {
+ j++;
+ }
+
+ // Skip whitespace at the end.
+ while (end > j && dataBuffer[end - 1] == ' ')
+ {
+ end --;
+ }
+
+ // Start copying to the left.
+ while (j < end)
+ {
+
+ char c = dataBuffer[j++];
+
+ // Normalise all other spaces to
+ // a single space.
+ if (c == ' ')
+ {
+ while (j < end && dataBuffer[j++] == ' ')
+ {
+ continue;
+ }
+ dataBuffer[i++] = ' ';
+ dataBuffer[i++] = dataBuffer[j - 1];
+ }
+ else
+ {
+ dataBuffer[i++] = c;
+ }
+ }
+
+ // The new length is <= the old one.
+ dataBufferPos = i;
+ }
+
+ /**
+ * Convert the data buffer to a string.
+ */
+ private String dataBufferToString()
+ {
+ String s = new String(dataBuffer, 0, dataBufferPos);
+ dataBufferPos = 0;
+ return s;
+ }
+
+ /**
+ * Flush the contents of the data buffer to the handler, as
+ * appropriate, and reset the buffer for new input.
+ */
+ private void dataBufferFlush()
+ throws SAXException
+ {
+ if (currentElementContent == CONTENT_ELEMENTS
+ && dataBufferPos > 0
+ && !inCDATA)
+ {
+ // We can't just trust the buffer to be whitespace, there
+ // are (error) cases when it isn't
+ for (int i = 0; i < dataBufferPos; i++)
+ {
+ if (!isWhitespace(dataBuffer[i]))
+ {
+ handler.charData(dataBuffer, 0, dataBufferPos);
+ dataBufferPos = 0;
+ }
+ }
+ if (dataBufferPos > 0)
+ {
+ handler.ignorableWhitespace(dataBuffer, 0, dataBufferPos);
+ dataBufferPos = 0;
+ }
+ }
+ else if (dataBufferPos > 0)
+ {
+ handler.charData(dataBuffer, 0, dataBufferPos);
+ dataBufferPos = 0;
+ }
+ }
+
+ /**
+ * Require a string to appear, or throw an exception.
+ * <p><em>Precondition:</em> Entity expansion is not required.
+ * <p><em>Precondition:</em> data buffer has no characters that
+ * will get sent to the application.
+ */
+ private void require(String delim)
+ throws SAXException, IOException
+ {
+ int length = delim.length();
+ char[] ch;
+
+ if (length < dataBuffer.length)
+ {
+ ch = dataBuffer;
+ delim.getChars(0, length, ch, 0);
+ }
+ else
+ {
+ ch = delim.toCharArray();
+ }
+
+ if (USE_CHEATS && length <= (readBufferLength - readBufferPos))
+ {
+ int offset = readBufferPos;
+
+ for (int i = 0; i < length; i++, offset++)
+ {
+ if (ch[i] != readBuffer[offset])
+ {
+ error ("required string", null, delim);
+ }
+ }
+ readBufferPos = offset;
+
+ }
+ else
+ {
+ for (int i = 0; i < length; i++)
+ {
+ require(ch[i]);
+ }
+ }
+ }
+
+ /**
+ * Require a character to appear, or throw an exception.
+ */
+ private void require(char delim)
+ throws SAXException, IOException
+ {
+ char c = readCh();
+
+ if (c != delim)
+ {
+ error("required character", c, Character.toString(delim));
+ }
+ }
+
+ /**
+ * Create an interned string from a character array.
+ * &AElig;lfred uses this method to create an interned version
+ * of all names and name tokens, so that it can test equality
+ * with <code>==</code> instead of <code>String.equals ()</code>.
+ *
+ * <p>This is much more efficient than constructing a non-interned
+ * string first, and then interning it.
+ *
+ * @param ch an array of characters for building the string.
+ * @param start the starting position in the array.
+ * @param length the number of characters to place in the string.
+ * @return an interned string.
+ * @see #intern (String)
+ * @see java.lang.String#intern
+ */
+ public String intern(char[] ch, int start, int length)
+ {
+ int index = 0;
+ int hash = 0;
+ Object[] bucket;
+
+ // Generate a hash code. This is a widely used string hash,
+ // often attributed to Brian Kernighan.
+ for (int i = start; i < start + length; i++)
+ {
+ hash = 31 * hash + ch[i];
+ }
+ hash = (hash & 0x7fffffff) % SYMBOL_TABLE_LENGTH;
+
+ // Get the bucket -- consists of {array,String} pairs
+ if ((bucket = symbolTable[hash]) == null)
+ {
+ // first string in this bucket
+ bucket = new Object[8];
+
+ // Search for a matching tuple, and
+ // return the string if we find one.
+ }
+ else
+ {
+ while (index < bucket.length)
+ {
+ char[] chFound = (char[]) bucket[index];
+
+ // Stop when we hit an empty entry.
+ if (chFound == null)
+ {
+ break;
+ }
+
+ // If they're the same length, check for a match.
+ if (chFound.length == length)
+ {
+ for (int i = 0; i < chFound.length; i++)
+ {
+ // continue search on failure
+ if (ch[start + i] != chFound[i])
+ {
+ break;
+ }
+ else if (i == length - 1)
+ {
+ // That's it, we have a match!
+ return (String) bucket[index + 1];
+ }
+ }
+ }
+ index += 2;
+ }
+ // Not found -- we'll have to add it.
+
+ // Do we have to grow the bucket?
+ bucket = (Object[]) extendArray(bucket, bucket.length, index);
+ }
+ symbolTable[hash] = bucket;
+
+ // OK, add it to the end of the bucket -- "local" interning.
+ // Intern "globally" to let applications share interning benefits.
+ // That is, "!=" and "==" work on our strings, not just equals().
+ String s = new String(ch, start, length).intern();
+ bucket[index] = s.toCharArray();
+ bucket[index + 1] = s;
+ return s;
+ }
+
+ /**
+ * Ensure the capacity of an array, allocating a new one if
+ * necessary. Usually extends only for name hash collisions.
+ */
+ private Object extendArray(Object array, int currentSize, int requiredSize)
+ {
+ if (requiredSize < currentSize)
+ {
+ return array;
+ }
+ else
+ {
+ Object newArray = null;
+ int newSize = currentSize * 2;
+
+ if (newSize <= requiredSize)
+ {
+ newSize = requiredSize + 1;
+ }
+
+ if (array instanceof char[])
+ {
+ newArray = new char[newSize];
+ }
+ else if (array instanceof Object[])
+ {
+ newArray = new Object[newSize];
+ }
+ else
+ {
+ throw new RuntimeException();
+ }
+
+ System.arraycopy(array, 0, newArray, 0, currentSize);
+ return newArray;
+ }
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // XML query routines.
+ //////////////////////////////////////////////////////////////////////
+
+ boolean isStandalone()
+ {
+ return docIsStandalone;
+ }
+
+ //
+ // Elements
+ //
+
+ private int getContentType(ElementDecl element, int defaultType)
+ {
+ int retval;
+
+ if (element == null)
+ {
+ return defaultType;
+ }
+ retval = element.contentType;
+ if (retval == CONTENT_UNDECLARED)
+ {
+ retval = defaultType;
+ }
+ return retval;
+ }
+
+ /**
+ * Look up the content type of an element.
+ * @param name The element type name.
+ * @return An integer constant representing the content type.
+ * @see #CONTENT_UNDECLARED
+ * @see #CONTENT_ANY
+ * @see #CONTENT_EMPTY
+ * @see #CONTENT_MIXED
+ * @see #CONTENT_ELEMENTS
+ */
+ public int getElementContentType(String name)
+ {
+ ElementDecl element = (ElementDecl) elementInfo.get(name);
+ return getContentType(element, CONTENT_UNDECLARED);
+ }
+
+ /**
+ * Register an element.
+ * Array format:
+ * [0] element type name
+ * [1] content model (mixed, elements only)
+ * [2] attribute hash table
+ */
+ private void setElement(String name, int contentType,
+ String contentModel, HashMap attributes)
+ throws SAXException
+ {
+ if (skippedPE)
+ {
+ return;
+ }
+
+ ElementDecl element = (ElementDecl) elementInfo.get(name);
+
+ // first <!ELEMENT ...> or <!ATTLIST ...> for this type?
+ if (element == null)
+ {
+ element = new ElementDecl();
+ element.contentType = contentType;
+ element.contentModel = contentModel;
+ element.attributes = attributes;
+ elementInfo.put(name, element);
+ return;
+ }
+
+ // <!ELEMENT ...> declaration?
+ if (contentType != CONTENT_UNDECLARED)
+ {
+ // ... following an associated <!ATTLIST ...>
+ if (element.contentType == CONTENT_UNDECLARED)
+ {
+ element.contentType = contentType;
+ element.contentModel = contentModel;
+ }
+ else
+ {
+ // VC: Unique Element Type Declaration
+ handler.verror("multiple declarations for element type: "
+ + name);
+ }
+ }
+
+ // first <!ATTLIST ...>, before <!ELEMENT ...> ?
+ else if (attributes != null)
+ {
+ element.attributes = attributes;
+ }
+ }
+
+ /**
+ * Look up the attribute hash table for an element.
+ * The hash table is the second item in the element array.
+ */
+ private HashMap getElementAttributes(String name)
+ {
+ ElementDecl element = (ElementDecl) elementInfo.get(name);
+ return (element == null) ? null : element.attributes;
+ }
+
+ //
+ // Attributes
+ //
+
+ /**
+ * Get the declared attributes for an element type.
+ * @param elname The name of the element type.
+ * @return An iterator over all the attributes declared for
+ * a specific element type. The results will be valid only
+ * after the DTD (if any) has been parsed.
+ * @see #getAttributeType
+ * @see #getAttributeEnumeration
+ * @see #getAttributeDefaultValueType
+ * @see #getAttributeDefaultValue
+ * @see #getAttributeExpandedValue
+ */
+ private Iterator declaredAttributes(ElementDecl element)
+ {
+ HashMap attlist;
+
+ if (element == null)
+ {
+ return null;
+ }
+ if ((attlist = element.attributes) == null)
+ {
+ return null;
+ }
+ return attlist.keySet().iterator();
+ }
+
+ /**
+ * Get the declared attributes for an element type.
+ * @param elname The name of the element type.
+ * @return An iterator over all the attributes declared for
+ * a specific element type. The results will be valid only
+ * after the DTD (if any) has been parsed.
+ * @see #getAttributeType
+ * @see #getAttributeEnumeration
+ * @see #getAttributeDefaultValueType
+ * @see #getAttributeDefaultValue
+ * @see #getAttributeExpandedValue
+ */
+ public Iterator declaredAttributes(String elname)
+ {
+ return declaredAttributes((ElementDecl) elementInfo.get(elname));
+ }
+
+ /**
+ * Retrieve the declared type of an attribute.
+ * @param name The name of the associated element.
+ * @param aname The name of the attribute.
+ * @return An interend string denoting the type, or null
+ * indicating an undeclared attribute.
+ */
+ public String getAttributeType(String name, String aname)
+ {
+ AttributeDecl attribute = getAttribute(name, aname);
+ return (attribute == null) ? null : attribute.type;
+ }
+
+ /**
+ * Retrieve the allowed values for an enumerated attribute type.
+ * @param name The name of the associated element.
+ * @param aname The name of the attribute.
+ * @return A string containing the token list.
+ */
+ public String getAttributeEnumeration(String name, String aname)
+ {
+ AttributeDecl attribute = getAttribute(name, aname);
+ // assert: attribute.enumeration is "ENUMERATION" or "NOTATION"
+ return (attribute == null) ? null : attribute.enumeration;
+ }
+
+ /**
+ * Retrieve the default value of a declared attribute.
+ * @param name The name of the associated element.
+ * @param aname The name of the attribute.
+ * @return The default value, or null if the attribute was
+ * #IMPLIED or simply undeclared and unspecified.
+ * @see #getAttributeExpandedValue
+ */
+ public String getAttributeDefaultValue(String name, String aname)
+ {
+ AttributeDecl attribute = getAttribute(name, aname);
+ return (attribute == null) ? null : attribute.value;
+ }
+
+ /*
+
+// FIXME: Leaving this in, until W3C finally resolves the confusion
+// between parts of the XML 2nd REC about when entity declararations
+// are guaranteed to be known. Current code matches what section 5.1
+// (conformance) describes, but some readings of the self-contradicting
+// text in 4.1 (the "Entity Declared" WFC and VC) seem to expect that
+// attribute expansion/normalization must be deferred in some cases
+// (just TRY to identify them!).
+
+ * Retrieve the expanded value of a declared attribute.
+ * <p>General entities (and char refs) will be expanded (once).
+ * @param name The name of the associated element.
+ * @param aname The name of the attribute.
+ * @return The expanded default value, or null if the attribute was
+ * #IMPLIED or simply undeclared
+ * @see #getAttributeDefaultValue
+ public String getAttributeExpandedValue (String name, String aname)
+ throws Exception
+ {
+ AttributeDecl attribute = getAttribute (name, aname);
+
+ if (attribute == null) {
+ return null;
+ } else if (attribute.defaultValue == null && attribute.value != null) {
+ // we MUST use the same buf for both quotes else the literal
+ // can't be properly terminated
+ char buf [] = new char [1];
+ int flags = LIT_ENTITY_REF | LIT_ATTRIBUTE;
+ String type = getAttributeType (name, aname);
+
+ if (type != "CDATA" && type != null)
+ flags |= LIT_NORMALIZE;
+ buf [0] = '"';
+ pushCharArray (null, buf, 0, 1);
+ pushString (null, attribute.value);
+ pushCharArray (null, buf, 0, 1);
+ attribute.defaultValue = readLiteral (flags);
+ }
+ return attribute.defaultValue;
+ }
+ */
+
+ /**
+ * Retrieve the default value mode of a declared attribute.
+ * @see #ATTRIBUTE_DEFAULT_SPECIFIED
+ * @see #ATTRIBUTE_DEFAULT_IMPLIED
+ * @see #ATTRIBUTE_DEFAULT_REQUIRED
+ * @see #ATTRIBUTE_DEFAULT_FIXED
+ */
+ public int getAttributeDefaultValueType(String name, String aname)
+ {
+ AttributeDecl attribute = getAttribute(name, aname);
+ return (attribute == null) ? ATTRIBUTE_DEFAULT_UNDECLARED :
+ attribute.valueType;
+ }
+
+ /**
+ * Register an attribute declaration for later retrieval.
+ * Format:
+ * - String type
+ * - String default value
+ * - int value type
+ * - enumeration
+ * - processed default value
+ */
+ private void setAttribute(String elName, String name, String type,
+ String enumeration, String value, int valueType)
+ throws Exception
+ {
+ HashMap attlist;
+
+ if (skippedPE)
+ {
+ return;
+ }
+
+ // Create a new hashtable if necessary.
+ attlist = getElementAttributes(elName);
+ if (attlist == null)
+ {
+ attlist = new HashMap();
+ }
+
+ // ignore multiple attribute declarations!
+ if (attlist.get(name) != null)
+ {
+ // warn ...
+ return;
+ }
+ else
+ {
+ AttributeDecl attribute = new AttributeDecl();
+ attribute.type = type;
+ attribute.value = value;
+ attribute.valueType = valueType;
+ attribute.enumeration = enumeration;
+ attlist.put(name, attribute);
+
+ // save; but don't overwrite any existing <!ELEMENT ...>
+ setElement(elName, CONTENT_UNDECLARED, null, attlist);
+ }
+ }
+
+ /**
+ * Retrieve the attribute declaration for the given element name and name.
+ */
+ private AttributeDecl getAttribute(String elName, String name)
+ {
+ HashMap attlist = getElementAttributes(elName);
+ return (attlist == null) ? null : (AttributeDecl) attlist.get(name);
+ }
+
+ //
+ // Entities
+ //
+
+ /**
+ * Find the type of an entity.
+ * @returns An integer constant representing the entity type.
+ * @see #ENTITY_UNDECLARED
+ * @see #ENTITY_INTERNAL
+ * @see #ENTITY_NDATA
+ * @see #ENTITY_TEXT
+ */
+ public int getEntityType(String ename)
+ {
+ EntityInfo entity = (EntityInfo) entityInfo.get(ename);
+ return (entity == null) ? ENTITY_UNDECLARED : entity.type;
+ }
+
+ /**
+ * Return an external entity's identifiers.
+ * @param ename The name of the external entity.
+ * @return The entity's public identifier, system identifier, and base URI.
+ * Null if the entity was not declared as an external entity.
+ * @see #getEntityType
+ */
+ public ExternalIdentifiers getEntityIds(String ename)
+ {
+ EntityInfo entity = (EntityInfo) entityInfo.get(ename);
+ return (entity == null) ? null : entity.ids;
+ }
+
+ /**
+ * Return an internal entity's replacement text.
+ * @param ename The name of the internal entity.
+ * @return The entity's replacement text, or null if
+ * the entity was not declared as an internal entity.
+ * @see #getEntityType
+ */
+ public String getEntityValue(String ename)
+ {
+ EntityInfo entity = (EntityInfo) entityInfo.get(ename);
+ return (entity == null) ? null : entity.value;
+ }
+
+ /**
+ * Register an entity declaration for later retrieval.
+ */
+ private void setInternalEntity(String eName, String value)
+ throws SAXException
+ {
+ if (skippedPE)
+ {
+ return;
+ }
+
+ if (entityInfo.get(eName) == null)
+ {
+ EntityInfo entity = new EntityInfo();
+ entity.type = ENTITY_INTERNAL;
+ entity.value = value;
+ entityInfo.put(eName, entity);
+ }
+ if (handler.stringInterning)
+ {
+ if ("lt" == eName || "gt" == eName || "quot" == eName
+ || "apos" == eName || "amp" == eName)
+ {
+ return;
+ }
+ }
+ else
+ {
+ if ("lt".equals(eName) || "gt".equals(eName) || "quot".equals(eName)
+ || "apos".equals(eName) || "amp".equals(eName))
+ {
+ return;
+ }
+ }
+ handler.getDeclHandler().internalEntityDecl(eName, value);
+ }
+
+ /**
+ * Register an external entity declaration for later retrieval.
+ */
+ private void setExternalEntity(String eName, int eClass,
+ ExternalIdentifiers ids, String nName)
+ {
+ if (entityInfo.get(eName) == null)
+ {
+ EntityInfo entity = new EntityInfo();
+ entity.type = eClass;
+ entity.ids = ids;
+ entity.notationName = nName;
+ entityInfo.put(eName, entity);
+ }
+ }
+
+ //
+ // Notations.
+ //
+
+ /**
+ * Report a notation declaration, checking for duplicates.
+ */
+ private void setNotation(String nname, ExternalIdentifiers ids)
+ throws SAXException
+ {
+ if (skippedPE)
+ {
+ return;
+ }
+
+ handler.notationDecl(nname, ids.publicId, ids.systemId, ids.baseUri);
+ if (notationInfo.get(nname) == null)
+ {
+ notationInfo.put(nname, nname);
+ }
+ else
+ {
+ // VC: Unique Notation Name
+ handler.verror("Duplicate notation name decl: " + nname);
+ }
+ }
+
+ //
+ // Location.
+ //
+
+ /**
+ * Return the current line number.
+ */
+ public int getLineNumber()
+ {
+ return line;
+ }
+
+ /**
+ * Return the current column number.
+ */
+ public int getColumnNumber()
+ {
+ return column;
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // High-level I/O.
+ //////////////////////////////////////////////////////////////////////
+
+ /**
+ * Read a single character from the readBuffer.
+ * <p>The readDataChunk () method maintains the buffer.
+ * <p>If we hit the end of an entity, try to pop the stack and
+ * keep going.
+ * <p> (This approach doesn't really enforce XML's rules about
+ * entity boundaries, but this is not currently a validating
+ * parser).
+ * <p>This routine also attempts to keep track of the current
+ * position in external entities, but it's not entirely accurate.
+ * @return The next available input character.
+ * @see #unread (char)
+ * @see #readDataChunk
+ * @see #readBuffer
+ * @see #line
+ * @return The next character from the current input source.
+ */
+ private char readCh()
+ throws SAXException, IOException
+ {
+ // As long as there's nothing in the
+ // read buffer, try reading more data
+ // (for an external entity) or popping
+ // the entity stack (for either).
+ while (readBufferPos >= readBufferLength)
+ {
+ switch (sourceType)
+ {
+ case INPUT_READER:
+ case INPUT_STREAM:
+ readDataChunk();
+ while (readBufferLength < 1)
+ {
+ popInput();
+ if (readBufferLength < 1)
+ {
+ readDataChunk();
+ }
+ }
+ break;
+
+ default:
+
+ popInput();
+ break;
+ }
+ }
+
+ char c = readBuffer[readBufferPos++];
+
+ if (c == '\n')
+ {
+ line++;
+ column = 0;
+ }
+ else
+ {
+ if (c == '<')
+ {
+ /* the most common return to parseContent () ... NOP */
+ }
+ else if (((c < 0x0020 && (c != '\t') && (c != '\r')) || c > 0xFFFD)
+ || ((c >= 0x007f) && (c <= 0x009f) && (c != 0x0085)
+ && xmlVersion == XML_11))
+ {
+ error("illegal XML character U+" + Integer.toHexString(c));
+ }
+
+ // If we're in the DTD and in a context where PEs get expanded,
+ // do so ... 1/14/2000 errata identify those contexts. There
+ // are also spots in the internal subset where PE refs are fatal
+ // errors, hence yet another flag.
+ else if (c == '%' && expandPE)
+ {
+ if (peIsError)
+ {
+ error("PE reference within decl in internal subset.");
+ }
+ parsePEReference();
+ return readCh();
+ }
+ column++;
+ }
+
+ return c;
+ }
+
+ /**
+ * Push a single character back onto the current input stream.
+ * <p>This method usually pushes the character back onto
+ * the readBuffer.
+ * <p>I don't think that this would ever be called with
+ * readBufferPos = 0, because the methods always reads a character
+ * before unreading it, but just in case, I've added a boundary
+ * condition.
+ * @param c The character to push back.
+ * @see #readCh
+ * @see #unread (char[])
+ * @see #readBuffer
+ */
+ private void unread(char c)
+ throws SAXException
+ {
+ // Normal condition.
+ if (c == '\n')
+ {
+ line--;
+ column = -1;
+ }
+ if (readBufferPos > 0)
+ {
+ readBuffer[--readBufferPos] = c;
+ }
+ else
+ {
+ pushString(null, Character.toString(c));
+ }
+ }
+
+ /**
+ * Push a char array back onto the current input stream.
+ * <p>NOTE: you must <em>never</em> push back characters that you
+ * haven't actually read: use pushString () instead.
+ * @see #readCh
+ * @see #unread (char)
+ * @see #readBuffer
+ * @see #pushString
+ */
+ private void unread(char[] ch, int length)
+ throws SAXException
+ {
+ for (int i = 0; i < length; i++)
+ {
+ if (ch[i] == '\n')
+ {
+ line--;
+ column = -1;
+ }
+ }
+ if (length < readBufferPos)
+ {
+ readBufferPos -= length;
+ }
+ else
+ {
+ pushCharArray(null, ch, 0, length);
+ }
+ }
+
+ /**
+ * Push, or skip, a new external input source.
+ * The source will be some kind of parsed entity, such as a PE
+ * (including the external DTD subset) or content for the body.
+ *
+ * @param url The java.net.URL object for the entity.
+ * @see SAXDriver#resolveEntity
+ * @see #pushString
+ * @see #sourceType
+ * @see #pushInput
+ * @see #detectEncoding
+ * @see #sourceType
+ * @see #readBuffer
+ */
+ private void pushURL(boolean isPE,
+ String ename,
+ ExternalIdentifiers ids,
+ Reader reader,
+ InputStream stream,
+ String encoding,
+ boolean doResolve)
+ throws SAXException, IOException
+ {
+ boolean ignoreEncoding;
+ String systemId;
+ InputSource source;
+
+ if (!isPE)
+ {
+ dataBufferFlush();
+ }
+
+ scratch.setPublicId(ids.publicId);
+ scratch.setSystemId(ids.systemId);
+
+ // See if we should skip or substitute the entity.
+ // If we're not skipping, resolving reports startEntity()
+ // and updates the (handler's) stack of URIs.
+ if (doResolve)
+ {
+ // assert (stream == null && reader == null && encoding == null)
+ source = handler.resolveEntity(isPE, ename, scratch, ids.baseUri);
+ if (source == null)
+ {
+ handler.warn("skipping entity: " + ename);
+ handler.skippedEntity(ename);
+ if (isPE)
+ {
+ skippedPE = true;
+ }
+ return;
+ }
+
+ // we might be using alternate IDs/encoding
+ systemId = source.getSystemId();
+ // The following warning and setting systemId was deleted bcause
+ // the application has the option of not setting systemId
+ // provided that it has set the characte/byte stream.
+ /*
+ if (systemId == null) {
+ handler.warn ("missing system ID, using " + ids.systemId);
+ systemId = ids.systemId;
+ }
+ */
+ }
+ else
+ {
+ // "[document]", or "[dtd]" via getExternalSubset()
+ scratch.setCharacterStream(reader);
+ scratch.setByteStream(stream);
+ scratch.setEncoding(encoding);
+ source = scratch;
+ systemId = ids.systemId;
+ if (handler.stringInterning)
+ {
+ handler.startExternalEntity(ename, systemId,
+ "[document]" == ename);
+ }
+ else
+ {
+ handler.startExternalEntity(ename, systemId,
+ "[document]".equals(ename));
+ }
+ }
+
+ // we may have been given I/O streams directly
+ if (source.getCharacterStream() != null)
+ {
+ if (source.getByteStream() != null)
+ error("InputSource has two streams!");
+ reader = source.getCharacterStream();
+ }
+ else if (source.getByteStream() != null)
+ {
+ encoding = source.getEncoding();
+ if (encoding == null)
+ {
+ stream = source.getByteStream();
+ }
+ else
+ {
+ try
+ {
+ reader = new InputStreamReader(source.getByteStream(),
+ encoding);
+ }
+ catch (IOException e)
+ {
+ stream = source.getByteStream();
+ }
+ }
+ }
+ else if (systemId == null)
+ {
+ error("InputSource has no URI!");
+ }
+ scratch.setCharacterStream(null);
+ scratch.setByteStream(null);
+ scratch.setEncoding(null);
+
+ // Push the existing status.
+ pushInput(ename);
+
+ // Create a new read buffer.
+ // (Note the four-character margin)
+ readBuffer = new char[READ_BUFFER_MAX + 4];
+ readBufferPos = 0;
+ readBufferLength = 0;
+ readBufferOverflow = -1;
+ is = null;
+ line = 1;
+ column = 0;
+ currentByteCount = 0;
+
+ // If there's an explicit character stream, just
+ // ignore encoding declarations.
+ if (reader != null)
+ {
+ sourceType = INPUT_READER;
+ this.reader = reader;
+ tryEncodingDecl(true);
+ return;
+ }
+
+ // Else we handle the conversion, and need to ensure
+ // it's done right.
+ sourceType = INPUT_STREAM;
+ if (stream != null)
+ {
+ is = stream;
+ }
+ else
+ {
+ // We have to open our own stream to the URL.
+ URL url = new URL(systemId);
+
+ externalEntity = url.openConnection();
+ externalEntity.connect();
+ is = externalEntity.getInputStream();
+ }
+
+ // If we get to here, there must be
+ // an InputStream available.
+ if (!is.markSupported())
+ {
+ is = new BufferedInputStream(is);
+ }
+
+ // Get any external encoding label.
+ if (encoding == null && externalEntity != null)
+ {
+ // External labels can be untrustworthy; filesystems in
+ // particular often have the wrong default for content
+ // that wasn't locally originated. Those we autodetect.
+ if (!"file".equals(externalEntity.getURL().getProtocol()))
+ {
+ int temp;
+
+ // application/xml;charset=something;otherAttr=...
+ // ... with many variants on 'something'
+ encoding = externalEntity.getContentType();
+
+ // MHK code (fix for Saxon 5.5.1/007):
+ // protect against encoding==null
+ if (encoding == null)
+ {
+ temp = -1;
+ }
+ else
+ {
+ temp = encoding.indexOf("charset");
+ }
+
+ // RFC 2376 sez MIME text defaults to ASCII, but since the
+ // JDK will create a MIME type out of thin air, we always
+ // autodetect when there's no explicit charset attribute.
+ if (temp < 0)
+ {
+ encoding = null; // autodetect
+ }
+ else
+ {
+ // only this one attribute
+ if ((temp = encoding.indexOf(';')) > 0)
+ {
+ encoding = encoding.substring(0, temp);
+ }
+
+ if ((temp = encoding.indexOf('=', temp + 7)) > 0)
+ {
+ encoding = encoding.substring(temp + 1);
+
+ // attributes can have comment fields (RFC 822)
+ if ((temp = encoding.indexOf('(')) > 0)
+ {
+ encoding = encoding.substring(0, temp);
+ }
+ // ... and values may be quoted
+ if ((temp = encoding.indexOf('"')) > 0)
+ {
+ encoding =
+ encoding.substring(temp + 1,
+ encoding.indexOf('"', temp + 2));
+ }
+ encoding = encoding.trim();
+ }
+ else
+ {
+ handler.warn("ignoring illegal MIME attribute: "
+ + encoding);
+ encoding = null;
+ }
+ }
+ }
+ }
+
+ // if we got an external encoding label, use it ...
+ if (encoding != null)
+ {
+ this.encoding = ENCODING_EXTERNAL;
+ setupDecoding(encoding);
+ ignoreEncoding = true;
+
+ // ... else autodetect from first bytes.
+ }
+ else
+ {
+ detectEncoding();
+ ignoreEncoding = false;
+ }
+
+ // Read any XML or text declaration.
+ // If we autodetected, it may tell us the "real" encoding.
+ try
+ {
+ tryEncodingDecl(ignoreEncoding);
+ }
+ catch (UnsupportedEncodingException x)
+ {
+ encoding = x.getMessage();
+
+ // if we don't handle the declared encoding,
+ // try letting a JVM InputStreamReader do it
+ try
+ {
+ if (sourceType != INPUT_STREAM)
+ {
+ throw x;
+ }
+
+ is.reset();
+ readBufferPos = 0;
+ readBufferLength = 0;
+ readBufferOverflow = -1;
+ line = 1;
+ currentByteCount = column = 0;
+
+ sourceType = INPUT_READER;
+ this.reader = new InputStreamReader(is, encoding);
+ is = null;
+
+ tryEncodingDecl(true);
+
+ }
+ catch (IOException e)
+ {
+ error("unsupported text encoding",
+ encoding,
+ null);
+ }
+ }
+ }
+
+ /**
+ * Check for an encoding declaration. This is the second part of the
+ * XML encoding autodetection algorithm, relying on detectEncoding to
+ * get to the point that this part can read any encoding declaration
+ * in the document (using only US-ASCII characters).
+ *
+ * <p> Because this part starts to fill parser buffers with this data,
+ * it's tricky to setup a reader so that Java's built-in decoders can be
+ * used for the character encodings that aren't built in to this parser
+ * (such as EUC-JP, KOI8-R, Big5, etc).
+ *
+ * @return any encoding in the declaration, uppercased; or null
+ * @see detectEncoding
+ */
+ private String tryEncodingDecl(boolean ignoreEncoding)
+ throws SAXException, IOException
+ {
+ // Read the XML/text declaration.
+ if (tryRead("<?xml"))
+ {
+ if (tryWhitespace())
+ {
+ if (inputStack.size() > 0)
+ {
+ return parseTextDecl(ignoreEncoding);
+ }
+ else
+ {
+ return parseXMLDecl(ignoreEncoding);
+ }
+ }
+ else
+ {
+ // <?xml-stylesheet ...?> or similar
+ unread('l');
+ unread('m');
+ unread('x');
+ unread('?');
+ unread('<');
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Attempt to detect the encoding of an entity.
+ * <p>The trick here (as suggested in the XML standard) is that
+ * any entity not in UTF-8, or in UCS-2 with a byte-order mark,
+ * <b>must</b> begin with an XML declaration or an encoding
+ * declaration; we simply have to look for "&lt;?xml" in various
+ * encodings.
+ * <p>This method has no way to distinguish among 8-bit encodings.
+ * Instead, it sets up for UTF-8, then (possibly) revises its assumption
+ * later in setupDecoding (). Any ASCII-derived 8-bit encoding
+ * should work, but most will be rejected later by setupDecoding ().
+ * @see #tryEncoding (byte[], byte, byte, byte, byte)
+ * @see #tryEncoding (byte[], byte, byte)
+ * @see #setupDecoding
+ */
+ private void detectEncoding()
+ throws SAXException, IOException
+ {
+ byte[] signature = new byte[4];
+
+ // Read the first four bytes for
+ // autodetection.
+ is.mark(4);
+ is.read(signature);
+ is.reset();
+
+ //
+ // FIRST: four byte encodings (who uses these?)
+ //
+ if (tryEncoding(signature, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x3c))
+ {
+ // UCS-4 must begin with "<?xml"
+ // 0x00 0x00 0x00 0x3c: UCS-4, big-endian (1234)
+ // "UTF-32BE"
+ encoding = ENCODING_UCS_4_1234;
+ }
+ else if (tryEncoding(signature, (byte) 0x3c, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00))
+ {
+ // 0x3c 0x00 0x00 0x00: UCS-4, little-endian (4321)
+ // "UTF-32LE"
+ encoding = ENCODING_UCS_4_4321;
+ }
+ else if (tryEncoding(signature, (byte) 0x00, (byte) 0x00,
+ (byte) 0x3c, (byte) 0x00))
+ {
+ // 0x00 0x00 0x3c 0x00: UCS-4, unusual (2143)
+ encoding = ENCODING_UCS_4_2143;
+ }
+ else if (tryEncoding(signature, (byte) 0x00, (byte) 0x3c,
+ (byte) 0x00, (byte) 0x00))
+ {
+ // 0x00 0x3c 0x00 0x00: UCS-4, unusual (3421)
+ encoding = ENCODING_UCS_4_3412;
+
+ // 00 00 fe ff UCS_4_1234 (with BOM)
+ // ff fe 00 00 UCS_4_4321 (with BOM)
+ }
+
+ //
+ // SECOND: two byte encodings
+ // note ... with 1/14/2000 errata the XML spec identifies some
+ // more "broken UTF-16" autodetection cases, with no XML decl,
+ // which we don't handle here (that's legal too).
+ //
+ else if (tryEncoding(signature, (byte) 0xfe, (byte) 0xff))
+ {
+ // UCS-2 with a byte-order marker. (UTF-16)
+ // 0xfe 0xff: UCS-2, big-endian (12)
+ encoding = ENCODING_UCS_2_12;
+ is.read(); is.read();
+ }
+ else if (tryEncoding(signature, (byte) 0xff, (byte) 0xfe))
+ {
+ // UCS-2 with a byte-order marker. (UTF-16)
+ // 0xff 0xfe: UCS-2, little-endian (21)
+ encoding = ENCODING_UCS_2_21;
+ is.read(); is.read();
+ }
+ else if (tryEncoding(signature, (byte) 0x00, (byte) 0x3c,
+ (byte) 0x00, (byte) 0x3f))
+ {
+ // UTF-16BE (otherwise, malformed UTF-16)
+ // 0x00 0x3c 0x00 0x3f: UCS-2, big-endian, no byte-order mark
+ encoding = ENCODING_UCS_2_12;
+ error("no byte-order mark for UCS-2 entity");
+ }
+ else if (tryEncoding(signature, (byte) 0x3c, (byte) 0x00,
+ (byte) 0x3f, (byte) 0x00))
+ {
+ // UTF-16LE (otherwise, malformed UTF-16)
+ // 0x3c 0x00 0x3f 0x00: UCS-2, little-endian, no byte-order mark
+ encoding = ENCODING_UCS_2_21;
+ error("no byte-order mark for UCS-2 entity");
+ }
+
+ //
+ // THIRD: ASCII-derived encodings, fixed and variable lengths
+ //
+ else if (tryEncoding(signature, (byte) 0x3c, (byte) 0x3f,
+ (byte) 0x78, (byte) 0x6d))
+ {
+ // ASCII derived
+ // 0x3c 0x3f 0x78 0x6d: UTF-8 or other 8-bit markup (read ENCODING)
+ encoding = ENCODING_UTF_8;
+ prefetchASCIIEncodingDecl();
+ }
+ else if (signature[0] == (byte) 0xef
+ && signature[1] == (byte) 0xbb
+ && signature[2] == (byte) 0xbf)
+ {
+ // 0xef 0xbb 0xbf: UTF-8 BOM (not part of document text)
+ // this un-needed notion slipped into XML 2nd ed through a
+ // "non-normative" erratum; now required by MSFT and UDDI,
+ // and E22 made it normative.
+ encoding = ENCODING_UTF_8;
+ is.read(); is.read(); is.read();
+ }
+ else
+ {
+ // 4c 6f a7 94 ... we don't understand EBCDIC flavors
+ // ... but we COULD at least kick in some fixed code page
+
+ // (default) UTF-8 without encoding/XML declaration
+ encoding = ENCODING_UTF_8;
+ }
+ }
+
+ /**
+ * Check for a four-byte signature.
+ * <p>Utility routine for detectEncoding ().
+ * <p>Always looks for some part of "<?XML" in a specific encoding.
+ * @param sig The first four bytes read.
+ * @param b1 The first byte of the signature
+ * @param b2 The second byte of the signature
+ * @param b3 The third byte of the signature
+ * @param b4 The fourth byte of the signature
+ * @see #detectEncoding
+ */
+ private static boolean tryEncoding(byte[] sig, byte b1, byte b2,
+ byte b3, byte b4)
+ {
+ return (sig[0] == b1 && sig[1] == b2
+ && sig[2] == b3 && sig[3] == b4);
+ }
+
+ /**
+ * Check for a two-byte signature.
+ * <p>Looks for a UCS-2 byte-order mark.
+ * <p>Utility routine for detectEncoding ().
+ * @param sig The first four bytes read.
+ * @param b1 The first byte of the signature
+ * @param b2 The second byte of the signature
+ * @see #detectEncoding
+ */
+ private static boolean tryEncoding(byte[] sig, byte b1, byte b2)
+ {
+ return ((sig[0] == b1) && (sig[1] == b2));
+ }
+
+ /**
+ * This method pushes a string back onto input.
+ * <p>It is useful either as the expansion of an internal entity,
+ * or for backtracking during the parse.
+ * <p>Call pushCharArray () to do the actual work.
+ * @param s The string to push back onto input.
+ * @see #pushCharArray
+ */
+ private void pushString(String ename, String s)
+ throws SAXException
+ {
+ char[] ch = s.toCharArray();
+ pushCharArray(ename, ch, 0, ch.length);
+ }
+
+ /**
+ * Push a new internal input source.
+ * <p>This method is useful for expanding an internal entity,
+ * or for unreading a string of characters. It creates a new
+ * readBuffer containing the characters in the array, instead
+ * of characters converted from an input byte stream.
+ * @param ch The char array to push.
+ * @see #pushString
+ * @see #pushURL
+ * @see #readBuffer
+ * @see #sourceType
+ * @see #pushInput
+ */
+ private void pushCharArray(String ename, char[] ch, int start, int length)
+ throws SAXException
+ {
+ // Push the existing status
+ pushInput(ename);
+ if (ename != null && doReport)
+ {
+ dataBufferFlush();
+ handler.startInternalEntity(ename);
+ }
+ sourceType = INPUT_INTERNAL;
+ readBuffer = ch;
+ readBufferPos = start;
+ readBufferLength = length;
+ readBufferOverflow = -1;
+ }
+
+ /**
+ * Save the current input source onto the stack.
+ * <p>This method saves all of the global variables associated with
+ * the current input source, so that they can be restored when a new
+ * input source has finished. It also tests for entity recursion.
+ * <p>The method saves the following global variables onto a stack
+ * using a fixed-length array:
+ * <ol>
+ * <li>sourceType
+ * <li>externalEntity
+ * <li>readBuffer
+ * <li>readBufferPos
+ * <li>readBufferLength
+ * <li>line
+ * <li>encoding
+ * </ol>
+ * @param ename The name of the entity (if any) causing the new input.
+ * @see #popInput
+ * @see #sourceType
+ * @see #externalEntity
+ * @see #readBuffer
+ * @see #readBufferPos
+ * @see #readBufferLength
+ * @see #line
+ * @see #encoding
+ */
+ private void pushInput(String ename)
+ throws SAXException
+ {
+ // Check for entity recursion.
+ if (ename != null)
+ {
+ Iterator entities = entityStack.iterator();
+ while (entities.hasNext())
+ {
+ String e = (String) entities.next();
+ if (e != null && e == ename)
+ {
+ error("recursive reference to entity", ename, null);
+ }
+ }
+ }
+ entityStack.addLast(ename);
+
+ // Don't bother if there is no current input.
+ if (sourceType == INPUT_NONE)
+ {
+ return;
+ }
+
+ // Set up a snapshot of the current
+ // input source.
+ Input input = new Input();
+
+ input.sourceType = sourceType;
+ input.externalEntity = externalEntity;
+ input.readBuffer = readBuffer;
+ input.readBufferPos = readBufferPos;
+ input.readBufferLength = readBufferLength;
+ input.line = line;
+ input.encoding = encoding;
+ input.readBufferOverflow = readBufferOverflow;
+ input.is = is;
+ input.currentByteCount = currentByteCount;
+ input.column = column;
+ input.reader = reader;
+
+ // Push it onto the stack.
+ inputStack.addLast(input);
+ }
+
+ /**
+ * Restore a previous input source.
+ * <p>This method restores all of the global variables associated with
+ * the current input source.
+ * @exception java.io.EOFException
+ * If there are no more entries on the input stack.
+ * @see #pushInput
+ * @see #sourceType
+ * @see #externalEntity
+ * @see #readBuffer
+ * @see #readBufferPos
+ * @see #readBufferLength
+ * @see #line
+ * @see #encoding
+ */
+ private void popInput()
+ throws SAXException, IOException
+ {
+ String ename = (String) entityStack.removeLast();
+
+ if (ename != null && doReport)
+ {
+ dataBufferFlush();
+ }
+ switch (sourceType)
+ {
+ case INPUT_STREAM:
+ handler.endExternalEntity(ename);
+ is.close();
+ break;
+ case INPUT_READER:
+ handler.endExternalEntity(ename);
+ reader.close();
+ break;
+ case INPUT_INTERNAL:
+ if (ename != null && doReport)
+ {
+ handler.endInternalEntity(ename);
+ }
+ break;
+ }
+
+ // Throw an EOFException if there
+ // is nothing else to pop.
+ if (inputStack.isEmpty())
+ {
+ throw new EOFException("no more input");
+ }
+
+ Input input = (Input) inputStack.removeLast();
+
+ sourceType = input.sourceType;
+ externalEntity = input.externalEntity;
+ readBuffer = input.readBuffer;
+ readBufferPos = input.readBufferPos;
+ readBufferLength = input.readBufferLength;
+ line = input.line;
+ encoding = input.encoding;
+ readBufferOverflow = input.readBufferOverflow;
+ is = input.is;
+ currentByteCount = input.currentByteCount;
+ column = input.column;
+ reader = input.reader;
+ }
+
+ /**
+ * Return true if we can read the expected character.
+ * <p>Note that the character will be removed from the input stream
+ * on success, but will be put back on failure. Do not attempt to
+ * read the character again if the method succeeds.
+ * @param delim The character that should appear next. For a
+ * insensitive match, you must supply this in upper-case.
+ * @return true if the character was successfully read, or false if
+ * it was not.
+ * @see #tryRead (String)
+ */
+ private boolean tryRead(char delim)
+ throws SAXException, IOException
+ {
+ char c;
+
+ // Read the character
+ c = readCh();
+
+ // Test for a match, and push the character
+ // back if the match fails.
+ if (c == delim)
+ {
+ return true;
+ }
+ else
+ {
+ unread(c);
+ return false;
+ }
+ }
+
+ /**
+ * Return true if we can read the expected string.
+ * <p>This is simply a convenience method.
+ * <p>Note that the string will be removed from the input stream
+ * on success, but will be put back on failure. Do not attempt to
+ * read the string again if the method succeeds.
+ * <p>This method will push back a character rather than an
+ * array whenever possible (probably the majority of cases).
+ * @param delim The string that should appear next.
+ * @return true if the string was successfully read, or false if
+ * it was not.
+ * @see #tryRead (char)
+ */
+ private boolean tryRead(String delim)
+ throws SAXException, IOException
+ {
+ return tryRead(delim.toCharArray());
+ }
+
+ private boolean tryRead(char[] ch)
+ throws SAXException, IOException
+ {
+ char c;
+
+ // Compare the input, character-
+ // by character.
+
+ for (int i = 0; i < ch.length; i++)
+ {
+ c = readCh();
+ if (c != ch[i])
+ {
+ unread(c);
+ if (i != 0)
+ {
+ unread(ch, i);
+ }
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Return true if we can read some whitespace.
+ * <p>This is simply a convenience method.
+ * <p>This method will push back a character rather than an
+ * array whenever possible (probably the majority of cases).
+ * @return true if whitespace was found.
+ */
+ private boolean tryWhitespace()
+ throws SAXException, IOException
+ {
+ char c;
+ c = readCh();
+ if (isWhitespace(c))
+ {
+ skipWhitespace();
+ return true;
+ }
+ else
+ {
+ unread(c);
+ return false;
+ }
+ }
+
+ /**
+ * Read all data until we find the specified string.
+ * This is useful for scanning CDATA sections and PIs.
+ * <p>This is inefficient right now, since it calls tryRead ()
+ * for every character.
+ * @param delim The string delimiter
+ * @see #tryRead (String, boolean)
+ * @see #readCh
+ */
+ private void parseUntil(String delim)
+ throws SAXException, IOException
+ {
+ parseUntil(delim.toCharArray());
+ }
+
+ private void parseUntil(char[] delim)
+ throws SAXException, IOException
+ {
+ char c;
+ int startLine = line;
+
+ try
+ {
+ while (!tryRead(delim))
+ {
+ c = readCh();
+ dataBufferAppend(c);
+ }
+ }
+ catch (EOFException e)
+ {
+ error("end of input while looking for delimiter "
+ + "(started on line " + startLine
+ + ')', null, new String(delim));
+ }
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // Low-level I/O.
+ //////////////////////////////////////////////////////////////////////
+
+ /**
+ * Prefetch US-ASCII XML/text decl from input stream into read buffer.
+ * Doesn't buffer more than absolutely needed, so that when an encoding
+ * decl says we need to create an InputStreamReader, we can discard our
+ * buffer and reset(). Caller knows the first chars of the decl exist
+ * in the input stream.
+ */
+ private void prefetchASCIIEncodingDecl()
+ throws SAXException, IOException
+ {
+ int ch;
+ readBufferPos = readBufferLength = 0;
+
+ is.mark(readBuffer.length);
+ while (true)
+ {
+ ch = is.read();
+ readBuffer[readBufferLength++] = (char) ch;
+ switch (ch)
+ {
+ case (int) '>':
+ return;
+ case -1:
+ error("file ends before end of XML or encoding declaration.",
+ null, "?>");
+ }
+ if (readBuffer.length == readBufferLength)
+ {
+ error("unfinished XML or encoding declaration");
+ }
+ }
+ }
+
+ /**
+ * Read a chunk of data from an external input source.
+ * <p>This is simply a front-end that fills the rawReadBuffer
+ * with bytes, then calls the appropriate encoding handler.
+ * @see #encoding
+ * @see #rawReadBuffer
+ * @see #readBuffer
+ * @see #filterCR
+ * @see #copyUtf8ReadBuffer
+ * @see #copyIso8859_1ReadBuffer
+ * @see #copyUcs_2ReadBuffer
+ * @see #copyUcs_4ReadBuffer
+ */
+ private void readDataChunk()
+ throws SAXException, IOException
+ {
+ int count;
+
+ // See if we have any overflow (filterCR sets for CR at end)
+ if (readBufferOverflow > -1)
+ {
+ readBuffer[0] = (char) readBufferOverflow;
+ readBufferOverflow = -1;
+ readBufferPos = 1;
+ sawCR = true;
+ }
+ else
+ {
+ readBufferPos = 0;
+ sawCR = false;
+ }
+
+ // input from a character stream.
+ if (sourceType == INPUT_READER)
+ {
+ count = reader.read(readBuffer,
+ readBufferPos, READ_BUFFER_MAX - readBufferPos);
+ if (count < 0)
+ {
+ readBufferLength = readBufferPos;
+ }
+ else
+ {
+ readBufferLength = readBufferPos + count;
+ }
+ if (readBufferLength > 0)
+ {
+ filterCR(count >= 0);
+ }
+ sawCR = false;
+ return;
+ }
+
+ // Read as many bytes as possible into the raw buffer.
+ count = is.read(rawReadBuffer, 0, READ_BUFFER_MAX);
+
+ // Dispatch to an encoding-specific reader method to populate
+ // the readBuffer. In most parser speed profiles, these routines
+ // show up at the top of the CPU usage chart.
+ if (count > 0)
+ {
+ switch (encoding)
+ {
+ // one byte builtins
+ case ENCODING_ASCII:
+ copyIso8859_1ReadBuffer(count, (char) 0x0080);
+ break;
+ case ENCODING_UTF_8:
+ copyUtf8ReadBuffer(count);
+ break;
+ case ENCODING_ISO_8859_1:
+ copyIso8859_1ReadBuffer(count, (char) 0);
+ break;
+
+ // two byte builtins
+ case ENCODING_UCS_2_12:
+ copyUcs2ReadBuffer(count, 8, 0);
+ break;
+ case ENCODING_UCS_2_21:
+ copyUcs2ReadBuffer(count, 0, 8);
+ break;
+
+ // four byte builtins
+ case ENCODING_UCS_4_1234:
+ copyUcs4ReadBuffer(count, 24, 16, 8, 0);
+ break;
+ case ENCODING_UCS_4_4321:
+ copyUcs4ReadBuffer(count, 0, 8, 16, 24);
+ break;
+ case ENCODING_UCS_4_2143:
+ copyUcs4ReadBuffer(count, 16, 24, 0, 8);
+ break;
+ case ENCODING_UCS_4_3412:
+ copyUcs4ReadBuffer(count, 8, 0, 24, 16);
+ break;
+ }
+ }
+ else
+ {
+ readBufferLength = readBufferPos;
+ }
+
+ readBufferPos = 0;
+
+ // Filter out all carriage returns if we've seen any
+ // (including any saved from a previous read)
+ if (sawCR)
+ {
+ filterCR(count >= 0);
+ sawCR = false;
+
+ // must actively report EOF, lest some CRs get lost.
+ if (readBufferLength == 0 && count >= 0)
+ {
+ readDataChunk();
+ }
+ }
+
+ if (count > 0)
+ {
+ currentByteCount += count;
+ }
+ }
+
+ /**
+ * Filter carriage returns in the read buffer.
+ * CRLF becomes LF; CR becomes LF.
+ * @param moreData true iff more data might come from the same source
+ * @see #readDataChunk
+ * @see #readBuffer
+ * @see #readBufferOverflow
+ */
+ private void filterCR(boolean moreData)
+ {
+ int i, j;
+
+ readBufferOverflow = -1;
+
+loop:
+ for (i = j = readBufferPos; j < readBufferLength; i++, j++)
+ {
+ switch (readBuffer[j])
+ {
+ case '\r':
+ if (j == readBufferLength - 1)
+ {
+ if (moreData)
+ {
+ readBufferOverflow = '\r';
+ readBufferLength--;
+ }
+ else // CR at end of buffer
+ {
+ readBuffer[i++] = '\n';
+ }
+ break loop;
+ }
+ else if (readBuffer[j + 1] == '\n')
+ {
+ j++;
+ }
+ readBuffer[i] = '\n';
+ break;
+
+ case '\n':
+ default:
+ readBuffer[i] = readBuffer[j];
+ break;
+ }
+ }
+ readBufferLength = i;
+ }
+
+ /**
+ * Convert a buffer of UTF-8-encoded bytes into UTF-16 characters.
+ * <p>When readDataChunk () calls this method, the raw bytes are in
+ * rawReadBuffer, and the final characters will appear in
+ * readBuffer.
+ * <p>Note that as of Unicode 3.1, good practice became a requirement,
+ * so that each Unicode character has exactly one UTF-8 representation.
+ * @param count The number of bytes to convert.
+ * @see #readDataChunk
+ * @see #rawReadBuffer
+ * @see #readBuffer
+ * @see #getNextUtf8Byte
+ */
+ private void copyUtf8ReadBuffer(int count)
+ throws SAXException, IOException
+ {
+ int i = 0;
+ int j = readBufferPos;
+ int b1;
+ char c = 0;
+
+ /*
+ // check once, so the runtime won't (if it's smart enough)
+ if (count < 0 || count > rawReadBuffer.length)
+ throw new ArrayIndexOutOfBoundsException (Integer.toString (count));
+ */
+
+ while (i < count)
+ {
+ b1 = rawReadBuffer[i++];
+
+ // Determine whether we are dealing
+ // with a one-, two-, three-, or four-
+ // byte sequence.
+ if (b1 < 0)
+ {
+ if ((b1 & 0xe0) == 0xc0)
+ {
+ // 2-byte sequence: 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
+ c = (char) (((b1 & 0x1f) << 6)
+ | getNextUtf8Byte(i++, count));
+ if (c < 0x0080)
+ {
+ encodingError("Illegal two byte UTF-8 sequence",
+ c, 0);
+ }
+
+ //Sec 2.11
+ // [1] the two-character sequence #xD #xA
+ // [2] the two-character sequence #xD #x85
+ if ((c == 0x0085 || c == 0x000a) && sawCR)
+ {
+ continue;
+ }
+
+ // Sec 2.11
+ // [3] the single character #x85
+
+ if (c == 0x0085 && xmlVersion == XML_11)
+ {
+ readBuffer[j++] = '\r';
+ }
+ }
+ else if ((b1 & 0xf0) == 0xe0)
+ {
+ // 3-byte sequence:
+ // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
+ // most CJKV characters
+ c = (char) (((b1 & 0x0f) << 12) |
+ (getNextUtf8Byte(i++, count) << 6) |
+ getNextUtf8Byte(i++, count));
+ //sec 2.11
+ //[4] the single character #x2028
+ if (c == 0x2028 && xmlVersion == XML_11)
+ {
+ readBuffer[j++] = '\r';
+ sawCR = true;
+ continue;
+ }
+ if (c < 0x0800 || (c >= 0xd800 && c <= 0xdfff))
+ {
+ encodingError("Illegal three byte UTF-8 sequence",
+ c, 0);
+ }
+ }
+ else if ((b1 & 0xf8) == 0xf0)
+ {
+ // 4-byte sequence: 11101110wwwwzzzzyy + 110111yyyyxxxxxx
+ // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
+ // (uuuuu = wwww + 1)
+ // "Surrogate Pairs" ... from the "Astral Planes"
+ // Unicode 3.1 assigned the first characters there
+ int iso646 = b1 & 07;
+ iso646 = (iso646 << 6) + getNextUtf8Byte(i++, count);
+ iso646 = (iso646 << 6) + getNextUtf8Byte(i++, count);
+ iso646 = (iso646 << 6) + getNextUtf8Byte(i++, count);
+
+ if (iso646 <= 0xffff)
+ {
+ encodingError("Illegal four byte UTF-8 sequence",
+ iso646, 0);
+ }
+ else
+ {
+ if (iso646 > 0x0010ffff)
+ {
+ encodingError("UTF-8 value out of range for Unicode",
+ iso646, 0);
+ }
+ iso646 -= 0x010000;
+ readBuffer[j++] = (char) (0xd800 | (iso646 >> 10));
+ readBuffer[j++] = (char) (0xdc00 | (iso646 & 0x03ff));
+ continue;
+ }
+ }
+ else
+ {
+ // The five and six byte encodings aren't supported;
+ // they exceed the Unicode (and XML) range.
+ encodingError("unsupported five or six byte UTF-8 sequence",
+ 0xff & b1, i);
+ // NOTREACHED
+ c = 0;
+ }
+ }
+ else
+ {
+ // 1-byte sequence: 000000000xxxxxxx = 0xxxxxxx
+ // (US-ASCII character, "common" case, one branch to here)
+ c = (char) b1;
+ }
+ readBuffer[j++] = c;
+ if (c == '\r')
+ {
+ sawCR = true;
+ }
+ }
+ // How many characters have we read?
+ readBufferLength = j;
+ }
+
+ /**
+ * Return the next byte value in a UTF-8 sequence.
+ * If it is not possible to get a byte from the current
+ * entity, throw an exception.
+ * @param pos The current position in the rawReadBuffer.
+ * @param count The number of bytes in the rawReadBuffer
+ * @return The significant six bits of a non-initial byte in
+ * a UTF-8 sequence.
+ * @exception EOFException If the sequence is incomplete.
+ */
+ private int getNextUtf8Byte(int pos, int count)
+ throws SAXException, IOException
+ {
+ int val;
+
+ // Take a character from the buffer
+ // or from the actual input stream.
+ if (pos < count)
+ {
+ val = rawReadBuffer[pos];
+ }
+ else
+ {
+ val = is.read();
+ if (val == -1)
+ {
+ encodingError("unfinished multi-byte UTF-8 sequence at EOF",
+ -1, pos);
+ }
+ }
+
+ // Check for the correct bits at the start.
+ if ((val & 0xc0) != 0x80)
+ {
+ encodingError("bad continuation of multi-byte UTF-8 sequence",
+ val, pos + 1);
+ }
+
+ // Return the significant bits.
+ return (val & 0x3f);
+ }
+
+ /**
+ * Convert a buffer of US-ASCII or ISO-8859-1-encoded bytes into
+ * UTF-16 characters.
+ *
+ * <p>When readDataChunk () calls this method, the raw bytes are in
+ * rawReadBuffer, and the final characters will appear in
+ * readBuffer.
+ *
+ * @param count The number of bytes to convert.
+ * @param mask For ASCII conversion, 0x7f; else, 0xff.
+ * @see #readDataChunk
+ * @see #rawReadBuffer
+ * @see #readBuffer
+ */
+ private void copyIso8859_1ReadBuffer(int count, char mask)
+ throws IOException
+ {
+ int i, j;
+ for (i = 0, j = readBufferPos; i < count; i++, j++)
+ {
+ char c = (char) (rawReadBuffer[i] & 0xff);
+ if ((c & mask) != 0)
+ {
+ throw new CharConversionException("non-ASCII character U+"
+ + Integer.toHexString(c));
+ }
+ if (c == 0x0085 && xmlVersion == XML_11)
+ {
+ c = '\r';
+ }
+ readBuffer[j] = c;
+ if (c == '\r')
+ {
+ sawCR = true;
+ }
+ }
+ readBufferLength = j;
+ }
+
+ /**
+ * Convert a buffer of UCS-2-encoded bytes into UTF-16 characters
+ * (as used in Java string manipulation).
+ *
+ * <p>When readDataChunk () calls this method, the raw bytes are in
+ * rawReadBuffer, and the final characters will appear in
+ * readBuffer.
+ * @param count The number of bytes to convert.
+ * @param shift1 The number of bits to shift byte 1.
+ * @param shift2 The number of bits to shift byte 2
+ * @see #readDataChunk
+ * @see #rawReadBuffer
+ * @see #readBuffer
+ */
+ private void copyUcs2ReadBuffer(int count, int shift1, int shift2)
+ throws SAXException
+ {
+ int j = readBufferPos;
+
+ if (count > 0 && (count % 2) != 0)
+ {
+ encodingError("odd number of bytes in UCS-2 encoding", -1, count);
+ }
+ // The loops are faster with less internal brancing; hence two
+ if (shift1 == 0)
+ { // "UTF-16-LE"
+ for (int i = 0; i < count; i += 2)
+ {
+ char c = (char) (rawReadBuffer[i + 1] << 8);
+ c |= 0xff & rawReadBuffer[i];
+ readBuffer[j++] = c;
+ if (c == '\r')
+ {
+ sawCR = true;
+ }
+ }
+ }
+ else
+ { // "UTF-16-BE"
+ for (int i = 0; i < count; i += 2)
+ {
+ char c = (char) (rawReadBuffer[i] << 8);
+ c |= 0xff & rawReadBuffer[i + 1];
+ readBuffer[j++] = c;
+ if (c == '\r')
+ {
+ sawCR = true;
+ }
+ }
+ }
+ readBufferLength = j;
+ }
+
+ /**
+ * Convert a buffer of UCS-4-encoded bytes into UTF-16 characters.
+ *
+ * <p>When readDataChunk () calls this method, the raw bytes are in
+ * rawReadBuffer, and the final characters will appear in
+ * readBuffer.
+ * <p>Java has Unicode chars, and this routine uses surrogate pairs
+ * for ISO-10646 values between 0x00010000 and 0x000fffff. An
+ * exception is thrown if the ISO-10646 character has no Unicode
+ * representation.
+ *
+ * @param count The number of bytes to convert.
+ * @param shift1 The number of bits to shift byte 1.
+ * @param shift2 The number of bits to shift byte 2
+ * @param shift3 The number of bits to shift byte 2
+ * @param shift4 The number of bits to shift byte 2
+ * @see #readDataChunk
+ * @see #rawReadBuffer
+ * @see #readBuffer
+ */
+ private void copyUcs4ReadBuffer(int count, int shift1, int shift2,
+ int shift3, int shift4)
+ throws SAXException
+ {
+ int j = readBufferPos;
+
+ if (count > 0 && (count % 4) != 0)
+ {
+ encodingError("number of bytes in UCS-4 encoding " +
+ "not divisible by 4",
+ -1, count);
+ }
+ for (int i = 0; i < count; i += 4)
+ {
+ int value = (((rawReadBuffer [i] & 0xff) << shift1) |
+ ((rawReadBuffer [i + 1] & 0xff) << shift2) |
+ ((rawReadBuffer [i + 2] & 0xff) << shift3) |
+ ((rawReadBuffer [i + 3] & 0xff) << shift4));
+ if (value < 0x0000ffff)
+ {
+ readBuffer [j++] = (char) value;
+ if (value == (int) '\r')
+ {
+ sawCR = true;
+ }
+ }
+ else if (value < 0x0010ffff)
+ {
+ value -= 0x010000;
+ readBuffer[j++] = (char) (0xd8 | ((value >> 10) & 0x03ff));
+ readBuffer[j++] = (char) (0xdc | (value & 0x03ff));
+ }
+ else
+ {
+ encodingError("UCS-4 value out of range for Unicode",
+ value, i);
+ }
+ }
+ readBufferLength = j;
+ }
+
+ /**
+ * Report a character encoding error.
+ */
+ private void encodingError(String message, int value, int offset)
+ throws SAXException
+ {
+ if (value != -1)
+ {
+ message = message + " (character code: 0x" +
+ Integer.toHexString(value) + ')';
+ error(message);
+ }
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // Local Variables.
+ //////////////////////////////////////////////////////////////////////
+
+ /**
+ * Re-initialize the variables for each parse.
+ */
+ private void initializeVariables()
+ {
+ // First line
+ line = 1;
+ column = 0;
+
+ // Set up the buffers for data and names
+ dataBufferPos = 0;
+ dataBuffer = new char[DATA_BUFFER_INITIAL];
+ nameBufferPos = 0;
+ nameBuffer = new char[NAME_BUFFER_INITIAL];
+
+ // Set up the DTD hash tables
+ elementInfo = new HashMap();
+ entityInfo = new HashMap();
+ notationInfo = new HashMap();
+ skippedPE = false;
+
+ // Set up the variables for the current
+ // element context.
+ currentElement = null;
+ currentElementContent = CONTENT_UNDECLARED;
+
+ // Set up the input variables
+ sourceType = INPUT_NONE;
+ inputStack = new LinkedList();
+ entityStack = new LinkedList();
+ externalEntity = null;
+ tagAttributePos = 0;
+ tagAttributes = new String[100];
+ rawReadBuffer = new byte[READ_BUFFER_MAX];
+ readBufferOverflow = -1;
+
+ scratch = new InputSource();
+
+ inLiteral = false;
+ expandPE = false;
+ peIsError = false;
+
+ doReport = false;
+
+ inCDATA = false;
+
+ symbolTable = new Object[SYMBOL_TABLE_LENGTH][];
+ }
+
+ static class ExternalIdentifiers
+ {
+
+ String publicId;
+ String systemId;
+ String baseUri;
+
+ ExternalIdentifiers()
+ {
+ }
+
+ ExternalIdentifiers(String publicId, String systemId, String baseUri)
+ {
+ this.publicId = publicId;
+ this.systemId = systemId;
+ this.baseUri = baseUri;
+ }
+
+ }
+
+ static class EntityInfo
+ {
+
+ int type;
+ ExternalIdentifiers ids;
+ String value;
+ String notationName;
+
+ }
+
+ static class AttributeDecl
+ {
+
+ String type;
+ String value;
+ int valueType;
+ String enumeration;
+ String defaultValue;
+
+ }
+
+ static class ElementDecl
+ {
+
+ int contentType;
+ String contentModel;
+ HashMap attributes;
+
+ }
+
+ static class Input
+ {
+
+ int sourceType;
+ URLConnection externalEntity;
+ char[] readBuffer;
+ int readBufferPos;
+ int readBufferLength;
+ int line;
+ int encoding;
+ int readBufferOverflow;
+ InputStream is;
+ int currentByteCount;
+ int column;
+ Reader reader;
+
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/aelfred2/XmlReader.java b/libjava/classpath/gnu/xml/aelfred2/XmlReader.java
new file mode 100644
index 000000000..e0a047612
--- /dev/null
+++ b/libjava/classpath/gnu/xml/aelfred2/XmlReader.java
@@ -0,0 +1,373 @@
+/* XmlReader.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.aelfred2;
+
+import java.io.IOException;
+import java.util.Locale;
+
+import org.xml.sax.*;
+import org.xml.sax.ext.*;
+
+import gnu.xml.pipeline.EventFilter;
+import gnu.xml.pipeline.ValidationConsumer;
+
+
+/**
+ * This SAX2 parser optionally layers a validator over the &AElig;lfred2
+ * SAX2 parser. While this will not evaluate every XML validity constraint,
+ * it does support all the validity constraints that are of any real utility
+ * outside the strict SGML-compatible world. See the documentation for the
+ * SAXDriver class for information about the SAX2 features and properties
+ * that are supported, and documentation for the ValidationConsumer for
+ * information about what validity constraints may not be supported.
+ * (&AElig;lfred2 tests some of those, even in non-validating mode, to
+ * achieve better conformance.)
+ *
+ * <p> Note that due to its internal construction, you can't change most
+ * handlers until parse() returns. This diverges slightly from SAX, which
+ * expects later binding to be supported. Early binding involves less
+ * runtime overhead, which is an issue for event pipelines as used inside
+ * this parser. Rather than relying on the parser to handle late binding
+ * to your own handlers, do it yourself.
+ *
+ * @see SAXDriver
+ * @see gnu.xml.pipeline.ValidationConsumer
+ *
+ * @author David Brownell
+ */
+public final class XmlReader
+ implements XMLReader
+{
+
+ static class FatalErrorHandler
+ extends DefaultHandler2
+ {
+
+ public void error(SAXParseException e)
+ throws SAXException
+ {
+ throw e;
+ }
+
+ }
+
+ private SAXDriver aelfred2 = new SAXDriver();
+ private EventFilter filter = new EventFilter();
+ private boolean isValidating;
+ private boolean active;
+
+ /**
+ * Constructs a SAX Parser.
+ */
+ public XmlReader()
+ {
+ }
+
+ /**
+ * Constructs a SAX Parser, optionally treating validity errors
+ * as if they were fatal errors.
+ */
+ public XmlReader(boolean invalidIsFatal)
+ {
+ if (invalidIsFatal)
+ {
+ setErrorHandler(new FatalErrorHandler());
+ }
+ }
+
+ /**
+ * <b>SAX2</b>: Returns the object used to report the logical
+ * content of an XML document.
+ */
+ public ContentHandler getContentHandler()
+ {
+ return filter.getContentHandler();
+ }
+
+ /**
+ * <b>SAX2</b>: Assigns the object used to report the logical
+ * content of an XML document.
+ * @exception IllegalStateException if called mid-parse
+ */
+ public void setContentHandler(ContentHandler handler)
+ {
+ if (active)
+ {
+ throw new IllegalStateException("already parsing");
+ }
+ filter.setContentHandler(handler);
+ }
+
+ /**
+ * <b>SAX2</b>: Returns the object used to process declarations related
+ * to notations and unparsed entities.
+ */
+ public DTDHandler getDTDHandler()
+ {
+ return filter.getDTDHandler();
+ }
+
+ /**
+ * <b>SAX1</b> Assigns DTD handler
+ * @exception IllegalStateException if called mid-parse
+ */
+ public void setDTDHandler(DTDHandler handler)
+ {
+ if (active)
+ {
+ throw new IllegalStateException("already parsing");
+ }
+ filter.setDTDHandler(handler);
+ }
+
+ /**
+ * <b>SAX2</b>: Returns the object used when resolving external
+ * entities during parsing (both general and parameter entities).
+ */
+ public EntityResolver getEntityResolver()
+ {
+ return aelfred2.getEntityResolver();
+ }
+
+ /**
+ * <b>SAX1</b> Assigns parser's entity resolver
+ */
+ public void setEntityResolver(EntityResolver handler)
+ {
+ aelfred2.setEntityResolver(handler);
+ }
+
+ /**
+ * <b>SAX2</b>: Returns the object used to receive callbacks for XML
+ * errors of all levels (fatal, nonfatal, warning); this is never null;
+ */
+ public ErrorHandler getErrorHandler()
+ {
+ return aelfred2.getErrorHandler();
+ }
+
+ /**
+ * <b>SAX1</b> Assigns error handler
+ * @exception IllegalStateException if called mid-parse
+ */
+ public void setErrorHandler(ErrorHandler handler)
+ {
+ if (active)
+ {
+ throw new IllegalStateException("already parsing");
+ }
+ aelfred2.setErrorHandler(handler);
+ }
+
+ /**
+ * <b>SAX2</b>: Assigns the specified property.
+ * @exception IllegalStateException if called mid-parse
+ */
+ public void setProperty(String propertyId, Object value)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ if (active)
+ {
+ throw new IllegalStateException("already parsing");
+ }
+ if (getProperty(propertyId) != value)
+ {
+ filter.setProperty(propertyId, value);
+ }
+ }
+
+ /**
+ * <b>SAX2</b>: Returns the specified property.
+ */
+ public Object getProperty(String propertyId)
+ throws SAXNotRecognizedException
+ {
+ if ((SAXDriver.PROPERTY + "declaration-handler").equals(propertyId)
+ || (SAXDriver.PROPERTY + "lexical-handler").equals(propertyId))
+ {
+ return filter.getProperty(propertyId);
+ }
+ throw new SAXNotRecognizedException(propertyId);
+ }
+
+ private void forceValidating()
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ aelfred2.setFeature(SAXDriver.FEATURE + "namespace-prefixes",
+ true);
+ aelfred2.setFeature(SAXDriver.FEATURE + "external-general-entities",
+ true);
+ aelfred2.setFeature(SAXDriver.FEATURE + "external-parameter-entities",
+ true);
+ }
+
+ /**
+ * <b>SAX2</b>: Sets the state of features supported in this parser.
+ * Note that this parser requires reporting of namespace prefixes when
+ * validating.
+ */
+ public void setFeature(String featureId, boolean state)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ boolean value = getFeature(featureId);
+
+ if (state == value)
+ {
+ return;
+ }
+
+ if ((SAXDriver.FEATURE + "validation").equals(featureId))
+ {
+ if (active)
+ {
+ throw new SAXNotSupportedException("already parsing");
+ }
+ if (state)
+ {
+ forceValidating();
+ }
+ isValidating = state;
+ }
+ else
+ {
+ aelfred2.setFeature(featureId, state);
+ }
+ }
+
+ /**
+ * <b>SAX2</b>: Tells whether this parser supports the specified feature.
+ * At this time, this directly parallels the underlying SAXDriver,
+ * except that validation is optionally supported.
+ *
+ * @see SAXDriver
+ */
+ public boolean getFeature(String featureId)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ if ((SAXDriver.FEATURE + "validation").equals(featureId))
+ {
+ return isValidating;
+ }
+
+ return aelfred2.getFeature(featureId);
+ }
+
+ /**
+ * <b>SAX1</b>: Sets the locale used for diagnostics; currently,
+ * only locales using the English language are supported.
+ * @param locale The locale for which diagnostics will be generated
+ */
+ public void setLocale(Locale locale)
+ throws SAXException
+ {
+ aelfred2.setLocale(locale);
+ }
+
+ /**
+ * <b>SAX1</b>: Preferred API to parse an XML document, using a
+ * system identifier (URI).
+ */
+ public void parse(String systemId)
+ throws SAXException, IOException
+ {
+ parse(new InputSource(systemId));
+ }
+
+ /**
+ * <b>SAX1</b>: Underlying API to parse an XML document, used
+ * directly when no URI is available. When this is invoked,
+ * and the parser is set to validate, some features will be
+ * automatically reset to appropriate values: for reporting
+ * namespace prefixes, and incorporating external entities.
+ *
+ * @param source The XML input source.
+ *
+ * @exception IllegalStateException if called mid-parse
+ * @exception SAXException The handlers may throw any SAXException,
+ * and the parser normally throws SAXParseException objects.
+ * @exception IOException IOExceptions are normally through through
+ * the parser if there are problems reading the source document.
+ */
+ public void parse(InputSource source)
+ throws SAXException, IOException
+ {
+ EventFilter next;
+ boolean nsdecls;
+
+ synchronized (aelfred2)
+ {
+ if (active)
+ {
+ throw new IllegalStateException("already parsing");
+ }
+ active = true;
+ }
+
+ // set up the output pipeline
+ if (isValidating)
+ {
+ forceValidating();
+ next = new ValidationConsumer(filter);
+ }
+ else
+ {
+ next = filter;
+ }
+
+ // connect pipeline and error handler
+ // don't let _this_ call to bind() affect xmlns* attributes
+ nsdecls = aelfred2.getFeature(SAXDriver.FEATURE + "namespace-prefixes");
+ EventFilter.bind(aelfred2, next);
+ if (!nsdecls)
+ {
+ aelfred2.setFeature(SAXDriver.FEATURE + "namespace-prefixes",
+ false);
+ }
+
+ // parse, clean up
+ try
+ {
+ aelfred2.parse(source);
+ }
+ finally
+ {
+ active = false;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/aelfred2/package.html b/libjava/classpath/gnu/xml/aelfred2/package.html
new file mode 100644
index 000000000..e20425844
--- /dev/null
+++ b/libjava/classpath/gnu/xml/aelfred2/package.html
@@ -0,0 +1,506 @@
+<!DOCTYPE html PUBLIC
+ '-//W3C//DTD XHTML 1.0 Transitional//EN'
+ 'http://www.w3.org/TR/xhtml1/DTD/transitional.dtd'>
+
+<html><head>
+ <title>package overview</title>
+<!--
+/*
+ * Copyright (C) 1999,2000,2001 The Free Software Foundation, Inc.
+ */
+-->
+</head><body>
+
+<p> This package contains &AElig;lfred2, which includes an
+enhanced SAX2-compatible version of the &AElig;lfred
+non-validating XML parser, a modular (and hence optional)
+DTD validating parser, and modular (and hence optional)
+JAXP glue to those.
+Use these like any other SAX2 parsers. </p>
+
+<ul>
+ <li><a href="#about">About &AElig;lfred</a><ul>
+ <li><a href="#principles">Design Principles</a></li>
+ <li><a href="#name">About the Name &AElig;lfred</a></li>
+ <li><a href="#encodings">Character Encodings</a></li>
+ <li><a href="#violations">Known Conformance Violations</a></li>
+ <li><a href="#copyright">Licensing</a></li>
+ </ul></li>
+
+ <li><a href="#changes">Changes Since the Last Microstar Release</a><ul>
+ <li><a href="#sax2">SAX2 Support</a></li>
+ <li><a href="#validation">Validation</a></li>
+ <li><a href="#smaller">You Want Smaller?</a></li>
+ <li><a href="#bugfixes">Bugs Fixed</a></li>
+ </ul></li>
+
+</ul>
+
+<h2><a name="about">About &AElig;lfred</a></h2>
+
+<p>&AElig;lfred is a XML parser written in the java programming language.
+
+<h3><a name="principles">Design Principles</a></h3>
+
+<p>In most Java applets and applications, XML should not be the central
+feature; instead, XML is the means to another end, such as loading
+configuration information, reading meta-data, or parsing transactions.</p>
+
+<p> When an XML parser is only a single component of a much larger
+program, it cannot be large, slow, or resource-intensive. With Java
+applets, in particular, code size is a significant issue. The standard
+modem is still not operating at 56 Kbaud, or sometimes even with data
+compression. Assuming an uncompressed 28.8 Kbaud modem, only about
+3 KBytes can be downloaded in one second; compression often doubles
+that speed, but a V.90 modem may not provide another doubling. When
+used with embedded processors, similar size concerns apply. </p>
+
+<p> &AElig;lfred is designed for easy and efficient use over the Internet,
+based on the following principles: </p> <ol>
+
+<li> &AElig;lfred must be as small as possible, so that it doesn't add too
+ much to an applet's download time. </li>
+
+<li> &AElig;lfred must use as few class files as possible, to minimize the
+ number of HTTP connections necessary. (The use of JAR files has made this
+ be less of a concern.) </li>
+
+<li> &AElig;lfred must be compatible with most or all Java implementations
+ and platforms. (Write once, run anywhere.) </li>
+
+<li> &AElig;lfred must use as little memory as possible, so that it does
+ not take away resources from the rest of your program. (It doesn't force
+ you to use DOM or a similar costly data structure API.)</li>
+
+<li> &AElig;lfred must run as fast as possible, so that it does not slow down
+ the rest of your program. </li>
+
+<li> &AElig;lfred must produce correct output for well-formed and valid
+ documents, but need not reject every document that is not valid or
+ not well-formed. (In &AElig;lfred2, correctness was a bigger concern
+ than in the original version; and a validation option is available.) </li>
+
+<li> &AElig;lfred must provide full internationalization from the first
+ release. (&AElig;lfred2 now automatically handles all encodings
+ supported by the underlying JVM; previous versions handled only
+ UTF-8, UTF_16, ASCII, and ISO-8859-1.)</li>
+
+</ol>
+
+<p>As you can see from this list, &AElig;lfred is designed for production
+use, but neither validation nor perfect conformance was a requirement.
+Good validating parsers exist, including one in this package,
+and you should use them as appropriate. (See conformance reviews
+available at <a href="http://www.xml.com/">http://www.xml.com</a>)
+</p>
+
+<p> One of the main goals of &AElig;lfred2 was to significantly improve
+conformance, while not significantly affecting the other goals stated above.
+Since the only use of this parser is with SAX, some classes could be
+removed, and so the overall size of &AElig;lfred was actually reduced.
+Subsequent performance work produced a notable speedup (over twenty
+percent on larger files). That is, the tradeoffs between speed, size, and
+conformance were re-targeted towards conformance and support of newer APIs
+(SAX2), with a a positive performance impact. </p>
+
+<p> The role anticipated for this version of &AElig;lfred is as a
+lightweight Free Software SAX parser that can be used in essentially every
+Java program where the handful of conformance violations (noted below)
+are acceptable.
+That certainly includes applets, and
+nowadays one must also mention embedded systems as being even more
+size-critical.
+At this writing, all parsers that are more conformant are
+significantly larger, even when counting the optional
+validation support in this version of &AElig;lfred. </p>
+
+
+<h3><a name="name">About the Name <em>&AElig;lfred</em></a></h3>
+
+<p>&AElig;lfred the Great (AElfred in ASCII) was King of Wessex, and
+some say of King of England, at the time of his death in 899 AD.
+&AElig;lfred introduced a wide-spread literacy program in the hope that
+his people would learn to read English, at least, if Latin was too
+difficult for them. This &AElig;lfred hopes to bring another sort of
+literacy to Java, using XML, at least, if full SGML is too difficult.</p>
+
+<p>The initial &AElig; ligature ("AE)" is also a reminder that XML is
+not limited to ASCII.</p>
+
+
+<h3><a name="encodings">Character Encodings</a></h3>
+
+<p> The &AElig;lfred parser currently builds in support for a handful
+of input encodings. Of course these include UTF-8 and UTF-16, which
+all XML parsers are required to support:</p> <ul>
+
+ <li> UTF-8 ... the standard eight bit encoding, used unless
+ you provide an encoding declaration or a MIME charset tag.</li>
+
+ <li> US-ASCII ... an extremely common seven bit encoding,
+ which happens to be a subset of UTF-8 and ISO-8859-1 as well
+ as many other encodings. XHTML web pages using US-ASCII
+ (without an encoding declaration) are probably more
+ widely interoperable than those in any other encoding. </li>
+
+ <li> ISO-8859-1 ... includes accented characters used in
+ much of western Europe (but excluding the Euro currency
+ symbol).</li>
+
+ <li> UTF-16 ... with several variants, this encodes each
+ sixteen bit Unicode character in sixteen bits of output.
+ Variants include UTF-16BE (big endian, no byte order mark),
+ UTF-16LE (little endian, no byte order mark), and
+ ISO-10646-UCS-2 (an older and less used encoding, using a
+ version of Unicode without surrogate pairs). This is
+ essentially the native encoding used by Java. </li>
+
+ <li> ISO-10646-UCS-4 ... a seldom-used four byte encoding,
+ also known as UTF-32BE. Four byte order variants are supported,
+ including one known as UTF-32LE. Some operating systems
+ standardized on UCS-4 despite its significant size penalty,
+ in anticipation that Unicode (even with surrogate pairs)
+ would eventually become limiting. UCS-4 permits encoding
+ of non-Unicode characters, which Java can't represent (and
+ XML doesn't allow).
+ </li>
+
+ </ul>
+
+<p> If you use any encoding other than UTF-8 or UTF-16 you should
+make sure to label your data appropriately: </p>
+
+<blockquote>
+&lt;?xml version="1.0" encoding="<b>ISO-8859-15</b>"?&gt;
+</blockquote>
+
+<p> Encodings accessed through <code>java.io.InputStreamReader</code>
+are now fully supported for both external labels (such as MIME types)
+and internal types (as shown above).
+There is one limitation in the support for internal labels:
+the encodings must be derived from the US-ASCII encoding,
+the EBCDIC family of encodings is not recognized.
+Note that Java defines its
+own encoding names, which don't always correspond to the standard
+Internet encoding names defined by the IETF/IANA, and that Java
+may even <em>require</em> use of nonstandard encoding names.
+Please report
+such problems; some of them can be worked around in this parser,
+and many can be worked around by using external labels.
+</p>
+
+<p>Note that if you are using the Euro symbol with an fixed length
+eight bit encoding, you should probably be using the encoding label
+<em>iso-8859-15</em> or, with a Microsoft OS, <em>cp-1252</em>.
+Of course, UTF-8 and UTF-16 handle the Euro symbol directly.
+</p>
+
+
+<h3><a name="violations">Known Conformance Violations</a></h3>
+
+<p>Known conformance issues should be of negligible importance for
+most applications, and include: </p><ul>
+
+ <li> Rather than following the voluminous "Appendix B" rules about
+ what characters may appear in names (and name tokens), the Unicode
+ rules embedded in <em>java.lang.Character</em> are used.
+ This means mostly that some names are inappropriately accepted,
+ though a few are inappropriately rejected. (It's much simpler
+ to avoid that much special case code. Recent OASIS/NIST test
+ cases may have these rules be realistically testable.) </li>
+
+ <li> Text containing "]]&gt;" is not rejected unless it fully resides
+ in an internal buffer ... which is, thankfully, the typical case. This
+ text is illegal, but sometimes appears in illegal attempts to
+ nest CDATA sections. (Not catching that boundary condition
+ substantially simplifies parsing text.) </li>
+
+ <li> Surrogate characters that aren't correctly paired are ignored
+ rather than rejected, unless they were encoded using UTF-8. (This
+ simplifies parsing text.) Unicode 3.1 assigned the first characters
+ to those character codes, in early 2001, so few documents (or tools)
+ use such characters in any case. </li>
+
+ <li> Declarations following references to an undefined parameter
+ entity reference are not ignored. (Not maintaining and using state
+ about this validity error simplifies declaration handling; few
+ XML parsers address this constraint in any case.) </li>
+
+ <li> Well formedness constraints for general entity references
+ are not enforced. (The code to handle the "content" production
+ is merged with the element parsing code, making it hard to reuse
+ for this additional situation.) </li>
+
+</ul>
+
+<p> When tested against the July 12, 1999 version of the OASIS
+XML Conformance test suite, an earlier version passed 1057 of 1067 tests.
+That contrasts with the original version, which passed 867. The
+current parser is top-ranked in terms of conformance, as is its
+validating sibling (which has some additional conformance violations
+imposed on it by SAX2 API deficiencies as well as some of the more
+curious SGML layering artifacts found in the XML specification). </p>
+
+<p> The XML 1.0 specification itself was not without problems,
+and after some delays the W3C has come out with a revised
+"second edition" specification. While that doesn't resolve all
+the problems identified the XML specification, many of the most
+egregious problems have been resolved. (You still need to drink
+magic Kool-Aid before some DTD-related issues make sense.)
+To the extent possible, this parser conforms to that second
+edition specification, and does well against corrected versions
+of the OASIS/NIST XML conformance test cases. See <a href=
+"http://xmlconf.sourceforge.net">http://xmlconf.sourceforge.net</a>
+for more information about SAX2/XML conformance testing. </p>
+
+
+<h3><a name="copyright">Copyright and distribution terms</a></h3>
+
+<p>
+The software in this package is distributed under the GNU General Public
+License (with a special exception described below).
+</p>
+
+<p>
+A copy of GNU General Public License (GPL) is included in this distribution,
+in the file COPYING. If you do not have the source code, it is available at:
+
+ <a href="http://www.gnu.org/software/classpath/">http://www.gnu.org/software/classpath/</a>
+</p>
+
+<pre>
+ Linking this library statically or dynamically with other modules is
+ making a combined work based on this library. Thus, the terms and
+ conditions of the GNU General Public License cover the whole
+ combination.
+
+ As a special exception, the copyright holders of this library give you
+ permission to link this library with independent modules to produce an
+ executable, regardless of the license terms of these independent
+ modules, and to copy and distribute the resulting executable under
+ terms of your choice, provided that you also meet, for each linked
+ independent module, the terms and conditions of the license of that
+ module. An independent module is a module which is not derived from
+ or based on this library. If you modify this library, you may extend
+ this exception to your version of the library, but you are not
+ obligated to do so. If you do not wish to do so, delete this
+ exception statement from your version.
+
+ Parts derived from code which carried the following notice:
+
+ Copyright (c) 1997, 1998 by Microstar Software Ltd.
+
+ AElfred is free for both commercial and non-commercial use and
+ redistribution, provided that Microstar's copyright and disclaimer are
+ retained intact. You are free to modify AElfred for your own use and
+ to redistribute AElfred with your modifications, provided that the
+ modifications are clearly documented.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ merchantability or fitness for a particular purpose. Please use it AT
+ YOUR OWN RISK.
+</pre>
+
+<p> Some of this documentation was modified from the original
+&AElig;lfred README.txt file. All of it has been updated. </p>
+
+</p>
+
+
+<h2><a name="changes">Changes Since the last Microstar Release</a></h2>
+
+<p> As noted above, Microstar has not updated this parser since
+the summer of 1998, when it released version 1.2a on its web site.
+This release is intended to benefit the developer community by
+refocusing the API on SAX2, and improving conformance to the extent
+that most developers should not need to use another XML parser. </p>
+
+<p> The code has been cleaned up (referring to the XML 1.0 spec in
+all the production numbers in
+comments, rather than some preliminary draft, for one example) and
+has been sped up a bit as well.
+JAXP support has been added, although developers are still
+strongly encouraged to use the SAX2 APIs directly. </p>
+
+
+<h3><a name="sax2">SAX2 Support</a></h3>
+
+<p> The original version of &AElig;lfred did not support the
+SAX2 APIs. </p>
+
+<p> This version supports the SAX2 APIs, exposing the standard
+boolean feature descriptors. It supports the "DeclHandler" property
+to provide access to all DTD declarations not already exposed
+through the SAX1 API. The "LexicalHandler" property is supported,
+exposing entity boundaries (including the unnamed external subset) and
+things like comments and CDATA boundaries. SAX1 compatibility is
+currently provided.</p>
+
+
+<h3><a name="validation">Validation</a></h3>
+
+<p> In the 'pipeline' package in this same software distribution is an
+<a href="../pipeline/ValidationConsumer.html">XML Validation component</a>
+using any full SAX2 event stream (including all document type declarations)
+to validate. There is now a <a href="XmlReader.html">XmlReader</a> class
+which combines that class and this enhanced &AElig;lfred parser, creating
+an optionally validating SAX2 parser. </p>
+
+<p> As noted in the documentation for that validating component, certain
+validity constraints can't reliably be tested by a layered validator.
+These include all constraints relying on
+layering violations (exposing XML at the level of tokens or below,
+required since XML isn't a context-free grammar), some that
+SAX2 doesn't support, and a few others. The resulting validating
+parser is conformant enough for most applications that aren't doing
+strange SGML tricks with DTDs.
+Moreover, that validating filter can be used without
+a parser ... any application component that emits SAX event streams
+can DTD-validate its output on demand. </p>
+
+<h3><a name="smaller">You want Smaller?</a></h3>
+
+<p> You'll have noticed that the original version of &AElig;lfred
+had small size as a top goal. &AElig;lfred2 normally includes a
+DTD validation layer, but you can package without that.
+Similarly, JAXP factory support is available but optional.
+Then the main added cost due to this revision are for
+supporting the SAX2 API itself; DTD validation is as
+cleanly layered as allowed by SAX2.</p>
+
+<h3><a name="bugfixes">Bugs Fixed</a></h3>
+
+<p> Bugs fixed in &AElig;lfred2 include: </p>
+
+<ol>
+ <li> Originally &AElig;lfred didn't close file descriptors, which
+ led to file descriptor leakage on programs which ran for any
+ length of time. </li>
+
+ <li> NOTATION declarations without system identifiers are
+ now handled correctly. </li>
+
+ <li> DTD events are now reported for all invocations of a
+ given parser, not just the first one. </li>
+
+ <li> More correct character handling: <ul>
+
+ <li> Rejects out-of-range characters, both in text and in
+ character references. </li>
+
+ <li> Correctly handles character references that expand to
+ surrogate pairs. </li>
+
+ <li> Correctly handles UTF-8 encodings of surrogate pairs. </li>
+
+ <li> Correctly handles Unicode 3.1 rules about illegal UTF-8
+ encodings: there is only one legal encoding per character. </li>
+
+ <li> PUBLIC identifiers are now rejected if they have illegal
+ characters. </li>
+
+ <li> The parser is more correct about what characters are allowed
+ in names and name tokens. Uses Unicode rules (built in to Java)
+ rather than the voluminous XML rules, although some extensions
+ have been made to match XML rules more closely.</li>
+
+ <li> Line ends are now normalized to newlines in all known
+ cases. </li>
+
+ </ul></li>
+
+ <li> Certain validity errors were previously treated as well
+ formedness violations. <ul>
+
+ <li> Repeated declarations of an element type are no
+ longer fatal errors. </li>
+
+ <li> Undeclared parameter entity references are no longer
+ fatal errors. </li>
+
+ </ul></li>
+
+ <li> Attribute handling is improved: <ul>
+
+ <li> Whitespace must exist between attributes. </li>
+
+ <li> Only one value for a given attribute is permitted. </li>
+
+ <li> ATTLIST declarations don't need to declare attributes. </li>
+
+ <li> Attribute values are normalized when required. </li>
+
+ <li> Tabs in attribute values are normalized to spaces. </li>
+
+ <li> Attribute values containing a literal "&lt;" are rejected. </li>
+
+ </ul></li>
+
+ <li> More correct entity handling: <ul>
+
+ <li> Whitespace must precede NDATA when declaring unparsed
+ entities.</li>
+
+ <li> Parameter entity declarations may not have NDATA annotations. </li>
+
+ <li> The XML specification has a bug in that it doesn't specify
+ that certain contexts exist within which parameter entity
+ expansion must not be performed. Lacking an offical erratum,
+ this parser now disables such expansion inside comments,
+ processing instructions, ignored sections, public identifiers,
+ and parts of entity declarations. </li>
+
+ <li> Entity expansions that include quote characters no longer
+ confuse parsing of strings using such expansions. </li>
+
+ <li> Whitespace in the values of internal entities is not mapped
+ to space characters. </li>
+
+ <li> General Entity references in attribute defaults within the
+ DTD now cause fatal errors when the entity is not defined at the
+ time it is referenced. </li>
+
+ <li> Malformed general entity references in entity declarations are
+ now detected. </li>
+
+ </ul></li>
+
+ <li> Neither conditional sections
+ nor parameter entity references within markup declarations
+ are permitted in the internal subset. </li>
+
+ <li> Processing instructions whose target names are "XML"
+ (ignoring case) are now rejected. </li>
+
+ <li> Comments may not include "--".</li>
+
+ <li> Most "]]&gt;" sequences in text are rejected. </li>
+
+ <li> Correct syntax for standalone declarations is enforced. </li>
+
+ <li> Setting a locale for diagnostics only produces an exception
+ if the language of that locale isn't English. </li>
+
+ <li> Some more encoding names are recognized. These include the
+ Unicode 3.0 variants of UTF-16 (UTF-16BE, UTF-16LE) as well as
+ US-ASCII and a few commonly seen synonyms. </li>
+
+ <li> Text (from character content, PIs, or comments) large enough
+ not to fit into internal buffers is now handled correctly even in
+ some cases which were originally handled incorrectly.</li>
+
+ <li> Content is now reported for element types for which attributes
+ have been declared, but no content model is known. (Such documents
+ are invalid, but may still be well formed.) </li>
+
+</ol>
+
+<p> Other bugs may also have been fixed. </p>
+
+<p> For better overall validation support, some of the validity
+constraints that can't be verified using the SAX2 event stream
+are now reported directly by &AElig;lfred2. </p>
+
+</body></html>
+
diff --git a/libjava/classpath/gnu/xml/dom/Consumer.java b/libjava/classpath/gnu/xml/dom/Consumer.java
new file mode 100644
index 000000000..836c8d257
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/Consumer.java
@@ -0,0 +1,352 @@
+/* Consumer.java --
+ Copyright (C) 2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.xml.dom;
+
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.ext.Attributes2;
+
+import gnu.xml.pipeline.DomConsumer;
+import gnu.xml.pipeline.EventConsumer;
+
+
+/**
+ * Event consumer which constructs DOM documents using the implementation
+ * in this package, using SAX2 events. This packages various backdoors
+ * into this DOM implementation, as needed to address DOM requirements
+ * that can't be met by strictly conforming implementations of DOM.
+ *
+ * <p> These requirements all relate to {@link DocumentType} nodes and
+ * features of that node type. These features are normally not used,
+ * because that interface only exposes a subset of the information found
+ * in DTDs. More, that subset does not include the most important typing
+ * information. For example, it excludes element content models and
+ * attribute typing. It does expose some entity management issues,
+ * although entity management doesn't relate to document typing.
+ *
+ * <p> Note that SAX2 does not expose the literal text of the DTD's
+ * internal subset, so it will not be present in DOM trees constructed
+ * using this API. (Though with a good SAX2 implementation, it could
+ * be partially recreated...)
+ *
+ * @author David Brownell
+ */
+public class Consumer extends DomConsumer
+{
+ /**
+ * Constructs an unconfigured event consumer,
+ * as a terminus in a SAX event pipeline.
+ */
+ // used by PipelineFactory [terminus]
+ public Consumer ()
+ throws SAXException
+ {
+ super (DomDocument.class);
+ setHandler (new Backdoor (this));
+ }
+
+ /**
+ * Constructs an unconfigured event consumer,
+ * as a stage in a SAX event pipeline.
+ */
+ // used by PipelineFactory [filter]
+ public Consumer (EventConsumer next)
+ throws SAXException
+ {
+ super (DomDocument.class, next);
+ setHandler (new Backdoor (this));
+ }
+
+ /**
+ * Implements the backdoors needed by DOM.
+ * All methods in this class use implementation-specific APIs that are
+ * implied by the DOM specification (needed to implement testable
+ * behavior) but which are excluded from the DOM specification.
+ */
+ public static class Backdoor extends DomConsumer.Handler
+ {
+ /**
+ * Constructor.
+ * @param consumer must have been initialized to use the
+ * {@link DomDocument} class (or a subclass) for
+ * constructing DOM trees
+ */
+ protected Backdoor (DomConsumer consumer)
+ throws SAXException
+ { super (consumer); }
+
+ // helper routine
+ private DomDoctype getDoctype ()
+ throws SAXException
+ {
+ DomDocument doc = (DomDocument) getDocument ();
+ DocumentType dt = doc.getDoctype ();
+
+ if (dt == null)
+ throw new SAXException ("doctype missing!");
+ return (DomDoctype) dt;
+ }
+
+ // SAX2 "lexical" event
+ public void startDTD (String name, String publicId, String systemId)
+ throws SAXException
+ {
+ DomDocument doc = (DomDocument) getDocument ();
+
+ super.startDTD (name, publicId, systemId);
+ // DOM L2 doctype creation model is bizarre
+ DomDoctype dt = new DomDoctype (doc, name, publicId, systemId);
+ doc.appendChild (dt);
+ }
+
+ // SAX2 "lexical" event
+ public void endDTD ()
+ throws SAXException
+ {
+ super.endDTD ();
+ // DOM L2 has no way to make things readonly
+ getDoctype ().makeReadonly ();
+ }
+
+ // SAX1 DTD event
+ public void notationDecl (
+ String name,
+ String publicId, String systemId
+ ) throws SAXException
+ {
+ // DOM L2 can't create/save notation nodes
+ getDoctype ().declareNotation (name, publicId, systemId);
+ }
+
+ // SAX1 DTD event
+ public void unparsedEntityDecl (
+ String name,
+ String publicId, String systemId,
+ String notationName
+ ) throws SAXException
+ {
+ // DOM L2 can't create/save entity nodes
+ getDoctype ().declareEntity (name, publicId, systemId,
+ notationName);
+ }
+
+ // SAX2 declaration event
+ public void internalEntityDecl (String name, String value)
+ throws SAXException
+ {
+ // DOM L2 can't create/save entity nodes
+ // NOTE: this doesn't save the value as a child of this
+ // node, though it could realistically do so.
+ getDoctype ().declareEntity (name, null, null, null);
+ }
+
+ // SAX2 declaration event
+ public void externalEntityDecl (
+ String name,
+ String publicId,
+ String systemId
+ ) throws SAXException
+ {
+ // DOM L2 can't create/save entity nodes
+ // NOTE: DOM allows for these to have children, if
+ // they don't have unbound namespace references.
+ getDoctype ().declareEntity (name, publicId, systemId, null);
+ }
+
+ // SAX2 element
+ public void startElement (
+ String uri,
+ String localName,
+ String qName,
+ Attributes atts
+ ) throws SAXException
+ {
+ Node top;
+
+ super.startElement (uri, localName, qName, atts);
+
+ // might there be more work?
+ top = getTop ();
+ if (!top.hasAttributes () || !(atts instanceof Attributes2))
+ return;
+
+ // remember any attributes that got defaulted
+ DomNamedNodeMap map = (DomNamedNodeMap) top.getAttributes ();
+ Attributes2 attrs = (Attributes2) atts;
+ int length = atts.getLength ();
+
+ //map.compact ();
+ for (int i = 0; i < length; i++) {
+ if (attrs.isSpecified (i))
+ continue;
+
+ // value was defaulted.
+ String temp = attrs.getQName (i);
+ DomAttr attr;
+
+ if ("".equals (temp))
+ attr = (DomAttr) map.getNamedItemNS (attrs.getURI (i),
+ atts.getLocalName (i));
+ else
+ attr = (DomAttr) map.getNamedItem (temp);
+
+ // DOM L2 can't write this flag, only read it
+ attr.setSpecified (false);
+ }
+ }
+
+ public void endElement (
+ String uri,
+ String localName,
+ String qName
+ ) throws SAXException
+ {
+ DomNode top = (DomNode) getTop ();
+ top.compact ();
+ super.endElement (uri, localName, qName);
+ }
+
+ protected Text createText (
+ boolean isCDATA,
+ char buf [],
+ int off,
+ int len
+ ) {
+ DomDocument doc = (DomDocument) getDocument ();
+
+ if (isCDATA)
+ return doc.createCDATASection (buf, off, len);
+ else
+ return doc.createTextNode (buf, off, len);
+ }
+
+ public void elementDecl(String name, String model)
+ throws SAXException
+ {
+ getDoctype().elementDecl(name, model);
+ }
+
+ public void attributeDecl (
+ String ename,
+ String aname,
+ String type,
+ String mode,
+ String value
+ ) throws SAXException
+ {
+ getDoctype().attributeDecl(ename, aname, type, mode, value);
+ /*
+ if (value == null && !"ID".equals (type))
+ return;
+
+ DomDoctype.ElementInfo info;
+
+ info = getDoctype ().getElementInfo (ename);
+ if (value != null)
+ info.setAttrDefault (aname, value);
+ if ("ID".equals (type))
+ info.setIdAttr (aname);
+ */
+
+ }
+
+ // force duplicate name checking off while we're
+ // using parser output (don't duplicate the work)
+ public void startDocument () throws SAXException
+ {
+ super.startDocument ();
+
+ DomDocument doc = (DomDocument) getDocument ();
+ doc.setStrictErrorChecking(false);
+ doc.setBuilding(true);
+ }
+
+ public void endDocument ()
+ throws SAXException
+ {
+ DomDocument doc = (DomDocument) getDocument ();
+ doc.setStrictErrorChecking(true);
+ doc.setBuilding(false);
+ doc.compact ();
+ DomDoctype doctype = (DomDoctype) doc.getDoctype();
+ if (doctype != null)
+ {
+ doctype.makeReadonly();
+ }
+ super.endDocument ();
+ }
+
+ // these three methods collaborate to populate entity
+ // refs, marking contents readonly on end-of-entity
+
+ public boolean canPopulateEntityRefs ()
+ { return true; }
+
+ public void startEntity (String name)
+ throws SAXException
+ {
+ if (name.charAt (0) == '%' || "[dtd]".equals (name))
+ return;
+ super.startEntity (name);
+
+ DomNode top = (DomNode) getTop ();
+
+ if (top.getNodeType () == Node.ENTITY_REFERENCE_NODE)
+ top.readonly = false;
+ }
+
+ public void endEntity (String name)
+ throws SAXException
+ {
+ if (name.charAt (0) == '%' || "[dtd]".equals (name))
+ return;
+ DomNode top = (DomNode) getTop ();
+
+ if (top.getNodeType () == Node.ENTITY_REFERENCE_NODE) {
+ top.compact ();
+ top.makeReadonly ();
+ }
+ super.endEntity (name);
+ }
+ }
+}
diff --git a/libjava/classpath/gnu/xml/dom/DTDAttributeTypeInfo.java b/libjava/classpath/gnu/xml/dom/DTDAttributeTypeInfo.java
new file mode 100644
index 000000000..d6e25529d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DTDAttributeTypeInfo.java
@@ -0,0 +1,83 @@
+/* DTDAttributeTypeInfo.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.TypeInfo;
+
+/**
+ * Attribute type information supplied by a DTD attribute declaration.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class DTDAttributeTypeInfo
+ implements TypeInfo
+{
+
+ final String elementName;
+ final String name;
+ final String type;
+ final String mode;
+ final String value;
+
+ DTDAttributeTypeInfo(String elementName, String name, String type,
+ String mode, String value)
+ {
+ this.elementName = elementName;
+ this.name = name;
+ this.type = type;
+ this.mode = mode;
+ this.value = value;
+ }
+
+ public final String getTypeName()
+ {
+ return type;
+ }
+
+ public final String getTypeNamespace()
+ {
+ return "http://www.w3.org/TR/REC-xml";
+ }
+
+ public final boolean isDerivedFrom(String typeNamespace, String typeName,
+ int derivationMethod)
+ {
+ return false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DTDElementTypeInfo.java b/libjava/classpath/gnu/xml/dom/DTDElementTypeInfo.java
new file mode 100644
index 000000000..9063d4c79
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DTDElementTypeInfo.java
@@ -0,0 +1,111 @@
+/* DTDElementTypeInfo.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import org.w3c.dom.TypeInfo;
+
+/**
+ * Element type information provided by a DTD element declaration.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class DTDElementTypeInfo
+ implements TypeInfo
+{
+
+ final String name;
+ String model;
+ HashMap attributes;
+ String idAttrName;
+
+ DTDElementTypeInfo(String name, String model)
+ {
+ this.name = name;
+ this.model = model;
+ }
+
+ public final String getTypeName()
+ {
+ return null;
+ }
+
+ public final String getTypeNamespace()
+ {
+ return "http://www.w3.org/TR/REC-xml";
+ }
+
+ public final boolean isDerivedFrom(String typeNamespace, String typeName,
+ int derivationMethod)
+ {
+ return false;
+ }
+
+ DTDAttributeTypeInfo getAttributeTypeInfo(String name)
+ {
+ if (attributes == null)
+ {
+ return null;
+ }
+ return (DTDAttributeTypeInfo) attributes.get(name);
+ }
+
+ void setAttributeTypeInfo(String name, DTDAttributeTypeInfo info)
+ {
+ if (attributes == null)
+ {
+ attributes = new HashMap();
+ }
+ attributes.put(name, info);
+ if ("ID".equals(info.type))
+ {
+ idAttrName = name;
+ }
+ }
+
+ Iterator attributes()
+ {
+ if (attributes == null)
+ {
+ return null;
+ }
+ return attributes.values().iterator();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomAttr.java b/libjava/classpath/gnu/xml/dom/DomAttr.java
new file mode 100644
index 000000000..421baf76a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomAttr.java
@@ -0,0 +1,411 @@
+/* DomAttr.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import gnu.java.lang.CPStringBuilder;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.TypeInfo;
+import org.w3c.dom.events.MutationEvent;
+
+
+/**
+ * <p> "Attr" implementation. In DOM, attributes cost quite a lot of
+ * memory because their values are complex structures rather than just
+ * simple strings. To reduce your costs, avoid having more than one
+ * child of an attribute; stick to a single Text node child, and ignore
+ * even that by using the attribute's "nodeValue" property.</p>
+ *
+ * <p> As a bit of general advice, only look at attribute modification
+ * events through the DOMAttrModified event (sent to the associated
+ * element). Implementations are not guaranteed to report other events
+ * in the same order, so you're very likely to write nonportable code if
+ * you monitor events at the "children of Attr" level.</p>
+ *
+ * <p> At this writing, not all attribute modifications will cause the
+ * DOMAttrModified event to be triggered ... only the ones using the string
+ * methods (setNodeValue, setValue, and Element.setAttribute) to modify
+ * those values. That is, if you manipulate those children directly,
+ * elements won't get notified that attribute values have changed.
+ * The natural fix for that will report other modifications, but won't
+ * be able to expose "previous" attribute value; it'll need to be cached
+ * or something (at which point why bother using child nodes). </p>
+ *
+ * <p><em>You are strongly advised not to use "children" of any attribute
+ * nodes you work with.</em> </p>
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomAttr
+ extends DomNsNode
+ implements Attr
+{
+
+ private boolean specified;
+ private String value; // string value cache
+
+ /**
+ * Constructs an Attr node associated with the specified document.
+ * The "specified" flag is initialized to true, since this DOM has
+ * no current "back door" mechanisms to manage default values so
+ * that every value must effectively be "specified".
+ *
+ * <p>This constructor should only be invoked by a Document as part of
+ * its createAttribute functionality, or through a subclass which is
+ * similarly used in a "Sub-DOM" style layer.
+ *
+ * @param owner The document with which this node is associated
+ * @param namespaceURI Combined with the local part of the name,
+ * this is used to uniquely identify a type of attribute
+ * @param name Name of this attribute, which may include a prefix
+ */
+ protected DomAttr(DomDocument owner, String namespaceURI, String name)
+ {
+ super(ATTRIBUTE_NODE, owner, namespaceURI, name);
+ specified = true;
+ length = 1;
+
+ // XXX register self to get insertion/removal events
+ // and character data change events and when they happen,
+ // report self-mutation
+ }
+
+ /**
+ * Constructs an Attr node associated with the specified document.
+ * The "specified" flag is initialized to true, since this DOM has
+ * no current "back door" mechanisms to manage default values so
+ * that every value must effectively be "specified".
+ *
+ * <p>This constructor should only be invoked by a Document as part of
+ * its createAttribute functionality, or through a subclass which is
+ * similarly used in a "Sub-DOM" style layer.
+ * <p>
+ * With this constructor, the prefix and local part are given explicitly
+ * rather than being computed. This allows them to be explicitly set to
+ * {@code null} as required by {@link Document#createAttribute(String)}.
+ * </p>
+ *
+ * @param owner The document with which this node is associated
+ * @param namespaceURI Combined with the local part of the name,
+ * this is used to uniquely identify a type of attribute
+ * @param name Name of this attribute, which may include a prefix
+ * @param prefix the namespace prefix of the name. May be {@code null}.
+ * @param localName the local part of the name. May be {@code null}.
+ */
+ protected DomAttr(DomDocument owner, String namespaceURI, String name,
+ String prefix, String localName)
+ {
+ super(ATTRIBUTE_NODE, owner, namespaceURI, name, prefix, localName);
+ specified = true;
+ length = 1;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the attribute name (same as getNodeName)
+ */
+ public final String getName()
+ {
+ return getNodeName();
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns true if a parser reported this was in the source text.
+ */
+ public final boolean getSpecified()
+ {
+ return specified;
+ }
+
+ /**
+ * Records whether this attribute was in the source text.
+ */
+ public final void setSpecified(boolean value)
+ {
+ specified = value;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the attribute value, with character and entity
+ * references substituted.
+ * <em>NOTE: entity refs as children aren't currently handled.</em>
+ */
+ public String getNodeValue()
+ {
+ // If we have a simple node-value, use that
+ if (first == null)
+ {
+ return (value == null) ? "" : value;
+ }
+ // Otherwise collect child node-values
+ CPStringBuilder buf = new CPStringBuilder();
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ switch (ctx.nodeType)
+ {
+ case Node.TEXT_NODE:
+ buf.append(ctx.getNodeValue());
+ break;
+ case Node.ENTITY_REFERENCE_NODE:
+ // TODO
+ break;
+ }
+ }
+ return buf.toString();
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Assigns the value of the attribute; it will have one child,
+ * which is a text node with the specified value (same as
+ * setNodeValue).
+ */
+ public final void setValue(String value)
+ {
+ setNodeValue(value);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the value of the attribute as a non-null string; same
+ * as getNodeValue.
+ * <em>NOTE: entity refs as children aren't currently handled.</em>
+ */
+ public final String getValue()
+ {
+ return getNodeValue();
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Assigns the attribute value; using this API, no entity or
+ * character references will exist.
+ * Causes a DOMAttrModified mutation event to be sent.
+ */
+ public void setNodeValue(String value)
+ {
+ if (readonly)
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+ if (value == null)
+ {
+ value = "";
+ }
+ String oldValue = getNodeValue();
+ while (last != null)
+ {
+ removeChild(last);
+ }
+ // don't create a new node just for this...
+ /*
+ Node text = owner.createTextNode(value);
+ appendChild(text);
+ */
+ this.value = value;
+ length = 1;
+ specified = true;
+
+ mutating(oldValue, value, MutationEvent.MODIFICATION);
+ }
+
+ public final Node getFirstChild()
+ {
+ // Create a child text node if necessary
+ if (first == null)
+ {
+ length = 0;
+ Node text = owner.createTextNode((value == null) ? "" : value);
+ appendChild(text);
+ }
+ return first;
+ }
+
+ public final Node getLastChild()
+ {
+ // Create a child text node if necessary
+ if (last == null)
+ {
+ length = 0;
+ Node text = owner.createTextNode((value == null) ? "" : value);
+ appendChild(text);
+ }
+ return last;
+ }
+
+ public Node item(int index)
+ {
+ // Create a child text node if necessary
+ if (first == null)
+ {
+ length = 0;
+ Node text = owner.createTextNode((value == null) ? "" : value);
+ appendChild(text);
+ }
+ return super.item(index);
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns the element with which this attribute is associated.
+ */
+ public final Element getOwnerElement()
+ {
+ return (Element) parent;
+ }
+
+ public final Node getNextSibling()
+ {
+ return null;
+ }
+
+ public final Node getPreviousSibling()
+ {
+ return null;
+ }
+
+ public Node getParentNode()
+ {
+ return null;
+ }
+
+ /**
+ * Records the element with which this attribute is associated.
+ */
+ public final void setOwnerElement(Element e)
+ {
+ if (parent != null)
+ {
+ throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR);
+ }
+ if (!(e instanceof DomElement))
+ {
+ throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR);
+ }
+ parent = (DomElement) e;
+ depth = parent.depth + 1;
+ }
+
+ /**
+ * The base URI of an Attr is always <code>null</code>.
+ */
+ public final String getBaseURI()
+ {
+ return null;
+ }
+
+ /**
+ * Shallow clone of the attribute, breaking all ties with any
+ * elements.
+ */
+ public Object clone()
+ {
+ DomAttr retval = (DomAttr) super.clone();
+ retval.specified = true;
+ return retval;
+ }
+
+ private void mutating(String oldValue, String newValue, short why)
+ {
+ if (!reportMutations || parent == null || equal(newValue, oldValue))
+ {
+ return;
+ }
+
+ // EVENT: DOMAttrModified, target = parent,
+ // prev/new values provided, also attr name
+ MutationEvent event;
+
+ event = (MutationEvent) createEvent ("MutationEvents");
+ event.initMutationEvent ("DOMAttrModified",
+ true /* bubbles */, false /* nocancel */,
+ null, oldValue, newValue, getNodeName (), why);
+ parent.dispatchEvent (event);
+ }
+
+ // DOM Level 3 methods
+
+ public TypeInfo getSchemaTypeInfo()
+ {
+ if (parent != null)
+ {
+ // DTD implementation
+ DomDoctype doctype = (DomDoctype) parent.owner.getDoctype();
+ if (doctype != null)
+ {
+ return doctype.getAttributeTypeInfo(parent.getNodeName(),
+ getNodeName());
+ }
+ // TODO XML Schema implementation
+ }
+ return null;
+ }
+
+ public boolean isId()
+ {
+ if (parent != null)
+ {
+ DomDoctype doctype = (DomDoctype) parent.owner.getDoctype();
+ if (doctype != null)
+ {
+ DTDAttributeTypeInfo info =
+ doctype.getAttributeTypeInfo(parent.getNodeName(),
+ getNodeName());
+ if (info != null && "ID".equals(info.type))
+ {
+ return true;
+ }
+ }
+ DomElement element = (DomElement) parent;
+ if (element.userIdAttrs != null &&
+ element.userIdAttrs.contains(this))
+ {
+ return true;
+ }
+ // TODO XML Schema implementation
+ }
+ return false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomCDATASection.java b/libjava/classpath/gnu/xml/dom/DomCDATASection.java
new file mode 100644
index 000000000..770f7d5dd
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomCDATASection.java
@@ -0,0 +1,90 @@
+/* DomCDATASection.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.CDATASection;
+
+/**
+ * <p> "CDATASection" implementation.
+ * This is a non-core DOM class, supporting the "XML" feature.
+ * CDATA sections are just ways to represent text using different
+ * delimeters. </p>
+ *
+ * <p> <em>You are strongly advised not to use CDATASection nodes.</em>
+ * The advantage of having slightly prettier ways to print text that may
+ * have lots of embedded XML delimiters, such as "&amp;" and "&lt;",
+ * can be dwarfed by the cost of dealing with multiple kinds of text
+ * nodes in all your algorithms. </p>
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomCDATASection
+ extends DomText
+ implements CDATASection
+{
+
+ /**
+ * Constructs a CDATA section node associated with the specified
+ * document and holding the specified data.
+ *
+ * <p>This constructor should only be invoked by a Document as part of
+ * its createCDATASection functionality, or through a subclass which is
+ * similarly used in a "Sub-DOM" style layer.
+ *
+ */
+ protected DomCDATASection(DomDocument owner, String value)
+ {
+ super(CDATA_SECTION_NODE, owner, value);
+ }
+
+ protected DomCDATASection(DomDocument owner, char buf [], int off, int len)
+ {
+ super(CDATA_SECTION_NODE, owner, buf, off, len);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the string "#cdata-section".
+ */
+ final public String getNodeName()
+ {
+ return "#cdata-section";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomCharacterData.java b/libjava/classpath/gnu/xml/dom/DomCharacterData.java
new file mode 100644
index 000000000..396c5f7a7
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomCharacterData.java
@@ -0,0 +1,344 @@
+/* DomCharacterData.java --
+ Copyright (C) 1999,2000,2001,2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.CharacterData;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.events.MutationEvent;
+
+
+/**
+ * <p> Abstract "CharacterData" implementation. This
+ * facilitates reusing code in classes implementing subtypes of that DOM
+ * interface (Text, Comment, CDATASection). </p>
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public abstract class DomCharacterData
+ extends DomNode
+ implements CharacterData
+{
+
+ /**
+ * Empty node list representing the children of character data nodes.
+ */
+ static class EmptyNodeList
+ implements NodeList
+ {
+
+ public int getLength()
+ {
+ return 0;
+ }
+
+ public Node item(int index)
+ {
+ return null;
+ }
+
+ }
+
+ /**
+ * Singleton empty node list for character data nodes.
+ */
+ static final NodeList CHILD_NODES = new EmptyNodeList();
+
+ private String text;
+
+ // package private
+ DomCharacterData(short nodeType, DomDocument doc, String value)
+ {
+ super(nodeType, doc);
+ text = (value == null) ? "" : value;
+ }
+
+ // package private
+ DomCharacterData(short nodeType, DomDocument doc,
+ char[] buf, int offset, int length)
+ {
+ super(nodeType, doc);
+ text = (buf == null) ? "" : new String(buf, offset, length);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Appends the specified data to the value of this node.
+ * Causes a DOMCharacterDataModified mutation event to be reported.
+ */
+ public void appendData(String arg)
+ {
+ if (isReadonly())
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+ String value = text + arg;
+ mutating(value);
+ text = value;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Modifies the value of this node.
+ * Causes a DOMCharacterDataModified mutation event to be reported.
+ */
+ public void deleteData(int offset, int count)
+ {
+ if (isReadonly())
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+ char[] raw = text.toCharArray();
+ if (offset < 0 || count < 0 || offset > raw.length)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ if ((offset + count) > raw.length)
+ {
+ count = raw.length - offset;
+ }
+ if (count == 0)
+ {
+ return;
+ }
+ try
+ {
+ char[] buf = new char[raw.length - count];
+ System.arraycopy(raw, 0, buf, 0, offset);
+ System.arraycopy(raw, offset + count, buf, offset,
+ raw.length - (offset + count));
+ String value = new String(buf);
+ mutating(value);
+ text = value;
+ }
+ catch (IndexOutOfBoundsException x)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the value of this node.
+ */
+ public String getNodeValue()
+ {
+ return text;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the value of this node; same as getNodeValue.
+ */
+ public final String getData()
+ {
+ return text;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the length of the data.
+ */
+ public int getLength()
+ {
+ return text.length();
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Modifies the value of this node.
+ */
+ public void insertData(int offset, String arg)
+ {
+ if (isReadonly())
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+ char[] raw = text.toCharArray();
+ char[] tmp = arg.toCharArray ();
+ char[] buf = new char[raw.length + tmp.length];
+
+ try
+ {
+ System.arraycopy(raw, 0, buf, 0, offset);
+ System.arraycopy(tmp, 0, buf, offset, tmp.length);
+ System.arraycopy(raw, offset, buf, offset + tmp.length,
+ raw.length - offset);
+ String value = new String(buf);
+ mutating(value);
+ text = value;
+ }
+ catch (IndexOutOfBoundsException x)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Modifies the value of this node. Causes DOMCharacterDataModified
+ * mutation events to be reported (at least one).
+ */
+ public void replaceData(int offset, int count, String arg)
+ {
+ if (readonly)
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+ char[] raw = text.toCharArray();
+
+ // deleteData
+ if (offset < 0 || count < 0 || offset > raw.length)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ if ((offset + count) > raw.length)
+ {
+ count = raw.length - offset;
+ }
+ try
+ {
+ char[] buf = new char[raw.length - count];
+ System.arraycopy(raw, 0, buf, 0, offset);
+ System.arraycopy(raw, offset + count, buf, offset,
+ raw.length - (offset + count));
+
+ // insertData
+ char[] tmp = arg.toCharArray ();
+ char[] buf2 = new char[buf.length + tmp.length];
+ System.arraycopy(raw, 0, buf, 0, offset);
+ System.arraycopy(tmp, 0, buf, offset, tmp.length);
+ System.arraycopy(raw, offset, buf, offset + tmp.length,
+ raw.length - offset);
+ String value = new String(buf);
+ mutating(value);
+ text = value;
+ }
+ catch (IndexOutOfBoundsException x)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Assigns the value of this node.
+ * Causes a DOMCharacterDataModified mutation event to be reported.
+ */
+ public void setNodeValue(String value)
+ {
+ if (isReadonly())
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+ if (value == null)
+ {
+ value = "";
+ }
+ mutating(value);
+ text = value;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Assigns the value of this node; same as setNodeValue.
+ */
+ final public void setData(String data)
+ {
+ setNodeValue(data);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the specified substring.
+ */
+ public String substringData(int offset, int count)
+ {
+ try
+ {
+ return text.substring(offset, count);
+ }
+ catch (StringIndexOutOfBoundsException e)
+ {
+ if (offset >= 0 && count >= 0 && offset < text.length())
+ {
+ return text.substring(offset);
+ }
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ }
+
+ /**
+ * Returns an empty node list.
+ * Character data nodes do not have children.
+ */
+ public NodeList getChildNodes()
+ {
+ return CHILD_NODES;
+ }
+
+ /**
+ * The base URI for character data is <code>null</code>.
+ * @since DOM Level 3 Core
+ */
+ public final String getBaseURI()
+ {
+ return null;
+ }
+
+ private void mutating(String newValue)
+ {
+ if (!reportMutations)
+ {
+ return;
+ }
+
+ // EVENT: DOMCharacterDataModified, target = this,
+ // prev/new values provided
+ MutationEvent event;
+
+ event = (MutationEvent) createEvent("MutationEvents");
+ event.initMutationEvent("DOMCharacterDataModified",
+ true /* bubbles */, false /* nocancel */,
+ null, text, newValue, null, (short) 0);
+ dispatchEvent(event);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomComment.java b/libjava/classpath/gnu/xml/dom/DomComment.java
new file mode 100644
index 000000000..5be6bd518
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomComment.java
@@ -0,0 +1,80 @@
+/* DomComment.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.Comment;
+
+/**
+ * <p> "Comment" implementation.
+ * Comments hold data intended for direct consumption by people;
+ * programs should only use ProcessingInstruction nodes. Note that
+ * since SAX makes comment reporting optional, XML systems that
+ * rely on comments (such as by using this class) will often lose
+ * those comments at some point in the processing pipeline. </p>
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomComment
+ extends DomCharacterData
+ implements Comment
+{
+
+ /**
+ * Constructs a comment node associated with the specified
+ * document and holding the specified data.
+ *
+ * <p>This constructor should only be invoked by a Document as part of
+ * its createComment functionality, or through a subclass which is
+ * similarly used in a "Sub-DOM" style layer.
+ */
+ protected DomComment(DomDocument owner, String value)
+ {
+ super(COMMENT_NODE, owner, value);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the string "#comment".
+ */
+ final public String getNodeName()
+ {
+ return "#comment";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomDOMException.java b/libjava/classpath/gnu/xml/dom/DomDOMException.java
new file mode 100644
index 000000000..4a3ba7a8c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomDOMException.java
@@ -0,0 +1,174 @@
+/* DomDOMException.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+
+/**
+ * <p> DOMException implementation. The version that
+ * is provided by the W3C is abstract, so it can't be instantiated.
+ *
+ * <p> This also provides a bit more information about the error
+ * that is being reported, in terms of the relevant DOM structures
+ * and data.
+ *
+ * @author David Brownell
+ */
+public class DomDOMException
+ extends DOMException
+{
+
+ /** @serial Data that caused an error to be reported */
+ private String data;
+
+ /** @serial Node associated with the error. */
+ private Node node;
+
+ /** @serial Data associated with the error. */
+ private int value;
+
+ /**
+ * Constructs an exception, with the diagnostic message
+ * corresponding to the specified code.
+ */
+ public DomDOMException(short code)
+ {
+ super(code, diagnostic(code));
+ }
+
+ /**
+ * Constructs an exception, with the diagnostic message
+ * corresponding to the specified code and additional
+ * information as provided.
+ */
+ public DomDOMException(short code, String data, Node node, int value)
+ {
+ super(code, diagnostic(code));
+ this.data = data;
+ this.node = node;
+ this.value = value;
+ }
+
+ /** Returns the node to which the diagnotic applies, or null. */
+ final public Node getNode()
+ {
+ return node;
+ }
+
+ /** Returns data to which the diagnotic applies, or null. */
+ final public String getData()
+ {
+ return data;
+ }
+
+ /** Returns data to which the diagnotic applies, or null. */
+ final public int getValue()
+ {
+ return value;
+ }
+
+ /**
+ * Returns a diagnostic message that may be slightly more useful
+ * than the generic one, where possible.
+ */
+ public String getMessage()
+ {
+ String retval = super.getMessage();
+
+ if (data != null)
+ {
+ retval += "\nMore Information: " + data;
+ }
+ if (value != 0)
+ {
+ retval += "\nNumber: " + value;
+ }
+ if (node != null)
+ {
+ retval += "\nNode Name: " + node.getNodeName();
+ }
+ return retval;
+ }
+
+ // these strings should be localizable.
+
+ private static String diagnostic(short code)
+ {
+ switch (code)
+ {
+ // DOM L1:
+ case INDEX_SIZE_ERR:
+ return "An index or size is out of range.";
+ case DOMSTRING_SIZE_ERR:
+ return "A string is too big.";
+ case HIERARCHY_REQUEST_ERR:
+ return "The node doesn't belong here.";
+ case WRONG_DOCUMENT_ERR:
+ return "The node belongs in another document.";
+ case INVALID_CHARACTER_ERR:
+ return "That character is not permitted.";
+ case NO_DATA_ALLOWED_ERR:
+ return "This node does not permit data.";
+ case NO_MODIFICATION_ALLOWED_ERR:
+ return "No changes are allowed.";
+ case NOT_FOUND_ERR:
+ return "The node was not found in that context.";
+ case NOT_SUPPORTED_ERR:
+ return "That object is not supported.";
+ case INUSE_ATTRIBUTE_ERR:
+ return "The attribute belongs to a different element.";
+
+ // DOM L2:
+ case INVALID_STATE_ERR:
+ return "The object is not usable.";
+ case SYNTAX_ERR:
+ return "An illegal string was provided.";
+ case INVALID_MODIFICATION_ERR:
+ return "An object's type may not be changed.";
+ case NAMESPACE_ERR:
+ return "The operation violates XML Namespaces.";
+ case INVALID_ACCESS_ERR:
+ return "Parameter or operation isn't supported by this node.";
+ case TYPE_MISMATCH_ERR:
+ return "The type of the argument is incompatible with the expected type.";
+ }
+ return "Reserved exception number: " + code;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomDoctype.java b/libjava/classpath/gnu/xml/dom/DomDoctype.java
new file mode 100644
index 000000000..ecf7a9588
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomDoctype.java
@@ -0,0 +1,455 @@
+/* DomDoctype.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import java.util.HashMap;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.Entity;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.Notation;
+
+/**
+ * <p> "DocumentType" implementation (with no extensions for supporting
+ * any document typing information). This is a non-core DOM class,
+ * supporting the "XML" feature. </p>
+ *
+ * <p> <em>Few XML applications will actually care about this partial
+ * DTD support</em>, since it doesn't expose any (!) of the data typing
+ * facilities which can motivate applications to use DTDs. It does not
+ * expose element content models, or information about attribute typing
+ * rules. Plus the information it exposes isn't very useful; as one example,
+ * DOM exposes information about unparsed ENTITY objects, which is only used
+ * with certain element attributes, but does not expose the information about
+ * those attributes which is needed to apply that data! </p>
+ *
+ * <p> Also, note that there are no nonportable ways to associate even the
+ * notation and entity information exposed by DOM with a DocumentType. While
+ * there is a DOM L2 method to construct a DocumentType, it only gives access
+ * to the textual content of the &lt;!DOCTYPE ...&gt; declaration. </p>
+ *
+ * <p> In short, <em>you are strongly advised not to rely on this incomplete
+ * DTD functionality</em> in your application code.</p>
+ *
+ * @see DomEntity
+ * @see DomEntityReference
+ * @see DomNotation
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomDoctype
+ extends DomExtern
+ implements DocumentType
+{
+
+ private DomNamedNodeMap notations;
+ private DomNamedNodeMap entities;
+ private final DOMImplementation implementation;
+ private String subset;
+
+ private HashMap elements = new HashMap();
+ private boolean ids;
+
+ /**
+ * Constructs a DocumentType node associated with the specified
+ * implementation, with the specified name.
+ *
+ * <p>This constructor should only be invoked by a DOMImplementation as
+ * part of its createDocumentType functionality, or through a subclass
+ * which is similarly used in a "Sub-DOM" style layer.
+ *
+ * <p> Note that at this time there is no standard SAX API granting
+ * access to the internal subset text, so that relying on that value
+ * is not currently portable.
+ *
+ * @param impl The implementation with which this object is associated
+ * @param name Name of this root element
+ * @param publicId If non-null, provides the external subset's
+ * PUBLIC identifier
+ * @param systemId If non-null, provides the external subset's
+ * SYSTEM identifier
+ * @param internalSubset Provides the literal value (unparsed, no
+ * entities expanded) of the DTD's internal subset.
+ */
+ protected DomDoctype(DOMImplementation impl,
+ String name,
+ String publicId,
+ String systemId,
+ String internalSubset)
+ {
+ super(DOCUMENT_TYPE_NODE, null, name, publicId, systemId);
+ implementation = impl;
+ subset = internalSubset;
+ }
+
+ /**
+ * JAXP builder constructor.
+ * @param doc the document
+ * @param name the name of the document element
+ * @param publicId the public ID of the document type declaration
+ * @param systemId the system ID of the document type declaration
+ */
+ public DomDoctype(DomDocument doc,
+ String name,
+ String publicId,
+ String systemId)
+ {
+ super(DOCUMENT_TYPE_NODE, doc, name, publicId, systemId);
+ implementation = doc.getImplementation();
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the root element's name (just like getNodeName).
+ */
+ final public String getName()
+ {
+ return getNodeName();
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns information about any general entities declared
+ * in the DTD.
+ *
+ * <p><em>Note: DOM L1 doesn't throw a DOMException here, but
+ * then it doesn't have the strange construction rules of L2.</em>
+ *
+ * @exception DOMException HIERARCHY_REQUEST_ERR if the DocumentType
+ * is not associated with a document.
+ */
+ public NamedNodeMap getEntities()
+ {
+ if (entities == null)
+ {
+ entities = new DomNamedNodeMap(this, Node.ENTITY_NODE);
+ }
+ return entities;
+ }
+
+ /**
+ * Records the declaration of a general entity in this DocumentType.
+ *
+ * @param name Name of the entity
+ * @param publicId If non-null, provides the entity's PUBLIC identifier
+ * @param systemId Provides the entity's SYSTEM identifier
+ * @param notation If non-null, provides the entity's notation
+ * (indicating an unparsed entity)
+ * @return The Entity that was declared, or null if the entity wasn't
+ * recorded (because it's a parameter entity or because an entity with
+ * this name was already declared).
+ *
+ * @exception DOMException NO_MODIFICATION_ALLOWED_ERR if the
+ * DocumentType is no longer writable.
+ * @exception DOMException HIERARCHY_REQUEST_ERR if the DocumentType
+ * is not associated with a document.
+ */
+ public Entity declareEntity(String name,
+ String publicId,
+ String systemId,
+ String notation)
+ {
+ DomEntity entity;
+
+ if (name.charAt(0) == '%' || "[dtd]".equals(name))
+ {
+ return null;
+ }
+ if (isReadonly())
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+ getEntities();
+
+ DomDocument.checkName(name, (owner != null) ?
+ "1.1".equals(owner.getXmlVersion()) : false);
+ if (entities.getNamedItem(name) != null)
+ {
+ return null;
+ }
+
+ entity = new DomEntity(owner, name, publicId, systemId, notation);
+ entities.setNamedItem(entity);
+ return entity;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns information about any notations declared in the DTD.
+ *
+ * <p><em>Note: DOM L1 doesn't throw a DOMException here, but
+ * then it doesn't have the strange construction rules of L2.</em>
+ *
+ * @exception DOMException HIERARCHY_REQUEST_ERR if the DocumentType
+ * is not associated with a document.
+ */
+ public NamedNodeMap getNotations()
+ {
+ if (notations == null)
+ {
+ notations = new DomNamedNodeMap(this, Node.NOTATION_NODE);
+ }
+ return notations;
+ }
+
+ /**
+ * Records the declaration of a notation in this DocumentType.
+ *
+ * @param name Name of the notation
+ * @param publicId If non-null, provides the notation's PUBLIC identifier
+ * @param systemId If non-null, provides the notation's SYSTEM identifier
+ * @return The notation that was declared.
+ *
+ * @exception DOMException NO_MODIFICATION_ALLOWED_ERR if the
+ * DocumentType is no longer writable.
+ * @exception DOMException HIERARCHY_REQUEST_ERR if the DocumentType
+ * is not associated with a document.
+ */
+ public Notation declareNotation(String name,
+ String publicId,
+ String systemId)
+ {
+ DomNotation notation;
+
+ if (isReadonly())
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+ getNotations();
+
+ DomDocument.checkName(name, (owner != null) ?
+ "1.1".equals(owner.getXmlVersion()) : false);
+
+ notation = new DomNotation(owner, name, publicId, systemId);
+ notations.setNamedItem(notation);
+ return notation;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns the internal subset of the document, as a string of unparsed
+ * XML declarations (and comments, PIs, whitespace); or returns null if
+ * there is no such subset. There is no vendor-independent expectation
+ * that this attribute be set, or that declarations found in it be
+ * reflected in the <em>entities</em> or <em>notations</em> attributes
+ * of this Document "Type" object.
+ *
+ * <p> Some application-specific XML profiles require that documents
+ * only use specific PUBLIC identifiers, without an internal subset
+ * to modify the interperetation of the declarations associated with
+ * that PUBLIC identifier through some standard.
+ */
+ public String getInternalSubset()
+ {
+ return subset;
+ }
+
+ /**
+ * The base URI of a DocumentType is always <code>null</code>.
+ * See the Infoset Mapping, appendix C.
+ */
+ public final String getBaseURI()
+ {
+ return null;
+ }
+
+ /**
+ * Sets the internal "readonly" flag so the node and its associated
+ * data (only lists of entities and notations, no type information
+ * at the moment) can't be changed.
+ */
+ public void makeReadonly()
+ {
+ super.makeReadonly();
+ if (entities != null)
+ {
+ entities.makeReadonly();
+ }
+ if (notations != null)
+ {
+ notations.makeReadonly();
+ }
+ }
+
+ void setOwner(DomDocument doc)
+ {
+ if (entities != null)
+ {
+ for (DomNode ctx = entities.first; ctx != null; ctx = ctx.next)
+ {
+ ctx.setOwner(doc);
+ }
+ }
+ if (notations != null)
+ {
+ for (DomNode ctx = notations.first; ctx != null; ctx = ctx.next)
+ {
+ ctx.setOwner(doc);
+ }
+ }
+ super.setOwner(doc);
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Consults the DOM implementation to determine if the requested
+ * feature is supported.
+ */
+ final public boolean supports(String feature, String version)
+ {
+ return implementation.hasFeature(feature, version);
+ }
+
+ /**
+ * Returns the implementation associated with this document type.
+ */
+ final public DOMImplementation getImplementation()
+ {
+ return implementation;
+ }
+
+ public void elementDecl(String name, String model)
+ {
+ DTDElementTypeInfo info = getElementTypeInfo(name);
+ if (info == null)
+ {
+ info = new DTDElementTypeInfo(name, model);
+ elements.put(name, info);
+ }
+ else
+ {
+ info.model = model;
+ }
+ }
+
+ DTDElementTypeInfo getElementTypeInfo(String name)
+ {
+ return (DTDElementTypeInfo) elements.get(name);
+ }
+
+ public void attributeDecl(String eName, String aName, String type,
+ String mode, String value)
+ {
+ DTDAttributeTypeInfo info = new DTDAttributeTypeInfo(eName, aName, type,
+ mode, value);
+ DTDElementTypeInfo elementInfo = getElementTypeInfo(eName);
+ if (elementInfo == null)
+ {
+ elementInfo = new DTDElementTypeInfo(eName, null);
+ elements.put(eName, elementInfo);
+ }
+ elementInfo.setAttributeTypeInfo(aName, info);
+ if ("ID".equals(type))
+ {
+ ids = true;
+ }
+ }
+
+ DTDAttributeTypeInfo getAttributeTypeInfo(String elementName, String name)
+ {
+ DTDElementTypeInfo elementInfo =
+ (DTDElementTypeInfo) elements.get(elementName);
+ return (elementInfo == null) ? null :
+ elementInfo.getAttributeTypeInfo(name);
+ }
+
+ boolean hasIds()
+ {
+ return ids;
+ }
+
+ public boolean isSameNode(Node arg)
+ {
+ if (equals(arg))
+ {
+ return true;
+ }
+ if (!(arg instanceof DocumentType))
+ {
+ return false;
+ }
+ DocumentType doctype = (DocumentType) arg;
+ if (!equal(getPublicId(), doctype.getPublicId()))
+ {
+ return false;
+ }
+ if (!equal(getSystemId(), doctype.getSystemId()))
+ {
+ return false;
+ }
+ if (!equal(getInternalSubset(), doctype.getInternalSubset()))
+ {
+ return false;
+ }
+ // TODO entities
+ // TODO notations
+ return true;
+ }
+
+ /**
+ * Shallow clone of the doctype, except that associated
+ * entities and notations are (deep) cloned.
+ */
+ public Object clone()
+ {
+ DomDoctype node = (DomDoctype) super.clone();
+
+ if (entities != null)
+ {
+ node.entities = new DomNamedNodeMap(node, Node.ENTITY_NODE);
+ for (DomNode ctx = entities.first; ctx != null; ctx = ctx.next)
+ {
+ node.entities.setNamedItem(ctx.cloneNode(true));
+ }
+ }
+ if (notations != null)
+ {
+ node.notations = new DomNamedNodeMap(node, Node.NOTATION_NODE);
+ for (DomNode ctx = notations.first; ctx != null; ctx = ctx.next)
+ {
+ node.notations.setNamedItem(ctx.cloneNode(true));
+ }
+ }
+ return node;
+ }
+
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomDocument.java b/libjava/classpath/gnu/xml/dom/DomDocument.java
new file mode 100644
index 000000000..b1e99f46d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomDocument.java
@@ -0,0 +1,1555 @@
+/* DomDocument.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import java.util.Iterator;
+import javax.xml.XMLConstants;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.DOMConfiguration;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Element;
+import org.w3c.dom.Entity;
+import org.w3c.dom.EntityReference;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.Notation;
+import org.w3c.dom.ProcessingInstruction;
+import org.w3c.dom.Text;
+import org.w3c.dom.UserDataHandler;
+import org.w3c.dom.traversal.DocumentTraversal;
+import org.w3c.dom.traversal.NodeFilter;
+import org.w3c.dom.traversal.NodeIterator;
+import org.w3c.dom.traversal.TreeWalker;
+import org.w3c.dom.xpath.XPathEvaluator;
+import org.w3c.dom.xpath.XPathException;
+import org.w3c.dom.xpath.XPathExpression;
+import org.w3c.dom.xpath.XPathNSResolver;
+
+/**
+ * <p> "Document" and "DocumentTraversal" implementation.
+ *
+ * <p> Note that when this checks names for legality, it uses an
+ * approximation of the XML rules, not the real ones. Specifically,
+ * it uses Unicode rules, with sufficient tweaks to pass a majority
+ * of basic XML conformance tests. (The huge XML character tables are
+ * hairy to implement.)
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomDocument
+ extends DomNode
+ implements Document, DocumentTraversal, XPathEvaluator
+{
+
+ private final DOMImplementation implementation;
+ private boolean checkingCharacters = true;
+ boolean checkingWellformedness = true;
+ private boolean defaultAttributes = true;
+
+ boolean building; // if true, skip mutation events in the tree
+
+ DomDocumentConfiguration config;
+
+ String inputEncoding;
+ String encoding;
+ String version = "1.0";
+ boolean standalone;
+ String systemId;
+
+ /**
+ * Constructs a Document node, associating it with an instance
+ * of the DomImpl class.
+ *
+ * <p> Note that this constructor disables character checking.
+ * It is normally used when connecting a DOM to an XML parser,
+ * and duplicating such checks is undesirable. When used for
+ * purposes other than connecting to a parser, you should
+ * re-enable that checking.
+ *
+ * @see #setCheckingCharacters
+ */
+ public DomDocument()
+ {
+ this(new DomImpl());
+ }
+
+ /**
+ * Constructs a Document node, associating it with the specified
+ * implementation. This should only be used in conjunction with
+ * a specialized implementation; it will normally be called by
+ * that implementation.
+ *
+ * @see DomImpl
+ * @see #setCheckingCharacters
+ */
+ protected DomDocument(DOMImplementation impl)
+ {
+ super(DOCUMENT_NODE, null);
+ implementation = impl;
+ }
+
+ /**
+ * Sets the <code>building</code> flag.
+ * Mutation events in the document are not reported.
+ */
+ public void setBuilding(boolean flag)
+ {
+ building = flag;
+ }
+
+ /**
+ * Sets whether to check for document well-formedness.
+ * If true, an exception will be raised if a second doctype or root
+ * element node is added to the document.
+ */
+ public void setCheckWellformedness(boolean flag)
+ {
+ checkingWellformedness = flag;
+ }
+
+ /**
+ * Sets whether to check for document characters.
+ */
+ public void setCheckingCharacters(boolean flag)
+ {
+ checkingCharacters = flag;
+ }
+
+ /**
+ * Sets whether to default attributes for new elements.
+ */
+ public void setDefaultAttributes(boolean flag)
+ {
+ defaultAttributes = flag;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the constant "#document".
+ */
+ final public String getNodeName()
+ {
+ return "#document";
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the document's root element, or null.
+ */
+ final public Element getDocumentElement()
+ {
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ if (ctx.nodeType == ELEMENT_NODE)
+ {
+ return (Element) ctx;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the document's DocumentType, or null.
+ */
+ final public DocumentType getDoctype()
+ {
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ if (ctx.nodeType == DOCUMENT_TYPE_NODE)
+ {
+ return (DocumentType) ctx;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the document's DOMImplementation.
+ */
+ final public DOMImplementation getImplementation()
+ {
+ return implementation;
+ }
+
+ /**
+ * <b>DOM L1 (relocated in DOM L2)</b>
+ * Returns the element with the specified "ID" attribute, or null.
+ *
+ * <p>Returns null unless {@link Consumer} was used to populate internal
+ * DTD declaration information, using package-private APIs. If that
+ * internal DTD information is available, the document may be searched for
+ * the element with that ID.
+ */
+ public Element getElementById(String id)
+ {
+ if (id == null || id.length() == 0)
+ {
+ return null;
+ }
+ DomDoctype doctype = (DomDoctype) getDoctype();
+ if (doctype != null && !doctype.hasIds())
+ {
+ doctype = null;
+ }
+
+ // yes, this is linear in size of document.
+ // it'd be easy enough to maintain a hashtable.
+ Node current = getDocumentElement();
+ Node temp;
+
+ if (current == null)
+ {
+ return null;
+ }
+ while (current != this)
+ {
+ // done?
+ if (current.getNodeType() == ELEMENT_NODE)
+ {
+ DomElement element = (DomElement) current;
+ if (element.userIdAttrs != null)
+ {
+ for (Iterator i = element.userIdAttrs.iterator();
+ i.hasNext(); )
+ {
+ Node idAttr = (Node) i.next();
+ if (id.equals(idAttr.getNodeValue()))
+ {
+ return element;
+ }
+ }
+ }
+ if (doctype != null)
+ {
+ DTDElementTypeInfo info =
+ doctype.getElementTypeInfo(current.getNodeName());
+ if (info != null &&
+ id.equals(element.getAttribute(info.idAttrName)))
+ {
+ return element;
+ }
+ }
+ // xml:id
+ String xmlId = element.getAttribute("xml:id");
+ if (xmlId == null)
+ {
+ xmlId = element.getAttributeNS(XMLConstants.XML_NS_URI,
+ "id");
+ }
+ if (id.equals(xmlId))
+ {
+ return element;
+ }
+ }
+
+ // descend?
+ if (current.hasChildNodes())
+ {
+ current = current.getFirstChild();
+ continue;
+ }
+
+ // lateral?
+ temp = current.getNextSibling();
+ if (temp != null)
+ {
+ current = temp;
+ continue;
+ }
+
+ // back up ...
+ do
+ {
+ temp = current.getParentNode();
+ if (temp == null)
+ {
+ return null;
+ }
+ current = temp;
+ temp = current.getNextSibling();
+ }
+ while (temp == null);
+ current = temp;
+ }
+ return null;
+ }
+
+ private void checkNewChild(Node newChild)
+ {
+ if (newChild.getNodeType() == ELEMENT_NODE
+ && getDocumentElement() != null)
+ {
+ throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR,
+ "document element already present: " +
+ getDocumentElement(), newChild, 0);
+ }
+ if (newChild.getNodeType() == DOCUMENT_TYPE_NODE
+ && getDoctype() != null)
+ {
+ throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR,
+ "document type already present: " +
+ getDoctype(), newChild, 0);
+ }
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Appends the specified node to this node's list of children,
+ * enforcing the constraints that there be only one root element
+ * and one document type child.
+ */
+ public Node appendChild(Node newChild)
+ {
+ if (checkingWellformedness)
+ {
+ checkNewChild(newChild);
+ }
+ return super.appendChild(newChild);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Inserts the specified node in this node's list of children,
+ * enforcing the constraints that there be only one root element
+ * and one document type child.
+ */
+ public Node insertBefore(Node newChild, Node refChild)
+ {
+ if (checkingWellformedness)
+ {
+ checkNewChild(newChild);
+ }
+ return super.insertBefore(newChild, refChild);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Replaces the specified node in this node's list of children,
+ * enforcing the constraints that there be only one root element
+ * and one document type child.
+ */
+ public Node replaceChild(Node newChild, Node refChild)
+ {
+ if (checkingWellformedness &&
+ ((newChild.getNodeType() == ELEMENT_NODE &&
+ refChild.getNodeType() != ELEMENT_NODE) ||
+ (newChild.getNodeType() == DOCUMENT_TYPE_NODE &&
+ refChild.getNodeType() != DOCUMENT_TYPE_NODE)))
+ {
+ checkNewChild(newChild);
+ }
+ return super.replaceChild(newChild, refChild);
+ }
+
+ // NOTE: DOM can't really tell when the name of an entity,
+ // notation, or PI must follow the namespace rules (excluding
+ // colons) instead of the XML rules (which allow them without
+ // much restriction). That's an API issue. verifyXmlName
+ // aims to enforce the XML rules, not the namespace rules.
+
+ /**
+ * Throws a DOM exception if the specified name is not a legal XML 1.0
+ * Name.
+ * @deprecated This method is deprecated and may be removed in future
+ * versions of GNU JAXP
+ */
+ public static void verifyXmlName(String name)
+ {
+ // XXX why is this public?
+ checkName(name, false);
+ }
+
+ static void checkName(String name, boolean xml11)
+ {
+ if (name == null)
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR, name, null, 0);
+ }
+ int len = name.length();
+ if (len == 0)
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR, name, null, 0);
+ }
+
+ // dog: rewritten to use the rules for XML 1.0 and 1.1
+
+ // Name start character
+ char c = name.charAt(0);
+ if (xml11)
+ {
+ // XML 1.1
+ if ((c < 0x0041 || c > 0x005a) &&
+ (c < 0x0061 || c > 0x007a) &&
+ c != ':' && c != '_' &&
+ (c < 0x00c0 || c > 0x00d6) &&
+ (c < 0x00d8 || c > 0x00f6) &&
+ (c < 0x00f8 || c > 0x02ff) &&
+ (c < 0x0370 || c > 0x037d) &&
+ (c < 0x037f || c > 0x1fff) &&
+ (c < 0x200c || c > 0x200d) &&
+ (c < 0x2070 || c > 0x218f) &&
+ (c < 0x2c00 || c > 0x2fef) &&
+ (c < 0x3001 || c > 0xd7ff) &&
+ (c < 0xf900 || c > 0xfdcf) &&
+ (c < 0xfdf0 || c > 0xfffd) &&
+ (c < 0x10000 || c > 0xeffff))
+ {
+ throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR,
+ name, null, c);
+ }
+ }
+ else
+ {
+ // XML 1.0
+ int type = Character.getType(c);
+ switch (type)
+ {
+ case Character.LOWERCASE_LETTER: // Ll
+ case Character.UPPERCASE_LETTER: // Lu
+ case Character.OTHER_LETTER: // Lo
+ case Character.TITLECASE_LETTER: // Lt
+ case Character.LETTER_NUMBER: // Nl
+ if ((c > 0xf900 && c < 0xfffe) ||
+ (c >= 0x20dd && c <= 0x20e0))
+ {
+ // Compatibility area and Unicode 2.0 exclusions
+ throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR,
+ name, null, c);
+ }
+ break;
+ default:
+ if (c != ':' && c != '_' && (c < 0x02bb || c > 0x02c1) &&
+ c != 0x0559 && c != 0x06e5 && c != 0x06e6)
+ {
+ throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR,
+ name, null, c);
+ }
+ }
+ }
+
+ // Subsequent characters
+ for (int i = 1; i < len; i++)
+ {
+ c = name.charAt(i);
+ if (xml11)
+ {
+ // XML 1.1
+ if ((c < 0x0041 || c > 0x005a) &&
+ (c < 0x0061 || c > 0x007a) &&
+ (c < 0x0030 || c > 0x0039) &&
+ c != ':' && c != '_' && c != '-' && c != '.' &&
+ (c < 0x00c0 || c > 0x00d6) &&
+ (c < 0x00d8 || c > 0x00f6) &&
+ (c < 0x00f8 || c > 0x02ff) &&
+ (c < 0x0370 || c > 0x037d) &&
+ (c < 0x037f || c > 0x1fff) &&
+ (c < 0x200c || c > 0x200d) &&
+ (c < 0x2070 || c > 0x218f) &&
+ (c < 0x2c00 || c > 0x2fef) &&
+ (c < 0x3001 || c > 0xd7ff) &&
+ (c < 0xf900 || c > 0xfdcf) &&
+ (c < 0xfdf0 || c > 0xfffd) &&
+ (c < 0x10000 || c > 0xeffff) &&
+ c != 0x00b7 &&
+ (c < 0x0300 || c > 0x036f) &&
+ (c < 0x203f || c > 0x2040))
+ {
+ throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, name,
+ null, c);
+ }
+ }
+ else
+ {
+ // XML 1.0
+ int type = Character.getType(c);
+ switch (type)
+ {
+ case Character.LOWERCASE_LETTER: // Ll
+ case Character.UPPERCASE_LETTER: // Lu
+ case Character.DECIMAL_DIGIT_NUMBER: // Nd
+ case Character.OTHER_LETTER: // Lo
+ case Character.TITLECASE_LETTER: // Lt
+ case Character.LETTER_NUMBER: // Nl
+ case Character.COMBINING_SPACING_MARK: // Mc
+ case Character.ENCLOSING_MARK: // Me
+ case Character.NON_SPACING_MARK: // Mn
+ case Character.MODIFIER_LETTER: // Lm
+ if ((c > 0xf900 && c < 0xfffe) ||
+ (c >= 0x20dd && c <= 0x20e0))
+ {
+ // Compatibility area and Unicode 2.0 exclusions
+ throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR,
+ name, null, c);
+ }
+ break;
+ default:
+ if (c != '-' && c != '.' && c != ':' && c != '_' &&
+ c != 0x0387 && (c < 0x02bb || c > 0x02c1) &&
+ c != 0x0559 && c != 0x06e5 && c != 0x06e6 && c != 0x00b7)
+ {
+ throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR,
+ name, null, c);
+ }
+ }
+ }
+ }
+
+ // FIXME characters with a font or compatibility decomposition (i.e.
+ // those with a "compatibility formatting tag" in field 5 of the
+ // database -- marked by field 5 beginning with a "<") are not allowed.
+ }
+
+ // package private
+ static void checkNCName(String name, boolean xml11)
+ {
+ checkName(name, xml11);
+ int len = name.length();
+ int index = name.indexOf(':');
+ if (index != -1)
+ {
+ if (index == 0 || index == (len - 1) || name.lastIndexOf(':') != index)
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR, name, null, 0);
+ }
+ }
+ }
+
+ // package private
+ static void checkChar(String value, boolean xml11)
+ {
+ char[] chars = value.toCharArray();
+ checkChar(chars, 0, chars.length, xml11);
+ }
+
+ static void checkChar(char[] buf, int off, int len, boolean xml11)
+ {
+ for (int i = 0; i < len; i++)
+ {
+ char c = buf[i];
+
+ // assume surrogate pairing checks out OK, for simplicity
+ if ((c >= 0x0020 && c <= 0xd7ff) ||
+ (c == 0x000a || c == 0x000d || c == 0x0009) ||
+ (c >= 0xe000 && c <= 0xfffd) ||
+ (c >= 0x10000 && c <= 0x10ffff))
+ {
+ continue;
+ }
+ if (xml11)
+ {
+ if ((c >= 0x0001 && c <= 0x001f) ||
+ (c >= 0x007f && c <= 0x0084) ||
+ (c >= 0x0086 && c <= 0x009f))
+ {
+ continue;
+ }
+ }
+ throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR,
+ new String(buf, off, len), null, c);
+ }
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns a newly created element with the specified name.
+ * The node name of the created element will be equal to {@code name}.
+ * The namespace, prefix and local name will all be {@code null}.
+ */
+ public Element createElement(String name)
+ {
+ Element element;
+
+ if (checkingCharacters)
+ {
+ checkName(name, "1.1".equals(version));
+ }
+ if (name.startsWith("xml:"))
+ {
+ element = createElementNS(null, name);
+ }
+ else
+ {
+ DomElement domElement = new DomElement(this, null, name, null, null);
+ element = domElement;
+ }
+ if (defaultAttributes)
+ setDefaultAttributes(element, name);
+ return element;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns a newly created element with the specified name
+ * and namespace information.
+ */
+ public Element createElementNS(String namespaceURI, String name)
+ {
+ if (checkingCharacters)
+ {
+ checkNCName(name, "1.1".equals(version));
+ }
+
+ if ("".equals(namespaceURI))
+ {
+ namespaceURI = null;
+ }
+ if (name.startsWith("xml:"))
+ {
+ if (namespaceURI != null
+ && !XMLConstants.XML_NS_URI.equals(namespaceURI))
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "xml namespace is always " +
+ XMLConstants.XML_NS_URI, this, 0);
+ }
+ namespaceURI = XMLConstants.XML_NS_URI;
+ }
+ else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) ||
+ name.startsWith("xmlns:"))
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "xmlns is reserved", this, 0);
+ }
+ else if (namespaceURI == null && name.indexOf(':') != -1)
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "prefixed name '" + name +
+ "' needs a URI", this, 0);
+ }
+
+ Element element = new DomElement(this, namespaceURI, name);
+ if (defaultAttributes)
+ setDefaultAttributes(element, name);
+ return element;
+ }
+
+ private void setDefaultAttributes(Element element, String name)
+ {
+ DomDoctype doctype = (DomDoctype) getDoctype();
+ if (doctype == null)
+ {
+ return;
+ }
+
+ // default any attributes that need it
+ DTDElementTypeInfo info = doctype.getElementTypeInfo(name);
+ if (info != null)
+ {
+ for (Iterator i = info.attributes(); i != null && i.hasNext(); )
+ {
+ DTDAttributeTypeInfo attr = (DTDAttributeTypeInfo) i.next();
+ String value = attr.value;
+ if ("#IMPLIED".equals(attr.mode) && value == null)
+ continue;
+ DomAttr node = (DomAttr) createAttribute(attr.name);
+
+ if (value == null)
+ {
+ value = "";
+ }
+ node.setValue(value);
+ node.setSpecified(false);
+ element.setAttributeNode(node);
+ }
+ }
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns a newly created document fragment.
+ */
+ public DocumentFragment createDocumentFragment()
+ {
+ return new DomDocumentFragment(this);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns a newly created text node with the specified value.
+ */
+ public Text createTextNode(String value)
+ {
+ if (checkingCharacters)
+ {
+ checkChar(value, "1.1".equals(version));
+ }
+ return new DomText(this, value);
+ }
+
+ /**
+ * Returns a newly created text node with the specified value.
+ */
+ public Text createTextNode(char[] buf, int off, int len)
+ {
+ if (checkingCharacters)
+ {
+ checkChar(buf, off, len, "1.1".equals(version));
+ }
+ return new DomText(this, buf, off, len);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns a newly created comment node with the specified value.
+ */
+ public Comment createComment(String value)
+ {
+ if (checkingCharacters)
+ {
+ checkChar(value, "1.1".equals(version));
+ }
+ return new DomComment(this, value);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns a newly created CDATA section node with the specified value.
+ */
+ public CDATASection createCDATASection(String value)
+ {
+ if (checkingCharacters)
+ {
+ checkChar(value, "1.1".equals(version));
+ }
+ return new DomCDATASection(this, value);
+ }
+
+ /**
+ * Returns a newly created CDATA section node with the specified value.
+ */
+ public CDATASection createCDATASection(char[] buf, int off, int len)
+ {
+ if (checkingCharacters)
+ {
+ checkChar(buf, off, len, "1.1".equals(version));
+ }
+ return new DomCDATASection(this, buf, off, len);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns a newly created processing instruction.
+ */
+ public ProcessingInstruction createProcessingInstruction(String target,
+ String data)
+ {
+ if (checkingCharacters)
+ {
+ boolean xml11 = "1.1".equals(version);
+ checkName(target, xml11);
+ if ("xml".equalsIgnoreCase(target))
+ {
+ throw new DomDOMException(DOMException.SYNTAX_ERR,
+ "illegal PI target name",
+ this, 0);
+ }
+ checkChar(data, xml11);
+ }
+ return new DomProcessingInstruction(this, target, data);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns a newly created attribute with the specified name.
+ */
+ public Attr createAttribute(String name)
+ {
+ if (checkingCharacters)
+ {
+ checkName(name, "1.1".equals(version));
+ }
+ if (name.startsWith("xml:"))
+ {
+ return createAttributeNS(XMLConstants.XML_NS_URI, name);
+ }
+ else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) ||
+ name.startsWith("xmlns:"))
+ {
+ return createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, name);
+ }
+ else
+ {
+ DomAttr ret = new DomAttr(this, null, name, null, null);
+ return ret;
+ }
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns a newly created attribute with the specified name
+ * and namespace information.
+ */
+ public Attr createAttributeNS(String namespaceURI, String name)
+ {
+ if (checkingCharacters)
+ {
+ checkNCName(name, "1.1".equals(version));
+ }
+
+ if ("".equals(namespaceURI))
+ {
+ namespaceURI = null;
+ }
+ if (name.startsWith ("xml:"))
+ {
+ if (namespaceURI == null)
+ {
+ namespaceURI = XMLConstants.XML_NS_URI;
+ }
+ else if (!XMLConstants.XML_NS_URI.equals(namespaceURI))
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "xml namespace is always " +
+ XMLConstants.XML_NS_URI,
+ this, 0);
+ }
+ }
+ else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) ||
+ name.startsWith("xmlns:"))
+ {
+ if (namespaceURI == null)
+ {
+ namespaceURI = XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
+ }
+ else if (!XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI))
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "xmlns namespace must be " +
+ XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
+ this, 0);
+ }
+ }
+ else if (namespaceURI == null && name.indexOf(':') != -1)
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "prefixed name needs a URI: " + name, this, 0);
+ }
+ return new DomAttr(this, namespaceURI, name);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns a newly created reference to the specified entity.
+ * The caller should populate this with the appropriate children
+ * and then mark it as readonly.
+ *
+ * @see DomNode#makeReadonly
+ */
+ public EntityReference createEntityReference(String name)
+ {
+ DomEntityReference ret = new DomEntityReference(this, name);
+ DocumentType doctype = getDoctype();
+ if (doctype != null)
+ {
+ DomEntity ent = (DomEntity) doctype.getEntities().getNamedItem(name);
+ if (ent != null)
+ {
+ for (DomNode ctx = ent.first; ctx != null; ctx = ctx.next)
+ {
+ ret.appendChild(ctx.cloneNode(true));
+ }
+ }
+ }
+ ret.makeReadonly();
+ return ret;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Makes a copy of the specified node, with all nodes "owned" by
+ * this document and with children optionally copied. This type
+ * of standard utility has become, well, a standard utility.
+ *
+ * <p> Note that EntityReference nodes created through this method (either
+ * directly, or recursively) never have children, and that there is no
+ * portable way to associate them with such children.
+ *
+ * <p> Note also that there is no requirement that the specified node
+ * be associated with a different document. This differs from the
+ * <em>cloneNode</em> operation in that the node itself is not given
+ * an opportunity to participate, so that any information managed
+ * by node subclasses will be lost.
+ */
+ public Node importNode(Node src, boolean deep)
+ {
+ Node dst = null;
+ switch (src.getNodeType())
+ {
+ case TEXT_NODE:
+ dst = createTextNode(src.getNodeValue());
+ break;
+ case CDATA_SECTION_NODE:
+ dst = createCDATASection(src.getNodeValue());
+ break;
+ case COMMENT_NODE:
+ dst = createComment(src.getNodeValue());
+ break;
+ case PROCESSING_INSTRUCTION_NODE:
+ dst = createProcessingInstruction(src.getNodeName(),
+ src.getNodeValue());
+ break;
+ case NOTATION_NODE:
+ // NOTE: There's no standard way to create
+ // these, or add them to a doctype. Useless.
+ Notation notation = (Notation) src;
+ dst = new DomNotation(this, notation.getNodeName(),
+ notation.getPublicId(),
+ notation.getSystemId());
+ break;
+ case ENTITY_NODE:
+ // NOTE: There's no standard way to create
+ // these, or add them to a doctype. Useless.
+ Entity entity = (Entity) src;
+ dst = new DomEntity(this, entity.getNodeName(),
+ entity.getPublicId(),
+ entity.getSystemId(),
+ entity.getNotationName());
+ if (deep)
+ {
+ for (Node ctx = src.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ dst.appendChild(importNode(ctx, deep));
+ }
+ }
+ break;
+ case ENTITY_REFERENCE_NODE:
+ dst = createEntityReference(src.getNodeName());
+ break;
+ case DOCUMENT_FRAGMENT_NODE:
+ dst = new DomDocumentFragment(this);
+ if (deep)
+ {
+ for (Node ctx = src.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ dst.appendChild(importNode(ctx, deep));
+ }
+ }
+ break;
+ case ATTRIBUTE_NODE:
+ String attr_nsuri = src.getNamespaceURI();
+ if (attr_nsuri != null)
+ {
+ dst = createAttributeNS(attr_nsuri, src.getNodeName());
+ }
+ else
+ {
+ dst = createAttribute(src.getNodeName());
+ }
+ // this is _always_ done regardless of "deep" setting
+ for (Node ctx = src.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ dst.appendChild(importNode(ctx, false));
+ }
+ break;
+ case ELEMENT_NODE:
+ String elem_nsuri = src.getNamespaceURI();
+ if (elem_nsuri != null)
+ {
+ dst = createElementNS(elem_nsuri, src.getNodeName());
+ }
+ else
+ {
+ dst = createElement(src.getNodeName());
+ }
+ NamedNodeMap srcAttrs = src.getAttributes();
+ NamedNodeMap dstAttrs = dst.getAttributes();
+ int len = srcAttrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Attr a = (Attr) srcAttrs.item(i);
+ Attr dflt;
+
+ // maybe update defaulted attributes
+ dflt = (Attr) dstAttrs.getNamedItem(a.getNodeName());
+ if (dflt != null)
+ {
+ String newval = a.getNodeValue();
+ if (!dflt.getNodeValue().equals(newval)
+ || a.getSpecified () == true)
+ {
+ dflt.setNodeValue (newval);
+ }
+ continue;
+ }
+
+ dstAttrs.setNamedItem((Attr) importNode(a, false));
+ }
+ if (deep)
+ {
+ for (Node ctx = src.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ dst.appendChild(importNode(ctx, true));
+ }
+ }
+ break;
+ // can't import document or doctype nodes
+ case DOCUMENT_NODE:
+ case DOCUMENT_TYPE_NODE:
+ // FALLTHROUGH
+ // can't import unrecognized or nonstandard nodes
+ default:
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, null, src, 0);
+ }
+
+ // FIXME cleanup a bit -- for deep copies, copy those
+ // children in one place, here (code sharing is healthy)
+
+ if (src instanceof DomNode)
+ {
+ ((DomNode) src).notifyUserDataHandlers(UserDataHandler.NODE_IMPORTED,
+ src, dst);
+ }
+ return dst;
+ }
+
+ /**
+ * <b>DOM L2 (Traversal)</b>
+ * Returns a newly created node iterator. Don't forget to detach
+ * this iterator when you're done using it!
+ *
+ * @see DomIterator
+ */
+ public NodeIterator createNodeIterator(Node root,
+ int whatToShow,
+ NodeFilter filter,
+ boolean expandEntities)
+ {
+ return new DomNodeIterator(root, whatToShow, filter, expandEntities,
+ false);
+ }
+
+ public TreeWalker createTreeWalker(Node root,
+ int whatToShow,
+ NodeFilter filter,
+ boolean expandEntities)
+ {
+ return new DomNodeIterator(root, whatToShow, filter, expandEntities,
+ true);
+ }
+
+ // DOM Level 3 methods
+
+ /**
+ * DOM L3
+ */
+ public String getInputEncoding()
+ {
+ return inputEncoding;
+ }
+
+ public void setInputEncoding(String inputEncoding)
+ {
+ this.inputEncoding = inputEncoding;
+ }
+
+ /**
+ * DOM L3
+ */
+ public String getXmlEncoding()
+ {
+ return encoding;
+ }
+
+ public void setXmlEncoding(String encoding)
+ {
+ this.encoding = encoding;
+ }
+
+ public boolean getXmlStandalone()
+ {
+ return standalone;
+ }
+
+ public void setXmlStandalone(boolean xmlStandalone)
+ {
+ standalone = xmlStandalone;
+ }
+
+ public String getXmlVersion()
+ {
+ return version;
+ }
+
+ public void setXmlVersion(String xmlVersion)
+ {
+ if (xmlVersion == null)
+ {
+ xmlVersion = "1.0";
+ }
+ if ("1.0".equals(xmlVersion) ||
+ "1.1".equals(xmlVersion))
+ {
+ version = xmlVersion;
+ }
+ else
+ {
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
+ }
+ }
+
+ public boolean getStrictErrorChecking()
+ {
+ return checkingCharacters;
+ }
+
+ public void setStrictErrorChecking(boolean strictErrorChecking)
+ {
+ checkingCharacters = strictErrorChecking;
+ }
+
+ public String lookupPrefix(String namespaceURI)
+ {
+ Node root = getDocumentElement();
+ return (root == null) ? null : root.lookupPrefix(namespaceURI);
+ }
+
+ public boolean isDefaultNamespace(String namespaceURI)
+ {
+ Node root = getDocumentElement();
+ return (root == null) ? false : root.isDefaultNamespace(namespaceURI);
+ }
+
+ public String lookupNamespaceURI(String prefix)
+ {
+ Node root = getDocumentElement();
+ return (root == null) ? null : root.lookupNamespaceURI(prefix);
+ }
+
+ public String getBaseURI()
+ {
+ return getDocumentURI();
+ /*
+ Node root = getDocumentElement();
+ if (root != null)
+ {
+ NamedNodeMap attrs = root.getAttributes();
+ Node xmlBase = attrs.getNamedItemNS(XMLConstants.XML_NS_URI, "base");
+ if (xmlBase != null)
+ {
+ return xmlBase.getNodeValue();
+ }
+ }
+ return systemId;
+ */
+ }
+
+ public String getDocumentURI()
+ {
+ return systemId;
+ }
+
+ public void setDocumentURI(String documentURI)
+ {
+ systemId = documentURI;
+ }
+
+ public Node adoptNode(Node source)
+ {
+ int sourceNodeType = source.getNodeType();
+ switch (sourceNodeType)
+ {
+ case DOCUMENT_NODE:
+ case DOCUMENT_TYPE_NODE:
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
+ case ENTITY_NODE:
+ case NOTATION_NODE:
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+ if (source instanceof DomNode)
+ {
+ // GNU native
+ DomNode src = (DomNode) source;
+ DomNode dst = src;
+ if (dst.parent != null)
+ {
+ dst = (DomNode) dst.cloneNode(true);
+ }
+ dst.setOwner(this);
+ src.notifyUserDataHandlers(UserDataHandler.NODE_ADOPTED, src, dst);
+ return dst;
+ }
+ else
+ {
+ // Some other implementation
+ Node dst = null;
+ switch (sourceNodeType)
+ {
+ case Node.ATTRIBUTE_NODE:
+ {
+ Attr src = (Attr) source;
+ String nodeName = src.getNodeName();
+ String localName = src.getLocalName();
+ String namespaceUri = src.getNamespaceURI();
+ dst = (localName == null) ?
+ createAttribute(nodeName) :
+ createAttributeNS(namespaceUri, nodeName);
+ adoptChildren(src, dst);
+ break;
+ }
+ case Node.CDATA_SECTION_NODE:
+ {
+ CDATASection src = (CDATASection) source;
+ dst = createCDATASection(src.getData());
+ break;
+ }
+ case Node.COMMENT_NODE:
+ {
+ Comment src = (Comment) source;
+ dst = createComment(src.getData());
+ break;
+ }
+ case Node.DOCUMENT_FRAGMENT_NODE:
+ {
+ DocumentFragment src = (DocumentFragment) source;
+ dst = createDocumentFragment();
+ adoptChildren(src, dst);
+ break;
+ }
+ case Node.ELEMENT_NODE:
+ {
+ Element src = (Element) source;
+ String nodeName = src.getNodeName();
+ String localName = src.getLocalName();
+ String namespaceUri = src.getNamespaceURI();
+ dst = (localName == null) ?
+ createElement(nodeName) :
+ createElementNS(namespaceUri, nodeName);
+ adoptAttributes(src, dst);
+ adoptChildren(src, dst);
+ break;
+ }
+ case Node.ENTITY_REFERENCE_NODE:
+ {
+ EntityReference src = (EntityReference) source;
+ dst = createEntityReference(src.getNodeName());
+ adoptChildren(src, dst);
+ break;
+ }
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ {
+ ProcessingInstruction src = (ProcessingInstruction) source;
+ dst = createProcessingInstruction(src.getTarget(),
+ src.getData());
+ break;
+ }
+ case Node.TEXT_NODE:
+ {
+ Text src = (Text) source;
+ dst = createTextNode(src.getData());
+ break;
+ }
+ }
+ return dst;
+ }
+ }
+
+ void adoptChildren(Node src, Node dst)
+ {
+ Node node = src.getFirstChild();
+ while (node != null)
+ {
+ Node next = node.getNextSibling();
+ dst.appendChild(adoptNode(node));
+ node = next;
+ }
+ }
+
+ void adoptAttributes(Node src, Node dst)
+ {
+ NamedNodeMap srcAttrs = src.getAttributes();
+ NamedNodeMap dstAttrs = dst.getAttributes();
+ int len = srcAttrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Node node = srcAttrs.item(i);
+ String localName = node.getLocalName();
+ if (localName == null)
+ {
+ dstAttrs.setNamedItem(adoptNode(node));
+ }
+ else
+ {
+ dstAttrs.setNamedItemNS(adoptNode(node));
+ }
+ }
+ }
+
+ public DOMConfiguration getDomConfig()
+ {
+ if (config == null)
+ {
+ config = new DomDocumentConfiguration();
+ }
+ return config;
+ }
+
+ public boolean isEqualNode(Node arg)
+ {
+ if (!super.isEqualNode(arg))
+ return false;
+ Document d = (Document) arg;
+ String dversion = d.getXmlVersion();
+ if (dversion == null || !dversion.equals(version))
+ return false;
+ boolean dstandalone = d.getXmlStandalone();
+ if (dstandalone != standalone)
+ return false;
+ String dencoding = d.getXmlEncoding();
+ if (dencoding == null || dencoding.equalsIgnoreCase("UTF-8"))
+ {
+ if (encoding != null && !encoding.equalsIgnoreCase("UTF-8"))
+ return false;
+ }
+ else
+ {
+ if (!dencoding.equals(encoding))
+ return false;
+ }
+ return true;
+ }
+
+ public void normalizeDocument()
+ {
+ boolean save = building;
+ building = true;
+ normalizeNode(this);
+ building = save;
+ }
+
+ void normalizeNode(DomNode node)
+ {
+ node.normalize();
+ if (config != null)
+ {
+ switch (node.nodeType)
+ {
+ case CDATA_SECTION_NODE:
+ if (!config.cdataSections)
+ {
+ // replace CDATA section with text node
+ Text text = createTextNode(node.getNodeValue());
+ node.parent.insertBefore(text, node);
+ node.parent.removeChild(node);
+ // merge adjacent text nodes
+ String data = text.getWholeText();
+ node = (DomNode) text.replaceWholeText(data);
+ }
+ else if (config.splitCdataSections)
+ {
+ String value = node.getNodeValue();
+ int i = value.indexOf("]]>");
+ while (i != -1)
+ {
+ Node node2 = createCDATASection(value.substring(0, i));
+ node.parent.insertBefore(node2, node);
+ value = value.substring(i + 3);
+ node.setNodeValue(value);
+ i = value.indexOf("]]>");
+ }
+ }
+ break;
+ case COMMENT_NODE:
+ if (!config.comments)
+ {
+ node.parent.removeChild(node);
+ }
+ break;
+ case TEXT_NODE:
+ if (!config.elementContentWhitespace &&
+ ((Text) node).isElementContentWhitespace())
+ {
+ node.parent.removeChild(node);
+ }
+ break;
+ case ENTITY_REFERENCE_NODE:
+ if (!config.entities)
+ {
+ for (DomNode ctx = node.first; ctx != null; )
+ {
+ DomNode ctxNext = ctx.next;
+ node.parent.insertBefore(ctx, node);
+ ctx = ctxNext;
+ }
+ node.parent.removeChild(node);
+ }
+ break;
+ case ELEMENT_NODE:
+ if (!config.namespaceDeclarations)
+ {
+ DomNamedNodeMap attrs =
+ (DomNamedNodeMap) node.getAttributes();
+ boolean aro = attrs.readonly;
+ attrs.readonly = false; // Ensure we can delete if necessary
+ int len = attrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Node attr = attrs.item(i);
+ String namespace = attr.getNamespaceURI();
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespace))
+ {
+ attrs.removeNamedItemNS(namespace,
+ attr.getLocalName());
+ i--;
+ len--;
+ }
+ }
+ attrs.readonly = aro;
+ }
+ break;
+ }
+ }
+ for (DomNode ctx = node.first; ctx != null; )
+ {
+ DomNode ctxNext = ctx.next;
+ normalizeNode(ctx);
+ ctx = ctxNext;
+ }
+ }
+
+ public Node renameNode(Node n, String namespaceURI, String qualifiedName)
+ throws DOMException
+ {
+ if (n instanceof DomNsNode)
+ {
+ DomNsNode src = (DomNsNode) n;
+ if (src == null)
+ {
+ throw new DomDOMException(DOMException.NOT_FOUND_ERR);
+ }
+ if (src.owner != this)
+ {
+ throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR,
+ null, src, 0);
+ }
+ boolean xml11 = "1.1".equals(version);
+ checkName(qualifiedName, xml11);
+ int ci = qualifiedName.indexOf(':');
+ if ("".equals(namespaceURI))
+ {
+ namespaceURI = null;
+ }
+ if (namespaceURI != null)
+ {
+ checkNCName(qualifiedName, xml11);
+ String prefix = (ci == -1) ? "" :
+ qualifiedName.substring(0, ci);
+ if (XMLConstants.XML_NS_PREFIX.equals(prefix) &&
+ !XMLConstants.XML_NS_URI.equals(namespaceURI))
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "xml namespace must be " +
+ XMLConstants.XML_NS_URI, src, 0);
+ }
+ else if (src.nodeType == ATTRIBUTE_NODE &&
+ (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) ||
+ XMLConstants.XMLNS_ATTRIBUTE.equals(qualifiedName)) &&
+ !XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI))
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "xmlns namespace must be " +
+ XMLConstants.XMLNS_ATTRIBUTE_NS_URI, src, 0);
+ }
+ if (XMLConstants.XML_NS_URI.equals(namespaceURI) &&
+ !XMLConstants.XML_NS_PREFIX.equals(prefix))
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "xml namespace must be " +
+ XMLConstants.XML_NS_URI, src, 0);
+ }
+ else if (src.nodeType == ATTRIBUTE_NODE &&
+ XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI) &&
+ !(XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) ||
+ XMLConstants.XMLNS_ATTRIBUTE.equals(qualifiedName)))
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "xmlns namespace must be " +
+ XMLConstants.XMLNS_ATTRIBUTE_NS_URI, src, 0);
+ }
+
+ }
+ src.setNodeName(qualifiedName);
+ src.setNamespaceURI(namespaceURI);
+ src.notifyUserDataHandlers(UserDataHandler.NODE_RENAMED, src, src);
+ // TODO MutationNameEvents
+ // DOMElementNameChanged or DOMAttributeNameChanged
+ return src;
+ }
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, null, n, 0);
+ }
+
+ // -- XPathEvaluator --
+
+ public XPathExpression createExpression(String expression,
+ XPathNSResolver resolver)
+ throws XPathException, DOMException
+ {
+ return new DomXPathExpression(this, expression, resolver);
+ }
+
+ public XPathNSResolver createNSResolver(Node nodeResolver)
+ {
+ return new DomXPathNSResolver(nodeResolver);
+ }
+
+ public Object evaluate(String expression,
+ Node contextNode,
+ XPathNSResolver resolver,
+ short type,
+ Object result)
+ throws XPathException, DOMException
+ {
+ XPathExpression xpe =
+ new DomXPathExpression(this, expression, resolver);
+ return xpe.evaluate(contextNode, type, result);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomDocumentBuilder.java b/libjava/classpath/gnu/xml/dom/DomDocumentBuilder.java
new file mode 100644
index 000000000..99c254481
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomDocumentBuilder.java
@@ -0,0 +1,227 @@
+/* DomDocumentBuilder.java --
+ Copyright (C) 2004,2006,2007 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.Reader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import javax.xml.parsers.DocumentBuilder;
+import org.w3c.dom.Document;
+import org.w3c.dom.DOMConfiguration;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.ls.DOMImplementationLS;
+import org.w3c.dom.ls.LSException;
+import org.w3c.dom.ls.LSInput;
+import org.w3c.dom.ls.LSParser;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * Document builder using the GNU DOM Load &amp; Save implementation.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class DomDocumentBuilder
+ extends DocumentBuilder
+{
+
+ final DOMImplementation impl;
+ final DOMImplementationLS ls;
+ final LSParser parser;
+
+ DomDocumentBuilder(DOMImplementation impl,
+ DOMImplementationLS ls,
+ LSParser parser)
+ {
+ this.impl = impl;
+ this.ls = ls;
+ this.parser = parser;
+ }
+
+ public boolean isNamespaceAware()
+ {
+ DOMConfiguration config = parser.getDomConfig();
+ return ((Boolean) config.getParameter("namespaces")).booleanValue();
+ }
+
+ public boolean isValidating()
+ {
+ DOMConfiguration config = parser.getDomConfig();
+ return ((Boolean) config.getParameter("validating")).booleanValue();
+ }
+
+ public boolean isXIncludeAware()
+ {
+ DOMConfiguration config = parser.getDomConfig();
+ return ((Boolean) config.getParameter("xinclude-aware")).booleanValue();
+ }
+
+ public void setEntityResolver(EntityResolver resolver)
+ {
+ DOMConfiguration config = parser.getDomConfig();
+ config.setParameter("entity-resolver", resolver);
+ }
+
+ public void setErrorHandler(ErrorHandler handler)
+ {
+ DOMConfiguration config = parser.getDomConfig();
+ config.setParameter("error-handler", handler);
+ }
+
+ public DOMImplementation getDOMImplementation()
+ {
+ return impl;
+ }
+
+ public Document newDocument()
+ {
+ return impl.createDocument(null, null, null);
+ }
+
+ public Document parse(InputStream in)
+ throws SAXException, IOException
+ {
+ LSInput input = ls.createLSInput();
+ input.setByteStream(in);
+ try
+ {
+ return parser.parse(input);
+ }
+ catch (LSException e)
+ {
+ Throwable e2 = e.getCause();
+ if (e2 instanceof IOException)
+ throw (IOException) e2;
+ else
+ throw e;
+ }
+ }
+
+ public Document parse(InputStream in, String systemId)
+ throws SAXException, IOException
+ {
+ LSInput input = ls.createLSInput();
+ input.setByteStream(in);
+ input.setSystemId(systemId);
+ try
+ {
+ return parser.parse(input);
+ }
+ catch (LSException e)
+ {
+ Throwable e2 = e.getCause();
+ if (e2 instanceof IOException)
+ throw (IOException) e2;
+ else
+ throw e;
+ }
+ }
+
+ public Document parse(String systemId)
+ throws SAXException, IOException
+ {
+ try
+ {
+ return parser.parseURI(systemId);
+ }
+ catch (LSException e)
+ {
+ Throwable e2 = e.getCause();
+ if (e2 instanceof IOException)
+ throw (IOException) e2;
+ else
+ throw e;
+ }
+ }
+
+ public Document parse(InputSource is)
+ throws SAXException, IOException
+ {
+ LSInput input = ls.createLSInput();
+ String systemId = is.getSystemId();
+ InputStream in = is.getByteStream();
+ if (in != null)
+ {
+ input.setByteStream(in);
+ }
+ else
+ {
+ Reader reader = is.getCharacterStream();
+ if (reader != null)
+ {
+ input.setCharacterStream(reader);
+ }
+ else
+ {
+ try
+ {
+ URL url = new URL(systemId);
+ input.setByteStream(url.openStream());
+ }
+ catch (MalformedURLException e)
+ {
+ // Maybe this is a relative file URL
+ File cwd = new File(System.getProperty("user.dir"));
+ URL url = new URL(cwd.toURL(), systemId);
+ input.setByteStream(url.openStream());
+ }
+ }
+ }
+ input.setPublicId(is.getPublicId());
+ input.setSystemId(systemId);
+ input.setEncoding(is.getEncoding());
+ try
+ {
+ return parser.parse(input);
+ }
+ catch (LSException e)
+ {
+ Throwable e2 = e.getCause();
+ if (e2 instanceof IOException)
+ throw (IOException) e2;
+ else
+ throw e;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomDocumentBuilderFactory.java b/libjava/classpath/gnu/xml/dom/DomDocumentBuilderFactory.java
new file mode 100644
index 000000000..1e3eaa582
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomDocumentBuilderFactory.java
@@ -0,0 +1,181 @@
+/* DomDocumentBuilderFactory.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import javax.xml.XMLConstants;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.FactoryConfigurationError;
+import javax.xml.parsers.ParserConfigurationException;
+import org.w3c.dom.DOMConfiguration;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.bootstrap.DOMImplementationRegistry;
+import org.w3c.dom.ls.DOMImplementationLS;
+import org.w3c.dom.ls.LSParser;
+
+/**
+ * Document builder factory that uses a DOM Level 3 Load &amp; Save
+ * implementation.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomDocumentBuilderFactory
+ extends DocumentBuilderFactory
+{
+
+ final DOMImplementation impl;
+ final DOMImplementationLS ls;
+ private boolean secureProcessing;
+
+ public DomDocumentBuilderFactory()
+ {
+ try
+ {
+ DOMImplementationRegistry reg =
+ DOMImplementationRegistry.newInstance();
+ impl = reg.getDOMImplementation("LS 3.0");
+ if (impl == null)
+ {
+ throw new FactoryConfigurationError("no LS implementations found");
+ }
+ ls = (DOMImplementationLS) impl;
+ }
+ catch (Exception e)
+ {
+ throw new FactoryConfigurationError(e);
+ }
+ }
+
+ public DocumentBuilder newDocumentBuilder()
+ throws ParserConfigurationException
+ {
+ LSParser parser = null;
+ try
+ {
+ parser = ls.createLSParser(DOMImplementationLS.MODE_ASYNCHRONOUS,
+ "http://www.w3.org/TR/REC-xml");
+ }
+ catch (DOMException e)
+ {
+ if (e.code == DOMException.NOT_SUPPORTED_ERR)
+ {
+ // Fall back to synchronous parser
+ try
+ {
+ parser = ls.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS,
+ "http://www.w3.org/TR/REC-xml");
+ }
+ catch (DOMException e2)
+ {
+ ParserConfigurationException pce =
+ new ParserConfigurationException();
+ pce.initCause(e2);
+ throw pce;
+ }
+ }
+ else
+ {
+ ParserConfigurationException pce =
+ new ParserConfigurationException();
+ pce.initCause(e);
+ throw pce;
+ }
+ }
+ DOMConfiguration config = parser.getDomConfig();
+ setParameter(config, "namespaces",
+ isNamespaceAware() ? Boolean.TRUE : Boolean.FALSE);
+ setParameter(config, "element-content-whitespace",
+ isIgnoringElementContentWhitespace() ? Boolean.FALSE :
+ Boolean.TRUE);
+ setParameter(config, "comments",
+ isIgnoringComments() ? Boolean.FALSE : Boolean.TRUE);
+ setParameter(config, "expand-entity-references",
+ isExpandEntityReferences() ? Boolean.TRUE : Boolean.FALSE);
+ setParameter(config, "coalescing",
+ isCoalescing() ? Boolean.TRUE : Boolean.FALSE);
+ setParameter(config, "validating",
+ isValidating() ? Boolean.TRUE : Boolean.FALSE);
+ setParameter(config, "xinclude-aware",
+ isXIncludeAware() ? Boolean.TRUE : Boolean.FALSE);
+ return new DomDocumentBuilder(impl, ls, parser);
+ }
+
+ void setParameter(DOMConfiguration config, String name, Object value)
+ throws ParserConfigurationException
+ {
+ if (!config.canSetParameter(name, value))
+ {
+ throw new ParserConfigurationException(name);
+ }
+ config.setParameter(name, value);
+ }
+
+ public Object getAttribute(String name)
+ {
+ // TODO
+ return null;
+ }
+
+ public void setAttribute(String name, Object value)
+ {
+ // TODO
+ }
+
+ public void setFeature(String name, boolean value)
+ throws ParserConfigurationException
+ {
+ if (name == null)
+ throw new NullPointerException();
+ if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name))
+ {
+ secureProcessing = true;
+ return;
+ }
+ throw new ParserConfigurationException(name);
+ }
+
+ public boolean getFeature(String name)
+ throws ParserConfigurationException
+ {
+ if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name))
+ return secureProcessing;
+ throw new ParserConfigurationException(name);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomDocumentConfiguration.java b/libjava/classpath/gnu/xml/dom/DomDocumentConfiguration.java
new file mode 100644
index 000000000..9aab44532
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomDocumentConfiguration.java
@@ -0,0 +1,260 @@
+/* DomDocumentConfiguration.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import java.util.Arrays;
+import java.util.List;
+import org.w3c.dom.DOMConfiguration;
+import org.w3c.dom.DOMErrorHandler;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.DOMStringList;
+
+/**
+ * Document configuration, used to store normalization and other parameters.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class DomDocumentConfiguration
+ implements DOMConfiguration, DOMStringList
+{
+
+ private static final List SUPPORTED_PARAMETERS =
+ Arrays.asList(new String[] { "cdata-sections",
+ "comments",
+ "element-content-whitespace",
+ "entities",
+ "error-handler",
+ "namespace-declarations",
+ "split-cdata-sections",
+ "infoset"});
+
+ boolean cdataSections = true;
+ boolean comments = true;
+ boolean elementContentWhitespace = true;
+ boolean entities = true;
+ DOMErrorHandler errorHandler;
+ boolean namespaceDeclarations = true;
+ boolean splitCdataSections = true;
+
+ public void setParameter(String name, Object value)
+ throws DOMException
+ {
+ name = name.toLowerCase();
+ if ("cdata-sections".equals(name))
+ {
+ cdataSections = "true".equals(value.toString());
+ }
+ else if ("comments".equals(name))
+ {
+ comments = "true".equals(value.toString());
+ }
+ else if ("element-content-whitespace".equals(name))
+ {
+ elementContentWhitespace = "true".equals(value.toString());
+ }
+ else if ("entities".equals(name))
+ {
+ entities = "true".equals(value.toString());
+ }
+ else if ("error-handler".equals(name))
+ {
+ try
+ {
+ errorHandler = (DOMErrorHandler) value;
+ }
+ catch (ClassCastException e)
+ {
+ throw new DomDOMException(DOMException.TYPE_MISMATCH_ERR,
+ value.getClass().getName(), null, 0);
+ }
+ }
+ else if ("namespace-declarations".equals(name))
+ {
+ namespaceDeclarations = "true".equals(value.toString());
+ }
+ else if ("split-cdata-sections".equals(name))
+ {
+ comments = "true".equals(value.toString());
+ }
+ else if ("infoset".equals(name))
+ {
+ if ("true".equals(value.toString()))
+ {
+ entities = false;
+ cdataSections = false;
+ namespaceDeclarations = true;
+ elementContentWhitespace = true;
+ comments = true;
+ }
+ }
+ else if (("canonical-form".equals(name) ||
+ "check-character-normalization".equals(name) ||
+ "datatype-normalization".equals(name) ||
+ "normalize-characters".equals(name) ||
+ "validate".equals(name) ||
+ "validate-if-schema".equals(name)) &&
+ "false".equals(value.toString()))
+ {
+ // NOOP
+ }
+ else if (("namespaces".equals(name) ||
+ "well-formed".equals(name)) &&
+ "true".equals(value.toString()))
+ {
+ // NOOP
+ }
+ else
+ {
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR,
+ name, null, 0);
+ }
+ }
+
+ public Object getParameter(String name)
+ throws DOMException
+ {
+ name = name.toLowerCase();
+ if ("cdata-sections".equals(name))
+ {
+ return cdataSections ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("comments".equals(name))
+ {
+ return comments ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("element-content-whitespace".equals(name))
+ {
+ return elementContentWhitespace ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("entities".equals(name))
+ {
+ return entities ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("error-handler".equals(name))
+ {
+ return errorHandler;
+ }
+ else if ("namespace-declarations".equals(name))
+ {
+ return namespaceDeclarations ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("split-cdata-sections".equals(name))
+ {
+ return comments ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("canonical-form".equals(name) ||
+ "check-character-normalization".equals(name) ||
+ "datatype-normalization".equals(name) ||
+ "normalize-characters".equals(name) ||
+ "validate".equals(name) ||
+ "validate-if-schema".equals(name))
+ {
+ return Boolean.FALSE;
+ }
+ else if ("namespaces".equals(name) ||
+ "well-formed".equals(name))
+ {
+ return Boolean.TRUE;
+ }
+ else if ("infoset".equals(name))
+ {
+ return (entities == false &&
+ cdataSections == false &&
+ namespaceDeclarations == true &&
+ comments == true) ? Boolean.TRUE : Boolean.FALSE;
+ }
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, name, null, 0);
+ }
+
+ public boolean canSetParameter(String name, Object value)
+ {
+ name = name.toLowerCase();
+ if ("error-handler".equals(name))
+ {
+ return (value == null || value instanceof DOMErrorHandler);
+ }
+ else if (contains(name))
+ {
+ return true;
+ }
+ else if ("canonical-form".equals(name) ||
+ "check-character-normalization".equals(name) ||
+ "datatype-normalization".equals(name) ||
+ "normalize-characters".equals(name) ||
+ "validate".equals(name) ||
+ "validate-if-schema".equals(name))
+ {
+ return "false".equals(value.toString());
+ }
+ else if ("namespaces".equals(name) ||
+ "well-formed".equals(name))
+ {
+ return "true".equals(value.toString());
+ }
+ return false;
+ }
+
+ public DOMStringList getParameterNames()
+ {
+ return this;
+ }
+
+ public String item(int i)
+ {
+ try
+ {
+ return (String) SUPPORTED_PARAMETERS.get(i);
+ }
+ catch (IndexOutOfBoundsException e)
+ {
+ return null;
+ }
+ }
+
+ public int getLength()
+ {
+ return SUPPORTED_PARAMETERS.size();
+ }
+
+ public boolean contains(String str)
+ {
+ str = str.toLowerCase();
+ return SUPPORTED_PARAMETERS.contains(str);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomDocumentFragment.java b/libjava/classpath/gnu/xml/dom/DomDocumentFragment.java
new file mode 100644
index 000000000..8c4e0db3b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomDocumentFragment.java
@@ -0,0 +1,75 @@
+/* DomDocumentFragment.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.DocumentFragment;
+
+/**
+ * <p> "DocumentFragment" implementation. </p>
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomDocumentFragment
+ extends DomNode
+ implements DocumentFragment
+{
+
+ /**
+ * Constructs a DocumentFragment node associated with the
+ * specified document.
+ *
+ * <p>This constructor should only be invoked by a Document as part of
+ * its createDocumentFragment functionality, or through a subclass which
+ * is similarly used in a "Sub-DOM" style layer.
+ */
+ protected DomDocumentFragment(DomDocument owner)
+ {
+ super(DOCUMENT_FRAGMENT_NODE, owner);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the string "#document-fragment".
+ */
+ final public String getNodeName()
+ {
+ return "#document-fragment";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomElement.java b/libjava/classpath/gnu/xml/dom/DomElement.java
new file mode 100644
index 000000000..429749978
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomElement.java
@@ -0,0 +1,582 @@
+/* DomElement.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import java.util.HashSet;
+import java.util.Set;
+import javax.xml.XMLConstants;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.TypeInfo;
+
+/**
+ * <p> "Element" implementation.
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomElement
+ extends DomNsNode
+ implements Element
+{
+
+ /**
+ * User-defined ID attributes.
+ * Used by DomAttr.isId and DomDocument.getElementById
+ */
+ Set userIdAttrs;
+
+ // Attributes are VERY expensive in DOM, and not just for
+ // this implementation. Avoid creating them.
+ private DomNamedNodeMap attributes;
+
+ // xml:space cache
+ String xmlSpace = "";
+
+ /**
+ * Constructs an Element node associated with the specified document.
+ *
+ * <p>This constructor should only be invoked by a Document as part
+ * of its createElement functionality, or through a subclass which is
+ * similarly used in a "Sub-DOM" style layer.
+ *
+ * @param owner The document with which this node is associated
+ * @param namespaceURI Combined with the local part of the name,
+ * this is used to uniquely identify a type of element
+ * @param name Name of this element, which may include a prefix
+ */
+ protected DomElement(DomDocument owner, String namespaceURI, String name)
+ {
+ super(ELEMENT_NODE, owner, namespaceURI, name);
+ }
+
+ /**
+ * <p>
+ * Constructs an Element node associated with the specified document.
+ * This constructor should only be invoked by a Document as part
+ * of its createElement functionality, or through a subclass which is
+ * similarly used in a "Sub-DOM" style layer.
+ * </p>
+ * <p>
+ * With this constructor, the prefix and local part are given explicitly
+ * rather than being computed. This allows them to be explicitly set to
+ * {@code null} as required by {@link Document#createElement(String)}.
+ * </p>
+ *
+ * @param owner The document with which this node is associated
+ * @param namespaceURI Combined with the local part of the name,
+ * this is used to uniquely identify a type of element
+ * @param name Name of this element, which may include a prefix
+ * @param prefix the namespace prefix of the name. May be {@code null}.
+ * @param localName the local part of the name. May be {@code null}.
+ */
+ protected DomElement(DomDocument owner, String namespaceURI, String name,
+ String prefix, String localName)
+ {
+ super(ELEMENT_NODE, owner, namespaceURI, name, prefix, localName);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the element's attributes
+ */
+ public NamedNodeMap getAttributes()
+ {
+ if (attributes == null)
+ {
+ attributes = new DomNamedNodeMap(this, Node.ATTRIBUTE_NODE);
+ }
+ return attributes;
+ }
+
+ /**
+ * <b>DOM L2></b>
+ * Returns true iff this is an element node with attributes.
+ */
+ public boolean hasAttributes()
+ {
+ return attributes != null && attributes.length != 0;
+ }
+
+ /**
+ * Shallow clone of the element, except that associated
+ * attributes are (deep) cloned.
+ */
+ public Object clone()
+ {
+ DomElement node = (DomElement) super.clone();
+
+ if (attributes != null)
+ {
+ node.attributes = new DomNamedNodeMap(node, Node.ATTRIBUTE_NODE);
+ for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
+ {
+ node.attributes.setNamedItem(ctx.cloneNode(true), true, true);
+ }
+ }
+ return node;
+ }
+
+ void setOwner(DomDocument doc)
+ {
+ if (attributes != null)
+ {
+ for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
+ {
+ ctx.setOwner(doc);
+ }
+ }
+ super.setOwner(doc);
+ }
+
+ /**
+ * Marks this element, its children, and its associated attributes as
+ * readonly.
+ */
+ public void makeReadonly()
+ {
+ super.makeReadonly();
+ if (attributes != null)
+ {
+ attributes.makeReadonly();
+ }
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the element name (same as getNodeName).
+ */
+ final public String getTagName()
+ {
+ return getNodeName();
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the value of the specified attribute, or an
+ * empty string.
+ */
+ public String getAttribute(String name)
+ {
+ if ("xml:space" == name) // NB only works on interned string
+ {
+ // Use cached value
+ return xmlSpace;
+ }
+ Attr attr = getAttributeNode(name);
+ return (attr == null) ? "" : attr.getValue();
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns true if the element has an attribute with the
+ * specified name (specified or DTD defaulted).
+ */
+ public boolean hasAttribute(String name)
+ {
+ return getAttributeNode(name) != null;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns true if the element has an attribute with the
+ * specified name (specified or DTD defaulted).
+ */
+ public boolean hasAttributeNS(String namespaceURI, String local)
+ {
+ return getAttributeNodeNS(namespaceURI, local) != null;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns the value of the specified attribute, or an
+ * empty string.
+ */
+ public String getAttributeNS(String namespaceURI, String local)
+ {
+ Attr attr = getAttributeNodeNS(namespaceURI, local);
+ return (attr == null) ? "" : attr.getValue();
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the appropriate attribute node; the name is the
+ * nodeName property of the attribute.
+ */
+ public Attr getAttributeNode(String name)
+ {
+ return (attributes == null) ? null :
+ (Attr) attributes.getNamedItem(name);
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns the appropriate attribute node; the name combines
+ * the namespace name and the local part.
+ */
+ public Attr getAttributeNodeNS(String namespace, String localPart)
+ {
+ return (attributes == null) ? null :
+ (Attr) attributes.getNamedItemNS(namespace, localPart);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Modifies an existing attribute to have the specified value,
+ * or creates a new one with that value. The name used is the
+ * nodeName value.
+ */
+ public void setAttribute(String name, String value)
+ {
+ Attr attr = getAttributeNode(name);
+ if (attr != null)
+ {
+ attr.setNodeValue(value);
+ ((DomAttr) attr).setSpecified(true);
+ return;
+ }
+ attr = owner.createAttribute(name);
+ attr.setNodeValue(value);
+ setAttributeNode(attr);
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Modifies an existing attribute to have the specified value,
+ * or creates a new one with that value.
+ */
+ public void setAttributeNS(String uri, String aname, String value)
+ {
+ if (("xmlns".equals (aname) || aname.startsWith ("xmlns:"))
+ && !XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals (uri))
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "setting xmlns attribute to illegal value", this, 0);
+ }
+
+ Attr attr = getAttributeNodeNS(uri, aname);
+ if (attr != null)
+ {
+ attr.setNodeValue(value);
+ return;
+ }
+ attr = owner.createAttributeNS(uri, aname);
+ attr.setNodeValue(value);
+ setAttributeNodeNS(attr);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Stores the specified attribute, optionally overwriting any
+ * existing one with that name.
+ */
+ public Attr setAttributeNode(Attr attr)
+ {
+ return (Attr) getAttributes().setNamedItem(attr);
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Stores the specified attribute, optionally overwriting any
+ * existing one with that name.
+ */
+ public Attr setAttributeNodeNS(Attr attr)
+ {
+ return (Attr) getAttributes().setNamedItemNS(attr);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Removes the appropriate attribute node.
+ * If there is no such node, this is (bizarrely enough) a NOP so you
+ * won't see exceptions if your code deletes non-existent attributes.
+ *
+ * <p>Note that since there is no portable way for DOM to record
+ * DTD information, default values for attributes will never be
+ * provided automatically.
+ */
+ public void removeAttribute(String name)
+ {
+ if (attributes == null)
+ {
+ return;
+ }
+
+ try
+ {
+ attributes.removeNamedItem(name);
+ }
+ catch (DomDOMException e)
+ {
+ if (e.code != DOMException.NOT_FOUND_ERR)
+ {
+ throw e;
+ }
+ }
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Removes the appropriate attribute node; the name is the
+ * nodeName property of the attribute.
+ *
+ * <p>Note that since there is no portable way for DOM to record
+ * DTD information, default values for attributes will never be
+ * provided automatically.
+ */
+ public Attr removeAttributeNode(Attr node)
+ {
+ if (attributes == null)
+ {
+ throw new DomDOMException(DOMException.NOT_FOUND_ERR, null, node, 0);
+ }
+ return (Attr) attributes.removeNamedItem(node.getNodeName());
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Removes the appropriate attribute node; the name combines
+ * the namespace name and the local part.
+ *
+ * <p>Note that since there is no portable way for DOM to record
+ * DTD information, default values for attributes will never be
+ * provided automatically.
+ */
+ public void removeAttributeNS(String namespace, String localPart)
+ {
+ if (attributes == null)
+ {
+ throw new DomDOMException(DOMException.NOT_FOUND_ERR, localPart, null, 0);
+ }
+ attributes.removeNamedItemNS (namespace, localPart);
+ }
+
+ // DOM Level 3 methods
+
+ public String lookupPrefix(String namespaceURI)
+ {
+ if (namespaceURI == null)
+ {
+ return null;
+ }
+ String namespace = getNamespaceURI();
+ if (namespace != null && namespace.equals(namespaceURI))
+ {
+ return getPrefix();
+ }
+ if (attributes != null)
+ {
+ for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
+ {
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
+ .equals(ctx.getNamespaceURI()))
+ {
+ String value = ctx.getNodeValue();
+ if (value.equals(namespaceURI))
+ {
+ return ctx.getLocalName();
+ }
+ }
+ }
+ }
+ return super.lookupPrefix(namespaceURI);
+ }
+
+ public boolean isDefaultNamespace(String namespaceURI)
+ {
+ String namespace = getNamespaceURI();
+ if (namespace != null && namespace.equals(namespaceURI))
+ {
+ return getPrefix() == null;
+ }
+ if (attributes != null)
+ {
+ for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
+ {
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
+ .equals(ctx.getNamespaceURI()))
+ {
+ String qName = ctx.getNodeName();
+ return (XMLConstants.XMLNS_ATTRIBUTE.equals(qName));
+ }
+ }
+ }
+ return super.isDefaultNamespace(namespaceURI);
+ }
+
+ public String lookupNamespaceURI(String prefix)
+ {
+ String namespace = getNamespaceURI();
+ if (namespace != null && equal(prefix, getPrefix()))
+ {
+ return namespace;
+ }
+ if (attributes != null)
+ {
+ for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
+ {
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
+ .equals(ctx.getNamespaceURI()))
+ {
+ if (prefix == null)
+ {
+ if (XMLConstants.XMLNS_ATTRIBUTE.equals(ctx.getNodeName()))
+ {
+ return ctx.getNodeValue();
+ }
+ }
+ else
+ {
+ if (prefix.equals(ctx.getLocalName()))
+ {
+ return ctx.getNodeValue();
+ }
+ }
+ }
+ }
+ }
+ return super.lookupNamespaceURI(prefix);
+ }
+
+ public String getBaseURI()
+ {
+ if (attributes != null)
+ {
+ Node xmlBase =
+ attributes.getNamedItemNS(XMLConstants.XML_NS_URI, "base");
+ if (xmlBase != null)
+ {
+ return xmlBase.getNodeValue();
+ }
+ }
+ return super.getBaseURI();
+ }
+
+ public TypeInfo getSchemaTypeInfo()
+ {
+ // DTD implementation
+ DomDoctype doctype = (DomDoctype) owner.getDoctype();
+ if (doctype != null)
+ {
+ return doctype.getElementTypeInfo(getNodeName());
+ }
+ // TODO XML Schema implementation
+ return null;
+ }
+
+ public void setIdAttribute(String name, boolean isId)
+ {
+ NamedNodeMap attrs = getAttributes();
+ Attr attr = (Attr) attrs.getNamedItem(name);
+ setIdAttributeNode(attr, isId);
+ }
+
+ public void setIdAttributeNode(Attr attr, boolean isId)
+ {
+ if (readonly)
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+ if (attr == null || attr.getOwnerElement() != this)
+ {
+ throw new DomDOMException(DOMException.NOT_FOUND_ERR);
+ }
+ if (isId)
+ {
+ if (userIdAttrs == null)
+ {
+ userIdAttrs = new HashSet();
+ }
+ userIdAttrs.add(attr);
+ }
+ else if (userIdAttrs != null)
+ {
+ userIdAttrs.remove(attr);
+ if (userIdAttrs.isEmpty())
+ {
+ userIdAttrs = null;
+ }
+ }
+ }
+
+ public void setIdAttributeNS(String namespaceURI, String localName,
+ boolean isId)
+ {
+ NamedNodeMap attrs = getAttributes();
+ Attr attr = (Attr) attrs.getNamedItemNS(namespaceURI, localName);
+ setIdAttributeNode(attr, isId);
+ }
+
+ public boolean isEqualNode(Node arg)
+ {
+ if (!super.isEqualNode(arg))
+ return false;
+ getAttributes();
+ NamedNodeMap argAttrs = arg.getAttributes();
+ int len = argAttrs.getLength();
+ if (argAttrs == null || (len != attributes.length))
+ return false;
+ for (int i = 0; i < len; i++)
+ {
+ Node argCtx = argAttrs.item(i);
+ // Don't compare namespace nodes
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
+ .equals(argCtx.getNamespaceURI()))
+ continue;
+ // Find corresponding attribute node
+ DomNode ctx = attributes.first;
+ for (; ctx != null; ctx = ctx.next)
+ {
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
+ .equals(ctx.getNamespaceURI()))
+ continue;
+ if (!ctx.isEqualNode(argCtx))
+ continue;
+ break;
+ }
+ if (ctx == null)
+ return false; // not found
+ }
+ return true;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomEntity.java b/libjava/classpath/gnu/xml/dom/DomEntity.java
new file mode 100644
index 000000000..be44d7359
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomEntity.java
@@ -0,0 +1,146 @@
+/* DomEntity.java --
+ Copyright (C) 1999,2000,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.Entity;
+
+/**
+ * <p> "Entity" implementation. This is a non-core DOM class, supporting the
+ * "XML" feature. There are two types of entities, neither of which works
+ * particularly well in this API:</p><dl>
+ *
+ * <dt><em>Unparsed Entities</em></dt>
+ * <dd>Since ENTITY/ENTITIES attributes, the only legal use of unparsed
+ * entities in XML, can't be detected with DOM, there isn't much point in
+ * trying to use unparsed entities in DOM applications. (XML Linking is
+ * working to provide a better version of this functionality.) </dd>
+ *
+ * <dt><em>Parsed Entities</em></dt>
+ * <dd> While the DOM specification permits nodes for parsed entities
+ * to have a readonly set of children, this is not required and there
+ * is no portable way to provide such children. <em>This implementation
+ * currently does not permit children to be added to Entities.</em>
+ * There are related issues with the use of EntityReference nodes. </dd>
+ *
+ * </dl>
+ *
+ * <p> In short, <em>avoid using this DOM functionality</em>.
+ *
+ * @see DomDoctype
+ * @see DomEntityReference
+ * @see DomNotation
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomEntity
+ extends DomExtern
+ implements Entity
+{
+
+ private String notation;
+
+ /**
+ * Constructs an Entity node associated with the specified document,
+ * with the specified descriptive data.
+ *
+ * <p>This constructor should only be invoked by a DomDoctype as part
+ * of its declareEntity functionality, or through a subclass which is
+ * similarly used in a "Sub-DOM" style layer.
+ *
+ * @param owner The document with which this entity is associated
+ * @param name Name of this entity
+ * @param publicId If non-null, provides the entity's PUBLIC identifier
+ * @param systemId Provides the entity's SYSTEM identifier (URI)
+ * @param notation If non-null, provides the unparsed entity's notation.
+ */
+ protected DomEntity(DomDocument owner,
+ String name,
+ String publicId,
+ String systemId,
+ String notation)
+ {
+ super(ENTITY_NODE, owner, name, publicId, systemId);
+ this.notation = notation;
+
+ // NOTE: if notation == null, this is a parsed entity
+ // which could reasonably be given child nodes ...
+ makeReadonly();
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the NOTATION identifier associated with this entity, if any.
+ */
+ final public String getNotationName()
+ {
+ return notation;
+ }
+
+ // DOM Level 3 methods
+
+ public String getInputEncoding()
+ {
+ // TODO
+ return null;
+ }
+
+ public String getXmlEncoding()
+ {
+ // TODO
+ return null;
+ }
+
+ public String getXmlVersion()
+ {
+ // TODO
+ return null;
+ }
+
+ /**
+ * The base URI of an external entity is its system ID.
+ * The base URI of an internal entity is the parent document's base URI.
+ * @since DOM Level 3 Core
+ */
+ public String getBaseURI()
+ {
+ String systemId = getSystemId();
+ return (systemId == null) ? owner.getBaseURI() : systemId;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomEntityReference.java b/libjava/classpath/gnu/xml/dom/DomEntityReference.java
new file mode 100644
index 000000000..1fa2ea628
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomEntityReference.java
@@ -0,0 +1,130 @@
+/* DomEntityReference.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.Entity;
+import org.w3c.dom.EntityReference;
+
+/**
+ * <p> "EntityReference" implementation (reference to parsed entity).
+ * This is a non-core DOM class, supporting the "XML" feature.
+ * It does not represent builtin entities (such as "&amp;amp;")
+ * or character references, which are always directly expanded in
+ * DOM trees.</p>
+ *
+ * <p> Note that while the DOM specification permits these nodes to have
+ * a readonly set of children, this is not required. Similarly, it does
+ * not require a DOM to couple EntityReference nodes with any Entity nodes
+ * that have the same entity name (and equivalent children). It also
+ * effectively guarantees that references created directly or indirectly
+ * through the <em>Document.ImportNode</em> method will not have children.
+ * The level of functionality you may get is extremely variable.
+ *
+ * <p> Also significant is that even at their most functional level, the fact
+ * that EntityReference children must be readonly has caused significant
+ * problems when modifying work products held in DOM trees. Other problems
+ * include issues related to undeclared namespace prefixes (and references
+ * to the current default namespace) that may be found in the text of such
+ * parsed entities nodes. These must be contextually bound as part of DOM
+ * tree construction. When such nodes are moved, the namespace associated
+ * with a given prefix (or default) may change to be in conflict with the
+ * namespace bound to the node at creation time.
+ *
+ * <p> In short, <em>avoid using this DOM functionality</em>.
+ *
+ * @see DomDoctype
+ * @see DomEntity
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomEntityReference
+ extends DomNode
+ implements EntityReference
+{
+
+ private String name;
+
+ /**
+ * Constructs an EntityReference node associated with the specified
+ * document. The creator should populate this with whatever contents
+ * are appropriate, and then mark it as readonly.
+ *
+ * <p>This constructor should only be invoked by a Document as part of
+ * its createEntityReference functionality, or through a subclass which
+ * is similarly used in a "Sub-DOM" style layer.
+ *
+ * @see DomNode#makeReadonly
+ */
+ protected DomEntityReference(DomDocument owner, String name)
+ {
+ super(ENTITY_REFERENCE_NODE, owner);
+ this.name = name;
+ }
+
+ /**
+ * Returns the name of the referenced entity.
+ * @since DOM Level 1 Core
+ */
+ public final String getNodeName()
+ {
+ return name;
+ }
+
+ /**
+ * The base URI of an entity reference is the base URI where the entity
+ * declaration occurs.
+ * @since DOM Level 3 Core
+ */
+ public final String getBaseURI()
+ {
+ DocumentType doctype = owner.getDoctype();
+ if (doctype == null)
+ {
+ return null;
+ }
+ Entity entity = (Entity) doctype.getEntities().getNamedItem(name);
+ if (entity == null)
+ {
+ return null;
+ }
+ return entity.getBaseURI();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomEvent.java b/libjava/classpath/gnu/xml/dom/DomEvent.java
new file mode 100644
index 000000000..3e9a6550a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomEvent.java
@@ -0,0 +1,351 @@
+/* DomEvent.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import gnu.java.lang.CPStringBuilder;
+
+import org.w3c.dom.Node;
+
+import org.w3c.dom.events.Event;
+import org.w3c.dom.events.EventTarget;
+import org.w3c.dom.events.MutationEvent;
+import org.w3c.dom.events.UIEvent;
+
+import org.w3c.dom.views.AbstractView; // used by UIEvent
+
+/**
+ * "Event" implementation. Events are
+ * created (through DocumentEvent interface methods on the document object),
+ * and are sent to any target node in the document.
+ *
+ * <p> Applications may define application specific event subclasses, but
+ * should otherwise use the <em>DocumentTraversal</em> interface to acquire
+ * event objects.
+ *
+ * @author David Brownell
+ */
+public class DomEvent
+ implements Event
+{
+
+ String type; // init
+ EventTarget target;
+ EventTarget currentNode;
+ short eventPhase;
+ boolean bubbles; // init
+ boolean cancelable; // init
+ long timeStamp; // ?
+
+ /** Returns the event's type (name) as initialized */
+ public final String getType()
+ {
+ return type;
+ }
+
+ /**
+ * Returns event's target; delivery of an event is initiated
+ * by a <em>target.dispatchEvent(event)</em> invocation.
+ */
+ public final EventTarget getTarget()
+ {
+ return target;
+ }
+
+ /**
+ * Returns the target to which events are currently being
+ * delivered. When capturing or bubbling, this will not
+ * be what <em>getTarget</em> returns.
+ */
+ public final EventTarget getCurrentTarget()
+ {
+ return currentNode;
+ }
+
+ /**
+ * Returns CAPTURING_PHASE, AT_TARGET, or BUBBLING;
+ * only meaningful within EventListener.handleEvent
+ */
+ public final short getEventPhase()
+ {
+ return eventPhase;
+ }
+
+ /**
+ * Returns true if the news of the event bubbles to tree tops
+ * (as specified during initialization).
+ */
+ public final boolean getBubbles()
+ {
+ return bubbles;
+ }
+
+ /**
+ * Returns true if the default handling may be canceled
+ * (as specified during initialization).
+ */
+ public final boolean getCancelable()
+ {
+ return cancelable;
+ }
+
+ /**
+ * Returns the event's timestamp.
+ */
+ public final long getTimeStamp()
+ {
+ return timeStamp;
+ }
+
+ boolean stop;
+ boolean doDefault;
+
+ /**
+ * Requests the event no longer be captured or bubbled; only
+ * listeners on the event target will see the event, if they
+ * haven't yet been notified.
+ *
+ * <p> <em> Avoid using this </em> except for application-specific
+ * events, for which you the protocol explicitly "blesses" the use
+ * of this with some event types. Otherwise, you are likely to break
+ * algorithms which depend on event notification either directly or
+ * through bubbling or capturing. </p>
+ *
+ * <p> Note that this method is not final, specifically to enable
+ * enforcing of policies about events always propagating. </p>
+ */
+ public void stopPropagation()
+ {
+ stop = true;
+ }
+
+ /**
+ * Requests that whoever dispatched the event not perform their
+ * default processing when event delivery completes. Initializes
+ * event timestamp.
+ */
+ public final void preventDefault()
+ {
+ doDefault = false;
+ }
+
+ /** Initializes basic event state. */
+ public void initEvent(String typeArg,
+ boolean canBubbleArg,
+ boolean cancelableArg)
+ {
+ eventPhase = 0;
+ type = typeArg;
+ bubbles = canBubbleArg;
+ cancelable = cancelableArg;
+ timeStamp = System.currentTimeMillis();
+ }
+
+ /** Constructs, but does not initialize, an event. */
+ public DomEvent(String type)
+ {
+ this.type = type;
+ }
+
+ /**
+ * Returns a basic printable description of the event's type,
+ * state, and delivery conditions
+ */
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("[Event ");
+ buf.append(type);
+ switch (eventPhase)
+ {
+ case CAPTURING_PHASE:
+ buf.append(", CAPTURING");
+ break;
+ case AT_TARGET:
+ buf.append(", AT TARGET");
+ break;
+ case BUBBLING_PHASE:
+ buf.append(", BUBBLING");
+ break;
+ default:
+ buf.append(", (inactive)");
+ break;
+ }
+ if (bubbles && eventPhase != BUBBLING_PHASE)
+ {
+ buf.append(", bubbles");
+ }
+ if (cancelable)
+ {
+ buf.append(", can cancel");
+ }
+ // were we to provide subclass info, this's where it'd live
+ buf.append("]");
+ return buf.toString();
+ }
+
+ /**
+ * "MutationEvent" implementation.
+ */
+ public static final class DomMutationEvent
+ extends DomEvent
+ implements MutationEvent
+ {
+
+ // package private
+ Node relatedNode; // init
+
+ private String prevValue; // init
+ private String newValue; // init
+
+ private String attrName; // init
+ private short attrChange; // init
+
+ /** Returns any "related" node provided by this type of event */
+ public final Node getRelatedNode()
+ {
+ return relatedNode;
+ }
+
+ /** Returns any "previous value" provided by this type of event */
+ public final String getPrevValue()
+ {
+ return prevValue;
+ }
+
+ /** Returns any "new value" provided by this type of event */
+ public final String getNewValue()
+ {
+ return newValue;
+ }
+
+ /** For attribute change events, returns the attribute's name */
+ public final String getAttrName()
+ {
+ return attrName;
+ }
+
+ /** For attribute change events, returns how the attribuet changed */
+ public final short getAttrChange()
+ {
+ return attrChange;
+ }
+
+ /** Initializes a mutation event */
+ public final void initMutationEvent(String typeArg,
+ boolean canBubbleArg,
+ boolean cancelableArg,
+ Node relatedNodeArg,
+ String prevValueArg,
+ String newValueArg,
+ String attrNameArg,
+ short attrChangeArg)
+ {
+ // super.initEvent is inlined here for speed
+ // (mutation events are issued on all DOM changes)
+ eventPhase = 0;
+ type = typeArg;
+ bubbles = canBubbleArg;
+ cancelable = cancelableArg;
+ timeStamp = System.currentTimeMillis();
+
+ relatedNode = relatedNodeArg;
+ prevValue = prevValueArg;
+ newValue = newValueArg;
+ attrName = attrNameArg;
+ attrChange = attrChangeArg;
+ }
+
+ // clear everything that should be GC-able
+ void clear()
+ {
+ type = null;
+ target = null;
+ relatedNode = null;
+ currentNode = null;
+ prevValue = newValue = attrName = null;
+ }
+
+ /** Constructs an uninitialized mutation event. */
+ public DomMutationEvent(String type)
+ {
+ super(type);
+ }
+
+ }
+
+ /**
+ * "UIEvent" implementation.
+ */
+ public static class DomUIEvent
+ extends DomEvent
+ implements UIEvent
+ {
+
+ private AbstractView view; // init
+ private int detail; // init
+
+ /** Constructs an uninitialized User Interface (UI) event */
+ public DomUIEvent (String type) { super (type); }
+
+ public final AbstractView getView () { return view; }
+ public final int getDetail () { return detail; }
+
+ /** Initializes a UI event */
+ public final void initUIEvent(String typeArg,
+ boolean canBubbleArg,
+ boolean cancelableArg,
+ AbstractView viewArg,
+ int detailArg)
+ {
+ super.initEvent(typeArg, canBubbleArg, cancelableArg);
+ view = viewArg;
+ detail = detailArg;
+ }
+
+ }
+
+ /*
+
+ static final class DomMouseEvent extends DomUIEvent
+ implements MouseEvent
+ {
+ // another half dozen state variables/accessors
+ }
+
+ */
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomExtern.java b/libjava/classpath/gnu/xml/dom/DomExtern.java
new file mode 100644
index 000000000..7f2e55afc
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomExtern.java
@@ -0,0 +1,116 @@
+/* DomExtern.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+/**
+ * <p> Abstract implemention of nodes describing external DTD-related
+ * objects. This facilitates reusing code for Entity, Notation, and
+ * DocumentType (really, external subset) nodes. Such support is not
+ * part of the core DOM; it's for the "XML" feature. </p>
+ *
+ * <p> Note that you are strongly advised to avoid using the DOM
+ * features that take advantage of this class, since (as of L2) none
+ * of them is defined fully enough to permit full use of the
+ * XML feature they partially expose. </p>
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public abstract class DomExtern
+ extends DomNode
+{
+
+ private final String name;
+ private final String publicId;
+ private final String systemId;
+
+ /**
+ * Constructs a node associated with the specified document,
+ * with the specified descriptive data.
+ *
+ * @param owner The document with which this object is associated
+ * @param name Name of this object
+ * @param publicId If non-null, provides the entity's PUBLIC identifier
+ * @param systemId If non-null, provides the entity's SYSTEM identifier
+ */
+ // package private
+ DomExtern(short nodeType,
+ DomDocument owner,
+ String name,
+ String publicId,
+ String systemId)
+ {
+ super(nodeType, owner);
+ this.name = name;
+ this.publicId = publicId;
+ this.systemId = systemId;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the SYSTEM identifier associated with this object, if any.
+ */
+ public final String getSystemId()
+ {
+ return systemId;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the PUBLIC identifier associated with this object, if any.
+ */
+ public final String getPublicId()
+ {
+ return publicId;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the object's name.
+ */
+ public final String getNodeName()
+ {
+ return name;
+ }
+
+ public final String getLocalName()
+ {
+ return name;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomImpl.java b/libjava/classpath/gnu/xml/dom/DomImpl.java
new file mode 100644
index 000000000..76a63caf6
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomImpl.java
@@ -0,0 +1,277 @@
+/* DomImpl.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.Element;
+import org.w3c.dom.ls.DOMImplementationLS;
+import org.w3c.dom.ls.LSInput;
+import org.w3c.dom.ls.LSOutput;
+import org.w3c.dom.ls.LSParser;
+import org.w3c.dom.ls.LSSerializer;
+import gnu.xml.dom.html2.DomHTMLImpl;
+import gnu.xml.dom.ls.DomLSInput;
+import gnu.xml.dom.ls.DomLSOutput;
+import gnu.xml.dom.ls.DomLSParser;
+import gnu.xml.dom.ls.DomLSSerializer;
+
+/**
+ * <p> "DOMImplementation" implementation. </p>
+ *
+ * <p> At this writing, the following features are supported:
+ * "XML" (L1, L2, L3),
+ * "Events" (L2), "MutationEvents" (L2), "USER-Events" (a conformant extension),
+ * "HTMLEvents" (L2), "UIEvents" (L2), "Traversal" (L2), "XPath" (L3),
+ * "LS" (L3) "LS-Async" (L3).
+ * It is possible to compile the package so it doesn't support some of these
+ * features (notably, Traversal).
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomImpl
+ implements DOMImplementation, DOMImplementationLS
+{
+
+ /**
+ * Constructs a DOMImplementation object which supports
+ * "XML" and other DOM Level 2 features.
+ */
+ public DomImpl()
+ {
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns true if the specified feature and version are
+ * supported. Note that the case of the feature name is ignored.
+ */
+ public boolean hasFeature(String name, String version)
+ {
+ if (name.length() == 0)
+ {
+ return false;
+ }
+ name = name.toLowerCase();
+ if (name.charAt(0) == '+')
+ {
+ name = name.substring(1);
+ }
+
+ if ("xml".equals(name) || "core".equals(name))
+ {
+ return (version == null ||
+ "".equals(version) ||
+ "1.0".equals(version) ||
+ "2.0".equals(version) ||
+ "3.0".equals(version));
+
+ }
+ else if ("ls".equals(name) || "ls-async".equals(name))
+ {
+ return (version == null ||
+ "".equals(version) ||
+ "3.0".equals(version));
+ }
+ else if ("events".equals(name)
+ || "mutationevents".equals(name)
+ || "uievents".equals(name)
+ // || "mouseevents".equals(name)
+ || "htmlevents".equals(name))
+ {
+ return (version == null ||
+ "".equals(version) ||
+ "2.0".equals(version));
+
+ // Extension: "USER-" prefix event types can
+ // be created and passed through the DOM.
+
+ }
+ else if ("user-events".equals(name))
+ {
+ return (version == null ||
+ "".equals(version) ||
+ "0.1".equals(version));
+
+ // NOTE: "hasFeature" for events is here interpreted to
+ // mean the DOM can manufacture those sorts of events,
+ // since actually choosing to report the events is more
+ // often part of the environment or application. It's
+ // only really an issue for mutation events.
+
+ }
+ else if (DomNode.reportMutations
+ && "traversal".equals(name))
+ {
+ return (version == null ||
+ "".equals(version) ||
+ "2.0".equals(version));
+ }
+ else if ("xpath".equals(name))
+ {
+ return (version == null ||
+ "".equals(version) ||
+ "3.0".equals(version));
+ }
+ else if ("html".equals(name) || "xhtml".equals(name))
+ {
+ return (version == null ||
+ "".equals(version) ||
+ "2.0".equals(version));
+ }
+
+ // views
+ // stylesheets
+ // css, css2
+ // range
+
+ return false;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Creates and returns a DocumentType, associated with this
+ * implementation. This DocumentType can have no associated
+ * objects(notations, entities) until the DocumentType is
+ * first associated with a document.
+ *
+ * <p> Note that there is no implication that this DTD will
+ * be parsed by the DOM, or ever have contents. Moreover, the
+ * DocumentType created here can only be added to a document by
+ * the createDocument method(below). <em>That means that the only
+ * portable way to create a Document object is to start parsing,
+ * queue comment and processing instruction (PI) nodes, and then only
+ * create a DOM Document after <b>(a)</b> it's known if a DocumentType
+ * object is needed, and <b>(b) the name and namespace of the root
+ * element is known. Queued comment and PI nodes would then be
+ * inserted appropriately in the document prologue, both before and
+ * after the DTD node, and additional attributes assigned to the
+ * root element.</em>
+ *(One hopes that the final DOM REC fixes this serious botch.)
+ */
+ public DocumentType createDocumentType(String rootName,
+ String publicId,
+ String systemId)
+ // CR2 deleted internal subset, ensuring DocumentType
+ // is 100% useless instead of just 90% so.
+ {
+ DomDocument.checkNCName(rootName, false);
+ return new DomDoctype(this, rootName, publicId, systemId, null);
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Creates and returns a Document, populated only with a root element and
+ * optionally a document type(if that was provided).
+ */
+ public Document createDocument(String namespaceURI,
+ String rootName,
+ DocumentType doctype)
+ {
+ Document doc = createDocument();
+ Element root = null;
+
+ if (rootName != null)
+ {
+ root = doc.createElementNS(namespaceURI, rootName);
+ if (rootName.startsWith("xmlns:"))
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "xmlns is reserved", null, 0);
+ }
+ }
+ // Bleech -- L2 seemingly _requires_ omission of xmlns attributes.
+ if (doctype != null)
+ {
+ doc.appendChild(doctype); // handles WRONG_DOCUMENT error
+ }
+ if (root != null)
+ {
+ doc.appendChild(root);
+ }
+ return doc;
+ }
+
+ protected Document createDocument()
+ {
+ return new DomDocument(this);
+ }
+
+ // DOM Level 3
+
+ public Object getFeature(String feature, String version)
+ {
+ if (hasFeature(feature, version))
+ {
+ if ("html".equalsIgnoreCase(feature) ||
+ "xhtml".equalsIgnoreCase(feature))
+ {
+ return new DomHTMLImpl();
+ }
+ return this;
+ }
+ return null;
+ }
+
+ // -- DOMImplementationLS --
+
+ public LSParser createLSParser(short mode, String schemaType)
+ throws DOMException
+ {
+ return new DomLSParser(mode, schemaType);
+ }
+
+ public LSSerializer createLSSerializer()
+ {
+ return new DomLSSerializer();
+ }
+
+ public LSInput createLSInput()
+ {
+ return new DomLSInput();
+ }
+
+ public LSOutput createLSOutput()
+ {
+ return new DomLSOutput();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomIterator.java b/libjava/classpath/gnu/xml/dom/DomIterator.java
new file mode 100644
index 000000000..d5f305cd8
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomIterator.java
@@ -0,0 +1,380 @@
+/* DomIterator.java --
+ Copyright (C) 1999, 2000, 2001, 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+import org.w3c.dom.events.Event;
+import org.w3c.dom.events.EventListener;
+import org.w3c.dom.events.EventTarget;
+import org.w3c.dom.events.MutationEvent;
+import org.w3c.dom.traversal.NodeFilter;
+import org.w3c.dom.traversal.NodeIterator;
+
+/**
+ * <p> "NodeIterator" implementation, usable with any L2 DOM which
+ * supports MutationEvents. </p>
+ *
+ * @author David Brownell
+ */
+public final class DomIterator
+ implements NodeIterator, EventListener
+{
+
+ private Node reference;
+ private boolean right;
+ private boolean done;
+
+ private final Node root;
+ private final int whatToShow;
+ private final NodeFilter filter;
+ private final boolean expandEntityReferences;
+
+ /**
+ * Constructs and initializes an iterator.
+ */
+ protected DomIterator(Node root,
+ int whatToShow,
+ NodeFilter filter,
+ boolean entityReferenceExpansion)
+ {
+ if (!root.isSupported("MutationEvents", "2.0"))
+ {
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR,
+ "Iterator needs mutation events", root, 0);
+ }
+
+ this.root = root;
+ this.whatToShow = whatToShow;
+ this.filter = filter;
+ this.expandEntityReferences = entityReferenceExpansion;
+
+ // start condition: going right, seen nothing yet.
+ reference = null;
+ right = true;
+
+ EventTarget target = (EventTarget) root;
+ target.addEventListener("DOMNodeRemoved", this, false);
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Flags the iterator as done, unregistering its event listener so
+ * that the iterator can be garbage collected without relying on weak
+ * references (a "Java 2" feature) in the event subsystem.
+ */
+ public void detach()
+ {
+ EventTarget target = (EventTarget) root;
+ target.removeEventListener("DOMNodeRemoved", this, false);
+ done = true;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns the flag controlling whether iteration descends
+ * through entity references.
+ */
+ public boolean getExpandEntityReferences()
+ {
+ return expandEntityReferences;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns the filter provided during construction.
+ */
+ public NodeFilter getFilter()
+ {
+ return filter;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns the root of the tree this is iterating through.
+ */
+ public Node getRoot()
+ {
+ return root;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns the mask of flags provided during construction.
+ */
+ public int getWhatToShow()
+ {
+ return whatToShow;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns the next node in a forward iteration, masked and filtered.
+ * Note that the node may be read-only due to entity expansions.
+ * A null return indicates the iteration is complete, but may still
+ * be processed backwards.
+ */
+ public Node nextNode()
+ {
+ if (done)
+ {
+ throw new DomDOMException(DOMException.INVALID_STATE_ERR);
+ }
+ right = true;
+ return walk(true);
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns the next node in a backward iteration, masked and filtered.
+ * Note that the node may be read-only due to entity expansions.
+ * A null return indicates the iteration is complete, but may still
+ * be processed forwards.
+ */
+ public Node previousNode()
+ {
+ if (done)
+ {
+ throw new DomDOMException(DOMException.INVALID_STATE_ERR);
+ }
+ Node previous = reference;
+ right = false;
+ walk(false);
+ return previous;
+ }
+
+ private boolean shouldShow(Node node)
+ // raises Runtime exceptions indirectly, via acceptNode()
+ {
+ if ((whatToShow & (1 << (node.getNodeType() - 1))) == 0)
+ {
+ return false;
+ }
+ if (filter == null)
+ {
+ return true;
+ }
+ return filter.acceptNode(node) == NodeFilter.FILTER_ACCEPT;
+ }
+
+ //
+ // scenario: root = 1, sequence = 1 2 ... 3 4
+ // forward walk: 1 2 ... 3 4 null
+ // then backward: 4 3 ... 2 1 null
+ //
+ // At the leftmost end, "previous" == null
+ // At the rightmost end, "previous" == 4
+ //
+ // The current draft spec really seem to make no sense re the
+ // role of the reference node, so what it says is ignored here.
+ //
+ private Node walk(boolean forward)
+ {
+ Node here = reference;
+
+ while ((here = successor(here, forward)) != null
+ && !shouldShow(here))
+ {
+ continue;
+ }
+ if (here != null || !forward)
+ {
+ reference = here;
+ }
+ return here;
+ }
+
+ private boolean isLeaf(Node here)
+ {
+ boolean leaf = !here.hasChildNodes();
+ if (!leaf && !expandEntityReferences)
+ {
+ leaf = (here.getNodeType() == Node.ENTITY_REFERENCE_NODE);
+ }
+ return leaf;
+ }
+
+ //
+ // Returns the immediate successor in a forward (or backward)
+ // document order walk, sans filtering ... except that it knows
+ // how to stop, returning null when done. This is a depth first
+ // preorder traversal when run in the forward direction.
+ //
+ private Node successor(Node here, boolean forward)
+ {
+ Node next;
+
+ // the "leftmost" end is funky
+ if (here == null)
+ {
+ return forward ? root : null;
+ }
+
+ //
+ // Forward, this is preorder: children before siblings.
+ // Backward, it's postorder: we saw the children already.
+ //
+ if (forward && !isLeaf(here))
+ {
+ return here.getFirstChild();
+ }
+
+ // There's no way up or sideways from the root, so if we
+ // couldn't move down to a child, there's nowhere to go.
+ //
+ if (here == root)
+ return null;
+
+ //
+ // Siblings ... if forward, we visit them, if backwards
+ // we visit their children first.
+ //
+ if (forward)
+ {
+ if ((next = here.getNextSibling()) != null)
+ {
+ return next;
+ }
+ }
+ else if ((next = here.getPreviousSibling()) != null)
+ {
+ if (isLeaf(next))
+ {
+ return next;
+ }
+ next = next.getLastChild();
+ while (!isLeaf(next))
+ {
+ next = next.getLastChild();
+ }
+ return next;
+ }
+
+ //
+ // We can't go down or lateral -- it's up, then. The logic is
+ // the converse of what's above: backwards is easy (the parent
+ // is next), forwards isn't.
+ //
+ next = here.getParentNode();
+ if (!forward)
+ {
+ return next;
+ }
+
+ Node temp = null;
+ while (next != null
+ && next != root
+ && (temp = next.getNextSibling()) == null)
+ {
+ next = next.getParentNode();
+ }
+
+ // If we have exceeded the root node then stop traversing.
+ if (next == root.getParentNode())
+ {
+ return null;
+ }
+ return temp;
+ }
+
+ /**
+ * Not for public use. This lets the iterator know when its
+ * reference node will be removed from the tree, so that a new
+ * one may be selected.
+ *
+ * <p> This version works by watching removal events as they
+ * bubble up. So, don't prevent them from bubbling.
+ */
+ public void handleEvent(Event e)
+ {
+ MutationEvent event;
+ Node ancestor, removed;
+
+ if (reference == null
+ || !"DOMNodeRemoved".equals(e.getType())
+ || e.getEventPhase() != Event.BUBBLING_PHASE)
+ {
+ return;
+ }
+
+ event = (MutationEvent) e;
+ removed = (Node) event.getTarget();
+
+ // See if the removal will cause trouble for this iterator
+ // by being the reference node or an ancestor of it.
+ for (ancestor = reference;
+ ancestor != null && ancestor != root;
+ ancestor = ancestor.getParentNode())
+ {
+ if (ancestor == removed)
+ {
+ break;
+ }
+ }
+ if (ancestor != removed)
+ {
+ return;
+ }
+
+ // OK, it'll cause trouble. We want to make the "next"
+ // node in our current traversal direction seem right.
+ // So we pick the nearest node that's not getting removed,
+ // but go in the _opposite_ direction from our current
+ // traversal ... so the "next" doesn't skip anything.
+ Node candidate;
+
+search:
+ while ((candidate = walk(!right)) != null)
+ {
+ for (ancestor = candidate;
+ ancestor != null && ancestor != root;
+ ancestor = ancestor.getParentNode())
+ {
+ if (ancestor == removed)
+ {
+ continue search;
+ }
+ }
+ return;
+ }
+
+ // The current DOM WD talks about a special case here;
+ // I've not yet seen it.
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomNSResolverContext.java b/libjava/classpath/gnu/xml/dom/DomNSResolverContext.java
new file mode 100644
index 000000000..3dd957560
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomNSResolverContext.java
@@ -0,0 +1,90 @@
+/* DomNSResolverContext.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import java.util.Iterator;
+import javax.xml.namespace.NamespaceContext;
+import org.w3c.dom.xpath.XPathNSResolver;
+
+/**
+ * Namespace content wrapper for an XPathNSResolver.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class DomNSResolverContext
+ implements NamespaceContext, Iterator
+{
+
+ final XPathNSResolver resolver;
+
+ DomNSResolverContext(XPathNSResolver resolver)
+ {
+ this.resolver = resolver;
+ }
+
+ public String getNamespaceURI(String prefix)
+ {
+ return resolver.lookupNamespaceURI(prefix);
+ }
+
+ public String getPrefix(String namespaceURI)
+ {
+ return null;
+ }
+
+ public Iterator getPrefixes(String namespaceURI)
+ {
+ return this;
+ }
+
+ public boolean hasNext()
+ {
+ return false;
+ }
+
+ public Object next()
+ {
+ return null;
+ }
+
+ public void remove()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomNamedNodeMap.java b/libjava/classpath/gnu/xml/dom/DomNamedNodeMap.java
new file mode 100644
index 000000000..7a767355a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomNamedNodeMap.java
@@ -0,0 +1,421 @@
+/* DomNamedNodeMap.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+/**
+ * <p> "NamedNodeMap" implementation. </p>
+ * Used mostly to hold element attributes, but sometimes also
+ * to list notations or entities.
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomNamedNodeMap
+ implements NamedNodeMap
+{
+
+ final DomNode owner;
+ final short type;
+
+ DomNode first;
+ int length;
+ boolean readonly;
+
+ // package private
+ DomNamedNodeMap(DomNode owner, short type)
+ {
+ this.owner = owner;
+ this.type = type;
+ }
+
+ /**
+ * Exposes the internal "readonly" flag. In DOM, all NamedNodeMap
+ * objects found in a DocumentType object are read-only (after
+ * they are fully constructed), and those holding attributes of
+ * a readonly element will also be readonly.
+ */
+ public final boolean isReadonly()
+ {
+ return readonly;
+ }
+
+ /**
+ * Sets the internal "readonly" flag so the node and its
+ * children can't be changed.
+ */
+ public void makeReadonly()
+ {
+ readonly = true;
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ ctx.makeReadonly();
+ }
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the named item from the map, or null; names are just
+ * the nodeName property.
+ */
+ public Node getNamedItem(String name)
+ {
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ if (ctx.getNodeName().equals(name))
+ {
+ return ctx;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns the named item from the map, or null; names are the
+ * localName and namespaceURI properties, ignoring any prefix.
+ */
+ public Node getNamedItemNS(String namespaceURI, String localName)
+ {
+ if ("".equals(namespaceURI))
+ {
+ namespaceURI = null;
+ }
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ String name = ctx.getLocalName();
+ if ((localName == null && name == null) ||
+ (localName != null && localName.equals(name)))
+ {
+ String uri = ctx.getNamespaceURI();
+ if ("".equals(uri))
+ {
+ uri = null;
+ }
+ if ((namespaceURI == null && uri == null) ||
+ (namespaceURI != null && namespaceURI.equals(uri)))
+ {
+ return ctx;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Stores the named item into the map, optionally overwriting
+ * any existing node with that name. The name used is just
+ * the nodeName attribute.
+ */
+ public Node setNamedItem(Node arg)
+ {
+ return setNamedItem(arg, false, false);
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Stores the named item into the map, optionally overwriting
+ * any existing node with that fully qualified name. The name
+ * used incorporates the localName and namespaceURI properties,
+ * and ignores any prefix.
+ */
+ public Node setNamedItemNS(Node arg)
+ {
+ return setNamedItem(arg, true, false);
+ }
+
+ Node setNamedItem(Node arg, boolean ns, boolean cloning)
+ {
+ if (readonly)
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+
+ DomNode node = (DomNode) arg;
+ if (!cloning && node.owner != owner.owner)
+ {
+ throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR);
+ }
+ if (node.nodeType != type)
+ {
+ throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR);
+ }
+ if (node.nodeType == Node.ATTRIBUTE_NODE)
+ {
+ DomNode element = node.parent;
+ if (element != null && element != owner)
+ {
+ throw new DomDOMException(DOMException.INUSE_ATTRIBUTE_ERR);
+ }
+ node.parent = owner;
+ node.depth = owner.depth + 1;
+ }
+
+ String nodeName = node.getNodeName();
+ String localName = ns ? node.getLocalName() : null;
+ String namespaceURI = ns ? node.getNamespaceURI() : null;
+ if ("".equals(namespaceURI))
+ {
+ namespaceURI = null;
+ }
+
+ // maybe attribute ADDITION events (?)
+ DomNode last = null;
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ boolean test = false;
+ if (ns)
+ {
+ String tln = ctx.getLocalName();
+ if (tln == null)
+ {
+ tln = ctx.getNodeName();
+ }
+ if (tln.equals(localName))
+ {
+ String tu = ctx.getNamespaceURI();
+ if ((tu == null && namespaceURI == null) ||
+ (tu != null && tu.equals(namespaceURI)))
+ {
+ test = true;
+ }
+ }
+ }
+ else
+ {
+ test = ctx.getNodeName().equals(nodeName);
+ }
+ if (test)
+ {
+ // replace
+ node.previous = ctx.previous;
+ node.next = ctx.next;
+ if (ctx.previous != null)
+ {
+ ctx.previous.next = node;
+ }
+ if (ctx.next != null)
+ {
+ ctx.next.previous = node;
+ }
+ if (first == ctx)
+ {
+ first = node;
+ }
+ reparent(node, nodeName, ctx.index);
+ ctx.parent = null;
+ ctx.next = null;
+ ctx.previous = null;
+ ctx.setDepth(0);
+ ctx.index = 0;
+ return ctx;
+ }
+ last = ctx;
+ }
+ // append
+ if (last != null)
+ {
+ last.next = node;
+ node.previous = last;
+ }
+ else
+ {
+ first = node;
+ }
+ length++;
+ reparent(node, nodeName, 0);
+ return null;
+ }
+
+ void reparent(DomNode node, String nodeName, int i)
+ {
+ node.parent = owner;
+ node.setDepth(owner.depth + 1);
+ // index renumbering
+ for (DomNode ctx = node; ctx != null; ctx = ctx.next)
+ {
+ ctx.index = i++;
+ }
+ // cache xml:space
+ boolean xmlSpace = "xml:space".equals(nodeName);
+ if (xmlSpace && owner instanceof DomElement)
+ {
+ ((DomElement) owner).xmlSpace = node.getNodeValue();
+ }
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Removes the named item from the map, or reports an exception;
+ * names are just the nodeName property.
+ */
+ public Node removeNamedItem(String name)
+ {
+ return removeNamedItem(null, name, false);
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Removes the named item from the map, or reports an exception;
+ * names are the localName and namespaceURI properties.
+ */
+ public Node removeNamedItemNS(String namespaceURI, String localName)
+ {
+ return removeNamedItem(namespaceURI, localName, true);
+ }
+
+ Node removeNamedItem(String uri, String name, boolean ns)
+ {
+ if (readonly)
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+
+ // report attribute REMOVAL event?
+
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ boolean test = false;
+ String nodeName = ctx.getNodeName();
+ if (ns)
+ {
+ String tln = ctx.getLocalName();
+ if (name != null && name.equals(tln))
+ {
+ String tu = ctx.getNamespaceURI();
+ if ((tu == null && uri == null) ||
+ (tu != null && tu.equals(uri)))
+ {
+ test = true;
+ }
+ }
+ }
+ else
+ {
+ test = nodeName.equals(name);
+ }
+ if (test)
+ {
+ // uncache xml:space
+ boolean xmlSpace = "xml:space".equals(nodeName);
+ if (xmlSpace && owner instanceof DomElement)
+ {
+ ((DomElement) owner).xmlSpace = "";
+ }
+ // is this a default attribute?
+ if (ctx.nodeType == Node.ATTRIBUTE_NODE)
+ {
+ String def = getDefaultValue(ctx.getNodeName());
+ if (def != null)
+ {
+ ctx.setNodeValue(def);
+ ((DomAttr) ctx).setSpecified(false);
+ return null;
+ }
+ }
+ // remove
+ if (ctx == first)
+ {
+ first = ctx.next;
+ }
+ if (ctx.previous != null)
+ {
+ ctx.previous.next = ctx.next;
+ }
+ if (ctx.next != null)
+ {
+ ctx.next.previous = ctx.previous;
+ }
+ length--;
+ ctx.previous = null;
+ ctx.next = null;
+ ctx.parent = null;
+ ctx.setDepth(0);
+ ctx.index = 0;
+ return ctx;
+ }
+ }
+ throw new DomDOMException(DOMException.NOT_FOUND_ERR);
+ }
+
+ String getDefaultValue(String name)
+ {
+ DomDoctype doctype = (DomDoctype) owner.owner.getDoctype();
+ if (doctype == null)
+ {
+ return null;
+ }
+ DTDAttributeTypeInfo info =
+ doctype.getAttributeTypeInfo(owner.getNodeName(), name);
+ if (info == null)
+ {
+ return null;
+ }
+ return info.value;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the indexed item from the map, or null.
+ */
+ public Node item(int index)
+ {
+ DomNode ctx = first;
+ int count = 0;
+ while (ctx != null && count < index)
+ {
+ ctx = ctx.next;
+ count++;
+ }
+ return ctx;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the length of the map.
+ */
+ public int getLength()
+ {
+ return length;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomNode.java b/libjava/classpath/gnu/xml/dom/DomNode.java
new file mode 100644
index 000000000..879baaa8d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomNode.java
@@ -0,0 +1,2222 @@
+/* DomNode.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+import org.w3c.dom.UserDataHandler;
+import org.w3c.dom.events.DocumentEvent;
+import org.w3c.dom.events.Event;
+import org.w3c.dom.events.EventException;
+import org.w3c.dom.events.EventListener;
+import org.w3c.dom.events.EventTarget;
+import org.w3c.dom.events.MutationEvent;
+import org.w3c.dom.traversal.NodeFilter;
+import org.w3c.dom.traversal.NodeIterator;
+
+/**
+ * <p> "Node", "EventTarget", and "DocumentEvent" implementation.
+ * This provides most of the core DOM functionality; only more
+ * specialized features are provided by subclasses. Those subclasses may
+ * have some particular constraints they must implement, by overriding
+ * methods defined here. Such constraints are noted here in the method
+ * documentation. </p>
+ *
+ * <p> Note that you can create events with type names prefixed with "USER-",
+ * and pass them through this DOM. This lets you use the DOM event scheme
+ * for application specific purposes, although you must use a predefined event
+ * structure (such as MutationEvent) to pass data along with those events.
+ * Test for existence of this feature with the "USER-Events" DOM feature
+ * name.</p>
+ *
+ * <p> Other kinds of events you can send include the "html" events,
+ * like "load", "unload", "abort", "error", and "blur"; and the mutation
+ * events. If this DOM has been compiled with mutation event support
+ * enabled, it will send mutation events when you change parts of the
+ * tree; otherwise you may create and send such events yourself, but
+ * they won't be generated by the DOM itself. </p>
+ *
+ * <p> Note that there is a namespace-aware name comparison method,
+ * <em>nameAndTypeEquals</em>, which compares the names (and types) of
+ * two nodes in conformance with the "Namespaces in XML" specification.
+ * While mostly intended for use with elements and attributes, this should
+ * also be helpful for ProcessingInstruction nodes and some others which
+ * do not have namespace URIs.
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public abstract class DomNode
+ implements Node, NodeList, EventTarget, DocumentEvent, Cloneable, Comparable
+{
+
+ // package private
+ //final static String xmlNamespace = "http://www.w3.org/XML/1998/namespace";
+ //final static String xmlnsURI = "http://www.w3.org/2000/xmlns/";
+
+ // tunable
+ // NKIDS_* affects arrays of children (which grow)
+ // (currently) fixed size:
+ // ANCESTORS_* is for event capture/bubbling, # ancestors
+ // NOTIFICATIONS_* is for per-node event delivery, # events
+ private static final int NKIDS_DELTA = 8;
+ private static final int ANCESTORS_INIT = 20;
+ private static final int NOTIFICATIONS_INIT = 10;
+
+ // tunable: enable mutation events or not? Enabling it costs about
+ // 10-15% in DOM construction time, last time it was measured.
+
+ // package private !!!
+ static final boolean reportMutations = true;
+
+ // locking protocol changeable only within this class
+ private static final Object lockNode = new Object();
+
+ // NON-FINAL class data
+
+ // Optimize event dispatch by not allocating memory each time
+ private static boolean dispatchDataLock;
+ private static DomNode[] ancestors = new DomNode[ANCESTORS_INIT];
+ private static ListenerRecord[] notificationSet
+ = new ListenerRecord[NOTIFICATIONS_INIT];
+
+ // Ditto for the (most common) event object itself!
+ private static boolean eventDataLock;
+ private static DomEvent.DomMutationEvent mutationEvent
+ = new DomEvent.DomMutationEvent(null);
+
+ //
+ // PER-INSTANCE DATA
+ //
+
+ DomDocument owner;
+ DomNode parent; // parent node;
+ DomNode previous; // previous sibling node
+ DomNode next; // next sibling node
+ DomNode first; // first child node
+ DomNode last; // last child node
+ int index; // index of this node in its parent's children
+ int depth; // depth of the node in the document
+ int length; // number of children
+ final short nodeType;
+
+ // Bleech ... "package private" so a builder can populate entity refs.
+ // writable during construction. DOM spec is nasty.
+ boolean readonly;
+
+ // event registrations
+ private HashSet listeners;
+ private int nListeners;
+
+ // DOM Level 3 userData dictionary.
+ private HashMap userData;
+ private HashMap userDataHandlers;
+
+ //
+ // Some of the methods here are declared 'final' because
+ // knowledge about their implementation is built into this
+ // class -- for both integrity and performance.
+ //
+
+ /**
+ * Reduces space utilization for this node.
+ */
+ public void compact()
+ {
+ }
+
+ /**
+ * Constructs a node and associates it with its owner. Only
+ * Document and DocumentType nodes may be created with no owner,
+ * and DocumentType nodes get an owner as soon as they are
+ * associated with a document.
+ */
+ protected DomNode(short nodeType, DomDocument owner)
+ {
+ this.nodeType = nodeType;
+
+ if (owner == null)
+ {
+ // DOM calls never go down this path
+ if (nodeType != DOCUMENT_NODE && nodeType != DOCUMENT_TYPE_NODE)
+ {
+ throw new IllegalArgumentException ("no owner!");
+ }
+ }
+ this.owner = owner;
+ this.listeners = new HashSet();
+ }
+
+
+ /**
+ * <b>DOM L1</b>
+ * Returns null; Element subclasses must override this method.
+ */
+ public NamedNodeMap getAttributes()
+ {
+ return null;
+ }
+
+ /**
+ * <b>DOM L2></b>
+ * Returns true iff this is an element node with attributes.
+ */
+ public boolean hasAttributes()
+ {
+ return false;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns a list, possibly empty, of the children of this node.
+ * In this implementation, to conserve memory, nodes are the same
+ * as their list of children. This can have ramifications for
+ * subclasses, which may need to provide their own getLength method
+ * for reasons unrelated to the NodeList method of the same name.
+ */
+ public NodeList getChildNodes()
+ {
+ return this;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the first child of this node, or null if there are none.
+ */
+ public Node getFirstChild()
+ {
+ return first;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the last child of this node, or null if there are none.
+ */
+ public Node getLastChild()
+ {
+ return last;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns true if this node has children.
+ */
+ public boolean hasChildNodes()
+ {
+ return length != 0;
+ }
+
+
+ /**
+ * Exposes the internal "readonly" flag. In DOM, children of
+ * entities and entity references are readonly, as are the
+ * objects associated with DocumentType objets.
+ */
+ public final boolean isReadonly()
+ {
+ return readonly;
+ }
+
+ /**
+ * Sets the internal "readonly" flag so this subtree can't be changed.
+ * Subclasses need to override this method for any associated content
+ * that's not a child node, such as an element's attributes or the
+ * (few) declarations associated with a DocumentType.
+ */
+ public void makeReadonly()
+ {
+ readonly = true;
+ for (DomNode child = first; child != null; child = child.next)
+ {
+ child.makeReadonly();
+ }
+ }
+
+ /**
+ * Used to adopt a node to a new document.
+ */
+ void setOwner(DomDocument doc)
+ {
+ this.owner = doc;
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ ctx.setOwner(doc);
+ }
+ }
+
+ // just checks the node for inclusion -- may be called many
+ // times (docfrag) before anything is allowed to change
+ private void checkMisc(DomNode child)
+ {
+ if (readonly && !owner.building)
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
+ null, this, 0);
+ }
+ for (DomNode ctx = this; ctx != null; ctx = ctx.parent)
+ {
+ if (child == ctx)
+ {
+ throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR,
+ "can't make ancestor into a child",
+ this, 0);
+ }
+ }
+
+ DomDocument owner = (nodeType == DOCUMENT_NODE) ? (DomDocument) this :
+ this.owner;
+ DomDocument childOwner = child.owner;
+ short childNodeType = child.nodeType;
+
+ if (childOwner != owner)
+ {
+ // new in DOM L2, this case -- patch it up later, in reparent()
+ if (!(childNodeType == DOCUMENT_TYPE_NODE && childOwner == null))
+ {
+ throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR,
+ null, child, 0);
+ }
+ }
+
+ // enforce various structural constraints
+ switch (nodeType)
+ {
+ case DOCUMENT_NODE:
+ switch (childNodeType)
+ {
+ case ELEMENT_NODE:
+ case PROCESSING_INSTRUCTION_NODE:
+ case COMMENT_NODE:
+ case DOCUMENT_TYPE_NODE:
+ return;
+ }
+ break;
+
+ case ATTRIBUTE_NODE:
+ switch (childNodeType)
+ {
+ case TEXT_NODE:
+ case ENTITY_REFERENCE_NODE:
+ return;
+ }
+ break;
+
+ case DOCUMENT_FRAGMENT_NODE:
+ case ENTITY_REFERENCE_NODE:
+ case ELEMENT_NODE:
+ case ENTITY_NODE:
+ switch (childNodeType)
+ {
+ case ELEMENT_NODE:
+ case TEXT_NODE:
+ case COMMENT_NODE:
+ case PROCESSING_INSTRUCTION_NODE:
+ case CDATA_SECTION_NODE:
+ case ENTITY_REFERENCE_NODE:
+ return;
+ }
+ break;
+ case DOCUMENT_TYPE_NODE:
+ if (!owner.building)
+ break;
+ switch (childNodeType)
+ {
+ case COMMENT_NODE:
+ case PROCESSING_INSTRUCTION_NODE:
+ return;
+ }
+ break;
+ }
+ if (owner.checkingWellformedness)
+ {
+ throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR,
+ "can't append " +
+ nodeTypeToString(childNodeType) +
+ " to node of type " +
+ nodeTypeToString(nodeType),
+ this, 0);
+ }
+ }
+
+ // Here's hoping a good optimizer will detect the case when the
+ // next several methods are never called, and won't allocate
+ // object code space of any kind. (Case: not reporting any
+ // mutation events. We can also remove some static variables
+ // listed above.)
+
+ private void insertionEvent(DomEvent.DomMutationEvent event,
+ DomNode target)
+ {
+ if (owner == null || owner.building)
+ {
+ return;
+ }
+ boolean doFree = false;
+
+ if (event == null)
+ {
+ event = getMutationEvent();
+ }
+ if (event != null)
+ {
+ doFree = true;
+ }
+ else
+ {
+ event = new DomEvent.DomMutationEvent(null);
+ }
+ event.initMutationEvent("DOMNodeInserted",
+ true /* bubbles */, false /* nocancel */,
+ this /* related */, null, null, null, (short) 0);
+ target.dispatchEvent(event);
+
+ // XXX should really visit every descendant of 'target'
+ // and sent a DOMNodeInsertedIntoDocument event to it...
+ // bleech, there's no way to keep that acceptably fast.
+
+ if (doFree)
+ {
+ event.target = null;
+ event.relatedNode = null;
+ event.currentNode = null;
+ eventDataLock = false;
+ } // else we created work for the GC
+ }
+
+ private void removalEvent(DomEvent.DomMutationEvent event,
+ DomNode target)
+ {
+ if (owner == null || owner.building)
+ {
+ return;
+ }
+ boolean doFree = false;
+
+ if (event == null)
+ {
+ event = getMutationEvent();
+ }
+ if (event != null)
+ {
+ doFree = true;
+ }
+ else
+ {
+ event = new DomEvent.DomMutationEvent(null);
+ }
+ event.initMutationEvent("DOMNodeRemoved",
+ true /* bubbles */, false /* nocancel */,
+ this /* related */, null, null, null, (short) 0);
+ target.dispatchEvent(event);
+
+ // XXX should really visit every descendant of 'target'
+ // and sent a DOMNodeRemovedFromDocument event to it...
+ // bleech, there's no way to keep that acceptably fast.
+
+ event.target = null;
+ event.relatedNode = null;
+ event.currentNode = null;
+ if (doFree)
+ {
+ eventDataLock = false;
+ }
+ // else we created more work for the GC
+ }
+
+ //
+ // Avoid creating lots of memory management work, by using a simple
+ // allocation strategy for the mutation event objects that get used
+ // at least once per tree modification. We can't use stack allocation,
+ // so we do the next simplest thing -- more or less, static allocation.
+ // Concurrent notifications should be rare, anyway.
+ //
+ // Returns the preallocated object, which needs to be carefully freed,
+ // or null to indicate the caller needs to allocate their own.
+ //
+ static private DomEvent.DomMutationEvent getMutationEvent()
+ {
+ synchronized (lockNode)
+ {
+ if (eventDataLock)
+ {
+ return null;
+ }
+ eventDataLock = true;
+ return mutationEvent;
+ }
+ }
+
+ // NOTE: this is manually inlined in the insertion
+ // and removal event methods above; change in sync.
+ static private void freeMutationEvent()
+ {
+ // clear fields to enable GC
+ mutationEvent.clear();
+ eventDataLock = false;
+ }
+
+ void setDepth(int depth)
+ {
+ this.depth = depth;
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ ctx.setDepth(depth + 1);
+ }
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Appends the specified node to this node's list of children.
+ * Document subclasses must override this to enforce the restrictions
+ * that there be only one element and document type child.
+ *
+ * <p> Causes a DOMNodeInserted mutation event to be reported.
+ * Will first cause a DOMNodeRemoved event to be reported if the
+ * parameter already has a parent. If the new child is a document
+ * fragment node, both events will be reported for each child of
+ * the fragment; the order in which children are removed and
+ * inserted is implementation-specific.
+ *
+ * <p> If this DOM has been compiled without mutation event support,
+ * these events will not be reported.
+ */
+ public Node appendChild(Node newChild)
+ {
+ try
+ {
+ DomNode child = (DomNode) newChild;
+
+ if (child.nodeType == DOCUMENT_FRAGMENT_NODE)
+ {
+ // Append all nodes in the fragment to this node
+ for (DomNode ctx = child.first; ctx != null; ctx = ctx.next)
+ {
+ checkMisc(ctx);
+ }
+ for (DomNode ctx = child.first; ctx != null; )
+ {
+ DomNode ctxNext = ctx.next;
+ appendChild(ctx);
+ ctx = ctxNext;
+ }
+ }
+ else
+ {
+ checkMisc(child);
+ if (child.parent != null)
+ {
+ child.parent.removeChild(child);
+ }
+ child.parent = this;
+ child.index = length++;
+ child.setDepth(depth + 1);
+ child.next = null;
+ if (last == null)
+ {
+ first = child;
+ child.previous = null;
+ }
+ else
+ {
+ last.next = child;
+ child.previous = last;
+ }
+ last = child;
+
+ if (reportMutations)
+ {
+ insertionEvent(null, child);
+ }
+ }
+
+ return child;
+ }
+ catch (ClassCastException e)
+ {
+ throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR,
+ null, newChild, 0);
+ }
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Inserts the specified node in this node's list of children.
+ * Document subclasses must override this to enforce the restrictions
+ * that there be only one element and document type child.
+ *
+ * <p> Causes a DOMNodeInserted mutation event to be reported. Will
+ * first cause a DOMNodeRemoved event to be reported if the newChild
+ * parameter already has a parent. If the new child is a document
+ * fragment node, both events will be reported for each child of
+ * the fragment; the order in which children are removed and inserted
+ * is implementation-specific.
+ *
+ * <p> If this DOM has been compiled without mutation event support,
+ * these events will not be reported.
+ */
+ public Node insertBefore(Node newChild, Node refChild)
+ {
+ if (refChild == null)
+ {
+ return appendChild(newChild);
+ }
+
+ try
+ {
+ DomNode child = (DomNode) newChild;
+ DomNode ref = (DomNode) refChild;
+
+ if (child.nodeType == DOCUMENT_FRAGMENT_NODE)
+ {
+ // Append all nodes in the fragment to this node
+ for (DomNode ctx = child.first; ctx != null; ctx = ctx.next)
+ {
+ checkMisc(ctx);
+ }
+ for (DomNode ctx = child.first; ctx != null; )
+ {
+ DomNode ctxNext = ctx.next;
+ insertBefore(ctx, ref);
+ ctx = ctxNext;
+ }
+ }
+ else
+ {
+ checkMisc(child);
+ if (ref == null || ref.parent != this)
+ {
+ throw new DomDOMException(DOMException.NOT_FOUND_ERR,
+ null, ref, 0);
+ }
+ if (ref == child)
+ {
+ throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR,
+ "can't insert node before itself",
+ ref, 0);
+ }
+
+ if (child.parent != null)
+ {
+ child.parent.removeChild(child);
+ }
+ child.parent = this;
+ int i = ref.index;
+ child.setDepth(depth + 1);
+ child.next = ref;
+ if (ref.previous != null)
+ {
+ ref.previous.next = child;
+ }
+ child.previous = ref.previous;
+ ref.previous = child;
+ if (first == ref)
+ {
+ first = child;
+ }
+ // index renumbering
+ for (DomNode ctx = child; ctx != null; ctx = ctx.next)
+ {
+ ctx.index = i++;
+ }
+
+ if (reportMutations)
+ {
+ insertionEvent(null, child);
+ }
+ length++;
+ }
+
+ return child;
+ }
+ catch (ClassCastException e)
+ {
+ throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR,
+ null, newChild, 0);
+ }
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Replaces the specified node in this node's list of children.
+ * Document subclasses must override this to test the restrictions
+ * that there be only one element and document type child.
+ *
+ * <p> Causes DOMNodeRemoved and DOMNodeInserted mutation event to be
+ * reported. Will cause another DOMNodeRemoved event to be reported if
+ * the newChild parameter already has a parent. These events may be
+ * delivered in any order, except that the event reporting removal
+ * from such an existing parent will always be delivered before the
+ * event reporting its re-insertion as a child of some other node.
+ * The order in which children are removed and inserted is implementation
+ * specific.
+ *
+ * <p> If your application needs to depend on the in which those removal
+ * and insertion events are delivered, don't use this API. Instead,
+ * invoke the removeChild and insertBefore methods directly, to guarantee
+ * a specific delivery order. Similarly, don't use document fragments,
+ * Otherwise your application code may not work on a DOM which implements
+ * this method differently.
+ *
+ * <p> If this DOM has been compiled without mutation event support,
+ * these events will not be reported.
+ */
+ public Node replaceChild(Node newChild, Node refChild)
+ {
+ try
+ {
+ DomNode child = (DomNode) newChild;
+ DomNode ref = (DomNode) refChild;
+
+ DomEvent.DomMutationEvent event = getMutationEvent();
+ boolean doFree = (event != null);
+
+ if (child.nodeType == DOCUMENT_FRAGMENT_NODE)
+ {
+ // Append all nodes in the fragment to this node
+ for (DomNode ctx = child.first; ctx != null; ctx = ctx.next)
+ {
+ checkMisc(ctx);
+ }
+ if (ref == null || ref.parent != this)
+ {
+ throw new DomDOMException(DOMException.NOT_FOUND_ERR,
+ null, ref, 0);
+ }
+
+ if (reportMutations)
+ {
+ removalEvent(event, ref);
+ }
+ length--;
+ length += child.length;
+
+ if (child.length == 0)
+ {
+ // Removal
+ if (ref.previous != null)
+ {
+ ref.previous.next = ref.next;
+ }
+ if (ref.next != null)
+ {
+ ref.next.previous = ref.previous;
+ }
+ if (first == ref)
+ {
+ first = ref.next;
+ }
+ if (last == ref)
+ {
+ last = ref.previous;
+ }
+ }
+ else
+ {
+ int i = ref.index;
+ for (DomNode ctx = child.first; ctx != null; ctx = ctx.next)
+ {
+ // Insertion
+ ctx.parent = this;
+ ctx.index = i++;
+ ctx.setDepth(ref.depth);
+ if (ctx == child.first)
+ {
+ ctx.previous = ref.previous;
+ }
+ if (ctx == child.last)
+ {
+ ctx.next = ref.next;
+ }
+ }
+ if (first == ref)
+ {
+ first = child.first;
+ }
+ if (last == ref)
+ {
+ last = child.last;
+ }
+ }
+ }
+ else
+ {
+ checkMisc(child);
+ if (ref == null || ref.parent != this)
+ {
+ throw new DomDOMException(DOMException.NOT_FOUND_ERR,
+ null, ref, 0);
+ }
+
+ if (reportMutations)
+ {
+ removalEvent(event, ref);
+ }
+
+ if (child.parent != null)
+ {
+ child.parent.removeChild(child);
+ }
+ child.parent = this;
+ child.index = ref.index;
+ child.setDepth(ref.depth);
+ if (ref.previous != null)
+ {
+ ref.previous.next = child;
+ }
+ child.previous = ref.previous;
+ if (ref.next != null)
+ {
+ ref.next.previous = child;
+ }
+ child.next = ref.next;
+ if (first == ref)
+ {
+ first = child;
+ }
+ if (last == ref)
+ {
+ last = child;
+ }
+
+ if (reportMutations)
+ {
+ insertionEvent(event, child);
+ }
+ if (doFree)
+ {
+ freeMutationEvent();
+ }
+ }
+ ref.parent = null;
+ ref.index = 0;
+ ref.setDepth(0);
+ ref.previous = null;
+ ref.next = null;
+
+ return ref;
+ }
+ catch (ClassCastException e)
+ {
+ throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR,
+ null, newChild, 0);
+ }
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Removes the specified child from this node's list of children,
+ * or else reports an exception.
+ *
+ * <p> Causes a DOMNodeRemoved mutation event to be reported.
+ *
+ * <p> If this DOM has been compiled without mutation event support,
+ * these events will not be reported.
+ */
+ public Node removeChild(Node refChild)
+ {
+ try
+ {
+ DomNode ref = (DomNode) refChild;
+
+ if (ref == null || ref.parent != this)
+ {
+ throw new DomDOMException(DOMException.NOT_FOUND_ERR,
+ null, ref, 0);
+ }
+ if (readonly && !owner.building)
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
+ null, this, 0);
+ }
+
+ for (DomNode child = first; child != null; child = child.next)
+ {
+ if (child == ref)
+ {
+ if (reportMutations)
+ {
+ removalEvent(null, child);
+ }
+
+ length--;
+ if (ref.previous != null)
+ {
+ ref.previous.next = ref.next;
+ }
+ if (ref.next != null)
+ {
+ ref.next.previous = ref.previous;
+ }
+ if (first == ref)
+ {
+ first = ref.next;
+ }
+ if (last == ref)
+ {
+ last = ref.previous;
+ }
+ // renumber indices
+ int i = 0;
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ ctx.index = i++;
+ }
+ ref.parent = null;
+ ref.setDepth(0);
+ ref.index = 0;
+ ref.previous = null;
+ ref.next = null;
+
+ return ref;
+ }
+ }
+ throw new DomDOMException(DOMException.NOT_FOUND_ERR,
+ "that's no child of mine", refChild, 0);
+ }
+ catch (ClassCastException e)
+ {
+ throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR,
+ null, refChild, 0);
+ }
+ }
+
+ /**
+ * <b>DOM L1 (NodeList)</b>
+ * Returns the item with the specified index in this NodeList,
+ * else null.
+ */
+ public Node item(int index)
+ {
+ DomNode child = first;
+ int count = 0;
+ while (child != null && count < index)
+ {
+ child = child.next;
+ count++;
+ }
+ return child;
+ }
+
+ /**
+ * <b>DOM L1 (NodeList)</b>
+ * Returns the number of elements in this NodeList.
+ * (Note that many interfaces have a "Length" property, not just
+ * NodeList, and if a node subtype must implement one of those,
+ * it will also need to override getChildNodes.)
+ */
+ public int getLength()
+ {
+ return length;
+ }
+
+ /**
+ * Minimize extra space consumed by this node to hold children and event
+ * listeners.
+ */
+ public void trimToSize()
+ {
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the previous sibling, if one is known.
+ */
+ public Node getNextSibling()
+ {
+ return next;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the previous sibling, if one is known.
+ */
+ public Node getPreviousSibling()
+ {
+ return previous;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the parent node, if one is known.
+ */
+ public Node getParentNode()
+ {
+ return parent;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Consults the DOM implementation to determine if the requested
+ * feature is supported. DocumentType subclasses must override
+ * this method, and associate themselves directly with the
+ * DOMImplementation node used. (This method relies on being able
+ * to access the DOMImplementation from the owner document, but
+ * DocumentType nodes can be created without an owner.)
+ */
+ public boolean isSupported(String feature, String version)
+ {
+ Document doc = owner;
+ DOMImplementation impl = null;
+
+ if (doc == null && nodeType == DOCUMENT_NODE)
+ {
+ doc = (Document) this;
+ }
+
+ if (doc == null)
+ {
+ // possible for DocumentType
+ throw new IllegalStateException ("unbound ownerDocument");
+ }
+
+ impl = doc.getImplementation();
+ return impl.hasFeature(feature, version);
+ }
+
+ /**
+ * <b>DOM L1 (modified in L2)</b>
+ * Returns the owner document. This is only null for Document nodes,
+ * and (new in L2) for DocumentType nodes which have not yet been
+ * associated with the rest of their document.
+ */
+ final public Document getOwnerDocument()
+ {
+ return owner;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Does nothing; this must be overridden (along with the
+ * getNodeValue method) for nodes with a non-null defined value.
+ */
+ public void setNodeValue(String value)
+ {
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns null; this must be overridden for nodes types with
+ * a defined value, along with the setNodeValue method.
+ */
+ public String getNodeValue()
+ {
+ return null;
+ }
+
+ /** This forces GCJ compatibility.
+ * Without this method GCJ is unable to compile to byte code.
+ */
+ public final short getNodeType()
+ {
+ return nodeType;
+ }
+
+ /** This forces GCJ compatibility.
+ * Without this method GCJ seems unable to natively compile GNUJAXP.
+ */
+ public abstract String getNodeName();
+
+ /**
+ * <b>DOM L2</b>
+ * Does nothing; this must be overridden (along with the
+ * getPrefix method) for element and attribute nodes.
+ */
+ public void setPrefix(String prefix)
+ {
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns null; this must be overridden for element and
+ * attribute nodes.
+ */
+ public String getPrefix()
+ {
+ return null;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns null; this must be overridden for element and
+ * attribute nodes.
+ */
+ public String getNamespaceURI()
+ {
+ return null;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns the node name; this must be overridden for element and
+ * attribute nodes.
+ */
+ public String getLocalName()
+ {
+ return null;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns a clone of this node which optionally includes cloned
+ * versions of child nodes. Clones are always mutable, except for
+ * entity reference nodes.
+ */
+ public Node cloneNode(boolean deep)
+ {
+ if (deep)
+ {
+ return cloneNodeDeepInternal(true, null);
+ }
+
+ DomNode node = (DomNode) clone();
+ if (nodeType == ENTITY_REFERENCE_NODE)
+ {
+ node.makeReadonly();
+ }
+ notifyUserDataHandlers(UserDataHandler.NODE_CLONED, this, node);
+ return node;
+ }
+
+ /**
+ * Returns a deep clone of this node.
+ */
+ private DomNode cloneNodeDeepInternal(boolean root, DomDocument doc)
+ {
+ DomNode node = (DomNode) clone();
+ boolean building = false; // Never used unless root is true
+ if (root)
+ {
+ doc = (nodeType == DOCUMENT_NODE) ? (DomDocument) node : node.owner;
+ building = doc.building;
+ doc.building = true; // Permit certain structural rules
+ }
+ node.owner = doc;
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ DomNode newChild = ctx.cloneNodeDeepInternal(false, doc);
+ node.appendChild(newChild);
+ }
+ if (nodeType == ENTITY_REFERENCE_NODE)
+ {
+ node.makeReadonly();
+ }
+ if (root)
+ {
+ doc.building = building;
+ }
+ notifyUserDataHandlers(UserDataHandler.NODE_CLONED, this, node);
+ return node;
+ }
+
+ void notifyUserDataHandlers(short op, Node src, Node dst)
+ {
+ if (userDataHandlers != null)
+ {
+ for (Iterator i = userDataHandlers.entrySet().iterator(); i.hasNext(); )
+ {
+ Map.Entry entry = (Map.Entry) i.next();
+ String key = (String) entry.getKey();
+ UserDataHandler handler = (UserDataHandler) entry.getValue();
+ Object data = userData.get(key);
+ handler.handle(op, key, data, src, dst);
+ }
+ }
+ }
+
+ /**
+ * Clones this node; roughly equivalent to cloneNode(false).
+ * Element subclasses must provide a new implementation which
+ * invokes this method to handle the basics, and then arranges
+ * to clone any element attributes directly. Attribute subclasses
+ * must make similar arrangements, ensuring that existing ties to
+ * elements are broken by cloning.
+ */
+ public Object clone()
+ {
+ try
+ {
+ DomNode node = (DomNode) super.clone();
+
+ node.parent = null;
+ node.depth = 0;
+ node.index = 0;
+ node.length = 0;
+ node.first = null;
+ node.last = null;
+ node.previous = null;
+ node.next = null;
+
+ node.readonly = false;
+ node.listeners = new HashSet();
+ node.nListeners = 0;
+ return node;
+
+ }
+ catch (CloneNotSupportedException x)
+ {
+ throw new Error("clone didn't work");
+ }
+ }
+
+ // the elements-by-tagname stuff is needed for both
+ // elements and documents ... this is in lieu of a
+ // common base class between Node and NodeNS.
+
+ /**
+ * <b>DOM L1</b>
+ * Creates a NodeList giving array-style access to elements with
+ * the specified name. Access is fastest if indices change by
+ * small values, and the DOM is not modified.
+ */
+ public NodeList getElementsByTagName(String tag)
+ {
+ return new ShadowList(null, tag);
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Creates a NodeList giving array-style access to elements with
+ * the specified namespace and local name. Access is fastest if
+ * indices change by small values, and the DOM is not modified.
+ */
+ public NodeList getElementsByTagNameNS(String namespace, String local)
+ {
+ return new ShadowList(namespace, local);
+ }
+
+
+ //
+ // This shadow class is GC-able even when the live list it shadows
+ // can't be, because of event registration hookups. Its finalizer
+ // makes that live list become GC-able.
+ //
+ final class ShadowList
+ implements NodeList
+ {
+
+ private LiveNodeList liveList;
+
+ ShadowList(String ns, String local)
+ {
+ liveList = new LiveNodeList(ns, local);
+ }
+
+ public void finalize()
+ {
+ liveList.detach();
+ liveList = null;
+ }
+
+ public Node item(int index)
+ {
+ return liveList.item(index);
+ }
+
+ public int getLength()
+ {
+ return liveList.getLength();
+ }
+ }
+
+ final class LiveNodeList
+ implements NodeList, EventListener, NodeFilter
+ {
+
+ private final boolean matchAnyURI;
+ private final boolean matchAnyName;
+ private final String elementURI;
+ private final String elementName;
+
+ private DomIterator current;
+ private int lastIndex;
+
+ LiveNodeList(String uri, String name)
+ {
+ elementURI = uri;
+ elementName = name;
+ matchAnyURI = "*".equals(uri);
+ matchAnyName = "*".equals(name);
+
+ DomNode.this.addEventListener("DOMNodeInserted", this, true);
+ DomNode.this.addEventListener("DOMNodeRemoved", this, true);
+ }
+
+ void detach()
+ {
+ if (current != null)
+ current.detach();
+ current = null;
+
+ DomNode.this.removeEventListener("DOMNodeInserted", this, true);
+ DomNode.this.removeEventListener("DOMNodeRemoved", this, true);
+ }
+
+ public short acceptNode(Node element)
+ {
+ if (element == DomNode.this)
+ {
+ return FILTER_SKIP;
+ }
+
+ // use namespace-aware matching ...
+ if (elementURI != null)
+ {
+ if (!(matchAnyURI
+ || elementURI.equals(element.getNamespaceURI())))
+ {
+ return FILTER_SKIP;
+ }
+ if (!(matchAnyName
+ || elementName.equals(element.getLocalName())))
+ {
+ return FILTER_SKIP;
+ }
+
+ // ... or qName-based kind.
+ }
+ else
+ {
+ if (!(matchAnyName
+ || elementName.equals(element.getNodeName())))
+ {
+ return FILTER_SKIP;
+ }
+ }
+ return FILTER_ACCEPT;
+ }
+
+ private DomIterator createIterator()
+ {
+ return new DomIterator(DomNode.this,
+ NodeFilter.SHOW_ELEMENT,
+ this, /* filter */
+ true /* expand entity refs */
+ );
+ }
+
+ public void handleEvent(Event e)
+ {
+ MutationEvent mutation = (MutationEvent) e;
+ Node related = mutation.getRelatedNode();
+
+ // XXX if it's got children ... check all kids too, they
+ // will invalidate our saved index
+
+ if (related.getNodeType() != Node.ELEMENT_NODE ||
+ related.getNodeName() != elementName ||
+ related.getNamespaceURI() != elementURI)
+ {
+ return;
+ }
+
+ if (current != null)
+ current.detach();
+ current = null;
+ }
+
+ public Node item(int index)
+ {
+ if (current == null)
+ {
+ current = createIterator();
+ lastIndex = -1;
+ }
+
+ // last node or before? go backwards
+ if (index <= lastIndex) {
+ while (index != lastIndex) {
+ current.previousNode ();
+ lastIndex--;
+ }
+ Node ret = current.previousNode ();
+ current.detach();
+ current = null;
+ return ret;
+ }
+
+ // somewhere after last node
+ while (++lastIndex != index)
+ current.nextNode ();
+
+ Node ret = current.nextNode ();
+ current.detach();
+ current = null;
+ return ret;
+ }
+
+ public int getLength()
+ {
+ int retval = 0;
+ NodeIterator iter = createIterator();
+
+ while (iter.nextNode() != null)
+ {
+ retval++;
+ }
+ iter.detach();
+ return retval;
+ }
+
+ }
+
+ //
+ // EventTarget support
+ //
+ static final class ListenerRecord
+ {
+
+ String type;
+ EventListener listener;
+ boolean useCapture;
+
+ // XXX use JDK 1.2 java.lang.ref.WeakReference to listener,
+ // and we can both get rid of "shadow" classes and remove
+ // the need for applications to apply similar trix ... but
+ // JDK 1.2 support isn't generally available yet
+
+ ListenerRecord(String type, EventListener listener, boolean useCapture)
+ {
+ this.type = type.intern();
+ this.listener = listener;
+ this.useCapture = useCapture;
+ }
+
+ public boolean equals(Object o)
+ {
+ ListenerRecord rec = (ListenerRecord)o;
+ return listener == rec.listener
+ && useCapture == rec.useCapture
+ && type == rec.type;
+ }
+
+ public int hashCode()
+ {
+ return listener.hashCode() ^ type.hashCode();
+ }
+ }
+
+ /**
+ * <b>DOM L2 (Events)</b>
+ * Returns an instance of the specified type of event object.
+ * Understands about DOM Mutation, HTML, and UI events.
+ *
+ * <p>If the name of the event type begins with "USER-", then an object
+ * implementing the "Event" class will be returned; this provides a
+ * limited facility for application-defined events to use the DOM event
+ * infrastructure. Alternatively, use one of the standard DOM event
+ * classes and initialize it using use such a "USER-" event type name;
+ * or defin, instantiate, and initialize an application-specific subclass
+ * of DomEvent and pass that to dispatchEvent().
+ *
+ * @param eventType Identifies the particular DOM feature module
+ * defining the type of event, such as "MutationEvents".
+ * <em>The event "name" is a different kind of "type".</em>
+ */
+ public Event createEvent(String eventType)
+ {
+ eventType = eventType.toLowerCase();
+
+ if ("mutationevents".equals(eventType))
+ {
+ return new DomEvent.DomMutationEvent(null);
+ }
+
+ if ("htmlevents".equals(eventType)
+ || "events".equals(eventType)
+ || "user-events".equals(eventType))
+ {
+ return new DomEvent(null);
+ }
+
+ if ("uievents".equals(eventType))
+ {
+ return new DomEvent.DomUIEvent(null);
+ }
+
+ // mouse events
+
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR,
+ eventType, null, 0);
+ }
+
+ /**
+ * <b>DOM L2 (Events)</b>
+ * Registers an event listener's interest in a class of events.
+ */
+ public final void addEventListener(String type,
+ EventListener listener,
+ boolean useCapture)
+ {
+ // prune duplicates
+ ListenerRecord record;
+
+ record = new ListenerRecord(type, listener, useCapture);
+ listeners.add(record);
+ nListeners = listeners.size();
+ }
+
+ // XXX this exception should be discarded from DOM
+
+ // this class can be instantiated, unlike the one in the spec
+ static final class DomEventException
+ extends EventException
+ {
+
+ DomEventException()
+ {
+ super(UNSPECIFIED_EVENT_TYPE_ERR, "unspecified event type");
+ }
+
+ }
+
+ /**
+ * <b>DOM L2 (Events)</b>
+ * Delivers an event to all relevant listeners, returning true if the
+ * caller should perform their default action. Note that the event
+ * must have been provided by the createEvent() method on this
+ * class, else it can't be dispatched.
+ *
+ * @see #createEvent
+ *
+ * @exception NullPointerException When a null event is passed.
+ * @exception ClassCastException When the event wasn't provided by
+ * the createEvent method, or otherwise isn't a DomEvent.
+ * @exception EventException If the event type wasn't specified
+ */
+ public final boolean dispatchEvent(Event event)
+ throws EventException
+ {
+ DomEvent e = (DomEvent) event;
+ DomNode[] ancestors = null;
+ int ancestorMax = 0;
+ boolean haveDispatchDataLock = false;
+
+ if (e.type == null)
+ {
+ throw new DomEventException();
+ }
+
+ e.doDefault = true;
+ e.target = this;
+
+ //
+ // Typical case: one nonrecursive dispatchEvent call at a time
+ // for this class. If that's our case, we can avoid allocating
+ // garbage, which is overall a big win. Even with advanced GCs
+ // that deal well with short-lived garbage, and wayfast allocators,
+ // it still helps.
+ //
+ // Remember -- EVERY mutation goes though here at least once.
+ //
+ // When populating a DOM tree, trying to send mutation events is
+ // the primary cost; this dominates the critical path.
+ //
+ try
+ {
+ DomNode current;
+ int index;
+ boolean haveAncestorRegistrations = false;
+ ListenerRecord[] notificationSet;
+ int ancestorLen;
+
+ synchronized (lockNode)
+ {
+ if (!dispatchDataLock)
+ {
+ haveDispatchDataLock = dispatchDataLock = true;
+ notificationSet = DomNode.notificationSet;
+ ancestors = DomNode.ancestors;
+ }
+ else
+ {
+ notificationSet = new ListenerRecord[NOTIFICATIONS_INIT];
+ ancestors = new DomNode[ANCESTORS_INIT];
+ }
+ ancestorLen = ancestors.length;
+ }
+
+ // Climb to the top of this subtree and handle capture, letting
+ // each node (from the top down) capture until one stops it or
+ // until we get to this one.
+ current = (parent == null) ? this : parent;
+ if (current.depth >= ANCESTORS_INIT)
+ {
+ DomNode[] newants = new DomNode[current.depth + 1];
+ System.arraycopy(ancestors, 0, newants, 0, ancestors.length);
+ ancestors = newants;
+ ancestorLen = ancestors.length;
+ }
+ for (index = 0; index < ancestorLen; index++)
+ {
+ if (current == null || current.depth == 0)
+ break;
+
+ if (current.nListeners != 0)
+ {
+ haveAncestorRegistrations = true;
+ }
+ ancestors [index] = current;
+ current = current.parent;
+ }
+ if (current.depth > 0)
+ {
+ throw new RuntimeException("dispatchEvent capture stack size");
+ }
+
+ ancestorMax = index;
+ e.stop = false;
+
+ if (haveAncestorRegistrations)
+ {
+ e.eventPhase = Event.CAPTURING_PHASE;
+ while (!e.stop && index-- > 0)
+ {
+ current = ancestors [index];
+ if (current.nListeners != 0)
+ {
+ notifyNode(e, current, true, notificationSet);
+ }
+ }
+ }
+
+ // Always deliver events to the target node (this)
+ // unless stopPropagation was called. If we saw
+ // no registrations yet (typical!), we never will.
+ if (!e.stop && nListeners != 0)
+ {
+ e.eventPhase = Event.AT_TARGET;
+ notifyNode (e, this, false, notificationSet);
+ }
+ else if (!haveAncestorRegistrations)
+ {
+ e.stop = true;
+ }
+
+ // If the event bubbles and propagation wasn't halted,
+ // walk back up the ancestor list. Stop bubbling when
+ // any bubbled event handler stops it.
+
+ if (!e.stop && e.bubbles)
+ {
+ e.eventPhase = Event.BUBBLING_PHASE;
+ for (index = 0;
+ !e.stop
+ && index < ancestorMax
+ && (current = ancestors[index]) != null;
+ index++)
+ {
+ if (current.nListeners != 0)
+ {
+ notifyNode(e, current, false, notificationSet);
+ }
+ }
+ }
+ e.eventPhase = 0;
+
+ // Caller chooses whether to perform the default
+ // action based on return from this method.
+ return e.doDefault;
+
+ }
+ finally
+ {
+ if (haveDispatchDataLock)
+ {
+ // synchronize to force write ordering
+ synchronized (lockNode)
+ {
+ // null out refs to ensure they'll be GC'd
+ for (int i = 0; i < ancestorMax; i++)
+ {
+ ancestors [i] = null;
+ }
+ // notificationSet handled by notifyNode
+
+ dispatchDataLock = false;
+ }
+ }
+ }
+ }
+
+ private void notifyNode(DomEvent e,
+ DomNode current,
+ boolean capture,
+ ListenerRecord[] notificationSet)
+ {
+ int count = 0;
+ Iterator iter;
+
+ iter = current.listeners.iterator();
+
+ // do any of this set of listeners get notified?
+ while (iter.hasNext())
+ {
+ ListenerRecord rec = (ListenerRecord)iter.next();
+
+ if (rec.useCapture != capture)
+ {
+ continue;
+ }
+ if (!e.type.equals (rec.type))
+ {
+ continue;
+ }
+ if (count >= notificationSet.length)
+ {
+ // very simple growth algorithm
+ int len = Math.max(notificationSet.length, 1);
+ ListenerRecord[] tmp = new ListenerRecord[len * 2];
+ System.arraycopy(notificationSet, 0, tmp, 0,
+ notificationSet.length);
+ notificationSet = tmp;
+ }
+ notificationSet[count++] = rec;
+ }
+ iter = null;
+
+ // Notify just those listeners
+ e.currentNode = current;
+ for (int i = 0; i < count; i++)
+ {
+ try
+ {
+ iter = current.listeners.iterator();
+ // Late in the DOM CR process (3rd or 4th CR?) the
+ // removeEventListener spec became asymmetric with respect
+ // to addEventListener ... effect is now immediate.
+ while (iter.hasNext())
+ {
+ ListenerRecord rec = (ListenerRecord)iter.next();
+
+ if (rec.equals(notificationSet[i]))
+ {
+ notificationSet[i].listener.handleEvent(e);
+ break;
+ }
+ }
+ iter = null;
+ }
+ catch (Exception x)
+ {
+ // ignore all exceptions
+ }
+ notificationSet[i] = null; // free for GC
+ }
+ }
+
+ /**
+ * <b>DOM L2 (Events)</b>
+ * Unregisters an event listener.
+ */
+ public final void removeEventListener(String type,
+ EventListener listener,
+ boolean useCapture)
+ {
+ listeners.remove(new ListenerRecord(type, listener, useCapture));
+ nListeners = listeners.size();
+ // no exceptions reported
+ }
+
+ /**
+ * <b>DOM L1 (relocated in DOM L2)</b>
+ * In this node and all contained nodes (including attributes if
+ * relevant) merge adjacent text nodes. This is done while ignoring
+ * text which happens to use CDATA delimiters).
+ */
+ public final void normalize()
+ {
+ // Suspend readonly status
+ boolean saved = readonly;
+ readonly = false;
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ boolean saved2 = ctx.readonly;
+ ctx.readonly = false;
+ switch (ctx.nodeType)
+ {
+ case TEXT_NODE:
+ case CDATA_SECTION_NODE:
+ while (ctx.next != null &&
+ (ctx.next.nodeType == TEXT_NODE ||
+ ctx.next.nodeType == CDATA_SECTION_NODE))
+ {
+ Text text = (Text) ctx;
+ text.appendData(ctx.next.getNodeValue());
+ removeChild(ctx.next);
+ }
+ break;
+ case ELEMENT_NODE:
+ NamedNodeMap attrs = ctx.getAttributes();
+ int len = attrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ DomNode attr = (DomNode) attrs.item(i);
+ boolean saved3 = attr.readonly;
+ attr.readonly = false;
+ attr.normalize();
+ attr.readonly = saved3;
+ }
+ // Fall through
+ case DOCUMENT_NODE:
+ case DOCUMENT_FRAGMENT_NODE:
+ case ATTRIBUTE_NODE:
+ case ENTITY_REFERENCE_NODE:
+ ctx.normalize();
+ break;
+ }
+ ctx.readonly = saved2;
+ }
+ readonly = saved;
+ }
+
+ /**
+ * Returns true iff node types match, and either (a) both nodes have no
+ * namespace and their getNodeName() values are the same, or (b) both
+ * nodes have the same getNamespaceURI() and same getLocalName() values.
+ *
+ * <p>Note that notion of a "Per-Element-Type" attribute name scope, as
+ * found in a non-normative appendix of the XML Namespaces specification,
+ * is not supported here. Your application must implement that notion,
+ * typically by not bothering to check nameAndTypeEquals for attributes
+ * without namespace URIs unless you already know their elements are
+ * nameAndTypeEquals.
+ */
+ public boolean nameAndTypeEquals(Node other)
+ {
+ if (other == this)
+ {
+ return true;
+ }
+ // node types must match
+ if (nodeType != other.getNodeType())
+ {
+ return false;
+ }
+
+ // if both have namespaces, do a "full" comparision
+ // this is a "global" partition
+ String ns1 = this.getNamespaceURI();
+ String ns2 = other.getNamespaceURI();
+
+ if (ns1 != null && ns2 != null)
+ {
+ return ns1.equals(ns2) &&
+ equal(getLocalName(), other.getLocalName());
+ }
+
+ // if neither has a namespace, this is a "no-namespace" name.
+ if (ns1 == null && ns2 == null)
+ {
+ if (!getNodeName().equals(other.getNodeName()))
+ {
+ return false;
+ }
+ // can test the non-normative "per-element-type" scope here.
+ // if this is an attribute node and both nodes have been bound
+ // to elements (!!), then return the nameAndTypeEquals()
+ // comparison of those elements.
+ return true;
+ }
+
+ // otherwise they're unequal: one scoped, one not.
+ return false;
+ }
+
+ // DOM Level 3 methods
+
+ public String getBaseURI()
+ {
+ return (parent != null) ? parent.getBaseURI() : null;
+ }
+
+ public short compareDocumentPosition(Node other)
+ throws DOMException
+ {
+ return (short) compareTo(other);
+ }
+
+ /**
+ * DOM nodes have a natural ordering: document order.
+ */
+ public final int compareTo(Object other)
+ {
+ if (other instanceof DomNode)
+ {
+ DomNode n1 = this;
+ DomNode n2 = (DomNode) other;
+ if (n1.owner != n2.owner)
+ {
+ return 0;
+ }
+ int d1 = n1.depth, d2 = n2.depth;
+ int delta = d1 - d2;
+ while (d1 > d2)
+ {
+ n1 = n1.parent;
+ d1--;
+ }
+ while (d2 > d1)
+ {
+ n2 = n2.parent;
+ d2--;
+ }
+ int c = compareTo2(n1, n2);
+ return (c != 0) ? c : delta;
+ }
+ return 0;
+ }
+
+ /**
+ * Compare two nodes at the same depth.
+ */
+ final int compareTo2(DomNode n1, DomNode n2)
+ {
+ if (n1 == n2 || n1.depth == 0 || n2.depth == 0)
+ {
+ return 0;
+ }
+ int c = compareTo2(n1.parent, n2.parent);
+ return (c != 0) ? c : n1.index - n2.index;
+ }
+
+ public final String getTextContent()
+ throws DOMException
+ {
+ return getTextContent(true);
+ }
+
+ final String getTextContent(boolean topLevel)
+ throws DOMException
+ {
+ switch (nodeType)
+ {
+ case ELEMENT_NODE:
+ case ENTITY_NODE:
+ case ENTITY_REFERENCE_NODE:
+ case DOCUMENT_FRAGMENT_NODE:
+ CPStringBuilder buffer = new CPStringBuilder();
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ String textContent = ctx.getTextContent(false);
+ if (textContent != null)
+ {
+ buffer.append(textContent);
+ }
+ }
+ return buffer.toString();
+ case TEXT_NODE:
+ case CDATA_SECTION_NODE:
+ if (((Text) this).isElementContentWhitespace())
+ {
+ return "";
+ }
+ return getNodeValue();
+ case ATTRIBUTE_NODE:
+ return getNodeValue();
+ case COMMENT_NODE:
+ case PROCESSING_INSTRUCTION_NODE:
+ return topLevel ? getNodeValue() : "";
+ default:
+ return null;
+ }
+ }
+
+ public void setTextContent(String textContent)
+ throws DOMException
+ {
+ switch (nodeType)
+ {
+ case ELEMENT_NODE:
+ case ATTRIBUTE_NODE:
+ case ENTITY_NODE:
+ case ENTITY_REFERENCE_NODE:
+ case DOCUMENT_FRAGMENT_NODE:
+ for (DomNode ctx = first; ctx != null; )
+ {
+ DomNode n = ctx.next;
+ removeChild(ctx);
+ ctx = n;
+ }
+ if (textContent != null)
+ {
+ Text text = owner.createTextNode(textContent);
+ appendChild(text);
+ }
+ break;
+ case TEXT_NODE:
+ case CDATA_SECTION_NODE:
+ case COMMENT_NODE:
+ case PROCESSING_INSTRUCTION_NODE:
+ setNodeValue(textContent);
+ break;
+ }
+ }
+
+ public boolean isSameNode(Node other)
+ {
+ return this == other;
+ }
+
+ public String lookupPrefix(String namespaceURI)
+ {
+ return (parent == null || parent == owner) ? null :
+ parent.lookupPrefix(namespaceURI);
+ }
+
+ public boolean isDefaultNamespace(String namespaceURI)
+ {
+ return (parent == null || parent == owner) ? false :
+ parent.isDefaultNamespace(namespaceURI);
+ }
+
+ public String lookupNamespaceURI(String prefix)
+ {
+ return (parent == null || parent == owner) ? null :
+ parent.lookupNamespaceURI(prefix);
+ }
+
+ public boolean isEqualNode(Node arg)
+ {
+ if (this == arg)
+ return true;
+ if (arg == null)
+ return false;
+ if (nodeType != arg.getNodeType())
+ return false;
+ switch (nodeType)
+ {
+ case ELEMENT_NODE:
+ case ATTRIBUTE_NODE:
+ if (!equal(getLocalName(), arg.getLocalName()) ||
+ !equal(getNamespaceURI(), arg.getNamespaceURI()))
+ return false;
+ break;
+ case PROCESSING_INSTRUCTION_NODE:
+ if (!equal(getNodeName(), arg.getNodeName()) ||
+ !equal(getNodeValue(), arg.getNodeValue()))
+ return false;
+ break;
+ case COMMENT_NODE:
+ case TEXT_NODE:
+ case CDATA_SECTION_NODE:
+ if (!equal(getNodeValue(), arg.getNodeValue()))
+ return false;
+ break;
+ }
+ // Children
+ Node argCtx = arg.getFirstChild();
+ getFirstChild(); // because of DomAttr lazy children
+ DomNode ctx = first;
+ for (; ctx != null && argCtx != null; ctx = ctx.next)
+ {
+ if (nodeType == DOCUMENT_NODE)
+ {
+ // Ignore whitespace outside document element
+ while (ctx != null && ctx.nodeType == TEXT_NODE)
+ ctx = ctx.next;
+ while (argCtx != null && ctx.getNodeType() == TEXT_NODE)
+ argCtx = argCtx.getNextSibling();
+ if (ctx == null && argCtx != null)
+ return false;
+ else if (argCtx == null && ctx != null)
+ return false;
+ }
+ if (!ctx.isEqualNode(argCtx))
+ return false;
+ argCtx = argCtx.getNextSibling();
+ }
+ if (ctx != null || argCtx != null)
+ return false;
+
+ // TODO DocumentType
+ return true;
+ }
+
+ boolean equal(String arg1, String arg2)
+ {
+ return ((arg1 == null && arg2 == null) ||
+ (arg1 != null && arg1.equals(arg2)));
+ }
+
+ public Object getFeature(String feature, String version)
+ {
+ DOMImplementation impl = (nodeType == DOCUMENT_NODE) ?
+ ((Document) this).getImplementation() : owner.getImplementation();
+ if (impl.hasFeature(feature, version))
+ {
+ return this;
+ }
+ return null;
+ }
+
+ public Object setUserData(String key, Object data, UserDataHandler handler)
+ {
+ if (userData == null)
+ {
+ userData = new HashMap();
+ }
+ if (handler != null)
+ {
+ if (userDataHandlers == null)
+ {
+ userDataHandlers = new HashMap();
+ }
+ userDataHandlers.put(key, handler);
+ }
+ return userData.put(key, data);
+ }
+
+ public Object getUserData(String key)
+ {
+ if (userData == null)
+ {
+ return null;
+ }
+ return userData.get(key);
+ }
+
+ public String toString()
+ {
+ String nodeName = getNodeName();
+ String nodeValue = getNodeValue();
+ CPStringBuilder buf = new CPStringBuilder(getClass().getName());
+ buf.append('[');
+ if (nodeName != null)
+ {
+ buf.append(nodeName);
+ }
+ if (nodeValue != null)
+ {
+ if (nodeName != null)
+ {
+ buf.append('=');
+ }
+ buf.append('\'');
+ buf.append(encode(nodeValue));
+ buf.append('\'');
+ }
+ buf.append(']');
+ return buf.toString();
+ }
+
+ String encode(String value)
+ {
+ CPStringBuilder buf = null;
+ int len = value.length();
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == '\n')
+ {
+ if (buf == null)
+ {
+ buf = new CPStringBuilder(value.substring(0, i));
+ }
+ buf.append("\\n");
+ }
+ else if (c == '\r')
+ {
+ if (buf == null)
+ {
+ buf = new CPStringBuilder(value.substring(0, i));
+ }
+ buf.append("\\r");
+ }
+ else if (buf != null)
+ {
+ buf.append(c);
+ }
+ }
+ return (buf != null) ? buf.toString() : value;
+ }
+
+ String nodeTypeToString(short nodeType)
+ {
+ switch (nodeType)
+ {
+ case ELEMENT_NODE:
+ return "ELEMENT_NODE";
+ case ATTRIBUTE_NODE:
+ return "ATTRIBUTE_NODE";
+ case TEXT_NODE:
+ return "TEXT_NODE";
+ case CDATA_SECTION_NODE:
+ return "CDATA_SECTION_NODE";
+ case DOCUMENT_NODE:
+ return "DOCUMENT_NODE";
+ case DOCUMENT_TYPE_NODE:
+ return "DOCUMENT_TYPE_NODE";
+ case COMMENT_NODE:
+ return "COMMENT_NODE";
+ case PROCESSING_INSTRUCTION_NODE:
+ return "PROCESSING_INSTRUCTION_NODE";
+ case DOCUMENT_FRAGMENT_NODE:
+ return "DOCUMENT_FRAGMENT_NODE";
+ case ENTITY_NODE:
+ return "ENTITY_NODE";
+ case ENTITY_REFERENCE_NODE:
+ return "ENTITY_REFERENCE_NODE";
+ case NOTATION_NODE:
+ return "NOTATION_NODE";
+ default:
+ return "UNKNOWN";
+ }
+ }
+
+ public void list(java.io.PrintStream out, int indent)
+ {
+ for (int i = 0; i < indent; i++)
+ out.print(" ");
+ out.println(toString());
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ ctx.list(out, indent + 1);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomNodeIterator.java b/libjava/classpath/gnu/xml/dom/DomNodeIterator.java
new file mode 100644
index 000000000..9f9ec7e39
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomNodeIterator.java
@@ -0,0 +1,328 @@
+/* DomNodeIterator.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+package gnu.xml.dom;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+import org.w3c.dom.traversal.NodeFilter;
+import org.w3c.dom.traversal.NodeIterator;
+import org.w3c.dom.traversal.TreeWalker;
+
+/**
+ * Node iterator and tree walker.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomNodeIterator
+ implements NodeIterator, TreeWalker
+{
+
+ Node root;
+ final int whatToShow;
+ final NodeFilter filter;
+ final boolean entityReferenceExpansion;
+ final boolean walk;
+ Node current;
+
+ public DomNodeIterator(Node root, int whatToShow, NodeFilter filter,
+ boolean entityReferenceExpansion, boolean walk)
+ {
+ if (root == null)
+ {
+ throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "null root");
+ }
+ this.root = root;
+ this.whatToShow = whatToShow;
+ this.filter = filter;
+ this.entityReferenceExpansion = entityReferenceExpansion;
+ this.walk = walk;
+ current = root;
+ }
+
+ public Node getRoot()
+ {
+ return root;
+ }
+
+ public int getWhatToShow()
+ {
+ return whatToShow;
+ }
+
+ public NodeFilter getFilter()
+ {
+ return filter;
+ }
+
+ public boolean getExpandEntityReferences()
+ {
+ return entityReferenceExpansion;
+ }
+
+ public Node nextNode()
+ throws DOMException
+ {
+ if (root == null)
+ {
+ throw new DOMException(DOMException.INVALID_STATE_ERR, "null root");
+ }
+ Node ret;
+ do
+ {
+ if (current.equals(root))
+ {
+ ret = root.getFirstChild();
+ }
+ else if (walk)
+ {
+ ret = current.getFirstChild();
+ if (ret == null)
+ {
+ ret = current.getNextSibling();
+ }
+ if (ret == null)
+ {
+ Node tmp = current;
+ ret = tmp.getParentNode();
+ while (!ret.equals(root) && tmp.equals(ret.getLastChild()))
+ {
+ tmp = ret;
+ ret = tmp.getParentNode();
+ }
+ if (ret.equals(root))
+ {
+ ret = null;
+ }
+ else
+ {
+ ret = ret.getNextSibling();
+ }
+ }
+ }
+ else
+ {
+ ret = current.getNextSibling();
+ }
+ current = (ret == null) ? current : ret;
+ }
+ while (!accept(ret));
+
+ return ret;
+ }
+
+ public Node previousNode()
+ throws DOMException
+ {
+ if (root == null)
+ {
+ throw new DOMException(DOMException.INVALID_STATE_ERR, "null root");
+ }
+ Node ret;
+ do
+ {
+ if (current.equals(root))
+ {
+ ret = current.getLastChild();
+ }
+ else if (walk)
+ {
+ ret = current.getLastChild();
+ if (ret == null)
+ {
+ ret = current.getPreviousSibling();
+ }
+ if (ret == null)
+ {
+ Node tmp = current;
+ ret = tmp.getParentNode();
+ while (!ret.equals(root) && tmp.equals(ret.getFirstChild()))
+ {
+ tmp = ret;
+ ret = tmp.getParentNode();
+ }
+ if (ret.equals(root))
+ {
+ ret = null;
+ }
+ else
+ {
+ ret = ret.getPreviousSibling();
+ }
+ }
+ }
+ else
+ {
+ ret = current.getPreviousSibling();
+ }
+ }
+ while (!accept(ret));
+ current = (ret == null) ? current : ret;
+ return ret;
+ }
+
+ public Node getCurrentNode()
+ {
+ return current;
+ }
+
+ public void setCurrentNode(Node current)
+ throws DOMException
+ {
+ if (current == null)
+ {
+ throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "null root");
+ }
+ this.current = current;
+ }
+
+ public Node parentNode()
+ {
+ Node ret = current.getParentNode();
+ if (!accept (ret))
+ {
+ ret = null;
+ }
+ current = (ret == null) ? current : ret;
+ return ret;
+ }
+
+ public Node firstChild ()
+ {
+ Node ret = current.getFirstChild();
+ while (!accept(ret))
+ {
+ ret = ret.getNextSibling();
+ }
+ current = (ret == null) ? current : ret;
+ return ret;
+ }
+
+ public Node lastChild()
+ {
+ Node ret = current.getLastChild();
+ while (!accept(ret))
+ {
+ ret = ret.getPreviousSibling();
+ }
+ current = (ret == null) ? current : ret;
+ return ret;
+ }
+
+ public Node previousSibling()
+ {
+ Node ret = current.getPreviousSibling();
+ while (!accept(ret))
+ {
+ ret = ret.getPreviousSibling();
+ }
+ current = (ret == null) ? current : ret;
+ return ret;
+ }
+
+ public Node nextSibling()
+ {
+ Node ret = current.getNextSibling();
+ while (!accept(ret))
+ {
+ ret = ret.getNextSibling();
+ }
+ current = (ret == null) ? current : ret;
+ return ret;
+ }
+
+ public void detach()
+ {
+ root = null;
+ }
+
+ boolean accept(Node node)
+ {
+ if (node == null)
+ {
+ return true;
+ }
+ boolean ret;
+ switch (node.getNodeType())
+ {
+ case Node.ATTRIBUTE_NODE:
+ ret = (whatToShow & NodeFilter.SHOW_ATTRIBUTE) != 0;
+ break;
+ case Node.CDATA_SECTION_NODE:
+ ret = (whatToShow & NodeFilter.SHOW_CDATA_SECTION) != 0;
+ break;
+ case Node.COMMENT_NODE:
+ ret = (whatToShow & NodeFilter.SHOW_COMMENT) != 0;
+ break;
+ case Node.DOCUMENT_NODE:
+ ret = (whatToShow & NodeFilter.SHOW_DOCUMENT) != 0;
+ break;
+ case Node.DOCUMENT_FRAGMENT_NODE:
+ ret = (whatToShow & NodeFilter.SHOW_DOCUMENT_FRAGMENT) != 0;
+ break;
+ case Node.DOCUMENT_TYPE_NODE:
+ ret = (whatToShow & NodeFilter.SHOW_DOCUMENT_TYPE) != 0;
+ break;
+ case Node.ELEMENT_NODE:
+ ret = (whatToShow & NodeFilter.SHOW_ELEMENT) != 0;
+ break;
+ case Node.ENTITY_NODE:
+ ret = (whatToShow & NodeFilter.SHOW_ENTITY) != 0;
+ break;
+ case Node.ENTITY_REFERENCE_NODE:
+ ret = (whatToShow & NodeFilter.SHOW_ENTITY_REFERENCE) != 0;
+ ret = ret && entityReferenceExpansion;
+ break;
+ case Node.NOTATION_NODE:
+ ret = (whatToShow & NodeFilter.SHOW_NOTATION) != 0;
+ break;
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ ret = (whatToShow & NodeFilter.SHOW_PROCESSING_INSTRUCTION) != 0;
+ break;
+ case Node.TEXT_NODE:
+ ret = (whatToShow & NodeFilter.SHOW_TEXT) != 0;
+ break;
+ default:
+ ret = true;
+ }
+ if (ret && filter != null)
+ {
+ ret = (filter.acceptNode(node) == NodeFilter.FILTER_ACCEPT);
+ }
+ return ret;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomNotation.java b/libjava/classpath/gnu/xml/dom/DomNotation.java
new file mode 100644
index 000000000..abb0e9da9
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomNotation.java
@@ -0,0 +1,102 @@
+/* DomNotation.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.Notation;
+
+/**
+ * <p> "Notation" implementation. This is a non-core DOM class, supporting
+ * the "XML" feature. </p>
+ *
+ * <p> Although unparsed entities using this notation can be detected using
+ * DOM, neither NOTATIONS nor ENTITY/ENTITIES attributes can be so detected.
+ * More, there is no portable way to construct a Notation node, so there's
+ * no way that vendor-neutral DOM construction APIs could even report a
+ * NOTATION used to identify the intended meaning of a ProcessingInstruction.
+ * </p>
+ *
+ * <p> In short, <em>avoid using this DOM functionality</em>.
+ *
+ * @see DomDoctype
+ * @see DomEntity
+ * @see DomPI
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomNotation
+ extends DomExtern
+ implements Notation
+{
+
+ /**
+ * Constructs a Notation node associated with the specified document,
+ * with the specified descriptive data. Note that at least one of
+ * the PUBLIC and SYSTEM identifiers must be provided; unlike other
+ * external objects in XML, notations may have only a PUBLIC identifier.
+ *
+ * <p>This constructor should only be invoked by a DomDoctype object
+ * as part of its declareNotation functionality, or through a subclass
+ * which is similarly used in a "Sub-DOM" style layer.
+ *
+ * @param owner The document with which this notation is associated
+ * @param name Name of this notation
+ * @param publicId If non-null, provides the notation's PUBLIC identifier
+ * @param systemId If non-null, rovides the notation's SYSTEM identifier
+ */
+ protected DomNotation(DomDocument owner,
+ String name,
+ String publicId,
+ String systemId)
+ {
+ super(NOTATION_NODE, owner, name, publicId, systemId);
+ makeReadonly();
+ }
+
+ /**
+ * The base URI of an external entity is its system ID.
+ * The base URI of an internal entity is the parent document's base URI.
+ * @since DOM Level 3 Core
+ */
+ public String getBaseURI()
+ {
+ String systemId = getSystemId();
+ return (systemId == null) ? owner.getBaseURI() : systemId;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomNsNode.java b/libjava/classpath/gnu/xml/dom/DomNsNode.java
new file mode 100644
index 000000000..55e351185
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomNsNode.java
@@ -0,0 +1,226 @@
+/* DomNsNode.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import javax.xml.XMLConstants;
+import org.w3c.dom.DOMException;
+
+/**
+ * <p> Abstract implemention of namespace support. This facilitates
+ * sharing code for attribute and element nodes.
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public abstract class DomNsNode
+ extends DomNode
+{
+
+ private String name;
+ private String namespace;
+ private String prefix;
+ private String localName;
+
+ /**
+ * Constructs a node associated with the specified document, and
+ * with the specified namespace information.
+ *
+ * @param owner The document with which this entity is associated
+ * @param namespaceURI Combined with the local part of the name,
+ * this identifies a type of element or attribute; may be null.
+ * If this is the empty string, it is reassigned as null so that
+ * applications only need to test that case.
+ * @param name Name of this node, which may include a prefix
+ */
+ // package private
+ DomNsNode(short nodeType, DomDocument owner, String namespaceURI, String name)
+ {
+ super(nodeType, owner);
+ setNodeName(name);
+ setNamespaceURI(namespaceURI);
+ }
+
+ /**
+ * Constructs a node associated with the specified document, and
+ * with the specified namespace information. The prefix and local part
+ * are given explicitly rather than being computed. This allows them
+ * to be explicitly set to {@code null} as required by
+ * {@link Document#createElement(String)}.
+ *
+ * @param owner The document with which this entity is associated
+ * @param namespaceURI Combined with the local part of the name,
+ * this identifies a type of element or attribute; may be null.
+ * If this is the empty string, it is reassigned as null so that
+ * applications only need to test that case.
+ * @param name Name of this node, which may include a prefix
+ * @param prefix the namespace prefix of the name. May be {@code null}.
+ * @param localName the local part of the name. May be {@code null}.
+ */
+ // package private
+ DomNsNode(short nodeType, DomDocument owner, String namespaceURI, String name,
+ String prefix, String localName)
+ {
+ super(nodeType, owner);
+ this.name = name.intern();
+ this.prefix = prefix == null ? null : prefix.intern();
+ this.localName = localName == null ? null : localName.intern();
+ setNamespaceURI(namespaceURI);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the node's name, including any namespace prefix.
+ */
+ public final String getNodeName()
+ {
+ return name;
+ }
+
+ final void setNodeName(String name)
+ {
+ this.name = name.intern();
+ int index = name.indexOf(':');
+ if (index == -1)
+ {
+ prefix = null;
+ localName = this.name;
+ }
+ else
+ {
+ prefix = name.substring(0, index).intern();
+ localName = name.substring(index + 1).intern();
+ }
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns the node's namespace URI
+ * <em>or null</em> if the node name is not namespace scoped.
+ */
+ public final String getNamespaceURI()
+ {
+ return namespace;
+ }
+
+ final void setNamespaceURI(String namespaceURI)
+ {
+ if ("".equals(namespaceURI))
+ {
+ namespaceURI = null;
+ }
+ namespace = (namespaceURI == null) ? null : namespaceURI.intern();
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns any prefix part of the node's name (before any colon).
+ */
+ public final String getPrefix()
+ {
+ return prefix;
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Assigns the prefix part of the node's name (before any colon).
+ */
+ public final void setPrefix(String prefix)
+ {
+ if (readonly)
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+
+ if (prefix == null)
+ {
+ name = localName;
+ return;
+ }
+ else if (namespace == null)
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "can't set prefix, node has no namespace URI",
+ this, 0);
+ }
+
+ DomDocument.checkName(prefix, "1.1".equals(owner.getXmlVersion()));
+ if (prefix.indexOf (':') != -1)
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "illegal prefix " + prefix, this, 0);
+ }
+
+ if (XMLConstants.XML_NS_PREFIX.equals(prefix)
+ && !XMLConstants.XML_NS_URI.equals(namespace))
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "xml namespace is always " +
+ XMLConstants.XML_NS_URI, this, 0);
+ }
+
+ if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
+ {
+ if (namespace != null || getNodeType() != ATTRIBUTE_NODE)
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "xmlns attribute prefix is reserved",
+ this, 0);
+ }
+ }
+ else if (getNodeType () == ATTRIBUTE_NODE
+ && (XMLConstants.XMLNS_ATTRIBUTE.equals(name) ||
+ name.startsWith("xmlns:")))
+ {
+ throw new DomDOMException(DOMException.NAMESPACE_ERR,
+ "namespace declarations can't change names",
+ this, 0);
+ }
+
+ this.prefix = prefix.intern();
+ }
+
+ /**
+ * <b>DOM L2</b>
+ * Returns the local part of the node's name (after any colon).
+ */
+ public final String getLocalName()
+ {
+ return localName;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomProcessingInstruction.java b/libjava/classpath/gnu/xml/dom/DomProcessingInstruction.java
new file mode 100644
index 000000000..6d443a293
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomProcessingInstruction.java
@@ -0,0 +1,146 @@
+/* DomProcessingInstruction.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.ProcessingInstruction;
+
+/**
+ * <p> "ProcessingInstruction" (PI) implementation.
+ * This is a non-core DOM class, supporting the "XML" feature. </p>
+ *
+ * <p> Unlike other DOM APIs in the "XML" feature, this one fully
+ * exposes the functionality it describes. So there is no reason
+ * inherent in DOM to avoid using this API, unless you want to rely
+ * on NOTATION declarations to associate meaning with your PIs;
+ * there is no vendor-neutal way to record those notations in DOM.</p>
+ *
+ * <p> Also of note is that PI support is part of SAX, so that XML
+ * systems using PIs can choose among multiple APIs. </p>
+ *
+ * @see DomNotation
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomProcessingInstruction
+ extends DomNode
+ implements ProcessingInstruction
+{
+
+ private String target;
+ private String data;
+
+ /**
+ * Constructs a ProcessingInstruction node associated with the
+ * specified document, with the specified data.
+ *
+ * <p>This constructor should only be invoked by a Document object as
+ * part of its createProcessingInstruction functionality, or through
+ * a subclass which is similarly used in a "Sub-DOM" style layer.
+ */
+ protected DomProcessingInstruction(DomDocument owner,
+ String target, String data)
+ {
+ super(PROCESSING_INSTRUCTION_NODE, owner);
+ this.target = target;
+ this.data = data;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the target of the processing instruction.
+ */
+ public final String getTarget()
+ {
+ return target;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the target of the processing instruction
+ * (same as getTarget).
+ */
+ public final String getNodeName()
+ {
+ return target;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the data associated with the processing instruction.
+ */
+ public final String getData()
+ {
+ return data;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the data associated with the processing instruction
+ * (same as getData).
+ */
+ public final String getNodeValue()
+ {
+ return data;
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Assigns the data associated with the processing instruction;
+ * same as setNodeValue.
+ */
+ public final void setData(String data)
+ {
+ setNodeValue(data);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Assigns the data associated with the processing instruction.
+ */
+ public final void setNodeValue(String data)
+ {
+ if (isReadonly())
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+ this.data = data;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomText.java b/libjava/classpath/gnu/xml/dom/DomText.java
new file mode 100644
index 000000000..b74c5b4c2
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomText.java
@@ -0,0 +1,222 @@
+/* DomText.java --
+ Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import gnu.java.lang.CPStringBuilder;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Text;
+
+/**
+ * <p> "Text" implementation. </p>
+ *
+ * @author David Brownell
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomText
+ extends DomCharacterData
+ implements Text
+{
+
+ // NOTE: deleted unused per-instance "isIgnorable"
+ // support to reclaim its space.
+
+ /**
+ * Constructs a text node associated with the specified
+ * document and holding the specified data.
+ *
+ * <p>This constructor should only be invoked by a Document object
+ * as part of its createTextNode functionality, or through a subclass
+ * which is similarly used in a "Sub-DOM" style layer.
+ */
+ protected DomText(DomDocument owner, String value)
+ {
+ super(TEXT_NODE, owner, value);
+ }
+
+ protected DomText(DomDocument owner, char[] buf, int off, int len)
+ {
+ super(TEXT_NODE, owner, buf, off, len);
+ }
+
+ // Used by DomCDATA
+ DomText(short nodeType, DomDocument owner, String value)
+ {
+ super(nodeType, owner, value);
+ }
+
+ DomText(short nodeType, DomDocument owner, char[] buf, int off, int len)
+ {
+ super(nodeType, owner, buf, off, len);
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Returns the string "#text".
+ */
+ // can't be 'final' with CDATA subclassing
+ public String getNodeName()
+ {
+ return "#text";
+ }
+
+ /**
+ * <b>DOM L1</b>
+ * Splits this text node in two parts at the offset, returning
+ * the new text node (the sibling with the second part).
+ */
+ public Text splitText(int offset)
+ {
+ if (isReadonly())
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+ try
+ {
+ String text = getNodeValue();
+ String before = text.substring(0, offset);
+ String after = text.substring(offset);
+ Text next;
+
+ if (getNodeType() == TEXT_NODE)
+ {
+ next = owner.createTextNode(after);
+ }
+ else // CDATA_SECTION_NODE
+ {
+ next = owner.createCDATASection(after);
+ }
+
+ if (this.next != null)
+ {
+ parent.insertBefore(next, this.next);
+ }
+ else
+ {
+ parent.appendChild(next);
+ }
+ setNodeValue(before);
+ return next;
+
+ }
+ catch (IndexOutOfBoundsException x)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ }
+
+ // DOM Level 3
+
+ public boolean isElementContentWhitespace()
+ {
+ if (parent != null)
+ {
+ DomDoctype doctype = (DomDoctype) owner.getDoctype();
+ if (doctype != null)
+ {
+ DTDElementTypeInfo info =
+ doctype.getElementTypeInfo(parent.getNodeName());
+ if (info != null)
+ {
+ if (info.model == null && info.model.indexOf("#PCDATA") != -1)
+ {
+ return false;
+ }
+ return getNodeValue().trim().length() == 0;
+ }
+ }
+ }
+ return false;
+ }
+
+ public String getWholeText()
+ {
+ DomNode ref = this;
+ DomNode ctx;
+ for (ctx = previous; ctx != null &&
+ (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
+ ctx = ctx.previous)
+ {
+ ref = ctx;
+ }
+ CPStringBuilder buf = new CPStringBuilder(ref.getNodeValue());
+ for (ctx = ref.next; ctx != null &&
+ (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
+ ctx = ctx.next)
+ {
+ buf.append(ctx.getNodeValue());
+ }
+ return buf.toString ();
+ }
+
+ public Text replaceWholeText(String content)
+ throws DOMException
+ {
+ boolean isEmpty = (content == null || content.length () == 0);
+ if (!isEmpty)
+ {
+ setNodeValue(content);
+ }
+
+ DomNode ref = this;
+ DomNode ctx;
+ for (ctx = previous; ctx != null &&
+ (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
+ ctx = ctx.previous)
+ {
+ ref = ctx;
+ }
+ ctx = ref.next;
+ if ((isEmpty || ref != this) && parent != null)
+ {
+ parent.removeChild(ref);
+ }
+ for (; ctx != null &&
+ (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
+ ctx = ref)
+ {
+ ref = ctx.next;
+ if ((isEmpty || ctx != this) && parent != null)
+ {
+ parent.removeChild(ctx);
+ }
+ }
+ return (isEmpty) ? null : this;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomXPathExpression.java b/libjava/classpath/gnu/xml/dom/DomXPathExpression.java
new file mode 100644
index 000000000..0b6962930
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomXPathExpression.java
@@ -0,0 +1,147 @@
+/* DomXPathExpression.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+import org.w3c.dom.xpath.XPathException;
+import org.w3c.dom.xpath.XPathNSResolver;
+import org.w3c.dom.xpath.XPathResult;
+import gnu.xml.xpath.DocumentOrderComparator;
+
+/**
+ * An XPath expression.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class DomXPathExpression
+implements org.w3c.dom.xpath.XPathExpression
+{
+
+ final DomDocument doc;
+ final XPathExpression expression;
+ final XPathNSResolver resolver;
+
+ DomXPathExpression(DomDocument doc, String expression,
+ XPathNSResolver resolver)
+ throws XPathException
+ {
+ this.doc = doc;
+ this.resolver = resolver;
+
+ XPathFactory factory = XPathFactory.newInstance();
+ XPath xpath = factory.newXPath();
+ if (resolver != null)
+ {
+ xpath.setNamespaceContext(new DomNSResolverContext(resolver));
+ }
+ try
+ {
+ this.expression = xpath.compile(expression);
+ }
+ catch (XPathExpressionException e)
+ {
+ throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
+ e.getMessage ());
+ }
+ }
+
+ public Object evaluate(Node contextNode, short type, Object result)
+ throws XPathException, DOMException
+ {
+ try
+ {
+ QName typeName = null;
+ switch (type)
+ {
+ case XPathResult.BOOLEAN_TYPE:
+ typeName = XPathConstants.BOOLEAN;
+ break;
+ case XPathResult.NUMBER_TYPE:
+ typeName = XPathConstants.NUMBER;
+ break;
+ case XPathResult.STRING_TYPE:
+ typeName = XPathConstants.STRING;
+ break;
+ case XPathResult.ANY_UNORDERED_NODE_TYPE:
+ case XPathResult.FIRST_ORDERED_NODE_TYPE:
+ typeName = XPathConstants.NODE;
+ break;
+ case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
+ case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
+ typeName = XPathConstants.NODESET;
+ break;
+ default:
+ throw new XPathException(XPathException.TYPE_ERR, null);
+ }
+ Object val = expression.evaluate(contextNode, typeName);
+ switch (type)
+ {
+ case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
+ // Sort the nodes
+ List ns = new ArrayList((Collection) val);
+ Collections.sort(ns, new DocumentOrderComparator());
+ val = ns;
+ }
+ return new DomXPathResult(val, type);
+ }
+ catch (javax.xml.xpath.XPathException e)
+ {
+ throw new XPathException(XPathException.TYPE_ERR, e.getMessage());
+ }
+ }
+
+ public String toString ()
+ {
+ return getClass ().getName () + "[expression=" + expression + "]";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomXPathNSResolver.java b/libjava/classpath/gnu/xml/dom/DomXPathNSResolver.java
new file mode 100644
index 000000000..fb0719a4e
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomXPathNSResolver.java
@@ -0,0 +1,64 @@
+/* DomXPathNSResolver.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.xpath.XPathNSResolver;
+
+/**
+ * Generic XPath namespace resolver using a DOM Node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class DomXPathNSResolver
+implements XPathNSResolver
+{
+
+ Node node;
+
+ DomXPathNSResolver (Node node)
+ {
+ this.node = node;
+ }
+
+ public String lookupNamespaceURI (String prefix)
+ {
+ return node.lookupNamespaceURI (prefix);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomXPathResult.java b/libjava/classpath/gnu/xml/dom/DomXPathResult.java
new file mode 100644
index 000000000..da8fcf513
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomXPathResult.java
@@ -0,0 +1,233 @@
+/* DomXPathResult.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import java.util.Collection;
+import java.util.Iterator;
+import org.w3c.dom.Node;
+import org.w3c.dom.xpath.XPathException;
+import org.w3c.dom.xpath.XPathResult;
+
+/**
+ * An XPath result object.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class DomXPathResult
+implements XPathResult
+{
+
+ final Object value;
+ final short type;
+ Iterator iterator;
+
+ DomXPathResult (Object value, short requestedType)
+ {
+ this.value = value;
+ if (value instanceof Boolean)
+ {
+ type = XPathResult.BOOLEAN_TYPE;
+ }
+ else if (value instanceof Double)
+ {
+ type = XPathResult.NUMBER_TYPE;
+ }
+ else if (value instanceof String)
+ {
+ type = XPathResult.STRING_TYPE;
+ }
+ else if (value instanceof Collection)
+ {
+ Collection ns = (Collection) value;
+ switch (requestedType)
+ {
+ case XPathResult.ANY_TYPE:
+ case XPathResult.ANY_UNORDERED_NODE_TYPE:
+ type = (ns.size () == 1) ? XPathResult.FIRST_ORDERED_NODE_TYPE :
+ XPathResult.ORDERED_NODE_ITERATOR_TYPE;
+ break;
+ default:
+ type = requestedType;
+ }
+ iterator = ns.iterator ();
+ }
+ else
+ {
+ throw new IllegalArgumentException ();
+ }
+ }
+
+ public boolean getBooleanValue()
+ {
+ if (type == XPathResult.BOOLEAN_TYPE)
+ {
+ return ((Boolean) value).booleanValue ();
+ }
+ throw new XPathException (XPathException.TYPE_ERR, value.toString ());
+ }
+
+ public boolean getInvalidIteratorState()
+ {
+ return iterator == null;
+ }
+
+ public double getNumberValue()
+ {
+ if (type == XPathResult.NUMBER_TYPE)
+ {
+ return ((Double) value).doubleValue ();
+ }
+ throw new XPathException (XPathException.TYPE_ERR, value.toString ());
+ }
+
+ public short getResultType()
+ {
+ return type;
+ }
+
+ public Node getSingleNodeValue()
+ {
+ switch (type)
+ {
+ case XPathResult.FIRST_ORDERED_NODE_TYPE:
+ case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
+ case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
+ Collection ns = (Collection) value;
+ if (ns.isEmpty ())
+ {
+ return null;
+ }
+ else
+ {
+ return (Node) ns.iterator ().next ();
+ }
+ }
+ throw new XPathException (XPathException.TYPE_ERR, value.toString ());
+ }
+
+ public int getSnapshotLength()
+ {
+ switch (type)
+ {
+ case XPathResult.FIRST_ORDERED_NODE_TYPE:
+ case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
+ case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
+ return ((Collection) value).size ();
+ }
+ throw new XPathException (XPathException.TYPE_ERR, value.toString ());
+ }
+
+ public String getStringValue()
+ {
+ if (type == XPathResult.STRING_TYPE)
+ {
+ return (String) value;
+ }
+ throw new XPathException (XPathException.TYPE_ERR, value.toString ());
+ }
+
+ public Node iterateNext()
+ {
+ if (iterator != null)
+ {
+ if (iterator.hasNext ())
+ {
+ return (Node) iterator.next ();
+ }
+ else
+ {
+ iterator = null;
+ return null;
+ }
+ }
+ throw new XPathException (XPathException.TYPE_ERR, value.toString ());
+ }
+
+ public Node snapshotItem(int index)
+ {
+ switch (type)
+ {
+ case XPathResult.FIRST_ORDERED_NODE_TYPE:
+ case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
+ case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
+ Collection ns = (Collection) value;
+ Node[] nodes = new Node[ns.size ()];
+ ns.toArray (nodes);
+ return nodes[index];
+ }
+ throw new XPathException (XPathException.TYPE_ERR, value.toString ());
+ }
+
+ public String toString ()
+ {
+ return getClass ().getName () + "[type=" + typeName (type) + ",value=" +
+ value + ']';
+ }
+
+ private String typeName (short type)
+ {
+ switch (type)
+ {
+ case XPathResult.BOOLEAN_TYPE:
+ return "BOOLEAN_TYPE";
+ case XPathResult.NUMBER_TYPE:
+ return "NUMBER_TYPE";
+ case XPathResult.STRING_TYPE:
+ return "STRING_TYPE";
+ case XPathResult.FIRST_ORDERED_NODE_TYPE:
+ return "FIRST_ORDERED_NODE_TYPE";
+ case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
+ return "ORDERED_NODE_ITERATOR_TYPE";
+ case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
+ return "ORDERED_NODE_SNAPSHOT_TYPE";
+ case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
+ return "UNORDERED_NODE_ITERATOR_TYPE";
+ case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
+ return "UNORDERED_NODE_SNAPSHOT_TYPE";
+ default:
+ return "(unknown)";
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ImplementationList.java b/libjava/classpath/gnu/xml/dom/ImplementationList.java
new file mode 100644
index 000000000..c99f55d0d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ImplementationList.java
@@ -0,0 +1,70 @@
+/* ImplementationList.java --
+ Copyright (C) 2004 Free Software Foundation, Inc..
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+package gnu.xml.dom;
+
+import java.util.List;
+
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.DOMImplementationList;
+
+/**
+ * Implementation list for GNU JAXP.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class ImplementationList
+ implements DOMImplementationList
+{
+
+ private List list;
+
+ ImplementationList(List list)
+ {
+ this.list = list;
+ }
+
+ public int getLength()
+ {
+ return list.size();
+ }
+
+ public DOMImplementation item(int index)
+ {
+ return (DOMImplementation) list.get(index);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ImplementationSource.java b/libjava/classpath/gnu/xml/dom/ImplementationSource.java
new file mode 100644
index 000000000..5f404423a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ImplementationSource.java
@@ -0,0 +1,167 @@
+/* ImplementationSource.java --
+ Copyright (C) 2004 Free Software Foundation, Inc..
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+package gnu.xml.dom;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.DOMImplementationList;
+import org.w3c.dom.DOMImplementationSource;
+
+/**
+ * Implementation source for GNU JAXP.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class ImplementationSource
+ implements DOMImplementationSource
+{
+
+ private static final String DIGITS = "1234567890";
+
+ /*
+ * GNU DOM implementations.
+ */
+ private static final DOMImplementation[] implementations;
+
+ static
+ {
+ List acc = new ArrayList();
+ acc.add(new gnu.xml.dom.DomImpl());
+ try
+ {
+ Class t = Class.forName("gnu.xml.libxmlj.dom.GnomeDocumentBuilder");
+ acc.add(t.newInstance());
+ }
+ catch (Exception e)
+ {
+ // libxmlj not available
+ }
+ catch (UnsatisfiedLinkError e)
+ {
+ // libxmlj not available
+ }
+ implementations = new DOMImplementation[acc.size()];
+ acc.toArray(implementations);
+ }
+
+ public DOMImplementation getDOMImplementation(String features)
+ {
+ List available = getImplementations(features);
+ if (available.isEmpty())
+ {
+ return null;
+ }
+ return (DOMImplementation) available.get(0);
+ }
+
+ public DOMImplementationList getDOMImplementationList(String features)
+ {
+ List available = getImplementations(features);
+ return new ImplementationList(available);
+ }
+
+ /**
+ * Returns a list of the implementations that support the specified
+ * features.
+ */
+ private final List getImplementations(String features)
+ {
+ List available = new ArrayList(Arrays.asList(implementations));
+ for (Iterator i = parseFeatures(features).iterator(); i.hasNext(); )
+ {
+ String feature = (String) i.next();
+ String version = null;
+ int si = feature.indexOf(' ');
+ if (si != -1)
+ {
+ version = feature.substring(si + 1);
+ feature = feature.substring(0, si);
+ }
+ for (Iterator j = available.iterator(); j.hasNext(); )
+ {
+ DOMImplementation impl = (DOMImplementation) j.next();
+ if (!impl.hasFeature(feature, version))
+ {
+ j.remove();
+ }
+ }
+ }
+ return available;
+ }
+
+ /**
+ * Parses the feature list into feature tokens.
+ */
+ final List parseFeatures(String features)
+ {
+ List list = new ArrayList();
+ int pos = 0, start = 0;
+ int len = features.length();
+ for (; pos < len; pos++)
+ {
+ char c = features.charAt(pos);
+ if (c == ' ')
+ {
+ if (pos + 1 < len &&
+ DIGITS.indexOf(features.charAt(pos + 1)) == -1)
+ {
+ list.add(getFeature(features, start, pos));
+ start = pos + 1;
+ }
+ }
+ }
+ if (pos > start)
+ {
+ list.add(getFeature(features, start, len));
+ }
+ return list;
+ }
+
+ final String getFeature(String features, int start, int end)
+ {
+ if (features.length() > 0 && features.charAt(start) == '+')
+ {
+ return features.substring(start + 1, end);
+ }
+ return features.substring(start, end);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/JAXPFactory.java b/libjava/classpath/gnu/xml/dom/JAXPFactory.java
new file mode 100644
index 000000000..d7d399b5b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/JAXPFactory.java
@@ -0,0 +1,308 @@
+/* JAXPFactory.java --
+ Copyright (C) 2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import java.io.IOException;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.DOMImplementation;
+
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+import javax.xml.XMLConstants;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParserFactory;
+
+
+/**
+ * DOM bootstrapping API, for use with JAXP.
+ *
+ * @see Consumer
+ *
+ * @author David Brownell
+ */
+public final class JAXPFactory
+ extends DocumentBuilderFactory
+{
+
+ private static final String PROPERTY = "http://xml.org/sax/properties/";
+ private static final String FEATURE = "http://xml.org/sax/features/";
+
+ private SAXParserFactory pf;
+ private boolean secureProcessing;
+
+ /**
+ * Default constructor.
+ */
+ public JAXPFactory()
+ {
+ }
+
+ /**
+ * Constructs a JAXP document builder which uses the default
+ * JAXP SAX2 parser and the DOM implementation in this package.
+ */
+ public DocumentBuilder newDocumentBuilder()
+ throws ParserConfigurationException
+ {
+ if (pf == null)
+ {
+ // Force use of AElfred2 since not all JAXP parsers
+ // conform very well to the SAX2 API spec ...
+ pf = new gnu.xml.aelfred2.JAXPFactory();
+ // pf = SAXParserFactory.newInstance ();
+ }
+
+ // JAXP default: false
+ pf.setValidating(isValidating());
+
+ // FIXME: this namespace setup may cause errors in some
+ // conformant SAX2 parsers, which we CAN patch up by
+ // splicing a "NSFilter" stage up front ...
+
+ // JAXP default: false
+ pf.setNamespaceAware(isNamespaceAware());
+
+ try
+ {
+ // undo rude "namespace-prefixes=false" default
+ pf.setFeature(FEATURE + "namespace-prefixes", true);
+
+ return new JAXPBuilder(pf.newSAXParser().getXMLReader(), this);
+ }
+ catch (SAXException e)
+ {
+ String msg = "can't create JAXP DocumentBuilder: " + e.getMessage();
+ throw new ParserConfigurationException(msg);
+ }
+ }
+
+ /** There seems to be no useful specification for attribute names */
+ public void setAttribute(String name, Object value)
+ throws IllegalArgumentException
+ {
+ if ("http://java.sun.com/xml/jaxp/properties/schemaLanguage".equals(name))
+ {
+ // TODO
+ }
+ else
+ {
+ throw new IllegalArgumentException(name);
+ }
+ }
+
+ /** There seems to be no useful specification for attribute names */
+ public Object getAttribute(String name)
+ throws IllegalArgumentException
+ {
+ throw new IllegalArgumentException(name);
+ }
+
+ public void setFeature(String name, boolean value)
+ throws ParserConfigurationException
+ {
+ if (name == null)
+ throw new NullPointerException();
+ if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name))
+ {
+ secureProcessing = true;
+ return;
+ }
+ throw new ParserConfigurationException(name);
+ }
+
+ public boolean getFeature(String name)
+ throws ParserConfigurationException
+ {
+ if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name))
+ return secureProcessing;
+ throw new ParserConfigurationException(name);
+ }
+
+ static final class JAXPBuilder
+ extends DocumentBuilder
+ implements ErrorHandler
+ {
+
+ private Consumer consumer;
+ private XMLReader producer;
+ private DomImpl impl;
+
+ JAXPBuilder(XMLReader parser, JAXPFactory factory)
+ throws ParserConfigurationException
+ {
+ impl = new DomImpl();
+
+ // set up consumer side
+ try
+ {
+ consumer = new Consumer();
+ }
+ catch (SAXException e)
+ {
+ throw new ParserConfigurationException(e.getMessage());
+ }
+
+ // JAXP defaults: true, noise nodes are good (bleech)
+ consumer.setHidingReferences(factory.isExpandEntityReferences());
+ consumer.setHidingComments(factory.isIgnoringComments());
+ consumer.setHidingWhitespace(factory.isIgnoringElementContentWhitespace());
+ consumer.setHidingCDATA(factory.isCoalescing());
+
+ // set up producer side
+ producer = parser;
+ producer.setContentHandler(consumer.getContentHandler());
+ producer.setDTDHandler(consumer.getDTDHandler());
+
+ try
+ {
+ String id;
+
+ // if validating, report validity errors, and default
+ // to treating them as fatal
+ if (factory.isValidating ())
+ {
+ producer.setFeature(FEATURE + "validation", true);
+ producer.setErrorHandler(this);
+ }
+
+ // always save prefix info, maybe do namespace processing
+ producer.setFeature(FEATURE + "namespace-prefixes", true);
+ producer.setFeature(FEATURE + "namespaces",
+ factory.isNamespaceAware());
+
+ // set important handlers
+ id = PROPERTY + "lexical-handler";
+ producer.setProperty(id, consumer.getProperty(id));
+
+ id = PROPERTY + "declaration-handler";
+ producer.setProperty(id, consumer.getProperty(id));
+
+ }
+ catch (SAXException e)
+ {
+ throw new ParserConfigurationException(e.getMessage());
+ }
+ }
+
+ public Document parse(InputSource source)
+ throws SAXException, IOException
+ {
+ producer.parse(source);
+ Document doc = consumer.getDocument();
+ // TODO inputEncoding
+ doc.setDocumentURI(source.getSystemId());
+ return doc;
+ }
+
+ public boolean isNamespaceAware()
+ {
+ try
+ {
+ return producer.getFeature(FEATURE + "namespaces");
+ }
+ catch (SAXException e)
+ {
+ // "can't happen"
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+
+ public boolean isValidating()
+ {
+ try
+ {
+ return producer.getFeature(FEATURE + "validation");
+ }
+ catch (SAXException e)
+ {
+ // "can't happen"
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+
+ public void setEntityResolver(EntityResolver resolver)
+ {
+ producer.setEntityResolver(resolver);
+ }
+
+ public void setErrorHandler(ErrorHandler handler)
+ {
+ producer.setErrorHandler(handler);
+ consumer.setErrorHandler(handler);
+ }
+
+ public DOMImplementation getDOMImplementation()
+ {
+ return impl;
+ }
+
+ public Document newDocument()
+ {
+ return new DomDocument();
+ }
+
+ // implementation of error handler that's used when validating
+ public void fatalError(SAXParseException e)
+ throws SAXException
+ {
+ throw e;
+ }
+
+ public void error(SAXParseException e)
+ throws SAXException
+ {
+ throw e;
+ }
+
+ public void warning(SAXParseException e)
+ throws SAXException
+ {
+ /* ignore */
+ }
+
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLAnchorElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAnchorElement.java
new file mode 100644
index 000000000..4448dd580
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAnchorElement.java
@@ -0,0 +1,188 @@
+/* DomHTMLAnchorElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLAnchorElement;
+
+/**
+ * An HTML 'A' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLAnchorElement
+ extends DomHTMLElement
+ implements HTMLAnchorElement
+{
+
+ protected DomHTMLAnchorElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAccessKey()
+ {
+ return getHTMLAttribute("accesskey");
+ }
+
+ public void setAccessKey(String accessKey)
+ {
+ setHTMLAttribute("accesskey", accessKey);
+ }
+
+ public String getCharset()
+ {
+ return getHTMLAttribute("charset");
+ }
+
+ public void setCharset(String charset)
+ {
+ setHTMLAttribute("charset", charset);
+ }
+
+ public String getCoords()
+ {
+ return getHTMLAttribute("coords");
+ }
+
+ public void setCoords(String coords)
+ {
+ setHTMLAttribute("coords", coords);
+ }
+
+ public String getHref()
+ {
+ return getHTMLAttribute("href");
+ }
+
+ public void setHref(String href)
+ {
+ setHTMLAttribute("href", href);
+ }
+
+ public String getHreflang()
+ {
+ return getHTMLAttribute("hreflang");
+ }
+
+ public void setHreflang(String hreflang)
+ {
+ setHTMLAttribute("hreflang", hreflang);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getRel()
+ {
+ return getHTMLAttribute("rel");
+ }
+
+ public void setRel(String rel)
+ {
+ setHTMLAttribute("rel", rel);
+ }
+
+ public String getRev()
+ {
+ return getHTMLAttribute("rev");
+ }
+
+ public void setRev(String rev)
+ {
+ setHTMLAttribute("rev", rev);
+ }
+
+ public String getShape()
+ {
+ return getHTMLAttribute("shape");
+ }
+
+ public void setShape(String shape)
+ {
+ setHTMLAttribute("shape", shape);
+ }
+
+ public int getTabIndex()
+ {
+ return getIntHTMLAttribute("tabindex");
+ }
+
+ public void setTabIndex(int tabIndex)
+ {
+ setIntHTMLAttribute("tabindex", tabIndex);
+ }
+
+ public String getTarget()
+ {
+ return getHTMLAttribute("target");
+ }
+
+ public void setTarget(String target)
+ {
+ setHTMLAttribute("target", target);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+ public void blur()
+ {
+ dispatchUIEvent("blur");
+ }
+
+ public void focus()
+ {
+ dispatchUIEvent("focus");
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLAppletElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAppletElement.java
new file mode 100644
index 000000000..429fb0038
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAppletElement.java
@@ -0,0 +1,187 @@
+/* DomHTMLAppletElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLAppletElement;
+
+/**
+ * An HTML 'APPLET' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLAppletElement
+ extends DomHTMLElement
+ implements HTMLAppletElement
+{
+
+ protected DomHTMLAppletElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getCls()
+ {
+ return getHTMLAttribute("class");
+ }
+
+ public void setCls(String cls)
+ {
+ setHTMLAttribute("class", cls);
+ }
+
+ public String getSrc()
+ {
+ return getHTMLAttribute("src");
+ }
+
+ public void setSrc(String src)
+ {
+ setHTMLAttribute("src", src);
+ }
+
+ public String getAlt()
+ {
+ return getHTMLAttribute("alt");
+ }
+
+ public void setAlt(String alt)
+ {
+ setHTMLAttribute("alt", alt);
+ }
+
+ public String getArchive()
+ {
+ return getHTMLAttribute("archive");
+ }
+
+ public void setArchive(String archive)
+ {
+ setHTMLAttribute("archive", archive);
+ }
+
+ public String getCode()
+ {
+ return getHTMLAttribute("code");
+ }
+
+ public void setCode(String code)
+ {
+ setHTMLAttribute("code", code);
+ }
+
+ public String getCodeBase()
+ {
+ return getHTMLAttribute("codebase");
+ }
+
+ public void setCodeBase(String codeBase)
+ {
+ setHTMLAttribute("codebase", codeBase);
+ }
+
+ public String getHeight()
+ {
+ return getHTMLAttribute("height");
+ }
+
+ public void setHeight(String height)
+ {
+ setHTMLAttribute("height", height);
+ }
+
+ public int getHspace()
+ {
+ return getIntHTMLAttribute("hspace");
+ }
+
+ public void setHspace(int hspace)
+ {
+ setIntHTMLAttribute("hspace", hspace);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getObject()
+ {
+ return getHTMLAttribute("object");
+ }
+
+ public void setObject(String object)
+ {
+ setHTMLAttribute("object", object);
+ }
+
+ public int getVspace()
+ {
+ return getIntHTMLAttribute("vspace");
+ }
+
+ public void setVspace(int vspace)
+ {
+ setIntHTMLAttribute("vspace", vspace);
+ }
+
+ public String getWidth()
+ {
+ return getHTMLAttribute("width");
+ }
+
+ public void setWidth(String width)
+ {
+ setHTMLAttribute("width", width);
+ }
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLAreaElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAreaElement.java
new file mode 100644
index 000000000..728b3fdfc
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAreaElement.java
@@ -0,0 +1,138 @@
+/* DomHTMLAreaElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLAreaElement;
+
+/**
+ * An HTML 'AREA' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLAreaElement
+ extends DomHTMLElement
+ implements HTMLAreaElement
+{
+
+ protected DomHTMLAreaElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAccessKey()
+ {
+ return getHTMLAttribute("accesskey");
+ }
+
+ public void setAccessKey(String accessKey)
+ {
+ setHTMLAttribute("accesskey", accessKey);
+ }
+
+ public String getAlt()
+ {
+ return getHTMLAttribute("alt");
+ }
+
+ public void setAlt(String alt)
+ {
+ setHTMLAttribute("alt", alt);
+ }
+
+ public String getCoords()
+ {
+ return getHTMLAttribute("coords");
+ }
+
+ public void setCoords(String coords)
+ {
+ setHTMLAttribute("coords", coords);
+ }
+
+ public String getHref()
+ {
+ return getHTMLAttribute("href");
+ }
+
+ public void setHref(String href)
+ {
+ setHTMLAttribute("href", href);
+ }
+
+ public boolean getNoHref()
+ {
+ return getBooleanHTMLAttribute("nohref");
+ }
+
+ public void setNoHref(boolean nohref)
+ {
+ setBooleanHTMLAttribute("nohref", nohref);
+ }
+
+ public String getShape()
+ {
+ return getHTMLAttribute("shape");
+ }
+
+ public void setShape(String shape)
+ {
+ setHTMLAttribute("shape", shape);
+ }
+
+ public int getTabIndex()
+ {
+ return getIntHTMLAttribute("tabindex");
+ }
+
+ public void setTabIndex(int tabIndex)
+ {
+ setIntHTMLAttribute("tabindex", tabIndex);
+ }
+
+ public String getTarget()
+ {
+ return getHTMLAttribute("target");
+ }
+
+ public void setTarget(String target)
+ {
+ setHTMLAttribute("target", target);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLBRElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBRElement.java
new file mode 100644
index 000000000..5ad49c549
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBRElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLBRElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLBRElement;
+
+/**
+ * An HTML 'BR' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLBRElement
+ extends DomHTMLElement
+ implements HTMLBRElement
+{
+
+ protected DomHTMLBRElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getClear()
+ {
+ return getHTMLAttribute("clear");
+ }
+
+ public void setClear(String clear)
+ {
+ setHTMLAttribute("clear", clear);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseElement.java
new file mode 100644
index 000000000..99a0993ba
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseElement.java
@@ -0,0 +1,78 @@
+/* DomHTMLBaseElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLBaseElement;
+
+/**
+ * An HTML 'BASE' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLBaseElement
+ extends DomHTMLElement
+ implements HTMLBaseElement
+{
+
+ protected DomHTMLBaseElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getHref()
+ {
+ return getHTMLAttribute("href");
+ }
+
+ public void setHref(String href)
+ {
+ setHTMLAttribute("href", href);
+ }
+
+ public String getTarget()
+ {
+ return getHTMLAttribute("target");
+ }
+
+ public void setTarget(String target)
+ {
+ setHTMLAttribute("target", target);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseFontElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseFontElement.java
new file mode 100644
index 000000000..73ec96dbd
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseFontElement.java
@@ -0,0 +1,88 @@
+/* DomHTMLBaseFontElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLBaseFontElement;
+
+/**
+ * An HTML 'BASEFONT' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLBaseFontElement
+ extends DomHTMLElement
+ implements HTMLBaseFontElement
+{
+
+ protected DomHTMLBaseFontElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getColor()
+ {
+ return getHTMLAttribute("color");
+ }
+
+ public void setColor(String color)
+ {
+ setHTMLAttribute("color", color);
+ }
+
+ public String getFace()
+ {
+ return getHTMLAttribute("face");
+ }
+
+ public void setFace(String face)
+ {
+ setHTMLAttribute("face", face);
+ }
+
+ public int getSize()
+ {
+ return getIntHTMLAttribute("size");
+ }
+
+ public void setSize(int size)
+ {
+ setIntHTMLAttribute("size", size);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLBodyElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBodyElement.java
new file mode 100644
index 000000000..d4528625b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBodyElement.java
@@ -0,0 +1,118 @@
+/* DomHTMLBodyElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLBodyElement;
+
+/**
+ * An HTML 'BODY' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLBodyElement
+ extends DomHTMLElement
+ implements HTMLBodyElement
+{
+
+ protected DomHTMLBodyElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getALink()
+ {
+ return getHTMLAttribute("alink");
+ }
+
+ public void setALink(String alink)
+ {
+ setHTMLAttribute("alink", alink);
+ }
+
+ public String getBackground()
+ {
+ return getHTMLAttribute("background");
+ }
+
+ public void setBackground(String background)
+ {
+ setHTMLAttribute("background", background);
+ }
+
+ public String getBgColor()
+ {
+ return getHTMLAttribute("bgcolor");
+ }
+
+ public void setBgColor(String bgcolor)
+ {
+ setHTMLAttribute("bgcolor", bgcolor);
+ }
+
+ public String getLink()
+ {
+ return getHTMLAttribute("link");
+ }
+
+ public void setLink(String link)
+ {
+ setHTMLAttribute("link", link);
+ }
+
+ public String getText()
+ {
+ return getHTMLAttribute("text");
+ }
+
+ public void setText(String text)
+ {
+ setHTMLAttribute("text", text);
+ }
+
+ public String getVLink()
+ {
+ return getHTMLAttribute("vlink");
+ }
+
+ public void setVLink(String vlink)
+ {
+ setHTMLAttribute("vlink", vlink);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLButtonElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLButtonElement.java
new file mode 100644
index 000000000..696169ac2
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLButtonElement.java
@@ -0,0 +1,119 @@
+/* DomHTMLButtonElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLButtonElement;
+import org.w3c.dom.html2.HTMLFormElement;
+
+/**
+ * An HTML 'BUTTON' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLButtonElement
+ extends DomHTMLElement
+ implements HTMLButtonElement
+{
+
+ protected DomHTMLButtonElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public String getAccessKey()
+ {
+ return getHTMLAttribute("accesskey");
+ }
+
+ public void setAccessKey(String accessKey)
+ {
+ setHTMLAttribute("accesskey", accessKey);
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public int getTabIndex()
+ {
+ return getIntHTMLAttribute("tabindex");
+ }
+
+ public void setTabIndex(int tabIndex)
+ {
+ setIntHTMLAttribute("tabindex", tabIndex);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public String getValue()
+ {
+ return getHTMLAttribute("value");
+ }
+
+ public void setValue(String value)
+ {
+ setHTMLAttribute("value", value);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLCollection.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLCollection.java
new file mode 100644
index 000000000..830028753
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLCollection.java
@@ -0,0 +1,225 @@
+/* DomHTMLCollection.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomDOMException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.html2.HTMLCollection;
+import org.w3c.dom.html2.HTMLOptionsCollection;
+import org.w3c.dom.traversal.NodeFilter;
+import org.w3c.dom.traversal.NodeIterator;
+
+/**
+ * An HTML element collection.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class DomHTMLCollection
+ implements HTMLCollection, HTMLOptionsCollection, NodeList, NodeFilter
+{
+
+ final DomHTMLDocument doc;
+ final Node root;
+ List nodeNames;
+ List attributeNames;
+ List results;
+
+ DomHTMLCollection(DomHTMLDocument doc, Node root)
+ {
+ this.doc = doc;
+ this.root = root;
+ }
+
+ // -- Node name and attribute filtering --
+
+ void addNodeName(String name)
+ {
+ if (nodeNames == null)
+ {
+ nodeNames = new LinkedList();
+ }
+ nodeNames.add(name);
+ }
+
+ void addAttributeName(String name)
+ {
+ if (attributeNames == null)
+ {
+ attributeNames = new LinkedList();
+ }
+ attributeNames.add(name);
+ }
+
+ public short acceptNode(Node n)
+ {
+ if (n.getNodeType() != Node.ELEMENT_NODE)
+ {
+ return NodeFilter.FILTER_SKIP;
+ }
+ String localName = n.getLocalName();
+ if (localName == null)
+ {
+ localName = n.getNodeName();
+ }
+ if (nodeNames != null && !acceptName(localName))
+ {
+ return NodeFilter.FILTER_SKIP;
+ }
+ if (attributeNames != null && !acceptAttributes(n.getAttributes()))
+ {
+ return NodeFilter.FILTER_SKIP;
+ }
+ return NodeFilter.FILTER_ACCEPT;
+ }
+
+ private boolean acceptName(String name)
+ {
+ for (Iterator i = nodeNames.iterator(); i.hasNext(); )
+ {
+ String nodeName = (String) i.next();
+ if (nodeName.equalsIgnoreCase(name))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private boolean acceptAttributes(NamedNodeMap attrs)
+ {
+ for (Iterator i = attributeNames.iterator(); i.hasNext(); )
+ {
+ String attributeName = (String) i.next();
+ Node attr = getNamedItem(attrs, attributeName);
+ if (attr != null)
+ {
+ // Check that attribute has a non-null value
+ String nodeValue = attr.getNodeValue();
+ if (nodeValue != null && nodeValue.length() > 0)
+ {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Case-insensitive version of getNamedItem.
+ */
+ private Node getNamedItem(NamedNodeMap attrs, String name)
+ {
+ int len = attrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Node attr = attrs.item(i);
+ String attrName = attr.getLocalName();
+ if (attrName == null)
+ {
+ attrName = attr.getNodeName();
+ }
+ if (name.equalsIgnoreCase(attrName))
+ {
+ return attr;
+ }
+ }
+ return null;
+ }
+
+ // -- Perform query --
+
+ void evaluate()
+ {
+ NodeIterator i = doc.createNodeIterator(root, NodeFilter.SHOW_ELEMENT,
+ this, true);
+ results = new ArrayList();
+ for (Node node = i.nextNode(); node != null; node = i.nextNode())
+ {
+ results.add(node);
+ }
+ }
+
+ // -- HTMLCollection/NodeList interface --
+
+ public int getLength()
+ {
+ return results.size();
+ }
+
+ public void setLength(int length)
+ {
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
+ }
+
+ public Node item(int index)
+ {
+ return (Node) results.get(index);
+ }
+
+ public Node namedItem(String name)
+ {
+ boolean xhtml = false; // FIXME detect XHTML document
+ for (Iterator i = results.iterator(); i.hasNext(); )
+ {
+ Node node = (Node) i.next();
+ NamedNodeMap attrs = node.getAttributes();
+ Node attr = getNamedItem(attrs, "id");
+ if (name.equals(attr.getTextContent()))
+ {
+ return node;
+ }
+ if (!xhtml)
+ {
+ attr = getNamedItem(attrs, "name");
+ if (name.equals(attr.getTextContent()))
+ {
+ return node;
+ }
+ }
+ }
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLDListElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDListElement.java
new file mode 100644
index 000000000..9acce856a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDListElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLDListElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLDListElement;
+
+/**
+ * An HTML 'DL' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLDListElement
+ extends DomHTMLElement
+ implements HTMLDListElement
+{
+
+ protected DomHTMLDListElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getCompact()
+ {
+ return getBooleanHTMLAttribute("compact");
+ }
+
+ public void setCompact(boolean compact)
+ {
+ setBooleanHTMLAttribute("compact", compact);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLDirectoryElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDirectoryElement.java
new file mode 100644
index 000000000..e0f94a1ed
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDirectoryElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLDirectoryElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLDirectoryElement;
+
+/**
+ * An HTML 'DIR' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLDirectoryElement
+ extends DomHTMLElement
+ implements HTMLDirectoryElement
+{
+
+ protected DomHTMLDirectoryElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getCompact()
+ {
+ return getBooleanHTMLAttribute("compact");
+ }
+
+ public void setCompact(boolean compact)
+ {
+ setBooleanHTMLAttribute("compact", compact);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLDivElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDivElement.java
new file mode 100644
index 000000000..66b3ac743
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDivElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLDivElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLDivElement;
+
+/**
+ * An HTML 'DIV' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLDivElement
+ extends DomHTMLElement
+ implements HTMLDivElement
+{
+
+ protected DomHTMLDivElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLDocument.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDocument.java
new file mode 100644
index 000000000..1afed7beb
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDocument.java
@@ -0,0 +1,426 @@
+/* DomHTMLDocument.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomDocument;
+import gnu.xml.dom.DomDOMException;
+import java.lang.reflect.Constructor;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.html2.HTMLCollection;
+import org.w3c.dom.html2.HTMLDocument;
+import org.w3c.dom.html2.HTMLElement;
+
+/**
+ * An HTML document.
+ * This is the factory object used to create HTML elements.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLDocument
+ extends DomDocument
+ implements HTMLDocument
+{
+
+ private static final Class[] ELEMENT_PT = new Class[] {
+ DomHTMLDocument.class,
+ String.class,
+ String.class
+ };
+
+ private static Map ELEMENT_CLASSES;
+ static
+ {
+ Map map = new HashMap();
+ map.put("a", DomHTMLAnchorElement.class);
+ map.put("applet", DomHTMLAppletElement.class);
+ map.put("area", DomHTMLAreaElement.class);
+ map.put("base", DomHTMLBaseElement.class);
+ map.put("basefont", DomHTMLBaseFontElement.class);
+ map.put("body", DomHTMLBodyElement.class);
+ map.put("br", DomHTMLBRElement.class);
+ map.put("button", DomHTMLButtonElement.class);
+ map.put("dir", DomHTMLDirectoryElement.class);
+ map.put("div", DomHTMLDivElement.class);
+ map.put("dlist", DomHTMLDListElement.class);
+ map.put("embed", DomHTMLEmbedElement.class);
+ map.put("fieldset", DomHTMLFieldSetElement.class);
+ map.put("font", DomHTMLFontElement.class);
+ map.put("form", DomHTMLFormElement.class);
+ map.put("frame", DomHTMLFrameElement.class);
+ map.put("frameset", DomHTMLFrameSetElement.class);
+ map.put("head", DomHTMLHeadElement.class);
+ map.put("h1", DomHTMLHeadingElement.class);
+ map.put("h2", DomHTMLHeadingElement.class);
+ map.put("h3", DomHTMLHeadingElement.class);
+ map.put("h4", DomHTMLHeadingElement.class);
+ map.put("h5", DomHTMLHeadingElement.class);
+ map.put("h6", DomHTMLHeadingElement.class);
+ map.put("html", DomHTMLHtmlElement.class);
+ map.put("iframe", DomHTMLIFrameElement.class);
+ map.put("img", DomHTMLImageElement.class);
+ map.put("input", DomHTMLInputElement.class);
+ map.put("isindex", DomHTMLIsIndexElement.class);
+ map.put("label", DomHTMLLabelElement.class);
+ map.put("legend", DomHTMLLegendElement.class);
+ map.put("li", DomHTMLLIElement.class);
+ map.put("link", DomHTMLLinkElement.class);
+ map.put("map", DomHTMLMapElement.class);
+ map.put("menu", DomHTMLMenuElement.class);
+ map.put("meta", DomHTMLMetaElement.class);
+ map.put("ins", DomHTMLModElement.class);
+ map.put("del", DomHTMLModElement.class);
+ map.put("object", DomHTMLObjectElement.class);
+ map.put("ol", DomHTMLOListElement.class);
+ map.put("optgroup", DomHTMLOptGroupElement.class);
+ map.put("option", DomHTMLOptionElement.class);
+ map.put("p", DomHTMLParagraphElement.class);
+ map.put("param", DomHTMLParamElement.class);
+ map.put("pre", DomHTMLPreElement.class);
+ map.put("q", DomHTMLQuoteElement.class);
+ map.put("blockquote", DomHTMLQuoteElement.class);
+ map.put("script", DomHTMLScriptElement.class);
+ map.put("select", DomHTMLSelectElement.class);
+ map.put("style", DomHTMLStyleElement.class);
+ map.put("caption", DomHTMLTableCaptionElement.class);
+ map.put("th", DomHTMLTableCellElement.class);
+ map.put("td", DomHTMLTableCellElement.class);
+ map.put("col", DomHTMLTableColElement.class);
+ map.put("colgroup", DomHTMLTableColElement.class);
+ map.put("table", DomHTMLTableElement.class);
+ map.put("tr", DomHTMLTableRowElement.class);
+ map.put("thead", DomHTMLTableSectionElement.class);
+ map.put("tfoot", DomHTMLTableSectionElement.class);
+ map.put("tbody", DomHTMLTableSectionElement.class);
+ map.put("textarea", DomHTMLTextAreaElement.class);
+ map.put("title", DomHTMLTitleElement.class);
+ map.put("ul", DomHTMLUListElement.class);
+ ELEMENT_CLASSES = Collections.unmodifiableMap(map);
+ }
+
+ private static Set HTML_NS_URIS;
+ static
+ {
+ Set set = new HashSet();
+ set.add("http://www.w3.org/TR/html4/strict");
+ set.add("http://www.w3.org/TR/html4/loose");
+ set.add("http://www.w3.org/TR/html4/frameset");
+ set.add("http://www.w3.org/1999/xhtml");
+ set.add("http://www.w3.org/TR/xhtml1/strict");
+ set.add("http://www.w3.org/TR/xhtml1/loose");
+ set.add("http://www.w3.org/TR/xhtml1/frameset");
+ HTML_NS_URIS = Collections.unmodifiableSet(set);
+ }
+
+ /**
+ * Convenience constructor.
+ */
+ public DomHTMLDocument()
+ {
+ this(new DomHTMLImpl());
+ }
+
+ /**
+ * Constructor.
+ * This is called by the DOMImplementation.
+ */
+ public DomHTMLDocument(DomHTMLImpl impl)
+ {
+ super(impl);
+ }
+
+ private Node getChildNodeByName(Node parent, String name)
+ {
+ for (Node ctx = parent.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if (name.equalsIgnoreCase(ctx.getNodeName()))
+ {
+ return ctx;
+ }
+ }
+ return null;
+ }
+
+ public String getTitle()
+ {
+ Node html = getDocumentElement();
+ if (html != null)
+ {
+ Node head = getChildNodeByName(html, "head");
+ if (head != null)
+ {
+ Node title = getChildNodeByName(head, "title");
+ if (title != null)
+ {
+ return title.getTextContent();
+ }
+ }
+ }
+ return null;
+ }
+
+ public void setTitle(String title)
+ {
+ Node html = getDocumentElement();
+ if (html == null)
+ {
+ html = createElement("html");
+ appendChild(html);
+ }
+ Node head = getChildNodeByName(html, "head");
+ if (head == null)
+ {
+ head = createElement("head");
+ Node first = html.getFirstChild();
+ if (first != null)
+ {
+ html.insertBefore(first, head);
+ }
+ else
+ {
+ html.appendChild(head);
+ }
+ }
+ Node titleNode = getChildNodeByName(head, "title");
+ if (titleNode == null)
+ {
+ titleNode = createElement("title");
+ Node first = head.getFirstChild();
+ if (first != null)
+ {
+ head.insertBefore(first, titleNode);
+ }
+ else
+ {
+ head.appendChild(titleNode);
+ }
+ }
+ titleNode.setTextContent(title);
+ }
+
+ public String getReferrer()
+ {
+ // TODO getReferrer
+ return null;
+ }
+
+ public String getDomain()
+ {
+ try
+ {
+ URL url = new URL(getDocumentURI());
+ return url.getHost();
+ }
+ catch (MalformedURLException e)
+ {
+ return null;
+ }
+ }
+
+ public String getURL()
+ {
+ return getDocumentURI();
+ }
+
+ public HTMLElement getBody()
+ {
+ Node html = getDocumentElement();
+ if (html != null)
+ {
+ Node body = getChildNodeByName(html, "body");
+ if (body == null)
+ {
+ body = getChildNodeByName(html, "frameset");
+ }
+ return (HTMLElement) body;
+ }
+ return null;
+ }
+
+ public void setBody(HTMLElement body)
+ {
+ Node html = getDocumentElement();
+ if (html == null)
+ {
+ html = createElement("html");
+ appendChild(html);
+ }
+ Node ref = getBody();
+ if (ref == null)
+ {
+ html.appendChild(body);
+ }
+ else
+ {
+ html.replaceChild(body, ref);
+ }
+ }
+
+ public HTMLCollection getImages()
+ {
+ DomHTMLCollection ret = new DomHTMLCollection(this, this);
+ ret.addNodeName("img");
+ ret.evaluate();
+ return ret;
+ }
+
+ public HTMLCollection getApplets()
+ {
+ DomHTMLCollection ret = new DomHTMLCollection(this, this);
+ ret.addNodeName("embed");
+ ret.addNodeName("object");
+ ret.addNodeName("applet");
+ ret.evaluate();
+ return ret;
+ }
+
+ public HTMLCollection getLinks()
+ {
+ DomHTMLCollection ret = new DomHTMLCollection(this, this);
+ ret.addNodeName("area");
+ ret.addNodeName("a");
+ ret.evaluate();
+ return ret;
+ }
+
+ public HTMLCollection getForms()
+ {
+ DomHTMLCollection ret = new DomHTMLCollection(this, this);
+ ret.addNodeName("form");
+ ret.evaluate();
+ return ret;
+ }
+
+ public HTMLCollection getAnchors()
+ {
+ DomHTMLCollection ret = new DomHTMLCollection(this, this);
+ ret.addNodeName("a");
+ ret.addAttributeName("name");
+ ret.evaluate();
+ return ret;
+ }
+
+ public String getCookie()
+ {
+ // TODO getCookie
+ return null;
+ }
+
+ public void setCookie(String cookie)
+ {
+ // TODO setCookie
+ }
+
+ public void open()
+ {
+ // TODO open
+ }
+
+ public void close()
+ {
+ // TODO close
+ }
+
+ public void write(String text)
+ {
+ // TODO write
+ }
+
+ public void writeln(String text)
+ {
+ // TODO write
+ }
+
+ public NodeList getElementsByName(String name)
+ {
+ DomHTMLCollection ret = new DomHTMLCollection(this, this);
+ ret.addNodeName(name);
+ ret.evaluate();
+ return ret;
+ // TODO xhtml: return only form controls (?)
+ }
+
+ public Element createElement(String tagName)
+ {
+ return createElementNS(null, tagName);
+ }
+
+ public Element createElementNS(String uri, String qName)
+ {
+ /* If a non-HTML element, use the default implementation. */
+ if (uri != null && !HTML_NS_URIS.contains(uri))
+ {
+ return super.createElementNS(uri, qName);
+ }
+ String localName = qName.toLowerCase();
+ int ci = qName.indexOf(':');
+ if (ci != -1)
+ {
+ localName = qName.substring(ci + 1);
+ }
+ Class t = (Class) ELEMENT_CLASSES.get(localName);
+ /* If a non-HTML element, use the default implementation. */
+ if (t == null)
+ {
+ return super.createElementNS(uri, qName);
+ }
+ try
+ {
+ Constructor c = t.getDeclaredConstructor(ELEMENT_PT);
+ Object[] args = new Object[] { this, uri, qName };
+ return (Element) c.newInstance(args);
+ }
+ catch (Exception e)
+ {
+ DOMException e2 = new DomDOMException(DOMException.TYPE_MISMATCH_ERR);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLElement.java
new file mode 100644
index 000000000..deeefec60
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLElement.java
@@ -0,0 +1,286 @@
+/* DomHTMLElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomDOMException;
+import gnu.xml.dom.DomElement;
+import gnu.xml.dom.DomEvent;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.events.UIEvent;
+import org.w3c.dom.html2.HTMLElement;
+
+/**
+ * Abstract implementation of an HTML element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public abstract class DomHTMLElement
+ extends DomElement
+ implements HTMLElement
+{
+
+ protected DomHTMLElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ /**
+ * Returns the value of the specified attribute.
+ * The attribute name is case insensitive.
+ */
+ protected String getHTMLAttribute(String name)
+ {
+ if (hasAttributes())
+ {
+ NamedNodeMap attrs = getAttributes();
+ int len = attrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Node attr = attrs.item(i);
+ String attrName = attr.getLocalName();
+ if (attrName == null)
+ {
+ attrName = attr.getNodeName();
+ }
+ if (attrName.equalsIgnoreCase(name))
+ {
+ return attr.getNodeValue();
+ }
+ }
+ }
+ return "";
+ }
+
+ protected int getIntHTMLAttribute(String name)
+ {
+ String value = getHTMLAttribute(name);
+ if (value == null)
+ {
+ return -1;
+ }
+ try
+ {
+ return Integer.parseInt(value);
+ }
+ catch (NumberFormatException e)
+ {
+ return -1;
+ }
+ }
+
+ protected boolean getBooleanHTMLAttribute(String name)
+ {
+ String value = getHTMLAttribute(name);
+ return value != null;
+ }
+
+ /**
+ * Sets the value of the specified attribute.
+ * The attribute name is case insensitive.
+ */
+ protected void setHTMLAttribute(String name, String value)
+ {
+ Node attr;
+ NamedNodeMap attrs = getAttributes();
+ int len = attrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ attr = attrs.item(i);
+ String attrName = attr.getLocalName();
+ if (attrName == null)
+ {
+ attrName = attr.getNodeName();
+ }
+ if (attrName.equalsIgnoreCase(name))
+ {
+ if (value != null)
+ {
+ attr.setNodeValue(value);
+ }
+ else
+ {
+ attrs.removeNamedItem(attr.getNodeName());
+ }
+ return;
+ }
+ }
+ if (value != null)
+ {
+ // Create a new attribute
+ DomHTMLDocument doc = (DomHTMLDocument) getOwnerDocument();
+ // XXX namespace URI for attribute?
+ attr = doc.createAttribute(name);
+ attr.setNodeValue(value);
+ }
+ }
+
+ protected void setIntHTMLAttribute(String name, int value)
+ {
+ setHTMLAttribute(name, Integer.toString(value));
+ }
+
+ protected void setBooleanHTMLAttribute(String name, boolean value)
+ {
+ setHTMLAttribute(name, value ? name : null);
+ }
+
+ /**
+ * Returns the first parent element with the specified name.
+ */
+ protected Node getParentElement(String name)
+ {
+ for (Node parent = getParentNode(); parent != null;
+ parent = parent.getParentNode())
+ {
+ String parentName = parent.getLocalName();
+ if (parentName == null)
+ {
+ parentName = parent.getNodeName();
+ }
+ if (name.equalsIgnoreCase(parentName))
+ {
+ return parent;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Returns the first child element with the specified name.
+ */
+ protected Node getChildElement(String name)
+ {
+ for (Node child = getFirstChild(); child != null;
+ child = child.getNextSibling())
+ {
+ String childName = child.getLocalName();
+ if (childName == null)
+ {
+ childName = child.getLocalName();
+ }
+ if (name.equalsIgnoreCase(childName))
+ {
+ return child;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Returns the index of this element among elements of the same name,
+ * relative to its parent.
+ */
+ protected int getIndex()
+ {
+ int index = 0;
+ Node parent = getParentNode();
+ if (parent != null)
+ {
+ for (Node ctx = parent.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if (ctx == this)
+ {
+ return index;
+ }
+ index++;
+ }
+ }
+ throw new DomDOMException(DOMException.NOT_FOUND_ERR);
+ }
+
+ protected void dispatchUIEvent(String name)
+ {
+ UIEvent event = new DomEvent.DomUIEvent(name);
+ dispatchEvent(event);
+ }
+
+ public String getId()
+ {
+ return getHTMLAttribute("id");
+ }
+
+ public void setId(String id)
+ {
+ setHTMLAttribute("id", id);
+ }
+
+ public String getTitle()
+ {
+ return getHTMLAttribute("title");
+ }
+
+ public void setTitle(String title)
+ {
+ setHTMLAttribute("title", title);
+ }
+
+ public String getLang()
+ {
+ return getHTMLAttribute("lang");
+ }
+
+ public void setLang(String lang)
+ {
+ setHTMLAttribute("lang", lang);
+ }
+
+ public String getDir()
+ {
+ return getHTMLAttribute("dir");
+ }
+
+ public void setDir(String dir)
+ {
+ setHTMLAttribute("dir", dir);
+ }
+
+ public String getClassName()
+ {
+ return getHTMLAttribute("class");
+ }
+
+ public void setClassName(String className)
+ {
+ setHTMLAttribute("class", className);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLEmbedElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLEmbedElement.java
new file mode 100644
index 000000000..221131533
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLEmbedElement.java
@@ -0,0 +1,129 @@
+/* DomHTMLEmbedElement.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.xml.dom.html2;
+
+public class DomHTMLEmbedElement
+ extends DomHTMLAppletElement
+{
+ protected DomHTMLEmbedElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getJavaObject()
+ {
+ return getHTMLAttribute("java_object");
+ }
+
+ public void setJavaObject(String object)
+ {
+ setHTMLAttribute("java_object", object);
+ }
+
+ public String getJavaCodeBase()
+ {
+ return getHTMLAttribute("java_codebase");
+ }
+
+ public void setJavaCodeBase(String codeBase)
+ {
+ setHTMLAttribute("java_codebase", codeBase);
+ }
+
+ public String getJavaArchive()
+ {
+ return getHTMLAttribute("java_archive");
+ }
+
+ public void setJavaArchive(String archive)
+ {
+ setHTMLAttribute("java_archive", archive);
+ }
+
+ public void setJavaCode(String code)
+ {
+ setHTMLAttribute("java_code", code);
+ }
+
+ public String getJavaCode()
+ {
+ return getHTMLAttribute("java_code");
+ }
+
+ public void setJavaType(String type)
+ {
+ setHTMLAttribute("java_type", type);
+ }
+
+ public String getJavaType()
+ {
+ return getHTMLAttribute("java_type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public String getPluginsPage()
+ {
+ return getHTMLAttribute("pluginspage");
+ }
+
+ public void setPluginsPage(String pluginspage)
+ {
+ setHTMLAttribute("pluginspage", pluginspage);
+ }
+
+ public String getMayscript()
+ {
+ return getHTMLAttribute("mayscript");
+ }
+
+ public void setMayscript(String mayscript)
+ {
+ setHTMLAttribute("mayscript", mayscript);
+ }
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFieldSetElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFieldSetElement.java
new file mode 100644
index 000000000..252cd3d2b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFieldSetElement.java
@@ -0,0 +1,64 @@
+/* DomHTMLFieldSetElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFieldSetElement;
+import org.w3c.dom.html2.HTMLFormElement;
+
+/**
+ * An HTML 'FIELDSET' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLFieldSetElement
+ extends DomHTMLElement
+ implements HTMLFieldSetElement
+{
+
+ protected DomHTMLFieldSetElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFontElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFontElement.java
new file mode 100644
index 000000000..5bfbb6f36
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFontElement.java
@@ -0,0 +1,88 @@
+/* DomHTMLFontElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFontElement;
+
+/**
+ * An HTML 'FONT' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLFontElement
+ extends DomHTMLElement
+ implements HTMLFontElement
+{
+
+ protected DomHTMLFontElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getColor()
+ {
+ return getHTMLAttribute("color");
+ }
+
+ public void setColor(String color)
+ {
+ setHTMLAttribute("color", color);
+ }
+
+ public String getFace()
+ {
+ return getHTMLAttribute("face");
+ }
+
+ public void setFace(String face)
+ {
+ setHTMLAttribute("face", face);
+ }
+
+ public String getSize()
+ {
+ return getHTMLAttribute("size");
+ }
+
+ public void setSize(String size)
+ {
+ setHTMLAttribute("size", size);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFormElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFormElement.java
new file mode 100644
index 000000000..9fe2ae802
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFormElement.java
@@ -0,0 +1,149 @@
+/* DomHTMLFormElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLCollection;
+import org.w3c.dom.html2.HTMLFormElement;
+
+/**
+ * An HTML 'FORM' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLFormElement
+ extends DomHTMLElement
+ implements HTMLFormElement
+{
+
+ protected DomHTMLFormElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLCollection getElements()
+ {
+ DomHTMLCollection ret =
+ new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
+ ret.addNodeName("input");
+ ret.addNodeName("button");
+ ret.addNodeName("select");
+ ret.addNodeName("textarea");
+ ret.addNodeName("isindex");
+ ret.addNodeName("label");
+ ret.addNodeName("option");
+ ret.evaluate();
+ return ret;
+ }
+
+ public int getLength()
+ {
+ return getElements().getLength();
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getAcceptCharset()
+ {
+ return getHTMLAttribute("accept-charset");
+ }
+
+ public void setAcceptCharset(String acceptCharset)
+ {
+ setHTMLAttribute("accept-charset", acceptCharset);
+ }
+
+ public String getAction()
+ {
+ return getHTMLAttribute("action");
+ }
+
+ public void setAction(String action)
+ {
+ setHTMLAttribute("action", action);
+ }
+
+ public String getEnctype()
+ {
+ return getHTMLAttribute("enctype");
+ }
+
+ public void setEnctype(String enctype)
+ {
+ setHTMLAttribute("enctype", enctype);
+ }
+
+ public String getMethod()
+ {
+ return getHTMLAttribute("method");
+ }
+
+ public void setMethod(String method)
+ {
+ setHTMLAttribute("method", method);
+ }
+
+ public String getTarget()
+ {
+ return getHTMLAttribute("target");
+ }
+
+ public void setTarget(String target)
+ {
+ setHTMLAttribute("target", target);
+ }
+
+ public void submit()
+ {
+ dispatchUIEvent("submit");
+ }
+
+ public void reset()
+ {
+ dispatchUIEvent("reset");
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameElement.java
new file mode 100644
index 000000000..2465c4b89
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameElement.java
@@ -0,0 +1,145 @@
+/* DomHTMLFrameElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.html2.HTMLFrameElement;
+
+/**
+ * An HTML 'FRAME' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLFrameElement
+ extends DomHTMLElement
+ implements HTMLFrameElement
+{
+
+ protected DomHTMLFrameElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getFrameBorder()
+ {
+ return getHTMLAttribute("frameborder");
+ }
+
+ public void setFrameBorder(String frameBorder)
+ {
+ setHTMLAttribute("frameborder", frameBorder);
+ }
+
+ public String getLongDesc()
+ {
+ return getHTMLAttribute("longdesc");
+ }
+
+ public void setLongDesc(String longDesc)
+ {
+ setHTMLAttribute("longdesc", longDesc);
+ }
+
+ public String getMarginHeight()
+ {
+ return getHTMLAttribute("marginheight");
+ }
+
+ public void setMarginHeight(String marginHeight)
+ {
+ setHTMLAttribute("marginheight", marginHeight);
+ }
+
+ public String getMarginWidth()
+ {
+ return getHTMLAttribute("marginwidth");
+ }
+
+ public void setMarginWidth(String marginWidth)
+ {
+ setHTMLAttribute("marginwidth", marginWidth);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public boolean getNoResize()
+ {
+ return getBooleanHTMLAttribute("noresize");
+ }
+
+ public void setNoResize(boolean noResize)
+ {
+ setBooleanHTMLAttribute("noresize", noResize);
+ }
+
+ public String getScrolling()
+ {
+ return getHTMLAttribute("scrolling");
+ }
+
+ public void setScrolling(String scrolling)
+ {
+ setHTMLAttribute("scrolling", scrolling);
+ }
+
+ public String getSrc()
+ {
+ return getHTMLAttribute("src");
+ }
+
+ public void setSrc(String src)
+ {
+ setHTMLAttribute("src", src);
+ }
+
+ public Document getContentDocument()
+ {
+ // TODO getContentDocument
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameSetElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameSetElement.java
new file mode 100644
index 000000000..dae9430fb
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameSetElement.java
@@ -0,0 +1,78 @@
+/* DomHTMLFrameSetElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFrameSetElement;
+
+/**
+ * An HTML 'FRAMESET' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLFrameSetElement
+ extends DomHTMLElement
+ implements HTMLFrameSetElement
+{
+
+ protected DomHTMLFrameSetElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getCols()
+ {
+ return getHTMLAttribute("cols");
+ }
+
+ public void setCols(String cols)
+ {
+ setHTMLAttribute("cols", cols);
+ }
+
+ public String getRows()
+ {
+ return getHTMLAttribute("rows");
+ }
+
+ public void setRows(String rows)
+ {
+ setHTMLAttribute("rows", rows);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLHRElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHRElement.java
new file mode 100644
index 000000000..e27ef5595
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHRElement.java
@@ -0,0 +1,98 @@
+/* DomHTMLHRElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLHRElement;
+
+/**
+ * An HTML 'HR' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLHRElement
+ extends DomHTMLElement
+ implements HTMLHRElement
+{
+
+ protected DomHTMLHRElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public boolean getNoShade()
+ {
+ return getBooleanHTMLAttribute("noshade");
+ }
+
+ public void setNoShade(boolean noShade)
+ {
+ setBooleanHTMLAttribute("noshade", noShade);
+ }
+
+ public String getSize()
+ {
+ return getHTMLAttribute("size");
+ }
+
+ public void setSize(String size)
+ {
+ setHTMLAttribute("size", size);
+ }
+
+ public String getWidth()
+ {
+ return getHTMLAttribute("width");
+ }
+
+ public void setWidth(String width)
+ {
+ setHTMLAttribute("width", width);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadElement.java
new file mode 100644
index 000000000..4a8ef995b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLHeadElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLHeadElement;
+
+/**
+ * An HTML 'HEAD' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLHeadElement
+ extends DomHTMLElement
+ implements HTMLHeadElement
+{
+
+ protected DomHTMLHeadElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getProfile()
+ {
+ return getHTMLAttribute("profile");
+ }
+
+ public void setProfile(String profile)
+ {
+ setHTMLAttribute("profile", profile);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadingElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadingElement.java
new file mode 100644
index 000000000..6e78e9004
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadingElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLHeadingElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLHeadingElement;
+
+/**
+ * An HTML 'H1', 'H2', 'H3', 'H4', 'H5', or 'H6' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLHeadingElement
+ extends DomHTMLElement
+ implements HTMLHeadingElement
+{
+
+ protected DomHTMLHeadingElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLHtmlElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHtmlElement.java
new file mode 100644
index 000000000..73f7243ed
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHtmlElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLHtmlElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLHtmlElement;
+
+/**
+ * An HTML 'HTML' top-level element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLHtmlElement
+ extends DomHTMLElement
+ implements HTMLHtmlElement
+{
+
+ protected DomHTMLHtmlElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getVersion()
+ {
+ return getHTMLAttribute("version");
+ }
+
+ public void setVersion(String version)
+ {
+ setHTMLAttribute("version", version);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLIFrameElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLIFrameElement.java
new file mode 100644
index 000000000..1a58fb73e
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLIFrameElement.java
@@ -0,0 +1,165 @@
+/* DomHTMLIFrameElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.html2.HTMLIFrameElement;
+
+/**
+ * An HTML 'IFRAME' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLIFrameElement
+ extends DomHTMLElement
+ implements HTMLIFrameElement
+{
+
+ protected DomHTMLIFrameElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getFrameBorder()
+ {
+ return getHTMLAttribute("frameborder");
+ }
+
+ public void setFrameBorder(String frameBorder)
+ {
+ setHTMLAttribute("frameborder", frameBorder);
+ }
+
+ public String getHeight()
+ {
+ return getHTMLAttribute("height");
+ }
+
+ public void setHeight(String height)
+ {
+ setHTMLAttribute("height", height);
+ }
+
+ public String getLongDesc()
+ {
+ return getHTMLAttribute("longdesc");
+ }
+
+ public void setLongDesc(String longDesc)
+ {
+ setHTMLAttribute("longdesc", longDesc);
+ }
+
+ public String getMarginHeight()
+ {
+ return getHTMLAttribute("marginheight");
+ }
+
+ public void setMarginHeight(String marginHeight)
+ {
+ setHTMLAttribute("marginheight", marginHeight);
+ }
+
+ public String getMarginWidth()
+ {
+ return getHTMLAttribute("marginwidth");
+ }
+
+ public void setMarginWidth(String marginWidth)
+ {
+ setHTMLAttribute("marginwidth", marginWidth);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getScrolling()
+ {
+ return getHTMLAttribute("scrolling");
+ }
+
+ public void setScrolling(String scrolling)
+ {
+ setHTMLAttribute("scrolling", scrolling);
+ }
+
+ public String getSrc()
+ {
+ return getHTMLAttribute("src");
+ }
+
+ public void setSrc(String src)
+ {
+ setHTMLAttribute("src", src);
+ }
+
+ public String getWidth()
+ {
+ return getHTMLAttribute("width");
+ }
+
+ public void setWidth(String width)
+ {
+ setHTMLAttribute("width", width);
+ }
+
+ public Document getContentDocument()
+ {
+ // TODO getContentDocument
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLImageElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLImageElement.java
new file mode 100644
index 000000000..c5d294c6f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLImageElement.java
@@ -0,0 +1,178 @@
+/* DomHTMLImageElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLImageElement;
+
+/**
+ * An HTML 'IMG' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLImageElement
+ extends DomHTMLElement
+ implements HTMLImageElement
+{
+
+ protected DomHTMLImageElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getAlt()
+ {
+ return getHTMLAttribute("alt");
+ }
+
+ public void setAlt(String alt)
+ {
+ setHTMLAttribute("alt", alt);
+ }
+
+ public String getBorder()
+ {
+ return getHTMLAttribute("border");
+ }
+
+ public void setBorder(String border)
+ {
+ setHTMLAttribute("border", border);
+ }
+
+ public int getHeight()
+ {
+ return getIntHTMLAttribute("height");
+ }
+
+ public void setHeight(int height)
+ {
+ setIntHTMLAttribute("height", height);
+ }
+
+ public int getHspace()
+ {
+ return getIntHTMLAttribute("hspace");
+ }
+
+ public void setHspace(int hspace)
+ {
+ setIntHTMLAttribute("hspace", hspace);
+ }
+
+ public boolean getIsMap()
+ {
+ return getBooleanHTMLAttribute("ismap");
+ }
+
+ public void setIsMap(boolean isMap)
+ {
+ setBooleanHTMLAttribute("ismap", isMap);
+ }
+
+ public String getLongDesc()
+ {
+ return getHTMLAttribute("longdesc");
+ }
+
+ public void setLongDesc(String longDesc)
+ {
+ setHTMLAttribute("longdesc", longDesc);
+ }
+
+ public String getSrc()
+ {
+ return getHTMLAttribute("src");
+ }
+
+ public void setSrc(String src)
+ {
+ setHTMLAttribute("src", src);
+ }
+
+ public String getUseMap()
+ {
+ return getHTMLAttribute("usemap");
+ }
+
+ public void setUseMap(String useMap)
+ {
+ setHTMLAttribute("usemap", useMap);
+ }
+
+ public int getVspace()
+ {
+ return getIntHTMLAttribute("vspace");
+ }
+
+ public void setVspace(int vspace)
+ {
+ setIntHTMLAttribute("vspace", vspace);
+ }
+
+ public int getWidth()
+ {
+ return getIntHTMLAttribute("width");
+ }
+
+ public void setWidth(int width)
+ {
+ setIntHTMLAttribute("width", width);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLImpl.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLImpl.java
new file mode 100644
index 000000000..a5faee5ce
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLImpl.java
@@ -0,0 +1,66 @@
+/* DomHTMLImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomImpl;
+import org.w3c.dom.Document;
+
+/**
+ * Specialised DOMImplementation for creating HTML documents.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLImpl
+ extends DomImpl
+{
+
+ protected Document createDocument()
+ {
+ return new DomHTMLDocument(this);
+ }
+
+ public Object getFeature(String feature, String version)
+ {
+ if (hasFeature(feature, version))
+ {
+ return this;
+ }
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLInputElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLInputElement.java
new file mode 100644
index 000000000..21201889d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLInputElement.java
@@ -0,0 +1,265 @@
+/* DomHTMLInputElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLInputElement;
+
+/**
+ * An HTML 'INPUT' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLInputElement
+ extends DomHTMLElement
+ implements HTMLInputElement
+{
+
+ protected String value;
+ protected Boolean checked;
+
+ protected DomHTMLInputElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getDefaultValue()
+ {
+ return getHTMLAttribute("value");
+ }
+
+ public void setDefaultValue(String defaultValue)
+ {
+ setHTMLAttribute("value", defaultValue);
+ }
+
+ public boolean getDefaultChecked()
+ {
+ return getBooleanHTMLAttribute("checked");
+ }
+
+ public void setDefaultChecked(boolean defaultChecked)
+ {
+ setBooleanHTMLAttribute("checked", defaultChecked);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public String getAccept()
+ {
+ return getHTMLAttribute("accept");
+ }
+
+ public void setAccept(String accept)
+ {
+ setHTMLAttribute("accept", accept);
+ }
+
+ public String getAccessKey()
+ {
+ return getHTMLAttribute("accesskey");
+ }
+
+ public void setAccessKey(String accessKey)
+ {
+ setHTMLAttribute("accesskey", accessKey);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getAlt()
+ {
+ return getHTMLAttribute("alt");
+ }
+
+ public void setAlt(String alt)
+ {
+ setHTMLAttribute("alt", alt);
+ }
+
+ public boolean getChecked()
+ {
+ if (checked == null)
+ {
+ checked = Boolean.valueOf(getDefaultChecked());
+ }
+ return checked.booleanValue();
+ }
+
+ public void setChecked(boolean checked)
+ {
+ this.checked = Boolean.valueOf(checked);
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public int getMaxLength()
+ {
+ return getIntHTMLAttribute("maxLength");
+ }
+
+ public void setMaxLength(int maxLength)
+ {
+ setIntHTMLAttribute("maxLength", maxLength);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public boolean getReadOnly()
+ {
+ return getBooleanHTMLAttribute("readonly");
+ }
+
+ public void setReadOnly(boolean readOnly)
+ {
+ setBooleanHTMLAttribute("readonly", readOnly);
+ }
+
+ public int getSize()
+ {
+ return getIntHTMLAttribute("size");
+ }
+
+ public void setSize(int size)
+ {
+ setIntHTMLAttribute("size", size);
+ }
+
+ public String getSrc()
+ {
+ return getHTMLAttribute("src");
+ }
+
+ public void setSrc(String src)
+ {
+ setHTMLAttribute("src", src);
+ }
+
+ public int getTabIndex()
+ {
+ return getIntHTMLAttribute("tabindex");
+ }
+
+ public void setTabIndex(int tabIndex)
+ {
+ setIntHTMLAttribute("tabindex", tabIndex);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+ public String getUseMap()
+ {
+ return getHTMLAttribute("usemap");
+ }
+
+ public void setUseMap(String useMap)
+ {
+ setHTMLAttribute("usemap", useMap);
+ }
+
+ public String getValue()
+ {
+ if (value == null)
+ {
+ value = getDefaultValue();
+ }
+ return value;
+ }
+
+ public void setValue(String value)
+ {
+ this.value = value;
+ }
+
+ public void blur()
+ {
+ dispatchUIEvent("blur");
+ }
+
+ public void focus()
+ {
+ dispatchUIEvent("focus");
+ }
+
+ public void select()
+ {
+ dispatchUIEvent("select");
+ }
+
+ public void click()
+ {
+ dispatchUIEvent("click");
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLIsIndexElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLIsIndexElement.java
new file mode 100644
index 000000000..8578cb6c0
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLIsIndexElement.java
@@ -0,0 +1,74 @@
+/* DomHTMLIsIndexElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLIsIndexElement;
+
+/**
+ * An HTML 'ISINDEX' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLIsIndexElement
+ extends DomHTMLElement
+ implements HTMLIsIndexElement
+{
+
+ protected DomHTMLIsIndexElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public String getPrompt()
+ {
+ return getHTMLAttribute("prompt");
+ }
+
+ public void setPrompt(String prompt)
+ {
+ setHTMLAttribute("prompt", prompt);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLLIElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLIElement.java
new file mode 100644
index 000000000..cab1412dd
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLIElement.java
@@ -0,0 +1,78 @@
+/* DomHTMLLIElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLLIElement;
+
+/**
+ * An HTML 'LI' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLLIElement
+ extends DomHTMLElement
+ implements HTMLLIElement
+{
+
+ protected DomHTMLLIElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+ public int getValue()
+ {
+ return getIntHTMLAttribute("value");
+ }
+
+ public void setValue(int value)
+ {
+ setIntHTMLAttribute("value", value);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLLabelElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLabelElement.java
new file mode 100644
index 000000000..96cfbaab5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLabelElement.java
@@ -0,0 +1,84 @@
+/* DomHTMLLabelElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLLabelElement;
+
+/**
+ * An HTML 'LABEL' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLLabelElement
+ extends DomHTMLElement
+ implements HTMLLabelElement
+{
+
+ protected DomHTMLLabelElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public String getAccessKey()
+ {
+ return getHTMLAttribute("accesskey");
+ }
+
+ public void setAccessKey(String accessKey)
+ {
+ setHTMLAttribute("accesskey", accessKey);
+ }
+
+ public String getHtmlFor()
+ {
+ return getHTMLAttribute("for");
+ }
+
+ public void setHtmlFor(String htmlFor)
+ {
+ setHTMLAttribute("for", htmlFor);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLLegendElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLegendElement.java
new file mode 100644
index 000000000..045861cae
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLegendElement.java
@@ -0,0 +1,84 @@
+/* DomHTMLLegendElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLLegendElement;
+
+/**
+ * An HTML 'LEGEND' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLLegendElement
+ extends DomHTMLElement
+ implements HTMLLegendElement
+{
+
+ protected DomHTMLLegendElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public String getAccessKey()
+ {
+ return getHTMLAttribute("accesskey");
+ }
+
+ public void setAccessKey(String accessKey)
+ {
+ setHTMLAttribute("accesskey", accessKey);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLLinkElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLinkElement.java
new file mode 100644
index 000000000..292ec9305
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLinkElement.java
@@ -0,0 +1,148 @@
+/* DomHTMLLinkElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLLinkElement;
+
+/**
+ * An HTML 'LINK' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLLinkElement
+ extends DomHTMLElement
+ implements HTMLLinkElement
+{
+
+ protected DomHTMLLinkElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public String getCharset()
+ {
+ return getHTMLAttribute("charset");
+ }
+
+ public void setCharset(String charset)
+ {
+ setHTMLAttribute("charset", charset);
+ }
+
+ public String getHref()
+ {
+ return getHTMLAttribute("href");
+ }
+
+ public void setHref(String href)
+ {
+ setHTMLAttribute("href", href);
+ }
+
+ public String getHreflang()
+ {
+ return getHTMLAttribute("hreflang");
+ }
+
+ public void setHreflang(String hreflang)
+ {
+ setHTMLAttribute("hreflang", hreflang);
+ }
+
+ public String getMedia()
+ {
+ return getHTMLAttribute("media");
+ }
+
+ public void setMedia(String media)
+ {
+ setHTMLAttribute("media", media);
+ }
+
+ public String getRel()
+ {
+ return getHTMLAttribute("rel");
+ }
+
+ public void setRel(String rel)
+ {
+ setHTMLAttribute("rel", rel);
+ }
+
+ public String getRev()
+ {
+ return getHTMLAttribute("rev");
+ }
+
+ public void setRev(String rev)
+ {
+ setHTMLAttribute("rev", rev);
+ }
+
+ public String getTarget()
+ {
+ return getHTMLAttribute("target");
+ }
+
+ public void setTarget(String target)
+ {
+ setHTMLAttribute("target", target);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLMapElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMapElement.java
new file mode 100644
index 000000000..4497b00e2
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMapElement.java
@@ -0,0 +1,78 @@
+/* DomHTMLMapElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLCollection;
+import org.w3c.dom.html2.HTMLMapElement;
+
+/**
+ * An HTML 'MAP' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLMapElement
+ extends DomHTMLElement
+ implements HTMLMapElement
+{
+
+ protected DomHTMLMapElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLCollection getAreas()
+ {
+ DomHTMLCollection ret =
+ new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
+ ret.addNodeName("area");
+ ret.evaluate();
+ return ret;
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLMenuElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMenuElement.java
new file mode 100644
index 000000000..2a6ff08f1
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMenuElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLMenuElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLMenuElement;
+
+/**
+ * An HTML 'MENU' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLMenuElement
+ extends DomHTMLElement
+ implements HTMLMenuElement
+{
+
+ protected DomHTMLMenuElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getCompact()
+ {
+ return getBooleanHTMLAttribute("compact");
+ }
+
+ public void setCompact(boolean compact)
+ {
+ setBooleanHTMLAttribute("compact", compact);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLMetaElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMetaElement.java
new file mode 100644
index 000000000..e555a4273
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMetaElement.java
@@ -0,0 +1,98 @@
+/* DomHTMLMetaElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLMetaElement;
+
+/**
+ * An HTML 'META' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLMetaElement
+ extends DomHTMLElement
+ implements HTMLMetaElement
+{
+
+ protected DomHTMLMetaElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getContent()
+ {
+ return getHTMLAttribute("content");
+ }
+
+ public void setContent(String content)
+ {
+ setHTMLAttribute("content", content);
+ }
+
+ public String getHttpEquiv()
+ {
+ return getHTMLAttribute("http-equiv");
+ }
+
+ public void setHttpEquiv(String httpEquiv)
+ {
+ setHTMLAttribute("http-equiv", httpEquiv);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getScheme()
+ {
+ return getHTMLAttribute("scheme");
+ }
+
+ public void setScheme(String scheme)
+ {
+ setHTMLAttribute("scheme", scheme);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLModElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLModElement.java
new file mode 100644
index 000000000..8c158f48a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLModElement.java
@@ -0,0 +1,78 @@
+/* DomHTMLModElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLModElement;
+
+/**
+ * An HTML 'INS' or 'DEL' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLModElement
+ extends DomHTMLElement
+ implements HTMLModElement
+{
+
+ protected DomHTMLModElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getCite()
+ {
+ return getHTMLAttribute("cite");
+ }
+
+ public void setCite(String cite)
+ {
+ setHTMLAttribute("cite", cite);
+ }
+
+ public String getDateTime()
+ {
+ return getHTMLAttribute("datetime");
+ }
+
+ public void setDateTime(String dateTime)
+ {
+ setHTMLAttribute("datetime", dateTime);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLOListElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOListElement.java
new file mode 100644
index 000000000..54f7fd46b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOListElement.java
@@ -0,0 +1,88 @@
+/* DomHTMLOListElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLOListElement;
+
+/**
+ * An HTML 'OL' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLOListElement
+ extends DomHTMLElement
+ implements HTMLOListElement
+{
+
+ protected DomHTMLOListElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getCompact()
+ {
+ return getBooleanHTMLAttribute("compact");
+ }
+
+ public void setCompact(boolean compact)
+ {
+ setBooleanHTMLAttribute("compact", compact);
+ }
+
+ public int getStart()
+ {
+ return getIntHTMLAttribute("start");
+ }
+
+ public void setStart(int start)
+ {
+ setIntHTMLAttribute("start", start);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLObjectElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLObjectElement.java
new file mode 100644
index 000000000..2ce5f45c3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLObjectElement.java
@@ -0,0 +1,320 @@
+/* DomHTMLObjectElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLObjectElement;
+
+/**
+ * An HTML 'OBJECT' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLObjectElement
+ extends DomHTMLElement
+ implements HTMLObjectElement
+{
+
+ protected DomHTMLObjectElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public String getCode()
+ {
+ return getHTMLAttribute("code");
+ }
+
+ public void setCode(String code)
+ {
+ setHTMLAttribute("code", code);
+ }
+
+ public String getJavaCode()
+ {
+ return getHTMLAttribute("java_code");
+ }
+
+ public void setJavaCode(String code)
+ {
+ setHTMLAttribute("java_code", code);
+ }
+
+ public String getObject()
+ {
+ return getHTMLAttribute("object");
+ }
+
+ public void setObject(String obj)
+ {
+ setHTMLAttribute("object", obj);
+ }
+
+ public String getJavaObject()
+ {
+ return getHTMLAttribute("java_object");
+ }
+
+ public void setJavaObject(String obj)
+ {
+ setHTMLAttribute("java_object", obj);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getArchive()
+ {
+ return getHTMLAttribute("archive");
+ }
+
+ public void setArchive(String archive)
+ {
+ setHTMLAttribute("archive", archive);
+ }
+
+ public String getJavaArchive()
+ {
+ return getHTMLAttribute("java_archive");
+ }
+
+ public void setJavaArchive(String archive)
+ {
+ setHTMLAttribute("java_archive", archive);
+ }
+
+ public String getBorder()
+ {
+ return getHTMLAttribute("border");
+ }
+
+ public void setBorder(String border)
+ {
+ setHTMLAttribute("border", border);
+ }
+
+ public String getCodeBase()
+ {
+ return getHTMLAttribute("codebase");
+ }
+
+ public void setCodeBase(String codeBase)
+ {
+ setHTMLAttribute("codebase", codeBase);
+ }
+
+ public String getJavaCodeBase()
+ {
+ return getHTMLAttribute("java_codebase");
+ }
+
+ public void setJavaCodeBase(String codeBase)
+ {
+ setHTMLAttribute("java_codebase", codeBase);
+ }
+
+ public String getCodeType()
+ {
+ return getHTMLAttribute("codetype");
+ }
+
+ public void setCodeType(String codeType)
+ {
+ setHTMLAttribute("codetype", codeType);
+ }
+
+ public String getData()
+ {
+ return getHTMLAttribute("data");
+ }
+
+ public void setData(String data)
+ {
+ setHTMLAttribute("data", data);
+ }
+
+ public boolean getDeclare()
+ {
+ return getBooleanHTMLAttribute("declare");
+ }
+
+ public void setDeclare(boolean declare)
+ {
+ setBooleanHTMLAttribute("declare", declare);
+ }
+
+ public String getHeight()
+ {
+ return getHTMLAttribute("height");
+ }
+
+ public void setHeight(String height)
+ {
+ setHTMLAttribute("height", height);
+ }
+
+ public int getHspace()
+ {
+ return getIntHTMLAttribute("hspace");
+ }
+
+ public void setHspace(int hspace)
+ {
+ setIntHTMLAttribute("hspace", hspace);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getStandby()
+ {
+ return getHTMLAttribute("standby");
+ }
+
+ public void setStandby(String standby)
+ {
+ setHTMLAttribute("standby", standby);
+ }
+
+ public int getTabIndex()
+ {
+ return getIntHTMLAttribute("tabindex");
+ }
+
+ public void setTabIndex(int tabIndex)
+ {
+ setIntHTMLAttribute("tabindex", tabIndex);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+ public String getJavaType()
+ {
+ return getHTMLAttribute("java_type");
+ }
+
+ public void setJavaType(String type)
+ {
+ setHTMLAttribute("java_type", type);
+ }
+
+ public String getUseMap()
+ {
+ return getHTMLAttribute("usemap");
+ }
+
+ public void setUseMap(String useMap)
+ {
+ setHTMLAttribute("usemap", useMap);
+ }
+
+ public int getVspace()
+ {
+ return getIntHTMLAttribute("vspace");
+ }
+
+ public void setVspace(int vspace)
+ {
+ setIntHTMLAttribute("vspace", vspace);
+ }
+
+ public String getWidth()
+ {
+ return getHTMLAttribute("width");
+ }
+
+ public void setWidth(String width)
+ {
+ setHTMLAttribute("width", width);
+ }
+
+ public Document getContentDocument()
+ {
+ // TODO getContentDocument
+ return null;
+ }
+
+ public void setMayscript(String may)
+ {
+ setHTMLAttribute("mayscript", may);
+ }
+
+ public String getMayscript()
+ {
+ return getHTMLAttribute("mayscript");
+ }
+
+ public void setScriptable(String scr)
+ {
+ setHTMLAttribute("scriptable", scr);
+ }
+
+ public String getScriptable()
+ {
+ return getHTMLAttribute("scriptable");
+ }
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptGroupElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptGroupElement.java
new file mode 100644
index 000000000..acb948484
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptGroupElement.java
@@ -0,0 +1,78 @@
+/* DomHTMLOptGroupElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLOptGroupElement;
+
+/**
+ * An HTML 'OPTGROUP' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLOptGroupElement
+ extends DomHTMLElement
+ implements HTMLOptGroupElement
+{
+
+ protected DomHTMLOptGroupElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public String getLabel()
+ {
+ return getHTMLAttribute("label");
+ }
+
+ public void setLabel(String label)
+ {
+ setHTMLAttribute("label", label);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptionElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptionElement.java
new file mode 100644
index 000000000..f775c138a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptionElement.java
@@ -0,0 +1,130 @@
+/* DomHTMLOptionElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLOptionElement;
+
+/**
+ * An HTML 'OPTION' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLOptionElement
+ extends DomHTMLElement
+ implements HTMLOptionElement
+{
+
+ protected Boolean selected;
+
+ protected DomHTMLOptionElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public boolean getDefaultSelected()
+ {
+ return getBooleanHTMLAttribute("selected");
+ }
+
+ public void setDefaultSelected(boolean defaultSelected)
+ {
+ setBooleanHTMLAttribute("selected", defaultSelected);
+ }
+
+ public String getText()
+ {
+ return getTextContent();
+ }
+
+ public int getIndex()
+ {
+ return super.getIndex();
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public String getLabel()
+ {
+ return getHTMLAttribute("label");
+ }
+
+ public void setLabel(String label)
+ {
+ setHTMLAttribute("label", label);
+ }
+
+ public boolean getSelected()
+ {
+ if (selected == null)
+ {
+ selected = Boolean.valueOf(getDefaultSelected());
+ }
+ return selected.booleanValue();
+ }
+
+ public void setSelected(boolean selected)
+ {
+ this.selected = Boolean.valueOf(selected);
+ }
+
+ public String getValue()
+ {
+ return getHTMLAttribute("value");
+ }
+
+ public void setValue(String value)
+ {
+ setHTMLAttribute("value", value);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLParagraphElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParagraphElement.java
new file mode 100644
index 000000000..4c867fdc1
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParagraphElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLParagraphElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLParagraphElement;
+
+/**
+ * An HTML 'P' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLParagraphElement
+ extends DomHTMLElement
+ implements HTMLParagraphElement
+{
+
+ protected DomHTMLParagraphElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLParamElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParamElement.java
new file mode 100644
index 000000000..cdd74ad4a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParamElement.java
@@ -0,0 +1,98 @@
+/* DomHTMLParamElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLParamElement;
+
+/**
+ * An HTML 'PARAM' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLParamElement
+ extends DomHTMLElement
+ implements HTMLParamElement
+{
+
+ protected DomHTMLParamElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+ public String getValue()
+ {
+ return getHTMLAttribute("value");
+ }
+
+ public void setValue(String value)
+ {
+ setHTMLAttribute("value", value);
+ }
+
+ public String getValueType()
+ {
+ return getHTMLAttribute("valuetype");
+ }
+
+ public void setValueType(String valueType)
+ {
+ setHTMLAttribute("valuetype", valueType);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLParser.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParser.java
new file mode 100644
index 000000000..88656d2d3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParser.java
@@ -0,0 +1,264 @@
+/* DomHTMLParser.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.xml.dom.html2;
+
+import java.io.IOException;
+import java.io.Reader;
+
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.LinkedList;
+
+import javax.swing.text.AttributeSet;
+import javax.swing.text.html.HTML;
+import javax.swing.text.html.parser.DTD;
+import javax.swing.text.html.parser.TagElement;
+
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.html2.HTMLDocument;
+
+/**
+ * This parser reads HTML from the given stream and stores into
+ * {@link HTMLDocument}. The HTML tag becomes the {@link Node}.
+ * The tag attributes become the node attributes. The text inside
+ * HTML tag is inserted as one or several text nodes. The nested
+ * HTML tags are inserted as child nodes.
+ *
+ * If the strict tree structure, closing the tag means closing all
+ * nested tags. To work around this, this parser closes the nested
+ * tags and immediately reopens them after the closed tag.
+ * In this way, <code>&lt;b&gt;&lt;i&gt;c&lt;/b&gt;d</code>
+ * is parsed as <code>&lt;b&gt;&lt;i&gt;c&lt;/i&gt;&lt;/b&gt;&lt;i&gt;d</code> .
+ *
+ * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
+ */
+public class DomHTMLParser
+ extends gnu.javax.swing.text.html.parser.support.Parser
+{
+ /**
+ * The target where HTML document will be inserted.
+ */
+ protected DomHTMLDocument document;
+
+ /**
+ * The subsequently created new nodes will be inserted as the
+ * childs of this cursor.
+ */
+ protected Node cursor;
+
+ /**
+ * Create parser using the given DTD.
+ *
+ * @param dtd the DTD (for example,
+ * {@link gnu.javax.swing.text.html.parser.HTML_401F}).
+ */
+ public DomHTMLParser(DTD dtd)
+ {
+ super(dtd);
+ }
+
+ /**
+ * Parse SGML insertion ( &lt;! ... &gt; ).
+ * Currently just treats it as comment.
+ */
+ public boolean parseMarkupDeclarations(StringBuffer strBuff)
+ throws java.io.IOException
+ {
+ Node c = document.createComment(strBuff.toString());
+ cursor.appendChild(c);
+ return false;
+ }
+
+ /**
+ * Read the document, present in the given stream, and
+ * return the corresponding {@link HTMLDocument}.
+ *
+ * @param input a stream to read from.
+ * @return a document, reflecting the structure of the provided HTML
+ * text.
+ *
+ * @throws IOException if the reader throws one.
+ */
+ public HTMLDocument parseDocument(Reader input)
+ throws IOException
+ {
+ try
+ {
+ document = new DomHTMLDocument();
+ document.setCheckWellformedness(false);
+ document.setCheckingCharacters(false);
+
+ cursor = document;
+
+ parse(input);
+
+ DomHTMLDocument h = document;
+ document = null;
+ return h;
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ throw new IOException("Exception: " + ex.getMessage());
+ }
+ }
+
+ /**
+ * Create a new node.
+ * @param name the name of node, case insensitive.
+ * @return the created node.
+ */
+ protected Node createNode(String name)
+ {
+ Node new_node = document.createElement(name.toLowerCase());
+ AttributeSet hatts = getAttributes();
+ NamedNodeMap natts = new_node.getAttributes();
+
+ Enumeration enumeration = hatts.getAttributeNames();
+ Object key;
+ Node attribute;
+
+ while (hatts != null)
+ {
+ while (enumeration.hasMoreElements())
+ {
+ key = enumeration.nextElement();
+ attribute = document.createAttribute(key.toString());
+ attribute.setNodeValue(hatts.getAttribute(key).toString());
+ natts.setNamedItem(attribute);
+ }
+
+ // The default values are stored in a parent node.
+ hatts = hatts.getResolveParent();
+ }
+
+ return new_node;
+ }
+
+ /**
+ * Handle comment by inserting the comment node.
+ * @param text the comment text.
+ */
+ protected void handleComment(char[] text)
+ {
+ Node c = document.createComment(new String(text));
+ cursor.appendChild(c);
+ }
+
+ /**
+ * Handle the tag with no content.
+ * @param tag the tag to handle.
+ */
+ protected void handleEmptyTag(TagElement tag)
+ {
+ String name = tag.getHTMLTag().toString();
+
+ if (name.equalsIgnoreCase("#pcdata"))
+ return;
+
+ Node c = createNode(name);
+ cursor.appendChild(c);
+ }
+
+ /**
+ * Close the given tag. Close and reopen all nested tags.
+ * @param tag the tag to close.
+ */
+ protected void handleEndTag(TagElement tag)
+ {
+ String name = tag.getHTMLTag().toString();
+ String nname = cursor.getNodeName();
+
+ // Closing the current tag.
+ if (nname != null && nname.equalsIgnoreCase(name))
+ {
+ cursor = cursor.getParentNode();
+ }
+ else
+ {
+ Node nCursor = cursor.getParentNode();
+
+ // Remember the opened nodes.
+ LinkedList open = new LinkedList();
+ Node close = cursor;
+ while (close != null && !close.getNodeName().equalsIgnoreCase(name))
+ {
+ if (close != document)
+ open.addFirst(close);
+ close = close.getParentNode();
+ }
+ if (close == null)
+ cursor = document;
+ else
+ cursor = close.getParentNode();
+
+ // Insert the copies of the opened nodes.
+ Iterator iter = open.iterator();
+ while (iter.hasNext())
+ {
+ Node item = (Node) iter.next();
+ cursor.appendChild(item);
+ cursor = item;
+ }
+ }
+ }
+
+ /**
+ * Handle the start tag by inserting the HTML element.
+ * @param tag the tag to handle.
+ */
+ protected void handleStartTag(TagElement tag)
+ {
+ HTML.Tag h = tag.getHTMLTag();
+ Node c = createNode(h.toString());
+ cursor.appendChild(c);
+ cursor = c;
+ }
+
+ /**
+ * Handle text by inserting the text node.
+ * @param text the text to insert.
+ */
+ protected void handleText(char[] text)
+ {
+ Node c = document.createTextNode(text, 0, text.length);
+ cursor.appendChild(c);
+ }
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLPreElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLPreElement.java
new file mode 100644
index 000000000..9a51aaeeb
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLPreElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLPreElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLPreElement;
+
+/**
+ * An HTML 'PRE' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLPreElement
+ extends DomHTMLElement
+ implements HTMLPreElement
+{
+
+ protected DomHTMLPreElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public int getWidth()
+ {
+ return getIntHTMLAttribute("width");
+ }
+
+ public void setWidth(int width)
+ {
+ setIntHTMLAttribute("width", width);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLQuoteElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLQuoteElement.java
new file mode 100644
index 000000000..811aed58b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLQuoteElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLQuoteElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLQuoteElement;
+
+/**
+ * An HTML 'Q' or 'BLOCKQUOTE' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLQuoteElement
+ extends DomHTMLElement
+ implements HTMLQuoteElement
+{
+
+ protected DomHTMLQuoteElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getCite()
+ {
+ return getHTMLAttribute("cite");
+ }
+
+ public void setCite(String cite)
+ {
+ setHTMLAttribute("cite", cite);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLScriptElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLScriptElement.java
new file mode 100644
index 000000000..1bd7b3417
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLScriptElement.java
@@ -0,0 +1,128 @@
+/* DomHTMLScriptElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLScriptElement;
+
+/**
+ * An HTML 'SCRIPT' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLScriptElement
+ extends DomHTMLElement
+ implements HTMLScriptElement
+{
+
+ protected DomHTMLScriptElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getText()
+ {
+ return getTextContent();
+ }
+
+ public void setText(String text)
+ {
+ setTextContent(text);
+ }
+
+ public String getHtmlFor()
+ {
+ return getHTMLAttribute("for");
+ }
+
+ public void setHtmlFor(String htmlFor)
+ {
+ setHTMLAttribute("for", htmlFor);
+ }
+
+ public String getEvent()
+ {
+ return getHTMLAttribute("event");
+ }
+
+ public void setEvent(String event)
+ {
+ setHTMLAttribute("event", event);
+ }
+
+ public String getCharset()
+ {
+ return getHTMLAttribute("charset");
+ }
+
+ public void setCharset(String charset)
+ {
+ setHTMLAttribute("charset", charset);
+ }
+
+ public boolean getDefer()
+ {
+ return getBooleanHTMLAttribute("defer");
+ }
+
+ public void setDefer(boolean defer)
+ {
+ setBooleanHTMLAttribute("defer", defer);
+ }
+
+ public String getSrc()
+ {
+ return getHTMLAttribute("src");
+ }
+
+ public void setSrc(String src)
+ {
+ setHTMLAttribute("src", src);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLSelectElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLSelectElement.java
new file mode 100644
index 000000000..fc1debbd6
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLSelectElement.java
@@ -0,0 +1,210 @@
+/* DomHTMLSelectElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomDOMException;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.html2.HTMLElement;
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLOptionElement;
+import org.w3c.dom.html2.HTMLOptionsCollection;
+import org.w3c.dom.html2.HTMLSelectElement;
+
+/**
+ * An HTML 'SELECT' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLSelectElement
+ extends DomHTMLElement
+ implements HTMLSelectElement
+{
+
+ protected DomHTMLSelectElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public int getSelectedIndex()
+ {
+ HTMLOptionsCollection options = getOptions();
+ int len = options.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ HTMLOptionElement option = (HTMLOptionElement) options.item(i);
+ if (option.getSelected())
+ {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ public void setSelectedIndex(int selectedIndex)
+ {
+ HTMLOptionsCollection options = getOptions();
+ int len = options.getLength();
+ if (selectedIndex < 0 || selectedIndex >= len)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ for (int i = 0; i < len; i++)
+ {
+ HTMLOptionElement option = (HTMLOptionElement) options.item(i);
+ option.setSelected(i == selectedIndex);
+ }
+ }
+
+ public String getValue()
+ {
+ return getHTMLAttribute("value");
+ }
+
+ public void setValue(String value)
+ {
+ setHTMLAttribute("value", value);
+ }
+
+ public int getLength()
+ {
+ return getIntHTMLAttribute("length");
+ }
+
+ public void setLength(int length)
+ {
+ setIntHTMLAttribute("length", length);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public HTMLOptionsCollection getOptions()
+ {
+ DomHTMLCollection ret =
+ new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
+ ret.addNodeName("option");
+ ret.evaluate();
+ return ret;
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public boolean getMultiple()
+ {
+ return getBooleanHTMLAttribute("multiple");
+ }
+
+ public void setMultiple(boolean multiple)
+ {
+ setBooleanHTMLAttribute("multiple", multiple);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public int getSize()
+ {
+ return getIntHTMLAttribute("size");
+ }
+
+ public void setSize(int size)
+ {
+ setIntHTMLAttribute("size", size);
+ }
+
+ public int getTabIndex()
+ {
+ return getIntHTMLAttribute("tabindex");
+ }
+
+ public void setTabIndex(int tabIndex)
+ {
+ setIntHTMLAttribute("tabindex", tabIndex);
+ }
+
+ public void add(HTMLElement element, HTMLElement before)
+ {
+ insertBefore(before, element);
+ }
+
+ public void remove(int index)
+ {
+ HTMLOptionsCollection options = getOptions();
+ int len = options.getLength();
+ if (index < 0 || index >= len)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ HTMLOptionElement option = (HTMLOptionElement) options.item(index);
+ option.getParentNode().removeChild(option);
+ }
+
+ public void blur()
+ {
+ dispatchUIEvent("blur");
+ }
+
+ public void focus()
+ {
+ dispatchUIEvent("focus");
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLStyleElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLStyleElement.java
new file mode 100644
index 000000000..78cef3b18
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLStyleElement.java
@@ -0,0 +1,88 @@
+/* DomHTMLStyleElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLStyleElement;
+
+/**
+ * An HTML 'STYLE' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLStyleElement
+ extends DomHTMLElement
+ implements HTMLStyleElement
+{
+
+ protected DomHTMLStyleElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public String getMedia()
+ {
+ return getHTMLAttribute("media");
+ }
+
+ public void setMedia(String media)
+ {
+ setHTMLAttribute("media", media);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCaptionElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCaptionElement.java
new file mode 100644
index 000000000..2133b1412
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCaptionElement.java
@@ -0,0 +1,69 @@
+/* DomHTMLTableCaptionElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLTableCaptionElement;
+
+/**
+ * An HTML 'CAPTION' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLTableCaptionElement
+ extends DomHTMLElement
+ implements HTMLTableCaptionElement
+{
+
+ protected DomHTMLTableCaptionElement(DomHTMLDocument owner,
+ String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCellElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCellElement.java
new file mode 100644
index 000000000..350f0bf42
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCellElement.java
@@ -0,0 +1,204 @@
+/* DomHTMLTableCellElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLTableCellElement;
+
+/**
+ * An HTML 'TH' or 'TD' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLTableCellElement
+ extends DomHTMLElement
+ implements HTMLTableCellElement
+{
+
+ protected DomHTMLTableCellElement(DomHTMLDocument owner,
+ String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public int getCellIndex()
+ {
+ return getIndex();
+ }
+
+ public String getAbbr()
+ {
+ return getHTMLAttribute("abbr");
+ }
+
+ public void setAbbr(String abbr)
+ {
+ setHTMLAttribute("abbr", abbr);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getAxis()
+ {
+ return getHTMLAttribute("axis");
+ }
+
+ public void setAxis(String axis)
+ {
+ setHTMLAttribute("axis", axis);
+ }
+
+ public String getBgColor()
+ {
+ return getHTMLAttribute("bgcolor");
+ }
+
+ public void setBgColor(String bgColor)
+ {
+ setHTMLAttribute("bgcolor", bgColor);
+ }
+
+ public String getCh()
+ {
+ return getHTMLAttribute("char");
+ }
+
+ public void setCh(String ch)
+ {
+ setHTMLAttribute("char", ch);
+ }
+
+ public String getChOff()
+ {
+ return getHTMLAttribute("charoff");
+ }
+
+ public void setChOff(String chOff)
+ {
+ setHTMLAttribute("charoff", chOff);
+ }
+
+ public int getColSpan()
+ {
+ return getIntHTMLAttribute("colspan");
+ }
+
+ public void setColSpan(int colSpan)
+ {
+ setIntHTMLAttribute("colspan", colSpan);
+ }
+
+ public String getHeaders()
+ {
+ return getHTMLAttribute("headers");
+ }
+
+ public void setHeaders(String headers)
+ {
+ setHTMLAttribute("headers", headers);
+ }
+
+ public String getHeight()
+ {
+ return getHTMLAttribute("height");
+ }
+
+ public void setHeight(String height)
+ {
+ setHTMLAttribute("height", height);
+ }
+
+ public boolean getNoWrap()
+ {
+ return getBooleanHTMLAttribute("nowrap");
+ }
+
+ public void setNoWrap(boolean noWrap)
+ {
+ setBooleanHTMLAttribute("nowrap", noWrap);
+ }
+
+ public int getRowSpan()
+ {
+ return getIntHTMLAttribute("rowspan");
+ }
+
+ public void setRowSpan(int rowSpan)
+ {
+ setIntHTMLAttribute("rowspan", rowSpan);
+ }
+
+ public String getScope()
+ {
+ return getHTMLAttribute("scope");
+ }
+
+ public void setScope(String scope)
+ {
+ setHTMLAttribute("scope", scope);
+ }
+
+ public String getVAlign()
+ {
+ return getHTMLAttribute("valign");
+ }
+
+ public void setVAlign(String vAlign)
+ {
+ setHTMLAttribute("valign", vAlign);
+ }
+
+ public String getWidth()
+ {
+ return getHTMLAttribute("width");
+ }
+
+ public void setWidth(String width)
+ {
+ setHTMLAttribute("width", width);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableColElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableColElement.java
new file mode 100644
index 000000000..c7cef31eb
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableColElement.java
@@ -0,0 +1,119 @@
+/* DomHTMLTableColElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLTableColElement;
+
+/**
+ * An HTML 'COL' or 'COLGROUP' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLTableColElement
+ extends DomHTMLElement
+ implements HTMLTableColElement
+{
+
+ protected DomHTMLTableColElement(DomHTMLDocument owner,
+ String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getCh()
+ {
+ return getHTMLAttribute("char");
+ }
+
+ public void setCh(String ch)
+ {
+ setHTMLAttribute("char", ch);
+ }
+
+ public String getChOff()
+ {
+ return getHTMLAttribute("charoff");
+ }
+
+ public void setChOff(String chOff)
+ {
+ setHTMLAttribute("charoff", chOff);
+ }
+
+ public int getSpan()
+ {
+ return getIntHTMLAttribute("span");
+ }
+
+ public void setSpan(int span)
+ {
+ setIntHTMLAttribute("span", span);
+ }
+
+ public String getVAlign()
+ {
+ return getHTMLAttribute("valign");
+ }
+
+ public void setVAlign(String vAlign)
+ {
+ setHTMLAttribute("valign", vAlign);
+ }
+
+ public String getWidth()
+ {
+ return getHTMLAttribute("width");
+ }
+
+ public void setWidth(String width)
+ {
+ setHTMLAttribute("width", width);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableElement.java
new file mode 100644
index 000000000..ade463ef7
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableElement.java
@@ -0,0 +1,397 @@
+/* DomHTMLTableElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomDOMException;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+import org.w3c.dom.html2.HTMLCollection;
+import org.w3c.dom.html2.HTMLElement;
+import org.w3c.dom.html2.HTMLTableCaptionElement;
+import org.w3c.dom.html2.HTMLTableElement;
+import org.w3c.dom.html2.HTMLTableSectionElement;
+
+/**
+ * An HTML 'TABLE' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLTableElement
+ extends DomHTMLElement
+ implements HTMLTableElement
+{
+
+ protected DomHTMLTableElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLTableCaptionElement getCaption()
+ {
+ return (HTMLTableCaptionElement) getChildElement("caption");
+ }
+
+ public void setCaption(HTMLTableCaptionElement caption)
+ {
+ HTMLTableCaptionElement ref = getCaption();
+ if (ref == null)
+ {
+ appendChild(caption);
+ }
+ else
+ {
+ replaceChild(caption, ref);
+ }
+ }
+
+ public HTMLTableSectionElement getTHead()
+ {
+ return (HTMLTableSectionElement) getChildElement("thead");
+ }
+
+ public void setTHead(HTMLTableSectionElement tHead)
+ {
+ HTMLTableSectionElement ref = getTHead();
+ if (ref == null)
+ {
+ appendChild(tHead);
+ }
+ else
+ {
+ replaceChild(tHead, ref);
+ }
+ }
+
+ public HTMLTableSectionElement getTFoot()
+ {
+ return (HTMLTableSectionElement) getChildElement("tfoot");
+ }
+
+ public void setTFoot(HTMLTableSectionElement tFoot)
+ {
+ HTMLTableSectionElement ref = getTFoot();
+ if (ref == null)
+ {
+ appendChild(tFoot);
+ }
+ else
+ {
+ replaceChild(tFoot, ref);
+ }
+ }
+
+ public HTMLCollection getRows()
+ {
+ DomHTMLCollection ret =
+ new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
+ ret.addNodeName("tr");
+ ret.evaluate();
+ return ret;
+ }
+
+ public HTMLCollection getTBodies()
+ {
+ DomHTMLCollection ret =
+ new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
+ ret.addNodeName("tbody");
+ ret.evaluate();
+ return ret;
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getBgColor()
+ {
+ return getHTMLAttribute("bgcolor");
+ }
+
+ public void setBgColor(String bgColor)
+ {
+ setHTMLAttribute("bgcolor", bgColor);
+ }
+
+ public String getBorder()
+ {
+ return getHTMLAttribute("border");
+ }
+
+ public void setBorder(String border)
+ {
+ setHTMLAttribute("border", border);
+ }
+
+ public String getCellPadding()
+ {
+ return getHTMLAttribute("cellpadding");
+ }
+
+ public void setCellPadding(String cellPadding)
+ {
+ setHTMLAttribute("cellpadding", cellPadding);
+ }
+
+ public String getCellSpacing()
+ {
+ return getHTMLAttribute("cellspacing");
+ }
+
+ public void setCellSpacing(String cellSpacing)
+ {
+ setHTMLAttribute("cellspacing", cellSpacing);
+ }
+
+ public String getFrame()
+ {
+ return getHTMLAttribute("frame");
+ }
+
+ public void setFrame(String frame)
+ {
+ setHTMLAttribute("frame", frame);
+ }
+
+ public String getRules()
+ {
+ return getHTMLAttribute("rules");
+ }
+
+ public void setRules(String rules)
+ {
+ setHTMLAttribute("rules", rules);
+ }
+
+ public String getSummary()
+ {
+ return getHTMLAttribute("summary");
+ }
+
+ public void setSummary(String summary)
+ {
+ setHTMLAttribute("summary", summary);
+ }
+
+ public String getWidth()
+ {
+ return getHTMLAttribute("width");
+ }
+
+ public void setWidth(String width)
+ {
+ setHTMLAttribute("width", width);
+ }
+
+ public HTMLElement createTHead()
+ {
+ HTMLTableSectionElement ref = getTHead();
+ if (ref == null)
+ {
+ return (HTMLElement) getOwnerDocument().createElement("thead");
+ }
+ else
+ {
+ return ref;
+ }
+ }
+
+ public void deleteTHead()
+ {
+ HTMLTableSectionElement ref = getTHead();
+ if (ref != null)
+ {
+ removeChild(ref);
+ }
+ }
+
+ public HTMLElement createTFoot()
+ {
+ HTMLTableSectionElement ref = getTFoot();
+ if (ref == null)
+ {
+ return (HTMLElement) getOwnerDocument().createElement("tfoot");
+ }
+ else
+ {
+ return ref;
+ }
+ }
+
+ public void deleteTFoot()
+ {
+ HTMLTableSectionElement ref = getTFoot();
+ if (ref != null)
+ {
+ removeChild(ref);
+ }
+ }
+
+ public HTMLElement createCaption()
+ {
+ HTMLTableCaptionElement ref = getCaption();
+ if (ref == null)
+ {
+ return (HTMLElement) getOwnerDocument().createElement("caption");
+ }
+ else
+ {
+ return ref;
+ }
+ }
+
+ public void deleteCaption()
+ {
+ HTMLTableCaptionElement ref = getCaption();
+ if (ref != null)
+ {
+ removeChild(ref);
+ }
+ }
+
+ public HTMLElement insertRow(int index)
+ {
+ Node ref = getRow(index);
+ Node row = getOwnerDocument().createElement("tr");
+ if (ref == null)
+ {
+ Node tbody = getChildElement("tbody");
+ if (tbody == null)
+ {
+ tbody = getOwnerDocument().createElement("tfoot");
+ appendChild(tbody);
+ }
+ tbody.appendChild(row);
+ }
+ else
+ {
+ ref.getParentNode().insertBefore(row, ref);
+ }
+ return (HTMLElement) row;
+ }
+
+ public void deleteRow(int index)
+ {
+ Node ref = getRow(index);
+ if (ref == null)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ ref.getParentNode().removeChild(ref);
+ }
+
+ Node getRow(final int index)
+ {
+ int i = 0;
+ Node thead = getChildElement("thead");
+ if (thead != null)
+ {
+ for (Node ctx = thead.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ String ctxName = ctx.getLocalName();
+ if (ctxName == null)
+ {
+ ctxName = ctx.getNodeName();
+ }
+ if (!"tr".equalsIgnoreCase(ctxName))
+ {
+ continue;
+ }
+ if (index == i)
+ {
+ return ctx;
+ }
+ i++;
+ }
+ }
+ Node tbody = getChildElement("tbody");
+ if (tbody == null)
+ {
+ tbody = this;
+ }
+ for (Node ctx = tbody.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ String ctxName = ctx.getLocalName();
+ if (ctxName == null)
+ {
+ ctxName = ctx.getNodeName();
+ }
+ if (!"tr".equalsIgnoreCase(ctxName))
+ {
+ continue;
+ }
+ if (index == i)
+ {
+ return ctx;
+ }
+ i++;
+ }
+ Node tfoot = getChildElement("tfoot");
+ if (tfoot != null)
+ {
+ for (Node ctx = tfoot.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ String ctxName = ctx.getLocalName();
+ if (ctxName == null)
+ {
+ ctxName = ctx.getNodeName();
+ }
+ if (!"tr".equalsIgnoreCase(ctxName))
+ {
+ continue;
+ }
+ if (index == i)
+ {
+ return ctx;
+ }
+ i++;
+ }
+ }
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableRowElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableRowElement.java
new file mode 100644
index 000000000..9943585f4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableRowElement.java
@@ -0,0 +1,228 @@
+/* DomHTMLTableRowElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomDOMException;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+import org.w3c.dom.html2.HTMLCollection;
+import org.w3c.dom.html2.HTMLElement;
+import org.w3c.dom.html2.HTMLTableRowElement;
+
+/**
+ * An HTML 'TR' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLTableRowElement
+ extends DomHTMLElement
+ implements HTMLTableRowElement
+{
+
+ protected DomHTMLTableRowElement(DomHTMLDocument owner,
+ String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public int getRowIndex()
+ {
+ return getIndex();
+ }
+
+ public int getSectionRowIndex()
+ {
+ int index = 0;
+ DomHTMLElement parent = (DomHTMLElement) getParentElement("table");
+ if (parent != null)
+ {
+ Node thead = parent.getChildElement("thead");
+ if (thead != null)
+ {
+ for (Node ctx = thead.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if (ctx == this)
+ {
+ return index;
+ }
+ index++;
+ }
+ }
+ Node tbody = parent.getChildElement("tbody");
+ if (tbody != null)
+ {
+ for (Node ctx = tbody.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if (ctx == this)
+ {
+ return index;
+ }
+ index++;
+ }
+ }
+ Node tfoot = parent.getChildElement("tfoot");
+ if (tfoot != null)
+ {
+ for (Node ctx = tfoot.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if (ctx == this)
+ {
+ return index;
+ }
+ index++;
+ }
+ }
+ }
+ throw new DomDOMException(DOMException.NOT_FOUND_ERR);
+ }
+
+ public HTMLCollection getCells()
+ {
+ DomHTMLCollection ret =
+ new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
+ ret.addNodeName("th");
+ ret.addNodeName("td");
+ ret.evaluate();
+ return ret;
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getBgColor()
+ {
+ return getHTMLAttribute("bgcolor");
+ }
+
+ public void setBgColor(String bgColor)
+ {
+ setHTMLAttribute("bgcolor", bgColor);
+ }
+
+ public String getCh()
+ {
+ return getHTMLAttribute("char");
+ }
+
+ public void setCh(String ch)
+ {
+ setHTMLAttribute("char", ch);
+ }
+
+ public String getChOff()
+ {
+ return getHTMLAttribute("charoff");
+ }
+
+ public void setChOff(String chOff)
+ {
+ setHTMLAttribute("charoff", chOff);
+ }
+
+ public String getVAlign()
+ {
+ return getHTMLAttribute("valign");
+ }
+
+ public void setVAlign(String vAlign)
+ {
+ setHTMLAttribute("valign", vAlign);
+ }
+
+ public HTMLElement insertCell(int index)
+ {
+ Node ref = getCell(index);
+ Node cell = getOwnerDocument().createElement("td");
+ if (ref == null)
+ {
+ appendChild(cell);
+ }
+ else
+ {
+ insertBefore(cell, ref);
+ }
+ return (HTMLElement) cell;
+ }
+
+ public void deleteCell(int index)
+ {
+ Node ref = getCell(index);
+ if (ref == null)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ removeChild(ref);
+ }
+
+ Node getCell(final int index)
+ {
+ int i = 0;
+ for (Node ctx = getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ String name = ctx.getLocalName();
+ if (name == null)
+ {
+ name = ctx.getNodeName();
+ }
+ if (!"td".equalsIgnoreCase(name) &&
+ !"th".equalsIgnoreCase(name))
+ {
+ continue;
+ }
+ if (index == i)
+ {
+ return ctx;
+ }
+ i++;
+ }
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableSectionElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableSectionElement.java
new file mode 100644
index 000000000..389eb591b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableSectionElement.java
@@ -0,0 +1,162 @@
+/* DomHTMLTableSectionElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomDOMException;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+import org.w3c.dom.html2.HTMLCollection;
+import org.w3c.dom.html2.HTMLElement;
+import org.w3c.dom.html2.HTMLTableSectionElement;
+
+/**
+ * An HTML 'THEAD', 'TFOOT', or 'TBODY' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLTableSectionElement
+ extends DomHTMLElement
+ implements HTMLTableSectionElement
+{
+
+ protected DomHTMLTableSectionElement(DomHTMLDocument owner,
+ String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getCh()
+ {
+ return getHTMLAttribute("char");
+ }
+
+ public void setCh(String ch)
+ {
+ setHTMLAttribute("char", ch);
+ }
+
+ public String getChOff()
+ {
+ return getHTMLAttribute("charoff");
+ }
+
+ public void setChOff(String chOff)
+ {
+ setHTMLAttribute("charoff", chOff);
+ }
+
+ public String getVAlign()
+ {
+ return getHTMLAttribute("valign");
+ }
+
+ public void setVAlign(String vAlign)
+ {
+ setHTMLAttribute("valign", vAlign);
+ }
+
+ public HTMLCollection getRows()
+ {
+ DomHTMLCollection ret =
+ new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
+ ret.addNodeName("tr");
+ ret.evaluate();
+ return ret;
+ }
+
+ public HTMLElement insertRow(int index)
+ {
+ Node ref = getRow(index);
+ Node row = getOwnerDocument().createElement("tr");
+ if (ref == null)
+ {
+ appendChild(row);
+ }
+ else
+ {
+ insertBefore(row, ref);
+ }
+ return (HTMLElement) row;
+ }
+
+ public void deleteRow(int index)
+ {
+ Node ref = getRow(index);
+ if (ref == null)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ removeChild(ref);
+ }
+
+ Node getRow(final int index)
+ {
+ int i = 0;
+ for (Node ctx = getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ String name = ctx.getLocalName();
+ if (name == null)
+ {
+ name = ctx.getNodeName();
+ }
+ if (!"tr".equalsIgnoreCase(name))
+ {
+ continue;
+ }
+ if (index == i)
+ {
+ return ctx;
+ }
+ i++;
+ }
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTextAreaElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTextAreaElement.java
new file mode 100644
index 000000000..9acfab134
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTextAreaElement.java
@@ -0,0 +1,181 @@
+/* DomHTMLTextAreaElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLTextAreaElement;
+
+/**
+ * An HTML 'TEXTAREA' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLTextAreaElement
+ extends DomHTMLElement
+ implements HTMLTextAreaElement
+{
+
+ protected String value;
+
+ protected DomHTMLTextAreaElement(DomHTMLDocument owner,
+ String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getDefaultValue()
+ {
+ return getHTMLAttribute("value");
+ }
+
+ public void setDefaultValue(String defaultValue)
+ {
+ setHTMLAttribute("value", defaultValue);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public String getAccessKey()
+ {
+ return getHTMLAttribute("accesskey");
+ }
+
+ public void setAccessKey(String accessKey)
+ {
+ setHTMLAttribute("accesskey", accessKey);
+ }
+
+ public int getCols()
+ {
+ return getIntHTMLAttribute("cols");
+ }
+
+ public void setCols(int cols)
+ {
+ setIntHTMLAttribute("cols", cols);
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public boolean getReadOnly()
+ {
+ return getBooleanHTMLAttribute("readOnly");
+ }
+
+ public void setReadOnly(boolean readOnly)
+ {
+ setBooleanHTMLAttribute("readonly", readOnly);
+ }
+
+ public int getRows()
+ {
+ return getIntHTMLAttribute("rows");
+ }
+
+ public void setRows(int rows)
+ {
+ setIntHTMLAttribute("rows", rows);
+ }
+
+ public int getTabIndex()
+ {
+ return getIntHTMLAttribute("tabindex");
+ }
+
+ public void setTabIndex(int tabIndex)
+ {
+ setIntHTMLAttribute("tabindex", tabIndex);
+ }
+
+ public String getType()
+ {
+ return "textarea";
+ }
+
+ public String getValue()
+ {
+ if (value == null)
+ {
+ value = getDefaultValue();
+ }
+ return value;
+ }
+
+ public void setValue(String value)
+ {
+ this.value = value;
+ }
+
+ public void blur()
+ {
+ dispatchUIEvent("blur");
+ }
+
+ public void focus()
+ {
+ dispatchUIEvent("focus");
+ }
+
+ public void select()
+ {
+ dispatchUIEvent("select");
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTitleElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTitleElement.java
new file mode 100644
index 000000000..4f581061b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTitleElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLTitleElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLTitleElement;
+
+/**
+ * An HTML 'TITLE' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLTitleElement
+ extends DomHTMLElement
+ implements HTMLTitleElement
+{
+
+ protected DomHTMLTitleElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getText()
+ {
+ return getTextContent();
+ }
+
+ public void setText(String text)
+ {
+ setTextContent(text);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLUListElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLUListElement.java
new file mode 100644
index 000000000..39cdce7d3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLUListElement.java
@@ -0,0 +1,78 @@
+/* DomHTMLUListElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLUListElement;
+
+/**
+ * An HTML 'UL' element node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomHTMLUListElement
+ extends DomHTMLElement
+ implements HTMLUListElement
+{
+
+ protected DomHTMLUListElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getCompact()
+ {
+ return getBooleanHTMLAttribute("compact");
+ }
+
+ public void setCompact(boolean compact)
+ {
+ setBooleanHTMLAttribute("compact", compact);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSException.java b/libjava/classpath/gnu/xml/dom/ls/DomLSException.java
new file mode 100644
index 000000000..31efc845f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/DomLSException.java
@@ -0,0 +1,57 @@
+/* DomLSException.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import org.w3c.dom.ls.LSException;
+
+/**
+ * A DOM LS exception incorporating a cause.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomLSException
+ extends LSException
+{
+
+ public DomLSException(short type, Exception cause)
+ {
+ super(type, (cause == null) ? null : cause.getMessage());
+ initCause(cause);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSInput.java b/libjava/classpath/gnu/xml/dom/ls/DomLSInput.java
new file mode 100644
index 000000000..39b17694f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/DomLSInput.java
@@ -0,0 +1,159 @@
+/* DomLSInput.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import org.w3c.dom.ls.LSInput;
+
+/**
+ * Specification of XML input to parse.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomLSInput
+ implements LSInput
+{
+
+ private InputStream in;
+ private String systemId;
+ private String publicId;
+ private String baseURI;
+ private String encoding;
+ private boolean certifiedText;
+
+ public Reader getCharacterStream()
+ {
+ return new InputStreamReader(in);
+ }
+
+ public void setCharacterStream(Reader characterStream)
+ {
+ in = new ReaderInputStream(characterStream);
+ }
+
+ public InputStream getByteStream()
+ {
+ return in;
+ }
+
+ public void setByteStream(InputStream byteStream)
+ {
+ in = byteStream;
+ }
+
+ public String getStringData()
+ {
+ CPStringBuilder acc = new CPStringBuilder();
+ Reader reader = getCharacterStream();
+ try
+ {
+ char[] buf = new char[4096];
+ for (int len = reader.read(buf); len != -1; len = reader.read(buf))
+ {
+ acc.append(buf, 0, len);
+ }
+ }
+ catch (IOException e)
+ {
+ return null; // ?
+ }
+ return acc.toString();
+ }
+
+ public void setStringData(String stringData)
+ {
+ in = new ReaderInputStream(new StringReader(stringData));
+ }
+
+ public String getSystemId()
+ {
+ return systemId;
+ }
+
+ public void setSystemId(String systemId)
+ {
+ this.systemId = systemId;
+ }
+
+ public String getPublicId()
+ {
+ return publicId;
+ }
+
+ public void setPublicId(String publicId)
+ {
+ this.publicId = publicId;
+ }
+
+ public String getBaseURI()
+ {
+ return baseURI;
+ }
+
+ public void setBaseURI(String baseURI)
+ {
+ this.baseURI = baseURI;
+ }
+
+ public String getEncoding()
+ {
+ return encoding;
+ }
+
+ public void setEncoding(String encoding)
+ {
+ this.encoding = encoding;
+ }
+
+ public boolean getCertifiedText()
+ {
+ return certifiedText;
+ }
+
+ public void setCertifiedText(boolean certifiedText)
+ {
+ this.certifiedText = certifiedText;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSOutput.java b/libjava/classpath/gnu/xml/dom/ls/DomLSOutput.java
new file mode 100644
index 000000000..e8bec2ec1
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/DomLSOutput.java
@@ -0,0 +1,98 @@
+/* DomLSOutput.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import org.w3c.dom.ls.LSOutput;
+
+/**
+ * Specification of XML output to produce.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomLSOutput
+ implements LSOutput
+{
+
+ private OutputStream out;
+ private String systemId;
+ private String encoding;
+
+ public Writer getCharacterStream()
+ {
+ return new OutputStreamWriter(out);
+ }
+
+ public void setCharacterStream(Writer characterStream)
+ {
+ out = new WriterOutputStream(characterStream);
+ }
+
+ public OutputStream getByteStream()
+ {
+ return out;
+ }
+
+ public void setByteStream(OutputStream out)
+ {
+ this.out = out;
+ }
+
+ public String getSystemId()
+ {
+ return systemId;
+ }
+
+ public void setSystemId(String systemId)
+ {
+ this.systemId = systemId;
+ }
+
+ public String getEncoding()
+ {
+ return encoding;
+ }
+
+ public void setEncoding(String encoding)
+ {
+ this.encoding = encoding;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSParser.java b/libjava/classpath/gnu/xml/dom/ls/DomLSParser.java
new file mode 100644
index 000000000..99db79d64
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/DomLSParser.java
@@ -0,0 +1,567 @@
+/* DomLSParser.java --
+ Copyright (C) 1999,2000,2001,2007 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.List;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.DOMConfiguration;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.DOMStringList;
+import org.w3c.dom.Node;
+import org.w3c.dom.ls.DOMImplementationLS;
+import org.w3c.dom.ls.LSException;
+import org.w3c.dom.ls.LSInput;
+import org.w3c.dom.ls.LSParser;
+import org.w3c.dom.ls.LSParserFilter;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import gnu.xml.dom.DomDocument;
+import gnu.xml.dom.DomDOMException;
+
+/**
+ * Parser implementation for GNU DOM.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomLSParser
+ implements LSParser, DOMConfiguration, DOMStringList, ErrorHandler
+{
+
+ private static final List SUPPORTED_PARAMETERS
+ = Arrays.asList(new String[] { "cdata-sections",
+ "comments",
+ "element-content-whitespace",
+ "namespaces",
+ "expand-entity-references",
+ "coalescing",
+ "validating",
+ "xinclude-aware",
+ "entity-resolver",
+ "error-handler" });
+
+ private LSParserFilter filter;
+ private final boolean async;
+ private String schemaType;
+ private SAXEventSink eventSink;
+ private SAXParserFactory factory;
+ private XMLReader reader;
+
+ private boolean namespaceAware = true;
+ private boolean ignoreWhitespace;
+ private boolean expandEntityReferences;
+ private boolean ignoreComments;
+ private boolean coalescing;
+ private boolean validating;
+ private boolean xIncludeAware;
+ private EntityResolver entityResolver;
+ private ErrorHandler errorHandler;
+
+ public DomLSParser(short mode, String schemaType)
+ throws DOMException
+ {
+ switch (mode)
+ {
+ case DOMImplementationLS.MODE_ASYNCHRONOUS:
+ async = true;
+ break;
+ case DOMImplementationLS.MODE_SYNCHRONOUS:
+ async = false;
+ break;
+ default:
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
+ }
+ // TODO schemaType
+ this.schemaType = schemaType;
+ factory = SAXParserFactory.newInstance();
+ }
+
+ // -- LSParser --
+
+ public DOMConfiguration getDomConfig()
+ {
+ return this;
+ }
+
+ public LSParserFilter getFilter()
+ {
+ return filter;
+ }
+
+ public void setFilter(LSParserFilter filter)
+ {
+ this.filter = filter;
+ }
+
+ public boolean getAsync()
+ {
+ return async;
+ }
+
+ public boolean getBusy()
+ {
+ return eventSink != null;
+ }
+
+ public Document parse(LSInput input)
+ throws DOMException, LSException
+ {
+ if (async)
+ {
+ return doParse(input);
+ }
+ else
+ {
+ synchronized (this)
+ {
+ return doParse(input);
+ }
+ }
+ }
+
+ public Document parseURI(String uri)
+ throws DOMException, LSException
+ {
+ LSInput input = new DomLSInput();
+ input.setSystemId(uri);
+ return parse(input);
+ }
+
+ public Node parseWithContext(LSInput input, Node context, short action)
+ throws DOMException, LSException
+ {
+ Document doc = (context.getNodeType() == Node.DOCUMENT_NODE) ?
+ (Document) context : context.getOwnerDocument();
+ input.setBaseURI(doc.getDocumentURI());
+ // TODO use namespaces defined on context node
+ Document ret = parse(input);
+ Node root = ret.getDocumentElement();
+ root = doc.adoptNode(root);
+ switch (action)
+ {
+ case ACTION_APPEND_AS_CHILDREN:
+ context.appendChild(root);
+ break;
+ case ACTION_REPLACE_CHILDREN:
+ Node c1 = context.getFirstChild();
+ while (c1 != null)
+ {
+ Node next = c1.getNextSibling();
+ context.removeChild(c1);
+ c1 = next;
+ }
+ context.appendChild(root);
+ break;
+ case ACTION_INSERT_BEFORE:
+ Node p1 = context.getParentNode();
+ p1.insertBefore(root, context);
+ break;
+ case ACTION_INSERT_AFTER:
+ Node p2 = context.getParentNode();
+ Node r1 = context.getNextSibling();
+ if (r1 == null)
+ {
+ p2.appendChild(root);
+ }
+ else
+ {
+ p2.insertBefore(root, r1);
+ }
+ break;
+ case ACTION_REPLACE:
+ Node p3 = context.getParentNode();
+ Node r2 = context.getNextSibling();
+ p3.removeChild(context);
+ if (r2 == null)
+ {
+ p3.appendChild(root);
+ }
+ else
+ {
+ p3.insertBefore(root, r2);
+ }
+ break;
+ }
+ return root;
+ }
+
+ public void abort()
+ {
+ if (eventSink != null)
+ {
+ eventSink.interrupt();
+ }
+ }
+
+ private Document doParse(LSInput input)
+ throws DOMException, LSException
+ {
+ // create event sink
+ if (eventSink != null)
+ {
+ throw new LSException(LSException.PARSE_ERR, "parse in progress");
+ }
+ InputSource source = getInputSource(input);
+ eventSink = (filter == null) ? new SAXEventSink() :
+ new FilteredSAXEventSink(filter);
+ // configure sink
+ eventSink.setNamespaceAware(namespaceAware);
+ eventSink.ignoreWhitespace = ignoreWhitespace;
+ eventSink.expandEntityReferences = expandEntityReferences;
+ eventSink.ignoreComments = ignoreComments;
+ eventSink.coalescing = coalescing;
+ // get and configure reader
+ XMLReader reader = getXMLReader();
+ eventSink.reader = reader;
+ try
+ {
+ reader.setContentHandler(eventSink);
+ reader.setDTDHandler(eventSink);
+ reader.setProperty("http://xml.org/sax/properties/lexical-handler",
+ eventSink);
+ reader.setProperty("http://xml.org/sax/properties/declaration-handler",
+ eventSink);
+ reader.setFeature("http://xml.org/sax/features/namespaces",
+ namespaceAware);
+ reader.setFeature("http://xml.org/sax/features/namespace-prefixes",
+ true);
+ reader.setFeature("http://xml.org/sax/features/validation",
+ validating);
+ try
+ {
+ reader.setFeature("http://gnu.org/sax/features/coalescing",
+ coalescing);
+ }
+ catch (SAXNotRecognizedException e)
+ {
+ // ignore
+ }
+ try
+ {
+ reader.setFeature("http://xml.org/sax/features/use-attributes2",
+ true);
+ }
+ catch (SAXNotRecognizedException e)
+ {
+ // ignore
+ }
+ try
+ {
+ reader.setFeature("http://xml.org/sax/features/external-general-entities",
+ true);
+ }
+ catch (SAXNotRecognizedException e)
+ {
+ // ignore
+ }
+ reader.setEntityResolver(entityResolver);
+ reader.setErrorHandler(errorHandler);
+ // parse
+ reader.parse(source);
+ }
+ catch (DOMException e)
+ {
+ reader = null;
+ eventSink = null;
+ throw e;
+ }
+ catch (SAXException e)
+ {
+ reader = null;
+ eventSink = null;
+ throw new DomLSException(LSException.PARSE_ERR, e);
+ }
+ catch (IOException e)
+ {
+ reader = null;
+ eventSink = null;
+ throw new DomLSException(LSException.PARSE_ERR, e);
+ }
+ // return document
+ Document ret = eventSink.doc;
+ String systemId = input.getSystemId();
+ if (systemId != null && ret instanceof DomDocument)
+ {
+ ((DomDocument) ret).setDocumentURI(systemId);
+ }
+ eventSink = null;
+ return ret;
+ }
+
+ private XMLReader getXMLReader()
+ throws LSException
+ {
+ if (reader == null)
+ {
+ factory.setNamespaceAware(namespaceAware);
+ factory.setValidating(validating);
+ factory.setXIncludeAware(xIncludeAware);
+ try
+ {
+ SAXParser parser = factory.newSAXParser();
+ reader = parser.getXMLReader();
+ }
+ catch (ParserConfigurationException e)
+ {
+ throw new DomLSException(LSException.PARSE_ERR, e);
+ }
+ catch (SAXException e)
+ {
+ throw new DomLSException(LSException.PARSE_ERR, e);
+ }
+ }
+ return reader;
+ }
+
+ private InputSource getInputSource(LSInput input)
+ throws LSException
+ {
+ InputSource source = null;
+ String systemId = input.getSystemId();
+ InputStream in = input.getByteStream();
+ if (in != null)
+ {
+ source = new InputSource(in);
+ source.setSystemId(systemId);
+ }
+ if (source == null)
+ {
+ URL url = null;
+ String base = input.getBaseURI();
+ try
+ {
+ try
+ {
+ URL baseURL = (base == null) ? null : new URL(base);
+ url = (baseURL == null) ? new URL(systemId) :
+ new URL(baseURL, systemId);
+ }
+ catch (MalformedURLException e)
+ {
+ File baseFile = (base == null) ? null : new File(base);
+ url = (baseFile == null) ? new File(systemId).toURL() :
+ new File(baseFile, systemId).toURL();
+ }
+ in = url.openStream();
+ systemId = url.toString();
+ source = new InputSource(in);
+ source.setSystemId(systemId);
+ }
+ catch (IOException e)
+ {
+ throw new DomLSException(LSException.PARSE_ERR, e);
+ }
+ }
+ return source;
+ }
+
+ // -- DOMConfiguration --
+
+ public void setParameter(String name, Object value)
+ throws DOMException
+ {
+ name = name.toLowerCase();
+ if ("cdata-sections".equals(name))
+ {
+ coalescing = !((Boolean) value).booleanValue();
+ }
+ else if ("comments".equals(name))
+ {
+ ignoreComments = !((Boolean) value).booleanValue();
+ }
+ else if ("element-content-whitespace".equals(name))
+ {
+ ignoreWhitespace = !((Boolean) value).booleanValue();
+ }
+ else if ("namespaces".equals(name))
+ {
+ namespaceAware = ((Boolean) value).booleanValue();
+ }
+ else if ("expand-entity-references".equals(name))
+ {
+ expandEntityReferences = ((Boolean) value).booleanValue();
+ }
+ else if ("coalescing".equals(name))
+ {
+ coalescing = ((Boolean) value).booleanValue();
+ }
+ else if ("validating".equals(name))
+ {
+ validating = ((Boolean) value).booleanValue();
+ }
+ else if ("xinclude-aware".equals(name))
+ {
+ xIncludeAware = ((Boolean) value).booleanValue();
+ }
+ else if ("entity-resolver".equals(name))
+ {
+ entityResolver = (EntityResolver) value;
+ }
+ else if ("error-handler".equals(name))
+ {
+ errorHandler = (ErrorHandler) value;
+ }
+ else
+ {
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
+ }
+ // invalidate reader, a new one will be created
+ reader = null;
+ }
+
+ public Object getParameter(String name)
+ throws DOMException
+ {
+ name = name.toLowerCase();
+ if ("cdata-sections".equals(name))
+ {
+ return coalescing ? Boolean.FALSE : Boolean.TRUE;
+ }
+ else if ("comments".equals(name))
+ {
+ return ignoreComments ? Boolean.FALSE : Boolean.TRUE;
+ }
+ else if ("element-content-whitespace".equals(name))
+ {
+ return ignoreWhitespace ? Boolean.FALSE : Boolean.TRUE;
+ }
+ else if ("namespaces".equals(name))
+ {
+ return namespaceAware ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("expand-entity-references".equals(name))
+ {
+ return expandEntityReferences ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("coalescing".equals(name))
+ {
+ return coalescing ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("validating".equals(name))
+ {
+ return validating ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("xinclude-aware".equals(name))
+ {
+ return xIncludeAware ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("entity-resolver".equals(name))
+ {
+ return entityResolver;
+ }
+ else if ("error-handler".equals(name))
+ {
+ return errorHandler;
+ }
+ else
+ {
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
+ }
+ }
+
+ public boolean canSetParameter(String name, Object value)
+ {
+ return contains(name);
+ }
+
+ public DOMStringList getParameterNames()
+ {
+ return this;
+ }
+
+ // -- DOMStringList --
+
+ public String item(int i)
+ {
+ return (String) SUPPORTED_PARAMETERS.get(i);
+ }
+
+ public int getLength()
+ {
+ return SUPPORTED_PARAMETERS.size();
+ }
+
+ public boolean contains(String str)
+ {
+ return SUPPORTED_PARAMETERS.contains(str);
+ }
+
+ // -- ErrorHandler --
+
+ public void warning(SAXParseException e)
+ throws SAXException
+ {
+ if (errorHandler != null)
+ {
+ errorHandler.warning(e);
+ }
+ }
+
+ public void error(SAXParseException e)
+ throws SAXException
+ {
+ if (errorHandler != null)
+ {
+ errorHandler.error(e);
+ }
+ }
+
+ public void fatalError(SAXParseException e)
+ throws SAXException
+ {
+ if (errorHandler != null)
+ {
+ errorHandler.fatalError(e);
+ }
+ abort();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSSerializer.java b/libjava/classpath/gnu/xml/dom/ls/DomLSSerializer.java
new file mode 100644
index 000000000..c282b0b9f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/DomLSSerializer.java
@@ -0,0 +1,353 @@
+/* DomLSSerializer.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Arrays;
+import java.util.List;
+import org.w3c.dom.DOMConfiguration;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.DOMStringList;
+import org.w3c.dom.Node;
+import org.w3c.dom.ls.LSException;
+import org.w3c.dom.ls.LSOutput;
+import org.w3c.dom.ls.LSSerializer;
+import org.w3c.dom.ls.LSSerializerFilter;
+import org.w3c.dom.traversal.NodeFilter;
+import gnu.xml.dom.DomDOMException;
+import gnu.xml.transform.StreamSerializer;
+
+/**
+ * Serialize a DOM node to a stream.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DomLSSerializer
+ extends StreamSerializer
+ implements LSSerializer, DOMConfiguration, DOMStringList
+{
+
+ private static final List SUPPORTED_PARAMETERS =
+ Arrays.asList(new String[] {"discard-default-content",
+ "xml-declaration"});
+
+ private LSSerializerFilter filter;
+ private StreamSerializer serializer;
+
+ public DomLSSerializer()
+ {
+ super();
+ discardDefaultContent = true;
+ }
+
+ // -- LSSerializer --
+
+ public DOMConfiguration getDomConfig()
+ {
+ return this;
+ }
+
+ public String getNewLine()
+ {
+ return eol;
+ }
+
+ public void setNewLine(String newLine)
+ {
+ if (newLine == null)
+ {
+ newLine = System.getProperty("line.separator");
+ }
+ eol = newLine;
+ }
+
+ public LSSerializerFilter getFilter()
+ {
+ return filter;
+ }
+
+ public void setFilter(LSSerializerFilter filter)
+ {
+ this.filter = filter;
+ }
+
+ public boolean write(Node node, LSOutput output)
+ throws LSException
+ {
+ OutputStream out = output.getByteStream();
+ try
+ {
+ if (out == null)
+ {
+ String systemId = output.getSystemId();
+ try
+ {
+ URL url = new URL(systemId);
+ URLConnection connection = url.openConnection();
+ connection.setDoOutput(true);
+ if (connection instanceof HttpURLConnection)
+ {
+ ((HttpURLConnection) connection).setRequestMethod("PUT");
+ }
+ out = connection.getOutputStream();
+ }
+ catch (MalformedURLException e)
+ {
+ File file = new File(systemId);
+ out = new FileOutputStream(file);
+ }
+ }
+ serialize(node, out);
+ out.flush();
+ return true;
+ }
+ catch (IOException e)
+ {
+ throw new DomLSException(LSException.SERIALIZE_ERR, e);
+ }
+ }
+
+ public boolean writeToURI(Node node, String uri)
+ throws LSException
+ {
+ LSOutput output = new DomLSOutput();
+ output.setSystemId(uri);
+ return write(node, output);
+ }
+
+ public String writeToString(Node node)
+ throws DOMException, LSException
+ {
+ Writer writer = new StringWriter();
+ LSOutput output = new DomLSOutput();
+ output.setCharacterStream(writer);
+ write(node, output);
+ return writer.toString();
+ }
+
+ public void serialize(Node node, OutputStream out)
+ throws IOException
+ {
+ if (filter == null)
+ {
+ super.serialize(node, out);
+ }
+ else
+ {
+ int wts = filter.getWhatToShow();
+ if (wts != NodeFilter.SHOW_ALL)
+ {
+ switch (node.getNodeType())
+ {
+ case Node.ATTRIBUTE_NODE:
+ if ((wts & NodeFilter.SHOW_ATTRIBUTE) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.TEXT_NODE:
+ if ((wts & NodeFilter.SHOW_TEXT) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.ELEMENT_NODE:
+ if ((wts & NodeFilter.SHOW_ELEMENT) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.CDATA_SECTION_NODE:
+ if ((wts & NodeFilter.SHOW_CDATA_SECTION) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.COMMENT_NODE:
+ if ((wts & NodeFilter.SHOW_COMMENT) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.DOCUMENT_NODE:
+ if ((wts & NodeFilter.SHOW_DOCUMENT) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.DOCUMENT_TYPE_NODE:
+ if ((wts & NodeFilter.SHOW_DOCUMENT_TYPE) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ if ((wts & NodeFilter.SHOW_PROCESSING_INSTRUCTION) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.DOCUMENT_FRAGMENT_NODE:
+ if ((wts & NodeFilter.SHOW_DOCUMENT_FRAGMENT) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.ENTITY_NODE:
+ if ((wts & NodeFilter.SHOW_ENTITY) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.ENTITY_REFERENCE_NODE:
+ if ((wts & NodeFilter.SHOW_ENTITY_REFERENCE) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.NOTATION_NODE:
+ if ((wts & NodeFilter.SHOW_NOTATION) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ }
+ }
+ switch (filter.acceptNode(node))
+ {
+ case NodeFilter.FILTER_ACCEPT:
+ super.serialize(node, out);
+ break;
+ case NodeFilter.FILTER_REJECT:
+ break;
+ case NodeFilter.FILTER_SKIP:
+ Node first = node.getFirstChild();
+ if (first != null)
+ {
+ serialize(first, out);
+ }
+ break;
+ }
+ }
+ }
+
+ // -- DOMConfiguration --
+
+ public void setParameter(String name, Object value)
+ throws DOMException
+ {
+ if ("discard-default-content".equals(name))
+ {
+ discardDefaultContent = "true".equals(value.toString());
+ }
+ else if ("xml-declaration".equals(name))
+ {
+ xmlDeclaration = "false".equals(value.toString());
+ }
+ else
+ {
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
+ }
+ }
+
+ public Object getParameter(String name)
+ throws DOMException
+ {
+ if ("discard-default-content".equals(name))
+ {
+ return discardDefaultContent ? "true" : "false";
+ }
+ else if ("xml-declaration".equals(name))
+ {
+ return xmlDeclaration ? "true" : "false";
+ }
+ else
+ {
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
+ }
+ }
+
+ public boolean canSetParameter(String name, Object value)
+ {
+ return contains(name);
+ }
+
+ public DOMStringList getParameterNames()
+ {
+ return this;
+ }
+
+ // -- DOMStringList --
+
+ public String item(int i)
+ {
+ return (String) SUPPORTED_PARAMETERS.get(i);
+ }
+
+ public int getLength()
+ {
+ return SUPPORTED_PARAMETERS.size();
+ }
+
+ public boolean contains(String str)
+ {
+ return SUPPORTED_PARAMETERS.contains(str);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/FilteredSAXEventSink.java b/libjava/classpath/gnu/xml/dom/ls/FilteredSAXEventSink.java
new file mode 100644
index 000000000..65c1d37a4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/FilteredSAXEventSink.java
@@ -0,0 +1,353 @@
+/* FilteredSAXEventSink.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import java.util.LinkedList;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+import org.w3c.dom.ls.LSParserFilter;
+import org.w3c.dom.traversal.NodeFilter;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+
+/**
+ * A SAX event sink that calls out to a parser filter in order to decide
+ * whether to insert nodes into the tree.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class FilteredSAXEventSink
+ extends SAXEventSink
+{
+
+ final LSParserFilter filter;
+ final int whatToShow;
+
+ /**
+ * Stack of elements to insert.
+ */
+ LinkedList nodes;
+
+ /**
+ * Corresponding stack of filter decisions about the nodes.
+ */
+ LinkedList decisions;
+
+ /**
+ * True when rejecting child nodes.
+ */
+ boolean rejecting;
+
+ FilteredSAXEventSink(LSParserFilter filter)
+ {
+ this.filter = filter;
+ whatToShow = filter.getWhatToShow();
+ }
+
+ public void startDocument()
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ nodes = new LinkedList();
+ decisions = new LinkedList();
+
+ super.startDocument();
+ }
+
+ public void endDocument()
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ super.endDocument();
+
+ switch (getDecision(ctx, false))
+ {
+ case LSParserFilter.FILTER_REJECT:
+ ctx = null;
+ doc = null;
+ break;
+ }
+
+ nodes = null;
+ decisions = null;
+ }
+
+ public void startElement(String uri, String localName, String qName,
+ Attributes atts)
+ throws SAXException
+ {
+ if (rejecting || interrupted)
+ {
+ return;
+ }
+ Element element = createElement(uri, localName, qName, atts);
+ ctx = element;
+
+ short decision = getDecision(element, true);
+ nodes.addLast(element);
+ decisions.addLast(new Short(decision));
+
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_REJECT:
+ rejecting = true;
+ break;
+ case LSParserFilter.FILTER_INTERRUPT:
+ interrupted = true;
+ break;
+ }
+ }
+
+ protected Attr createAttr(Attributes atts, int index)
+ {
+ Attr attr = super.createAttr(atts, index);
+ short decision = getDecision(attr, false);
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_REJECT:
+ return null;
+ case LSParserFilter.FILTER_INTERRUPT:
+ interrupted = true;
+ return null;
+ }
+ return attr;
+ }
+
+ public void endElement(String uri, String localName, String qName)
+ throws SAXException
+ {
+ if (rejecting || interrupted)
+ {
+ return;
+ }
+ super.endElement(uri, localName, qName);
+
+ Element element = (Element) nodes.removeLast();
+ Node parent = nodes.isEmpty() ? doc : (Node) nodes.getLast();
+ ctx = parent;
+ short decision = ((Short) decisions.removeLast()).shortValue();
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_SKIP:
+ // Add all children of element to parent
+ for (Node child = element.getFirstChild(); child != null;
+ child = child.getNextSibling())
+ {
+ parent.insertBefore(child, element);
+ }
+ return;
+ case LSParserFilter.FILTER_REJECT:
+ rejecting = false;
+ break;
+ }
+ decision = getDecision(element, false);
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_ACCEPT:
+ parent.appendChild(element);
+ break;
+ case LSParserFilter.FILTER_INTERRUPT:
+ interrupted = true;
+ break;
+ }
+ }
+
+ public void characters(char[] c, int off, int len)
+ throws SAXException
+ {
+ if (rejecting || interrupted)
+ {
+ return;
+ }
+ Text text = createText(c, off, len);
+ short decision = getDecision(text, false);
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_ACCEPT:
+ ctx.appendChild(text);
+ break;
+ case LSParserFilter.FILTER_INTERRUPT:
+ interrupted = true;
+ break;
+ }
+ }
+
+ public void processingInstruction(String target, String data)
+ throws SAXException
+ {
+ if (rejecting || interrupted || inDTD)
+ {
+ return;
+ }
+ Node pi = createProcessingInstruction(target, data);
+ short decision = getDecision(pi, false);
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_ACCEPT:
+ ctx.appendChild(pi);
+ break;
+ case LSParserFilter.FILTER_INTERRUPT:
+ interrupted = true;
+ break;
+ }
+ }
+
+ public void startDTD(String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ Node doctype = createDocumentType(name, publicId, systemId);
+ ctx = doctype;
+ inDTD = true;
+ nodes.addLast(doctype);
+ decisions.addLast(new Short(LSParserFilter.FILTER_ACCEPT));
+ }
+
+ public void endDTD()
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ Node doctype = (Node) nodes.removeLast();
+ decisions.removeLast();
+ inDTD = false;
+ ctx = doc;
+ short decision = getDecision(doctype, false);
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_ACCEPT:
+ ctx.appendChild(doctype);
+ break;
+ case LSParserFilter.FILTER_INTERRUPT:
+ interrupted = true;
+ break;
+ }
+ }
+
+ public void comment(char[] c, int off, int len)
+ throws SAXException
+ {
+ if (rejecting || interrupted || inDTD)
+ {
+ return;
+ }
+ Node comment = createComment(c, off, len);
+ short decision = getDecision(comment, false);
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_ACCEPT:
+ ctx.appendChild(comment);
+ break;
+ case LSParserFilter.FILTER_INTERRUPT:
+ interrupted = true;
+ break;
+ }
+ }
+
+ // TODO declarations
+
+ short getDecision(Node node, boolean start)
+ {
+ boolean show = (whatToShow == NodeFilter.SHOW_ALL);
+ if (!show)
+ {
+ switch (node.getNodeType())
+ {
+ case Node.ATTRIBUTE_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_ATTRIBUTE) != 0);
+ break;
+ case Node.TEXT_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_TEXT) != 0);
+ break;
+ case Node.CDATA_SECTION_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_CDATA_SECTION) != 0);
+ break;
+ case Node.ELEMENT_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_ELEMENT) != 0);
+ break;
+ case Node.COMMENT_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_COMMENT) != 0);
+ break;
+ case Node.DOCUMENT_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_DOCUMENT) != 0);
+ break;
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_PROCESSING_INSTRUCTION) != 0);
+ break;
+ case Node.DOCUMENT_FRAGMENT_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_DOCUMENT_FRAGMENT) != 0);
+ break;
+ case Node.DOCUMENT_TYPE_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_DOCUMENT_TYPE) != 0);
+ break;
+ case Node.ENTITY_REFERENCE_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_ENTITY_REFERENCE) != 0);
+ break;
+ case Node.ENTITY_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_ENTITY) != 0);
+ break;
+ case Node.NOTATION_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_NOTATION) != 0);
+ break;
+ }
+ }
+ if (!show)
+ {
+ return LSParserFilter.FILTER_ACCEPT;
+ }
+ if (start)
+ {
+ return filter.startElement((Element) node);
+ }
+ return filter.acceptNode(node);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/ReaderInputStream.java b/libjava/classpath/gnu/xml/dom/ls/ReaderInputStream.java
new file mode 100644
index 000000000..cf279ab39
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/ReaderInputStream.java
@@ -0,0 +1,236 @@
+/* ReaderInputStream.java --
+ Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * Character stream wrapper.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ * @author <a href='mailto:mark@klomp.org'>Mark Wielaard</a>
+ */
+public class ReaderInputStream
+ extends InputStream
+{
+
+ private Reader reader;
+ private String encoding;
+
+ // Holds extra spillover data if necessary
+ private byte extra[];
+ private int pos;
+
+ private byte extra_marked[];
+ private int pos_marked;
+
+ public ReaderInputStream(Reader reader)
+ {
+ this.reader = reader;
+ this.encoding = "UTF-8";
+ }
+
+ void setEncoding(String encoding)
+ {
+ this.encoding = encoding;
+ }
+
+ public int read()
+ throws IOException
+ {
+ if (extra != null)
+ {
+ int result = extra[pos];
+ pos++;
+ if (pos >= extra.length)
+ {
+ extra = null;
+ }
+ return result;
+ }
+ return reader.read();
+ }
+
+ public int read(byte[] b)
+ throws IOException
+ {
+ return read(b, 0, b.length);
+ }
+
+ public int read(byte[] b, int off, int len)
+ throws IOException
+ {
+ if (len == 0)
+ {
+ return 0;
+ }
+
+ if (extra != null)
+ {
+ int available = extra.length - pos;
+ int l = available < len ? available : len;
+ System.arraycopy(extra, 0, b, off, l);
+ pos += l;
+ if (pos >= extra.length)
+ {
+ extra = null;
+ }
+ return l;
+ }
+
+ char[] c = new char[len];
+ int l = reader.read(c, 0, len);
+ if (l == -1)
+ {
+ return -1;
+ }
+
+ String s = new String(c, 0, l);
+ byte[] d = s.getBytes(encoding);
+
+ int available = d.length;
+ int more = d.length - len;
+ if (more > 0)
+ {
+ extra = new byte[more];
+ pos = 0;
+ System.arraycopy(d, len, extra, 0, more);
+ available -= more;
+ }
+
+ System.arraycopy(d, 0, b, off, available);
+ return available;
+ }
+
+ public void close()
+ throws IOException
+ {
+ reader.close();
+ }
+
+ public boolean markSupported()
+ {
+ return reader.markSupported();
+ }
+
+ public void mark(int limit)
+ {
+ if (extra != null)
+ {
+ extra_marked = new byte[extra.length];
+ System.arraycopy(extra, 0, extra_marked, 0, extra.length);
+ pos_marked = pos;
+ }
+ else
+ {
+ extra_marked = null;
+ }
+
+ try
+ {
+ // Note that this might be a bit more than asked for.
+ // Because we might also have the extra_marked bytes.
+ // That is fine (and necessary for reset() to work).
+ reader.mark(limit);
+ }
+ catch (IOException ioe)
+ {
+ throw new RuntimeException(ioe);
+ }
+ }
+
+ public void reset()
+ throws IOException
+ {
+ extra = extra_marked;
+ pos = pos_marked;
+ extra_marked = null;
+
+ reader.reset();
+ }
+
+ public long skip(long n)
+ throws IOException
+ {
+ long done = 0;
+ if (extra != null)
+ {
+ int available = extra.length - pos;
+ done = available < n ? available : n;
+ pos += done;
+ if (pos >= extra.length)
+ {
+ extra = null;
+ }
+ }
+
+ n -= done;
+ if (n > 0)
+ {
+ return reader.skip(n) + done;
+ }
+ else
+ {
+ return done;
+ }
+ }
+
+ /**
+ * Returns conservative number of bytes available without blocking.
+ * Actual number of bytes that can be read without blocking might
+ * be (much) bigger.
+ */
+ public int available()
+ throws IOException
+ {
+ if (extra != null)
+ {
+ return pos - extra.length;
+ }
+
+ return reader.ready() ? 1 : 0;
+ }
+
+ public String toString()
+ {
+ return getClass().getName() + "[" + reader + ", " + encoding + "]";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/SAXEventSink.java b/libjava/classpath/gnu/xml/dom/ls/SAXEventSink.java
new file mode 100644
index 000000000..06333dd7e
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/SAXEventSink.java
@@ -0,0 +1,603 @@
+/* SAXEventSink.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import javax.xml.XMLConstants;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.Element;
+import org.w3c.dom.Entity;
+import org.w3c.dom.EntityReference;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.DTDHandler;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.ext.Attributes2;
+import org.xml.sax.ext.DeclHandler;
+import org.xml.sax.ext.LexicalHandler;
+import org.xml.sax.ext.Locator2;
+import gnu.xml.dom.DomAttr;
+import gnu.xml.dom.DomDocument;
+import gnu.xml.dom.DomDoctype;
+import gnu.xml.dom.DomNode;
+
+/**
+ * A SAX content and lexical handler used to construct a DOM document.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class SAXEventSink
+ implements ContentHandler, LexicalHandler, DTDHandler, DeclHandler
+{
+
+ private static final String XMLNS_URI = XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
+ private static final String XMLNS_PREFIX = XMLConstants.XMLNS_ATTRIBUTE;
+ private static final HashSet PREDEFINED_ENTITIES = new HashSet();
+ static
+ {
+ PREDEFINED_ENTITIES.add("amp");
+ PREDEFINED_ENTITIES.add("lt");
+ PREDEFINED_ENTITIES.add("gt");
+ PREDEFINED_ENTITIES.add("quot");
+ PREDEFINED_ENTITIES.add("apos");
+ }
+
+ private boolean namespaceAware;
+ boolean ignoreWhitespace;
+ boolean expandEntityReferences;
+ boolean ignoreComments;
+ boolean coalescing;
+
+ XMLReader reader; // reference back to the parser to get features
+
+ DomDocument doc; // document being constructed
+ Node ctx; // current context (parent node)
+ LinkedList entityCtx; // entity context
+ List pending; // namespace nodes waiting for a declaring element
+ Locator locator;
+ boolean inCDATA;
+ boolean inDTD;
+ boolean interrupted;
+
+ void interrupt()
+ {
+ interrupted = true;
+ }
+
+ public Document getDocument()
+ {
+ return doc;
+ }
+
+ public void setReader(XMLReader reader)
+ {
+ this.reader = reader;
+ }
+
+ // -- ContentHandler2 --
+
+ public void setDocumentLocator(Locator locator)
+ {
+ this.locator = locator;
+ }
+
+ public void setNamespaceAware(boolean namespaceAware)
+ {
+ this.namespaceAware = namespaceAware;
+ }
+
+ public void startDocument()
+ throws SAXException
+ {
+ if (namespaceAware)
+ {
+ pending = new LinkedList();
+ }
+ doc = new DomDocument();
+ doc.setStrictErrorChecking(false);
+ doc.setBuilding(true);
+ doc.setDefaultAttributes(false);
+ ctx = doc;
+
+ final String FEATURES = "http://xml.org/sax/features/";
+ final String PROPERTIES = "http://xml.org/sax/properties/";
+ final String GNU_PROPERTIES = "http://gnu.org/sax/properties/";
+
+ if (reader != null)
+ {
+ boolean standalone = reader.getFeature(FEATURES + "is-standalone");
+ doc.setXmlStandalone(standalone);
+ try
+ {
+ String version = (String) reader.getProperty(PROPERTIES +
+ "document-xml-version");
+ doc.setXmlVersion(version);
+ }
+ catch (SAXNotRecognizedException e)
+ {
+ }
+ catch (SAXNotSupportedException e)
+ {
+ }
+ try
+ {
+ String encoding = (String) reader.getProperty(GNU_PROPERTIES +
+ "document-xml-encoding");
+ doc.setXmlEncoding(encoding);
+ }
+ catch (SAXNotRecognizedException e)
+ {
+ }
+ catch (SAXNotSupportedException e)
+ {
+ }
+ }
+ if (locator != null && locator instanceof Locator2)
+ {
+ String encoding = ((Locator2) locator).getEncoding();
+ doc.setInputEncoding(encoding);
+ }
+ }
+
+ public void endDocument()
+ throws SAXException
+ {
+ doc.setStrictErrorChecking(true);
+ doc.setBuilding(false);
+ doc.setDefaultAttributes(true);
+ DomDoctype doctype = (DomDoctype) doc.getDoctype();
+ if (doctype != null)
+ {
+ doctype.makeReadonly();
+ }
+ ctx = null;
+ locator = null;
+ }
+
+ public void startPrefixMapping(String prefix, String uri)
+ throws SAXException
+ {
+ if (namespaceAware)
+ {
+ String nsName = (prefix != null && prefix.length() > 0) ?
+ XMLNS_PREFIX + ":" + prefix : XMLNS_PREFIX;
+ DomAttr ns = (DomAttr) doc.createAttributeNS(XMLNS_URI, nsName);
+ ns.setNodeValue(uri);
+ if (ctx.getNodeType() == Node.ATTRIBUTE_NODE)
+ {
+ // Add to owner element
+ Node target = ((Attr) ctx).getOwnerElement();
+ target.getAttributes().setNamedItemNS(ns);
+ }
+ else
+ {
+ // Add to pending list; namespace node will be inserted when
+ // element is seen
+ pending.add(ns);
+ }
+ }
+ }
+
+ public void endPrefixMapping(String prefix)
+ throws SAXException
+ {
+ }
+
+ public void startElement(String uri, String localName, String qName,
+ Attributes atts)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ Element element = createElement(uri, localName, qName, atts);
+ // add element to context
+ ctx.appendChild(element);
+ ctx = element;
+ }
+
+ protected Element createElement(String uri, String localName, String qName,
+ Attributes atts)
+ throws SAXException
+ {
+ // create element node
+ Element element = namespaceAware ?
+ doc.createElementNS(uri, qName) :
+ doc.createElement(qName);
+ NamedNodeMap attrs = element.getAttributes();
+ if (namespaceAware && !pending.isEmpty())
+ {
+ // add pending namespace nodes
+ for (Iterator i = pending.iterator(); i.hasNext(); )
+ {
+ Node ns = (Node) i.next();
+ attrs.setNamedItemNS(ns);
+ }
+ pending.clear();
+ }
+ // add attributes
+ int len = atts.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ // create attribute
+ Attr attr = createAttr(atts, i);
+ if (attr != null)
+ {
+ // add attribute to element
+ if (namespaceAware)
+ {
+ attrs.setNamedItemNS(attr);
+ }
+ else
+ {
+ attrs.setNamedItem(attr);
+ }
+ }
+ }
+ return element;
+ }
+
+ protected Attr createAttr(Attributes atts, int index)
+ {
+ DomAttr attr;
+ if (namespaceAware)
+ {
+ String a_uri = atts.getURI(index);
+ String a_qName = atts.getQName(index);
+ attr = (DomAttr) doc.createAttributeNS(a_uri, a_qName);
+ }
+ else
+ {
+ String a_qName = atts.getQName(index);
+ attr = (DomAttr) doc.createAttribute(a_qName);
+ }
+ attr.setNodeValue(atts.getValue(index));
+ if (atts instanceof Attributes2)
+ {
+ Attributes2 atts2 = (Attributes2) atts;
+ // TODO attr.setDeclared(atts2.isDeclared(index));
+ attr.setSpecified(atts2.isSpecified(index));
+ }
+ return attr;
+ }
+
+ public void endElement(String uri, String localName, String qName)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (namespaceAware)
+ {
+ pending.clear();
+ }
+ ctx = ctx.getParentNode();
+ }
+
+ public void characters(char[] c, int off, int len)
+ throws SAXException
+ {
+ if (interrupted || len < 1)
+ {
+ return;
+ }
+ ctx.appendChild(createText(c, off, len));
+ }
+
+ protected Text createText(char[] c, int off, int len)
+ throws SAXException
+ {
+ Text text = (inCDATA && !coalescing) ?
+ doc.createCDATASection(new String(c, off, len)) :
+ doc.createTextNode(new String(c, off, len));
+ return text;
+ }
+
+ public void ignorableWhitespace(char[] c, int off, int len)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (!ignoreWhitespace)
+ {
+ characters(c, off, len);
+ }
+ }
+
+ public void processingInstruction(String target, String data)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ Node pi = createProcessingInstruction(target, data);
+ ctx.appendChild(pi);
+ }
+
+ protected Node createProcessingInstruction(String target, String data)
+ {
+ return doc.createProcessingInstruction(target, data);
+ }
+
+ public void skippedEntity(String name)
+ throws SAXException
+ {
+ // This callback is totally pointless
+ }
+
+ // -- LexicalHandler --
+
+ public void startDTD(String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ Node doctype = createDocumentType(name, publicId, systemId);
+ doc.appendChild(doctype);
+ ctx = doctype;
+ inDTD = true;
+ }
+
+ protected Node createDocumentType(String name, String publicId,
+ String systemId)
+ {
+ return new DomDoctype(doc, name, publicId, systemId);
+ }
+
+ public void endDTD()
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ inDTD = false;
+ ctx = ctx.getParentNode();
+ }
+
+ public void startEntity(String name)
+ throws SAXException
+ {
+ if (interrupted)
+ return;
+ DocumentType doctype = doc.getDoctype();
+ if (doctype == null)
+ {
+ throw new SAXException("SAX parser error: " +
+ "reference to entity in undeclared doctype");
+ }
+ if ("[dtd]".equals(name) || name.charAt(0) == '%')
+ return;
+ if (PREDEFINED_ENTITIES.contains(name))
+ return;
+ // Get entity
+ NamedNodeMap entities = doctype.getEntities();
+ Entity entity = (Entity) entities.getNamedItem(name);
+ if (entity == null)
+ {
+ throw new SAXException("SAX parser error: " +
+ "reference to undeclared entity: " + name);
+ }
+ EntityReference ref = doc.createEntityReference(name);
+ // DomDocument populates with the entity replacement text, remove this
+ Node child = ref.getFirstChild();
+ while (child != null)
+ {
+ Node nextChild = child.getNextSibling();
+ ref.removeChild(child);
+ child = nextChild;
+ }
+ ctx.appendChild(ref);
+ ctx = ref;
+ }
+
+ public void endEntity(String name)
+ throws SAXException
+ {
+ if (interrupted)
+ return;
+ if ("[dtd]".equals(name) || name.charAt(0) == '%')
+ return;
+ if (PREDEFINED_ENTITIES.contains(name))
+ return;
+ // Get entity reference
+ EntityReference ref = (EntityReference) ctx;
+ if (!ref.getNodeName().equals(name))
+ throw new SAXException("expecting end of "+ref.getNodeName()+" entity");
+ ctx = ctx.getParentNode();
+ if (ref instanceof DomNode)
+ ((DomNode) ref).makeReadonly();
+ if (expandEntityReferences)
+ {
+ // Move entity content from reference node onto context
+ Node child = ref.getFirstChild();
+ while (child != null)
+ {
+ Node nextChild = child.getNextSibling();
+ ctx.appendChild(child);
+ child = nextChild;
+ }
+ ctx.removeChild(ref);
+ }
+ }
+
+ public void startCDATA()
+ throws SAXException
+ {
+ inCDATA = true;
+ }
+
+ public void endCDATA()
+ throws SAXException
+ {
+ inCDATA = false;
+ }
+
+ public void comment(char[] c, int off, int len)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ Node comment = createComment(c, off, len);
+ ctx.appendChild(comment);
+ }
+
+ protected Node createComment(char[] c, int off, int len)
+ {
+ return doc.createComment(new String(c, off, len));
+ }
+
+ // -- DTDHandler --
+
+ public void notationDecl(String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (!inDTD)
+ throw new SAXException("notation decl outside DTD");
+ DomDoctype doctype = (DomDoctype) ctx;
+ doctype.declareNotation(name, publicId, systemId);
+ }
+
+ public void unparsedEntityDecl(String name, String publicId, String systemId,
+ String notationName)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (!inDTD)
+ throw new SAXException("unparsed entity decl outside DTD");
+ DomDoctype doctype = (DomDoctype) ctx;
+ Entity entity = doctype.declareEntity(name, publicId, systemId,
+ notationName);
+ }
+
+ // -- DeclHandler --
+
+ public void elementDecl(String name, String model)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (!inDTD)
+ throw new SAXException("element decl outside DTD");
+ // Ignore fake element declarations generated by ValidationConsumer.
+ // If an element is not really declared in the DTD it will not be
+ // declared in the document model.
+ if (!(ctx instanceof DomDoctype))
+ {
+ return;
+ }
+ DomDoctype doctype = (DomDoctype) ctx;
+ doctype.elementDecl(name, model);
+ }
+
+ public void attributeDecl(String eName, String aName, String type,
+ String mode, String value)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (!inDTD)
+ throw new SAXException("attribute decl outside DTD");
+ DomDoctype doctype = (DomDoctype) ctx;
+ doctype.attributeDecl(eName, aName, type, mode, value);
+ }
+
+ public void internalEntityDecl(String name, String value)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (!inDTD)
+ throw new SAXException("internal entity decl outside DTD");
+ DomDoctype doctype = (DomDoctype) ctx;
+ Entity entity = doctype.declareEntity(name, null, null, null);
+ if (entity != null)
+ {
+ Node text = doc.createTextNode(value);
+ entity.appendChild(text);
+ }
+ }
+
+ public void externalEntityDecl(String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (!inDTD)
+ throw new SAXException("external entity decl outside DTD");
+ DomDoctype doctype = (DomDoctype) ctx;
+ Entity entity = doctype.declareEntity(name, publicId, systemId, null);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/WriterOutputStream.java b/libjava/classpath/gnu/xml/dom/ls/WriterOutputStream.java
new file mode 100644
index 000000000..f1ae344f4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/WriterOutputStream.java
@@ -0,0 +1,97 @@
+/* WriterOutputStream.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Writer;
+
+/**
+ * Character stream wrapper.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class WriterOutputStream
+ extends OutputStream
+{
+
+ private Writer writer;
+ private String encoding;
+
+ public WriterOutputStream(Writer writer)
+ {
+ this.writer = writer;
+ this.encoding = "UTF-8";
+ }
+
+ void setEncoding(String encoding)
+ {
+ this.encoding = encoding;
+ }
+
+ public void write(int c)
+ throws IOException
+ {
+ writer.write(c);
+ }
+
+ public void write(byte[] b)
+ throws IOException
+ {
+ write(b, 0, b.length);
+ }
+
+ public void write(byte[] b, int off, int len)
+ throws IOException
+ {
+ writer.write(new String(b, off, len, encoding));
+ }
+
+ public void close()
+ throws IOException
+ {
+ writer.close();
+ }
+
+ public void flush()
+ throws IOException
+ {
+ writer.flush();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/package.html b/libjava/classpath/gnu/xml/dom/package.html
new file mode 100644
index 000000000..fbc864a4d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/package.html
@@ -0,0 +1,273 @@
+<html>
+<body>
+
+<p>
+This is a Free Software DOM Level 3 implementation, supporting these features:
+<ul>
+<li>"XML"</li>
+<li>"Events"</li>
+<li>"MutationEvents"</li>
+<li>"HTMLEvents" (won't generate them though)</li>
+<li>"UIEvents" (also won't generate them)</li>
+<li>"USER-Events" (a conformant extension)</li>
+<li>"Traversal" (optional)</li>
+<li>"XPath"</li>
+<li>"LS" and "LS-Async"</li>
+</ul>
+It is intended to be a reasonable base both for
+experimentation and supporting additional DOM modules as clean layers.
+</p>
+
+<p>
+Note that while DOM does not specify its behavior in the
+face of concurrent access, this implementation does.
+Specifically:
+<ul>
+<li>If only one thread at a time accesses a Document,
+of if several threads cooperate for read-only access,
+then no concurrency conflicts will occur.</li>
+<li>If several threads mutate a given document
+(or send events using it) at the same time,
+there is currently no guarantee that
+they won't interfere with each other.</li>
+</ul>
+</p>
+
+<h3>Design Goals</h3>
+
+<p>
+A number of DOM implementations are available in Java, including
+commercial ones from Sun, IBM, Oracle, and DataChannel as well as
+noncommercial ones from Docuverse, OpenXML, and Silfide. Why have
+another? Some of the goals of this version:
+</p>
+
+<ul>
+<li>Advanced DOM support. This was the first generally available
+implementation of DOM Level 2 in Java, and one of the first Level 3
+and XPath implementations.</li>
+
+<li> Free Software. This one is distributed under the GPL (with
+"library exception") so it can be used with a different class of
+application.</li>
+
+<li>Second implementation syndrome. I can do it simpler this time
+around ... and heck, writing it only takes a bit over a day once you
+know your way around.</li>
+
+<li>Sanity check the then-current Last Call DOM draft. Best to find
+bugs early, when they're relatively fixable. Yes, bugs were found.</li>
+
+<li>Modularity. Most of the implementations mentioned above are part
+of huge packages; take all (including bugs, of which some have far
+too many), or take nothing. I prefer a menu approach, when possible.
+This code is standalone, not beholden to any particular parser or XSL
+or XPath code.</li>
+
+<li>OK, I'm a hacker, I like to write code.</li>
+</ul>
+
+<p>
+This also works with the GNU Compiler for Java (GCJ). GCJ promises
+to be quite the environment for programming Java, both directly and from
+C++ using the new CNI interfaces (which really use C++, unlike JNI). </p>
+
+
+<h3>Open Issues</h3>
+
+<p>At this writing:</p>
+<ul>
+<li>See below for some restrictions on the mutation event
+support ... some events aren't reported (and likely won't be).</li>
+
+<li>More testing and conformance work is needed.</li>
+
+<li>We need an XML Schema validator (actually we need validation in the DOM
+full stop).</li>
+</ul>
+
+<p>
+I ran a profiler a few times and remove some of the performance hotspots,
+but it's not tuned. Reporting mutation events, in particular, is
+rather costly -- it started at about a 40% penalty for appendNode calls,
+I've got it down around 12%, but it'll be hard to shrink it much further.
+The overall code size is relatively small, though you may want to be rid of
+many of the unused DOM interface classes (HTML, CSS, and so on).
+</p>
+
+
+<h2><a name="features">Features of this Package</a></h2>
+
+<p> Starting with DOM Level 2, you can really see that DOM is constructed
+as a bunch of optional modules around a core of either XML or HTML
+functionality. Different implementations will support different optional
+modules. This implementation provides a set of features that should be
+useful if you're not depending on the HTML functionality (lots of convenience
+functions that mostly don't buy much except API surface area) and user
+interface support. That is, browsers will want more -- but what they
+need should be cleanly layered over what's already here. </p>
+
+<h3> Core Feature Set: "XML" </h3>
+
+<p> This DOM implementation supports the "XML" feature set, which basically
+gets you four things over the bare core (which you're officially not supposed
+to implement except in conjunction with the "XML" or "HTML" feature). In
+order of decreasing utility, those four things are: </p> <ol>
+
+ <li> ProcessingInstruction nodes. These are probably the most
+ valuable thing. Handy little buggers, in part because all the APIs
+ you need to use them are provided, and they're designed to let you
+ escape XML document structure rules in controlled ways.</li>
+
+ <li> CDATASection nodes. These are of of limited utility since CDATA
+ is just text that prints funny. These are of use to some sorts of
+ applications, though I encourage folk to not use them. </li>
+
+ <li> DocumentType nodes, and associated Notation and Entity nodes.
+ These appear to be useless. Briefly, these "Type" nodes expose no
+ typing information. They're only really usable to expose some lexical
+ structure that almost every application needs to ignore. (XML editors
+ might like to see them, but they need true typing information much more.)
+ I strongly encourage people not to use these. </li>
+
+ <li> EntityReference nodes can show up. These are actively annoying,
+ since they add an extra level of hierarchy, are the cause of most of
+ the complexity in attribute values, and their contents are immutable.
+ Avoid these.</li>
+
+ </ol>
+
+<h3> Optional Feature Sets: "Events", and friends </h3>
+
+<p> Events may be one of the more interesting new features in Level 2.
+This package provides the core feature set and exposes mutation events.
+No gooey events though; if you want that, write a layered implementation! </p>
+
+<p> Three mutation events aren't currently generated:</p> <ul>
+
+ <li> <em>DOMSubtreeModified</em> is poorly specified. Think of this
+ as generating one such event around the time of finalization, which
+ is a fully conformant implementation. This implementation is exactly
+ as useful as that one. </li>
+
+ <li> <em>DOMNodeRemovedFromDocument</em> and
+ <em>DOMNodeInsertedIntoDocument</em> are supposed to get sent to
+ every node in a subtree that gets removed or inserted (respectively).
+ This can be <em>extremely costly</em>, and the removal and insertion
+ processing is already significantly slower due to event reporting.
+ It's much easier, and more efficient, to have a listener higher in the
+ tree watch removal and insertion events through the bubbling or capture
+ mechanisms, than it is to watch for these two events.</li>
+
+ </ul>
+
+<p> In addition, certain kinds of attribute modification aren't reported.
+A fix is known, but it couldn't report the previous value of the attribute.
+More work could fix all of this (as well as reduce the generally high cost
+of childful attributes), but that's not been done yet. </p>
+
+<p> Also, note that it is a <em>Bad Thing&#153;</em> to have the listener
+for a mutation event change the ancestry for the target of that event.
+Or to prevent mutation events from bubbling to where they're needed.
+Just don't do those, OK? </p>
+
+<p> As an experimental feature (named "USER-Events"), you can provide
+your own "user" events. Just name them anything starting with "USER-"
+and you're set. Dispatch them through, bubbling, capturing, or what
+ever takes your fancy. One important thing you can't currently do is
+pass any data (like an object) with those events. Maybe later there
+will be a "UserEvent" interface letting you get some substantial use
+out of this mechanism even if you're not "inside" of a DOM package.</p>
+
+<p> You can create and send HTML events. Ditto UIEvents. Since DOM
+doesn't require a UI, it's the UI's job to send them; perhaps that's
+part of your application. </p>
+
+<p><em>This package may be built without the ability to report mutation
+events, gaining a significant speedup in DOM construction time. However,
+if that is done then certain other features -- notably node iterators
+and getElementsByTagname -- will not be available.</em>
+
+
+<h3> Optional Feature: "Traversal" </h3>
+
+<p> Each DOM node has all you need to walk to everything connected
+to that node. Lightweight, efficient utilities are easily layered on
+top of just the core APIs. </p>
+
+<p> Traversal APIs are an optional part of DOM Level 2, providing
+a not-so-lightweight way to walk over DOM trees, if your application
+didn't already have such utilities for use with data represented via
+DOM. Implementing this helped debug the (optional) event and mutation
+event subsystems, so it's provided here. </p>
+
+<p> At this writing, the "TreeWalker" interface isn't implemented. </p>
+
+
+
+<h2><a name='avoid'>DOM Functionality to Avoid</a></h2>
+
+<p> For what appear to be a combination of historical and "committee
+logic" reasons, DOM has a number of <em>features which I strongly advise
+you to avoid using</em> in your library and application code. These
+include the following types of DOM nodes; see the documentation for the
+implementation class for more information: <ul>
+
+ <li> CDATASection
+ (<a href='DomCDATA.html'>DomCDATA</a> class)
+ ... use normal Text nodes instead, so you don't have to make
+ every algorithm recognize multiple types of character data
+
+ <li> DocumentType
+ (<a href='DomDoctype.html'>DomDocType</a> class)
+ ... if this held actual typing information, it might be useful
+
+ <li> Entity
+ (<a href='DomEntity.html'>DomEntity</a> class)
+ ... neither parsed nor unparsed entities work well in DOM; it
+ won't even tell you which attributes identify unparsed entities
+
+ <li> EntityReference
+ (<a href='DomEntityReference.html'>DomEntityReference</a> class)
+ ... permitted implementation variances are extreme, all children
+ are readonly, and these can interact poorly with namespaces
+
+ <li> Notation
+ (<a href='DomNotation.html'>DomNotation</a> class)
+ ... only really usable with unparsed entities (which aren't well
+ supported; see above) or perhaps with PIs after the DTD, not with
+ NOTATION attributes
+
+ </ul>
+
+<p> If you really need to use unparsed entities or notations, use SAX;
+it offers better support for all DTD-related functionality.
+It also exposes actual
+document typing information (such as element content models).</p>
+
+<p> Also, when accessing attribute values, use methods that provide their
+values as single strings, rather than those which expose value substructure
+(Text and EntityReference nodes). (See the <a href='DomAttr.html'>DomAttr</a>
+documentation for more information.) </p>
+
+<p> Note that many of these features were provided as partial support for
+editor functionality (including the incomplete DTD access). Full editor
+functionality requires access to potentially malformed lexical structure,
+at the level of unparsed tokens and below. Access at such levels is so
+complex that using it in non-editor applications sacrifices all the
+benefits of XML; editor aplications need extremely specialized APIs. </p>
+
+<p> (This isn't a slam against DTDs, note; only against the broken support
+for them in DOM. Even despite inclusion of some dubious SGML legacy features
+such as notations and unparsed entities,
+and the ongoing proliferation of alternative schema and validation tools,
+DTDs are still the most widely adopted tool
+to constrain XML document structure.
+Alternative schemes generally focus on data transfer style
+applications; open document architectures comparable to
+DocBook 4.0 don't yet exist in the schema world.
+Feel free to use DTDs; just don't expect DOM to help you.) </p>
+
+</body>
+</html>
+
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeAttr.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeAttr.java
new file mode 100644
index 000000000..da9c62c9d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeAttr.java
@@ -0,0 +1,117 @@
+/* GnomeAttr.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import gnu.java.lang.CPStringBuilder;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.TypeInfo;
+
+/**
+ * A DOM attribute node implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeAttr
+ extends GnomeNode
+ implements Attr
+{
+
+ GnomeAttr(Object id)
+ {
+ super(id);
+ }
+
+ public String getName()
+ {
+ return getNodeName();
+ }
+
+ public native boolean getSpecified();
+
+ public native String getValue();
+
+ public native void setValue(String value)
+ throws DOMException;
+
+ public Node getParentNode()
+ {
+ return null;
+ }
+
+ public Element getOwnerElement()
+ {
+ return (Element) super.getParentNode();
+ }
+
+ // DOM Level 3 methods
+
+ public TypeInfo getSchemaTypeInfo()
+ {
+ return new GnomeTypeInfo(id);
+ }
+
+ public boolean isId()
+ {
+ if (xmljIsId())
+ {
+ return true;
+ }
+ GnomeElement element = (GnomeElement) getOwnerElement();
+ return (element != null &&
+ element.userIdAttrs != null &&
+ element.userIdAttrs.contains(this));
+ }
+
+ private native boolean xmljIsId();
+
+ public String toString()
+ {
+ CPStringBuilder buffer = new CPStringBuilder(getClass().getName());
+ buffer.append("[name=");
+ buffer.append(getName());
+ buffer.append(",value=");
+ buffer.append(getValue());
+ buffer.append("]");
+ return buffer.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCDATASection.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCDATASection.java
new file mode 100644
index 000000000..3e408e653
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCDATASection.java
@@ -0,0 +1,57 @@
+/* GnomeCDATASection.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import org.w3c.dom.CDATASection;
+
+/**
+ * A DOM CDATA section node implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeCDATASection
+ extends GnomeText
+ implements CDATASection
+{
+
+ GnomeCDATASection(Object id)
+ {
+ super(id);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCharacterData.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCharacterData.java
new file mode 100644
index 000000000..3f3d6ecb5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCharacterData.java
@@ -0,0 +1,119 @@
+/* GnomeCharacterData.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import gnu.java.lang.CPStringBuilder;
+
+import org.w3c.dom.CharacterData;
+import org.w3c.dom.DOMException;
+
+/**
+ * A DOM character data node implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+abstract class GnomeCharacterData
+ extends GnomeNode
+ implements CharacterData
+{
+
+ GnomeCharacterData(Object id)
+ {
+ super(id);
+ }
+
+ public String getData()
+ throws DOMException
+ {
+ return getNodeValue();
+ }
+
+ public void setData(String data)
+ throws DOMException
+ {
+ setNodeValue(data);
+ }
+
+ public int getLength()
+ {
+ return getData().length();
+ }
+
+ public String substringData(int offset, int count)
+ throws DOMException
+ {
+ return getData().substring(offset, offset + count);
+ }
+
+ public void appendData(String arg)
+ throws DOMException
+ {
+ setData(getData() + arg);
+ }
+
+ public void insertData(int offset, String arg)
+ throws DOMException
+ {
+ String data = getData();
+ setData(data.substring(0, offset) + arg + data.substring(offset));
+ }
+
+ public void deleteData(int offset, int count)
+ throws DOMException
+ {
+ String data = getData();
+ setData(data.substring(0, offset) + data.substring(offset + count));
+ }
+
+ public void replaceData(int offset, int count, String arg)
+ {
+ String data = getData();
+ setData(data.substring(0, offset) + arg +
+ data.substring(offset + count));
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buffer = new CPStringBuilder(getClass().getName());
+ buffer.append("[data=");
+ buffer.append(getData());
+ buffer.append("]");
+ return buffer.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeComment.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeComment.java
new file mode 100644
index 000000000..b6985c4a4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeComment.java
@@ -0,0 +1,57 @@
+/* GnomeComment.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import org.w3c.dom.Comment;
+
+/**
+ * A DOM comment node implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeComment
+ extends GnomeCharacterData
+ implements Comment
+{
+
+ GnomeComment(Object id)
+ {
+ super(id);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMException.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMException.java
new file mode 100644
index 000000000..a1af82335
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMException.java
@@ -0,0 +1,98 @@
+/* GnomeDOMException.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import org.w3c.dom.DOMException;
+
+class GnomeDOMException
+ extends DOMException
+{
+
+ GnomeDOMException(short code, String message)
+ {
+ super(code, createMessage(code, message));
+ }
+
+ private static String createMessage(int code, String message)
+ {
+ if (message != null)
+ {
+ return message;
+ }
+ switch (code)
+ {
+ case INDEX_SIZE_ERR:
+ return "INDEX_SIZE_ERR";
+ case DOMSTRING_SIZE_ERR:
+ return "DOMSTRING_SIZE_ERR";
+ case HIERARCHY_REQUEST_ERR:
+ return "HIERARCHY_REQUEST_ERR";
+ case WRONG_DOCUMENT_ERR:
+ return "WRONG_DOCUMENT_ERR";
+ case INVALID_CHARACTER_ERR:
+ return "INVALID_CHARACTER_ERR";
+ case NO_DATA_ALLOWED_ERR:
+ return "NO_DATA_ALLOWED_ERR";
+ case NO_MODIFICATION_ALLOWED_ERR:
+ return "NO_MODIFICATION_ALLOWED_ERR";
+ case NOT_FOUND_ERR:
+ return "NOT_FOUND_ERR";
+ case NOT_SUPPORTED_ERR:
+ return "NOT_SUPPORTED_ERR";
+ case INUSE_ATTRIBUTE_ERR:
+ return "INUSE_ATTRIBUTE_ERR";
+ case INVALID_STATE_ERR:
+ return "INVALID_STATE_ERR";
+ case SYNTAX_ERR:
+ return "SYNTAX_ERR";
+ case INVALID_MODIFICATION_ERR:
+ return "INVALID_MODIFICATION_ERR";
+ case NAMESPACE_ERR:
+ return "NAMESPACE_ERR";
+ case INVALID_ACCESS_ERR:
+ return "INVALID_ACCESS_ERR";
+ case VALIDATION_ERR:
+ return "VALIDATION_ERR";
+ case TYPE_MISMATCH_ERR:
+ return "TYPE_MISMATCH_ERR";
+ default:
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMStringList.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMStringList.java
new file mode 100644
index 000000000..c415ec782
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMStringList.java
@@ -0,0 +1,84 @@
+/* GnomeDOMStringList.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import org.w3c.dom.DOMStringList;
+
+/**
+ * Implementation of a string list using an array of strings.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeDOMStringList
+ implements DOMStringList
+{
+
+ final String[] values;
+
+ GnomeDOMStringList(String[] values)
+ {
+ this.values = values;
+ }
+
+ public int getLength()
+ {
+ return values.length;
+ }
+
+ public String item(int index)
+ {
+ if (index < 0 || index >= values.length)
+ {
+ return null;
+ }
+ return values[index];
+ }
+
+ public boolean contains(String value)
+ {
+ for (int i = 0; i < values.length; i++)
+ {
+ if (values[i].equalsIgnoreCase(value))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocument.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocument.java
new file mode 100644
index 000000000..0d1b487d8
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocument.java
@@ -0,0 +1,562 @@
+/* GnomeDocument.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import gnu.java.lang.CPStringBuilder;
+import gnu.xml.dom.DomNodeIterator;
+
+import java.util.Iterator;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.DOMConfiguration;
+import org.w3c.dom.DOMErrorHandler;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.DOMStringList;
+import org.w3c.dom.Element;
+import org.w3c.dom.EntityReference;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.ProcessingInstruction;
+import org.w3c.dom.Text;
+import org.w3c.dom.UserDataHandler;
+import org.w3c.dom.traversal.DocumentTraversal;
+import org.w3c.dom.traversal.NodeFilter;
+import org.w3c.dom.traversal.NodeIterator;
+import org.w3c.dom.traversal.TreeWalker;
+import org.w3c.dom.xpath.XPathEvaluator;
+import org.w3c.dom.xpath.XPathException;
+import org.w3c.dom.xpath.XPathExpression;
+import org.w3c.dom.xpath.XPathNSResolver;
+
+/**
+ * A DOM document node implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class GnomeDocument
+ extends GnomeNode
+ implements Document, DOMConfiguration, XPathEvaluator, DocumentTraversal
+{
+
+ DOMImplementation dom;
+
+ /**
+ * Not currently used.
+ */
+ boolean strictErrorChecking;
+
+ /* DOMConfiguration */
+ boolean canonicalForm = false;
+ boolean cdataSections = true;
+ boolean checkCharacterNormalization = false;
+ boolean comments = true;
+ boolean datatypeNormalization = false;
+ boolean elementContentWhitespace = true;
+ boolean entities = true;
+ DOMErrorHandler errorHandler;
+ boolean namespaces = true;
+ boolean namespaceDeclarations = true;
+ boolean normalizeCharacters = false;
+ boolean splitCdataSections = true;
+ boolean validate = false;
+ boolean validateIfSchema = false;
+ boolean wellFormed = true;
+
+ GnomeDocument(Object id)
+ {
+ super(id);
+ strictErrorChecking = true;
+ }
+
+ protected void finalize()
+ {
+ free(id);
+ }
+
+ private native void free(Object id);
+
+ public native DocumentType getDoctype();
+
+ public DOMImplementation getImplementation()
+ {
+ return dom;
+ }
+
+ public native Element getDocumentElement();
+
+ public Element createElement(String tagName)
+ throws DOMException
+ {
+ return createElementNS(null, tagName);
+ }
+
+ public native DocumentType createDocumentType(String name, String publicId,
+ String systemId);
+
+ public native DocumentFragment createDocumentFragment();
+
+ public native Text createTextNode(String data);
+
+ public native Comment createComment(String data);
+
+ public native CDATASection createCDATASection(String data)
+ throws DOMException;
+
+ public native ProcessingInstruction createProcessingInstruction(String target,
+ String data)
+ throws DOMException;
+
+ public Attr createAttribute(String name)
+ throws DOMException
+ {
+ return createAttributeNS(null, name);
+ }
+
+ public native EntityReference createEntityReference(String name)
+ throws DOMException;
+
+ public native NodeList getElementsByTagName(String tagName);
+
+ public Node importNode(Node importedNode, boolean deep)
+ throws DOMException
+ {
+ Node ret = xmljImportNode(importedNode, deep);
+ if (importedNode instanceof GnomeNode)
+ {
+ ((GnomeNode) importedNode)
+ .notifyUserDataHandlers(UserDataHandler.NODE_IMPORTED,
+ importedNode, ret);
+ }
+ return ret;
+ }
+
+ private native Node xmljImportNode(Node importedNode, boolean deep)
+ throws DOMException;
+
+ public native Element createElementNS(String namespaceURI, String
+ qualifiedName)
+ throws DOMException;
+
+ public native Attr createAttributeNS(String namespaceURI, String
+ qualifiedName)
+ throws DOMException;
+
+ public native NodeList getElementsByTagNameNS(String namespaceURI,
+ String localName);
+
+ public Element getElementById(String elementId)
+ {
+ Element element = xmljGetElementById(elementId);
+ if (element == null)
+ {
+ TreeWalker walker = createTreeWalker(this, NodeFilter.SHOW_ELEMENT,
+ null, false);
+ for (Node node = walker.nextNode(); node != null;
+ node = walker.nextNode())
+ {
+ GnomeElement e = (GnomeElement) node;
+ if (e.userIdAttrs != null)
+ {
+ for (Iterator i = e.userIdAttrs.iterator(); i.hasNext(); )
+ {
+ Attr attr = (Attr) i.next();
+ if (attr.getNodeValue().equals(elementId))
+ {
+ return e;
+ }
+ }
+ }
+ }
+ }
+ return element;
+ }
+
+ private native Element xmljGetElementById(String elementId);
+
+ // DOM Level 3 methods
+
+ public native String getInputEncoding();
+
+ public native String getXmlEncoding();
+
+ public native boolean getXmlStandalone();
+
+ public native void setXmlStandalone(boolean xmlStandalone);
+
+ public native String getXmlVersion();
+
+ public native void setXmlVersion(String xmlVersion);
+
+ public boolean getStrictErrorChecking()
+ {
+ return strictErrorChecking;
+ }
+
+ public void setStrictErrorChecking(boolean strictErrorChecking)
+ {
+ this.strictErrorChecking = strictErrorChecking;
+ }
+
+ public native String getDocumentURI();
+
+ public native void setDocumentURI(String documentURI);
+
+ public Node adoptNode(Node source)
+ throws DOMException
+ {
+ if (source == null || !(source instanceof GnomeNode))
+ {
+ return null;
+ }
+ Node ret = xmljAdoptNode(source);
+ if (source instanceof GnomeNode)
+ {
+ ((GnomeNode) source).
+ notifyUserDataHandlers(UserDataHandler.NODE_ADOPTED,
+ source, ret);
+ }
+ return ret;
+ }
+
+ private native Node xmljAdoptNode(Node source)
+ throws DOMException;
+
+ public DOMConfiguration getDomConfig()
+ {
+ return this;
+ }
+
+ public void normalizeDocument()
+ {
+ normalize();
+ }
+
+ public native Node renameNode(Node n, String namespaceURI,
+ String qualifiedName);
+
+ // -- DOMConfiguration methods --
+
+ public void setParameter(String name, Object value)
+ throws DOMException
+ {
+ name = name.toLowerCase();
+ if ("canonical-form".equals(name))
+ {
+ /* optional
+ canonicalForm = getBooleanValue(value);*/
+ }
+ else if ("cdata-sections".equals(name))
+ {
+ cdataSections = getBooleanValue(value);
+ }
+ else if ("check-character-normalization".equals(name))
+ {
+ /* optional
+ checkCharacterNormalization = getBooleanValue(value);*/
+ }
+ else if ("comments".equals(name))
+ {
+ comments = getBooleanValue(value);
+ }
+ else if ("datatype-normalization".equals(name))
+ {
+ /* optional
+ datatypeNormalization = getBooleanValue(value);*/
+ }
+ else if ("element-content-whitespace".equals(name))
+ {
+ /* optional
+ elementContentWhitespace = getBooleanValue(value);*/
+ }
+ else if ("entities".equals(name))
+ {
+ entities = getBooleanValue(value);
+ }
+ else if ("error-handler".equals(name))
+ {
+ errorHandler = (DOMErrorHandler) value;
+ }
+ else if ("infoset".equals(name))
+ {
+ if (getBooleanValue(value))
+ {
+ validateIfSchema = false;
+ entities = false;
+ datatypeNormalization = false;
+ cdataSections = false;
+ namespaceDeclarations = true;
+ wellFormed = true;
+ elementContentWhitespace = true;
+ comments = true;
+ namespaces = true;
+ }
+ }
+ else if ("namespaces".equals(name))
+ {
+ /* optional
+ namespaces = getBooleanValue(value);*/
+ }
+ else if ("namespace-declarations".equals(name))
+ {
+ namespaceDeclarations = getBooleanValue(value);
+ }
+ else if ("normalize-characters".equals(name))
+ {
+ /* optional
+ normalizeCharacters = getBooleanValue(value);*/
+ }
+ else if ("split-cdata-sections".equals(name))
+ {
+ splitCdataSections = getBooleanValue(value);
+ }
+ else if ("validate".equals(name))
+ {
+ /* optional
+ validate = getBooleanValue(value);*/
+ }
+ else if ("validate-if-schema".equals(name))
+ {
+ /* optional
+ validateIfSchema = getBooleanValue(value);*/
+ }
+ else if ("well-formed".equals(name))
+ {
+ /* optional
+ wellFormed = getBooleanValue(value);*/
+ }
+ else
+ {
+ throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, name);
+ }
+ }
+
+ public Object getParameter(String name)
+ throws DOMException
+ {
+ name = name.toLowerCase();
+ if ("canonical-form".equals(name))
+ {
+ return Boolean.valueOf(canonicalForm);
+ }
+ else if ("cdata-sections".equals(name))
+ {
+ return Boolean.valueOf(cdataSections);
+ }
+ else if ("check-character-normalization".equals(name))
+ {
+ return Boolean.valueOf(checkCharacterNormalization);
+ }
+ else if ("comments".equals(name))
+ {
+ return Boolean.valueOf(comments);
+ }
+ else if ("datatype-normalization".equals(name))
+ {
+ return Boolean.valueOf(datatypeNormalization);
+ }
+ else if ("element-content-whitespace".equals(name))
+ {
+ return Boolean.valueOf(elementContentWhitespace);
+ }
+ else if ("entities".equals(name))
+ {
+ return Boolean.valueOf(entities);
+ }
+ else if ("error-handler".equals(name))
+ {
+ return errorHandler;
+ }
+ else if ("infoset".equals(name))
+ {
+ return Boolean.valueOf(!validateIfSchema &&
+ !entities &&
+ !datatypeNormalization &&
+ !cdataSections &&
+ namespaceDeclarations &&
+ wellFormed &&
+ elementContentWhitespace &&
+ comments &&
+ namespaces);
+ }
+ else if ("namespaces".equals(name))
+ {
+ return Boolean.valueOf(namespaces);
+ }
+ else if ("namespace-declarations".equals(name))
+ {
+ return Boolean.valueOf(namespaceDeclarations);
+ }
+ else if ("normalize-characters".equals(name))
+ {
+ return Boolean.valueOf(normalizeCharacters);
+ }
+ else if ("split-cdata-sections".equals(name))
+ {
+ return Boolean.valueOf(splitCdataSections);
+ }
+ else if ("validate".equals(name))
+ {
+ return Boolean.valueOf(validate);
+ }
+ else if ("validate-if-schema".equals(name))
+ {
+ return Boolean.valueOf(validateIfSchema);
+ }
+ else if ("well-formed".equals(name))
+ {
+ return Boolean.valueOf(wellFormed);
+ }
+ else
+ {
+ throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, name);
+ }
+ }
+
+ public boolean canSetParameter(String name, Object value)
+ {
+ name = name.toLowerCase();
+ if ("error-handler".equals(name))
+ {
+ return (value == null || value instanceof DOMErrorHandler);
+ }
+ return ("cdata-sections".equals(name) ||
+ "comments".equals(name) ||
+ "entities".equals(name) ||
+ "namespace-declarations".equals(name) ||
+ "split-cdata-sections".equals(name));
+ }
+
+ public DOMStringList getParameterNames()
+ {
+ String[] names = new String[] {
+ "canonical-form",
+ "cdata-sections",
+ "check-character-normalization",
+ "comments",
+ "datatype-normalization",
+ "element-content-whitespace",
+ "entities",
+ "error-handler",
+ "infoset",
+ "namespaces",
+ "namespace-declarations",
+ "normalize-characters",
+ "split-cdata-sections",
+ "validate",
+ "validate-if-schema",
+ "well-formed"
+ };
+ return new GnomeDOMStringList(names);
+ }
+
+ private boolean getBooleanValue(Object value)
+ {
+ if (value instanceof Boolean)
+ {
+ return ((Boolean) value).booleanValue();
+ }
+ else if (value instanceof String)
+ {
+ return Boolean.valueOf ((String) value).booleanValue();
+ }
+ return false;
+ }
+
+ // -- XPathEvaluator methods --
+
+ public XPathExpression createExpression(String expression,
+ XPathNSResolver resolver)
+ throws XPathException, DOMException
+ {
+ return new GnomeXPathExpression(this, expression, resolver);
+ }
+
+ public XPathNSResolver createNSResolver(Node nodeResolver)
+ {
+ return new GnomeXPathNSResolver(nodeResolver);
+ }
+
+ public native Object evaluate(String expression,
+ Node contextNode,
+ XPathNSResolver resolver,
+ short type,
+ Object result)
+ throws XPathException, DOMException;
+
+ // -- DocumentTraversal methods --
+
+ public NodeIterator createNodeIterator(Node root,
+ int whatToShow,
+ NodeFilter filter,
+ boolean entityReferenceExpansion)
+ throws DOMException
+ {
+ return new DomNodeIterator(root, whatToShow, filter,
+ entityReferenceExpansion, false);
+ }
+
+ public TreeWalker createTreeWalker(Node root,
+ int whatToShow,
+ NodeFilter filter,
+ boolean entityReferenceExpansion)
+ throws DOMException
+ {
+ return new DomNodeIterator(root, whatToShow, filter,
+ entityReferenceExpansion, true);
+ }
+
+ // -- Debugging --
+
+ public String toString()
+ {
+ CPStringBuilder buffer = new CPStringBuilder(getClass().getName());
+ buffer.append("[version=");
+ buffer.append(getXmlVersion());
+ buffer.append(",standalone=");
+ buffer.append(getXmlStandalone());
+ buffer.append("]");
+ return buffer.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilder.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilder.java
new file mode 100644
index 000000000..6dea87226
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilder.java
@@ -0,0 +1,326 @@
+/* GnomeDocumentBuilder.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.net.URL;
+import javax.xml.parsers.DocumentBuilder;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.DOMImplementation;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+import gnu.xml.libxmlj.util.NamedInputStream;
+import gnu.xml.libxmlj.util.StandaloneDocumentType;
+import gnu.xml.libxmlj.util.StandaloneLocator;
+import gnu.xml.libxmlj.util.XMLJ;
+
+/**
+ * A JAXP DOM implementation that uses Gnome libxml2 as the underlying
+ * parser and node representation.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class GnomeDocumentBuilder
+ extends DocumentBuilder
+ implements DOMImplementation
+{
+
+ static
+ {
+ XMLJ.init();
+ }
+
+ // -- DocumentBuilder --
+
+ private boolean validate;
+ private boolean coalesce;
+ private boolean expandEntities;
+ private EntityResolver entityResolver;
+ private ErrorHandler errorHandler;
+ private boolean seenFatalError;
+
+ /**
+ * Constructs a new validating document builder.
+ */
+ public GnomeDocumentBuilder()
+ {
+ this(true, false, false);
+ }
+
+ /**
+ * Constructs a new document builder.
+ * @param validate whether to validate during parsing
+ * @param coalesce whether to merge CDATA as text nodes
+ * @param expandEntities whether to expand entity references
+ */
+ public GnomeDocumentBuilder(boolean validate,
+ boolean coalesce,
+ boolean expandEntities)
+ {
+ this.validate = validate;
+ this.coalesce = coalesce;
+ this.expandEntities = expandEntities;
+ }
+
+ public DOMImplementation getDOMImplementation()
+ {
+ return this;
+ }
+
+ public boolean isNamespaceAware()
+ {
+ return true;
+ }
+
+ public boolean isValidating()
+ {
+ return validate;
+ }
+
+ public Document newDocument()
+ {
+ return createDocument(null, null, null);
+ }
+
+ public Document parse(InputSource input)
+ throws SAXException, IOException
+ {
+ NamedInputStream in = XMLJ.getInputStream(input);
+ byte[] detectBuffer = in.getDetectBuffer();
+ String publicId = input.getPublicId();
+ String systemId = input.getSystemId();
+ String base = XMLJ.getBaseURI(systemId);
+ // Handle zero-length document
+ if (detectBuffer == null)
+ {
+ throw new SAXParseException("No document element", publicId,
+ systemId, 0, 0);
+ }
+ seenFatalError = false;
+ return parseStream(in,
+ detectBuffer,
+ publicId,
+ systemId,
+ base,
+ validate,
+ coalesce,
+ expandEntities,
+ true, //entityResolver != null,
+ errorHandler != null);
+ }
+
+ private native Document parseStream(InputStream in,
+ byte[] detectBuffer,
+ String publicId,
+ String systemId,
+ String base,
+ boolean validate,
+ boolean coalesce,
+ boolean expandEntities,
+ boolean entityResolver,
+ boolean errorHandler);
+
+ public void setEntityResolver(EntityResolver resolver)
+ {
+ entityResolver = resolver;
+ }
+
+ public void setErrorHandler(ErrorHandler handler)
+ {
+ errorHandler = handler;
+ }
+
+ // -- DOMImplementation --
+
+ public boolean hasFeature(String name, String version)
+ {
+ if (name.length() == 0)
+ {
+ return false;
+ }
+ name = name.toLowerCase();
+ if (name.charAt(0) == '+')
+ {
+ name = name.substring(1);
+ }
+
+ if ("xml".equals(name) || "core".equals(name))
+ {
+ return (version == null ||
+ "".equals(version) ||
+ "1.0".equals(version) ||
+ "2.0".equals(version) ||
+ "3.0".equals(version));
+
+ }
+ else if ("ls".equals(name) || "ls-async".equals(name))
+ {
+ // TODO
+ /*
+ return (version == null ||
+ "".equals(version) ||
+ "3.0".equals(version));
+ */
+ return false;
+ }
+ else if ("traversal".equals(name))
+ {
+ return (version == null ||
+ "".equals(version) ||
+ "2.0".equals(version));
+ }
+ else if ("xpath".equals(name))
+ {
+ return (version == null ||
+ "".equals(version) ||
+ "3.0".equals(version));
+ }
+ return false;
+ }
+
+ // DOM Level 3
+
+ public Object getFeature(String feature, String version)
+ {
+ if (hasFeature(feature, version))
+ {
+ return this;
+ }
+ return null;
+ }
+
+ public native Document createDocument(String namespaceURI,
+ String qualifiedName,
+ DocumentType doctype);
+
+ public DocumentType createDocumentType(String qualifiedName,
+ String publicId,
+ String systemId)
+ {
+ return new StandaloneDocumentType(qualifiedName, publicId, systemId);
+ }
+
+ // Callback hooks from JNI
+
+ private void setDocumentLocator(Object ctx, Object loc)
+ {
+ // ignore
+ }
+
+ private InputStream resolveEntity(String publicId, String systemId,
+ String base)
+ throws SAXException, IOException
+ {
+ String url = XMLJ.getAbsoluteURI(base, systemId);
+ InputStream in = null;
+ if (entityResolver != null)
+ {
+ InputSource source = entityResolver.resolveEntity(publicId, url);
+ if (source != null)
+ {
+ in = XMLJ.getInputStream(source);
+ }
+ }
+ if (in == null)
+ {
+ in = XMLJ.getInputStream(new URL(url));
+ }
+ return in;
+ }
+
+ private void warning(String message,
+ int lineNumber,
+ int columnNumber,
+ String publicId,
+ String systemId)
+ throws SAXException
+ {
+ if (!seenFatalError && errorHandler != null)
+ {
+ Locator l = new StandaloneLocator(lineNumber,
+ columnNumber,
+ publicId,
+ systemId);
+ errorHandler.warning(new SAXParseException(message, l));
+ }
+ }
+
+ private void error(String message,
+ int lineNumber,
+ int columnNumber,
+ String publicId,
+ String systemId)
+ throws SAXException
+ {
+ if (!seenFatalError && errorHandler != null)
+ {
+ Locator l = new StandaloneLocator(lineNumber,
+ columnNumber,
+ publicId,
+ systemId);
+ errorHandler.error(new SAXParseException(message, l));
+ }
+ }
+
+ private void fatalError(String message,
+ int lineNumber,
+ int columnNumber,
+ String publicId,
+ String systemId)
+ throws SAXException
+ {
+ if (!seenFatalError && errorHandler != null)
+ {
+ seenFatalError = true;
+ Locator l = new StandaloneLocator(lineNumber,
+ columnNumber,
+ publicId,
+ systemId);
+ errorHandler.fatalError(new SAXParseException(message, l));
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilderFactory.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilderFactory.java
new file mode 100644
index 000000000..448e12351
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilderFactory.java
@@ -0,0 +1,118 @@
+/* GnomeDocumentBuilderFactory.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import javax.xml.XMLConstants;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+/**
+ * Factory for JAXP document builders using the libxml2 implementation.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class GnomeDocumentBuilderFactory
+ extends DocumentBuilderFactory
+{
+
+ private boolean secureProcessing;
+
+ public GnomeDocumentBuilderFactory ()
+ {
+ setNamespaceAware (true);
+ }
+
+ public Object getAttribute (String name)
+ {
+ // TODO
+ return null;
+ }
+
+ public DocumentBuilder newDocumentBuilder ()
+ throws ParserConfigurationException
+ {
+ /*
+ if (!isNamespaceAware ())
+ {
+ String msg = "Parser must be namespace-aware";
+ throw new ParserConfigurationException (msg);
+ }
+ if (isIgnoringComments ())
+ {
+ String msg = "Ignoring comments not supported";
+ throw new ParserConfigurationException (msg);
+ }
+ if (isIgnoringElementContentWhitespace ())
+ {
+ String msg = "Ignoring element content whitespace not supported";
+ throw new ParserConfigurationException (msg);
+ }
+ */
+ return new GnomeDocumentBuilder (isValidating (),
+ isCoalescing (),
+ isExpandEntityReferences ());
+ }
+
+ public void setAttribute (String name, Object value)
+ {
+ // TODO
+ }
+
+ public void setFeature(String name, boolean value)
+ throws ParserConfigurationException
+ {
+ if (name == null)
+ throw new NullPointerException();
+ if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name))
+ {
+ secureProcessing = true;
+ return;
+ }
+ throw new ParserConfigurationException(name);
+ }
+
+ public boolean getFeature(String name)
+ throws ParserConfigurationException
+ {
+ if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name))
+ return secureProcessing;
+ throw new ParserConfigurationException(name);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentFragment.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentFragment.java
new file mode 100644
index 000000000..3623eb6cf
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentFragment.java
@@ -0,0 +1,57 @@
+/* GnomeDocumentFragment.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import org.w3c.dom.DocumentFragment;
+
+/**
+ * A DOM document fragment node implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeDocumentFragment
+extends GnomeNode
+implements DocumentFragment
+{
+
+ GnomeDocumentFragment (Object id)
+ {
+ super (id);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentType.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentType.java
new file mode 100644
index 000000000..7649b7339
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentType.java
@@ -0,0 +1,98 @@
+/* GnomeDocumentType.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import gnu.java.lang.CPStringBuilder;
+
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.NamedNodeMap;
+
+/**
+ * A DOM document type node implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeDocumentType
+extends GnomeNode
+implements DocumentType
+{
+
+ GnomeDocumentType (Object id)
+ {
+ super (id);
+ }
+
+ public String getName ()
+ {
+ return getNodeName ();
+ }
+
+ public NamedNodeMap getEntities ()
+ {
+ return new GnomeNamedNodeMap (id, 1);
+ }
+
+ public NamedNodeMap getNotations ()
+ {
+ return new GnomeNamedNodeMap (id, 2);
+ }
+
+ public native String getPublicId ();
+
+ public native String getSystemId ();
+
+ public native String getInternalSubset ();
+
+ public String toString ()
+ {
+ String publicId = getPublicId ();
+ CPStringBuilder buffer = new CPStringBuilder (getClass ().getName ());
+ buffer.append ("[");
+ if (publicId != null)
+ {
+ buffer.append ("publicId=");
+ buffer.append (publicId);
+ buffer.append (",");
+ }
+ buffer.append ("systemId=");
+ buffer.append (getSystemId ());
+ buffer.append ("]");
+ return buffer.toString ();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeElement.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeElement.java
new file mode 100644
index 000000000..19889d663
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeElement.java
@@ -0,0 +1,184 @@
+/* GnomeElement.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.TypeInfo;
+
+/**
+ * A DOM element node implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeElement
+ extends GnomeNode
+ implements Element
+{
+
+ /**
+ * User-defined ID attributes.
+ */
+ Set userIdAttrs;
+
+ GnomeElement(Object id)
+ {
+ super(id);
+ }
+
+ public String getTagName()
+ {
+ return getNodeName();
+ }
+
+ public native String getAttribute(String name);
+
+ public native void setAttribute(String name, String value)
+ throws DOMException;
+
+ public void removeAttribute(String name)
+ throws DOMException
+ {
+ Attr attr = getAttributeNode(name);
+ if (attr != null)
+ {
+ removeAttributeNode(attr);
+ }
+ }
+
+ public native Attr getAttributeNode(String name);
+
+ public native Attr setAttributeNode(Attr newAttr)
+ throws DOMException;
+
+ public native Attr removeAttributeNode(Attr oldAttr)
+ throws DOMException;
+
+ public native NodeList getElementsByTagName(String name);
+
+ public native String getAttributeNS(String namespaceURI, String localName);
+
+ public native void setAttributeNS(String namespaceURI, String
+ qualifiedName, String value)
+ throws DOMException;
+
+ public void removeAttributeNS(String namespaceURI, String localName)
+ throws DOMException
+ {
+ Attr attr = getAttributeNodeNS(namespaceURI, localName);
+ if (attr != null)
+ {
+ removeAttributeNode(attr);
+ }
+ }
+
+ public native Attr getAttributeNodeNS(String namespaceURI,
+ String localName);
+
+ public native Attr setAttributeNodeNS(Attr newAttr)
+ throws DOMException;
+
+ public native NodeList getElementsByTagNameNS(String namespaceURI,
+ String localName);
+
+ public native boolean hasAttribute(String name);
+
+ public native boolean hasAttributeNS(String namespaceURI,
+ String localName);
+
+ // DOM Level 3 methods
+
+ public TypeInfo getSchemaTypeInfo()
+ {
+ return new GnomeTypeInfo(id);
+ }
+
+ public void setIdAttribute(String name, boolean isId)
+ {
+ Attr attr = getAttributeNode(name);
+ setIdAttributeNode(attr, isId);
+ }
+
+ public void setIdAttributeNode(Attr attr, boolean isId)
+ {
+ if (attr == null)// FIXME || !attr.getOwnerElement().equals(this))
+ {
+ throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null);
+ }
+ if (isId)
+ {
+ if (userIdAttrs == null)
+ {
+ userIdAttrs = new HashSet();
+ }
+ userIdAttrs.add(attr);
+ }
+ else if (userIdAttrs != null)
+ {
+ userIdAttrs.remove(attr);
+ if (userIdAttrs.isEmpty())
+ {
+ userIdAttrs = null;
+ }
+ }
+ }
+
+ public void setIdAttributeNS(String namespaceURI, String localName,
+ boolean isId)
+ {
+ Attr attr = getAttributeNodeNS(namespaceURI, localName);
+ setIdAttributeNode(attr, isId);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buffer = new CPStringBuilder(getClass().getName());
+ buffer.append("[tagName=");
+ buffer.append(getTagName());
+ buffer.append("]");
+ return buffer.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntity.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntity.java
new file mode 100644
index 000000000..a05217473
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntity.java
@@ -0,0 +1,104 @@
+/* GnomeEntity.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import gnu.java.lang.CPStringBuilder;
+
+import org.w3c.dom.Entity;
+
+/**
+ * A DOM entity node implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeEntity
+extends GnomeNode
+implements Entity
+{
+
+ GnomeEntity (Object id)
+ {
+ super (id);
+ }
+
+ public native String getPublicId ();
+
+ public native String getSystemId ();
+
+ public native String getNotationName ();
+
+ // DOM Level 3 methods
+
+ public String getInputEncoding ()
+ {
+ // TODO
+ return null;
+ }
+
+ public String getXmlEncoding ()
+ {
+ // TODO
+ return null;
+ }
+
+ public String getXmlVersion ()
+ {
+ // TODO
+ return null;
+ }
+
+ public String toString ()
+ {
+ String publicId = getPublicId ();
+ CPStringBuilder buffer = new CPStringBuilder (getClass ().getName ());
+ buffer.append ("[");
+ if (publicId != null)
+ {
+ buffer.append ("publicId=");
+ buffer.append (publicId);
+ buffer.append (",");
+ }
+ buffer.append ("systemId=");
+ buffer.append (getSystemId ());
+ buffer.append (",notationName=");
+ buffer.append (getNotationName ());
+ buffer.append ("]");
+ return buffer.toString ();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntityReference.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntityReference.java
new file mode 100644
index 000000000..aa2fcf1e5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntityReference.java
@@ -0,0 +1,57 @@
+/* GnomeEntityReference.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import org.w3c.dom.EntityReference;
+
+/**
+ * A DOM entity reference node implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeEntityReference
+extends GnomeNode
+implements EntityReference
+{
+
+ GnomeEntityReference (Object id)
+ {
+ super (id);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNamedNodeMap.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNamedNodeMap.java
new file mode 100644
index 000000000..b8df4fede
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNamedNodeMap.java
@@ -0,0 +1,92 @@
+/* GnomeNamedNodeMap.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+/**
+ * A DOM named node map implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeNamedNodeMap
+implements NamedNodeMap
+{
+
+ /**
+ * The node id.
+ */
+ private final Object id;
+
+ /**
+ * The map type.
+ * 0=attributes
+ * 1=entities
+ * 2=notations
+ */
+ private final int type;
+
+ GnomeNamedNodeMap (Object id, int type)
+ {
+ this.id = id;
+ this.type = type;
+ }
+
+ public native Node getNamedItem (String name);
+
+ public native Node setNamedItem (Node arg)
+ throws DOMException;
+
+ public native Node removeNamedItem (String name)
+ throws DOMException;
+
+ public native Node item (int index);
+
+ public native int getLength ();
+
+ public native Node getNamedItemNS (String namespaceURI, String localName);
+
+ public native Node setNamedItemNS (Node arg)
+ throws DOMException;
+
+ public native Node removeNamedItemNS (String namespaceURI,
+ String localName);
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNode.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNode.java
new file mode 100644
index 000000000..5874916e5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNode.java
@@ -0,0 +1,501 @@
+/* GnomeNode.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+import org.w3c.dom.UserDataHandler;
+
+import gnu.xml.libxmlj.util.StandaloneDocumentType;
+
+/**
+ * A DOM node implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeNode
+ implements Node, Comparable
+{
+
+ /**
+ * Maps document pointers to a map of node pointers to node instances.
+ */
+ static Map instances;
+
+ /**
+ * Retrieves the node instance for the specified node pointer.
+ * This creates a new instance and adds it to the cache if required.
+ * @param doc the document pointer
+ * @param node the node pointer
+ * @param type the node type
+ */
+ static GnomeNode newInstance(final Object doc, final Object node,
+ final int type)
+ {
+ if (doc == null)
+ {
+ throw new NullPointerException("doc");
+ }
+ if (node == null)
+ {
+ throw new NullPointerException("node");
+ }
+ if (instances == null)
+ {
+ instances = new HashMap();
+ }
+ Map docNodes = (Map) instances.get(doc);
+ if (docNodes == null)
+ {
+ docNodes = new HashMap(1024); // TODO review optimal initial capacity
+ instances.put(doc, docNodes);
+ }
+ GnomeNode nodeInstance = (GnomeNode) docNodes.get(node);
+ if (nodeInstance != null)
+ {
+ return nodeInstance; // Return cached version
+ }
+ switch (type)
+ {
+ case ELEMENT_NODE:
+ nodeInstance = new GnomeElement(node);
+ break;
+ case ATTRIBUTE_NODE:
+ nodeInstance = new GnomeAttr(node);
+ break;
+ case TEXT_NODE:
+ nodeInstance = new GnomeText(node);
+ break;
+ case CDATA_SECTION_NODE:
+ nodeInstance = new GnomeCDATASection(node);
+ break;
+ case ENTITY_REFERENCE_NODE:
+ nodeInstance = new GnomeEntityReference(node);
+ break;
+ case ENTITY_NODE:
+ nodeInstance = new GnomeEntity(node);
+ break;
+ case PROCESSING_INSTRUCTION_NODE:
+ nodeInstance = new GnomeProcessingInstruction(node);
+ break;
+ case COMMENT_NODE:
+ nodeInstance = new GnomeComment(node);
+ break;
+ case DOCUMENT_NODE:
+ nodeInstance = new GnomeDocument(node);
+ break;
+ case DOCUMENT_TYPE_NODE:
+ nodeInstance = new GnomeDocumentType(node);
+ break;
+ case DOCUMENT_FRAGMENT_NODE:
+ nodeInstance = new GnomeDocumentFragment(node);
+ break;
+ case NOTATION_NODE:
+ nodeInstance = new GnomeNotation(node);
+ break;
+ default:
+ throw new IllegalArgumentException("Unknown node type: " + type);
+ }
+ docNodes.put(node, nodeInstance);
+ return nodeInstance;
+ }
+
+ /**
+ * Frees the specified document.
+ * This removes all its nodes from the cache.
+ */
+ static void freeDocument(final Object doc)
+ {
+ if (instances == null || doc == null)
+ {
+ return;
+ }
+ instances.remove(doc);
+ //System.out.println("Freed "+instances.remove(doc));
+ }
+
+ /**
+ * xmlNodePtr
+ */
+ final Object id;
+
+ Map userData;
+ Map userDataHandlers;
+
+ GnomeNode(final Object id)
+ {
+ this.id = id;
+ }
+
+ public native String getNodeName();
+
+ public native String getNodeValue()
+ throws DOMException;
+
+ public native void setNodeValue(String nodeValue)
+ throws DOMException;
+
+ public native short getNodeType();
+
+ public native Node getParentNode();
+
+ public NodeList getChildNodes()
+ {
+ return new GnomeNodeList(id);
+ }
+
+ public native Node getFirstChild();
+
+ public native Node getLastChild();
+
+ public native Node getPreviousSibling();
+
+ public native Node getNextSibling();
+
+ public NamedNodeMap getAttributes()
+ {
+ return new GnomeNamedNodeMap(id, 0);
+ }
+
+ public native Document getOwnerDocument();
+
+ public Node insertBefore(Node newChild, Node refChild)
+ throws DOMException
+ {
+ if (newChild instanceof StandaloneDocumentType)
+ {
+ DocumentType dt = (DocumentType) newChild;
+ newChild = ((GnomeDocument) getOwnerDocument())
+ .createDocumentType(dt.getName(), dt.getPublicId(),
+ dt.getSystemId());
+ }
+ if (newChild == null)
+ {
+ throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null);
+ }
+ if (!(newChild instanceof GnomeNode))
+ {
+ throw new GnomeDOMException(DOMException.WRONG_DOCUMENT_ERR, null);
+ }
+ if (refChild == null || !(refChild instanceof GnomeNode))
+ {
+ throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null);
+ }
+ return xmljInsertBefore(newChild, refChild);
+ }
+
+ private native Node xmljInsertBefore(Node newChild, Node refChild)
+ throws DOMException;
+
+ public Node replaceChild(Node newChild, Node oldChild)
+ throws DOMException
+ {
+ if (newChild instanceof StandaloneDocumentType)
+ {
+ DocumentType dt = (DocumentType) newChild;
+ newChild = ((GnomeDocument) getOwnerDocument())
+ .createDocumentType(dt.getName(), dt.getPublicId(),
+ dt.getSystemId());
+ }
+ if (newChild == null)
+ {
+ throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null);
+ }
+ if (!(newChild instanceof GnomeNode))
+ {
+ throw new GnomeDOMException(DOMException.WRONG_DOCUMENT_ERR, newChild.toString());
+ }
+ if (oldChild == null || !(oldChild instanceof GnomeNode))
+ {
+ throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null);
+ }
+ return xmljReplaceChild(newChild, oldChild);
+ }
+
+ private native Node xmljReplaceChild(Node newChild, Node oldChild)
+ throws DOMException;
+
+ public Node removeChild(Node oldChild)
+ throws DOMException
+ {
+ if (!(oldChild instanceof GnomeNode))
+ {
+ throw new GnomeDOMException(DOMException.WRONG_DOCUMENT_ERR, null);
+ }
+ return xmljRemoveChild(oldChild);
+ }
+
+ private native Node xmljRemoveChild(Node oldChild)
+ throws DOMException;
+
+ public Node appendChild(Node newChild)
+ throws DOMException
+ {
+ if (newChild instanceof StandaloneDocumentType)
+ {
+ DocumentType dt = (DocumentType) newChild;
+ newChild = ((GnomeDocument) getOwnerDocument())
+ .createDocumentType(dt.getName(), dt.getPublicId(),
+ dt.getSystemId());
+ }
+ if (!(newChild instanceof GnomeNode))
+ {
+ throw new GnomeDOMException(DOMException.WRONG_DOCUMENT_ERR, null);
+ }
+ return xmljAppendChild(newChild);
+ }
+
+ private native Node xmljAppendChild(Node newChild)
+ throws DOMException;
+
+ public native boolean hasChildNodes();
+
+ public Node cloneNode(boolean deep)
+ {
+ Node ret = xmljCloneNode(deep);
+ notifyUserDataHandlers(UserDataHandler.NODE_CLONED, this, ret);
+ return ret;
+ }
+
+ private native Node xmljCloneNode(boolean deep);
+
+ public native void normalize();
+
+ public boolean isSupported(String feature, String version)
+ {
+ return getOwnerDocument().getImplementation()
+ .hasFeature(feature, version);
+ }
+
+ public native String getNamespaceURI();
+
+ public native String getPrefix();
+
+ public native void setPrefix(String prefix)
+ throws DOMException;
+
+ public native String getLocalName();
+
+ public native boolean hasAttributes();
+
+ public int hashCode()
+ {
+ return id.hashCode();
+ }
+
+ public boolean equals(Object other)
+ {
+ if (other == this)
+ {
+ return true;
+ }
+ return (other instanceof GnomeNode &&
+ ((GnomeNode) other).id == id);
+ }
+
+ // DOM Level 3 methods
+
+ public native String getBaseURI();
+
+ public short compareDocumentPosition(Node other)
+ throws DOMException
+ {
+ return (short) compareTo(other);
+ }
+
+ public final int compareTo(Object other)
+ {
+ if (other instanceof GnomeNode)
+ {
+ return xmljCompareTo(other);
+ }
+ return 0;
+ }
+
+ private native int xmljCompareTo(Object other);
+
+ public String getTextContent()
+ throws DOMException
+ {
+ switch (getNodeType())
+ {
+ case ELEMENT_NODE:
+ case ATTRIBUTE_NODE:
+ case ENTITY_NODE:
+ case ENTITY_REFERENCE_NODE:
+ case DOCUMENT_FRAGMENT_NODE:
+ CPStringBuilder buffer = new CPStringBuilder();
+ NodeList children = getChildNodes();
+ int len = children.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Node child = children.item(i);
+ String textContent = child.getTextContent();
+ if (textContent != null)
+ {
+ buffer.append(textContent);
+ }
+ }
+ return buffer.toString();
+ case TEXT_NODE:
+ case CDATA_SECTION_NODE:
+ case COMMENT_NODE:
+ case PROCESSING_INSTRUCTION_NODE:
+ return getNodeValue();
+ default:
+ return null;
+ }
+ }
+
+ public void setTextContent(String textContent)
+ throws DOMException
+ {
+ switch (getNodeType())
+ {
+ case ENTITY_REFERENCE_NODE:
+ // entity references are read only
+ throw new GnomeDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
+ null);
+ case ELEMENT_NODE:
+ case ATTRIBUTE_NODE:
+ case ENTITY_NODE:
+ case DOCUMENT_FRAGMENT_NODE:
+ NodeList children = getChildNodes();
+ int len = children.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Node child = children.item(i);
+ removeChild(child);
+ }
+ if (textContent != null)
+ {
+ Text text = getOwnerDocument().createTextNode(textContent);
+ appendChild(text);
+ }
+ break;
+ case TEXT_NODE:
+ case CDATA_SECTION_NODE:
+ case COMMENT_NODE:
+ case PROCESSING_INSTRUCTION_NODE:
+ setNodeValue(textContent);
+ break;
+ }
+ }
+
+ public boolean isSameNode(Node other)
+ {
+ return equals(other);
+ }
+
+ public native String lookupPrefix(String namespaceURI);
+
+ public native boolean isDefaultNamespace(String namespaceURI);
+
+ public native String lookupNamespaceURI(String prefix);
+
+ public native boolean isEqualNode(Node arg);
+
+ public Object getFeature(String feature, String version)
+ {
+ return getOwnerDocument().getImplementation()
+ .getFeature(feature, version);
+ }
+
+ public Object setUserData(String key, Object data, UserDataHandler handler)
+ {
+ // TODO handler
+ if (userData == null)
+ {
+ userData = new HashMap();
+ }
+ if (handler != null)
+ {
+ if (userDataHandlers == null)
+ {
+ userDataHandlers = new HashMap();
+ }
+ userDataHandlers.put(key, handler);
+ }
+ return userData.put(key, data);
+ }
+
+ public Object getUserData(String key)
+ {
+ if (userData == null)
+ {
+ return null;
+ }
+ return userData.get(key);
+ }
+
+ void notifyUserDataHandlers(short op, Node src, Node dst)
+ {
+ if (userDataHandlers != null)
+ {
+ for (Iterator i = userDataHandlers.entrySet().iterator(); i.hasNext(); )
+ {
+ Map.Entry entry = (Map.Entry) i.next();
+ String key = (String) entry.getKey();
+ UserDataHandler handler = (UserDataHandler) entry.getValue();
+ Object data = userData.get(key);
+ handler.handle(op, key, data, src, dst);
+ }
+ }
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buffer = new CPStringBuilder(getClass().getName());
+ buffer.append("[nodeName=");
+ buffer.append(getNodeName());
+ buffer.append("]");
+ return buffer.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNodeList.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNodeList.java
new file mode 100644
index 000000000..51a6d0bfa
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNodeList.java
@@ -0,0 +1,66 @@
+/* GnomeNodeList.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * A DOM node list implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeNodeList
+implements NodeList
+{
+
+ /**
+ * The node id.
+ */
+ private final Object id;
+
+ GnomeNodeList (Object id)
+ {
+ this.id = id;
+ }
+
+ public native Node item (int index);
+
+ public native int getLength ();
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNotation.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNotation.java
new file mode 100644
index 000000000..50e48e2a3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNotation.java
@@ -0,0 +1,80 @@
+/* GnomeNotation.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import gnu.java.lang.CPStringBuilder;
+
+import org.w3c.dom.Notation;
+
+/**
+ * A DOM notation node implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeNotation
+extends GnomeNode
+implements Notation
+{
+
+ GnomeNotation (Object id)
+ {
+ super (id);
+ }
+
+ public native String getPublicId ();
+
+ public native String getSystemId ();
+
+ public String toString ()
+ {
+ String publicId = getPublicId ();
+ CPStringBuilder buffer = new CPStringBuilder (getClass ().getName ());
+ buffer.append ("[");
+ if (publicId != null)
+ {
+ buffer.append ("publicId=");
+ buffer.append (publicId);
+ buffer.append (",");
+ }
+ buffer.append ("systemId=");
+ buffer.append (getSystemId ());
+ buffer.append ("]");
+ return buffer.toString ();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeProcessingInstruction.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeProcessingInstruction.java
new file mode 100644
index 000000000..fad6e108a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeProcessingInstruction.java
@@ -0,0 +1,79 @@
+/* GnomeProcessingInstruction.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import gnu.java.lang.CPStringBuilder;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.ProcessingInstruction;
+
+/**
+ * A DOM processing instruction node implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeProcessingInstruction
+extends GnomeNode
+implements ProcessingInstruction
+{
+
+ GnomeProcessingInstruction (Object id)
+ {
+ super (id);
+ }
+
+ public String getTarget ()
+ {
+ return getNodeName ();
+ }
+
+ public native String getData ();
+
+ public native void setData (String data)
+ throws DOMException;
+
+ public String toString ()
+ {
+ CPStringBuilder buffer = new CPStringBuilder (getClass ().getName ());
+ buffer.append ("[data=");
+ buffer.append (getData ());
+ buffer.append ("]");
+ return buffer.toString ();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeText.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeText.java
new file mode 100644
index 000000000..e00be18a9
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeText.java
@@ -0,0 +1,132 @@
+/* GnomeText.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import gnu.java.lang.CPStringBuilder;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+
+/**
+ * A DOM text node implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeText
+extends GnomeCharacterData
+implements Text
+{
+
+ GnomeText (Object id)
+ {
+ super (id);
+ }
+
+ public Text splitText (int offset)
+ throws DOMException
+ {
+ String value = getNodeValue ();
+ String part1 = value.substring (0, offset);
+ String part2 = value.substring (offset);
+ Text text = getOwnerDocument ().createTextNode (part1);
+ getParentNode ().insertBefore (text, this);
+ setNodeValue (part2);
+ return text;
+ }
+
+ // DOM Level 3
+
+ public boolean isElementContentWhitespace ()
+ {
+ return getTextContent ().trim ().length () == 0;
+ }
+
+ public String getWholeText ()
+ {
+ Node first = this;
+ Node node = getPreviousSibling ();
+ while (node != null && node instanceof Text)
+ {
+ first = node;
+ node = node.getPreviousSibling ();
+ }
+ CPStringBuilder buf = new CPStringBuilder (first.getNodeValue ());
+ node = first.getNextSibling ();
+ while (node != null && node instanceof Text)
+ {
+ buf.append (node.getNodeValue ());
+ node = node.getNextSibling ();
+ }
+ return buf.toString ();
+ }
+
+ public Text replaceWholeText (String content) throws DOMException
+ {
+ boolean isEmpty = (content == null || content.length () == 0);
+ if (!isEmpty)
+ {
+ setNodeValue (content);
+ }
+
+ Node first = this;
+ Node node = getPreviousSibling ();
+ while (node != null && node instanceof Text)
+ {
+ first = node;
+ node = node.getPreviousSibling ();
+ }
+ node = first.getNextSibling ();
+ Node parent = getParentNode ();
+ if (first != this || isEmpty)
+ {
+ parent.removeChild (first);
+ }
+ while (node != null && node instanceof Text)
+ {
+ Node tmp = node;
+ node = node.getNextSibling ();
+ if (tmp != this || isEmpty)
+ {
+ parent.removeChild (tmp);
+ }
+ }
+ return (isEmpty) ? null : this;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeTypeInfo.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeTypeInfo.java
new file mode 100644
index 000000000..5517e8a99
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeTypeInfo.java
@@ -0,0 +1,65 @@
+/* GnomeTypeInfo.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import org.w3c.dom.TypeInfo;
+
+/**
+ * Provides XML Schema information about an element or attribute.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeTypeInfo implements TypeInfo
+{
+
+ final Object id;
+
+ GnomeTypeInfo(Object id)
+ {
+ this.id = id;
+ }
+
+ public native String getTypeName ();
+
+ public native String getTypeNamespace ();
+
+ public native boolean isDerivedFrom (String typeNamespaceArg,
+ String typeNameArg,
+ int derivationMethod);
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathExpression.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathExpression.java
new file mode 100644
index 000000000..52f4daa21
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathExpression.java
@@ -0,0 +1,86 @@
+/* GnomeXPathExpression.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+import org.w3c.dom.xpath.XPathException;
+import org.w3c.dom.xpath.XPathExpression;
+import org.w3c.dom.xpath.XPathNSResolver;
+
+/**
+ * A compiled XPath expression implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeXPathExpression
+implements XPathExpression
+{
+
+ /**
+ * xmlXPathCompExprPtr
+ */
+ final Object expr;
+
+ GnomeXPathExpression (GnomeDocument doc, String expression,
+ XPathNSResolver resolver)
+ {
+ expr = init (expression);
+ // TODO resolver
+ }
+
+ protected void finalize ()
+ {
+ free (expr);
+ }
+
+ private native Object init (String expression);
+
+ private native void free (Object expr);
+
+ public Object evaluate (Node contextNode, short type, Object result)
+ throws XPathException, DOMException
+ {
+ return doEvaluate (expr, contextNode, type, result);
+ }
+
+ private native Object doEvaluate (Object expr, Node contextNode,
+ short type, Object result)
+ throws XPathException, DOMException;
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNSResolver.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNSResolver.java
new file mode 100644
index 000000000..039a79a03
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNSResolver.java
@@ -0,0 +1,64 @@
+/* GnomeXPathNSResolver.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.xpath.XPathNSResolver;
+
+/**
+ * XPath namespace URI resolver implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeXPathNSResolver
+ implements XPathNSResolver
+{
+
+ Node node;
+
+ GnomeXPathNSResolver(Node node)
+ {
+ this.node = node;
+ }
+
+ public String lookupNamespaceURI(String prefix)
+ {
+ return node.lookupNamespaceURI(prefix);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNodeList.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNodeList.java
new file mode 100644
index 000000000..b8d0a3d0a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNodeList.java
@@ -0,0 +1,73 @@
+/* GnomeXPathNodeList.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * A node list that uses an XPath result object.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeXPathNodeList
+implements NodeList
+{
+
+ /**
+ * xmlXPathObjectPtr
+ */
+ final Object obj;
+
+ GnomeXPathNodeList (Object obj)
+ {
+ this.obj = obj;
+ }
+
+ protected void finalize ()
+ {
+ free(obj);
+ }
+
+ private native void free (Object obj);
+
+ public native int getLength ();
+
+ public native Node item (int index);
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathResult.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathResult.java
new file mode 100644
index 000000000..b2c706f65
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathResult.java
@@ -0,0 +1,134 @@
+/* GnomeXPathResult.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.dom;
+
+import gnu.java.lang.CPStringBuilder;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+import org.w3c.dom.xpath.XPathException;
+import org.w3c.dom.xpath.XPathResult;
+
+/**
+ * An XPath result object implemented in libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeXPathResult
+implements XPathResult
+{
+
+ /**
+ * xmlXPathObjectPtr
+ */
+ final Object obj;
+
+ GnomeXPathResult (Object obj)
+ {
+ this.obj = obj;
+ }
+
+ protected void finalize ()
+ {
+ free (obj);
+ }
+
+ private native void free (Object obj);
+
+ public native short getResultType ();
+
+ public native double getNumberValue ()
+ throws XPathException;
+
+ public native String getStringValue ()
+ throws XPathException;
+
+ public native boolean getBooleanValue ()
+ throws XPathException;
+
+ public native Node getSingleNodeValue ()
+ throws XPathException;
+
+ public native boolean getInvalidIteratorState();
+
+ public native int getSnapshotLength ()
+ throws XPathException;
+
+ public native Node iterateNext ()
+ throws XPathException, DOMException;
+
+ public native Node snapshotItem (int index)
+ throws XPathException;
+
+ public String toString ()
+ {
+ short type = getResultType ();
+ switch (type)
+ {
+ case STRING_TYPE:
+ return getStringValue ();
+ case NUMBER_TYPE:
+ return Double.toString (getNumberValue ());
+ case BOOLEAN_TYPE:
+ return Boolean.valueOf (getBooleanValue ()).toString ();
+ case UNORDERED_NODE_SNAPSHOT_TYPE:
+ int len = getSnapshotLength ();
+ switch (len) {
+ case 0:
+ return "[no matches]";
+ case 1:
+ return getSingleNodeValue ().toString ();
+ default:
+ CPStringBuilder buffer = new CPStringBuilder ();
+ for (int i = 0; i < len; i++)
+ {
+ if (i > 0)
+ {
+ buffer.append (',');
+ }
+ buffer.append (snapshotItem (i));
+ }
+ return buffer.toString ();
+ }
+ default:
+ return getClass ().getName () + "[type=" + type + ",length=" +
+ getSnapshotLength () + ']';
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/GnomeLocator.java b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeLocator.java
new file mode 100644
index 000000000..dba0a72c0
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeLocator.java
@@ -0,0 +1,99 @@
+/* GnomeLocator.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.sax;
+
+import org.xml.sax.Locator;
+
+/**
+ * SAX Locator implementation that uses libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeLocator
+implements Locator
+{
+
+ // An xmlParserCtxtPtr
+ private final Object ctx;
+
+ // An xmlSAXLocatorPtr
+ private final Object loc;
+
+ GnomeLocator (Object ctx, Object loc)
+ {
+ this.ctx = ctx;
+ this.loc = loc;
+ if (ctx == null)
+ {
+ throw new NullPointerException ("ctx");
+ }
+ if (loc == null)
+ {
+ throw new NullPointerException ("loc");
+ }
+ }
+
+ public String getPublicId ()
+ {
+ return publicId (ctx, loc);
+ }
+
+ private native String publicId (Object ctx, Object loc);
+
+ public String getSystemId ()
+ {
+ return systemId (ctx, loc);
+ }
+
+ private native String systemId (Object ctx, Object loc);
+
+ public int getLineNumber ()
+ {
+ return lineNumber (ctx, loc);
+ }
+
+ private native int lineNumber (Object ctx, Object loc);
+
+ public int getColumnNumber ()
+ {
+ return columnNumber (ctx, loc);
+ }
+
+ private native int columnNumber (Object ctx, Object loc);
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParser.java b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParser.java
new file mode 100644
index 000000000..86d326b7c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParser.java
@@ -0,0 +1,105 @@
+/* GnomeSAXParser.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.sax;
+
+import javax.xml.parsers.SAXParser;
+
+import org.xml.sax.Parser;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.XMLReader;
+
+/**
+ * JAXP SAX parser implementation that uses libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GnomeSAXParser
+extends SAXParser
+{
+
+ private boolean namespaceAware;
+ private boolean validating;
+
+ /**
+ * Creates a new SAX parser.
+ */
+ GnomeSAXParser (boolean namespaceAware, boolean validating)
+ {
+ this.namespaceAware = namespaceAware;
+ this.validating = validating;
+ }
+
+ public Parser getParser ()
+ throws SAXException
+ {
+ throw new SAXNotSupportedException ("SAX version 1 not supported");
+ }
+
+ public XMLReader getXMLReader ()
+ throws SAXException
+ {
+ return new GnomeXMLReader (namespaceAware, validating);
+ }
+
+ public Object getProperty (String name)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ GnomeXMLReader.checkPropertyName (name);
+ throw new SAXNotSupportedException (name);
+ }
+
+ public void setProperty (String name, Object value)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ GnomeXMLReader.checkPropertyName (name);
+ throw new SAXNotSupportedException (name);
+ }
+
+ public boolean isNamespaceAware ()
+ {
+ return namespaceAware;
+ }
+
+ public boolean isValidating ()
+ {
+ return validating;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParserFactory.java b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParserFactory.java
new file mode 100644
index 000000000..0c2881240
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParserFactory.java
@@ -0,0 +1,92 @@
+/* GnomeSAXParserFactory.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.sax;
+
+import java.util.Map;
+import java.util.HashMap;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+
+/**
+ * JAXP SAX parser factory implementation that uses libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class GnomeSAXParserFactory
+extends SAXParserFactory
+{
+
+ private Map features;
+
+ /**
+ * Creates a new SAX parser factory.
+ */
+ public GnomeSAXParserFactory ()
+ {
+ features = new HashMap ();
+ }
+
+ public SAXParser newSAXParser ()
+ throws ParserConfigurationException, SAXException
+ {
+ // TODO features
+ return new GnomeSAXParser (isNamespaceAware (), isValidating ());
+ }
+
+ public boolean getFeature (String name)
+ throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException
+ {
+ GnomeXMLReader.checkFeatureName (name);
+ Boolean val = (Boolean) features.get (name);
+ return (val == null) ? false : val.booleanValue ();
+ }
+
+ public void setFeature (String name, boolean flag)
+ throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException
+ {
+ GnomeXMLReader.checkFeatureName (name);
+ features.put (name, flag ? Boolean.TRUE : Boolean.FALSE);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/GnomeXMLReader.java b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeXMLReader.java
new file mode 100644
index 000000000..3a34bc934
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeXMLReader.java
@@ -0,0 +1,1065 @@
+/* GnomeXMLReader.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.sax;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.DTDHandler;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.ext.DeclHandler;
+import org.xml.sax.ext.LexicalHandler;
+
+import gnu.xml.libxmlj.util.NamedInputStream;
+import gnu.xml.libxmlj.util.StandaloneLocator;
+import gnu.xml.libxmlj.util.XMLJ;
+
+/**
+ * A SAX2 parser that uses libxml2.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class GnomeXMLReader
+implements XMLReader
+{
+
+ static
+ {
+ XMLJ.init ();
+ }
+
+ private static final String FEATURES_PREFIX =
+ "http://xml.org/sax/features/";
+
+ private static final List RECOGNIZED_FEATURES =
+ Arrays.asList (new String[]
+ {
+ "external-general-entities",
+ "external-parameter-entities",
+ "is-standalone",
+ "lexical-handler/parameter-entities",
+ "namespaces",
+ "namespace-prefixes",
+ "resolve-dtd-uris",
+ "string-interning",
+ "use-attributes2",
+ "use-locator2",
+ "use-entity-resolver2",
+ "validation"
+ });
+
+ private static final String PROPERTIES_PREFIX =
+ "http://xml.org/sax/properties/";
+
+ private static final List RECOGNIZED_PROPERTIES =
+ Arrays.asList (new String[]
+ {
+ "declaration-handler",
+ "dom-node",
+ "lexical-handler",
+ "xml-string"
+ });
+
+ // Features
+
+ private transient boolean standalone;
+ private boolean namespaces;
+ private boolean namespacePrefixes;
+ private boolean validation;
+
+ // Callback handlers
+
+ private ContentHandler contentHandler;
+
+ private DTDHandler dtdHandler;
+
+ private EntityResolver entityResolver;
+
+ private ErrorHandler errorHandler;
+
+ private DeclHandler declarationHandler;
+
+ private LexicalHandler lexicalHandler;
+
+ private GnomeLocator locator;
+
+ // Namespace helper for handling callbacks
+ private transient Namespaces ns;
+
+ // If true, do not invoke callback methods except endDocument
+ private transient boolean seenFatalError;
+
+ private transient boolean seenStartDocument;
+
+ private transient String base;
+
+ public GnomeXMLReader ()
+ {
+ this (true, true);
+ }
+
+ public GnomeXMLReader (boolean namespaces, boolean validation)
+ {
+ this.namespaces = namespaces;
+ this.validation = validation;
+ ns = new Namespaces ();
+ }
+
+ public ContentHandler getContentHandler ()
+ {
+ return contentHandler;
+ }
+
+ public void setContentHandler (ContentHandler handler)
+ {
+ contentHandler = handler;
+ }
+
+ public DTDHandler getDTDHandler ()
+ {
+ return dtdHandler;
+ }
+
+ public void setDTDHandler (DTDHandler handler)
+ {
+ dtdHandler = handler;
+ }
+
+ public EntityResolver getEntityResolver ()
+ {
+ return entityResolver;
+ }
+
+ public void setEntityResolver (EntityResolver resolver)
+ {
+ entityResolver = resolver;
+ }
+
+ public ErrorHandler getErrorHandler ()
+ {
+ return errorHandler;
+ }
+
+ public void setErrorHandler (ErrorHandler handler)
+ {
+ errorHandler = handler;
+ }
+
+ // Features
+
+ public boolean getFeature (String name)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ checkFeatureName (name);
+ String key = name.substring (FEATURES_PREFIX.length ());
+ if ("external-general-entities".equals (key))
+ {
+ return validation; // TODO check this
+ }
+ else if ("external-parameter-entities".equals (key))
+ {
+ return validation; // TODO check this
+ }
+ else if ("is-standalone".equals (key))
+ {
+ return standalone;
+ }
+ else if ("namespaces".equals (key))
+ {
+ return namespaces;
+ }
+ else if ("namespace-prefixes".equals (key))
+ {
+ return namespacePrefixes;
+ }
+ else if ("resolve-dtd-uris".equals (key))
+ {
+ return true;
+ }
+ else if ("validation".equals (key))
+ {
+ return validation;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ public void setFeature (String name, boolean value)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ checkFeatureName (name);
+ String key = name.substring (FEATURES_PREFIX.length ());
+ if ("namespaces".equals (key))
+ {
+ namespaces = value;
+ }
+ else if ("namespace-prefixes".equals (key))
+ {
+ namespacePrefixes = value;
+ }
+ else if ("validation".equals (key))
+ {
+ validation = value;
+ }
+ }
+
+ /**
+ * Check that the specified feature name is recognized.
+ */
+ static void checkFeatureName (String name)
+ throws SAXNotRecognizedException
+ {
+ if (name == null || !name.startsWith (FEATURES_PREFIX))
+ {
+ throw new SAXNotRecognizedException (name);
+ }
+ String key = name.substring (FEATURES_PREFIX.length ());
+ if (!RECOGNIZED_FEATURES.contains (key))
+ {
+ throw new SAXNotRecognizedException (name);
+ }
+ }
+
+ // Properties
+
+ public Object getProperty (String name)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ checkPropertyName (name);
+ String key = name.substring (PROPERTIES_PREFIX.length ());
+ if ("declaration-handler".equals (key))
+ {
+ return getDeclarationHandler ();
+ }
+ else if ("lexical-handler".equals (key))
+ {
+ return getLexicalHandler ();
+ }
+ else
+ {
+ throw new SAXNotSupportedException (name);
+ }
+ }
+
+ public void setProperty (String name, Object value)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ checkPropertyName (name);
+ String key = name.substring (PROPERTIES_PREFIX.length ());
+ if ("declaration-handler".equals (key))
+ {
+ setDeclarationHandler ((DeclHandler) value);
+ }
+ else if ("lexical-handler".equals (key))
+ {
+ setLexicalHandler ((LexicalHandler) value);
+ }
+ }
+
+ public DeclHandler getDeclarationHandler ()
+ {
+ return declarationHandler;
+ }
+
+ public void setDeclarationHandler (DeclHandler declarationHandler)
+ {
+ this.declarationHandler = declarationHandler;
+ }
+
+ public LexicalHandler getLexicalHandler ()
+ {
+ return lexicalHandler;
+ }
+
+ public void setLexicalHandler (LexicalHandler lexicalHandler)
+ {
+ this.lexicalHandler = lexicalHandler;
+ }
+
+ /**
+ * Check that the specified property name is recognized.
+ */
+ static void checkPropertyName (String name)
+ throws SAXNotRecognizedException
+ {
+ if (!name.startsWith (PROPERTIES_PREFIX))
+ {
+ throw new SAXNotRecognizedException (name);
+ }
+ String key = name.substring (PROPERTIES_PREFIX.length ());
+ if (!RECOGNIZED_PROPERTIES.contains (key))
+ {
+ throw new SAXNotRecognizedException (name);
+ }
+ }
+
+ // Parse
+
+ public void parse (String systemId)
+ throws IOException, SAXException
+ {
+ URL url = null;
+ try
+ {
+ url = new URL (systemId);
+ }
+ catch (MalformedURLException e)
+ {
+ File file = new File(systemId);
+ if (!file.exists ())
+ {
+ throw new FileNotFoundException (systemId);
+ }
+ String path = file.getAbsolutePath();
+ if (File.separatorChar != '/')
+ {
+ path = path.replace (File.separatorChar, '/');
+ }
+ if (!path.startsWith ("/"))
+ {
+ path = "/" + path;
+ }
+ if (!path.endsWith ("/") && file.isDirectory ())
+ {
+ path = path + "/";
+ }
+ url = new URL ("file:" + path);
+ }
+ InputSource source = new InputSource(url.toString ());
+ source.setByteStream (url.openStream ());
+ parse (source);
+ }
+
+ public synchronized void parse (InputSource input)
+ throws IOException, SAXException
+ {
+ NamedInputStream in = XMLJ.getInputStream (input);
+ byte[] detectBuffer = in.getDetectBuffer ();
+ String publicId = input.getPublicId ();
+ String systemId = input.getSystemId ();
+ base = XMLJ.getBaseURI (systemId);
+ // Reset state
+ standalone = false;
+ seenFatalError = false;
+ seenStartDocument = false;
+ if (systemId != null)
+ {
+ int lsi = systemId.lastIndexOf ('/');
+ if (lsi != -1)
+ {
+ base = systemId.substring (0, lsi + 1);
+ }
+ }
+ // Handle zero-length document
+ if (detectBuffer == null)
+ {
+ startDocument (true);
+ fatalError ("No document element", 0, 0, publicId, systemId);
+ endDocument ();
+ return;
+ }
+ // Parse
+ parseStream(in,
+ detectBuffer,
+ publicId,
+ systemId,
+ base,
+ validation,
+ contentHandler != null,
+ dtdHandler != null,
+ entityResolver != null,
+ errorHandler != null,
+ declarationHandler != null,
+ lexicalHandler != null);
+ in.close ();
+ }
+
+ native void parseStream (InputStream in,
+ byte[] detectBuffer,
+ String publicId,
+ String systemId,
+ String base,
+ boolean validate,
+ boolean contentHandler,
+ boolean dtdHandler,
+ boolean entityResolver,
+ boolean errorHandler,
+ boolean declarationHandler,
+ boolean lexicalHandler)
+ throws IOException, SAXException;
+
+ String getURI (String prefix)
+ {
+ if (!namespaces)
+ {
+ return null;
+ }
+ return ns.getURI (prefix);
+ }
+
+ // Callbacks from libxmlj
+
+ private void startDTD (String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (seenFatalError || lexicalHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ systemId = XMLJ.getAbsoluteURI (base, systemId);
+ lexicalHandler.startDTD (name, publicId, systemId);
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void externalEntityDecl (String name, String publicId,
+ String systemId)
+ throws SAXException
+ {
+ if (seenFatalError || declarationHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ systemId = XMLJ.getAbsoluteURI (base, systemId);
+ declarationHandler.externalEntityDecl (name, publicId, systemId);
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void internalEntityDecl (String name, String value)
+ throws SAXException
+ {
+ if (seenFatalError || declarationHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ declarationHandler.internalEntityDecl (name, value);
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private InputStream resolveEntity (String publicId, String systemId)
+ throws SAXException, IOException
+ {
+ if (entityResolver == null)
+ {
+ return null;
+ }
+ try
+ {
+ systemId = XMLJ.getAbsoluteURI (base, systemId);
+ InputSource source = entityResolver.resolveEntity (publicId, systemId);
+ return (source == null) ? null : XMLJ.getInputStream (source);
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void notationDecl (String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (seenFatalError || dtdHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ systemId = XMLJ.getAbsoluteURI (base, systemId);
+ dtdHandler.notationDecl (name, publicId, systemId);
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void attributeDecl (String eName, String aName, String type,
+ String mode, String value)
+ throws SAXException
+ {
+ if (seenFatalError || declarationHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ declarationHandler.attributeDecl (eName, aName, type, mode, value);
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void elementDecl (String name, String model)
+ throws SAXException
+ {
+ if (seenFatalError || declarationHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ declarationHandler.elementDecl (name, model);
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void unparsedEntityDecl (String name, String publicId,
+ String systemId, String notationName)
+ throws SAXException
+ {
+ if (seenFatalError || dtdHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ systemId = XMLJ.getAbsoluteURI (base, systemId);
+ dtdHandler.unparsedEntityDecl (name, publicId, systemId,
+ notationName);
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void setDocumentLocator (Object ctx, Object loc)
+ {
+ locator = new GnomeLocator (ctx, loc);
+ if (seenFatalError || contentHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ contentHandler.setDocumentLocator (locator);
+ }
+ catch (Exception e)
+ {
+ }
+ }
+
+ private void startDocument (boolean standalone)
+ throws SAXException
+ {
+ this.standalone = standalone;
+ seenStartDocument = true;
+ if (contentHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ contentHandler.startDocument ();
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void endDocument ()
+ throws SAXException
+ {
+ if (contentHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ contentHandler.endDocument();
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void startElement(String name, String[] attrs)
+ throws SAXException
+ {
+ if (seenFatalError || contentHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ XMLName xName = new XMLName (this, name);
+ if (namespaces)
+ {
+ // Handle defined namespaces
+ ns.push ();
+ int len = (attrs == null) ? 0 : attrs.length;
+ if (len > 0)
+ {
+ ArrayList filtered = new ArrayList (len);
+ for (int i = 0; i < len; i += 2)
+ {
+ String attName = attrs[i];
+ String attValue = attrs[i + 1];
+ if (attName.equals ("xmlns"))
+ {
+ startPrefixMapping ("", attValue);
+ }
+ else if (attName.startsWith ("xmlns:"))
+ {
+ startPrefixMapping (attName.substring (6), attValue);
+ }
+ else
+ {
+ filtered.add (attName);
+ filtered.add (attValue);
+ }
+ }
+ // Remove xmlns attributes
+ attrs = new String[filtered.size ()];
+ filtered.toArray (attrs);
+ }
+ }
+ // Construct attributes
+ Attributes atts = new StringArrayAttributes (this, attrs);
+ contentHandler.startElement (xName.uri, xName.localName, xName.qName,
+ atts);
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void endElement (String name)
+ throws SAXException
+ {
+ if (seenFatalError || contentHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ XMLName xName = new XMLName (this, name);
+ String uri = (xName.uri == null) ? "" : xName.uri;
+ contentHandler.endElement (uri, xName.localName, xName.qName);
+ // Handle undefining namespaces
+ if (namespaces)
+ {
+ for (Iterator i = ns.currentPrefixes (); i.hasNext (); )
+ {
+ endPrefixMapping ((String) i.next ());
+ }
+ ns.pop (); // releases current depth
+ }
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void startPrefixMapping (String prefix, String uri)
+ throws SAXException
+ {
+ if (seenFatalError || contentHandler == null)
+ {
+ return;
+ }
+ ns.define (prefix, uri);
+ contentHandler.startPrefixMapping (prefix, uri);
+ }
+
+ private void endPrefixMapping (String prefix)
+ throws SAXException
+ {
+ if (seenFatalError || contentHandler == null)
+ {
+ return;
+ }
+ contentHandler.endPrefixMapping (prefix);
+ }
+
+ private void characters (String text)
+ throws SAXException
+ {
+ if (seenFatalError || contentHandler == null || text == null)
+ {
+ return;
+ }
+ try
+ {
+ char[] ch = text.toCharArray ();
+ contentHandler.characters (ch, 0, ch.length);
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void ignorableWhitespace (String text)
+ throws SAXException
+ {
+ if (seenFatalError || contentHandler == null || text == null)
+ {
+ return;
+ }
+ try
+ {
+ char[] ch = text.toCharArray ();
+ contentHandler.ignorableWhitespace (ch, 0, ch.length);
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void processingInstruction (String target, String data)
+ throws SAXException
+ {
+ if (seenFatalError || contentHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ if (data == null)
+ {
+ data = "";
+ }
+ contentHandler.processingInstruction (target, data);
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void comment (String text)
+ throws SAXException
+ {
+ if (seenFatalError || lexicalHandler == null || text == null)
+ {
+ return;
+ }
+ try
+ {
+ char[] ch = text.toCharArray ();
+ lexicalHandler.comment (ch, 0, ch.length);
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void cdataBlock (String text)
+ throws SAXException
+ {
+ if (seenFatalError || text == null)
+ {
+ return;
+ }
+ try
+ {
+ if (lexicalHandler == null)
+ {
+ characters(text);
+ }
+ else
+ {
+ lexicalHandler.startCDATA();
+ characters(text);
+ lexicalHandler.endCDATA();
+ }
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void warning (String message,
+ int lineNumber, int columnNumber,
+ String publicId, String systemId)
+ throws SAXException
+ {
+ if (seenFatalError || errorHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ Locator l = new StandaloneLocator (lineNumber, columnNumber,
+ publicId, systemId);
+ errorHandler.warning (new SAXParseException (message, l));
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void error (String message,
+ int lineNumber, int columnNumber,
+ String publicId, String systemId)
+ throws SAXException
+ {
+ if (seenFatalError || errorHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ Locator l = new StandaloneLocator (lineNumber, columnNumber,
+ publicId, systemId);
+ errorHandler.error (new SAXParseException (message, l));
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+ private void fatalError (String message,
+ int lineNumber, int columnNumber,
+ String publicId, String systemId)
+ throws SAXException
+ {
+ if (seenFatalError || errorHandler == null)
+ {
+ return;
+ }
+ try
+ {
+ if (!seenStartDocument)
+ {
+ startDocument (false);
+ }
+ seenFatalError = true;
+ Locator l = new StandaloneLocator (lineNumber, columnNumber,
+ publicId, systemId);
+ errorHandler.fatalError (new SAXParseException (message, l));
+ }
+ catch (Exception e)
+ {
+ if (e instanceof SAXException)
+ {
+ throw (SAXException) e;
+ }
+ else
+ {
+ throw new SAXException (e);
+ }
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/Namespaces.java b/libjava/classpath/gnu/xml/libxmlj/sax/Namespaces.java
new file mode 100644
index 000000000..473d02028
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/sax/Namespaces.java
@@ -0,0 +1,122 @@
+/* Namespaces.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.sax;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+
+/**
+ * Helper class for managing namespaces.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class Namespaces
+{
+
+ ArrayList stack = new ArrayList ();
+
+ /**
+ * Increments the tree depth.
+ * This allocates a new potential namespace entry.
+ */
+ void push ()
+ {
+ stack.add (null);
+ }
+
+ /**
+ * Decrements the tree depth.
+ * This removes namespaces defined at the extremity.
+ */
+ void pop ()
+ {
+ stack.remove (stack.size() - 1);
+ }
+
+ /**
+ * Searches for the namespace URI corresponding to the specified prefix.
+ */
+ String getURI (String prefix)
+ {
+ for (int i = stack.size () - 1; i >= 0; i--)
+ {
+ HashMap ns = (HashMap) stack.get (i);
+ if (ns != null && ns.containsKey (prefix))
+ {
+ String ret = (String) ns.get (prefix);
+ return (ret == null) ? "" : ret;
+ }
+ }
+ return "";
+ }
+
+ /**
+ * Defines the specified prefix-URI mapping at the current depth in the
+ * tree.
+ */
+ void define (String prefix, String uri)
+ {
+ int index = stack.size () - 1;
+ HashMap ns = (HashMap) stack.get (index);
+ if (ns == null)
+ {
+ ns = new HashMap ();
+ stack.set (index, ns);
+ }
+ ns.put (prefix, uri);
+ }
+
+ /**
+ * Returns an iterator over the prefixes defined at the current depth.
+ */
+ Iterator currentPrefixes ()
+ {
+ HashMap ns = (HashMap) stack.get (stack.size () - 1);
+ if (ns == null)
+ {
+ return Collections.EMPTY_LIST.iterator ();
+ }
+ else
+ {
+ return ns.keySet ().iterator ();
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/StringArrayAttributes.java b/libjava/classpath/gnu/xml/libxmlj/sax/StringArrayAttributes.java
new file mode 100644
index 000000000..f5a753c56
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/sax/StringArrayAttributes.java
@@ -0,0 +1,170 @@
+/* StringArrayAttributes.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.sax;
+
+import org.xml.sax.Attributes;
+
+/**
+ * An implementation of Attributes that reads values from an array of
+ * strings, supplied by libxml2.
+ * Each pair of elements in the array represents a key followed by a value.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class StringArrayAttributes
+implements Attributes
+{
+
+ private int len;
+ private XMLName[] keys;
+ private String[] values;
+
+ StringArrayAttributes (GnomeXMLReader parser, String[] pairs)
+ {
+ len = (pairs == null) ? 0 : pairs.length / 2;
+ keys = new XMLName[len];
+ values = new String[len];
+ for (int i = 0; i < len; i++)
+ {
+ int pairIndex = i * 2;
+ keys[i] = new XMLName (parser, pairs[pairIndex]);
+ values[i] = pairs[pairIndex + 1];
+ }
+ }
+
+ public int getLength ()
+ {
+ return len;
+ }
+
+ public String getURI (int index)
+ {
+ if (index < 0 || index >= len)
+ {
+ return null;
+ }
+ return keys[index].uri;
+ }
+
+ public String getLocalName (int index)
+ {
+ if (index < 0 || index >= len)
+ {
+ return null;
+ }
+ return keys[index].localName;
+ }
+
+ public String getQName (int index)
+ {
+ if (index < 0 || index >= len)
+ {
+ return null;
+ }
+ return keys[index].qName;
+ }
+
+ public String getType (int index)
+ {
+ if (index < 0 || index >= len)
+ {
+ return null;
+ }
+ // TODO can we get this information from libxml2?
+ return "CDATA";
+ }
+
+ public String getValue (int index)
+ {
+ if (index < 0 || index >= len)
+ {
+ return null;
+ }
+ return values[index];
+ }
+
+ public int getIndex (String uri, String localName)
+ {
+ for (int i = 0; i < len; i++)
+ {
+ XMLName key = keys[i];
+ if (key.localName.equals (localName))
+ {
+ if ((key.uri == null && uri == null) ||
+ (key.uri != null && key.uri.equals(uri)))
+ {
+ return i;
+ }
+ }
+ }
+ return -1;
+ }
+
+ public int getIndex (String qName)
+ {
+ for (int i = 0; i < len; i++)
+ {
+ if (keys[i].qName.equals (qName))
+ {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ public String getType (String uri, String localName)
+ {
+ return getType (getIndex (uri, localName));
+ }
+
+ public String getType (String qName)
+ {
+ return getType (getIndex (qName));
+ }
+
+ public String getValue (String uri, String localName)
+ {
+ return getValue (getIndex (uri, localName));
+ }
+
+ public String getValue (String qName)
+ {
+ return getValue (getIndex (qName));
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/XMLName.java b/libjava/classpath/gnu/xml/libxmlj/sax/XMLName.java
new file mode 100644
index 000000000..b9bdf9159
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/sax/XMLName.java
@@ -0,0 +1,92 @@
+/* XMLName.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.sax;
+
+/**
+ * Structure containing the components of an XML element/attribute name.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class XMLName
+{
+
+ private static final String XML_URI = "http://www.w3.org/XML/1998/namespace";
+
+ final String uri;
+ final String localName;
+ final String qName;
+ final String prefix;
+
+ XMLName (GnomeXMLReader parser, String qName)
+ {
+ this.qName = qName;
+ int ci = qName.lastIndexOf (':');
+ if (ci < 1)
+ {
+ localName = qName;
+ prefix = null;
+ uri = "";
+ }
+ else
+ {
+ localName = qName.substring (ci + 1);
+ prefix = qName.substring (0, ci);
+ if ("xml".equals (prefix))
+ {
+ if ("lang".equals (localName) || "space".equals (localName))
+ {
+ uri = XML_URI;
+ }
+ else
+ {
+ uri = parser.getURI (prefix);
+ }
+ }
+ else
+ {
+ uri = parser.getURI (prefix);
+ }
+ }
+ }
+
+ public String toString ()
+ {
+ return qName;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/ErrorListenerErrorHandler.java b/libjava/classpath/gnu/xml/libxmlj/transform/ErrorListenerErrorHandler.java
new file mode 100644
index 000000000..398a0ba2e
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/transform/ErrorListenerErrorHandler.java
@@ -0,0 +1,111 @@
+/* ErrorListenerErrorHandler.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.transform;
+
+import javax.xml.transform.ErrorListener;
+import javax.xml.transform.TransformerException;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+/**
+ * Provides a SAX ErrorHandler interface to an ErrorListener.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class ErrorListenerErrorHandler
+implements ErrorHandler
+{
+
+ private ErrorListener listener;
+
+ ErrorListenerErrorHandler (ErrorListener listener)
+ {
+ this.listener = listener;
+ }
+
+ public void warning (SAXParseException e)
+ throws SAXException
+ {
+ try
+ {
+ listener.warning (new TransformerException (e));
+ }
+ catch (TransformerException te)
+ {
+ throw getSAXException (te);
+ }
+ }
+
+ public void error (SAXParseException e)
+ throws SAXException
+ {
+ try
+ {
+ listener.error (new TransformerException (e));
+ }
+ catch (TransformerException te)
+ {
+ throw getSAXException (te);
+ }
+ }
+
+ public void fatalError (SAXParseException e)
+ throws SAXException
+ {
+ try
+ {
+ listener.fatalError (new TransformerException (e));
+ }
+ catch (TransformerException te)
+ {
+ throw getSAXException (te);
+ }
+ }
+
+ private SAXException getSAXException (TransformerException e)
+ {
+ Throwable cause = e.getCause ();
+ if (cause instanceof SAXException)
+ {
+ return (SAXException) cause;
+ }
+ return new SAXException (e);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformer.java b/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformer.java
new file mode 100755
index 000000000..5f31cb898
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformer.java
@@ -0,0 +1,572 @@
+/* GnomeTransformer.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.transform;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import java.net.URL;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.xml.transform.ErrorListener;
+import javax.xml.transform.Source;
+import javax.xml.transform.SourceLocator;
+import javax.xml.transform.Result;
+import javax.xml.transform.Templates;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.URIResolver;
+
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Node;
+
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+
+import gnu.xml.libxmlj.dom.GnomeDocument;
+import gnu.xml.libxmlj.sax.GnomeXMLReader;
+import gnu.xml.libxmlj.util.NamedInputStream;
+import gnu.xml.libxmlj.util.StandaloneLocator;
+import gnu.xml.libxmlj.util.XMLJ;
+
+/**
+ * An implementation of {@link javax.xml.transform.Transformer} which
+ * performs XSLT transformation using <code>libxslt</code>.
+ *
+ * @author Julian Scheid
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class GnomeTransformer
+ extends Transformer
+ implements Templates
+{
+
+ /**
+ * The parameters added by the user via {@link setParameter()}.
+ */
+ private Map parameters;
+
+ /**
+ * The output properties set by the user.
+ */
+ private Properties outputProperties;
+
+ /**
+ * The URI resolver to use during transformation.
+ */
+ private URIResolver resolver;
+
+ /**
+ * The error listener for transformation errors.
+ */
+ private ErrorListener errorListener;
+
+ /**
+ * Handle to the source stylesheet.
+ * This is a native pointer of type xsltStylesheetPtr.
+ */
+ private Object stylesheet;
+
+ /**
+ * Constructor.
+ * @param source the XSLT stylesheet document source
+ * @param resolver the resolver to use during transformation
+ * @param errorListener the error listener for transformation errors
+ */
+ GnomeTransformer (Source source,
+ URIResolver resolver,
+ ErrorListener errorListener)
+ throws TransformerConfigurationException
+ {
+ this.resolver = resolver;
+ this.errorListener = errorListener;
+ parameters = new HashMap ();
+ outputProperties = new Properties ();
+
+ if (source == null)
+ {
+ stylesheet = newStylesheet ();
+ }
+ else if (source instanceof StreamSource)
+ {
+ try
+ {
+ StreamSource ss = (StreamSource) source;
+ NamedInputStream in = XMLJ.getInputStream (ss);
+ String systemId = ss.getSystemId ();
+ String publicId = ss.getPublicId ();
+ String base = XMLJ.getBaseURI (systemId);
+ byte[] detectBuffer = in.getDetectBuffer ();
+ if (detectBuffer == null)
+ {
+ String msg = "No document element";
+ throw new TransformerConfigurationException (msg);
+ }
+ stylesheet = newStylesheetFromStream (in, detectBuffer, publicId,
+ systemId, base,
+ (resolver != null),
+ (errorListener != null));
+ }
+ catch (IOException e)
+ {
+ throw new TransformerConfigurationException (e);
+ }
+ }
+ else if (source instanceof DOMSource)
+ {
+ DOMSource ds = (DOMSource) source;
+ Node node = ds.getNode ();
+ if (!(node instanceof GnomeDocument))
+ {
+ String msg = "Node is not a GnomeDocument";
+ throw new TransformerConfigurationException (msg);
+ }
+ GnomeDocument doc = (GnomeDocument) node;
+ stylesheet = newStylesheetFromDoc (doc);
+ }
+ else
+ {
+ String msg = "Source type not supported (" + source + ")";
+ throw new TransformerConfigurationException (msg);
+ }
+ }
+
+ /**
+ * Copy constructor.
+ */
+ private GnomeTransformer (Object stylesheet,
+ URIResolver resolver,
+ ErrorListener errorListener,
+ Map parameters,
+ Properties outputProperties)
+ {
+ this.stylesheet = stylesheet;
+ this.resolver = resolver;
+ this.errorListener = errorListener;
+ this.parameters = parameters;
+ this.outputProperties = outputProperties;
+ }
+
+ private native Object newStylesheet ()
+ throws TransformerConfigurationException;
+
+ private native Object newStylesheetFromStream (InputStream in,
+ byte[] detectBuffer,
+ String publicId,
+ String systemId,
+ String base,
+ boolean entityResolver,
+ boolean errorHandler)
+ throws TransformerConfigurationException;
+
+ private native Object newStylesheetFromDoc (GnomeDocument doc)
+ throws TransformerConfigurationException;
+
+ //--- Implementation of javax.xml.transform.Transformer follows.
+
+ // Set, get and clear the parameters to use on transformation
+
+ public synchronized void setParameter (String parameter, Object value)
+ {
+ parameters.put (parameter, value);
+ }
+
+ public synchronized Object getParameter (String name)
+ {
+ return parameters.get (name);
+ }
+
+ public synchronized void clearParameters ()
+ {
+ parameters.clear ();
+ }
+
+ // Set and get the ErrorListener to use on transformation
+
+ public void setErrorListener (ErrorListener listener)
+ {
+ this.errorListener = listener;
+ }
+
+ public ErrorListener getErrorListener ()
+ {
+ return errorListener;
+ }
+
+ // Set and get the URIResolver to use on transformation
+
+ public void setURIResolver (URIResolver resolver)
+ {
+ this.resolver = resolver;
+ }
+
+ public URIResolver getURIResolver ()
+ {
+ return resolver;
+ }
+
+ // Set the output properties to use on transformation; get default
+ // output properties and output properties specified in the
+ // stylesheet or by the user.
+
+ public void setOutputProperties (Properties outputProperties)
+ {
+ // Note: defensive copying
+ this.outputProperties = new Properties (outputProperties);
+ }
+
+ public void setOutputProperty (String name, String value)
+ {
+ outputProperties.setProperty (name, value);
+ }
+
+ public Properties getOutputProperties ()
+ {
+ // Note: defensive copying
+ return new Properties (this.outputProperties);
+ }
+
+ public String getOutputProperty (String name)
+ {
+ return outputProperties.getProperty (name);
+ }
+
+ // -- Templates --
+
+ public Transformer newTransformer ()
+ {
+ return new GnomeTransformer (stylesheet, resolver, errorListener,
+ new HashMap (parameters),
+ new Properties (outputProperties));
+ }
+
+ // -- transform --
+
+ /**
+ * Transforms the given source and writes the result to the
+ * given target.
+ */
+ public void transform (Source source, Result result)
+ throws TransformerException
+ {
+ if (source instanceof StreamSource)
+ {
+ try
+ {
+ StreamSource ss = (StreamSource) source;
+ NamedInputStream in = XMLJ.getInputStream (ss);
+ String publicId = ss.getPublicId ();
+ String systemId = ss.getSystemId ();
+ String base = XMLJ.getBaseURI (systemId);
+ byte[] detectBuffer = in.getDetectBuffer ();
+ if (detectBuffer == null)
+ {
+ throw new TransformerException ("No document element");
+ }
+ if (result instanceof StreamResult)
+ {
+ OutputStream out = XMLJ.getOutputStream ((StreamResult) result);
+ transformStreamToStream (in, detectBuffer, publicId, systemId,
+ base, (resolver != null),
+ (errorListener != null), out);
+ }
+ else if (result instanceof DOMResult)
+ {
+ DOMResult dr = (DOMResult) result;
+ GnomeDocument ret =
+ transformStreamToDoc (in, detectBuffer, publicId, systemId,
+ base, (resolver != null),
+ (errorListener != null));
+ dr.setNode (ret);
+ dr.setSystemId (null);
+ }
+ else if (result instanceof SAXResult)
+ {
+ SAXResult sr = (SAXResult) result;
+ transformStreamToSAX (in, detectBuffer, publicId, systemId,
+ base, (resolver != null),
+ (errorListener != null),
+ getSAXContext (sr));
+ }
+ else
+ {
+ String msg = "Result type not supported (" + result + ")";
+ throw new TransformerConfigurationException (msg);
+ }
+ }
+ catch (IOException e)
+ {
+ throw new TransformerException (e);
+ }
+ }
+ else if (source instanceof DOMSource)
+ {
+ DOMSource ds = (DOMSource) source;
+ Node node = ds.getNode ();
+ if (!(node instanceof GnomeDocument))
+ {
+ String msg = "Node is not a GnomeDocument (" + node + ")";
+ throw new TransformerException (msg);
+ }
+ GnomeDocument doc = (GnomeDocument) node;
+ if (result instanceof StreamResult)
+ {
+ try
+ {
+ OutputStream out = XMLJ.getOutputStream ((StreamResult) result);
+ transformDocToStream (doc, out);
+ }
+ catch (IOException e)
+ {
+ throw new TransformerException (e);
+ }
+ }
+ else if (result instanceof DOMResult)
+ {
+ DOMResult dr = (DOMResult) result;
+ GnomeDocument ret = transformDocToDoc (doc);
+ dr.setNode (ret);
+ dr.setSystemId (null);
+ }
+ else if (result instanceof SAXResult)
+ {
+ SAXResult sr = (SAXResult) result;
+ transformDocToSAX (doc, getSAXContext (sr));
+ }
+ else
+ {
+ String msg = "Result type not supported";
+ throw new TransformerConfigurationException (msg);
+ }
+ }
+ else
+ {
+ String msg = "Source type not supported";
+ throw new TransformerConfigurationException (msg);
+ }
+ }
+
+ private GnomeXMLReader getSAXContext (SAXResult result)
+ {
+ GnomeXMLReader ctx = new GnomeXMLReader ();
+ ctx.setContentHandler (result.getHandler ());
+ ctx.setLexicalHandler (result.getLexicalHandler ());
+ if (errorListener != null)
+ {
+ ErrorHandler errorHandler =
+ new ErrorListenerErrorHandler (errorListener);
+ ctx.setErrorHandler (errorHandler);
+ }
+ if (resolver != null)
+ {
+ EntityResolver entityResolver =
+ new URIResolverEntityResolver (resolver);
+ ctx.setEntityResolver (entityResolver);
+ }
+ return ctx;
+ }
+
+ private native void transformStreamToStream (InputStream in,
+ byte[] detectBuffer,
+ String publicId,
+ String systemId,
+ String base,
+ boolean entityResolver,
+ boolean errorHandler,
+ OutputStream out)
+ throws TransformerException;
+
+ private native GnomeDocument transformStreamToDoc (InputStream in,
+ byte[] detectBuffer,
+ String publicId,
+ String systemId,
+ String base,
+ boolean entityResolver,
+ boolean errorHandler)
+ throws TransformerException;
+
+ private native void transformStreamToSAX (InputStream in,
+ byte[] detectBuffer,
+ String publicId,
+ String systemId,
+ String base,
+ boolean entityResolver,
+ boolean errorHandler,
+ GnomeXMLReader out)
+ throws TransformerException;
+
+ private native void transformDocToStream (GnomeDocument in,
+ OutputStream out)
+ throws TransformerException;
+
+ private native GnomeDocument transformDocToDoc (GnomeDocument in)
+ throws TransformerException;
+
+ private native void transformDocToSAX (GnomeDocument in,
+ GnomeXMLReader out)
+ throws TransformerException;
+
+ /*
+ * Retrieve parameters as a string array.
+ * This is a convenience method called from native code.
+ */
+ private String[] getParameterArray ()
+ {
+ String[] parameterArray = new String[parameters.size () * 2];
+ int index = 0;
+ for (Iterator it = parameters.keySet ().iterator ();
+ it.hasNext ();
+ ++index)
+ {
+ String parameterKey = (String) it.next ();
+ String parameterValue = (String) parameters.get (parameterKey);
+ parameterArray[index * 2 + 0] = parameterKey;
+ parameterArray[index * 2 + 1] =
+ "'" + ((parameterValue != null) ? parameterValue : "") + "'";
+ // FIXME encode parameter value correctly for XPath
+ }
+ return parameterArray;
+ }
+
+ // -- Free xsltStylesheet handle --
+
+ public void finalize ()
+ {
+ if (stylesheet != null)
+ {
+ free ();
+ stylesheet = null;
+ }
+ }
+
+ private native void free ();
+
+ // -- Callbacks --
+
+ private InputStream resolveEntity (String publicId, String systemId)
+ throws TransformerException
+ {
+ if (resolver != null)
+ {
+ systemId = resolver.resolve (null, systemId).getSystemId ();
+ }
+ if (systemId == null)
+ {
+ return null;
+ }
+ try
+ {
+ URL url = new URL (systemId);
+ return XMLJ.getInputStream (url);
+ }
+ catch (IOException e)
+ {
+ throw new TransformerException (e);
+ }
+ }
+
+ private void setDocumentLocator (Object ctx, Object loc)
+ {
+ }
+
+ private void warning (String message,
+ int lineNumber,
+ int columnNumber,
+ String publicId,
+ String systemId)
+ throws TransformerException
+ {
+ if (errorListener == null)
+ {
+ return;
+ }
+ SourceLocator l = new StandaloneLocator (lineNumber,
+ columnNumber,
+ publicId,
+ systemId);
+ errorListener.warning (new TransformerException (message, l));
+ }
+
+ private void error (String message,
+ int lineNumber,
+ int columnNumber,
+ String publicId,
+ String systemId)
+ throws TransformerException
+ {
+ if (errorListener == null)
+ {
+ return;
+ }
+ SourceLocator l = new StandaloneLocator (lineNumber,
+ columnNumber,
+ publicId,
+ systemId);
+ errorListener.error (new TransformerException (message, l));
+ }
+
+ private void fatalError (String message,
+ int lineNumber,
+ int columnNumber,
+ String publicId,
+ String systemId)
+ throws TransformerException
+ {
+ if (errorListener == null)
+ {
+ return;
+ }
+ SourceLocator l = new StandaloneLocator (lineNumber,
+ columnNumber,
+ publicId,
+ systemId);
+ errorListener.fatalError (new TransformerException (message, l));
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformerFactory.java b/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformerFactory.java
new file mode 100755
index 000000000..4a0100a21
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformerFactory.java
@@ -0,0 +1,349 @@
+/* GnomeTransformerFactory.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.transform;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.parsers.FactoryConfigurationError;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import javax.xml.transform.ErrorListener;
+import javax.xml.transform.Source;
+import javax.xml.transform.Templates;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.URIResolver;
+
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.DefaultHandler;
+
+import gnu.xml.libxmlj.util.XMLJ;
+
+/**
+ * An implementation of <code>TransformerFactory</code> producing
+ * <code>Transformer</code> objects which use <code>libxslt</code>
+ * for transformation.
+ *
+ * @author Julian Scheid
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class GnomeTransformerFactory
+ extends TransformerFactory
+{
+
+ static
+ {
+ XMLJ.init ();
+ }
+
+ /**
+ * URIResolver set by user, or default implementation.
+ */
+ private URIResolver uriResolver;
+
+ /**
+ * ErrorListener set by user, or default implementation.
+ */
+ private ErrorListener errorListener;
+
+ /**
+ * Attributes set by user.
+ */
+ private Map attributes = new HashMap ();
+
+ //--- Implementation of javax.xml.transform.TransformerFactory
+ //--- follows.
+
+ // -- begin getAssociatedStylesheet implementation --
+
+ /**
+ * Returns the stylesheet associated with the specified XML source, or
+ * <code>null</code> if no associated stylesheet could be found.
+ */
+ public Source getAssociatedStylesheet(Source source, String media,
+ String title, String charset)
+ throws TransformerConfigurationException
+ {
+ String href= null;
+ String base = source.getSystemId();
+ if (source instanceof DOMSource)
+ {
+ Node node = ((DOMSource) source).getNode();
+ Document doc = (node.getNodeType() == Node.DOCUMENT_NODE) ?
+ (Document) node : node.getOwnerDocument();
+ if (base == null)
+ {
+ base = doc.getDocumentURI();
+ }
+ for (node = doc.getFirstChild(); node != null;
+ node = node.getNextSibling())
+ {
+ if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE &&
+ "xml-stylesheet".equals(node.getNodeName()))
+ {
+ String data = node.getNodeValue();
+ if (media != null &&
+ !media.equals(parseParameter(data, "type")))
+ {
+ continue;
+ }
+ if (title != null &&
+ !title.equals(parseParameter(data, "title")))
+ {
+ continue;
+ }
+ href = parseParameter(data, "href");
+ }
+ }
+ }
+ else
+ {
+ InputSource input;
+ XMLReader parser = null;
+ try
+ {
+ if (source instanceof SAXSource)
+ {
+ SAXSource sax = (SAXSource) source;
+ input = sax.getInputSource();
+ parser = sax.getXMLReader();
+ }
+ else
+ {
+ StreamSource stream = (StreamSource) source;
+ InputStream in = stream.getInputStream();
+ input = new InputSource(in);
+ }
+ input.setSystemId(base);
+ if (parser == null)
+ {
+ parser = createXMLReader();
+ }
+ AssociatedStylesheetHandler ash =
+ new AssociatedStylesheetHandler();
+ ash.media = media;
+ ash.title = title;
+ parser.setContentHandler(ash);
+ parser.parse(input);
+ href = ash.href;
+ }
+ catch (SAXException e)
+ {
+ throw new TransformerConfigurationException(e);
+ }
+ catch (IOException e)
+ {
+ throw new TransformerConfigurationException(e);
+ }
+ }
+ if (href == null)
+ {
+ return null;
+ }
+ if (base != null)
+ {
+ base = XMLJ.getBaseURI(base);
+ }
+ href = XMLJ.getAbsoluteURI(base, href);
+ return new StreamSource(href);
+ }
+
+ private XMLReader createXMLReader()
+ throws TransformerConfigurationException
+ {
+ try
+ {
+ SAXParserFactory factory = SAXParserFactory.newInstance();
+ SAXParser parser = factory.newSAXParser();
+ return parser.getXMLReader();
+ }
+ catch (FactoryConfigurationError e)
+ {
+ throw new TransformerConfigurationException(e);
+ }
+ catch (ParserConfigurationException e)
+ {
+ throw new TransformerConfigurationException(e);
+ }
+ catch (SAXException e)
+ {
+ throw new TransformerConfigurationException(e);
+ }
+ }
+
+ class AssociatedStylesheetHandler
+ extends DefaultHandler
+ {
+
+ String media;
+ String title;
+ String href;
+
+ public void processingInstruction(String target, String data)
+ throws SAXException
+ {
+ if ("xml-stylesheet".equals(target))
+ {
+ if (media != null && !media.equals(parseParameter(data, "type")))
+ {
+ return;
+ }
+ if (title != null && !title.equals(parseParameter(data, "title")))
+ {
+ return;
+ }
+ href = parseParameter(data, "href");
+ }
+ }
+
+ }
+
+ String parseParameter(String data, String name)
+ {
+ int start = data.indexOf(name + "=");
+ if (start != -1)
+ {
+ start += name.length() + 2;
+ char delim = data.charAt(start - 1);
+ int end = data.indexOf(delim, start);
+ if (end != -1)
+ {
+ return data.substring(start, end);
+ }
+ }
+ return null;
+ }
+
+ // -- end getAssociatedStylesheet implementation --
+
+ public synchronized void setAttribute (String name, Object value)
+ {
+ this.attributes.put (name, value);
+ }
+
+ public synchronized Object getAttribute (String name)
+ {
+ return attributes.get (name);
+ }
+
+ public void setErrorListener (ErrorListener errorListener)
+ {
+ this.errorListener = errorListener;
+ }
+
+ public ErrorListener getErrorListener ()
+ {
+ return errorListener;
+ }
+
+ public void setURIResolver (URIResolver uriResolver)
+ {
+ this.uriResolver = uriResolver;
+ }
+
+ public URIResolver getURIResolver ()
+ {
+ return uriResolver;
+ }
+
+ public boolean getFeature (String name)
+ {
+ return (StreamSource.FEATURE.equals (name) ||
+ StreamResult.FEATURE.equals (name) ||
+ DOMSource.FEATURE.equals (name) ||
+ DOMResult.FEATURE.equals (name));
+ }
+
+ public void setFeature(String name, boolean value)
+ throws TransformerConfigurationException
+ {
+ throw new TransformerConfigurationException(name);
+ }
+
+ /**
+ * Returns a new instance of class {@link Transformer} for a
+ * null souce.
+ */
+ public Transformer newTransformer ()
+ throws TransformerConfigurationException
+ {
+ return newTransformer (null);
+ }
+
+ /**
+ * Returns a new instance of class {@link Transformer} for
+ * the given souce.
+ */
+ public Transformer newTransformer (Source source)
+ throws TransformerConfigurationException
+ {
+ return new GnomeTransformer (source, uriResolver, errorListener);
+ }
+
+ /**
+ * Returns a new instance of class {@link Templates} for
+ * the given souce.
+ */
+ public Templates newTemplates (Source source)
+ throws TransformerConfigurationException
+ {
+ return new GnomeTransformer (source, uriResolver, errorListener);
+ }
+
+ /**
+ * Perform native cleanup.
+ */
+ public static native void freeLibxsltGlobal ();
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/URIResolverEntityResolver.java b/libjava/classpath/gnu/xml/libxmlj/transform/URIResolverEntityResolver.java
new file mode 100644
index 000000000..0ce71d0ef
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/transform/URIResolverEntityResolver.java
@@ -0,0 +1,87 @@
+/* URIResolverEntityResolver.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.transform;
+
+import java.io.IOException;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.sax.SAXSource;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * Provides an EntityResolver interface to a URIResolver.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class URIResolverEntityResolver
+implements EntityResolver
+{
+
+ private URIResolver resolver;
+
+ URIResolverEntityResolver (URIResolver resolver)
+ {
+ this.resolver = resolver;
+ }
+
+ public InputSource resolveEntity (String publicId, String systemId)
+ throws SAXException, IOException
+ {
+ try
+ {
+ return SAXSource.sourceToInputSource (resolver.resolve (systemId,
+ null));
+ }
+ catch (TransformerException e)
+ {
+ Throwable cause = e.getCause ();
+ if (cause instanceof SAXException)
+ {
+ throw (SAXException) cause;
+ }
+ else if (cause instanceof IOException)
+ {
+ throw (IOException) cause;
+ }
+ throw new SAXException (e);
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/package.html b/libjava/classpath/gnu/xml/libxmlj/transform/package.html
new file mode 100755
index 000000000..dac1027ff
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/transform/package.html
@@ -0,0 +1,14 @@
+<body>
+<p>
+ A JAXP-compliant wrapper for the XSLT C library for Gnome, also
+ known as libxslt. Allows to use libxslt via the Java API for XML
+ processing.
+</p>
+
+<p>
+ <b>Usage:</b>
+ <li>Set the system property <code>javax.xml.transform.TransformerFactory</code>
+ to <code>gnu.xml.libxmlj.GnomeTransformerFactory</code>.</li>
+ </ul>
+</p>
+</body>
diff --git a/libjava/classpath/gnu/xml/libxmlj/util/EmptyNodeList.java b/libjava/classpath/gnu/xml/libxmlj/util/EmptyNodeList.java
new file mode 100644
index 000000000..60019ddd4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/util/EmptyNodeList.java
@@ -0,0 +1,62 @@
+/* EmptyNodeList.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.util;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * An empty node list.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class EmptyNodeList
+implements NodeList
+{
+
+ public Node item (int index)
+ {
+ return null;
+ }
+
+ public int getLength ()
+ {
+ return 0;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/util/NamedInputStream.java b/libjava/classpath/gnu/xml/libxmlj/util/NamedInputStream.java
new file mode 100644
index 000000000..01b3af90d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/util/NamedInputStream.java
@@ -0,0 +1,99 @@
+/* NamedInputStream.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.util;
+
+import java.io.FilterInputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.PushbackInputStream;
+
+/**
+ * An input stream associated with an XML system ID.
+ * It can report the system ID and the first few bytes of the stream
+ * in order to detect the character encoding of the stream.
+ *
+ * @author <a href='dog@gnu.org'>Chris Burdess</a>
+ */
+public class NamedInputStream
+extends FilterInputStream
+{
+
+ private static int DETECT_BUFFER_SIZE = 50;
+
+ private String name;
+
+ NamedInputStream (String name, InputStream in, int size)
+ {
+ super (new PushbackInputStream (in, size));
+ this.name = name;
+ }
+
+ /**
+ * Returns the name of the stream (the XML system ID).
+ */
+ public String getName ()
+ {
+ return name;
+ }
+
+ /**
+ * Returns the first few bytes of the stream for character encoding
+ * purposes. The entire stream can thereafter be read normally from the
+ * beginning. This method is only valid if no bytes have yet been read
+ * from the stream.
+ */
+ public byte[] getDetectBuffer ()
+ throws IOException
+ {
+ PushbackInputStream p = (PushbackInputStream) in;
+ byte[] buffer = new byte[DETECT_BUFFER_SIZE];
+ int len = p.read (buffer);
+ if (len < 0)
+ {
+ return null;
+ }
+ else
+ {
+ p.unread (buffer, 0, len);
+ byte[] ret = new byte[len];
+ System.arraycopy (buffer, 0, ret, 0, len);
+ return ret;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/util/StandaloneDocumentType.java b/libjava/classpath/gnu/xml/libxmlj/util/StandaloneDocumentType.java
new file mode 100644
index 000000000..5c3316611
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/util/StandaloneDocumentType.java
@@ -0,0 +1,294 @@
+/* StandaloneDocumentType.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.util;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.UserDataHandler;
+
+/**
+ * A "standalone" document type, i.e. one that isn't attached to a document
+ * node.
+ * This can be used to create new documents.
+ */
+public final class StandaloneDocumentType
+implements DocumentType
+{
+
+ private final String name;
+ private final String publicId;
+ private final String systemId;
+
+ public StandaloneDocumentType (String name, String publicId, String systemId)
+ {
+ this.name = name;
+ this.publicId = publicId;
+ this.systemId = systemId;
+ }
+
+ public String getName ()
+ {
+ return name;
+ }
+
+ public NamedNodeMap getEntities ()
+ {
+ // TODO
+ return null;
+ }
+
+ public NamedNodeMap getNotations ()
+ {
+ // TODO
+ return null;
+ }
+
+ public String getPublicId ()
+ {
+ return publicId;
+ }
+
+ public String getSystemId ()
+ {
+ return systemId;
+ }
+
+ public String getInternalSubset ()
+ {
+ return null;
+ }
+
+ // -- Node --
+
+ public String getNodeName ()
+ {
+ return getName ();
+ }
+
+ public String getNodeValue ()
+ throws DOMException
+ {
+ return null;
+ }
+
+ public void setNodeValue (String nodeValue)
+ throws DOMException
+ {
+ }
+
+ public short getNodeType ()
+ {
+ return DOCUMENT_TYPE_NODE;
+ }
+
+ public Node getParentNode ()
+ {
+ return null;
+ }
+
+ public NodeList getChildNodes ()
+ {
+ return new EmptyNodeList ();
+ }
+
+ public Node getFirstChild ()
+ {
+ return null;
+ }
+
+ public Node getLastChild ()
+ {
+ return null;
+ }
+
+ public Node getPreviousSibling ()
+ {
+ return null;
+ }
+
+ public Node getNextSibling ()
+ {
+ return null;
+ }
+
+ public NamedNodeMap getAttributes ()
+ {
+ return null;
+ }
+
+ public Document getOwnerDocument ()
+ {
+ return null;
+ }
+
+ public Node insertBefore (Node newChild, Node refChild)
+ throws DOMException
+ {
+ throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
+ }
+
+ public Node replaceChild (Node newChild, Node oldChild)
+ throws DOMException
+ {
+ throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
+ }
+
+ public Node removeChild (Node oldChild)
+ throws DOMException
+ {
+ throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
+ }
+
+ public Node appendChild (Node oldChild)
+ throws DOMException
+ {
+ throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
+ }
+
+ public boolean hasChildNodes ()
+ {
+ return false;
+ }
+
+ public Node cloneNode (boolean deep)
+ {
+ return new StandaloneDocumentType (name, publicId, systemId);
+ }
+
+ public void normalize ()
+ {
+ }
+
+ public boolean isSupported (String feature, String version)
+ {
+ return false;
+ }
+
+ public String getNamespaceURI ()
+ {
+ return null;
+ }
+
+ public String getPrefix ()
+ {
+ return null;
+ }
+
+ public void setPrefix (String prefix)
+ {
+ throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
+ }
+
+ public String getLocalName ()
+ {
+ return getName ();
+ }
+
+ public boolean hasAttributes ()
+ {
+ return false;
+ }
+
+ // DOM Level 3
+
+ public String getBaseURI ()
+ {
+ return null;
+ }
+
+ public short compareDocumentPosition (Node node)
+ {
+ return -1;
+ }
+
+ public String getTextContent ()
+ {
+ return null;
+ }
+
+ public void setTextContent (String content)
+ {
+ throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, null);
+ }
+
+ public boolean isSameNode (Node other)
+ {
+ return equals (other);
+ }
+
+ public String lookupPrefix (String namespace)
+ {
+ return null;
+ }
+
+ public boolean isDefaultNamespace (String namespace)
+ {
+ return false;
+ }
+
+ public String lookupNamespaceURI (String prefix)
+ {
+ return null;
+ }
+
+ public boolean isEqualNode (Node other)
+ {
+ return equals (other);
+ }
+
+ public Object getFeature (String feature, String version)
+ {
+ return null;
+ }
+
+ public Object setUserData (String name, Object value,
+ UserDataHandler handler)
+ {
+ return null;
+ }
+
+ public Object getUserData (String name)
+ {
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/util/StandaloneLocator.java b/libjava/classpath/gnu/xml/libxmlj/util/StandaloneLocator.java
new file mode 100644
index 000000000..75b40bdf1
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/util/StandaloneLocator.java
@@ -0,0 +1,89 @@
+/* StandaloneLocator.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.util;
+
+import javax.xml.transform.SourceLocator;
+import org.xml.sax.Locator;
+
+/**
+ * SAX Locator implementation that uses the specified values.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class StandaloneLocator
+implements Locator, SourceLocator
+{
+
+ private final int lineNumber;
+
+ private final int columnNumber;
+
+ private final String publicId;
+
+ private final String systemId;
+
+ public StandaloneLocator (int lineNumber, int columnNumber,
+ String publicId, String systemId)
+ {
+ this.lineNumber = lineNumber;
+ this.columnNumber = columnNumber;
+ this.publicId = publicId;
+ this.systemId = systemId;
+ }
+
+ public String getPublicId ()
+ {
+ return publicId;
+ }
+
+ public String getSystemId ()
+ {
+ return systemId;
+ }
+
+ public int getLineNumber ()
+ {
+ return lineNumber;
+ }
+
+ public int getColumnNumber ()
+ {
+ return columnNumber;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/util/XMLJ.java b/libjava/classpath/gnu/xml/libxmlj/util/XMLJ.java
new file mode 100644
index 000000000..8d954d0be
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/util/XMLJ.java
@@ -0,0 +1,280 @@
+/* XMLJ.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.util;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.xml.sax.InputSource;
+
+import gnu.xml.libxmlj.transform.GnomeTransformerFactory;
+
+import gnu.xml.dom.ls.ReaderInputStream;
+import gnu.xml.dom.ls.WriterOutputStream;
+
+/**
+ * Utility functions for libxmlj.
+ */
+public final class XMLJ
+{
+
+ static class XMLJShutdownHook
+ implements Runnable
+ {
+
+ public void run ()
+ {
+ // Make sure finalizers are run
+ System.gc ();
+ Runtime.getRuntime ().runFinalization ();
+
+ // Perform global cleanup on the native level
+ GnomeTransformerFactory.freeLibxsltGlobal ();
+ }
+
+ }
+
+ private static boolean initialised = false;
+
+ public static void init ()
+ {
+ if (!initialised)
+ {
+ System.loadLibrary ("xmlj");
+
+ XMLJShutdownHook hook = new XMLJShutdownHook ();
+ Runtime.getRuntime ().addShutdownHook (new Thread (hook));
+ }
+ initialised = true;
+ }
+
+ private static final int LOOKAHEAD = 50;
+
+ /**
+ * Returns an input stream for the specified input source.
+ * This returns a pushback stream that libxmlj can use to detect the
+ * character encoding of the stream.
+ */
+ public static NamedInputStream getInputStream (InputSource input)
+ throws IOException
+ {
+ InputStream in = input.getByteStream ();
+ String systemId = input.getSystemId ();
+ if (in == null)
+ {
+ Reader r = input.getCharacterStream();
+ if (r != null)
+ in = new ReaderInputStream(r);
+ }
+ if (in == null)
+ {
+ in = getInputStream(systemId);
+ }
+ return new NamedInputStream (systemId, in, LOOKAHEAD);
+ }
+
+ /**
+ * Returns an input stream for the specified transformer source.
+ * This returns a pushback stream that libxmlj can use to detect the
+ * character encoding of the stream.
+ */
+ public static NamedInputStream getInputStream (Source source)
+ throws IOException
+ {
+ if (source instanceof SAXSource)
+ {
+ return getInputStream (((SAXSource) source).getInputSource ());
+ }
+ InputStream in = null;
+ String systemId = source.getSystemId ();
+ if (source instanceof StreamSource)
+ {
+ in = ((StreamSource) source).getInputStream ();
+ }
+ if (in == null)
+ {
+ in = getInputStream(systemId);
+ }
+ return new NamedInputStream (systemId, in, LOOKAHEAD);
+ }
+
+ private static InputStream getInputStream(String systemId)
+ throws IOException
+ {
+ if (systemId == null)
+ {
+ throw new IOException("no system ID");
+ }
+ try
+ {
+ return new URL(systemId).openStream();
+ }
+ catch (MalformedURLException e)
+ {
+ return new FileInputStream(systemId);
+ }
+ }
+
+ /**
+ * Returns an input stream for the specified URL.
+ * This returns a pushback stream that libxmlj can use to detect the
+ * character encoding of the stream.
+ */
+ public static NamedInputStream getInputStream (URL url)
+ throws IOException
+ {
+ return new NamedInputStream (url.toString (), url.openStream(),
+ LOOKAHEAD);
+ }
+
+ /**
+ * Convenience method for xmljDocLoader
+ */
+ static NamedInputStream xmljGetInputStream(String base, String url)
+ throws IOException
+ {
+ try
+ {
+ if (base != null)
+ {
+ url = new URL(new URL(base), url).toString();
+ }
+ }
+ catch (MalformedURLException e)
+ {
+ }
+ InputStream in = getInputStream(url);
+ return new NamedInputStream(url, in, LOOKAHEAD);
+ }
+
+ /**
+ * Returns an output stream for the specified transformer result.
+ */
+ public static OutputStream getOutputStream (Result result)
+ throws IOException
+ {
+ OutputStream out = null;
+ if (result instanceof StreamResult)
+ {
+ out = ((StreamResult) result).getOutputStream ();
+ }
+ if (out == null)
+ {
+ Writer w = ((StreamResult) result).getWriter ();
+ if (w != null)
+ out = new WriterOutputStream (w);
+ }
+ if (out == null)
+ {
+ String systemId = result.getSystemId ();
+ if (systemId == null)
+ {
+ throw new IOException ("no system ID");
+ }
+ try
+ {
+ URL url = new URL (systemId);
+ URLConnection connection = url.openConnection ();
+ connection.setDoOutput (true);
+ out = connection.getOutputStream ();
+ }
+ catch (MalformedURLException e)
+ {
+ out = new FileOutputStream (systemId);
+ }
+ }
+
+ return out;
+ }
+
+ /**
+ * Returns the absolute form of the specified URI.
+ * If the URI is already absolute, returns it as-is.
+ * Otherwise returns a new URI relative to the given base URI.
+ */
+ public static String getAbsoluteURI (String base, String uri)
+ {
+ if (uri != null &&
+ base != null &&
+ (uri.length() > 0) &&
+ (uri.indexOf(':') == -1) &&
+ (uri.charAt(0) != '/'))
+ {
+ // URI is relative
+ if (base.charAt(base.length() - 1) != '/')
+ {
+ int i = base.lastIndexOf('/');
+ base = base.substring(0, i + 1);
+ }
+ return base + uri;
+ }
+ else
+ {
+ // URI is absolute or no base specified
+ return uri;
+ }
+ }
+
+ public static String getBaseURI(String uri)
+ {
+ if (uri != null)
+ {
+ int si = uri.lastIndexOf('/');
+ if (si != -1)
+ {
+ uri = uri.substring(0, si + 1);
+ }
+ }
+ return uri;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/CallFilter.java b/libjava/classpath/gnu/xml/pipeline/CallFilter.java
new file mode 100644
index 000000000..2398b8685
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/CallFilter.java
@@ -0,0 +1,257 @@
+/* CallFilter.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.pipeline;
+
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.net.URL;
+import java.net.URLConnection;
+import java.io.Writer;
+
+import org.xml.sax.DTDHandler;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
+
+import gnu.xml.util.Resolver;
+import gnu.xml.util.XMLWriter;
+
+
+/**
+ * Input is sent as an XML request to given URI, and the output of this
+ * filter is the parsed response to that request.
+ * A connection is opened to the remote URI when the startDocument call is
+ * issued through this filter, and the request is finished when the
+ * endDocument call is issued. Events should be written quickly enough to
+ * prevent the remote HTTP server from aborting the connection due to
+ * inactivity; you may want to buffer text in an earlier pipeline stage.
+ * If your application requires validity checking of such
+ * outputs, have the output pipeline include a validation stage.
+ *
+ * <p>In effect, this makes a remote procedure call to the URI, with the
+ * request and response document syntax as chosen by the application.
+ * <em>Note that all the input events must be seen, and sent to the URI,
+ * before the first output event can be seen. </em> Clients are delayed
+ * at least by waiting for the server to respond, constraining concurrency.
+ * Services can thus be used to synchronize concurrent activities, and
+ * even to prioritize service among different clients.
+ *
+ * <p> You are advised to avoid restricting yourself to an "RPC" model
+ * for distributed computation. With a World Wide Web, network latencies
+ * and failures (e.g. non-availability)
+ * are significant; adopting a "procedure" model, rather than a workflow
+ * model where bulk requests are sent and worked on asynchronously, is not
+ * generally an optimal system-wide architecture. When the messages may
+ * need authentication, such as with an OpenPGP signature, or when server
+ * loads don't argue in favor of immediate responses, non-RPC models can
+ * be advantageous. (So-called "peer to peer" computing models are one
+ * additional type of model, though too often that term is applied to
+ * systems that still have a centralized control structure.)
+ *
+ * <p> <em>Be strict in what you send, liberal in what you accept,</em> as
+ * the Internet tradition goes. Strictly conformant data should never cause
+ * problems to its receiver; make your request pipeline be very strict, and
+ * don't compromise on that. Make your response pipeline strict as well,
+ * but be ready to tolerate specific mild, temporary, and well-documented
+ * variations from specific communications peers.
+ *
+ * @see XmlServlet
+ *
+ * @author David Brownell
+ */
+final public class CallFilter implements EventConsumer
+{
+ private Requestor req;
+ private EventConsumer next;
+ private URL target;
+ private URLConnection conn;
+ private ErrorHandler errHandler;
+
+
+ /**
+ * Initializes a call filter so that its inputs are sent to the
+ * specified URI, and its outputs are sent to the next consumer
+ * provided.
+ *
+ * @exception IOException if the URI isn't accepted as a URL
+ */
+ // constructor used by PipelineFactory
+ public CallFilter (String uri, EventConsumer next)
+ throws IOException
+ {
+ this.next = next;
+ req = new Requestor ();
+ setCallTarget (uri);
+ }
+
+ /**
+ * Assigns the URI of the call target to be used.
+ * Does not affect calls currently being made.
+ */
+ final public void setCallTarget (String uri)
+ throws IOException
+ {
+ target = new URL (uri);
+ }
+
+ /**
+ * Assigns the error handler to be used to present most fatal
+ * errors.
+ */
+ public void setErrorHandler (ErrorHandler handler)
+ {
+ req.setErrorHandler (handler);
+ }
+
+
+ /**
+ * Returns the call target's URI.
+ */
+ final public String getCallTarget ()
+ {
+ return target.toString ();
+ }
+
+ /** Returns the content handler currently in use. */
+ final public org.xml.sax.ContentHandler getContentHandler ()
+ {
+ return req;
+ }
+
+ /** Returns the DTD handler currently in use. */
+ final public DTDHandler getDTDHandler ()
+ {
+ return req;
+ }
+
+
+ /**
+ * Returns the declaration or lexical handler currently in
+ * use, or throws an exception for other properties.
+ */
+ final public Object getProperty (String id)
+ throws SAXNotRecognizedException
+ {
+ if (EventFilter.DECL_HANDLER.equals (id))
+ return req;
+ if (EventFilter.LEXICAL_HANDLER.equals (id))
+ return req;
+ throw new SAXNotRecognizedException (id);
+ }
+
+
+ // JDK 1.1 seems to need it to be done this way, sigh
+ ErrorHandler getErrorHandler () { return errHandler; }
+
+ //
+ // Takes input and echoes to server as POST input.
+ // Then sends the POST reply to the next pipeline element.
+ //
+ final class Requestor extends XMLWriter
+ {
+ Requestor ()
+ {
+ super ((Writer)null);
+ }
+
+ public synchronized void startDocument () throws SAXException
+ {
+ // Connect to remote object and set up to send it XML text
+ try {
+ if (conn != null)
+ throw new IllegalStateException ("call is being made");
+
+ conn = target.openConnection ();
+ conn.setDoOutput (true);
+ conn.setRequestProperty ("Content-Type",
+ "application/xml;charset=UTF-8");
+
+ setWriter (new OutputStreamWriter (
+ conn.getOutputStream (),
+ "UTF8"), "UTF-8");
+
+ } catch (IOException e) {
+ fatal ("can't write (POST) to URI: " + target, e);
+ }
+
+ // NOW base class can safely write that text!
+ super.startDocument ();
+ }
+
+ public void endDocument () throws SAXException
+ {
+ //
+ // Finish writing the request (for HTTP, a POST);
+ // this closes the output stream.
+ //
+ super.endDocument ();
+
+ //
+ // Receive the response.
+ // Produce events for the next stage.
+ //
+ InputSource source;
+ XMLReader producer;
+ String encoding;
+
+ try {
+
+ source = new InputSource (conn.getInputStream ());
+
+// FIXME if status is anything but success, report it!! It'd be good to
+// save the request data just in case we need to deal with a forward.
+
+ encoding = Resolver.getEncoding (conn.getContentType ());
+ if (encoding != null)
+ source.setEncoding (encoding);
+
+ producer = XMLReaderFactory.createXMLReader ();
+ producer.setErrorHandler (getErrorHandler ());
+ EventFilter.bind (producer, next);
+ producer.parse (source);
+ conn = null;
+
+ } catch (IOException e) {
+ fatal ("I/O Exception reading response, " + e.getMessage (), e);
+ }
+ }
+ }
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/DomConsumer.java b/libjava/classpath/gnu/xml/pipeline/DomConsumer.java
new file mode 100644
index 000000000..141f36eca
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/DomConsumer.java
@@ -0,0 +1,967 @@
+/* DomConsumer.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.pipeline;
+
+import gnu.xml.util.DomParser;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.DTDHandler;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.ext.DeclHandler;
+import org.xml.sax.ext.LexicalHandler;
+import org.xml.sax.helpers.AttributesImpl;
+import org.w3c.dom.Attr;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.CharacterData;
+import org.w3c.dom.Document;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.Element;
+import org.w3c.dom.EntityReference;
+import org.w3c.dom.Node;
+import org.w3c.dom.ProcessingInstruction;
+import org.w3c.dom.Text;
+
+/**
+ * This consumer builds a DOM Document from its input, acting either as a
+ * pipeline terminus or as an intermediate buffer. When a document's worth
+ * of events has been delivered to this consumer, that document is read with
+ * a {@link DomParser} and sent to the next consumer. It is also available
+ * as a read-once property.
+ *
+ * <p>The DOM tree is constructed as faithfully as possible. There are some
+ * complications since a DOM should expose behaviors that can't be implemented
+ * without API backdoors into that DOM, and because some SAX parsers don't
+ * report all the information that DOM permits to be exposed. The general
+ * problem areas involve information from the Document Type Declaration (DTD).
+ * DOM only represents a limited subset, but has some behaviors that depend
+ * on much deeper knowledge of a document's DTD. You shouldn't have much to
+ * worry about unless you change handling of "noise" nodes from its default
+ * setting (which ignores them all); note if you use JAXP to populate your
+ * DOM trees, it wants to save "noise" nodes by default. (Such nodes include
+ * ignorable whitespace, comments, entity references and CDATA boundaries.)
+ * Otherwise, your
+ * main worry will be if you use a SAX parser that doesn't flag ignorable
+ * whitespace unless it's validating (few don't).
+ *
+ * <p> The SAX2 events used as input must contain XML Names for elements
+ * and attributes, with original prefixes. In SAX2,
+ * this is optional unless the "namespace-prefixes" parser feature is set.
+ * Moreover, many application components won't provide completely correct
+ * structures anyway. <em>Before you convert a DOM to an output document,
+ * you should plan to postprocess it to create or repair such namespace
+ * information.</em> The {@link NSFilter} pipeline stage does such work.
+ *
+ * <p> <em>Note: changes late in DOM L2 process made it impractical to
+ * attempt to create the DocumentType node in any implementation-neutral way,
+ * much less to populate it (L1 didn't support even creating such nodes).
+ * To create and populate such a node, subclass the inner
+ * {@link DomConsumer.Handler} class and teach it about the backdoors into
+ * whatever DOM implementation you want. It's possible that some revised
+ * DOM API (L3?) will make this problem solvable again. </em>
+ *
+ * @see DomParser
+ *
+ * @author David Brownell
+ */
+public class DomConsumer implements EventConsumer
+{
+ private Class domImpl;
+
+ private boolean hidingCDATA = true;
+ private boolean hidingComments = true;
+ private boolean hidingWhitespace = true;
+ private boolean hidingReferences = true;
+
+ private Handler handler;
+ private ErrorHandler errHandler;
+
+ private EventConsumer next;
+
+ // FIXME: this can't be a generic pipeline stage just now,
+ // since its input became a Class not a String (to be turned
+ // into a class, using the right class loader)
+
+
+ /**
+ * Configures this pipeline terminus to use the specified implementation
+ * of DOM when constructing its result value.
+ *
+ * @param impl class implementing {@link org.w3c.dom.Document Document}
+ * which publicly exposes a default constructor
+ *
+ * @exception SAXException when there is a problem creating an
+ * empty DOM document using the specified implementation
+ */
+ public DomConsumer (Class impl)
+ throws SAXException
+ {
+ domImpl = impl;
+ handler = new Handler (this);
+ }
+
+ /**
+ * This is the hook through which a subclass provides a handler
+ * which knows how to access DOM extensions, specific to some
+ * implementation, to record additional data in a DOM.
+ * Treat this as part of construction; don't call it except
+ * before (or between) parses.
+ */
+ protected void setHandler (Handler h)
+ {
+ handler = h;
+ }
+
+
+ private Document emptyDocument ()
+ throws SAXException
+ {
+ try {
+ return (Document) domImpl.newInstance ();
+ } catch (IllegalAccessException e) {
+ throw new SAXException ("can't access constructor: "
+ + e.getMessage ());
+ } catch (InstantiationException e) {
+ throw new SAXException ("can't instantiate Document: "
+ + e.getMessage ());
+ }
+ }
+
+
+ /**
+ * Configures this consumer as a buffer/filter, using the specified
+ * DOM implementation when constructing its result value.
+ *
+ * <p> This event consumer acts as a buffer and filter, in that it
+ * builds a DOM tree and then writes it out when <em>endDocument</em>
+ * is invoked. Because of the limitations of DOM, much information
+ * will as a rule not be seen in that replay. To get a full fidelity
+ * copy of the input event stream, use a {@link TeeConsumer}.
+ *
+ * @param impl class implementing {@link org.w3c.dom.Document Document}
+ * which publicly exposes a default constructor
+ * @param next receives a "replayed" sequence of parse events when
+ * the <em>endDocument</em> method is invoked.
+ *
+ * @exception SAXException when there is a problem creating an
+ * empty DOM document using the specified DOM implementation
+ */
+ public DomConsumer (Class impl, EventConsumer n)
+ throws SAXException
+ {
+ this (impl);
+ next = n;
+ }
+
+
+ /**
+ * Returns the document constructed from the preceding
+ * sequence of events. This method should not be
+ * used again until another sequence of events has been
+ * given to this EventConsumer.
+ */
+ final public Document getDocument ()
+ {
+ return handler.clearDocument ();
+ }
+
+ public void setErrorHandler (ErrorHandler handler)
+ {
+ errHandler = handler;
+ }
+
+
+ /**
+ * Returns true if the consumer is hiding entity references nodes
+ * (the default), and false if EntityReference nodes should
+ * instead be created. Such EntityReference nodes will normally be
+ * empty, unless an implementation arranges to populate them and then
+ * turn them back into readonly objects.
+ *
+ * @see #setHidingReferences
+ */
+ final public boolean isHidingReferences ()
+ { return hidingReferences; }
+
+ /**
+ * Controls whether the consumer will hide entity expansions,
+ * or will instead mark them with entity reference nodes.
+ *
+ * @see #isHidingReferences
+ * @param flag False if entity reference nodes will appear
+ */
+ final public void setHidingReferences (boolean flag)
+ { hidingReferences = flag; }
+
+
+ /**
+ * Returns true if the consumer is hiding comments (the default),
+ * and false if they should be placed into the output document.
+ *
+ * @see #setHidingComments
+ */
+ public final boolean isHidingComments ()
+ { return hidingComments; }
+
+ /**
+ * Controls whether the consumer is hiding comments.
+ *
+ * @see #isHidingComments
+ */
+ public final void setHidingComments (boolean flag)
+ { hidingComments = flag; }
+
+
+ /**
+ * Returns true if the consumer is hiding ignorable whitespace
+ * (the default), and false if such whitespace should be placed
+ * into the output document as children of element nodes.
+ *
+ * @see #setHidingWhitespace
+ */
+ public final boolean isHidingWhitespace ()
+ { return hidingWhitespace; }
+
+ /**
+ * Controls whether the consumer hides ignorable whitespace
+ *
+ * @see #isHidingComments
+ */
+ public final void setHidingWhitespace (boolean flag)
+ { hidingWhitespace = flag; }
+
+
+ /**
+ * Returns true if the consumer is saving CDATA boundaries, or
+ * false (the default) otherwise.
+ *
+ * @see #setHidingCDATA
+ */
+ final public boolean isHidingCDATA ()
+ { return hidingCDATA; }
+
+ /**
+ * Controls whether the consumer will save CDATA boundaries.
+ *
+ * @see #isHidingCDATA
+ * @param flag True to treat CDATA text differently from other
+ * text nodes
+ */
+ final public void setHidingCDATA (boolean flag)
+ { hidingCDATA = flag; }
+
+
+
+ /** Returns the document handler being used. */
+ final public ContentHandler getContentHandler ()
+ { return handler; }
+
+ /** Returns the DTD handler being used. */
+ final public DTDHandler getDTDHandler ()
+ { return handler; }
+
+ /**
+ * Returns the lexical handler being used.
+ * (DOM construction can't really use declaration handlers.)
+ */
+ final public Object getProperty (String id)
+ throws SAXNotRecognizedException
+ {
+ if ("http://xml.org/sax/properties/lexical-handler".equals (id))
+ return handler;
+ if ("http://xml.org/sax/properties/declaration-handler".equals (id))
+ return handler;
+ throw new SAXNotRecognizedException (id);
+ }
+
+ EventConsumer getNext () { return next; }
+
+ ErrorHandler getErrorHandler () { return errHandler; }
+
+ /**
+ * Class used to intercept various parsing events and use them to
+ * populate a DOM document. Subclasses would typically know and use
+ * backdoors into specific DOM implementations, used to implement
+ * DTD-related functionality.
+ *
+ * <p> Note that if this ever throws a DOMException (runtime exception)
+ * that will indicate a bug in the DOM (e.g. doesn't support something
+ * per specification) or the parser (e.g. emitted an illegal name, or
+ * accepted illegal input data). </p>
+ */
+ public static class Handler
+ implements ContentHandler, LexicalHandler,
+ DTDHandler, DeclHandler
+ {
+ protected DomConsumer consumer;
+
+ private DOMImplementation impl;
+ private Document document;
+ private boolean isL2;
+
+ private Locator locator;
+ private Node top;
+ private boolean inCDATA;
+ private boolean mergeCDATA;
+ private boolean inDTD;
+ private String currentEntity;
+
+ private boolean recreatedAttrs;
+ private AttributesImpl attributes = new AttributesImpl ();
+
+ /**
+ * Subclasses may use SAX2 events to provide additional
+ * behaviors in the resulting DOM.
+ */
+ protected Handler (DomConsumer consumer)
+ throws SAXException
+ {
+ this.consumer = consumer;
+ document = consumer.emptyDocument ();
+ impl = document.getImplementation ();
+ isL2 = impl.hasFeature ("XML", "2.0");
+ }
+
+ private void fatal (String message, Exception x)
+ throws SAXException
+ {
+ SAXParseException e;
+ ErrorHandler errHandler = consumer.getErrorHandler ();
+
+ if (locator == null)
+ e = new SAXParseException (message, null, null, -1, -1, x);
+ else
+ e = new SAXParseException (message, locator, x);
+ if (errHandler != null)
+ errHandler.fatalError (e);
+ throw e;
+ }
+
+ /**
+ * Returns and forgets the document produced. If the handler is
+ * reused, a new document may be created.
+ */
+ Document clearDocument ()
+ {
+ Document retval = document;
+ document = null;
+ locator = null;
+ return retval;
+ }
+
+ /**
+ * Returns the document under construction.
+ */
+ protected Document getDocument ()
+ { return document; }
+
+ /**
+ * Returns the current node being populated. This is usually
+ * an Element or Document, but it might be an EntityReference
+ * node if some implementation-specific code knows how to put
+ * those into the result tree and later mark them as readonly.
+ */
+ protected Node getTop ()
+ { return top; }
+
+
+ // SAX1
+ public void setDocumentLocator (Locator locator)
+ {
+ this.locator = locator;
+ }
+
+ // SAX1
+ public void startDocument ()
+ throws SAXException
+ {
+ if (document == null)
+ try {
+ if (isL2) {
+ // couple to original implementation
+ document = impl.createDocument (null, "foo", null);
+ document.removeChild (document.getFirstChild ());
+ } else {
+ document = consumer.emptyDocument ();
+ }
+ } catch (Exception e) {
+ fatal ("DOM create document", e);
+ }
+ top = document;
+ }
+
+ // SAX1
+ public void endDocument ()
+ throws SAXException
+ {
+ try {
+ if (consumer.getNext () != null && document != null) {
+ DomParser parser = new DomParser (document);
+
+ EventFilter.bind (parser, consumer.getNext ());
+ parser.parse ("ignored");
+ }
+ } finally {
+ top = null;
+ }
+ }
+
+ // SAX1
+ public void processingInstruction (String target, String data)
+ throws SAXException
+ {
+ // we can't create populated entity ref nodes using
+ // only public DOM APIs (they've got to be readonly)
+ if (currentEntity != null)
+ return;
+
+ ProcessingInstruction pi;
+
+ if (isL2
+ // && consumer.isUsingNamespaces ()
+ && target.indexOf (':') != -1)
+ namespaceError (
+ "PI target name is namespace nonconformant: "
+ + target);
+ if (inDTD)
+ return;
+ pi = document.createProcessingInstruction (target, data);
+ top.appendChild (pi);
+ }
+
+ /**
+ * Subclasses may overrride this method to provide a more efficient
+ * way to construct text nodes.
+ * Typically, copying the text into a single character array will
+ * be more efficient than doing that as well as allocating other
+ * needed for a String, including an internal StringBuffer.
+ * Those additional memory and CPU costs can be incurred later,
+ * if ever needed.
+ * Unfortunately the standard DOM factory APIs encourage those costs
+ * to be incurred early.
+ */
+ protected Text createText (
+ boolean isCDATA,
+ char ch [],
+ int start,
+ int length
+ ) {
+ String value = new String (ch, start, length);
+
+ if (isCDATA)
+ return document.createCDATASection (value);
+ else
+ return document.createTextNode (value);
+ }
+
+ // SAX1
+ public void characters (char ch [], int start, int length)
+ throws SAXException
+ {
+ // we can't create populated entity ref nodes using
+ // only public DOM APIs (they've got to be readonly
+ // at creation time)
+ if (currentEntity != null)
+ return;
+
+ Node lastChild = top.getLastChild ();
+
+ // merge consecutive text or CDATA nodes if appropriate.
+ if (lastChild instanceof Text) {
+ if (consumer.isHidingCDATA ()
+ // consecutive Text content ... always merge
+ || (!inCDATA
+ && !(lastChild instanceof CDATASection))
+ // consecutive CDATASection content ... don't
+ // merge between sections, only within them
+ || (inCDATA && mergeCDATA
+ && lastChild instanceof CDATASection)
+ ) {
+ CharacterData last = (CharacterData) lastChild;
+ String value = new String (ch, start, length);
+
+ last.appendData (value);
+ return;
+ }
+ }
+ if (inCDATA && !consumer.isHidingCDATA ()) {
+ top.appendChild (createText (true, ch, start, length));
+ mergeCDATA = true;
+ } else
+ top.appendChild (createText (false, ch, start, length));
+ }
+
+ // SAX2
+ public void skippedEntity (String name)
+ throws SAXException
+ {
+ // this callback is useless except to report errors, since
+ // we can't know if the ref was in content, within an
+ // attribute, within a declaration ... only one of those
+ // cases supports more intelligent action than a panic.
+ fatal ("skipped entity: " + name, null);
+ }
+
+ // SAX2
+ public void startPrefixMapping (String prefix, String uri)
+ throws SAXException
+ {
+ // reconstruct "xmlns" attributes deleted by all
+ // SAX2 parsers without "namespace-prefixes" = true
+ if ("".equals (prefix))
+ attributes.addAttribute ("", "", "xmlns",
+ "CDATA", uri);
+ else
+ attributes.addAttribute ("", "", "xmlns:" + prefix,
+ "CDATA", uri);
+ recreatedAttrs = true;
+ }
+
+ // SAX2
+ public void endPrefixMapping (String prefix)
+ throws SAXException
+ { }
+
+ // SAX2
+ public void startElement (
+ String uri,
+ String localName,
+ String qName,
+ Attributes atts
+ ) throws SAXException
+ {
+ // we can't create populated entity ref nodes using
+ // only public DOM APIs (they've got to be readonly)
+ if (currentEntity != null)
+ return;
+
+ // parser discarded basic information; DOM tree isn't writable
+ // without massaging to assign prefixes to all nodes.
+ // the "NSFilter" class does that massaging.
+ if (qName.length () == 0)
+ qName = localName;
+
+
+ Element element;
+ int length = atts.getLength ();
+
+ if (!isL2) {
+ element = document.createElement (qName);
+
+ // first the explicit attributes ...
+ length = atts.getLength ();
+ for (int i = 0; i < length; i++)
+ element.setAttribute (atts.getQName (i),
+ atts.getValue (i));
+ // ... then any recreated ones (DOM deletes duplicates)
+ if (recreatedAttrs) {
+ recreatedAttrs = false;
+ length = attributes.getLength ();
+ for (int i = 0; i < length; i++)
+ element.setAttribute (attributes.getQName (i),
+ attributes.getValue (i));
+ attributes.clear ();
+ }
+
+ top.appendChild (element);
+ top = element;
+ return;
+ }
+
+ // For an L2 DOM when namespace use is enabled, use
+ // createElementNS/createAttributeNS except when
+ // (a) it's an element in the default namespace, or
+ // (b) it's an attribute with no prefix
+ String namespace;
+
+ if (localName.length () != 0)
+ namespace = (uri.length () == 0) ? null : uri;
+ else
+ namespace = getNamespace (getPrefix (qName), atts);
+
+ if (namespace == null)
+ element = document.createElement (qName);
+ else
+ element = document.createElementNS (namespace, qName);
+
+ populateAttributes (element, atts);
+ if (recreatedAttrs) {
+ recreatedAttrs = false;
+ // ... DOM deletes any duplicates
+ populateAttributes (element, attributes);
+ attributes.clear ();
+ }
+
+ top.appendChild (element);
+ top = element;
+ }
+
+ final static String xmlnsURI = "http://www.w3.org/2000/xmlns/";
+
+ private void populateAttributes (Element element, Attributes attrs)
+ throws SAXParseException
+ {
+ int length = attrs.getLength ();
+
+ for (int i = 0; i < length; i++) {
+ String type = attrs.getType (i);
+ String value = attrs.getValue (i);
+ String name = attrs.getQName (i);
+ String local = attrs.getLocalName (i);
+ String uri = attrs.getURI (i);
+
+ // parser discarded basic information, DOM tree isn't writable
+ if (name.length () == 0)
+ name = local;
+
+ // all attribute types other than these three may not
+ // contain scoped names... enumerated attributes get
+ // reported as NMTOKEN, except for NOTATION values
+ if (!("CDATA".equals (type)
+ || "NMTOKEN".equals (type)
+ || "NMTOKENS".equals (type))) {
+ if (value.indexOf (':') != -1) {
+ namespaceError (
+ "namespace nonconformant attribute value: "
+ + "<" + element.getNodeName ()
+ + " " + name + "='" + value + "' ...>");
+ }
+ }
+
+ // xmlns="" is legal (undoes default NS)
+ // xmlns:foo="" is illegal
+ String prefix = getPrefix (name);
+ String namespace;
+
+ if ("xmlns".equals (prefix)) {
+ if ("".equals (value))
+ namespaceError ("illegal null namespace decl, " + name);
+ namespace = xmlnsURI;
+ } else if ("xmlns".equals (name))
+ namespace = xmlnsURI;
+
+ else if (prefix == null)
+ namespace = null;
+ else if (!"".equals(uri) && uri.length () != 0)
+ namespace = uri;
+ else
+ namespace = getNamespace (prefix, attrs);
+
+ if (namespace == null)
+ element.setAttribute (name, value);
+ else
+ element.setAttributeNS (namespace, name, value);
+ }
+ }
+
+ private String getPrefix (String name)
+ {
+ int temp;
+
+ if ((temp = name.indexOf (':')) > 0)
+ return name.substring (0, temp);
+ return null;
+ }
+
+ // used with SAX1-level parser output
+ private String getNamespace (String prefix, Attributes attrs)
+ throws SAXParseException
+ {
+ String namespace;
+ String decl;
+
+ // defaulting
+ if (prefix == null) {
+ decl = "xmlns";
+ namespace = attrs.getValue (decl);
+ if ("".equals (namespace))
+ return null;
+ else if (namespace != null)
+ return namespace;
+
+ // "xmlns" is like a keyword
+ // ... according to the Namespace REC, but DOM L2 CR2+
+ // and Infoset violate that by assigning a namespace.
+ // that conflict is resolved elsewhere.
+ } else if ("xmlns".equals (prefix))
+ return null;
+
+ // "xml" prefix is fixed
+ else if ("xml".equals (prefix))
+ return "http://www.w3.org/XML/1998/namespace";
+
+ // otherwise, expect a declaration
+ else {
+ decl = "xmlns:" + prefix;
+ namespace = attrs.getValue (decl);
+ }
+
+ // if we found a local declaration, great
+ if (namespace != null)
+ return namespace;
+
+
+ // ELSE ... search up the tree we've been building
+ for (Node n = top;
+ n != null && n.getNodeType () != Node.DOCUMENT_NODE;
+ n = n.getParentNode ()) {
+ if (n.getNodeType () == Node.ENTITY_REFERENCE_NODE)
+ continue;
+ Element e = (Element) n;
+ Attr attr = e.getAttributeNode (decl);
+ if (attr != null)
+ return attr.getNodeValue ();
+ }
+ // see above re "xmlns" as keyword
+ if ("xmlns".equals (decl))
+ return null;
+
+ namespaceError ("Undeclared namespace prefix: " + prefix);
+ return null;
+ }
+
+ // SAX2
+ public void endElement (String uri, String localName, String qName)
+ throws SAXException
+ {
+ // we can't create populated entity ref nodes using
+ // only public DOM APIs (they've got to be readonly)
+ if (currentEntity != null)
+ return;
+
+ top = top.getParentNode ();
+ }
+
+ // SAX1 (mandatory reporting if validating)
+ public void ignorableWhitespace (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (consumer.isHidingWhitespace ())
+ return;
+ characters (ch, start, length);
+ }
+
+ // SAX2 lexical event
+ public void startCDATA ()
+ throws SAXException
+ {
+ inCDATA = true;
+ // true except for the first fragment of a cdata section
+ mergeCDATA = false;
+ }
+
+ // SAX2 lexical event
+ public void endCDATA ()
+ throws SAXException
+ {
+ inCDATA = false;
+ }
+
+ // SAX2 lexical event
+ //
+ // this SAX2 callback merges two unrelated things:
+ // - Declaration of the root element type ... belongs with
+ // the other DTD declaration methods, NOT HERE.
+ // - IDs for the optional external subset ... belongs here
+ // with other lexical information.
+ //
+ // ...and it doesn't include the internal DTD subset, desired
+ // both to support DOM L2 and to enable "pass through" processing
+ //
+ public void startDTD (String name, String publicId, String SystemId)
+ throws SAXException
+ {
+ // need to filter out comments and PIs within the DTD
+ inDTD = true;
+ }
+
+ // SAX2 lexical event
+ public void endDTD ()
+ throws SAXException
+ {
+ inDTD = false;
+ }
+
+ // SAX2 lexical event
+ public void comment (char ch [], int start, int length)
+ throws SAXException
+ {
+ Node comment;
+
+ // we can't create populated entity ref nodes using
+ // only public DOM APIs (they've got to be readonly)
+ if (consumer.isHidingComments ()
+ || inDTD
+ || currentEntity != null)
+ return;
+ comment = document.createComment (new String (ch, start, length));
+ top.appendChild (comment);
+ }
+
+ /**
+ * May be overridden by subclasses to return true, indicating
+ * that entity reference nodes can be populated and then made
+ * read-only.
+ */
+ public boolean canPopulateEntityRefs ()
+ { return false; }
+
+ // SAX2 lexical event
+ public void startEntity (String name)
+ throws SAXException
+ {
+ // are we ignoring what would be contents of an
+ // entity ref, since we can't populate it?
+ if (currentEntity != null)
+ return;
+
+ // Are we hiding all entity boundaries?
+ if (consumer.isHidingReferences ())
+ return;
+
+ // SAX2 shows parameter entities; DOM hides them
+ if (name.charAt (0) == '%' || "[dtd]".equals (name))
+ return;
+
+ // Since we can't create a populated entity ref node in any
+ // standard way, we create an unpopulated one.
+ EntityReference ref = document.createEntityReference (name);
+ top.appendChild (ref);
+ top = ref;
+
+ // ... allowing subclasses to populate them
+ if (!canPopulateEntityRefs ())
+ currentEntity = name;
+ }
+
+ // SAX2 lexical event
+ public void endEntity (String name)
+ throws SAXException
+ {
+ if (name.charAt (0) == '%' || "[dtd]".equals (name))
+ return;
+ if (name.equals (currentEntity))
+ currentEntity = null;
+ if (!consumer.isHidingReferences ())
+ top = top.getParentNode ();
+ }
+
+
+ // SAX1 DTD event
+ public void notationDecl (
+ String name,
+ String publicId, String SystemId
+ ) throws SAXException
+ {
+ /* IGNORE -- no public DOM API lets us store these
+ * into the doctype node
+ */
+ }
+
+ // SAX1 DTD event
+ public void unparsedEntityDecl (
+ String name,
+ String publicId, String SystemId,
+ String notationName
+ ) throws SAXException
+ {
+ /* IGNORE -- no public DOM API lets us store these
+ * into the doctype node
+ */
+ }
+
+ // SAX2 declaration event
+ public void elementDecl (String name, String model)
+ throws SAXException
+ {
+ /* IGNORE -- no content model support in DOM L2 */
+ }
+
+ // SAX2 declaration event
+ public void attributeDecl (
+ String eName,
+ String aName,
+ String type,
+ String mode,
+ String value
+ ) throws SAXException
+ {
+ /* IGNORE -- no attribute model support in DOM L2 */
+ }
+
+ // SAX2 declaration event
+ public void internalEntityDecl (String name, String value)
+ throws SAXException
+ {
+ /* IGNORE -- no public DOM API lets us store these
+ * into the doctype node
+ */
+ }
+
+ // SAX2 declaration event
+ public void externalEntityDecl (
+ String name,
+ String publicId,
+ String SystemId
+ ) throws SAXException
+ {
+ /* IGNORE -- no public DOM API lets us store these
+ * into the doctype node
+ */
+ }
+
+ //
+ // These really should offer the option of nonfatal handling,
+ // like other validity errors, though that would cause major
+ // chaos in the DOM data structures. DOM is already spec'd
+ // to treat many of these as fatal, so this is consistent.
+ //
+ private void namespaceError (String description)
+ throws SAXParseException
+ {
+ SAXParseException err;
+
+ err = new SAXParseException (description, locator);
+ throw err;
+ }
+ }
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/EventConsumer.java b/libjava/classpath/gnu/xml/pipeline/EventConsumer.java
new file mode 100644
index 000000000..a0a8824f7
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/EventConsumer.java
@@ -0,0 +1,95 @@
+/* EventConsumer.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.pipeline;
+
+import org.xml.sax.*;
+
+
+/**
+ * Collects the event consumption apparatus of a SAX pipeline stage.
+ * Consumers which permit some handlers or other characteristics to be
+ * configured will provide methods to support that configuration.
+ *
+ * <p> Two important categories of consumers include <em>filters</em>, which
+ * process events and pass them on to other consumers, and <em>terminus</em>
+ * (or <em>terminal</em>) stages, which don't pass events on. Filters are not
+ * necessarily derived from the {@link EventFilter} class, although that
+ * class can substantially simplify their construction by automating the
+ * most common activities.
+ *
+ * <p> Event consumers which follow certain conventions for the signatures
+ * of their constructors can be automatically assembled into pipelines
+ * by the {@link PipelineFactory} class.
+ *
+ * @author David Brownell
+ */
+public interface EventConsumer
+{
+ /** Most stages process these core SAX callbacks. */
+ public ContentHandler getContentHandler ();
+
+ /** Few stages will use unparsed entities. */
+ public DTDHandler getDTDHandler ();
+
+ /**
+ * This method works like the SAX2 XMLReader method of the same name,
+ * and is used to retrieve the optional lexical and declaration handlers
+ * in a pipeline.
+ *
+ * @param id This is a URI identifying the type of property desired.
+ * @return The value of that property, if it is defined.
+ *
+ * @exception SAXNotRecognizedException Thrown if the particular
+ * pipeline stage does not understand the specified identifier.
+ */
+ public Object getProperty (String id)
+ throws SAXNotRecognizedException;
+
+ /**
+ * This method provides a filter stage with a handler that abstracts
+ * presentation of warnings and both recoverable and fatal errors.
+ * Most pipeline stages should share a single policy and mechanism
+ * for such reports, since application components require consistency
+ * in such activities. Accordingly, typical responses to this method
+ * invocation involve saving the handler for use; filters will pass
+ * it on to any other consumers they use.
+ *
+ * @param handler encapsulates error handling policy for this stage
+ */
+ public void setErrorHandler (ErrorHandler handler);
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/EventFilter.java b/libjava/classpath/gnu/xml/pipeline/EventFilter.java
new file mode 100644
index 000000000..b3cc2d654
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/EventFilter.java
@@ -0,0 +1,796 @@
+/* EventFilter.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.pipeline;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.xml.sax.*;
+import org.xml.sax.ext.*;
+import org.xml.sax.helpers.XMLFilterImpl;
+
+/**
+ * A customizable event consumer, used to assemble various kinds of filters
+ * using SAX handlers and an optional second consumer. It can be constructed
+ * in two ways: <ul>
+ *
+ * <li> To serve as a passthrough, sending all events to a second consumer.
+ * The second consumer may be identified through {@link #getNext}.
+ *
+ * <li> To serve as a dead end, with all handlers null;
+ * {@link #getNext} returns null.
+ *
+ * </ul>
+ *
+ * <p> Additionally, SAX handlers may be assigned, which completely replace
+ * the "upstream" view (through {@link EventConsumer}) of handlers, initially
+ * null or the "next" consumer provided to the constructor. To make
+ * it easier to build specialized filter classes, this class implements
+ * all the standard SAX consumer handlers, and those implementations
+ * delegate "downstream" to the consumer accessed by {@link #getNext}.
+ *
+ * <p> The simplest way to create a custom a filter class is to create a
+ * subclass which overrides one or more handler interface methods. The
+ * constructor for that subclass then registers itself as a handler for
+ * those interfaces using a call such as <em>setContentHandler(this)</em>,
+ * so the "upstream" view of event delivery is modified from the state
+ * established in the base class constructor. That way,
+ * the overridden methods intercept those event callbacks
+ * as they go "downstream", and
+ * all other event callbacks will pass events to any next consumer.
+ * Overridden methods may invoke superclass methods (perhaps after modifying
+ * parameters) if they wish to delegate such calls. Such subclasses
+ * should use {@link #getErrorHandler} to report errors using the
+ * common error reporting mechanism.
+ *
+ * <p> Another important technique is to construct a filter consisting
+ * of only a few specific types of handler. For example, one could easily
+ * prune out lexical events or various declarations by providing handlers
+ * which don't pass those events downstream, or by providing null handlers.
+ *
+ * <hr />
+ *
+ * <p> This may be viewed as the consumer oriented analogue of the SAX2
+ * {@link org.xml.sax.helpers.XMLFilterImpl XMLFilterImpl} class.
+ * Key differences include: <ul>
+ *
+ * <li> This fully separates consumer and producer roles: it
+ * does not implement the producer side <em>XMLReader</em> or
+ * <em>EntityResolver</em> interfaces, so it can only be used
+ * in "push" mode (it has no <em>parse()</em> methods).
+ *
+ * <li> "Extension" handlers are fully supported, enabling a
+ * richer set of application requirements.
+ * And it implements {@link EventConsumer}, which groups related
+ * consumer methods together, rather than leaving them separated.
+ *
+ * <li> The chaining which is visible is "downstream" to the next
+ * consumer, not "upstream" to the preceding producer.
+ * It supports "fan-in", where
+ * a consumer can be fed by several producers. (For "fan-out",
+ * see the {@link TeeConsumer} class.)
+ *
+ * <li> Event chaining is set up differently. It is intended to
+ * work "upstream" from terminus towards producer, during filter
+ * construction, as described above.
+ * This is part of an early binding model:
+ * events don't need to pass through stages which ignore them.
+ *
+ * <li> ErrorHandler support is separated, on the grounds that
+ * pipeline stages need to share the same error handling policy.
+ * For the same reason, error handler setup goes "downstream":
+ * when error handlers get set, they are passed to subsequent
+ * consumers.
+ *
+ * </ul>
+ *
+ * <p> The {@link #chainTo chainTo()} convenience routine supports chaining to
+ * an XMLFilterImpl, in its role as a limited functionality event
+ * consumer. Its event producer role ({@link XMLFilter}) is ignored.
+ *
+ * <hr />
+ *
+ * <p> The {@link #bind bind()} routine may be used associate event pipelines
+ * with any kind of {@link XMLReader} that will produce the events.
+ * Such pipelines don't necessarily need to have any members which are
+ * implemented using this class. That routine has some intelligence
+ * which supports automatic changes to parser feature flags, letting
+ * event piplines become largely independent of the particular feature
+ * sets of parsers.
+ *
+ * @author David Brownell
+ */
+public class EventFilter
+ implements EventConsumer, ContentHandler, DTDHandler,
+ LexicalHandler, DeclHandler
+{
+ // SAX handlers
+ private ContentHandler docHandler, docNext;
+ private DTDHandler dtdHandler, dtdNext;
+ private LexicalHandler lexHandler, lexNext;
+ private DeclHandler declHandler, declNext;
+ // and ideally, one more for the stuff SAX2 doesn't show
+
+ private Locator locator;
+ private EventConsumer next;
+ private ErrorHandler errHandler;
+
+
+ /** SAX2 URI prefix for standard feature flags. */
+ public static final String FEATURE_URI
+ = "http://xml.org/sax/features/";
+ /** SAX2 URI prefix for standard properties (mostly for handlers). */
+ public static final String PROPERTY_URI
+ = "http://xml.org/sax/properties/";
+
+ /** SAX2 property identifier for {@link DeclHandler} events */
+ public static final String DECL_HANDLER
+ = PROPERTY_URI + "declaration-handler";
+ /** SAX2 property identifier for {@link LexicalHandler} events */
+ public static final String LEXICAL_HANDLER
+ = PROPERTY_URI + "lexical-handler";
+
+ //
+ // These class objects will be null if the relevant class isn't linked.
+ // Small configurations (pJava and some kinds of embedded systems) need
+ // to facilitate smaller executables. So "instanceof" is undesirable
+ // when bind() sees if it can remove some stages.
+ //
+ // SECURITY NOTE: assuming all these classes are part of the same sealed
+ // package, there's no problem saving these in the instance of this class
+ // that's associated with "this" class loader. But that wouldn't be true
+ // for classes in another package.
+ //
+ private static boolean loaded;
+ private static Class nsClass;
+ private static Class validClass;
+ private static Class wfClass;
+ private static Class xincClass;
+
+ static ClassLoader getClassLoader ()
+ {
+ Method m = null;
+
+ try {
+ m = Thread.class.getMethod("getContextClassLoader");
+ } catch (NoSuchMethodException e) {
+ // Assume that we are running JDK 1.1, use the current ClassLoader
+ return EventFilter.class.getClassLoader();
+ }
+
+ try {
+ return (ClassLoader) m.invoke(Thread.currentThread());
+ } catch (IllegalAccessException e) {
+ // assert(false)
+ throw new UnknownError(e.getMessage());
+ } catch (InvocationTargetException e) {
+ // assert(e.getTargetException() instanceof SecurityException)
+ throw new UnknownError(e.getMessage());
+ }
+ }
+
+ static Class loadClass (ClassLoader classLoader, String className)
+ {
+ try {
+ if (classLoader == null)
+ return Class.forName(className);
+ else
+ return classLoader.loadClass(className);
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ static private void loadClasses ()
+ {
+ ClassLoader loader = getClassLoader ();
+
+ nsClass = loadClass (loader, "gnu.xml.pipeline.NSFilter");
+ validClass = loadClass (loader, "gnu.xml.pipeline.ValidationConsumer");
+ wfClass = loadClass (loader, "gnu.xml.pipeline.WellFormednessFilter");
+ xincClass = loadClass (loader, "gnu.xml.pipeline.XIncludeFilter");
+ loaded = true;
+ }
+
+
+ /**
+ * Binds the standard SAX2 handlers from the specified consumer
+ * pipeline to the specified producer. These handlers include the core
+ * {@link ContentHandler} and {@link DTDHandler}, plus the extension
+ * {@link DeclHandler} and {@link LexicalHandler}. Any additional
+ * application-specific handlers need to be bound separately.
+ * The {@link ErrorHandler} is handled differently: the producer's
+ * error handler is passed through to the consumer pipeline.
+ * The producer is told to include namespace prefix information if it
+ * can, since many pipeline stages need that Infoset information to
+ * work well.
+ *
+ * <p> At the head of the pipeline, certain standard event filters are
+ * recognized and handled specially. This facilitates construction
+ * of processing pipelines that work regardless of the capabilities
+ * of the XMLReader implementation in use; for example, it permits
+ * validating output of a {@link gnu.xml.util.DomParser}. <ul>
+ *
+ * <li> {@link NSFilter} will be removed if the producer can be
+ * told not to discard namespace data, using the "namespace-prefixes"
+ * feature flag.
+ *
+ * <li> {@link ValidationConsumer} will be removed if the producer
+ * can be told to validate, using the "validation" feature flag.
+ *
+ * <li> {@link WellFormednessFilter} is always removed, on the
+ * grounds that no XMLReader is permitted to producee malformed
+ * event streams and this would just be processing overhead.
+ *
+ * <li> {@link XIncludeFilter} stops the special handling, except
+ * that it's told about the "namespace-prefixes" feature of the
+ * event producer so that the event stream is internally consistent.
+ *
+ * <li> The first consumer which is not one of those classes stops
+ * such special handling. This means that if you want to force
+ * one of those filters to be used, you could just precede it with
+ * an instance of {@link EventFilter} configured as a pass-through.
+ * You might need to do that if you are using an {@link NSFilter}
+ * subclass to fix names found in attributes or character data.
+ *
+ * </ul>
+ *
+ * <p> Other than that, this method works with any kind of event consumer,
+ * not just event filters. Note that in all cases, the standard handlers
+ * are assigned; any previous handler assignments for the handler will
+ * be overridden.
+ *
+ * @param producer will deliver events to the specified consumer
+ * @param consumer pipeline supplying event handlers to be associated
+ * with the producer (may not be null)
+ */
+ public static void bind (XMLReader producer, EventConsumer consumer)
+ {
+ Class klass = null;
+ boolean prefixes;
+
+ if (!loaded)
+ loadClasses ();
+
+ // DOM building, printing, layered validation, and other
+ // things don't work well when prefix info is discarded.
+ // Include it by default, whenever possible.
+ try {
+ producer.setFeature (FEATURE_URI + "namespace-prefixes",
+ true);
+ prefixes = true;
+ } catch (SAXException e) {
+ prefixes = false;
+ }
+
+ // NOTE: This loop doesn't use "instanceof", since that
+ // would prevent compiling/linking without those classes
+ // being present.
+ while (consumer != null) {
+ klass = consumer.getClass ();
+
+ // we might have already changed this problematic SAX2 default.
+ if (nsClass != null && nsClass.isAssignableFrom (klass)) {
+ if (!prefixes)
+ break;
+ consumer = ((EventFilter)consumer).getNext ();
+
+ // the parser _might_ do DTD validation by default ...
+ // if not, maybe we can change this setting.
+ } else if (validClass != null
+ && validClass.isAssignableFrom (klass)) {
+ try {
+ producer.setFeature (FEATURE_URI + "validation",
+ true);
+ consumer = ((ValidationConsumer)consumer).getNext ();
+ } catch (SAXException e) {
+ break;
+ }
+
+ // parsers are required not to have such bugs
+ } else if (wfClass != null && wfClass.isAssignableFrom (klass)) {
+ consumer = ((WellFormednessFilter)consumer).getNext ();
+
+ // stop on the first pipeline stage we can't remove
+ } else
+ break;
+
+ if (consumer == null)
+ klass = null;
+ }
+
+ // the actual setting here doesn't matter as much
+ // as that producer and consumer agree
+ if (xincClass != null && klass != null
+ && xincClass.isAssignableFrom (klass))
+ ((XIncludeFilter)consumer).setSavingPrefixes (prefixes);
+
+ // Some SAX parsers can't handle null handlers -- bleech
+ DefaultHandler2 h = new DefaultHandler2 ();
+
+ if (consumer != null && consumer.getContentHandler () != null)
+ producer.setContentHandler (consumer.getContentHandler ());
+ else
+ producer.setContentHandler (h);
+ if (consumer != null && consumer.getDTDHandler () != null)
+ producer.setDTDHandler (consumer.getDTDHandler ());
+ else
+ producer.setDTDHandler (h);
+
+ try {
+ Object dh;
+
+ if (consumer != null)
+ dh = consumer.getProperty (DECL_HANDLER);
+ else
+ dh = null;
+ if (dh == null)
+ dh = h;
+ producer.setProperty (DECL_HANDLER, dh);
+ } catch (Exception e) { /* ignore */ }
+ try {
+ Object lh;
+
+ if (consumer != null)
+ lh = consumer.getProperty (LEXICAL_HANDLER);
+ else
+ lh = null;
+ if (lh == null)
+ lh = h;
+ producer.setProperty (LEXICAL_HANDLER, lh);
+ } catch (Exception e) { /* ignore */ }
+
+ // this binding goes the other way around
+ if (producer.getErrorHandler () == null)
+ producer.setErrorHandler (h);
+ if (consumer != null)
+ consumer.setErrorHandler (producer.getErrorHandler ());
+ }
+
+ /**
+ * Initializes all handlers to null.
+ */
+ // constructor used by PipelineFactory
+ public EventFilter () { }
+
+
+ /**
+ * Handlers that are not otherwise set will default to those from
+ * the specified consumer, making it easy to pass events through.
+ * If the consumer is null, all handlers are initialzed to null.
+ */
+ // constructor used by PipelineFactory
+ public EventFilter (EventConsumer consumer)
+ {
+ if (consumer == null)
+ return;
+
+ next = consumer;
+
+ // We delegate through the "xxNext" handlers, and
+ // report the "xxHandler" ones on our input side.
+
+ // Normally a subclass would both override handler
+ // methods and register itself as the "xxHandler".
+
+ docHandler = docNext = consumer.getContentHandler ();
+ dtdHandler = dtdNext = consumer.getDTDHandler ();
+ try {
+ declHandler = declNext = (DeclHandler)
+ consumer.getProperty (DECL_HANDLER);
+ } catch (SAXException e) { /* leave value null */ }
+ try {
+ lexHandler = lexNext = (LexicalHandler)
+ consumer.getProperty (LEXICAL_HANDLER);
+ } catch (SAXException e) { /* leave value null */ }
+ }
+
+ /**
+ * Treats the XMLFilterImpl as a limited functionality event consumer,
+ * by arranging to deliver events to it; this lets such classes be
+ * "wrapped" as pipeline stages.
+ *
+ * <p> <em>Upstream Event Setup:</em>
+ * If no handlers have been assigned to this EventFilter, then the
+ * handlers from specified XMLFilterImpl are returned from this
+ * {@link EventConsumer}: the XMLFilterImpl is just "wrapped".
+ * Otherwise the specified handlers will be returned.
+ *
+ * <p> <em>Downstream Event Setup:</em>
+ * Subclasses may chain event delivery to the specified XMLFilterImpl
+ * by invoking the appropiate superclass methods,
+ * as if their constructor passed a "next" EventConsumer to the
+ * constructor for this class.
+ * If this EventFilter has an ErrorHandler, it is assigned as
+ * the error handler for the XMLFilterImpl, just as would be
+ * done for a next stage implementing {@link EventConsumer}.
+ *
+ * @param next the next downstream component of the pipeline.
+ * @exception IllegalStateException if the "next" consumer has
+ * already been set through the constructor.
+ */
+ public void chainTo (XMLFilterImpl next)
+ {
+ if (this.next != null)
+ throw new IllegalStateException ();
+
+ docNext = next.getContentHandler ();
+ if (docHandler == null)
+ docHandler = docNext;
+ dtdNext = next.getDTDHandler ();
+ if (dtdHandler == null)
+ dtdHandler = dtdNext;
+
+ try {
+ declNext = (DeclHandler) next.getProperty (DECL_HANDLER);
+ if (declHandler == null)
+ declHandler = declNext;
+ } catch (SAXException e) { /* leave value null */ }
+ try {
+ lexNext = (LexicalHandler) next.getProperty (LEXICAL_HANDLER);
+ if (lexHandler == null)
+ lexHandler = lexNext;
+ } catch (SAXException e) { /* leave value null */ }
+
+ if (errHandler != null)
+ next.setErrorHandler (errHandler);
+ }
+
+ /**
+ * Records the error handler that should be used by this stage, and
+ * passes it "downstream" to any subsequent stage.
+ */
+ final public void setErrorHandler (ErrorHandler handler)
+ {
+ errHandler = handler;
+ if (next != null)
+ next.setErrorHandler (handler);
+ }
+
+ /**
+ * Returns the error handler assigned this filter stage, or null
+ * if no such assigment has been made.
+ */
+ final public ErrorHandler getErrorHandler ()
+ {
+ return errHandler;
+ }
+
+
+ /**
+ * Returns the next event consumer in sequence; or null if there
+ * is no such handler.
+ */
+ final public EventConsumer getNext ()
+ { return next; }
+
+
+ /**
+ * Assigns the content handler to use; a null handler indicates
+ * that these events will not be forwarded.
+ * This overrides the previous settting for this handler, which was
+ * probably pointed to the next consumer by the base class constructor.
+ */
+ final public void setContentHandler (ContentHandler h)
+ {
+ docHandler = h;
+ }
+
+ /** Returns the content handler being used. */
+ final public ContentHandler getContentHandler ()
+ {
+ return docHandler;
+ }
+
+ /**
+ * Assigns the DTD handler to use; a null handler indicates
+ * that these events will not be forwarded.
+ * This overrides the previous settting for this handler, which was
+ * probably pointed to the next consumer by the base class constructor.
+ */
+ final public void setDTDHandler (DTDHandler h)
+ { dtdHandler = h; }
+
+ /** Returns the dtd handler being used. */
+ final public DTDHandler getDTDHandler ()
+ {
+ return dtdHandler;
+ }
+
+ /**
+ * Stores the property, normally a handler; a null handler indicates
+ * that these events will not be forwarded.
+ * This overrides the previous handler settting, which was probably
+ * pointed to the next consumer by the base class constructor.
+ */
+ final public void setProperty (String id, Object o)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ try {
+ Object value = getProperty (id);
+
+ if (value == o)
+ return;
+ if (DECL_HANDLER.equals (id)) {
+ declHandler = (DeclHandler) o;
+ return;
+ }
+ if (LEXICAL_HANDLER.equals (id)) {
+ lexHandler = (LexicalHandler) o;
+ return;
+ }
+ throw new SAXNotSupportedException (id);
+
+ } catch (ClassCastException e) {
+ throw new SAXNotSupportedException (id);
+ }
+ }
+
+ /** Retrieves a property of unknown intent (usually a handler) */
+ final public Object getProperty (String id)
+ throws SAXNotRecognizedException
+ {
+ if (DECL_HANDLER.equals (id))
+ return declHandler;
+ if (LEXICAL_HANDLER.equals (id))
+ return lexHandler;
+
+ throw new SAXNotRecognizedException (id);
+ }
+
+ /**
+ * Returns any locator provided to the next consumer, if this class
+ * (or a subclass) is handling {@link ContentHandler } events.
+ */
+ public Locator getDocumentLocator ()
+ { return locator; }
+
+
+ // CONTENT HANDLER DELEGATIONS
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void setDocumentLocator (Locator locator)
+ {
+ this.locator = locator;
+ if (docNext != null)
+ docNext.setDocumentLocator (locator);
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void startDocument () throws SAXException
+ {
+ if (docNext != null)
+ docNext.startDocument ();
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void skippedEntity (String name) throws SAXException
+ {
+ if (docNext != null)
+ docNext.skippedEntity (name);
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void processingInstruction (String target, String data)
+ throws SAXException
+ {
+ if (docNext != null)
+ docNext.processingInstruction (target, data);
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void characters (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (docNext != null)
+ docNext.characters (ch, start, length);
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void ignorableWhitespace (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (docNext != null)
+ docNext.ignorableWhitespace (ch, start, length);
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void startPrefixMapping (String prefix, String uri)
+ throws SAXException
+ {
+ if (docNext != null)
+ docNext.startPrefixMapping (prefix, uri);
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void startElement (
+ String uri, String localName,
+ String qName, Attributes atts
+ ) throws SAXException
+ {
+ if (docNext != null)
+ docNext.startElement (uri, localName, qName, atts);
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void endElement (String uri, String localName, String qName)
+ throws SAXException
+ {
+ if (docNext != null)
+ docNext.endElement (uri, localName, qName);
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void endPrefixMapping (String prefix) throws SAXException
+ {
+ if (docNext != null)
+ docNext.endPrefixMapping (prefix);
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void endDocument () throws SAXException
+ {
+ if (docNext != null)
+ docNext.endDocument ();
+ locator = null;
+ }
+
+
+ // DTD HANDLER DELEGATIONS
+
+ /** <b>SAX1:</b> passes this callback to the next consumer, if any */
+ public void unparsedEntityDecl (
+ String name,
+ String publicId,
+ String systemId,
+ String notationName
+ ) throws SAXException
+ {
+ if (dtdNext != null)
+ dtdNext.unparsedEntityDecl (name, publicId, systemId, notationName);
+ }
+
+ /** <b>SAX1:</b> passes this callback to the next consumer, if any */
+ public void notationDecl (String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (dtdNext != null)
+ dtdNext.notationDecl (name, publicId, systemId);
+ }
+
+
+ // LEXICAL HANDLER DELEGATIONS
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void startDTD (String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (lexNext != null)
+ lexNext.startDTD (name, publicId, systemId);
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void endDTD ()
+ throws SAXException
+ {
+ if (lexNext != null)
+ lexNext.endDTD ();
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void comment (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (lexNext != null)
+ lexNext.comment (ch, start, length);
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void startCDATA ()
+ throws SAXException
+ {
+ if (lexNext != null)
+ lexNext.startCDATA ();
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void endCDATA ()
+ throws SAXException
+ {
+ if (lexNext != null)
+ lexNext.endCDATA ();
+ }
+
+ /**
+ * <b>SAX2:</b> passes this callback to the next consumer, if any.
+ */
+ public void startEntity (String name)
+ throws SAXException
+ {
+ if (lexNext != null)
+ lexNext.startEntity (name);
+ }
+
+ /**
+ * <b>SAX2:</b> passes this callback to the next consumer, if any.
+ */
+ public void endEntity (String name)
+ throws SAXException
+ {
+ if (lexNext != null)
+ lexNext.endEntity (name);
+ }
+
+
+ // DECLARATION HANDLER DELEGATIONS
+
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void elementDecl (String name, String model)
+ throws SAXException
+ {
+ if (declNext != null)
+ declNext.elementDecl (name, model);
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void attributeDecl (String eName, String aName,
+ String type, String mode, String value)
+ throws SAXException
+ {
+ if (declNext != null)
+ declNext.attributeDecl (eName, aName, type, mode, value);
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void externalEntityDecl (String name,
+ String publicId, String systemId)
+ throws SAXException
+ {
+ if (declNext != null)
+ declNext.externalEntityDecl (name, publicId, systemId);
+ }
+
+ /** <b>SAX2:</b> passes this callback to the next consumer, if any */
+ public void internalEntityDecl (String name, String value)
+ throws SAXException
+ {
+ if (declNext != null)
+ declNext.internalEntityDecl (name, value);
+ }
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/LinkFilter.java b/libjava/classpath/gnu/xml/pipeline/LinkFilter.java
new file mode 100644
index 000000000..e11a5eca6
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/LinkFilter.java
@@ -0,0 +1,242 @@
+/* LinkFilter.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.pipeline;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Vector;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+
+
+/**
+ * Pipeline filter to remember XHTML links found in a document,
+ * so they can later be crawled. Fragments are not counted, and duplicates
+ * are ignored. Callers are responsible for filtering out URLs they aren't
+ * interested in. Events are passed through unmodified.
+ *
+ * <p> Input MUST include a setDocumentLocator() call, as it's used to
+ * resolve relative links in the absence of a "base" element. Input MUST
+ * also include namespace identifiers, since it is the XHTML namespace
+ * identifier which is used to identify the relevant elements.
+ *
+ * <p><em>FIXME:</em> handle xml:base attribute ... in association with
+ * a stack of base URIs. Similarly, recognize/support XLink data.
+ *
+ * @author David Brownell
+ */
+public class LinkFilter extends EventFilter
+{
+ // for storing URIs
+ private Vector vector = new Vector ();
+
+ // struct for "full" link record (tbd)
+ // these for troubleshooting original source:
+ // original uri
+ // uri as resolved (base, relative, etc)
+ // URI of originating doc
+ // line #
+ // original element + attrs (img src, desc, etc)
+
+ // XLink model of the link ... for inter-site pairups ?
+
+ private String baseURI;
+
+ private boolean siteRestricted = false;
+
+ //
+ // XXX leverage blacklist info (like robots.txt)
+ //
+ // XXX constructor w/param ... pipeline for sending link data
+ // probably XHTML --> XLink, providing info as sketched above
+ //
+
+
+ /**
+ * Constructs a new event filter, which collects links in private data
+ * structure for later enumeration.
+ */
+ // constructor used by PipelineFactory
+ public LinkFilter ()
+ {
+ super.setContentHandler (this);
+ }
+
+
+ /**
+ * Constructs a new event filter, which collects links in private data
+ * structure for later enumeration and passes all events, unmodified,
+ * to the next consumer.
+ */
+ // constructor used by PipelineFactory
+ public LinkFilter (EventConsumer next)
+ {
+ super (next);
+ super.setContentHandler (this);
+ }
+
+
+ /**
+ * Returns an enumeration of the links found since the filter
+ * was constructed, or since removeAllLinks() was called.
+ *
+ * @return enumeration of strings.
+ */
+ public Enumeration getLinks ()
+ {
+ return vector.elements ();
+ }
+
+ /**
+ * Removes records about all links reported to the event
+ * stream, as if the filter were newly created.
+ */
+ public void removeAllLinks ()
+ {
+ vector = new Vector ();
+ }
+
+
+ /**
+ * Collects URIs for (X)HTML content from elements which hold them.
+ */
+ public void startElement (
+ String uri,
+ String localName,
+ String qName,
+ Attributes atts
+ ) throws SAXException
+ {
+ String link;
+
+ // Recognize XHTML links.
+ if ("http://www.w3.org/1999/xhtml".equals (uri)) {
+
+ if ("a".equals (localName) || "base".equals (localName)
+ || "area".equals (localName))
+ link = atts.getValue ("href");
+ else if ("iframe".equals (localName) || "frame".equals (localName))
+ link = atts.getValue ("src");
+ else if ("blockquote".equals (localName) || "q".equals (localName)
+ || "ins".equals (localName) || "del".equals (localName))
+ link = atts.getValue ("cite");
+ else
+ link = null;
+ link = maybeAddLink (link);
+
+ // "base" modifies designated baseURI
+ if ("base".equals (localName) && link != null)
+ baseURI = link;
+
+ if ("iframe".equals (localName) || "img".equals (localName))
+ maybeAddLink (atts.getValue ("longdesc"));
+ }
+
+ super.startElement (uri, localName, qName, atts);
+ }
+
+ private String maybeAddLink (String link)
+ {
+ int index;
+
+ // ignore empty links and fragments inside docs
+ if (link == null)
+ return null;
+ if ((index = link.indexOf ("#")) >= 0)
+ link = link.substring (0, index);
+ if (link.equals (""))
+ return null;
+
+ try {
+ // get the real URI
+ URL base = new URL ((baseURI != null)
+ ? baseURI
+ : getDocumentLocator ().getSystemId ());
+ URL url = new URL (base, link);
+
+ link = url.toString ();
+
+ // ignore duplicates
+ if (vector.contains (link))
+ return link;
+
+ // other than what "base" does, stick to original site:
+ if (siteRestricted) {
+ // don't switch protocols
+ if (!base.getProtocol ().equals (url.getProtocol ()))
+ return link;
+ // don't switch servers
+ if (base.getHost () != null
+ && !base.getHost ().equals (url.getHost ()))
+ return link;
+ }
+
+ vector.addElement (link);
+
+ return link;
+
+ } catch (IOException e) {
+ // bad URLs we don't want
+ }
+ return null;
+ }
+
+ /**
+ * Reports an error if no Locator has been made available.
+ */
+ public void startDocument ()
+ throws SAXException
+ {
+ if (getDocumentLocator () == null)
+ throw new SAXException ("no Locator!");
+ }
+
+ /**
+ * Forgets about any base URI information that may be recorded.
+ * Applications will often want to call removeAllLinks(), likely
+ * after examining the links which were reported.
+ */
+ public void endDocument ()
+ throws SAXException
+ {
+ baseURI = null;
+ super.endDocument ();
+ }
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/NSFilter.java b/libjava/classpath/gnu/xml/pipeline/NSFilter.java
new file mode 100644
index 000000000..0fa4621d3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/NSFilter.java
@@ -0,0 +1,341 @@
+/* NSFilter.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.pipeline;
+
+import java.util.Enumeration;
+import java.util.Stack;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.helpers.AttributesImpl;
+import org.xml.sax.helpers.NamespaceSupport;
+
+/**
+ * This filter ensures that element and attribute names are properly prefixed,
+ * and that such prefixes are declared. Such data is critical for operations
+ * like writing XML text, and validating against DTDs: names or their prefixes
+ * may have been discarded, although they are essential to the exchange of
+ * information using XML. There are various common ways that such data
+ * gets discarded: <ul>
+ *
+ * <li> By default, SAX2 parsers must discard the "xmlns*"
+ * attributes, and may also choose not to report properly prefixed
+ * names for elements or attributes. (Some parsers may support
+ * changing the <em>namespace-prefixes</em> value from the default
+ * to <em>true</em>, effectively eliminating the need to use this
+ * filter on their output.)
+ *
+ * <li> When event streams are generated from a DOM tree, they may
+ * have never have had prefixes or declarations for namespaces; or
+ * the existing prefixes or declarations may have been invalidated
+ * by structural modifications to that DOM tree.
+ *
+ * <li> Other software writing SAX event streams won't necessarily
+ * be worrying about prefix management, and so they will need to
+ * have a transparent solution for managing them.
+ *
+ * </ul>
+ *
+ * <p> This filter uses a heuristic to choose the prefix to assign to any
+ * particular name which wasn't already corectly prefixed. The associated
+ * namespace will be correct, and the prefix will be declared. Original
+ * structures facilitating text editing, such as conventions about use of
+ * mnemonic prefix names or the scoping of prefixes, can't always be
+ * reconstructed after they are discarded, as strongly encouraged by the
+ * current SAX2 defaults.
+ *
+ * <p> Note that this can't possibly know whether values inside attribute
+ * value or document content involve prefixed names. If your application
+ * requires using prefixed names in such locations you'll need to add some
+ * appropriate logic (perhaps adding additional heuristics in a subclass).
+ *
+ * @author David Brownell
+ */
+public class NSFilter extends EventFilter
+{
+ private NamespaceSupport nsStack = new NamespaceSupport ();
+ private Stack elementStack = new Stack ();
+
+ private boolean pushedContext;
+ private String nsTemp [] = new String [3];
+ private AttributesImpl attributes = new AttributesImpl ();
+ private boolean usedDefault;
+
+ // gensymmed prefixes use this root name
+ private static final String prefixRoot = "prefix-";
+
+
+ /**
+ * Passes events through to the specified consumer, after first
+ * processing them.
+ *
+ * @param next the next event consumer to receive events.
+ */
+ // constructor used by PipelineFactory
+ public NSFilter (EventConsumer next)
+ {
+ super (next);
+
+ setContentHandler (this);
+ }
+
+ private void fatalError (String message)
+ throws SAXException
+ {
+ SAXParseException e;
+ ErrorHandler handler = getErrorHandler ();
+ Locator locator = getDocumentLocator ();
+
+ if (locator == null)
+ e = new SAXParseException (message, null, null, -1, -1);
+ else
+ e = new SAXParseException (message, locator);
+ if (handler != null)
+ handler.fatalError (e);
+ throw e;
+ }
+
+
+ public void startDocument () throws SAXException
+ {
+ elementStack.removeAllElements ();
+ nsStack.reset ();
+ pushedContext = false;
+ super.startDocument ();
+ }
+
+ /**
+ * This call is not passed to the next consumer in the chain.
+ * Prefix declarations and scopes are only exposed in the form
+ * of attributes; this callback just records a declaration that
+ * will be exposed as an attribute.
+ */
+ public void startPrefixMapping (String prefix, String uri)
+ throws SAXException
+ {
+ if (pushedContext == false) {
+ nsStack.pushContext ();
+ pushedContext = true;
+ }
+
+ // this check is awkward, but the paranoia prevents big trouble
+ for (Enumeration e = nsStack.getDeclaredPrefixes ();
+ e.hasMoreElements ();
+ /* NOP */ ) {
+ String declared = (String) e.nextElement ();
+
+ if (!declared.equals (prefix))
+ continue;
+ if (uri.equals (nsStack.getURI (prefix)))
+ return;
+ fatalError ("inconsistent binding for prefix '" + prefix
+ + "' ... " + uri + " (was " + nsStack.getURI (prefix) + ")");
+ }
+
+ if (!nsStack.declarePrefix (prefix, uri))
+ fatalError ("illegal prefix declared: " + prefix);
+ }
+
+ private String fixName (String ns, String l, String name, boolean isAttr)
+ throws SAXException
+ {
+ if ("".equals (name) || name == null) {
+ name = l;
+ if ("".equals (name) || name == null)
+ fatalError ("empty/null name");
+ }
+
+ // can we correctly process the name as-is?
+ // handles "element scope" attribute names here.
+ if (nsStack.processName (name, nsTemp, isAttr) != null
+ && nsTemp [0].equals (ns)
+ ) {
+ return nsTemp [2];
+ }
+
+ // nope, gotta modify the name or declare a default mapping
+ int temp;
+
+ // get rid of any current prefix
+ if ((temp = name.indexOf (':')) >= 0) {
+ name = name.substring (temp + 1);
+
+ // ... maybe that's enough (use/prefer default namespace) ...
+ if (!isAttr && nsStack.processName (name, nsTemp, false) != null
+ && nsTemp [0].equals (ns)
+ ) {
+ return nsTemp [2];
+ }
+ }
+
+ // must we define and use the default/undefined prefix?
+ if ("".equals (ns)) {
+ if (isAttr)
+ fatalError ("processName bug");
+ if (attributes.getIndex ("xmlns") != -1)
+ fatalError ("need to undefine default NS, but it's bound: "
+ + attributes.getValue ("xmlns"));
+
+ nsStack.declarePrefix ("", "");
+ attributes.addAttribute ("", "", "xmlns", "CDATA", "");
+ return name;
+ }
+
+ // is there at least one non-null prefix we can use?
+ for (Enumeration e = nsStack.getDeclaredPrefixes ();
+ e.hasMoreElements ();
+ /* NOP */) {
+ String prefix = (String) e.nextElement ();
+ String uri = nsStack.getURI (prefix);
+
+ if (uri == null || !uri.equals (ns))
+ continue;
+ return prefix + ":" + name;
+ }
+
+ // no such luck. create a prefix name, declare it, use it.
+ for (temp = 0; temp >= 0; temp++) {
+ String prefix = prefixRoot + temp;
+
+ if (nsStack.getURI (prefix) == null) {
+ nsStack.declarePrefix (prefix, ns);
+ attributes.addAttribute ("", "", "xmlns:" + prefix,
+ "CDATA", ns);
+ return prefix + ":" + name;
+ }
+ }
+ fatalError ("too many prefixes genned");
+ // NOTREACHED
+ return null;
+ }
+
+ public void startElement (
+ String uri, String localName,
+ String qName, Attributes atts
+ ) throws SAXException
+ {
+ if (!pushedContext)
+ nsStack.pushContext ();
+ pushedContext = false;
+
+ // make sure we have all NS declarations handy before we start
+ int length = atts.getLength ();
+
+ for (int i = 0; i < length; i++) {
+ String aName = atts.getQName (i);
+
+ if (!aName.startsWith ("xmlns"))
+ continue;
+
+ String prefix;
+
+ if ("xmlns".equals (aName))
+ prefix = "";
+ else if (aName.indexOf (':') == 5)
+ prefix = aName.substring (6);
+ else // "xmlnsfoo" etc.
+ continue;
+ startPrefixMapping (prefix, atts.getValue (i));
+ }
+
+ // put namespace decls at the start of our regenned attlist
+ attributes.clear ();
+ for (Enumeration e = nsStack.getDeclaredPrefixes ();
+ e.hasMoreElements ();
+ /* NOP */) {
+ String prefix = (String) e.nextElement ();
+
+ attributes.addAttribute ("", "",
+ ("".equals (prefix)
+ ? "xmlns"
+ : "xmlns:" + prefix),
+ "CDATA",
+ nsStack.getURI (prefix));
+ }
+
+ // name fixups: element, then attributes.
+ // fixName may declare a new prefix or, for the element,
+ // redeclare the default (if element name needs it).
+ qName = fixName (uri, localName, qName, false);
+
+ for (int i = 0; i < length; i++) {
+ String aName = atts.getQName (i);
+ String aNS = atts.getURI (i);
+ String aLocal = atts.getLocalName (i);
+ String aType = atts.getType (i);
+ String aValue = atts.getValue (i);
+
+ if (aName.startsWith ("xmlns"))
+ continue;
+ aName = fixName (aNS, aLocal, aName, true);
+ attributes.addAttribute (aNS, aLocal, aName, aType, aValue);
+ }
+
+ elementStack.push (qName);
+
+ // pass event along, with cleaned-up names and decls.
+ super.startElement (uri, localName, qName, attributes);
+ }
+
+ public void endElement (String uri, String localName, String qName)
+ throws SAXException
+ {
+ nsStack.popContext ();
+ qName = (String) elementStack.pop ();
+ super.endElement (uri, localName, qName);
+ }
+
+ /**
+ * This call is not passed to the next consumer in the chain.
+ * Prefix declarations and scopes are only exposed in their
+ * attribute form.
+ */
+ public void endPrefixMapping (String prefix)
+ throws SAXException
+ { }
+
+ public void endDocument () throws SAXException
+ {
+ elementStack.removeAllElements ();
+ nsStack.reset ();
+ super.endDocument ();
+ }
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/PipelineFactory.java b/libjava/classpath/gnu/xml/pipeline/PipelineFactory.java
new file mode 100644
index 000000000..c2adab021
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/PipelineFactory.java
@@ -0,0 +1,723 @@
+/* PipelineFactory.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.pipeline;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.lang.reflect.Constructor;
+import java.util.StringTokenizer;
+
+import org.xml.sax.*;
+import org.xml.sax.ext.*;
+
+
+/**
+ * This provides static factory methods for creating simple event pipelines.
+ * These pipelines are specified by strings, suitable for passing on
+ * command lines or embedding in element attributes. For example, one way
+ * to write a pipeline that restores namespace syntax, validates (stopping
+ * the pipeline on validity errors) and then writes valid data to standard
+ * output is this: <pre>
+ * nsfix | validate | write ( stdout )</pre>
+ *
+ * <p> In this syntax, the tokens are always separated by whitespace, and each
+ * stage of the pipeline may optionally have a parameter (which can be a
+ * pipeline) in parentheses. Interior stages are called filters, and the
+ * rightmost end of a pipeline is called a terminus.
+ *
+ * <p> Stages are usually implemented by a single class, which may not be
+ * able to act as both a filter and a terminus; but any terminus can be
+ * automatically turned into a filter, through use of a {@link TeeConsumer}.
+ * The stage identifiers are either class names, or are one of the following
+ * short identifiers built into this class. (Most of these identifiers are
+ * no more than aliases for classes.) The built-in identifiers include:</p>
+
+ <table border="1" cellpadding="3" cellspacing="0">
+ <tr bgcolor="#ccccff" class="TableHeadingColor">
+ <th align="center" width="5%">Stage</th>
+ <th align="center" width="9%">Parameter</th>
+ <th align="center" width="1%">Terminus</th>
+ <th align="center">Description</th>
+ </tr>
+
+ <tr valign="top" align="center">
+ <td><a href="../dom/Consumer.html">dom</a></td>
+ <td><em>none</em></td>
+ <td> yes </td>
+ <td align="left"> Applications code can access a DOM Document built
+ from the input event stream. When used as a filter, this buffers
+ data up to an <em>endDocument</em> call, and then uses a DOM parser
+ to report everything that has been recorded (which can easily be
+ less than what was reported to it). </td>
+ </tr>
+ <tr valign="top" align="center">
+ <td><a href="NSFilter.html">nsfix</a></td>
+ <td><em>none</em></td>
+ <td>no</td>
+ <td align="left">This stage ensures that the XML element and attribute
+ names in its output use namespace prefixes and declarations correctly.
+ That is, so that they match the "Namespace plus LocalName" naming data
+ with which each XML element and attribute is already associated. </td>
+ </tr>
+ <tr valign="top" align="center">
+ <td><a href="EventFilter.html">null</a></td>
+ <td><em>none</em></td>
+ <td>yes</td>
+ <td align="left">This stage ignores all input event data.</td>
+ </tr>
+ <tr valign="top" align="center">
+ <td><a href="CallFilter.html">server</a></td>
+ <td><em>required</em><br> server URL </td>
+ <td>no</td>
+ <td align="left">Sends its input as XML request to a remote server,
+ normally a web application server using the HTTP or HTTPS protocols.
+ The output of this stage is the parsed response from that server.</td>
+ </tr>
+ <tr valign="top" align="center">
+ <td><a href="TeeConsumer.html">tee</a></td>
+ <td><em>required</em><br> first pipeline</td>
+ <td>no</td>
+ <td align="left">This sends its events down two paths; its parameter
+ is a pipeline descriptor for the first path, and the second path
+ is the output of this stage.</td>
+ </tr>
+
+ <tr valign="top" align="center">
+ <td><a href="ValidationConsumer.html">validate</a></td>
+ <td><em>none</em></td>
+ <td>yes</td>
+ <td align="left">This checks for validity errors, and reports them
+ through its error handler. The input must include declaration events
+ and some lexical events. </td>
+ </tr>
+ <tr valign="top" align="center">
+ <td><a href="WellFormednessFilter.html">wf</a></td>
+ <td><em>none</em></td>
+ <td>yes</td>
+ <td align="left"> This class provides some basic "well formedness"
+ tests on the input event stream, and reports a fatal error if any
+ of them fail. One example: start/end calls for elements must match.
+ No SAX parser is permitted to produce malformed output, but other
+ components can easily do so.</td>
+ </tr>
+ <tr valign="top" align="center">
+ <td>write</td>
+ <td><em>required</em><br> "stdout", "stderr", or filename</td>
+ <td>yes</td>
+ <td align="left"> Writes its input to the specified output, as pretty
+ printed XML text encoded using UTF-8. Input events must be well
+ formed and "namespace fixed", else the output won't be XML (or possibly
+ namespace) conformant. The symbolic names represent
+ <em>System.out</em> and <em>System.err</em> respectively; names must
+ correspond to files which don't yet exist.</td>
+ </tr>
+ <tr valign="top" align="center">
+ <td>xhtml</td>
+ <td><em>required</em><br> "stdout", "stderr", or filename</td>
+ <td>yes</td>
+ <td align="left"> Like <em>write</em> (above), except that XHTML rules
+ are followed. The XHTML 1.0 Transitional document type is declared,
+ and only ASCII characters are written (for interoperability). Other
+ characters are written as entity or character references; the text is
+ pretty printed.</td>
+ </tr>
+ <tr valign="top" align="center">
+ <td><a href="XIncludeFilter.html">xinclude</a></td>
+ <td><em>none</em></td>
+ <td>no</td>
+ <td align="left">This stage handles XInclude processing.
+ This is like entity inclusion, except that the included content
+ is declared in-line rather than in the DTD at the beginning of
+ a document.
+ </td>
+ </tr>
+ <tr valign="top" align="center">
+ <td><a href="XsltFilter.html">xslt</a></td>
+ <td><em>required</em><br> XSLT stylesheet URI</td>
+ <td>no</td>
+ <td align="left">This stage handles XSLT transformation
+ according to a stylesheet.
+ The implementation of the transformation may not actually
+ stream data, although if such an XSLT engine is in use
+ then that can happen.
+ </td>
+ </tr>
+
+ </table>
+
+ * <p> Note that {@link EventFilter#bind} can automatically eliminate
+ * some filters by setting SAX2 parser features appropriately. This means
+ * that you can routinely put filters like "nsfix", "validate", or "wf" at the
+ * front of a pipeline (for components that need inputs conditioned to match
+ * that level of correctness), and know that it won't actually be used unless
+ * it's absolutely necessary.
+ *
+ * @author David Brownell
+ */
+public class PipelineFactory
+{
+ /**
+ * Creates a simple pipeline according to the description string passed in.
+ */
+ public static EventConsumer createPipeline (String description)
+ throws IOException
+ {
+ return createPipeline (description, null);
+ }
+
+ /**
+ * Extends an existing pipeline by prepending the filter pipeline to the
+ * specified consumer. Some pipelines need more customization than can
+ * be done through this simplified syntax. When they are set up with
+ * direct API calls, use this method to merge more complex pipeline
+ * segments with easily configured ones.
+ */
+ public static EventConsumer createPipeline (
+ String description,
+ EventConsumer next
+ ) throws IOException
+ {
+ // tokens are (for now) what's separated by whitespace;
+ // very easy to parse, but IDs never have spaces.
+
+ StringTokenizer tokenizer;
+ String tokens [];
+
+ tokenizer = new StringTokenizer (description);
+ tokens = new String [tokenizer.countTokens ()];
+ for (int i = 0; i < tokens.length; i++)
+ tokens [i] = tokenizer.nextToken ();
+
+ PipelineFactory factory = new PipelineFactory ();
+ Pipeline pipeline = factory.parsePipeline (tokens, next);
+
+ return pipeline.createPipeline ();
+ }
+
+
+ private PipelineFactory () { /* NYET */ }
+
+
+ /**
+ * Extends an existing pipeline by prepending a pre-tokenized filter
+ * pipeline to the specified consumer. Tokens are class names (or the
+ * predefined aliases) left and right parenthesis, and the vertical bar.
+ */
+ public static EventConsumer createPipeline (
+ String tokens [],
+ EventConsumer next
+ ) throws IOException
+ {
+ PipelineFactory factory = new PipelineFactory ();
+ Pipeline pipeline = factory.parsePipeline (tokens, next);
+
+ return pipeline.createPipeline ();
+ }
+
+
+ private String tokens [];
+ private int index;
+
+ private Pipeline parsePipeline (String toks [], EventConsumer next)
+ {
+ tokens = toks;
+ index = 0;
+
+ Pipeline retval = parsePipeline (next);
+
+ if (index != toks.length)
+ throw new ArrayIndexOutOfBoundsException (
+ "extra token: " + tokens [index]);
+ return retval;
+ }
+
+ // pipeline ::= stage | stage '|' pipeline
+ private Pipeline parsePipeline (EventConsumer next)
+ {
+ Pipeline retval = new Pipeline (parseStage ());
+
+ // minimal pipelines: "stage" and "... | id"
+ if (index > (tokens.length - 2)
+ || !"|".equals (tokens [index])
+ ) {
+ retval.next = next;
+ return retval;
+ }
+ index++;
+ retval.rest = parsePipeline (next);
+ return retval;
+ }
+
+ // stage ::= id | id '(' pipeline ')'
+ private Stage parseStage ()
+ {
+ Stage retval = new Stage (tokens [index++]);
+
+ // minimal stages: "id" and "id ( id )"
+ if (index > (tokens.length - 2)
+ || !"(".equals (tokens [index]) /*)*/
+ )
+ return retval;
+
+ index++;
+ retval.param = parsePipeline (null);
+ if (index >= tokens.length)
+ throw new ArrayIndexOutOfBoundsException (
+ "missing right paren");
+ if (/*(*/ !")".equals (tokens [index++]))
+ throw new ArrayIndexOutOfBoundsException (
+ "required right paren, not: " + tokens [index - 1]);
+ return retval;
+ }
+
+
+ //
+ // these classes obey the conventions for constructors, so they're
+ // only built in to this table of shortnames
+ //
+ // - filter (one or two types of arglist)
+ // * last constructor is 'next' element
+ // * optional (first) string parameter
+ //
+ // - terminus (one or types of arglist)
+ // * optional (only) string parameter
+ //
+ // terminus stages are transformed into filters if needed, by
+ // creating a "tee". filter stages aren't turned to terminus
+ // stages though; either eliminate such stages, or add some
+ // terminus explicitly.
+ //
+ private static final String builtinStages [][] = {
+ { "dom", "gnu.xml.dom.Consumer" },
+ { "nsfix", "gnu.xml.pipeline.NSFilter" },
+ { "null", "gnu.xml.pipeline.EventFilter" },
+ { "server", "gnu.xml.pipeline.CallFilter" },
+ { "tee", "gnu.xml.pipeline.TeeConsumer" },
+ { "validate", "gnu.xml.pipeline.ValidationConsumer" },
+ { "wf", "gnu.xml.pipeline.WellFormednessFilter" },
+ { "xinclude", "gnu.xml.pipeline.XIncludeFilter" },
+ { "xslt", "gnu.xml.pipeline.XsltFilter" },
+
+// XXX want: option for validate, to preload external part of a DTD
+
+ // xhtml, write ... nyet generic-ready
+ };
+
+ private static class Stage
+ {
+ String id;
+ Pipeline param;
+
+ Stage (String name)
+ { id = name; }
+
+ public String toString ()
+ {
+ if (param == null)
+ return id;
+ return id + " ( " + param + " )";
+ }
+
+ private void fail (String message)
+ throws IOException
+ {
+ throw new IOException ("in '" + id
+ + "' stage of pipeline, " + message);
+ }
+
+ EventConsumer createStage (EventConsumer next)
+ throws IOException
+ {
+ String name = id;
+
+ // most builtins are just class aliases
+ for (int i = 0; i < builtinStages.length; i++) {
+ if (id.equals (builtinStages [i][0])) {
+ name = builtinStages [i][1];
+ break;
+ }
+ }
+
+ // Save output as XML or XHTML text
+ if ("write".equals (name) || "xhtml".equals (name)) {
+ String filename;
+ boolean isXhtml = "xhtml".equals (name);
+ OutputStream out = null;
+ TextConsumer consumer;
+
+ if (param == null)
+ fail ("parameter is required");
+
+ filename = param.toString ();
+ if ("stdout".equals (filename))
+ out = System.out;
+ else if ("stderr".equals (filename))
+ out = System.err;
+ else {
+ File f = new File (filename);
+
+/*
+ if (!f.isAbsolute ())
+ fail ("require absolute file paths");
+ */
+ if (f.exists ())
+ fail ("file already exists: " + f.getName ());
+
+// XXX this races against the existence test
+ out = new FileOutputStream (f);
+ }
+
+ if (!isXhtml)
+ consumer = new TextConsumer (out);
+ else
+ consumer = new TextConsumer (
+ new OutputStreamWriter (out, "8859_1"),
+ true);
+
+ consumer.setPrettyPrinting (true);
+ if (next == null)
+ return consumer;
+ return new TeeConsumer (consumer, next);
+
+ } else {
+ //
+ // Here go all the builtins that are just aliases for
+ // classes, and all stage IDs that started out as such
+ // class names. The following logic relies on several
+ // documented conventions for constructor invocation.
+ //
+ String msg = null;
+
+ try {
+ Class klass = Class.forName (name);
+ Class argTypes [] = null;
+ Constructor constructor = null;
+ boolean filter = false;
+ Object params [] = null;
+ Object obj = null;
+
+ // do we need a filter stage?
+ if (next != null) {
+ // "next" consumer is always passed, with
+ // or without the optional string param
+ if (param == null) {
+ argTypes = new Class [1];
+ argTypes [0] = EventConsumer.class;
+
+ params = new Object [1];
+ params [0] = next;
+
+ msg = "no-param filter";
+ } else {
+ argTypes = new Class [2];
+ argTypes [0] = String.class;
+ argTypes [1] = EventConsumer.class;
+
+ params = new Object [2];
+ params [0] = param.toString ();
+ params [1] = next;
+
+ msg = "one-param filter";
+ }
+
+
+ try {
+ constructor = klass.getConstructor (argTypes);
+ } catch (NoSuchMethodException e) {
+ // try creating a filter from a
+ // terminus and a tee
+ filter = true;
+ msg += " built from ";
+ }
+ }
+
+ // build from a terminus stage, with or
+ // without the optional string param
+ if (constructor == null) {
+ String tmp;
+
+ if (param == null) {
+ argTypes = new Class [0];
+ params = new Object [0];
+
+ tmp = "no-param terminus";
+ } else {
+ argTypes = new Class [1];
+ argTypes [0] = String.class;
+
+ params = new Object [1];
+ params [0] = param.toString ();
+
+ tmp = "one-param terminus";
+ }
+ if (msg == null)
+ msg = tmp;
+ else
+ msg += tmp;
+ constructor = klass.getConstructor (argTypes);
+ // NOT creating terminus by dead-ending
+ // filters ... users should think about
+ // that one, something's likely wrong
+ }
+
+ obj = constructor.newInstance (params);
+
+ // return EventConsumers directly, perhaps after
+ // turning them into a filter
+ if (obj instanceof EventConsumer) {
+ if (filter)
+ return new TeeConsumer ((EventConsumer) obj, next);
+ return (EventConsumer) obj;
+ }
+
+ // if it's not a handler, it's an error
+ // we can wrap handlers in a filter
+ EventFilter retval = new EventFilter ();
+ boolean updated = false;
+
+ if (obj instanceof ContentHandler) {
+ retval.setContentHandler ((ContentHandler) obj);
+ updated = true;
+ }
+ if (obj instanceof DTDHandler) {
+ retval.setDTDHandler ((DTDHandler) obj);
+ updated = true;
+ }
+ if (obj instanceof LexicalHandler) {
+ retval.setProperty (
+ EventFilter.PROPERTY_URI + "lexical-handler",
+ obj);
+ updated = true;
+ }
+ if (obj instanceof DeclHandler) {
+ retval.setProperty (
+ EventFilter.PROPERTY_URI + "declaration-handler",
+ obj);
+ updated = true;
+ }
+
+ if (!updated)
+ fail ("class is neither Consumer nor Handler");
+
+ if (filter)
+ return new TeeConsumer (retval, next);
+ return retval;
+
+ } catch (IOException e) {
+ throw e;
+
+ } catch (NoSuchMethodException e) {
+ fail (name + " constructor missing -- " + msg);
+
+ } catch (ClassNotFoundException e) {
+ fail (name + " class not found");
+
+ } catch (Exception e) {
+ // e.printStackTrace ();
+ fail ("stage not available: " + e.getMessage ());
+ }
+ }
+ // NOTREACHED
+ return null;
+ }
+ }
+
+ private static class Pipeline
+ {
+ Stage stage;
+
+ // rest may be null
+ Pipeline rest;
+ EventConsumer next;
+
+ Pipeline (Stage s)
+ { stage = s; }
+
+ public String toString ()
+ {
+ if (rest == null && next == null)
+ return stage.toString ();
+ if (rest != null)
+ return stage + " | " + rest;
+ throw new IllegalArgumentException ("next");
+ }
+
+ EventConsumer createPipeline ()
+ throws IOException
+ {
+ if (next == null) {
+ if (rest == null)
+ next = stage.createStage (null);
+ else
+ next = stage.createStage (rest.createPipeline ());
+ }
+ return next;
+ }
+ }
+
+/*
+ public static void main (String argv [])
+ {
+ try {
+ // three basic terminus cases
+ createPipeline ("null");
+ createPipeline ("validate");
+ createPipeline ("write ( stdout )");
+
+ // four basic filters
+ createPipeline ("nsfix | write ( stderr )");
+ createPipeline ("wf | null");
+ createPipeline ("null | null");
+ createPipeline (
+"call ( http://www.example.com/services/xml-1a ) | xhtml ( stdout )");
+
+ // tee junctions
+ createPipeline ("tee ( validate ) | write ( stdout )");
+ createPipeline ("tee ( nsfix | write ( stdout ) ) | validate");
+
+ // longer pipeline
+ createPipeline ("nsfix | tee ( validate ) | write ( stdout )");
+ createPipeline (
+ "null | wf | nsfix | tee ( validate ) | write ( stdout )");
+
+ // try some parsing error cases
+ try {
+ createPipeline ("null ("); // extra token '('
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+
+ try {
+ createPipeline ("nsfix |"); // extra token '|'
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+
+ try {
+ createPipeline ("xhtml ( foo"); // missing right paren
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+
+ try {
+ createPipeline ("xhtml ( foo bar"); // required right paren
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+
+ try {
+ createPipeline ("tee ( nsfix | validate");// missing right paren
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+
+ // try some construction error cases
+
+ try {
+ createPipeline ("call"); // missing param
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+ try {
+ createPipeline ("call ( foobar )"); // broken param
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+ try {
+ createPipeline ("nsfix ( foobar )"); // illegal param
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+ try {
+ createPipeline ("null ( foobar )"); // illegal param
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+ try {
+ createPipeline ("wf ( foobar )"); // illegal param
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+ try {
+ createPipeline ("xhtml ( foobar.html )");
+ new File ("foobar.html").delete ();
+ // now supported
+ } catch (Exception e) {
+ System.err.println ("** err: " + e.getMessage ()); }
+ try {
+ createPipeline ("xhtml"); // missing param
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+ try {
+ createPipeline ("write ( stdout ) | null"); // nonterminal
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+ try {
+ createPipeline ("validate | null");
+ // now supported
+ } catch (Exception e) {
+ System.err.println ("** err: " + e.getMessage ()); }
+ try {
+ createPipeline ("validate ( foo )"); // illegal param
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+ try {
+ createPipeline ("tee"); // missing param
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+ try {
+ // only builtins so far
+ createPipeline ("com.example.xml.FilterClass");
+ System.err.println ("** didn't report error");
+ } catch (Exception e) {
+ System.err.println ("== err: " + e.getMessage ()); }
+
+ } catch (Exception e) {
+ e.printStackTrace ();
+ }
+ }
+/**/
+
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/TeeConsumer.java b/libjava/classpath/gnu/xml/pipeline/TeeConsumer.java
new file mode 100644
index 000000000..3ac860575
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/TeeConsumer.java
@@ -0,0 +1,417 @@
+/* TeeConsumer.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.pipeline;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.DTDHandler;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.ext.DeclHandler;
+import org.xml.sax.ext.LexicalHandler;
+
+/**
+ * Fans its events out to two other consumers, a "tee" filter stage in an
+ * event pipeline. Networks can be assembled with multiple output points.
+ *
+ * <p> Error handling should be simple if you remember that exceptions
+ * you throw will cancel later stages in that callback's pipeline, and
+ * generally the producer will stop if it sees such an exception. You
+ * may want to protect your pipeline against such backflows, making a
+ * kind of reverse filter (or valve?) so that certain exceptions thrown by
+ * your pipeline will caught and handled before the producer sees them.
+ * Just use a "try/catch" block, rememebering that really important
+ * cleanup tasks should be in "finally" clauses.
+ *
+ * <p> That issue isn't unique to "tee" consumers, but tee consumers have
+ * the additional twist that exceptions thrown by the first consumer
+ * will cause the second consumer not to see the callback (except for
+ * the endDocument callback, which signals state cleanup).
+ *
+ * @author David Brownell
+ */
+final public class TeeConsumer
+ implements EventConsumer,
+ ContentHandler, DTDHandler,
+ LexicalHandler,DeclHandler
+{
+ private EventConsumer first, rest;
+
+ // cached to minimize time overhead
+ private ContentHandler docFirst, docRest;
+ private DeclHandler declFirst, declRest;
+ private LexicalHandler lexFirst, lexRest;
+
+
+ /**
+ * Constructs a consumer which sends all its events to the first
+ * consumer, and then the second one. If the first consumer throws
+ * an exception, the second one will not see the event which
+ * caused that exception to be reported.
+ *
+ * @param car The first consumer to get the events
+ * @param cdr The second consumer to get the events
+ */
+ public TeeConsumer (EventConsumer car, EventConsumer cdr)
+ {
+ if (car == null || cdr == null)
+ throw new NullPointerException ();
+ first = car;
+ rest = cdr;
+
+ //
+ // Cache the handlers.
+ //
+ docFirst = first.getContentHandler ();
+ docRest = rest.getContentHandler ();
+ // DTD handler isn't cached (rarely needed)
+
+ try {
+ declFirst = null;
+ declFirst = (DeclHandler) first.getProperty (
+ EventFilter.DECL_HANDLER);
+ } catch (SAXException e) {}
+ try {
+ declRest = null;
+ declRest = (DeclHandler) rest.getProperty (
+ EventFilter.DECL_HANDLER);
+ } catch (SAXException e) {}
+
+ try {
+ lexFirst = null;
+ lexFirst = (LexicalHandler) first.getProperty (
+ EventFilter.LEXICAL_HANDLER);
+ } catch (SAXException e) {}
+ try {
+ lexRest = null;
+ lexRest = (LexicalHandler) rest.getProperty (
+ EventFilter.LEXICAL_HANDLER);
+ } catch (SAXException e) {}
+ }
+
+/* FIXME
+ /**
+ * Constructs a pipeline, and is otherwise a shorthand for the
+ * two-consumer constructor for this class.
+ *
+ * @param first Description of the first pipeline to get events,
+ * which will be passed to {@link PipelineFactory#createPipeline}
+ * @param rest The second pipeline to get the events
+ * /
+ // constructor used by PipelineFactory
+ public TeeConsumer (String first, EventConsumer rest)
+ throws IOException
+ {
+ this (PipelineFactory.createPipeline (first), rest);
+ }
+*/
+
+ /** Returns the first pipeline to get event calls. */
+ public EventConsumer getFirst ()
+ { return first; }
+
+ /** Returns the second pipeline to get event calls. */
+ public EventConsumer getRest ()
+ { return rest; }
+
+ /** Returns the content handler being used. */
+ final public ContentHandler getContentHandler ()
+ {
+ if (docRest == null)
+ return docFirst;
+ if (docFirst == null)
+ return docRest;
+ return this;
+ }
+
+ /** Returns the dtd handler being used. */
+ final public DTDHandler getDTDHandler ()
+ {
+ // not cached (hardly used)
+ if (rest.getDTDHandler () == null)
+ return first.getDTDHandler ();
+ if (first.getDTDHandler () == null)
+ return rest.getDTDHandler ();
+ return this;
+ }
+
+ /** Returns the declaration or lexical handler being used. */
+ final public Object getProperty (String id)
+ throws SAXNotRecognizedException
+ {
+ //
+ // in degenerate cases, we have no work to do.
+ //
+ Object firstProp = null, restProp = null;
+
+ try { firstProp = first.getProperty (id); }
+ catch (SAXNotRecognizedException e) { /* ignore */ }
+ try { restProp = rest.getProperty (id); }
+ catch (SAXNotRecognizedException e) { /* ignore */ }
+
+ if (restProp == null)
+ return firstProp;
+ if (firstProp == null)
+ return restProp;
+
+ //
+ // we've got work to do; handle two builtin cases.
+ //
+ if (EventFilter.DECL_HANDLER.equals (id))
+ return this;
+ if (EventFilter.LEXICAL_HANDLER.equals (id))
+ return this;
+
+ //
+ // non-degenerate, handled by both consumers, but we don't know
+ // how to handle this.
+ //
+ throw new SAXNotRecognizedException ("can't tee: " + id);
+ }
+
+ /**
+ * Provides the error handler to both subsequent nodes of
+ * this filter stage.
+ */
+ public void setErrorHandler (ErrorHandler handler)
+ {
+ first.setErrorHandler (handler);
+ rest.setErrorHandler (handler);
+ }
+
+
+ //
+ // ContentHandler
+ //
+ public void setDocumentLocator (Locator locator)
+ {
+ // this call is not made by all parsers
+ docFirst.setDocumentLocator (locator);
+ docRest.setDocumentLocator (locator);
+ }
+
+ public void startDocument ()
+ throws SAXException
+ {
+ docFirst.startDocument ();
+ docRest.startDocument ();
+ }
+
+ public void endDocument ()
+ throws SAXException
+ {
+ try {
+ docFirst.endDocument ();
+ } finally {
+ docRest.endDocument ();
+ }
+ }
+
+ public void startPrefixMapping (String prefix, String uri)
+ throws SAXException
+ {
+ docFirst.startPrefixMapping (prefix, uri);
+ docRest.startPrefixMapping (prefix, uri);
+ }
+
+ public void endPrefixMapping (String prefix)
+ throws SAXException
+ {
+ docFirst.endPrefixMapping (prefix);
+ docRest.endPrefixMapping (prefix);
+ }
+
+ public void skippedEntity (String name)
+ throws SAXException
+ {
+ docFirst.skippedEntity (name);
+ docRest.skippedEntity (name);
+ }
+
+ public void startElement (String uri, String localName,
+ String qName, Attributes atts)
+ throws SAXException
+ {
+ docFirst.startElement (uri, localName, qName, atts);
+ docRest.startElement (uri, localName, qName, atts);
+ }
+
+ public void endElement (String uri, String localName, String qName)
+ throws SAXException
+ {
+ docFirst.endElement (uri, localName, qName);
+ docRest.endElement (uri, localName, qName);
+ }
+
+ public void processingInstruction (String target, String data)
+ throws SAXException
+ {
+ docFirst.processingInstruction (target, data);
+ docRest.processingInstruction (target, data);
+ }
+
+ public void characters (char ch [], int start, int length)
+ throws SAXException
+ {
+ docFirst.characters (ch, start, length);
+ docRest.characters (ch, start, length);
+ }
+
+ public void ignorableWhitespace (char ch [], int start, int length)
+ throws SAXException
+ {
+ docFirst.ignorableWhitespace (ch, start, length);
+ docRest.ignorableWhitespace (ch, start, length);
+ }
+
+
+ //
+ // DTDHandler
+ //
+ public void notationDecl (String name, String publicId, String systemId)
+ throws SAXException
+ {
+ DTDHandler l1 = first.getDTDHandler ();
+ DTDHandler l2 = rest.getDTDHandler ();
+
+ l1.notationDecl (name, publicId, systemId);
+ l2.notationDecl (name, publicId, systemId);
+ }
+
+ public void unparsedEntityDecl (String name,
+ String publicId, String systemId,
+ String notationName
+ ) throws SAXException
+ {
+ DTDHandler l1 = first.getDTDHandler ();
+ DTDHandler l2 = rest.getDTDHandler ();
+
+ l1.unparsedEntityDecl (name, publicId, systemId, notationName);
+ l2.unparsedEntityDecl (name, publicId, systemId, notationName);
+ }
+
+
+ //
+ // DeclHandler
+ //
+ public void attributeDecl (String eName, String aName,
+ String type,
+ String mode, String value)
+ throws SAXException
+ {
+ declFirst.attributeDecl (eName, aName, type, mode, value);
+ declRest.attributeDecl (eName, aName, type, mode, value);
+ }
+
+ public void elementDecl (String name, String model)
+ throws SAXException
+ {
+ declFirst.elementDecl (name, model);
+ declRest.elementDecl (name, model);
+ }
+
+ public void externalEntityDecl (String name,
+ String publicId, String systemId)
+ throws SAXException
+ {
+ declFirst.externalEntityDecl (name, publicId, systemId);
+ declRest.externalEntityDecl (name, publicId, systemId);
+ }
+
+ public void internalEntityDecl (String name, String value)
+ throws SAXException
+ {
+ declFirst.internalEntityDecl (name, value);
+ declRest.internalEntityDecl (name, value);
+ }
+
+
+ //
+ // LexicalHandler
+ //
+ public void comment (char ch [], int start, int length)
+ throws SAXException
+ {
+ lexFirst.comment (ch, start, length);
+ lexRest.comment (ch, start, length);
+ }
+
+ public void startCDATA ()
+ throws SAXException
+ {
+ lexFirst.startCDATA ();
+ lexRest.startCDATA ();
+ }
+
+ public void endCDATA ()
+ throws SAXException
+ {
+ lexFirst.endCDATA ();
+ lexRest.endCDATA ();
+ }
+
+ public void startEntity (String name)
+ throws SAXException
+ {
+ lexFirst.startEntity (name);
+ lexRest.startEntity (name);
+ }
+
+ public void endEntity (String name)
+ throws SAXException
+ {
+ lexFirst.endEntity (name);
+ lexRest.endEntity (name);
+ }
+
+ public void startDTD (String name, String publicId, String systemId)
+ throws SAXException
+ {
+ lexFirst.startDTD (name, publicId, systemId);
+ lexRest.startDTD (name, publicId, systemId);
+ }
+
+ public void endDTD ()
+ throws SAXException
+ {
+ lexFirst.endDTD ();
+ lexRest.endDTD ();
+ }
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/TextConsumer.java b/libjava/classpath/gnu/xml/pipeline/TextConsumer.java
new file mode 100644
index 000000000..13dcfa7f6
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/TextConsumer.java
@@ -0,0 +1,117 @@
+/* TextConsumer.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.pipeline;
+
+import java.io.*;
+
+import org.xml.sax.*;
+
+import gnu.xml.util.XMLWriter;
+
+
+/**
+ * Terminates a pipeline, consuming events to print them as well formed
+ * XML (or XHTML) text.
+ *
+ * <p> Input must be well formed, and must include XML names (e.g. the
+ * prefixes and prefix declarations must be present), or the output of
+ * this class is undefined.
+ *
+ * @see NSFilter
+ * @see WellFormednessFilter
+ *
+ * @author David Brownell
+ */
+public class TextConsumer extends XMLWriter implements EventConsumer
+{
+ /**
+ * Constructs an event consumer which echoes its input as text,
+ * optionally adhering to some basic XHTML formatting options
+ * which increase interoperability with old (v3) browsers.
+ *
+ * <p> For the best interoperability, when writing as XHTML only
+ * ASCII characters are emitted; other characters are turned to
+ * entity or character references as needed, and no XML declaration
+ * is provided in the document.
+ */
+ public TextConsumer (Writer w, boolean isXhtml)
+ throws IOException
+ {
+ super (w, isXhtml ? "US-ASCII" : null);
+ setXhtml (isXhtml);
+ }
+
+ /**
+ * Constructs a consumer that writes its input as XML text.
+ * XHTML rules are not followed.
+ */
+ public TextConsumer (Writer w)
+ throws IOException
+ {
+ this (w, false);
+ }
+
+ /**
+ * Constructs a consumer that writes its input as XML text,
+ * encoded in UTF-8. XHTML rules are not followed.
+ */
+ public TextConsumer (OutputStream out)
+ throws IOException
+ {
+ this (new OutputStreamWriter (out, "UTF8"), false);
+ }
+
+ /** <b>EventConsumer</b> Returns the document handler being used. */
+ public ContentHandler getContentHandler ()
+ { return this; }
+
+ /** <b>EventConsumer</b> Returns the dtd handler being used. */
+ public DTDHandler getDTDHandler ()
+ { return this; }
+
+ /** <b>XMLReader</b>Retrieves a property (lexical and decl handlers) */
+ public Object getProperty (String propertyId)
+ throws SAXNotRecognizedException
+ {
+ if (EventFilter.LEXICAL_HANDLER.equals (propertyId))
+ return this;
+ if (EventFilter.DECL_HANDLER.equals (propertyId))
+ return this;
+ throw new SAXNotRecognizedException (propertyId);
+ }
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/ValidationConsumer.java b/libjava/classpath/gnu/xml/pipeline/ValidationConsumer.java
new file mode 100644
index 000000000..0346984d3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/ValidationConsumer.java
@@ -0,0 +1,1928 @@
+/* ValidationConsumer.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.pipeline;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.EmptyStackException;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.Stack;
+import java.util.StringTokenizer;
+import java.util.Vector;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
+
+/**
+ * This class checks SAX2 events to report validity errors; it works as
+ * both a filter and a terminus on an event pipeline. It relies on the
+ * producer of SAX events to: </p> <ol>
+ *
+ * <li> Conform to the specification of a non-validating XML parser that
+ * reads all external entities, reported using SAX2 events. </li>
+ *
+ * <li> Report ignorable whitespace as such (through the ContentHandler
+ * interface). This is, strictly speaking, optional for nonvalidating
+ * XML processors. </li>
+ *
+ * <li> Make SAX2 DeclHandler callbacks, with default
+ * attribute values already normalized (and without "&lt;").</li>
+ *
+ * <li> Make SAX2 LexicalHandler startDTD() and endDTD ()
+ * callbacks. </li>
+ *
+ * <li> Act as if the <em>(URI)/namespace-prefixes</em> property were
+ * set to true, by providing XML 1.0 names and all <code>xmlns*</code>
+ * attributes (rather than omitting either or both). </li>
+ *
+ * </ol>
+ *
+ * <p> At this writing, the major SAX2 parsers (such as &AElig;lfred2,
+ * Crimson, and Xerces) meet these requirements, and this validation
+ * module is used by the optional &AElig;lfred2 validation support.
+ * </p>
+ *
+ * <p> Note that because this is a layered validator, it has to duplicate some
+ * work that the parser is doing; there are also other cost to layering.
+ * However, <em>because of layering it doesn't need a parser</em> in order
+ * to work! You can use it with anything that generates SAX events, such
+ * as an application component that wants to detect invalid content in
+ * a changed area without validating an entire document, or which wants to
+ * ensure that it doesn't write invalid data to a communications partner.</p>
+ *
+ * <p> Also, note that because this is a layered validator, the line numbers
+ * reported for some errors may seem strange. For example, if an element does
+ * not permit character content, the validator
+ * will use the locator provided to it.
+ * That might reflect the last character of a <em>characters</em> event
+ * callback, rather than the first non-whitespace character. </p>
+ *
+ * <hr />
+ *
+ * <!--
+ * <p> Of interest is the fact that unlike most currently known XML validators,
+ * this one can report some cases of non-determinism in element content models.
+ * It is a compile-time option, enabled by default. This will only report
+ * such XML errors if they relate to content actually appearing in a document;
+ * content models aren't aggressively scanned for non-deterministic structure.
+ * Documents which trigger such non-deterministic transitions may be handled
+ * differently by different validating parsers, without losing conformance
+ * to the XML specification. </p>
+ * -->
+ *
+ * <p> Current limitations of the validation performed are in roughly three
+ * categories. </p>
+ *
+ * <p> The first category represents constraints which demand violations
+ * of software layering: exposing lexical details, one of the first things
+ * that <em>application</em> programming interfaces (APIs) hide. These
+ * invariably relate to XML entity handling, and to historical oddities
+ * of the XML validation semantics. Curiously,
+ * recent (Autumn 1999) conformance testing showed that these constraints are
+ * among those handled worst by existing XML validating parsers. Arguments
+ * have been made that each of these VCs should be turned into WFCs (most
+ * of them) or discarded (popular for the standalone declaration); in short,
+ * that these are bugs in the XML specification (not all via SGML): </p><ul>
+ *
+ * <li> The <em>Proper Declaration/PE Nesting</em> and
+ * <em>Proper Group/PE Nesting</em> VCs can't be tested because they
+ * require access to particularly low level lexical level information.
+ * In essence, the reason XML isn't a simple thing to parse is that
+ * it's not a context free grammar, and these constraints elevate that
+ * SGML-derived context sensitivity to the level of a semantic rule.
+ *
+ * <li> The <em>Standalone Document Declaration</em> VC can't be
+ * tested. This is for two reasons. First, this flag isn't made
+ * available through SAX2. Second, it also requires breaking that
+ * lexical layering boundary. (If you ever wondered why classes
+ * in compiler construction or language design barely mention the
+ * existence of context-sensitive grammars, it's because of messy
+ * issues like these.)
+ *
+ * <li> The <em>Entity Declared</em> VC can't be tested, because it
+ * also requires breaking that lexical layering boundary! There's also
+ * another issue: the VC wording (and seemingly intent) is ambiguous.
+ * (This is still true in the "Second edition" XML spec.)
+ * Since there is a WFC of the same name, everyone's life would be
+ * easier if references to undeclared parsed entities were always well
+ * formedness errors, regardless of whether they're parameter entities
+ * or not. (Note that nonvalidating parsers are not required
+ * to report all such well formedness errors if they don't read external
+ * parameter entities, although currently most XML parsers read them
+ * in an attempt to avoid problems from inconsistent parser behavior.)
+ *
+ * </ul>
+ *
+ * <p> The second category of limitations on this validation represent
+ * constraints associated with information that is not guaranteed to be
+ * available (or in one case, <em>is guaranteed not to be available</em>,
+ * through the SAX2 API: </p><ul>
+ *
+ * <li> The <em>Unique Element Type Declaration</em> VC may not be
+ * reportable, if the underlying parser happens not to expose
+ * multiple declarations. (&AElig;lfred2 reports these validity
+ * errors directly.)</li>
+ *
+ * <li> Similarly, the <em>Unique Notation Name</em> VC, added in the
+ * 14-January-2000 XML spec errata to restrict typing models used by
+ * elements, may not be reportable. (&AElig;lfred reports these
+ * validity errors directly.) </li>
+ *
+ * </ul>
+ *
+ * <p> A third category relates to ease of implementation. (Think of this
+ * as "bugs".) The most notable issue here is character handling. Rather
+ * than attempting to implement the voluminous character tables in the XML
+ * specification (Appendix B), Unicode rules are used directly from
+ * the java.lang.Character class. Recent JVMs have begun to diverge from
+ * the original specification for that class (Unicode 2.0), meaning that
+ * different JVMs may handle that aspect of conformance differently.
+ * </p>
+ *
+ * <p> Note that for some of the validity errors that SAX2 does not
+ * expose, a nonvalidating parser is permitted (by the XML specification)
+ * to report validity errors. When used with a parser that does so for
+ * the validity constraints mentioned above (or any other SAX2 event
+ * stream producer that does the same thing), overall conformance is
+ * substantially improved.
+ *
+ * @see gnu.xml.aelfred2.SAXDriver
+ * @see gnu.xml.aelfred2.XmlReader
+ *
+ * @author David Brownell
+ */
+public final class ValidationConsumer extends EventFilter
+{
+ // report error if we happen to notice a non-deterministic choice?
+ // we won't report buggy content models; just buggy instances
+ private static final boolean warnNonDeterministic = false;
+
+ // for tracking active content models
+ private String rootName;
+ private Stack contentStack = new Stack ();
+
+ // flags for "saved DTD" processing
+ private boolean disableDeclarations;
+ private boolean disableReset;
+
+ //
+ // most VCs get tested when we see element start tags. the per-element
+ // info (including attributes) recorded here duplicates that found inside
+ // many nonvalidating parsers, hence dual lookups etc ... that's why a
+ // layered validator isn't going to be as fast as a non-layered one.
+ //
+
+ // key = element name; value = ElementInfo
+ private Hashtable elements = new Hashtable ();
+
+ // some VCs relate to ID/IDREF/IDREFS attributes
+ // key = id; value = boolean true (defd) or false (refd)
+ private Hashtable ids = new Hashtable ();
+
+ // we just record declared notation and unparsed entity names.
+ // the implementation here is simple/slow; these features
+ // are seldom used, one hopes they'll wither away soon
+ private Vector notations = new Vector (5, 5);
+ private Vector nDeferred = new Vector (5, 5);
+ private Vector unparsed = new Vector (5, 5);
+ private Vector uDeferred = new Vector (5, 5);
+
+ // note: DocBk 3.1.7 XML defines over 2 dozen notations,
+ // used when defining unparsed entities for graphics
+ // (and maybe in other places)
+
+
+
+ /**
+ * Creates a pipeline terminus which consumes all events passed to
+ * it; this will report validity errors as if they were fatal errors,
+ * unless an error handler is assigned.
+ *
+ * @see #setErrorHandler
+ */
+ // constructor used by PipelineFactory
+ // ... and want one taking system ID of an external subset
+ public ValidationConsumer ()
+ {
+ this (null);
+ }
+
+ /**
+ * Creates a pipeline filter which reports validity errors and then
+ * passes events on to the next consumer if they were not fatal.
+ *
+ * @see #setErrorHandler
+ */
+ // constructor used by PipelineFactory
+ // ... and want one taking system ID of an external subset
+ // (which won't send declaration events)
+ public ValidationConsumer (EventConsumer next)
+ {
+ super (next);
+
+ setContentHandler (this);
+ setDTDHandler (this);
+ try { setProperty (DECL_HANDLER, this); }
+ catch (Exception e) { /* "can't happen" */ }
+ try { setProperty (LEXICAL_HANDLER, this); }
+ catch (Exception e) { /* "can't happen" */ }
+ }
+
+
+ private static final String fakeRootName
+ = ":Nobody:in:their_Right.Mind_would:use:this-name:1x:";
+
+ /**
+ * Creates a validation consumer which is preloaded with the DTD provided.
+ * It does this by constructing a document with that DTD, then parsing
+ * that document and recording its DTD declarations. Then it arranges
+ * not to modify that information.
+ *
+ * <p> The resulting validation consumer will only validate against
+ * the specified DTD, regardless of whether some other DTD is found
+ * in a document being parsed.
+ *
+ * @param rootName The name of the required root element; if this is
+ * null, any root element name will be accepted.
+ * @param publicId If non-null and there is a non-null systemId, this
+ * identifier provides an alternate access identifier for the DTD's
+ * external subset.
+ * @param systemId If non-null, this is a URI (normally URL) that
+ * may be used to access the DTD's external subset.
+ * @param internalSubset If non-null, holds literal markup declarations
+ * comprising the DTD's internal subset.
+ * @param resolver If non-null, this will be provided to the parser for
+ * use when resolving parameter entities (including any external subset).
+ * @param resolver If non-null, this will be provided to the parser for
+ * use when resolving parameter entities (including any external subset).
+ * @param minimalElement If non-null, a minimal valid document.
+ *
+ * @exception SAXNotSupportedException If the default SAX parser does
+ * not support the standard lexical or declaration handlers.
+ * @exception SAXParseException If the specified DTD has either
+ * well-formedness or validity errors
+ * @exception IOException If the specified DTD can't be read for
+ * some reason
+ */
+ public ValidationConsumer (
+ String rootName,
+ String publicId,
+ String systemId,
+ String internalSubset,
+ EntityResolver resolver,
+ String minimalDocument
+ ) throws SAXException, IOException
+ {
+ this (null);
+
+ disableReset = true;
+ if (rootName == null)
+ rootName = fakeRootName;
+
+ //
+ // Synthesize document with that DTD; is it possible to do
+ // better for the declaration of the root element?
+ //
+ // NOTE: can't use SAX2 to write internal subsets.
+ //
+ StringWriter writer = new StringWriter ();
+
+ writer.write ("<!DOCTYPE ");
+ writer.write (rootName);
+ if (systemId != null) {
+ writer.write ("\n ");
+ if (publicId != null) {
+ writer.write ("PUBLIC '");
+ writer.write (publicId);
+ writer.write ("'\n\t'");
+ } else
+ writer.write ("SYSTEM '");
+ writer.write (systemId);
+ writer.write ("'");
+ }
+ writer.write (" [ ");
+ if (rootName == fakeRootName) {
+ writer.write ("\n<!ELEMENT ");
+ writer.write (rootName);
+ writer.write (" EMPTY>");
+ }
+ if (internalSubset != null)
+ writer.write (internalSubset);
+ writer.write ("\n ]>");
+
+ if (minimalDocument != null) {
+ writer.write ("\n");
+ writer.write (minimalDocument);
+ writer.write ("\n");
+ } else {
+ writer.write (" <");
+ writer.write (rootName);
+ writer.write ("/>\n");
+ }
+ minimalDocument = writer.toString ();
+
+ //
+ // OK, load it
+ //
+ XMLReader producer;
+
+ producer = XMLReaderFactory.createXMLReader ();
+ bind (producer, this);
+
+ if (resolver != null)
+ producer.setEntityResolver (resolver);
+
+ InputSource in;
+
+ in = new InputSource (new StringReader (minimalDocument));
+ producer.parse (in);
+
+ disableDeclarations = true;
+ if (rootName == fakeRootName)
+ this.rootName = null;
+ }
+
+ private void resetState ()
+ {
+ if (!disableReset) {
+ rootName = null;
+ contentStack.removeAllElements ();
+ elements.clear ();
+ ids.clear ();
+
+ notations.removeAllElements ();
+ nDeferred.removeAllElements ();
+ unparsed.removeAllElements ();
+ uDeferred.removeAllElements ();
+ }
+ }
+
+
+ private void warning (String description)
+ throws SAXException
+ {
+ ErrorHandler errHandler = getErrorHandler ();
+ Locator locator = getDocumentLocator ();
+ SAXParseException err;
+
+ if (errHandler == null)
+ return;
+
+ if (locator == null)
+ err = new SAXParseException (description, null, null, -1, -1);
+ else
+ err = new SAXParseException (description, locator);
+ errHandler.warning (err);
+ }
+
+ // package private (for ChildrenRecognizer)
+ private void error (String description)
+ throws SAXException
+ {
+ ErrorHandler errHandler = getErrorHandler ();
+ Locator locator = getDocumentLocator ();
+ SAXParseException err;
+
+ if (locator == null)
+ err = new SAXParseException (description, null, null, -1, -1);
+ else
+ err = new SAXParseException (description, locator);
+ if (errHandler != null)
+ errHandler.error (err);
+ else // else we always treat it as fatal!
+ throw err;
+ }
+
+ private void fatalError (String description)
+ throws SAXException
+ {
+ ErrorHandler errHandler = getErrorHandler ();
+ Locator locator = getDocumentLocator ();
+ SAXParseException err;
+
+ if (locator != null)
+ err = new SAXParseException (description, locator);
+ else
+ err = new SAXParseException (description, null, null, -1, -1);
+ if (errHandler != null)
+ errHandler.fatalError (err);
+ // we always treat this as fatal, regardless of the handler
+ throw err;
+ }
+
+
+ private static boolean isExtender (char c)
+ {
+ // [88] Extender ::= ...
+ return c == 0x00b7 || c == 0x02d0 || c == 0x02d1 || c == 0x0387
+ || c == 0x0640 || c == 0x0e46 || c == 0x0ec6 || c == 0x3005
+ || (c >= 0x3031 && c <= 0x3035)
+ || (c >= 0x309d && c <= 0x309e)
+ || (c >= 0x30fc && c <= 0x30fe);
+ }
+
+
+ // use augmented Unicode rules, not full XML rules
+ private boolean isName (String name, String context, String id)
+ throws SAXException
+ {
+ char buf [] = name.toCharArray ();
+ boolean pass = true;
+
+ if (!Character.isUnicodeIdentifierStart (buf [0])
+ && ":_".indexOf (buf [0]) == -1)
+ pass = false;
+ else {
+ int max = buf.length;
+ for (int i = 1; pass && i < max; i++) {
+ char c = buf [i];
+ if (!Character.isUnicodeIdentifierPart (c)
+ && ":-_.".indexOf (c) == -1
+ && !isExtender (c))
+ pass = false;
+ }
+ }
+
+ if (!pass)
+ error ("In " + context + " for " + id
+ + ", '" + name + "' is not a name");
+ return pass; // true == OK
+ }
+
+ // use augmented Unicode rules, not full XML rules
+ private boolean isNmtoken (String nmtoken, String context, String id)
+ throws SAXException
+ {
+ char buf [] = nmtoken.toCharArray ();
+ boolean pass = true;
+ int max = buf.length;
+
+ // XXX make this share code with isName
+
+ for (int i = 0; pass && i < max; i++) {
+ char c = buf [i];
+ if (!Character.isUnicodeIdentifierPart (c)
+ && ":-_.".indexOf (c) == -1
+ && !isExtender (c))
+ pass = false;
+ }
+
+ if (!pass)
+ error ("In " + context + " for " + id
+ + ", '" + nmtoken + "' is not a name token");
+ return pass; // true == OK
+ }
+
+ private void checkEnumeration (String value, String type, String name)
+ throws SAXException
+ {
+ if (!hasMatch (value, type))
+ // VC: Enumeration
+ error ("Value '" + value
+ + "' for attribute '" + name
+ + "' is not permitted: " + type);
+ }
+
+ // used to test enumerated attributes and mixed content models
+ // package private
+ static boolean hasMatch (String value, String orList)
+ {
+ int len = value.length ();
+ int max = orList.length () - len;
+
+ for (int start = 0;
+ (start = orList.indexOf (value, start)) != -1;
+ start++) {
+ char c;
+
+ if (start > max)
+ break;
+ c = orList.charAt (start - 1);
+ if (c != '|' && c != '('/*)*/)
+ continue;
+ c = orList.charAt (start + len);
+ if (c != '|' && /*(*/ c != ')')
+ continue;
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * <b>LexicalHandler</b> Records the declaration of the root
+ * element, so it can be verified later.
+ * Passed to the next consumer, unless this one was
+ * preloaded with a particular DTD.
+ */
+ public void startDTD (String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (disableDeclarations)
+ return;
+
+ rootName = name;
+ super.startDTD (name, publicId, systemId);
+ }
+
+ /**
+ * <b>LexicalHandler</b> Verifies that all referenced notations
+ * and unparsed entities have been declared.
+ * Passed to the next consumer, unless this one was
+ * preloaded with a particular DTD.
+ */
+ public void endDTD ()
+ throws SAXException
+ {
+ if (disableDeclarations)
+ return;
+
+ // this is a convenient hook for end-of-dtd checks, but we
+ // could also trigger it in the first startElement call.
+ // locator info is more appropriate here though.
+
+ // VC: Notation Declared (NDATA can refer to them before decls,
+ // as can NOTATION attribute enumerations and defaults)
+ int length = nDeferred.size ();
+ for (int i = 0; i < length; i++) {
+ String notation = (String) nDeferred.elementAt (i);
+ if (!notations.contains (notation)) {
+ error ("A declaration referred to notation '" + notation
+ + "' which was never declared");
+ }
+ }
+ nDeferred.removeAllElements ();
+
+ // VC: Entity Name (attribute values can refer to them
+ // before they're declared); VC Attribute Default Legal
+ length = uDeferred.size ();
+ for (int i = 0; i < length; i++) {
+ String entity = (String) uDeferred.elementAt (i);
+ if (!unparsed.contains (entity)) {
+ error ("An attribute default referred to entity '" + entity
+ + "' which was never declared");
+ }
+ }
+ uDeferred.removeAllElements ();
+ super.endDTD ();
+ }
+
+
+ // These are interned, so we can rely on "==" to find the type of
+ // all attributes except enumerations ...
+ // "(this|or|that|...)" and "NOTATION (this|or|that|...)"
+ static final String types [] = {
+ "CDATA",
+ "ID", "IDREF", "IDREFS",
+ "NMTOKEN", "NMTOKENS",
+ "ENTITY", "ENTITIES"
+ };
+
+
+ /**
+ * <b>DecllHandler</b> Records attribute declaration for later use
+ * in validating document content, and checks validity constraints
+ * that are applicable to attribute declarations.
+ * Passed to the next consumer, unless this one was
+ * preloaded with a particular DTD.
+ */
+ public void attributeDecl (
+ String eName,
+ String aName,
+ String type,
+ String mode,
+ String value
+ ) throws SAXException
+ {
+ if (disableDeclarations)
+ return;
+
+ ElementInfo info = (ElementInfo) elements.get (eName);
+ AttributeInfo ainfo = new AttributeInfo ();
+ boolean checkOne = false;
+ boolean interned = false;
+
+ // cheap interning of type names and #FIXED, #REQUIRED
+ // for faster startElement (we can use "==")
+ for (int i = 0; i < types.length; i++) {
+ if (types [i].equals (type)) {
+ type = types [i];
+ interned = true;
+ break;
+ }
+ }
+ if ("#FIXED".equals (mode))
+ mode = "#FIXED";
+ else if ("#REQUIRED".equals (mode))
+ mode = "#REQUIRED";
+
+ ainfo.type = type;
+ ainfo.mode = mode;
+ ainfo.value = value;
+
+ // we might not have seen the content model yet
+ if (info == null) {
+ info = new ElementInfo (eName);
+ elements.put (eName, info);
+ }
+ if ("ID" == type) {
+ checkOne = true;
+ if (!("#REQUIRED" == mode || "#IMPLIED".equals (mode))) {
+ // VC: ID Attribute Default
+ error ("ID attribute '" + aName
+ + "' must be #IMPLIED or #REQUIRED");
+ }
+
+ } else if (!interned && type.startsWith ("NOTATION ")) {
+ checkOne = true;
+
+ // VC: Notation Attributes (notations must be declared)
+ StringTokenizer tokens = new StringTokenizer (
+ type.substring (10, type.lastIndexOf (')')),
+ "|");
+ while (tokens.hasMoreTokens ()) {
+ String token = tokens.nextToken ();
+ if (!notations.contains (token))
+ nDeferred.addElement (token);
+ }
+ }
+ if (checkOne) {
+ for (Enumeration e = info.attributes.keys ();
+ e.hasMoreElements ();
+ /* NOP */) {
+ String name;
+ AttributeInfo ainfo2;
+
+ name = (String) e.nextElement ();
+ ainfo2 = (AttributeInfo) info.attributes.get (name);
+ if (type == ainfo2.type || !interned /* NOTATION */) {
+ // VC: One ID per Element Type
+ // VC: One Notation per Element TYpe
+ error ("Element '" + eName
+ + "' already has an attribute of type "
+ + (interned ? "NOTATION" : type)
+ + " ('" + name
+ + "') so '" + aName
+ + "' is a validity error");
+ }
+ }
+ }
+
+ // VC: Attribute Default Legal
+ if (value != null) {
+
+ if ("CDATA" == type) {
+ // event source rejected '<'
+
+ } else if ("NMTOKEN" == type) {
+ // VC: Name Token (is a nmtoken)
+ isNmtoken (value, "attribute default", aName);
+
+ } else if ("NMTOKENS" == type) {
+ // VC: Name Token (is a nmtoken; at least one value)
+ StringTokenizer tokens = new StringTokenizer (value);
+ if (!tokens.hasMoreTokens ())
+ error ("Default for attribute '" + aName
+ + "' must have at least one name token.");
+ else do {
+ String token = tokens.nextToken ();
+ isNmtoken (token, "attribute default", aName);
+ } while (tokens.hasMoreTokens ());
+
+ } else if ("IDREF" == type || "ENTITY" == type) {
+ // VC: Entity Name (is a name)
+ // VC: IDREF (is a name) (is declared)
+ isName (value, "attribute default", aName);
+ if ("ENTITY" == type && !unparsed.contains (value))
+ uDeferred.addElement (value);
+
+ } else if ("IDREFS" == type || "ENTITIES" == type) {
+ // VC: Entity Name (is a name; at least one value)
+ // VC: IDREF (is a name; at least one value)
+ StringTokenizer names = new StringTokenizer (value);
+ if (!names.hasMoreTokens ())
+ error ("Default for attribute '" + aName
+ + "' must have at least one name.");
+ else do {
+ String name = names.nextToken ();
+ isName (name, "attribute default", aName);
+ if ("ENTITIES" == type && !unparsed.contains (name))
+ uDeferred.addElement (value);
+ } while (names.hasMoreTokens ());
+
+ } else if (type.charAt (0) == '(' /*)*/ ) {
+ // VC: Enumeration (must match)
+ checkEnumeration (value, type, aName);
+
+ } else if (!interned && checkOne) { /* NOTATION */
+ // VC: Notation attributes (must be names)
+ isName (value, "attribute default", aName);
+
+ // VC: Notation attributes (must be declared)
+ if (!notations.contains (value))
+ nDeferred.addElement (value);
+
+ // VC: Enumeration (must match)
+ checkEnumeration (value, type, aName);
+
+ } else if ("ID" != type)
+ throw new RuntimeException ("illegal attribute type: " + type);
+ }
+
+ if (info.attributes.get (aName) == null)
+ info.attributes.put (aName, ainfo);
+ /*
+ else
+ warning ("Element '" + eName
+ + "' already has an attribute named '" + aName + "'");
+ */
+
+ if ("xml:space".equals (aName)) {
+ if (!("(default|preserve)".equals (type)
+ || "(preserve|default)".equals (type)
+ // these next two are arguable; XHTML's DTD doesn't
+ // deserve errors. After all, it's not like any
+ // illegal _value_ could pass ...
+ || "(preserve)".equals (type)
+ || "(default)".equals (type)
+ ))
+ error (
+ "xml:space attribute type must be like '(default|preserve)'"
+ + " not '" + type + "'"
+ );
+
+ }
+ super.attributeDecl (eName, aName, type, mode, value);
+ }
+
+ /**
+ * <b>DecllHandler</b> Records the element declaration for later use
+ * when checking document content, and checks validity constraints that
+ * apply to element declarations. Passed to the next consumer, unless
+ * this one was preloaded with a particular DTD.
+ */
+ public void elementDecl (String name, String model)
+ throws SAXException
+ {
+ if (disableDeclarations)
+ return;
+
+ ElementInfo info = (ElementInfo) elements.get (name);
+
+ // we might have seen an attribute decl already
+ if (info == null) {
+ info = new ElementInfo (name);
+ elements.put (name, info);
+ }
+ if (info.model != null) {
+ // NOTE: not all parsers can report such duplicates.
+ // VC: Unique Element Type Declaration
+ error ("Element type '" + name
+ + "' was already declared.");
+ } else {
+ info.model = model;
+
+ // VC: No Duplicate Types (in mixed content models)
+ if (model.charAt (1) == '#') // (#PCDATA...
+ info.getRecognizer (this);
+ }
+ super.elementDecl (name, model);
+ }
+
+ /**
+ * <b>DecllHandler</b> passed to the next consumer, unless this
+ * one was preloaded with a particular DTD
+ */
+ public void internalEntityDecl (String name, String value)
+ throws SAXException
+ {
+ if (!disableDeclarations)
+ super.internalEntityDecl (name, value);
+ }
+
+ /**
+ * <b>DecllHandler</b> passed to the next consumer, unless this
+ * one was preloaded with a particular DTD
+ */
+ public void externalEntityDecl (String name,
+ String publicId, String systemId)
+ throws SAXException
+ {
+ if (!disableDeclarations)
+ super.externalEntityDecl (name, publicId, systemId);
+ }
+
+
+ /**
+ * <b>DTDHandler</b> Records the notation name, for checking
+ * NOTATIONS attribute values and declararations of unparsed
+ * entities. Passed to the next consumer, unless this one was
+ * preloaded with a particular DTD.
+ */
+ public void notationDecl (String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (disableDeclarations)
+ return;
+
+ notations.addElement (name);
+ super.notationDecl (name, publicId, systemId);
+ }
+
+ /**
+ * <b>DTDHandler</b> Records the entity name, for checking
+ * ENTITY and ENTITIES attribute values; records the notation
+ * name if it hasn't yet been declared. Passed to the next consumer,
+ * unless this one was preloaded with a particular DTD.
+ */
+ public void unparsedEntityDecl (
+ String name,
+ String publicId,
+ String systemId,
+ String notationName
+ ) throws SAXException
+ {
+ if (disableDeclarations)
+ return;
+
+ unparsed.addElement (name);
+ if (!notations.contains (notationName))
+ nDeferred.addElement (notationName);
+ super.unparsedEntityDecl (name, publicId, systemId, notationName);
+ }
+
+
+ /**
+ * <b>ContentHandler</b> Ensures that state from any previous parse
+ * has been deleted.
+ * Passed to the next consumer.
+ */
+ public void startDocument ()
+ throws SAXException
+ {
+ resetState ();
+ super.startDocument ();
+ }
+
+
+ private static boolean isAsciiLetter (char c)
+ {
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
+ }
+
+
+ /**
+ * <b>ContentHandler</b> Reports a fatal exception. Validating
+ * XML processors may not skip any entities.
+ */
+ public void skippedEntity (String name)
+ throws SAXException
+ {
+ fatalError ("may not skip entities");
+ }
+
+ /*
+ * SAX2 doesn't expand non-PE refs in attribute defaults...
+ */
+ private String expandDefaultRefs (String s)
+ throws SAXException
+ {
+ if (s.indexOf ('&') < 0)
+ return s;
+
+// FIXME: handle &#nn; &#xnn; &name;
+ String message = "Can't expand refs in attribute default: " + s;
+ warning (message);
+
+ return s;
+ }
+
+ /**
+ * <b>ContentHandler</b> Performs validity checks against element
+ * (and document) content models, and attribute values.
+ * Passed to the next consumer.
+ */
+ public void startElement (
+ String uri,
+ String localName,
+ String qName,
+ Attributes atts
+ ) throws SAXException
+ {
+ //
+ // First check content model for the enclosing scope.
+ //
+ if (contentStack.isEmpty ()) {
+ // VC: Root Element Type
+ if (!qName.equals (rootName)) {
+ if (rootName == null)
+ warning ("This document has no DTD, can't be valid");
+ else
+ error ("Root element type '" + qName
+ + "' was declared to be '" + rootName + "'");
+ }
+ } else {
+ Recognizer state = (Recognizer) contentStack.peek ();
+
+ if (state != null) {
+ Recognizer newstate = state.acceptElement (qName);
+
+ if (newstate == null)
+ error ("Element type '" + qName
+ + "' in element '" + state.type.name
+ + "' violates content model " + state.type.model
+ );
+ if (newstate != state) {
+ contentStack.pop ();
+ contentStack.push (newstate);
+ }
+ }
+ }
+
+ //
+ // Then check that this element was declared, and push the
+ // object used to validate its content model onto our stack.
+ //
+ // This is where the recognizer gets created, if needed; if
+ // it's a "children" (elements) content model, an NDFA is
+ // created. (One recognizer is used per content type, no
+ // matter how complex that recognizer is.)
+ //
+ ElementInfo info;
+
+ info = (ElementInfo) elements.get (qName);
+ if (info == null || info.model == null) {
+ // VC: Element Valid (base clause)
+ error ("Element type '" + qName + "' was not declared");
+ contentStack.push (null);
+
+ // for less diagnostic noise, fake a declaration.
+ elementDecl (qName, "ANY");
+ } else
+ contentStack.push (info.getRecognizer (this));
+
+ //
+ // Then check each attribute present
+ //
+ int len;
+ String aname;
+ AttributeInfo ainfo;
+
+ if (atts != null)
+ len = atts.getLength ();
+ else
+ len = 0;
+
+ for (int i = 0; i < len; i++) {
+ aname = atts.getQName (i);
+
+ if (info == null
+ || (ainfo = (AttributeInfo) info.attributes.get (aname))
+ == null) {
+ // VC: Attribute Value Type
+ error ("Attribute '" + aname
+ + "' was not declared for element type " + qName);
+ continue;
+ }
+
+ String value = atts.getValue (i);
+
+ // note that "==" for type names and "#FIXED" is correct
+ // (and fast) since we've interned those literals.
+
+ if ("#FIXED" == ainfo.mode) {
+ String expanded = expandDefaultRefs (ainfo.value);
+
+ // VC: Fixed Attribute Default
+ if (!value.equals (expanded)) {
+ error ("Attribute '" + aname
+ + "' must match " + expanded
+ );
+ continue;
+ }
+ }
+
+ if ("CDATA" == ainfo.type)
+ continue;
+
+ //
+ // For all other attribute types, there are various
+ // rules to follow.
+ //
+
+ if ("ID" == ainfo.type) {
+ // VC: ID (must be a name)
+ if (isName (value, "ID attribute", aname)) {
+ if (Boolean.TRUE == ids.get (value))
+ // VC: ID (appears once)
+ error ("ID attribute " + aname
+ + " uses an ID value '" + value
+ + "' which was already declared.");
+ else
+ // any forward refs are no longer problems
+ ids.put (value, Boolean.TRUE);
+ }
+ continue;
+ }
+
+ if ("IDREF" == ainfo.type) {
+ // VC: IDREF (value must be a name)
+ if (isName (value, "IDREF attribute", aname)) {
+ // VC: IDREF (must match some ID attribute)
+ if (ids.get (value) == null)
+ // new -- assume it's a forward ref
+ ids.put (value, Boolean.FALSE);
+ }
+ continue;
+ }
+
+ if ("IDREFS" == ainfo.type) {
+ StringTokenizer tokens = new StringTokenizer (value, " ");
+
+ if (!tokens.hasMoreTokens ()) {
+ // VC: IDREF (one or more values)
+ error ("IDREFS attribute " + aname
+ + " must have at least one ID ref");
+ } else do {
+ String id = tokens.nextToken ();
+
+ // VC: IDREF (value must be a name)
+ if (isName (id, "IDREFS attribute", aname)) {
+ // VC: IDREF (must match some ID attribute)
+ if (ids.get (id) == null)
+ // new -- assume it's a forward ref
+ ids.put (id, Boolean.FALSE);
+ }
+ } while (tokens.hasMoreTokens ());
+ continue;
+ }
+
+ if ("NMTOKEN" == ainfo.type) {
+ // VC: Name Token (is a name token)
+ isNmtoken (value, "NMTOKEN attribute", aname);
+ continue;
+ }
+
+ if ("NMTOKENS" == ainfo.type) {
+ StringTokenizer tokens = new StringTokenizer (value, " ");
+
+ if (!tokens.hasMoreTokens ()) {
+ // VC: Name Token (one or more values)
+ error ("NMTOKENS attribute " + aname
+ + " must have at least one name token");
+ } else do {
+ String token = tokens.nextToken ();
+
+ // VC: Name Token (is a name token)
+ isNmtoken (token, "NMTOKENS attribute", aname);
+ } while (tokens.hasMoreTokens ());
+ continue;
+ }
+
+ if ("ENTITY" == ainfo.type) {
+ if (!unparsed.contains (value))
+ // VC: Entity Name
+ error ("Value of attribute '" + aname
+ + "' refers to unparsed entity '" + value
+ + "' which was not declared.");
+ continue;
+ }
+
+ if ("ENTITIES" == ainfo.type) {
+ StringTokenizer tokens = new StringTokenizer (value, " ");
+
+ if (!tokens.hasMoreTokens ()) {
+ // VC: Entity Name (one or more values)
+ error ("ENTITIES attribute " + aname
+ + " must have at least one name token");
+ } else do {
+ String entity = tokens.nextToken ();
+
+ if (!unparsed.contains (entity))
+ // VC: Entity Name
+ error ("Value of attribute '" + aname
+ + "' refers to unparsed entity '" + entity
+ + "' which was not declared.");
+ } while (tokens.hasMoreTokens ());
+ continue;
+ }
+
+ //
+ // check for enumerations last; more expensive
+ //
+ if (ainfo.type.charAt (0) == '(' /*)*/
+ || ainfo.type.startsWith ("NOTATION ")
+ ) {
+ // VC: Enumeration (value must be defined)
+ checkEnumeration (value, ainfo.type, aname);
+ continue;
+ }
+ }
+
+ //
+ // Last, check that all #REQUIRED attributes were provided
+ //
+ if (info != null) {
+ Hashtable table = info.attributes;
+
+ if (table.size () != 0) {
+ Enumeration e = table.keys ();
+
+ // XXX table.keys uses the heap, bleech -- slows things
+
+ while (e.hasMoreElements ()) {
+ aname = (String) e.nextElement ();
+ ainfo = (AttributeInfo) table.get (aname);
+
+ // "#REQUIRED" mode was interned in attributeDecl
+ if ("#REQUIRED" == ainfo.mode
+ && atts.getValue (aname) == null) {
+ // VC: Required Attribute
+ error ("Attribute '" + aname + "' must be specified "
+ + "for element type " + qName);
+ }
+ }
+ }
+ }
+ super.startElement (uri, localName, qName, atts);
+ }
+
+ /**
+ * <b>ContentHandler</b> Reports a validity error if the element's content
+ * model does not permit character data.
+ * Passed to the next consumer.
+ */
+ public void characters (char ch [], int start, int length)
+ throws SAXException
+ {
+ Recognizer state;
+
+ if (contentStack.empty ())
+ state = null;
+ else
+ state = (Recognizer) contentStack.peek ();
+
+ // NOTE: if this ever supports with SAX parsers that don't
+ // report ignorable whitespace as such (only XP?), this class
+ // needs to morph it into ignorableWhitespace() as needed ...
+
+ if (state != null && !state.acceptCharacters ())
+ // VC: Element Valid (clauses three, four -- see recognizer)
+ error ("Character content not allowed in element "
+ + state.type.name);
+
+ super.characters (ch, start, length);
+ }
+
+
+ /**
+ * <b>ContentHandler</b> Reports a validity error if the element's content
+ * model does not permit end-of-element yet, or a well formedness error
+ * if there was no matching startElement call.
+ * Passed to the next consumer.
+ */
+ public void endElement (String uri, String localName, String qName)
+ throws SAXException
+ {
+ try {
+ Recognizer state = (Recognizer) contentStack.pop ();
+
+ if (state != null && !state.completed ())
+ // VC: Element valid (clauses two, three, four; see Recognizer)
+ error ("Premature end for element '"
+ + state.type.name
+ + "', content model "
+ + state.type.model);
+
+ // could insist on match of start element, but that's
+ // something the input stream must to guarantee.
+
+ } catch (EmptyStackException e) {
+ fatalError ("endElement without startElement: " + qName
+ + ((uri == null)
+ ? ""
+ : ( " { '" + uri + "', " + localName + " }")));
+ }
+ super.endElement (uri, localName, qName);
+ }
+
+ /**
+ * <b>ContentHandler</b> Checks whether all ID values that were
+ * referenced have been declared, and releases all resources.
+ * Passed to the next consumer.
+ *
+ * @see #setDocumentLocator
+ */
+ public void endDocument ()
+ throws SAXException
+ {
+ for (Enumeration idNames = ids.keys ();
+ idNames.hasMoreElements ();
+ /* NOP */) {
+ String id = (String) idNames.nextElement ();
+
+ if (Boolean.FALSE == ids.get (id)) {
+ // VC: IDREF (must match ID)
+ error ("Undeclared ID value '" + id
+ + "' was referred to by an IDREF/IDREFS attribute");
+ }
+ }
+
+ resetState ();
+ super.endDocument ();
+ }
+
+
+ /** Holds per-element declarations */
+ static private final class ElementInfo
+ {
+ String name;
+ String model;
+
+ // key = attribute name; value = AttributeInfo
+ Hashtable attributes = new Hashtable (11);
+
+ ElementInfo (String n) { name = n; }
+
+ private Recognizer recognizer;
+
+ // for validating content models: one per type, shared,
+ // and constructed only on demand ... so unused elements do
+ // not need to consume resources.
+ Recognizer getRecognizer (ValidationConsumer consumer)
+ throws SAXException
+ {
+ if (recognizer == null) {
+ if ("ANY".equals (model))
+ recognizer = ANY;
+ else if ("EMPTY".equals (model))
+ recognizer = new EmptyRecognizer (this);
+ else if ('#' == model.charAt (1))
+ // n.b. this constructor does a validity check
+ recognizer = new MixedRecognizer (this, consumer);
+ else
+ recognizer = new ChildrenRecognizer (this, consumer);
+ }
+ return recognizer;
+ }
+ }
+
+ /** Holds per-attribute declarations */
+ static private final class AttributeInfo
+ {
+ String type;
+ String mode; // #REQUIRED, etc (or null)
+ String value; // or null
+ }
+
+
+ //
+ // Content model validation
+ //
+
+ static private final Recognizer ANY = new Recognizer (null);
+
+
+ // Base class defines the calls used to validate content,
+ // and supports the "ANY" content model
+ static private class Recognizer
+ {
+ final ElementInfo type;
+
+ Recognizer (ElementInfo t) { type = t; }
+
+ // return true iff character data is legal here
+ boolean acceptCharacters ()
+ throws SAXException
+ // VC: Element Valid (third and fourth clauses)
+ { return true; }
+
+ // null return = failure
+ // otherwise, next state (like an FSM)
+ // prerequisite: tested that name was declared
+ Recognizer acceptElement (String name)
+ throws SAXException
+ // VC: Element Valid (fourth clause)
+ { return this; }
+
+ // return true iff model is completed, can finish
+ boolean completed ()
+ throws SAXException
+ // VC: Element Valid (fourth clause)
+ { return true; }
+
+ public String toString ()
+ // n.b. "children" is the interesting case!
+ { return (type == null) ? "ANY" : type.model; }
+ }
+
+ // "EMPTY" content model -- no characters or elements
+ private static final class EmptyRecognizer extends Recognizer
+ {
+ public EmptyRecognizer (ElementInfo type)
+ { super (type); }
+
+ // VC: Element Valid (first clause)
+ boolean acceptCharacters ()
+ { return false; }
+
+ // VC: Element Valid (first clause)
+ Recognizer acceptElement (String name)
+ { return null; }
+ }
+
+ // "Mixed" content model -- ANY, but restricts elements
+ private static final class MixedRecognizer extends Recognizer
+ {
+ private String permitted [];
+
+ // N.B. constructor tests for duplicated element names (VC)
+ public MixedRecognizer (ElementInfo t, ValidationConsumer v)
+ throws SAXException
+ {
+ super (t);
+
+ // (#PCDATA...)* or (#PCDATA) ==> ... or empty
+ // with the "..." being "|elname|..."
+ StringTokenizer tokens = new StringTokenizer (
+ t.model.substring (8, t.model.lastIndexOf (')')),
+ "|");
+ Vector vec = new Vector ();
+
+ while (tokens.hasMoreTokens ()) {
+ String token = tokens.nextToken ();
+
+ if (vec.contains (token))
+ v.error ("element " + token
+ + " is repeated in mixed content model: "
+ + t.model);
+ else
+ vec.addElement (token.intern ());
+ }
+ permitted = new String [vec.size ()];
+ for (int i = 0; i < permitted.length; i++)
+ permitted [i] = (String) vec.elementAt (i);
+
+ // in one large machine-derived DTD sample, most of about
+ // 250 mixed content models were empty, and 25 had ten or
+ // more entries. 2 had over a hundred elements. Linear
+ // search isn't obviously wrong.
+ }
+
+ // VC: Element Valid (third clause)
+ Recognizer acceptElement (String name)
+ {
+ int length = permitted.length;
+
+ // first pass -- optimistic w.r.t. event source interning
+ // (and document validity)
+ for (int i = 0; i < length; i++)
+ if (permitted [i] == name)
+ return this;
+ // second pass -- pessimistic w.r.t. event source interning
+ for (int i = 0; i < length; i++)
+ if (permitted [i].equals (name))
+ return this;
+ return null;
+ }
+ }
+
+
+ // recognizer loop flags, see later
+ private static final int F_LOOPHEAD = 0x01;
+ private static final int F_LOOPNEXT = 0x02;
+
+ // for debugging -- used to label/count nodes in toString()
+ private static int nodeCount;
+
+ /**
+ * "Children" content model -- these are nodes in NDFA state graphs.
+ * They work in fixed space. Note that these graphs commonly have
+ * cycles, handling features such as zero-or-more and one-or-more.
+ *
+ * <p>It's readonly, so only one copy is ever needed. The content model
+ * stack may have any number of pointers into each graph, when a model
+ * happens to be needed more than once due to element nesting. Since
+ * traversing the graph just moves to another node, and never changes
+ * it, traversals never interfere with each other.
+ *
+ * <p>There is an option to report non-deterministic models. These are
+ * always XML errors, but ones which are not often reported despite the
+ * fact that they can lead to different validating parsers giving
+ * different results for the same input. (The XML spec doesn't require
+ * them to be reported.)
+ *
+ * <p><b>FIXME</b> There's currently at least one known bug here, in that
+ * it's not actually detecting the non-determinism it tries to detect.
+ * (Of the "optional.xml" test, the once-or-twice-2* tests are all non-D;
+ * maybe some others.) This may relate to the issue flagged below as
+ * "should not" happen (but it was), which showed up when patching the
+ * graph to have one exit node (or more EMPTY nodes).
+ */
+ private static final class ChildrenRecognizer extends Recognizer
+ implements Cloneable
+ {
+ // for reporting non-deterministic content models
+ // ... a waste of space if we're not reporting those!
+ // ... along with the 'model' member (in base class)
+ private ValidationConsumer consumer;
+
+ // for CHOICE nodes -- each component is an arc that
+ // accepts a different NAME (or is EMPTY indicating
+ // NDFA termination).
+ private Recognizer components [];
+
+ // for NAME/SEQUENCE nodes -- accepts that NAME and
+ // then goes to the next node (CHOICE, NAME, EMPTY).
+ private String name;
+ private Recognizer next;
+
+ // loops always point back to a CHOICE node. we mark such choice
+ // nodes (F_LOOPHEAD) for diagnostics and faster deep cloning.
+ // We also mark nodes before back pointers (F_LOOPNEXT), to ensure
+ // termination when we patch sequences and loops.
+ private int flags;
+
+
+ // prevent a needless indirection between 'this' and 'node'
+ private void copyIn (ChildrenRecognizer node)
+ {
+ // model & consumer are already set
+ components = node.components;
+ name = node.name;
+ next = node.next;
+ flags = node.flags;
+ }
+
+ // used to construct top level "children" content models,
+ public ChildrenRecognizer (ElementInfo type, ValidationConsumer vc)
+ {
+ this (vc, type);
+ populate (type.model.toCharArray (), 0);
+ patchNext (new EmptyRecognizer (type), null);
+ }
+
+ // used internally; populating is separate
+ private ChildrenRecognizer (ValidationConsumer vc, ElementInfo type)
+ {
+ super (type);
+ consumer = vc;
+ }
+
+
+ //
+ // When rewriting some graph nodes we need deep clones in one case;
+ // mostly shallow clones (what the JVM handles for us) are fine.
+ //
+ private ChildrenRecognizer shallowClone ()
+ {
+ try {
+ return (ChildrenRecognizer) clone ();
+ } catch (CloneNotSupportedException e) {
+ throw new Error ("clone");
+ }
+ }
+
+ private ChildrenRecognizer deepClone ()
+ {
+ return deepClone (new Hashtable (37));
+ }
+
+ private ChildrenRecognizer deepClone (Hashtable table)
+ {
+ ChildrenRecognizer retval;
+
+ if ((flags & F_LOOPHEAD) != 0) {
+ retval = (ChildrenRecognizer) table.get (this);
+ if (retval != null)
+ return this;
+
+ retval = shallowClone ();
+ table.put (this, retval);
+ } else
+ retval = shallowClone ();
+
+ if (next != null) {
+ if (next instanceof ChildrenRecognizer)
+ retval.next = ((ChildrenRecognizer)next)
+ .deepClone (table);
+ else if (!(next instanceof EmptyRecognizer))
+ throw new RuntimeException ("deepClone");
+ }
+
+ if (components != null) {
+ retval.components = new Recognizer [components.length];
+ for (int i = 0; i < components.length; i++) {
+ Recognizer temp = components [i];
+
+ if (temp == null)
+ retval.components [i] = null;
+ else if (temp instanceof ChildrenRecognizer)
+ retval.components [i] = ((ChildrenRecognizer)temp)
+ .deepClone (table);
+ else if (!(temp instanceof EmptyRecognizer))
+ throw new RuntimeException ("deepClone");
+ }
+ }
+
+ return retval;
+ }
+
+ // connect subgraphs, first to next (sequencing)
+ private void patchNext (Recognizer theNext, Hashtable table)
+ {
+ // backpointers must not be repatched or followed
+ if ((flags & F_LOOPNEXT) != 0)
+ return;
+
+ // XXX this table "shouldn't" be needed, right?
+ // but some choice nodes looped if it isn't there.
+ if (table != null && table.get (this) != null)
+ return;
+ if (table == null)
+ table = new Hashtable ();
+
+ // NAME/SEQUENCE
+ if (name != null) {
+ if (next == null)
+ next = theNext;
+ else if (next instanceof ChildrenRecognizer) {
+ ((ChildrenRecognizer)next).patchNext (theNext, table);
+ } else if (!(next instanceof EmptyRecognizer))
+ throw new RuntimeException ("patchNext");
+ return;
+ }
+
+ // CHOICE
+ for (int i = 0; i < components.length; i++) {
+ if (components [i] == null)
+ components [i] = theNext;
+ else if (components [i] instanceof ChildrenRecognizer) {
+ ((ChildrenRecognizer)components [i])
+ .patchNext (theNext, table);
+ } else if (!(components [i] instanceof EmptyRecognizer))
+ throw new RuntimeException ("patchNext");
+ }
+
+ if (table != null && (flags & F_LOOPHEAD) != 0)
+ table.put (this, this);
+ }
+
+ /**
+ * Parses a 'children' spec (or recursively 'cp') and makes this
+ * become a regular graph node.
+ *
+ * @return index after this particle
+ */
+ private int populate (char parseBuf [], int startPos)
+ {
+ int nextPos = startPos + 1;
+ char c;
+
+ if (nextPos < 0 || nextPos >= parseBuf.length)
+ throw new IndexOutOfBoundsException ();
+
+ // Grammar of the string is from the XML spec, but
+ // with whitespace removed by the SAX parser.
+
+ // children ::= (choice | seq) ('?' | '*' | '+')?
+ // cp ::= (Name | choice | seq) ('?' | '*' | '+')?
+ // choice ::= '(' cp ('|' choice)* ')'
+ // seq ::= '(' cp (',' choice)* ')'
+
+ // interior nodes only
+ // cp ::= name ...
+ if (parseBuf [startPos] != '('/*)*/) {
+ boolean done = false;
+ do {
+ switch (c = parseBuf [nextPos]) {
+ case '?': case '*': case '+':
+ case '|': case ',':
+ case /*(*/ ')':
+ done = true;
+ continue;
+ default:
+ nextPos++;
+ continue;
+ }
+ } while (!done);
+ name = new String (parseBuf, startPos, nextPos - startPos);
+
+ // interior OR toplevel nodes
+ // cp ::= choice ..
+ // cp ::= seq ..
+ } else {
+ // collect everything as a separate list, and merge it
+ // into "this" later if we can (SEQUENCE or singleton)
+ ChildrenRecognizer first;
+
+ first = new ChildrenRecognizer (consumer, type);
+ nextPos = first.populate (parseBuf, nextPos);
+ c = parseBuf [nextPos++];
+
+ if (c == ',' || c == '|') {
+ ChildrenRecognizer current = first;
+ char separator = c;
+ Vector v = null;
+
+ if (separator == '|') {
+ v = new Vector ();
+ v.addElement (first);
+ }
+
+ do {
+ ChildrenRecognizer link;
+
+ link = new ChildrenRecognizer (consumer, type);
+ nextPos = link.populate (parseBuf, nextPos);
+
+ if (separator == ',') {
+ current.patchNext (link, null);
+ current = link;
+ } else
+ v.addElement (link);
+
+ c = parseBuf [nextPos++];
+ } while (c == separator);
+
+ // choice ... collect everything into one array.
+ if (separator == '|') {
+ // assert v.size() > 1
+ components = new Recognizer [v.size ()];
+ for (int i = 0; i < components.length; i++) {
+ components [i] = (Recognizer)
+ v.elementAt (i);
+ }
+ // assert flags == 0
+
+ // sequence ... merge into "this" to be smaller.
+ } else
+ copyIn (first);
+
+ // treat singletons like one-node sequences.
+ } else
+ copyIn (first);
+
+ if (c != /*(*/ ')')
+ throw new RuntimeException ("corrupt content model");
+ }
+
+ //
+ // Arity is optional, and the root of all fun. We keep the
+ // FSM state graph simple by only having NAME/SEQUENCE and
+ // CHOICE nodes (or EMPTY to terminate a model), easily
+ // evaluated. So we rewrite each node that has arity, using
+ // those primitives. We create loops here, if needed.
+ //
+ if (nextPos < parseBuf.length) {
+ c = parseBuf [nextPos];
+ if (c == '?' || c == '*' || c == '+') {
+ nextPos++;
+
+ // Rewrite 'zero-or-one' "?" arity to a CHOICE:
+ // - SEQUENCE (clone, what's next)
+ // - or, what's next
+ // Size cost: N --> N + 1
+ if (c == '?') {
+ Recognizer once = shallowClone ();
+
+ components = new Recognizer [2];
+ components [0] = once;
+ // components [1] initted to null
+ name = null;
+ next = null;
+ flags = 0;
+
+
+ // Rewrite 'zero-or-more' "*" arity to a CHOICE.
+ // - LOOP (clone, back to this CHOICE)
+ // - or, what's next
+ // Size cost: N --> N + 1
+ } else if (c == '*') {
+ ChildrenRecognizer loop = shallowClone ();
+
+ loop.patchNext (this, null);
+ loop.flags |= F_LOOPNEXT;
+ flags = F_LOOPHEAD;
+
+ components = new Recognizer [2];
+ components [0] = loop;
+ // components [1] initted to null
+ name = null;
+ next = null;
+
+
+ // Rewrite 'one-or-more' "+" arity to a SEQUENCE.
+ // Basically (a)+ --> ((a),(a)*).
+ // - this
+ // - CHOICE
+ // * LOOP (clone, back to the CHOICE)
+ // * or, whatever's next
+ // Size cost: N --> 2N + 1
+ } else if (c == '+') {
+ ChildrenRecognizer loop = deepClone ();
+ ChildrenRecognizer choice;
+
+ choice = new ChildrenRecognizer (consumer, type);
+ loop.patchNext (choice, null);
+ loop.flags |= F_LOOPNEXT;
+ choice.flags = F_LOOPHEAD;
+
+ choice.components = new Recognizer [2];
+ choice.components [0] = loop;
+ // choice.components [1] initted to null
+ // choice.name, choice.next initted to null
+
+ patchNext (choice, null);
+ }
+ }
+ }
+
+ return nextPos;
+ }
+
+ // VC: Element Valid (second clause)
+ boolean acceptCharacters ()
+ { return false; }
+
+ // VC: Element Valid (second clause)
+ Recognizer acceptElement (String type)
+ throws SAXException
+ {
+ // NAME/SEQUENCE
+ if (name != null) {
+ if (name.equals (type))
+ return next;
+ return null;
+ }
+
+ // CHOICE ... optionally reporting nondeterminism we
+ // run across. we won't check out every transition
+ // for nondeterminism; only the ones we follow.
+ Recognizer retval = null;
+
+ for (int i = 0; i < components.length; i++) {
+ Recognizer temp = components [i].acceptElement (type);
+
+ if (temp == null)
+ continue;
+ else if (!warnNonDeterministic)
+ return temp;
+ else if (retval == null)
+ retval = temp;
+ else if (retval != temp)
+ consumer.error ("Content model " + this.type.model
+ + " is non-deterministic for " + type);
+ }
+ return retval;
+ }
+
+ // VC: Element Valid (second clause)
+ boolean completed ()
+ throws SAXException
+ {
+ // expecting a specific element
+ if (name != null)
+ return false;
+
+ // choice, some sequences
+ for (int i = 0; i < components.length; i++) {
+ if (components [i].completed ())
+ return true;
+ }
+
+ return false;
+ }
+
+/** /
+ // FOR DEBUGGING ... flattens the graph for printing.
+
+ public String toString ()
+ {
+ StringBuffer buf = new StringBuffer ();
+
+ // only one set of loop labels can be generated
+ // at a time...
+ synchronized (ANY) {
+ nodeCount = 0;
+
+ toString (buf, new Hashtable ());
+ return buf.toString ();
+ }
+ }
+
+ private void toString (StringBuffer buf, Hashtable table)
+ {
+ // When we visit a node, label and count it.
+ // Nodes are never visited/counted more than once.
+ // For small models labels waste space, but if arity
+ // mappings were used the savings are substantial.
+ // (Plus, the output can be more readily understood.)
+ String temp = (String) table.get (this);
+
+ if (temp != null) {
+ buf.append ('{');
+ buf.append (temp);
+ buf.append ('}');
+ return;
+ } else {
+ StringBuffer scratch = new StringBuffer (15);
+
+ if ((flags & F_LOOPHEAD) != 0)
+ scratch.append ("loop");
+ else
+ scratch.append ("node");
+ scratch.append ('-');
+ scratch.append (++nodeCount);
+ temp = scratch.toString ();
+
+ table.put (this, temp);
+ buf.append ('[');
+ buf.append (temp);
+ buf.append (']');
+ buf.append (':');
+ }
+
+ // NAME/SEQUENCE
+ if (name != null) {
+ // n.b. some output encodings turn some name chars into '?'
+ // e.g. with Japanese names and ASCII output
+ buf.append (name);
+ if (components != null) // bug!
+ buf.append ('$');
+ if (next == null)
+ buf.append (",*");
+ else if (next instanceof EmptyRecognizer) // patch-to-next
+ buf.append (",{}");
+ else if (next instanceof ChildrenRecognizer) {
+ buf.append (',');
+ ((ChildrenRecognizer)next).toString (buf, table);
+ } else // bug!
+ buf.append (",+");
+ return;
+ }
+
+ // CHOICE
+ buf.append ("<");
+ for (int i = 0; i < components.length; i++) {
+ if (i != 0)
+ buf.append ("|");
+ if (components [i] instanceof EmptyRecognizer) {
+ buf.append ("{}");
+ } else if (components [i] == null) { // patch-to-next
+ buf.append ('*');
+ } else {
+ ChildrenRecognizer r;
+
+ r = (ChildrenRecognizer) components [i];
+ r.toString (buf, table);
+ }
+ }
+ buf.append (">");
+ }
+/**/
+ }
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/WellFormednessFilter.java b/libjava/classpath/gnu/xml/pipeline/WellFormednessFilter.java
new file mode 100644
index 000000000..7a3db6593
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/WellFormednessFilter.java
@@ -0,0 +1,363 @@
+/* WellFormednessFilter.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.pipeline;
+
+import java.util.EmptyStackException;
+import java.util.Stack;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+/**
+ * This filter reports fatal exceptions in the case of event streams that
+ * are not well formed. The rules currently tested include: <ul>
+ *
+ * <li>setDocumentLocator ... may be called only before startDocument
+ *
+ * <li>startDocument/endDocument ... must be paired, and all other
+ * calls (except setDocumentLocator) must be nested within these.
+ *
+ * <li>startElement/endElement ... must be correctly paired, and
+ * may never appear within CDATA sections.
+ *
+ * <li>comment ... can't contain "--"
+ *
+ * <li>character data ... can't contain "]]&gt;"
+ *
+ * <li>whitespace ... can't contain CR
+ *
+ * <li>whitespace and character data must be within an element
+ *
+ * <li>processing instruction ... can't contain "?&gt;" or CR
+ *
+ * <li>startCDATA/endCDATA ... must be correctly paired.
+ *
+ * </ul>
+ *
+ * <p> Other checks for event stream correctness may be provided in
+ * the future. For example, insisting that
+ * entity boundaries nest correctly,
+ * namespace scopes nest correctly,
+ * namespace values never contain relative URIs,
+ * attributes don't have "&lt;" characters;
+ * and more.
+ *
+ * @author David Brownell
+ */
+public final class WellFormednessFilter extends EventFilter
+{
+ private boolean startedDoc;
+ private Stack elementStack = new Stack ();
+ private boolean startedCDATA;
+ private String dtdState = "before";
+
+
+ /**
+ * Swallows all events after performing well formedness checks.
+ */
+ // constructor used by PipelineFactory
+ public WellFormednessFilter ()
+ { this (null); }
+
+
+ /**
+ * Passes events through to the specified consumer, after first
+ * processing them.
+ */
+ // constructor used by PipelineFactory
+ public WellFormednessFilter (EventConsumer consumer)
+ {
+ super (consumer);
+
+ setContentHandler (this);
+ setDTDHandler (this);
+
+ try {
+ setProperty (LEXICAL_HANDLER, this);
+ } catch (SAXException e) { /* can't happen */ }
+ }
+
+ /**
+ * Resets state as if any preceding event stream was well formed.
+ * Particularly useful if it ended through some sort of error,
+ * and the endDocument call wasn't made.
+ */
+ public void reset ()
+ {
+ startedDoc = false;
+ startedCDATA = false;
+ elementStack.removeAllElements ();
+ }
+
+
+ private SAXParseException getException (String message)
+ {
+ SAXParseException e;
+ Locator locator = getDocumentLocator ();
+
+ if (locator == null)
+ return new SAXParseException (message, null, null, -1, -1);
+ else
+ return new SAXParseException (message, locator);
+ }
+
+ private void fatalError (String message)
+ throws SAXException
+ {
+ SAXParseException e = getException (message);
+ ErrorHandler handler = getErrorHandler ();
+
+ if (handler != null)
+ handler.fatalError (e);
+ throw e;
+ }
+
+ /**
+ * Throws an exception when called after startDocument.
+ *
+ * @param locator the locator, to be used in error reporting or relative
+ * URI resolution.
+ *
+ * @exception IllegalStateException when called after the document
+ * has already been started
+ */
+ public void setDocumentLocator (Locator locator)
+ {
+ if (startedDoc)
+ throw new IllegalStateException (
+ "setDocumentLocator called after startDocument");
+ super.setDocumentLocator (locator);
+ }
+
+ public void startDocument () throws SAXException
+ {
+ if (startedDoc)
+ fatalError ("startDocument called more than once");
+ startedDoc = true;
+ startedCDATA = false;
+ elementStack.removeAllElements ();
+ super.startDocument ();
+ }
+
+ public void startElement (
+ String uri, String localName,
+ String qName, Attributes atts
+ ) throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ if ("inside".equals (dtdState))
+ fatalError ("element inside DTD?");
+ else
+ dtdState = "after";
+ if (startedCDATA)
+ fatalError ("element inside CDATA section");
+ if (qName == null || "".equals (qName))
+ fatalError ("startElement name missing");
+ elementStack.push (qName);
+ super.startElement (uri, localName, qName, atts);
+ }
+
+ public void endElement (String uri, String localName, String qName)
+ throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ if (startedCDATA)
+ fatalError ("element inside CDATA section");
+ if (qName == null || "".equals (qName))
+ fatalError ("endElement name missing");
+
+ try {
+ String top = (String) elementStack.pop ();
+
+ if (!qName.equals (top))
+ fatalError ("<" + top + " ...>...</" + qName + ">");
+ // XXX could record/test namespace info
+ } catch (EmptyStackException e) {
+ fatalError ("endElement without startElement: </" + qName + ">");
+ }
+ super.endElement (uri, localName, qName);
+ }
+
+ public void endDocument () throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ dtdState = "before";
+ startedDoc = false;
+ super.endDocument ();
+ }
+
+
+ public void startDTD (String root, String publicId, String systemId)
+ throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ if ("before" != dtdState)
+ fatalError ("two DTDs?");
+ if (!elementStack.empty ())
+ fatalError ("DTD must precede root element");
+ dtdState = "inside";
+ super.startDTD (root, publicId, systemId);
+ }
+
+ public void notationDecl (String name, String publicId, String systemId)
+ throws SAXException
+ {
+// FIXME: not all parsers will report startDTD() ...
+// we'd rather insist we're "inside".
+ if ("after" == dtdState)
+ fatalError ("not inside DTD");
+ super.notationDecl (name, publicId, systemId);
+ }
+
+ public void unparsedEntityDecl (String name,
+ String publicId, String systemId, String notationName)
+ throws SAXException
+ {
+// FIXME: not all parsers will report startDTD() ...
+// we'd rather insist we're "inside".
+ if ("after" == dtdState)
+ fatalError ("not inside DTD");
+ super.unparsedEntityDecl (name, publicId, systemId, notationName);
+ }
+
+ // FIXME: add the four DeclHandler calls too
+
+ public void endDTD ()
+ throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ if ("inside" != dtdState)
+ fatalError ("DTD ends without start?");
+ dtdState = "after";
+ super.endDTD ();
+ }
+
+ public void characters (char ch [], int start, int length)
+ throws SAXException
+ {
+ int here = start, end = start + length;
+ if (elementStack.empty ())
+ fatalError ("characters must be in an element");
+ while (here < end) {
+ if (ch [here++] != ']')
+ continue;
+ if (here == end) // potential problem ...
+ continue;
+ if (ch [here++] != ']')
+ continue;
+ if (here == end) // potential problem ...
+ continue;
+ if (ch [here++] == '>')
+ fatalError ("character data can't contain \"]]>\"");
+ }
+ super.characters (ch, start, length);
+ }
+
+ public void ignorableWhitespace (char ch [], int start, int length)
+ throws SAXException
+ {
+ int here = start, end = start + length;
+ if (elementStack.empty ())
+ fatalError ("characters must be in an element");
+ while (here < end) {
+ if (ch [here++] == '\r')
+ fatalError ("whitespace can't contain CR");
+ }
+ super.ignorableWhitespace (ch, start, length);
+ }
+
+ public void processingInstruction (String target, String data)
+ throws SAXException
+ {
+ if (data.indexOf ('\r') > 0)
+ fatalError ("PIs can't contain CR");
+ if (data.indexOf ("?>") > 0)
+ fatalError ("PIs can't contain \"?>\"");
+ }
+
+ public void comment (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ if (startedCDATA)
+ fatalError ("comments can't nest in CDATA");
+ int here = start, end = start + length;
+ while (here < end) {
+ if (ch [here] == '\r')
+ fatalError ("comments can't contain CR");
+ if (ch [here++] != '-')
+ continue;
+ if (here == end)
+ fatalError ("comments can't end with \"--->\"");
+ if (ch [here++] == '-')
+ fatalError ("comments can't contain \"--\"");
+ }
+ super.comment (ch, start, length);
+ }
+
+ public void startCDATA ()
+ throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ if (startedCDATA)
+ fatalError ("CDATA starts can't nest");
+ startedCDATA = true;
+ super.startCDATA ();
+ }
+
+ public void endCDATA ()
+ throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ if (!startedCDATA)
+ fatalError ("CDATA end without start?");
+ startedCDATA = false;
+ super.endCDATA ();
+ }
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/XIncludeFilter.java b/libjava/classpath/gnu/xml/pipeline/XIncludeFilter.java
new file mode 100644
index 000000000..a1445fa0c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/XIncludeFilter.java
@@ -0,0 +1,579 @@
+/* XIncludeFilter.java --
+ Copyright (C) 2001,2002 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.pipeline;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Hashtable;
+import java.util.Stack;
+import java.util.Vector;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
+
+import gnu.xml.util.Resolver;
+
+
+
+/**
+ * Filter to process an XPointer-free subset of
+ * <a href="http://www.w3.org/TR/xinclude">XInclude</a>, supporting its
+ * use as a kind of replacement for parsed general entities.
+ * XInclude works much like the <code>#include</code> of C/C++ but
+ * works for XML documents as well as unparsed text files.
+ * Restrictions from the 17-Sept-2002 CR draft of XInclude are as follows:
+ *
+ * <ul>
+ *
+ * <li> URIs must not include fragment identifiers.
+ * The CR specifies support for XPointer <em>element()</em> fragment IDs,
+ * which is not currently implemented here.
+ *
+ * <li> <em>xi:fallback</em> handling of resource errors is not
+ * currently supported.
+ *
+ * <li> DTDs are not supported in included files, since the SAX DTD events
+ * must have completely preceded any included file.
+ * The CR explicitly allows the DTD related portions of the infoset to
+ * grow as an effect of including XML documents.
+ *
+ * <li> <em>xml:base</em> fixup isn't done.
+ *
+ * </ul>
+ *
+ * <p> XML documents that are included will normally be processed using
+ * the default SAX namespace rules, meaning that prefix information may
+ * be discarded. This may be changed with {@link #setSavingPrefixes
+ * setSavingPrefixes()}. <em>You are strongly advised to do this.</em>
+ *
+ * <p> Note that XInclude allows highly incompatible implementations, which
+ * are specialized to handle application-specific infoset extensions. Some
+ * such implementations can be implemented by subclassing this one, but
+ * they may only be substituted in applications at "user option".
+ *
+ * <p>TBD: "IURI" handling.
+ *
+ * @author David Brownell
+ */
+public class XIncludeFilter extends EventFilter implements Locator
+{
+ private Hashtable extEntities = new Hashtable (5, 5);
+ private int ignoreCount;
+ private Stack uris = new Stack ();
+ private Locator locator;
+ private Vector inclusions = new Vector (5, 5);
+ private boolean savingPrefixes;
+
+ /**
+ */
+ public XIncludeFilter (EventConsumer next)
+ throws SAXException
+ {
+ super (next);
+ setContentHandler (this);
+ // DTDHandler callbacks pass straight through
+ setProperty (DECL_HANDLER, this);
+ setProperty (LEXICAL_HANDLER, this);
+ }
+
+ private void fatal (SAXParseException e) throws SAXException
+ {
+ ErrorHandler eh;
+
+ eh = getErrorHandler ();
+ if (eh != null)
+ eh.fatalError (e);
+ throw e;
+ }
+
+ /**
+ * Passes "this" down the filter chain as a proxy locator.
+ */
+ public void setDocumentLocator (Locator locator)
+ {
+ this.locator = locator;
+ super.setDocumentLocator (this);
+ }
+
+ /** Used for proxy locator; do not call directly. */
+ public String getSystemId ()
+ { return (locator == null) ? null : locator.getSystemId (); }
+ /** Used for proxy locator; do not call directly. */
+ public String getPublicId ()
+ { return (locator == null) ? null : locator.getPublicId (); }
+ /** Used for proxy locator; do not call directly. */
+ public int getLineNumber ()
+ { return (locator == null) ? -1 : locator.getLineNumber (); }
+ /** Used for proxy locator; do not call directly. */
+ public int getColumnNumber ()
+ { return (locator == null) ? -1 : locator.getColumnNumber (); }
+
+ /**
+ * Assigns the flag controlling the setting of the SAX2
+ * <em>namespace-prefixes</em> flag.
+ */
+ public void setSavingPrefixes (boolean flag)
+ { savingPrefixes = flag; }
+
+ /**
+ * Returns the flag controlling the setting of the SAX2
+ * <em>namespace-prefixes</em> flag when parsing included documents.
+ * The default value is the SAX2 default (false), which discards
+ * information that can be useful.
+ */
+ public boolean isSavingPrefixes ()
+ { return savingPrefixes; }
+
+ //
+ // Two mechanisms are interacting here.
+ //
+ // - XML Base implies a stack of base URIs, updated both by
+ // "real entity" boundaries and element boundaries.
+ //
+ // - Active "Real Entities" (for document and general entities,
+ // and by xincluded files) are tracked to prevent circular
+ // inclusions.
+ //
+ private String addMarker (String uri)
+ throws SAXException
+ {
+ if (locator != null && locator.getSystemId () != null)
+ uri = locator.getSystemId ();
+
+ // guard against InputSource objects without system IDs
+ if (uri == null)
+ fatal (new SAXParseException ("Entity URI is unknown", locator));
+
+ try {
+ URL url = new URL (uri);
+
+ uri = url.toString ();
+ if (inclusions.contains (uri))
+ fatal (new SAXParseException (
+ "XInclude, circular inclusion", locator));
+ inclusions.addElement (uri);
+ uris.push (url);
+ } catch (IOException e) {
+ // guard against illegal relative URIs (Xerces)
+ fatal (new SAXParseException ("parser bug: relative URI",
+ locator, e));
+ }
+ return uri;
+ }
+
+ private void pop (String uri)
+ {
+ inclusions.removeElement (uri);
+ uris.pop ();
+ }
+
+ //
+ // Document entity boundaries get both treatments.
+ //
+ public void startDocument () throws SAXException
+ {
+ ignoreCount = 0;
+ addMarker (null);
+ super.startDocument ();
+ }
+
+ public void endDocument () throws SAXException
+ {
+ inclusions.setSize (0);
+ extEntities.clear ();
+ uris.setSize (0);
+ super.endDocument ();
+ }
+
+ //
+ // External general entity boundaries get both treatments.
+ //
+ public void externalEntityDecl (String name,
+ String publicId, String systemId)
+ throws SAXException
+ {
+ if (name.charAt (0) == '%')
+ return;
+ try {
+ URL url = new URL (locator.getSystemId ());
+ systemId = new URL (url, systemId).toString ();
+ } catch (IOException e) {
+ // what could we do?
+ }
+ extEntities.put (name, systemId);
+ }
+
+ public void startEntity (String name)
+ throws SAXException
+ {
+ if (ignoreCount != 0) {
+ ignoreCount++;
+ return;
+ }
+
+ String uri = (String) extEntities.get (name);
+ if (uri != null)
+ addMarker (uri);
+ super.startEntity (name);
+ }
+
+ public void endEntity (String name)
+ throws SAXException
+ {
+ if (ignoreCount != 0) {
+ if (--ignoreCount != 0)
+ return;
+ }
+
+ String uri = (String) extEntities.get (name);
+
+ if (uri != null)
+ pop (uri);
+ super.endEntity (name);
+ }
+
+ //
+ // element boundaries only affect the base URI stack,
+ // unless they're XInclude elements.
+ //
+ public void
+ startElement (String uri, String localName, String qName, Attributes atts)
+ throws SAXException
+ {
+ if (ignoreCount != 0) {
+ ignoreCount++;
+ return;
+ }
+
+ URL baseURI = (URL) uris.peek ();
+ String base;
+
+ base = atts.getValue ("http://www.w3.org/XML/1998/namespace", "base");
+ if (base == null)
+ uris.push (baseURI);
+ else {
+ URL url;
+
+ if (base.indexOf ('#') != -1)
+ fatal (new SAXParseException (
+ "xml:base with fragment: " + base,
+ locator));
+
+ try {
+ baseURI = new URL (baseURI, base);
+ uris.push (baseURI);
+ } catch (Exception e) {
+ fatal (new SAXParseException (
+ "xml:base with illegal uri: " + base,
+ locator, e));
+ }
+ }
+
+ if (!"http://www.w3.org/2001/XInclude".equals (uri)) {
+ super.startElement (uri, localName, qName, atts);
+ return;
+ }
+
+ if ("include".equals (localName)) {
+ String href = atts.getValue ("href");
+ String parse = atts.getValue ("parse");
+ String encoding = atts.getValue ("encoding");
+ URL url = (URL) uris.peek ();
+ SAXParseException x = null;
+
+ if (href == null)
+ fatal (new SAXParseException (
+ "XInclude missing href",
+ locator));
+ if (href.indexOf ('#') != -1)
+ fatal (new SAXParseException (
+ "XInclude with fragment: " + href,
+ locator));
+
+ if (parse == null || "xml".equals (parse))
+ x = xinclude (url, href);
+ else if ("text".equals (parse))
+ x = readText (url, href, encoding);
+ else
+ fatal (new SAXParseException (
+ "unknown XInclude parsing mode: " + parse,
+ locator));
+ if (x == null) {
+ // strip out all child content
+ ignoreCount++;
+ return;
+ }
+
+ // FIXME the 17-Sept-2002 CR of XInclude says we "must"
+ // use xi:fallback elements to handle resource errors,
+ // if they exist.
+ fatal (x);
+
+ } else if ("fallback".equals (localName)) {
+ fatal (new SAXParseException (
+ "illegal top level XInclude 'fallback' element",
+ locator));
+ } else {
+ ErrorHandler eh = getErrorHandler ();
+
+ // CR doesn't say this is an error
+ if (eh != null)
+ eh.warning (new SAXParseException (
+ "unrecognized toplevel XInclude element: " + localName,
+ locator));
+ super.startElement (uri, localName, qName, atts);
+ }
+ }
+
+ public void endElement (String uri, String localName, String qName)
+ throws SAXException
+ {
+ if (ignoreCount != 0) {
+ if (--ignoreCount != 0)
+ return;
+ }
+
+ uris.pop ();
+ if (!("http://www.w3.org/2001/XInclude".equals (uri)
+ && "include".equals (localName)))
+ super.endElement (uri, localName, qName);
+ }
+
+ //
+ // ignore all content within non-empty xi:include elements
+ //
+ public void characters (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.characters (ch, start, length);
+ }
+
+ public void processingInstruction (String target, String value)
+ throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.processingInstruction (target, value);
+ }
+
+ public void ignorableWhitespace (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.ignorableWhitespace (ch, start, length);
+ }
+
+ public void comment (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.comment (ch, start, length);
+ }
+
+ public void startCDATA () throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.startCDATA ();
+ }
+
+ public void endCDATA () throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.endCDATA ();
+ }
+
+ public void startPrefixMapping (String prefix, String uri)
+ throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.startPrefixMapping (prefix, uri);
+ }
+
+ public void endPrefixMapping (String prefix) throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.endPrefixMapping (prefix);
+ }
+
+ public void skippedEntity (String name) throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.skippedEntity (name);
+ }
+
+ // JDK 1.1 seems to need it to be done this way, sigh
+ void setLocator (Locator l) { locator = l; }
+ Locator getLocator () { return locator; }
+
+
+ //
+ // for XIncluded entities, manage the current locator and
+ // filter out events that would be incorrect to report
+ //
+ private class Scrubber extends EventFilter
+ {
+ Scrubber (EventFilter f)
+ throws SAXException
+ {
+ // delegation passes to next in chain
+ super (f);
+
+ // process all content events
+ super.setContentHandler (this);
+ super.setProperty (LEXICAL_HANDLER, this);
+
+ // drop all DTD events
+ super.setDTDHandler (null);
+ super.setProperty (DECL_HANDLER, null);
+ }
+
+ // maintain proxy locator
+ // only one startDocument()/endDocument() pair per event stream
+ public void setDocumentLocator (Locator l)
+ { setLocator (l); }
+ public void startDocument ()
+ { }
+ public void endDocument ()
+ { }
+
+ private void reject (String message) throws SAXException
+ { fatal (new SAXParseException (message, getLocator ())); }
+
+ // only the DTD from the "base document" gets reported
+ public void startDTD (String root, String publicId, String systemId)
+ throws SAXException
+ { reject ("XIncluded DTD: " + systemId); }
+ public void endDTD ()
+ throws SAXException
+ { reject ("XIncluded DTD"); }
+ // ... so this should never happen
+ public void skippedEntity (String name) throws SAXException
+ { reject ("XInclude skipped entity: " + name); }
+
+ // since we rejected DTDs, only builtin entities can be reported
+ }
+
+ // <xi:include parse='xml' ...>
+ // relative to the base URI passed
+ private SAXParseException xinclude (URL url, String href)
+ throws SAXException
+ {
+ XMLReader helper;
+ Scrubber scrubber;
+ Locator savedLocator = locator;
+
+ // start with a parser acting just like our input
+ // modulo DTD-ish stuff (validation flag, entity resolver)
+ helper = XMLReaderFactory.createXMLReader ();
+ helper.setErrorHandler (getErrorHandler ());
+ helper.setFeature (FEATURE_URI + "namespace-prefixes", true);
+
+ // Set up the proxy locator and event filter.
+ scrubber = new Scrubber (this);
+ locator = null;
+ bind (helper, scrubber);
+
+ // Merge the included document, except its DTD
+ try {
+ url = new URL (url, href);
+ href = url.toString ();
+
+ if (inclusions.contains (href))
+ fatal (new SAXParseException (
+ "XInclude, circular inclusion", locator));
+
+ inclusions.addElement (href);
+ uris.push (url);
+ helper.parse (new InputSource (href));
+ return null;
+ } catch (java.io.IOException e) {
+ return new SAXParseException (href, locator, e);
+ } finally {
+ pop (href);
+ locator = savedLocator;
+ }
+ }
+
+ // <xi:include parse='text' ...>
+ // relative to the base URI passed
+ private SAXParseException readText (URL url, String href, String encoding)
+ throws SAXException
+ {
+ InputStream in = null;
+
+ try {
+ URLConnection conn;
+ InputStreamReader reader;
+ char buf [] = new char [4096];
+ int count;
+
+ url = new URL (url, href);
+ conn = url.openConnection ();
+ in = conn.getInputStream ();
+ if (encoding == null)
+ encoding = Resolver.getEncoding (conn.getContentType ());
+ if (encoding == null) {
+ ErrorHandler eh = getErrorHandler ();
+ if (eh != null)
+ eh.warning (new SAXParseException (
+ "guessing text encoding for URL: " + url,
+ locator));
+ reader = new InputStreamReader (in);
+ } else
+ reader = new InputStreamReader (in, encoding);
+
+ while ((count = reader.read (buf, 0, buf.length)) != -1)
+ super.characters (buf, 0, count);
+ in.close ();
+ return null;
+ } catch (IOException e) {
+ return new SAXParseException (
+ "can't XInclude text",
+ locator, e);
+ }
+ }
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/XsltFilter.java b/libjava/classpath/gnu/xml/pipeline/XsltFilter.java
new file mode 100644
index 000000000..86b6190c5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/XsltFilter.java
@@ -0,0 +1,130 @@
+/* XsltFilter.java --
+ Copyright (C) 2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.pipeline;
+
+import java.io.IOException;
+
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.sax.*;
+import javax.xml.transform.stream.StreamSource;
+
+import org.xml.sax.SAXException;
+import org.xml.sax.ext.LexicalHandler;
+
+
+/**
+ * Packages an XSLT transform as a pipeline component.
+ * Note that all DTD events (callbacks to DeclHandler and DTDHandler
+ * interfaces) are discarded, although XSLT transforms may be set up to
+ * use the LexicalHandler to write DTDs with only an external subset.
+ * Not every XSLT engine will necessarily be usable with this filter,
+ * but current versions of
+ * <a href="http://saxon.sourceforge.net">SAXON</a> and
+ * <a href="http://xml.apache.org/xalan-j">Xalan</a> should work well.
+ *
+ * @see TransformerFactory
+ *
+ * @author David Brownell
+ */
+final public class XsltFilter extends EventFilter
+{
+ /**
+ * Creates a filter that performs the specified transform.
+ * Uses the JAXP 1.1 interfaces to access the default XSLT
+ * engine configured for in the current execution context,
+ * and parses the stylesheet without custom EntityResolver
+ * or ErrorHandler support.
+ *
+ * @param stylesheet URI for the stylesheet specifying the
+ * XSLT transform
+ * @param next provides the ContentHandler and LexicalHandler
+ * to receive XSLT output.
+ * @exception SAXException if the stylesheet can't be parsed
+ * @exception IOException if there are difficulties
+ * bootstrapping the XSLT engine, such as it not supporting
+ * SAX well enough to use this way.
+ */
+ public XsltFilter (String stylesheet, EventConsumer next)
+ throws SAXException, IOException
+ {
+ // First, get a transformer with the stylesheet preloaded
+ TransformerFactory tf = null;
+ TransformerHandler th;
+
+ try {
+ SAXTransformerFactory stf;
+
+ tf = TransformerFactory.newInstance ();
+ if (!tf.getFeature (SAXTransformerFactory.FEATURE) // sax inputs
+ || !tf.getFeature (SAXResult.FEATURE) // sax outputs
+ || !tf.getFeature (StreamSource.FEATURE) // stylesheet
+ )
+ throw new IOException ("XSLT factory ("
+ + tf.getClass ().getName ()
+ + ") does not support SAX");
+ stf = (SAXTransformerFactory) tf;
+ th = stf.newTransformerHandler (new StreamSource (stylesheet));
+ } catch (TransformerConfigurationException e) {
+ throw new IOException ("XSLT factory ("
+ + (tf == null
+ ? "none available"
+ : tf.getClass ().getName ())
+ + ") configuration error, "
+ + e.getMessage ()
+ );
+ }
+
+ // Hook its outputs up to the pipeline ...
+ SAXResult out = new SAXResult ();
+
+ out.setHandler (next.getContentHandler ());
+ try {
+ LexicalHandler lh;
+ lh = (LexicalHandler) next.getProperty (LEXICAL_HANDLER);
+ out.setLexicalHandler (lh);
+ } catch (Exception e) {
+ // ignore
+ }
+ th.setResult (out);
+
+ // ... and make sure its inputs look like ours.
+ setContentHandler (th);
+ setProperty (LEXICAL_HANDLER, th);
+ }
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/package.html b/libjava/classpath/gnu/xml/pipeline/package.html
new file mode 100644
index 000000000..352f4c87c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/package.html
@@ -0,0 +1,255 @@
+<html><head><title>
+blah
+<!--
+/*
+ * Copyright (C) 1999-2001 The Free Software Foundation, Inc.
+ */
+-->
+</title></head><body>
+
+<p>This package exposes a kind of XML processing pipeline, based on sending
+SAX events, which can be used as components of application architectures.
+Pipelines are used to convey streams of processing events from a producer
+to one or more consumers, and to let each consumer control the data seen by
+later consumers.
+
+<p> There is a <a href="PipelineFactory.html">PipelineFactory</a> class which
+accepts a syntax describing how to construct some simple pipelines. Strings
+describing such pipelines can be used in command line tools (see the
+<a href="../util/DoParse.html">DoParse</a> class)
+and in other places that it is
+useful to let processing be easily reconfigured. Pipelines can of course
+be constructed programmatically, providing access to options that the
+factory won't.
+
+<p> Web applications are supported by making it easy for servlets (or
+non-Java web application components) to be part of a pipeline. They can
+originate XML (or XHTML) data through an <em>InputSource</em> or in
+response to XML messages sent from clients using <em>CallFilter</em>
+pipeline stages. Such facilities are available using the simple syntax
+for pipeline construction.
+
+
+<h2> Programming Models </h2>
+
+<p> Pipelines should be simple to understand.
+
+<ul>
+ <li> XML content, typically entire documents,
+ is pushed through consumers by producers.
+
+ <li> Pipelines are basically about consuming SAX2 callback events,
+ where the events encapsulate XML infoset-level data.<ul>
+
+ <li> Pipelines are constructed by taking one or more consumer
+ stages and combining them to produce a composite consumer.
+
+ <li> A pipeline is presumed to have pending tasks and state from
+ the beginning of its ContentHandler.startDocument() callback until
+ it's returned from its ContentHandler.doneDocument() callback.
+
+ <li> Pipelines may have multiple output stages ("fan-out")
+ or multiple input stages ("fan-in") when appropriate.
+
+ <li> Pipelines may be long-lived, but need not be.
+
+ </ul>
+
+ <li> There is flexibility about event production. <ul>
+
+ <li> SAX2 XMLReader objects are producers, which
+ provide a high level "pull" model: documents (text or DOM) are parsed,
+ and the parser pushes individual events through the pipeline.
+
+ <li> Events can be pushed directly to event consumer components
+ by application modules, if they invoke SAX2 callbacks directly.
+ That is, application modules use the XML Infoset as exposed
+ through SAX2 event callbacks.
+
+ </ul>
+
+ <li> Multiple producer threads may concurrently access a pipeline,
+ if they coordinate appropriately.
+
+ <li> Pipeline processing is not the only framework applications
+ will use.
+
+ </ul>
+
+
+<h3> Producers: XMLReader or Custom </h3>
+
+<p> Many producers will be SAX2 XMLReader objects, and
+will read (pull) data which is then written (pushed) as events.
+Typically these will parse XML text (acquired from
+<code>org.xml.sax.helpers.XMLReaderFactory</code>) or a DOM tree
+(using a <code><a href="../util/DomParser.html">DomParser</a></code>)
+These may be bound to event consumer using a convenience routine,
+<em><a href="EventFilter.html">EventFilter</a>.bind()</em>.
+Once bound, these producers may be given additional documents to
+sent through its pipeline.
+
+<p> In other cases, you will write producers yourself. For example, some
+data structures might know how to write themselves out using one or
+more XML models, expressed as sequences of SAX2 event callbacks.
+An application module might
+itself be a producer, issuing startDocument and endDocument events
+and then asking those data structures to write themselves out to a
+given EventConsumer, or walking data structures (such as JDBC query
+results) and applying its own conversion rules. WAP format XML
+(WBMXL) can be directly converted to producer output.
+
+<p> SAX2 introduced an "XMLFilter" interface, which is a kind of XMLReader.
+It is most useful in conjunction with its XMLFilterImpl helper class;
+see the <em><a href="EventFilter.html">EventFilter</a></em> javadoc
+for information contrasting that XMLFilterImpl approach with the
+relevant parts of this pipeline framework. Briefly, such XMLFilterImpl
+children can be either producers or consumers, and are more limited in
+configuration flexibility. In this framework, the focus of filters is
+on the EventConsumer side; see the section on
+<a href="#fitting">pipe fitting</a> below.
+
+
+<h3> Consume to Standard or Custom Data Representations </h3>
+
+<p> Many consumers will be used to create standard representations of XML
+data. The <a href="TextConsumer.html">TextConsumer</a> takes its events
+and writes them as text for a single XML document,
+using an internal <a href="../util/XMLWriter.html">XMLWriter</a>.
+The <a href="DomConsumer.html">DomConsumer</a> takes its events and uses
+them to create and populate a DOM Document.
+
+<p> In other cases, you will write consumers yourself. For example,
+you might use a particular unmarshaling filter to produce objects
+that fit your application's requirements, instead of using DOM.
+Such consumers work at the level of XML data models, rather than with
+specific representations such as XML text or a DOM tree. You could
+convert your output directly to WAP format data (WBXML).
+
+
+<h3><a name="fitting">Pipe Fitting</a></h3>
+
+<p> Pipelines are composite event consumers, with each stage having
+the opportunity to transform the data before delivering it to any
+subsequent stages.
+
+<p> The <a href="PipelineFactory.html">PipelineFactory</a> class
+provides access to much of this functionality through a simple syntax.
+See the table in that class's javadoc describing a number of standard
+components. Direct API calls are still needed for many of the most
+interesting pipeline configurations, including ones leveraging actual
+or logical concurrency.
+
+<p> Four basic types of pipe fitting are directly supported. These may
+be used to construct complex pipeline networks. <ul>
+
+ <li> <a href="TeeConsumer.html">TeeConsumer</a> objects split event
+ flow so it goes to two two different consumers, one before the other.
+ This is a basic form of event fan-out; you can use this class to
+ copy events to any number of output pipelines.
+
+ <li> Clients can call remote components through HTTP or HTTPS using
+ the <a href="CallFilter.html">CallFilter</a> component, and Servlets
+ can implement such components by extending the
+ <a href="XmlServlet.html">XmlServlet</a> component. Java is not
+ required on either end, and transport protocols other than HTTP may
+ also be used.
+
+ <li> <a href="EventFilter.html">EventFilter</a> objects selectively
+ provide handling for callbacks, and can pass unhandled ones to a
+ subsequent stage. They are often subclassed, since much of the
+ basic filtering machinery is already in place in the base class.
+
+ <li> Applications can merge two event flows by just using the same
+ consumer in each one. If multiple threads are in use, synchronization
+ needs to be addressed by the appropriate application level policy.
+
+ </ul>
+
+<p> Note that filters can be as complex as
+<a href="XsltFilter.html">XSLT transforms</a>
+available) on input data, or as simple as removing simple syntax data
+such as ignorable whitespace, comments, and CDATA delimiters.
+Some simple "built-in" filters are part of this package.
+
+
+<h3> Coding Conventions: Filter and Terminus Stages</h3>
+
+<p> If you follow these coding conventions, your classes may be used
+directly (give the full class name) in pipeline descriptions as understood
+by the PipelineFactory. There are four constructors the factory may
+try to use; in order of decreasing numbers of parameters, these are: <ul>
+
+ <li> Filters that need a single String setup parameter should have
+ a public constructor with two parameters: that string, then the
+ EventConsumer holding the "next" consumer to get events.
+
+ <li> Filters that don't need setup parameters should have a public
+ constructor that accepts a single EventConsumer holding the "next"
+ consumer to get events when they are done.
+
+ <li> Terminus stages may have a public constructor taking a single
+ paramter: the string value of that parameter.
+
+ <li> Terminus stages may have a public no-parameters constructor.
+
+ </ul>
+
+<p> Of course, classes may support more than one such usage convention;
+if they do, they can automatically be used in multiple modes. If you
+try to use a terminus class as a filter, and that terminus has a constructor
+with the appropriate number of arguments, it is automatically wrapped in
+a "tee" filter.
+
+
+<h2> Debugging Tip: "Tee" Joints can Snapshot Data</h2>
+
+<p> It can sometimes be hard to see what's happening, when something
+goes wrong. Easily fixed: just snapshot the data. Then you can find
+out where things start to go wrong.
+
+<p> If you're using pipeline descriptors so that they're easily
+administered, just stick a <em>write&nbsp;(&nbsp;filename&nbsp;)</em>
+filter into the pipeline at an appropriate point.
+
+<p> Inside your programs, you can do the same thing directly: perhaps
+by saving a Writer (perhaps a StringWriter) in a variable, using that
+to create a TextConsumer, and making that the first part of a tee --
+splicing that into your pipeline at a convenient location.
+
+<p> You can also use a DomConsumer to buffer the data, but remember
+that DOM doesn't save all the information that XML provides, so that DOM
+snapshots are relatively low fidelity. They also are substantially more
+expensive in terms of memory than a StringWriter holding similar data.
+
+<h2> Debugging Tip: Non-XML Producers</h2>
+
+<p> Producers in pipelines don't need to start from XML
+data structures, such as text in XML syntax (likely coming
+from some <em>XMLReader</em> that parses XML) or a
+DOM representation (perhaps with a
+<a href="../util/DomParser.html">DomParser</a>).
+
+<p> One common type of event producer will instead make
+direct calls to SAX event handlers returned from an
+<a href="EventConsumer.html">EventConsumer</a>.
+For example, making <em>ContentHandler.startElement</em>
+calls and matching <em>ContentHandler.endElement</em> calls.
+
+<p> Applications making such calls can catch certain
+common "syntax errors" by using a
+<a href="WellFormednessFilter.html">WellFormednessFilter</a>.
+That filter will detect (and report) erroneous input data
+such as mismatched document, element, or CDATA start/end calls.
+Use such a filter near the head of the pipeline that your
+producer feeds, at least while debugging, to help ensure that
+you're providing legal XML Infoset data.
+
+<p> You can also arrange to validate data on the fly.
+For DTD validation, you can configure a
+<a href="ValidationConsumer.html">ValidationConsumer</a>
+to work as a filter, using any DTD you choose.
+Other validation schemes can be handled with other
+validation filters.
+
+</body></html>
diff --git a/libjava/classpath/gnu/xml/stream/AttributeImpl.java b/libjava/classpath/gnu/xml/stream/AttributeImpl.java
new file mode 100644
index 000000000..58a0dbe21
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/AttributeImpl.java
@@ -0,0 +1,123 @@
+/* AttributeImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.namespace.QName;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+
+/**
+ * An attribute event.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class AttributeImpl
+ extends XMLEventImpl
+ implements Attribute
+{
+
+ protected final QName name;
+ protected final String value;
+ protected final String type;
+ protected final boolean specified;
+
+ protected AttributeImpl(Location location,
+ QName name, String value, String type,
+ boolean specified)
+ {
+ super(location);
+ this.name = name;
+ this.value = value;
+ this.type = type;
+ this.specified = specified;
+ }
+
+ public int getEventType()
+ {
+ return ATTRIBUTE;
+ }
+
+ public QName getName()
+ {
+ return name;
+ }
+
+ public String getValue()
+ {
+ return value;
+ }
+
+ public String getDTDType()
+ {
+ return type;
+ }
+
+ public boolean isSpecified()
+ {
+ return specified;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ String prefix = name.getPrefix();
+ if (prefix != null && !"".equals(prefix))
+ {
+ writer.write(prefix);
+ writer.write(':');
+ }
+ writer.write(name.getLocalPart());
+ writer.write('=');
+ writer.write('"');
+ writer.write(encode(value, true));
+ writer.write('"');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/BufferedReader.java b/libjava/classpath/gnu/xml/stream/BufferedReader.java
new file mode 100644
index 000000000..dc69fb34b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/BufferedReader.java
@@ -0,0 +1,198 @@
+/* BufferedReader.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * A mark-capable buffered reader.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class BufferedReader
+ extends Reader
+{
+
+ static final int DEFAULT_BUFFER_SIZE = 4096;
+
+ final Reader in;
+ char[] buf;
+ int pos, count, markpos, marklimit, bufferSize;
+
+ BufferedReader(Reader in)
+ {
+ this(in, DEFAULT_BUFFER_SIZE);
+ }
+
+ BufferedReader(Reader in, int bufferSize)
+ {
+ if (bufferSize < 1)
+ throw new IllegalArgumentException();
+ this.in = in;
+ this.bufferSize = bufferSize;
+ buf = new char[bufferSize];
+ pos = count = bufferSize;
+ }
+
+ public void close()
+ throws IOException
+ {
+ buf = null;
+ pos = count = 0;
+ markpos = -1;
+ in.close();
+ }
+
+ public void mark(int readlimit)
+ throws IOException
+ {
+ marklimit = readlimit;
+ markpos = pos;
+ }
+
+ public boolean markSupported()
+ {
+ return true;
+ }
+
+ public int read()
+ throws IOException
+ {
+ if (pos >= count && !refill())
+ return -1;
+ return (int) buf[pos++];
+ }
+
+ public int read(char[] b)
+ throws IOException
+ {
+ return read(b, 0, b.length);
+ }
+
+ public int read(char[] b, int off, int len)
+ throws IOException
+ {
+ if (off < 0 || len < 0 || b.length - off < len)
+ throw new IndexOutOfBoundsException();
+
+ if (len == 0)
+ return 0;
+
+ if (pos >= count && !refill())
+ return -1;
+
+ int ret = Math.min(count - pos, len);
+ System.arraycopy(buf, pos, b, off, ret);
+ pos += ret;
+ off += ret;
+ len -= ret;
+
+ while (len > 0 && refill())
+ {
+ int remain = Math.min(count - pos, len);
+ System.arraycopy(buf, pos, b, off, remain);
+ pos += remain;
+ off += remain;
+ len -= remain;
+ ret += remain;
+ }
+
+ return ret;
+ }
+
+ public void reset()
+ throws IOException
+ {
+ if (markpos == -1)
+ throw new IOException(buf == null ? "Stream closed." : "Invalid mark.");
+ pos = markpos;
+ }
+
+ public long skip(long n)
+ throws IOException
+ {
+ if (buf == null)
+ throw new IOException("Stream closed.");
+ final long origN = n;
+ while (n > 0L)
+ {
+ if (pos >= count && !refill())
+ break;
+ int numread = (int) Math.min((long) (count - pos), n);
+ pos += numread;
+ n -= numread;
+ }
+ return origN - n;
+ }
+
+ private boolean refill()
+ throws IOException
+ {
+ if (buf == null)
+ throw new IOException("Stream closed.");
+
+ int markcount = count - markpos;
+ if (markpos == -1 || markcount >= marklimit)
+ {
+ markpos = -1;
+ pos = count = 0;
+ }
+ else
+ {
+ char[] newbuf = buf;
+ if (markpos < bufferSize)
+ {
+ newbuf = new char[count - markpos + bufferSize];
+ }
+ System.arraycopy(buf, markpos, newbuf, 0, markcount);
+ buf = newbuf;
+ count = markcount;
+ pos -= markpos;
+ markpos = 0;
+ }
+
+ int numread = in.read(buf, count, bufferSize);
+ if (numread <= 0)
+ return false;
+
+ count += numread;
+ return true;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/CRLFReader.java b/libjava/classpath/gnu/xml/stream/CRLFReader.java
new file mode 100644
index 000000000..7f3cf4d74
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/CRLFReader.java
@@ -0,0 +1,180 @@
+/* CRLFReader.java --
+ Copyright (C) 2005,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * Filtered reader that normalizes CRLF pairs into LFs.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class CRLFReader
+ extends Reader
+{
+
+ /**
+ * The CR octet.
+ */
+ public static final int CR = 13;
+
+ /**
+ * The LF octet.
+ */
+ public static final int LF = 10;
+
+ private boolean doReset;
+
+ protected Reader in;
+
+ /**
+ * Constructor.
+ */
+ protected CRLFReader(Reader in)
+ {
+ if (!in.markSupported())
+ in = new BufferedReader(in);
+ this.in = in;
+ }
+
+ public int read()
+ throws IOException
+ {
+ int c = in.read();
+ if (c == 13) // CR
+ {
+ in.mark(1);
+ int d = in.read();
+ if (d == 10) // LF
+ c = d;
+ else
+ in.reset();
+ }
+ return c;
+ }
+
+ public int read(char[] b)
+ throws IOException
+ {
+ return read(b, 0, b.length);
+ }
+
+ public int read(char[] b, int off, int len)
+ throws IOException
+ {
+ in.mark(len + 1);
+ int l = in.read(b, off, len);
+ if (l > 0)
+ {
+ int i = indexOfCRLF(b, off, l);
+ if (doReset)
+ {
+ in.reset();
+ if (i != -1)
+ {
+ l = in.read(b, off, (i + 1) - off); // read to CR
+ in.read(); // skip LF
+ b[i] = '\n'; // fix CR as LF
+ }
+ else
+ l = in.read(b, off, len); // CR(s) but no LF
+ }
+ }
+ return l;
+ }
+
+ public boolean markSupported()
+ {
+ return in.markSupported();
+ }
+
+ public void mark(int limit)
+ throws IOException
+ {
+ in.mark(limit);
+ }
+
+ public void reset()
+ throws IOException
+ {
+ in.reset();
+ }
+
+ public long skip(long n)
+ throws IOException
+ {
+ return in.skip(n);
+ }
+
+ public void close()
+ throws IOException
+ {
+ in.close();
+ }
+
+ private int indexOfCRLF(char[] b, int off, int len)
+ throws IOException
+ {
+ doReset = false;
+ int end = off + len;
+ int em1 = end - 1;
+ for (int i = off; i < end; i++)
+ {
+ if (b[i] == '\r') // CR
+ {
+ int d;
+ if (i == em1)
+ {
+ d = in.read();
+ doReset = true;
+ }
+ else
+ d = b[i + 1];
+ if (d == '\n') // LF
+ {
+ doReset = true;
+ return i;
+ }
+ }
+ }
+ return -1;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/CharactersImpl.java b/libjava/classpath/gnu/xml/stream/CharactersImpl.java
new file mode 100644
index 000000000..2107a5a6d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/CharactersImpl.java
@@ -0,0 +1,119 @@
+/* CharactersImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Characters;
+
+/**
+ * A character data (text) event.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class CharactersImpl
+ extends XMLEventImpl
+ implements Characters
+{
+
+ protected final String data;
+ protected final boolean whitespace;
+ protected final boolean cdata;
+ protected final boolean ignorableWhitespace;
+
+ protected CharactersImpl(Location location,
+ String data, boolean whitespace, boolean cdata,
+ boolean ignorableWhitespace)
+ {
+ super(location);
+ this.data = data;
+ this.whitespace = whitespace;
+ this.cdata = cdata;
+ this.ignorableWhitespace = ignorableWhitespace;
+ }
+
+ public int getEventType()
+ {
+ return cdata ? CDATA : whitespace ? SPACE : CHARACTERS;
+ }
+
+ public String getData()
+ {
+ return data;
+ }
+
+ public boolean isWhiteSpace()
+ {
+ return whitespace;
+ }
+
+ public boolean isCData()
+ {
+ return cdata;
+ }
+
+ public boolean isIgnorableWhiteSpace()
+ {
+ return ignorableWhitespace;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ if (cdata)
+ {
+ writer.write("<![CDATA[");
+ writer.write(data);
+ writer.write("]]>");
+ }
+ else
+ writer.write(encode(data, false));
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/CommentImpl.java b/libjava/classpath/gnu/xml/stream/CommentImpl.java
new file mode 100644
index 000000000..118ac7a57
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/CommentImpl.java
@@ -0,0 +1,91 @@
+/* CommentImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Comment;
+
+/**
+ * A comment event.
+ *
+ * @author <a href'mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class CommentImpl
+ extends XMLEventImpl
+ implements Comment
+{
+
+ protected final String text;
+
+ protected CommentImpl(Location location, String text)
+ {
+ super(location);
+ this.text = text;
+ }
+
+ public int getEventType()
+ {
+ return COMMENT;
+ }
+
+ public String getText()
+ {
+ return text;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("<!--");
+ writer.write(encode(text, false));
+ writer.write("-->");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/DTDImpl.java b/libjava/classpath/gnu/xml/stream/DTDImpl.java
new file mode 100644
index 000000000..cf049d362
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/DTDImpl.java
@@ -0,0 +1,114 @@
+/* DTDImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.DTD;
+
+/**
+ * A DOCTYPE declaration event.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DTDImpl
+ extends XMLEventImpl
+ implements DTD
+{
+
+ protected final String body;
+ protected final Object impl;
+ protected final List notations;
+ protected final List entities;
+
+ protected DTDImpl(Location location,
+ String body, Object impl, List notations, List entities)
+ {
+ super(location);
+ this.body = body;
+ this.impl = impl;
+ this.notations = notations;
+ this.entities = entities;
+ }
+
+ public int getEventType()
+ {
+ return DTD;
+ }
+
+ public String getDocumentTypeDeclaration()
+ {
+ return body;
+ }
+
+ public Object getProcessedDTD()
+ {
+ return impl;
+ }
+
+ public List getNotations()
+ {
+ return notations;
+ }
+
+ public List getEntities()
+ {
+ return entities;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("<!DOCTYPE ");
+ writer.write(body);
+ writer.write(">");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/EndDocumentImpl.java b/libjava/classpath/gnu/xml/stream/EndDocumentImpl.java
new file mode 100644
index 000000000..13877c5c9
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/EndDocumentImpl.java
@@ -0,0 +1,70 @@
+/* EndDocumentImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.EndDocument;
+
+/**
+ * An end-document event.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class EndDocumentImpl
+ extends XMLEventImpl
+ implements EndDocument
+{
+
+ protected EndDocumentImpl(Location location)
+ {
+ super(location);
+ }
+
+ public int getEventType()
+ {
+ return END_DOCUMENT;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/EndElementImpl.java b/libjava/classpath/gnu/xml/stream/EndElementImpl.java
new file mode 100644
index 000000000..71f6aa0bc
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/EndElementImpl.java
@@ -0,0 +1,107 @@
+/* EndElementImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.EndElement;
+
+/**
+ * An end-element event.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class EndElementImpl
+ extends XMLEventImpl
+ implements EndElement
+{
+
+ protected final QName name;
+ protected final List namespaces;
+
+ protected EndElementImpl(Location location, QName name, List namespaces)
+ {
+ super(location);
+ this.name = name;
+ this.namespaces = namespaces;
+ }
+
+ public int getEventType()
+ {
+ return END_ELEMENT;
+ }
+
+ public QName getName()
+ {
+ return name;
+ }
+
+ public Iterator getNamespaces()
+ {
+ return namespaces.iterator();
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("</");
+ String prefix = name.getPrefix();
+ if (prefix != null && !"".equals(prefix))
+ {
+ writer.write(prefix);
+ writer.write(':');
+ }
+ writer.write(name.getLocalPart());
+ writer.write(">");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/EntityDeclarationImpl.java b/libjava/classpath/gnu/xml/stream/EntityDeclarationImpl.java
new file mode 100644
index 000000000..b6e33e8d2
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/EntityDeclarationImpl.java
@@ -0,0 +1,163 @@
+/* EntityDeclarationImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.EntityDeclaration;
+
+/**
+ * An entity declaration event.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class EntityDeclarationImpl
+ extends XMLEventImpl
+ implements EntityDeclaration
+{
+
+ protected final String publicId;
+ protected final String systemId;
+ protected final String name;
+ protected final String notationName;
+ protected final String replacementText;
+ protected final String baseUri;
+
+ protected EntityDeclarationImpl(Location location,
+ String publicId, String systemId,
+ String name, String notationName,
+ String replacementText, String baseUri)
+ {
+ super(location);
+ this.publicId = publicId;
+ this.systemId = systemId;
+ this.name = name;
+ this.notationName = notationName;
+ this.replacementText = replacementText;
+ this.baseUri = baseUri;
+ }
+
+ public int getEventType()
+ {
+ return ENTITY_DECLARATION;
+ }
+
+ public String getPublicId()
+ {
+ return publicId;
+ }
+
+ public String getSystemId()
+ {
+ return systemId;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public String getNotationName()
+ {
+ return notationName;
+ }
+
+ public String getReplacementText()
+ {
+ return replacementText;
+ }
+
+ public String getBaseURI()
+ {
+ return baseUri;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("<!ENTITY ");
+ writer.write(name);
+ writer.write(' ');
+ if (systemId != null)
+ {
+ if (publicId != null)
+ {
+ writer.write(" PUBLIC ");
+ writer.write('"');
+ writer.write(publicId);
+ writer.write('"');
+ writer.write(' ');
+ writer.write('"');
+ writer.write(systemId);
+ writer.write('"');
+ }
+ else
+ {
+ writer.write(" SYSTEM ");
+ writer.write('"');
+ writer.write(systemId);
+ writer.write('"');
+ }
+ if (notationName != null)
+ {
+ writer.write(" NDATA ");
+ writer.write(notationName);
+ }
+ }
+ else
+ {
+ writer.write('"');
+ if (replacementText != null)
+ writer.write(replacementText);
+ writer.write('"');
+ }
+ writer.write(">");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/EntityReferenceImpl.java b/libjava/classpath/gnu/xml/stream/EntityReferenceImpl.java
new file mode 100644
index 000000000..6f8a11c80
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/EntityReferenceImpl.java
@@ -0,0 +1,101 @@
+/* EntityReferenceImpl.java --
+ Copyright (C) 2005,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.EntityDeclaration;
+import javax.xml.stream.events.EntityReference;
+
+/**
+ * An entity reference event.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class EntityReferenceImpl
+ extends XMLEventImpl
+ implements EntityReference
+{
+
+ protected final EntityDeclaration decl;
+ protected final String name;
+
+ protected EntityReferenceImpl(Location location,
+ EntityDeclaration decl,
+ String name)
+ {
+ super(location);
+ this.decl = decl;
+ this.name = name;
+ }
+
+ public int getEventType()
+ {
+ return ENTITY_REFERENCE;
+ }
+
+ public EntityDeclaration getDeclaration()
+ {
+ return decl;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write('&');
+ writer.write(name);
+ writer.write(';');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/FilteredEventReader.java b/libjava/classpath/gnu/xml/stream/FilteredEventReader.java
new file mode 100644
index 000000000..1ddb469df
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/FilteredEventReader.java
@@ -0,0 +1,114 @@
+/* FilteredEventReader.java --
+ Copyright (C) 2005,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import javax.xml.stream.EventFilter;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.XMLEvent;
+import javax.xml.stream.util.EventReaderDelegate;
+
+class FilteredEventReader
+ extends EventReaderDelegate
+{
+
+ final EventFilter filter;
+
+ FilteredEventReader(XMLEventReader reader, EventFilter filter)
+ {
+ super(reader);
+ this.filter = filter;
+ }
+
+ public boolean hasNext()
+ {
+ // XXX ???
+ return super.hasNext();
+ }
+
+ public XMLEvent nextEvent()
+ throws XMLStreamException
+ {
+ XMLEvent ret;
+ do
+ {
+ ret = super.nextEvent();
+ }
+ while (!filter.accept(ret));
+ return ret;
+ }
+
+ public Object next()
+ {
+ try
+ {
+ return nextEvent();
+ }
+ catch (XMLStreamException e)
+ {
+ RuntimeException e2 = new RuntimeException();
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public XMLEvent peek()
+ throws XMLStreamException
+ {
+ XMLEvent ret;
+ do
+ {
+ ret = super.peek();
+ }
+ while (!filter.accept(ret));
+ return ret;
+ }
+
+ public XMLEvent nextTag()
+ throws XMLStreamException
+ {
+ XMLEvent ret;
+ do
+ {
+ ret = super.nextTag();
+ }
+ while (!filter.accept(ret));
+ return ret;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/FilteredStreamReader.java b/libjava/classpath/gnu/xml/stream/FilteredStreamReader.java
new file mode 100644
index 000000000..62d96488e
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/FilteredStreamReader.java
@@ -0,0 +1,90 @@
+/* FilteredStreamReader.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import javax.xml.stream.StreamFilter;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.util.StreamReaderDelegate;
+
+class FilteredStreamReader
+ extends StreamReaderDelegate
+{
+
+ final XMLStreamReader reader;
+ final StreamFilter filter;
+
+ FilteredStreamReader(XMLStreamReader reader, StreamFilter filter)
+ {
+ super(reader);
+ this.reader = reader;
+ this.filter = filter;
+ }
+
+ public boolean hasNext()
+ throws XMLStreamException
+ {
+ // XXX ???
+ return super.hasNext();
+ }
+
+ public int next()
+ throws XMLStreamException
+ {
+ int ret;
+ do
+ {
+ ret = super.next();
+ }
+ while (!filter.accept(reader));
+ return ret;
+ }
+
+ public int nextTag()
+ throws XMLStreamException
+ {
+ int ret;
+ do
+ {
+ ret = super.nextTag();
+ }
+ while (!filter.accept(reader));
+ return ret;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/NamespaceImpl.java b/libjava/classpath/gnu/xml/stream/NamespaceImpl.java
new file mode 100644
index 000000000..241f980b6
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/NamespaceImpl.java
@@ -0,0 +1,137 @@
+/* NamespaceImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.namespace.QName;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Namespace;
+
+/**
+ * A namespace declaration event.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class NamespaceImpl
+ extends XMLEventImpl
+ implements Namespace
+{
+
+ protected final String prefix;
+ protected final String uri;
+ protected final boolean specified;
+
+ protected NamespaceImpl(Location location, String prefix, String uri,
+ boolean specified)
+ {
+ super(location);
+ this.prefix = prefix;
+ this.uri = uri;
+ this.specified = specified;
+ }
+
+ public int getEventType()
+ {
+ return NAMESPACE;
+ }
+
+ public String getPrefix()
+ {
+ return prefix;
+ }
+
+ public String getNamespaceURI()
+ {
+ return uri;
+ }
+
+ public boolean isSpecified()
+ {
+ return specified;
+ }
+
+ public QName getName()
+ {
+ if (isDefaultNamespaceDeclaration())
+ return new QName("", "xmlns", null);
+ else
+ return new QName("", prefix, "xmlns");
+ }
+
+ public String getDTDType()
+ {
+ return "CDATA";
+ }
+
+ public String getValue()
+ {
+ return uri;
+ }
+
+ public boolean isDefaultNamespaceDeclaration()
+ {
+ return (prefix == null || "".equals(prefix));
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("xmlns");
+ if (prefix != null && !"".equals(prefix))
+ {
+ writer.write(':');
+ writer.write(prefix);
+ }
+ writer.write('=');
+ writer.write('"');
+ writer.write(encode(uri, true));
+ writer.write('"');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/NotationDeclarationImpl.java b/libjava/classpath/gnu/xml/stream/NotationDeclarationImpl.java
new file mode 100644
index 000000000..a338237ee
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/NotationDeclarationImpl.java
@@ -0,0 +1,125 @@
+/* NotationDeclarationImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.NotationDeclaration;
+
+/**
+ * A notation declaration event.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class NotationDeclarationImpl
+ extends XMLEventImpl
+ implements NotationDeclaration
+{
+
+ protected final String name;
+ protected final String publicId;
+ protected final String systemId;
+
+ protected NotationDeclarationImpl(Location location,
+ String name, String publicId,
+ String systemId)
+ {
+ super(location);
+ this.name = name;
+ this.publicId = publicId;
+ this.systemId = systemId;
+ }
+
+ public int getEventType()
+ {
+ return NOTATION_DECLARATION;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public String getPublicId()
+ {
+ return publicId;
+ }
+
+ public String getSystemId()
+ {
+ return systemId;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("<!NOTATION ");
+ writer.write(name);
+ if (publicId != null)
+ {
+ writer.write(" PUBLIC ");
+ writer.write('"');
+ writer.write(publicId);
+ writer.write('"');
+ writer.write(' ');
+ writer.write('"');
+ writer.write(systemId);
+ writer.write('"');
+ }
+ else
+ {
+ writer.write(" SYSTEM ");
+ writer.write('"');
+ writer.write(systemId);
+ writer.write('"');
+ }
+ writer.write('>');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/ProcessingInstructionImpl.java b/libjava/classpath/gnu/xml/stream/ProcessingInstructionImpl.java
new file mode 100644
index 000000000..4ce0badc8
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/ProcessingInstructionImpl.java
@@ -0,0 +1,104 @@
+/* ProcessingInstructionImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.ProcessingInstruction;
+
+/**
+ * A processing instruction event.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class ProcessingInstructionImpl
+ extends XMLEventImpl
+ implements ProcessingInstruction
+{
+
+ protected final String target;
+ protected final String data;
+
+ protected ProcessingInstructionImpl(Location location,
+ String target, String data)
+ {
+ super(location);
+ this.target = target;
+ this.data = data;
+ }
+
+ public int getEventType()
+ {
+ return PROCESSING_INSTRUCTION;
+ }
+
+ public String getTarget()
+ {
+ return target;
+ }
+
+ public String getData()
+ {
+ return data;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("<?");
+ writer.write(target);
+ if (data != null)
+ {
+ writer.write(' ');
+ writer.write(data);
+ }
+ writer.write("?>");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/SAXParser.java b/libjava/classpath/gnu/xml/stream/SAXParser.java
new file mode 100644
index 000000000..b71d98ed3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/SAXParser.java
@@ -0,0 +1,1041 @@
+/* SAXParser.java --
+ Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.Reader;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.Map;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLReporter;
+import javax.xml.stream.XMLResolver;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.xml.sax.ContentHandler;
+import org.xml.sax.DTDHandler;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.Parser;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.ext.Attributes2;
+import org.xml.sax.ext.DeclHandler;
+import org.xml.sax.ext.LexicalHandler;
+import org.xml.sax.ext.Locator2;
+
+/**
+ * JAXP SAX parser using an underlying StAX parser.
+ * This parser supports the following additional SAX features and
+ * properties:
+ * <table>
+ * <tr><th colspan='4'>Features</th></tr>
+ * <tr><td>http://gnu.org/sax/features/xml-base</td>
+ * <td colspan='2'>read/write</td>
+ * <td>Indicates or sets whether XML Base processing is enabled</td></tr>
+ * <tr><th colspan='4'>Properties</th></tr>
+ * <tr><td>http://gnu.org/sax/properties/base-uri</td>
+ * <td>read-only</td><td>String</td>
+ * <td>Returns the base URI of the current event</td></tr>
+ * <tr><td>http://gnu.org/sax/properties/document-xml-encoding</td>
+ * <td>read-only</td><td>String</td>
+ * <td>Returns the encoding specified in the XML declaration</td></tr>
+ * </table>
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class SAXParser
+ extends javax.xml.parsers.SAXParser
+ implements XMLReader, Attributes2, Locator2, XMLReporter, XMLResolver
+{
+
+ ContentHandler contentHandler;
+ DeclHandler declHandler;
+ DTDHandler dtdHandler;
+ EntityResolver entityResolver;
+ ErrorHandler errorHandler;
+ LexicalHandler lexicalHandler;
+
+ boolean validating = false;
+ boolean namespaceAware = true;
+ boolean xIncludeAware = false;
+ boolean stringInterning = true;
+ boolean coalescing = true;
+ boolean replaceERefs = true;
+ boolean externalEntities = true;
+ boolean supportDTD = true;
+ boolean baseAware = true;
+
+ XMLParser parser;
+ XMLStreamReader reader;
+ String encoding;
+ String xmlVersion;
+ boolean xmlStandalone;
+ String xmlEncoding;
+ String baseURI;
+
+ public SAXParser()
+ {
+ }
+
+ SAXParser(boolean validating, boolean namespaceAware, boolean xIncludeAware)
+ {
+ this.validating = validating;
+ this.namespaceAware = namespaceAware;
+ this.xIncludeAware = xIncludeAware;
+ }
+
+ // -- SAXParser --
+
+ public Parser getParser()
+ throws SAXException
+ {
+ return null;
+ }
+
+ public XMLReader getXMLReader()
+ throws SAXException
+ {
+ return this;
+ }
+
+ public boolean isNamespaceAware()
+ {
+ return namespaceAware;
+ }
+
+ public boolean isValidating()
+ {
+ return validating;
+ }
+
+ public void setProperty(String name, Object value)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ if (parser != null)
+ throw new IllegalStateException("parsing in progress");
+ final String FEATURES = "http://xml.org/sax/features/";
+ final String PROPERTIES = "http://xml.org/sax/properties/";
+ final String GNU_FEATURES = "http://gnu.org/sax/features/";
+ if ((FEATURES + "namespaces").equals(name))
+ namespaceAware = Boolean.TRUE.equals(value);
+ else if ((FEATURES + "namespace-prefixes").equals(name))
+ {
+ // NOOP
+ }
+ else if ((FEATURES + "string-interning").equals(name))
+ stringInterning = Boolean.TRUE.equals(value);
+ else if ((FEATURES + "use-attributes2").equals(name))
+ {
+ // NOOP
+ }
+ else if ((FEATURES + "validation").equals(name))
+ validating = Boolean.TRUE.equals(value);
+ else if ((FEATURES + "external-general-entities").equals(name))
+ externalEntities = Boolean.TRUE.equals(value);
+ else if ((FEATURES + "external-parameter-entities").equals(name))
+ externalEntities = Boolean.TRUE.equals(value);
+ else if ((PROPERTIES + "declaration-handler").equals(name))
+ declHandler = (DeclHandler) value;
+ else if ((PROPERTIES + "lexical-handler").equals(name))
+ lexicalHandler = (LexicalHandler) value;
+ else if ((GNU_FEATURES + "xml-base").equals(name))
+ baseAware = Boolean.TRUE.equals(value);
+ else if ((GNU_FEATURES + "coalescing").equals(name))
+ coalescing = Boolean.TRUE.equals(value);
+ else
+ throw new SAXNotSupportedException(name);
+ }
+
+ public Object getProperty(String name)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ final String FEATURES = "http://xml.org/sax/features/";
+ final String PROPERTIES = "http://xml.org/sax/properties/";
+ final String GNU_FEATURES = "http://gnu.org/sax/features/";
+ final String GNU_PROPERTIES = "http://gnu.org/sax/properties/";
+ if ((GNU_FEATURES + "base-uri").equals(name))
+ return baseURI;
+ if ((FEATURES + "is-standalone").equals(name))
+ return xmlStandalone ? Boolean.TRUE : Boolean.FALSE;
+ if ((FEATURES + "namespaces").equals(name))
+ return namespaceAware ? Boolean.TRUE : Boolean.FALSE;
+ if ((FEATURES + "namespace-prefixes").equals(name))
+ return Boolean.TRUE;
+ if ((FEATURES + "string-interning").equals(name))
+ return stringInterning ? Boolean.TRUE : Boolean.FALSE;
+ if ((FEATURES + "use-attributes2").equals(name))
+ return Boolean.TRUE;
+ if ((FEATURES + "use-locator2").equals(name))
+ return Boolean.TRUE;
+ if ((FEATURES + "use-entity-resolver2").equals(name))
+ return Boolean.FALSE;
+ if ((FEATURES + "validation").equals(name))
+ return validating ? Boolean.TRUE : Boolean.FALSE;
+ if ((FEATURES + "external-general-entities").equals(name))
+ return externalEntities ? Boolean.TRUE : Boolean.FALSE;
+ if ((FEATURES + "external-parameter-entities").equals(name))
+ return externalEntities ? Boolean.TRUE : Boolean.FALSE;
+ if ((FEATURES + "xml-1.1").equals(name))
+ return Boolean.TRUE;
+ if ((PROPERTIES + "declaration-handler").equals(name))
+ return declHandler;
+ if ((PROPERTIES + "document-xml-version").equals(name))
+ return xmlVersion;
+ if ((PROPERTIES + "lexical-handler").equals(name))
+ return lexicalHandler;
+ if ((GNU_FEATURES + "xml-base").equals(name))
+ return baseAware ? Boolean.TRUE : Boolean.FALSE;
+ if ((GNU_PROPERTIES + "document-xml-encoding").equals(name))
+ return xmlEncoding;
+ throw new SAXNotRecognizedException(name);
+ }
+
+ public boolean isXIncludeAware()
+ {
+ return xIncludeAware;
+ }
+
+ public void reset()
+ {
+ parser = null;
+ encoding = null;
+ xmlVersion = null;
+ xmlStandalone = false;
+ }
+
+ // -- XMLReader --
+
+ public boolean getFeature(String name)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ Object ret = getProperty(name);
+ if (ret instanceof Boolean)
+ return ((Boolean) ret).booleanValue();
+ throw new SAXNotSupportedException(name);
+ }
+
+ public void setFeature(String name, boolean value)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ setProperty(name, value ? Boolean.TRUE : Boolean.FALSE);
+ }
+
+ public void setEntityResolver(EntityResolver resolver)
+ {
+ entityResolver = resolver;
+ }
+
+ public EntityResolver getEntityResolver()
+ {
+ return entityResolver;
+ }
+
+ public void setDTDHandler(DTDHandler handler)
+ {
+ dtdHandler = handler;
+ }
+
+ public DTDHandler getDTDHandler()
+ {
+ return dtdHandler;
+ }
+
+ public void setContentHandler(ContentHandler handler)
+ {
+ contentHandler = handler;
+ }
+
+ public ContentHandler getContentHandler()
+ {
+ return contentHandler;
+ }
+
+ public void setErrorHandler(ErrorHandler handler)
+ {
+ errorHandler = handler;
+ }
+
+ public ErrorHandler getErrorHandler()
+ {
+ return errorHandler;
+ }
+
+ public synchronized void parse(InputSource input)
+ throws IOException, SAXException
+ {
+ reset();
+ String systemId = input.getSystemId();
+ InputStream in = input.getByteStream();
+ boolean opened = false;
+ if (in != null)
+ parser = new XMLParser(in, systemId,
+ validating,
+ namespaceAware,
+ coalescing,
+ replaceERefs,
+ externalEntities,
+ supportDTD,
+ baseAware,
+ stringInterning,
+ true,
+ this,
+ this);
+ else
+ {
+ Reader r = input.getCharacterStream();
+ if (r != null)
+ parser = new XMLParser(r, systemId,
+ validating,
+ namespaceAware,
+ coalescing,
+ replaceERefs,
+ externalEntities,
+ supportDTD,
+ baseAware,
+ stringInterning,
+ true,
+ this,
+ this);
+ }
+ if (parser == null)
+ {
+ if (systemId == null)
+ throw new SAXException("No stream or system ID specified");
+ systemId = XMLParser.absolutize(null, systemId);
+ in = new URL(systemId).openStream();
+ opened = true;
+ parser = new XMLParser(in, systemId,
+ validating,
+ namespaceAware,
+ coalescing,
+ replaceERefs,
+ externalEntities,
+ supportDTD,
+ baseAware,
+ stringInterning,
+ true,
+ this,
+ this);
+ }
+ reader = parser;
+ baseURI = systemId;
+
+ if (xIncludeAware)
+ reader = new XIncludeFilter(parser, systemId, namespaceAware,
+ validating, true);
+
+ if (contentHandler != null)
+ contentHandler.setDocumentLocator(this);
+ boolean startDocumentDone = false;
+ try
+ {
+ while (parser.hasNext())
+ {
+ int event = parser.next();
+ if (baseAware)
+ baseURI = parser.getXMLBase();
+ switch (event)
+ {
+ case XMLStreamConstants.CHARACTERS:
+ if (contentHandler != null)
+ {
+ char[] b = reader.getTextCharacters();
+ contentHandler.characters(b, 0, b.length);
+ }
+ break;
+ case XMLStreamConstants.SPACE:
+ if (contentHandler != null)
+ {
+ char[] b = reader.getTextCharacters();
+ if (isIgnorableWhitespace(parser, b, false))
+ contentHandler.ignorableWhitespace(b, 0, b.length);
+ else
+ contentHandler.characters(b, 0, b.length);
+ }
+ break;
+ case XMLStreamConstants.CDATA:
+ if (lexicalHandler != null)
+ lexicalHandler.startCDATA();
+ if (contentHandler != null)
+ {
+ char[] b = reader.getTextCharacters();
+ if (isIgnorableWhitespace(parser, b, true))
+ contentHandler.ignorableWhitespace(b, 0, b.length);
+ else
+ contentHandler.characters(b, 0, b.length);
+ }
+ if (lexicalHandler != null)
+ lexicalHandler.endCDATA();
+ break;
+ case XMLStreamConstants.START_ELEMENT:
+ if (contentHandler != null)
+ {
+ QName name = reader.getName();
+ String uri = name.getNamespaceURI();
+ String localName = name.getLocalPart();
+ String prefix = name.getPrefix();
+ String qName = localName;
+ if (!"".equals(prefix))
+ qName = prefix + ":" + localName;
+ if (!namespaceAware)
+ {
+ uri = "";
+ localName = "";
+ }
+ else
+ {
+ int nc = reader.getNamespaceCount();
+ for (int i = 0; i < nc; i++)
+ {
+ String nsuri = reader.getNamespaceURI(i);
+ String nsprefix = reader.getNamespacePrefix(i);
+ if ("xml".equals(nsprefix))
+ continue;
+ contentHandler.startPrefixMapping(nsprefix, nsuri);
+ }
+ }
+ contentHandler.startElement(uri, localName, qName, this);
+ }
+ break;
+ case XMLStreamConstants.END_ELEMENT:
+ if (contentHandler != null)
+ {
+ QName name = reader.getName();
+ String uri = name.getNamespaceURI();
+ String localName = name.getLocalPart();
+ String prefix = name.getPrefix();
+ String qName = localName;
+ if (!"".equals(prefix))
+ qName = prefix + ":" + localName;
+ if (!namespaceAware)
+ {
+ uri = "";
+ localName = "";
+ }
+ contentHandler.endElement(uri, localName, qName);
+ if (namespaceAware)
+ {
+ int nc = reader.getNamespaceCount();
+ for (int i = 0; i < nc; i++)
+ {
+ String nsprefix = reader.getNamespacePrefix(i);
+ if ("xml".equals(nsprefix))
+ continue;
+ contentHandler.endPrefixMapping(nsprefix);
+ }
+ }
+ }
+ break;
+ case XMLStreamConstants.COMMENT:
+ if (lexicalHandler != null)
+ {
+ char[] b = reader.getTextCharacters();
+ lexicalHandler.comment(b, 0, b.length);
+ }
+ break;
+ case XMLStreamConstants.PROCESSING_INSTRUCTION:
+ if (contentHandler != null)
+ {
+ String target = reader.getPITarget();
+ String data = reader.getPIData();
+ if (data == null)
+ data = "";
+ contentHandler.processingInstruction(target, data);
+ }
+ break;
+ case XMLParser.START_ENTITY:
+ if (lexicalHandler != null)
+ {
+ String name = reader.getText();
+ lexicalHandler.startEntity(name);
+ }
+ break;
+ case XMLParser.END_ENTITY:
+ if (lexicalHandler != null)
+ {
+ String name = reader.getText();
+ lexicalHandler.endEntity(name);
+ }
+ break;
+ case XMLStreamConstants.START_DOCUMENT:
+ encoding = reader.getEncoding();
+ xmlVersion = reader.getVersion();
+ xmlStandalone = reader.isStandalone();
+ xmlEncoding = reader.getCharacterEncodingScheme();
+ if (contentHandler != null)
+ contentHandler.startDocument();
+ startDocumentDone = true;
+ break;
+ case XMLStreamConstants.END_DOCUMENT:
+ if (contentHandler != null)
+ contentHandler.endDocument();
+ break;
+ case XMLStreamConstants.DTD:
+ XMLParser.Doctype doctype = parser.doctype;
+ if (lexicalHandler != null)
+ {
+ String rootName = doctype.rootName;
+ String publicId = doctype.publicId;
+ String systemId2 = doctype.systemId;
+ lexicalHandler.startDTD(rootName, publicId, systemId2);
+ }
+ for (Iterator i = doctype.entryIterator(); i.hasNext(); )
+ {
+ String entry = (String) i.next();
+ char c = entry.charAt(0);
+ String name = entry.substring(1);
+ if ('E' == c)
+ {
+ // Element decl
+ if (declHandler != null)
+ {
+ XMLParser.ContentModel model =
+ doctype.getElementModel(name);
+ declHandler.elementDecl(name, model.text);
+ }
+ }
+ else if ('A' == c)
+ {
+ // Attlist decl
+ if (declHandler != null)
+ {
+ for (Iterator j = doctype.attlistIterator(name);
+ j.hasNext(); )
+ {
+ Map.Entry att = (Map.Entry) j.next();
+ String aname = (String) att.getKey();
+ XMLParser.AttributeDecl decl =
+ (XMLParser.AttributeDecl) att.getValue();
+ String type = decl.type;
+ String value = decl.value;
+ String mode = null;
+ switch (decl.valueType)
+ {
+ case XMLParser.ATTRIBUTE_DEFAULT_FIXED:
+ mode = "#FIXED";
+ break;
+ case XMLParser.ATTRIBUTE_DEFAULT_REQUIRED:
+ mode = "#REQUIRED";
+ break;
+ case XMLParser.ATTRIBUTE_DEFAULT_IMPLIED:
+ mode = "#IMPLIED";
+ break;
+ }
+ declHandler.attributeDecl(name, aname,
+ type, mode, value);
+ }
+ }
+ }
+ else if ('e' == c)
+ {
+ // Entity decl
+ Object entity = doctype.getEntity(name);
+ if (entity instanceof String)
+ {
+ if (declHandler != null)
+ declHandler.internalEntityDecl(name,
+ (String) entity);
+ }
+ else
+ {
+ XMLParser.ExternalIds ids =
+ (XMLParser.ExternalIds) entity;
+ if (ids.notationName != null)
+ {
+ if (dtdHandler != null)
+ {
+ String pub = ids.publicId;
+ String url = ids.systemId;
+ String not = ids.notationName;
+ dtdHandler.unparsedEntityDecl(name,
+ pub,
+ url,
+ not);
+ }
+ }
+ else
+ {
+ if (declHandler != null)
+ {
+ String pub = ids.publicId;
+ String url = ids.systemId;
+ declHandler.externalEntityDecl(name,
+ pub,
+ url);
+ }
+ }
+ }
+ }
+ else if ('n' == c)
+ {
+ // Notation decl
+ if (dtdHandler != null)
+ {
+ XMLParser.ExternalIds ids =
+ doctype.getNotation(name);
+ String pub = ids.publicId;
+ String url = ids.systemId;
+ dtdHandler.notationDecl(name, pub, url);
+ }
+ }
+ else if ('c' == c)
+ {
+ // Comment
+ if (lexicalHandler != null)
+ {
+ String comment = doctype.getComment(name);
+ char[] b = comment.toCharArray();
+ lexicalHandler.comment(b, 0, b.length);
+ }
+ }
+ else if ('p' == c)
+ {
+ // Processing instruction
+ if (contentHandler != null)
+ {
+ String[] pi = doctype.getPI(name);
+ String target = pi[0];
+ String data = pi[1];
+ if (data == null)
+ data = "";
+ contentHandler.processingInstruction(target, data);
+ }
+ }
+ }
+ if (lexicalHandler != null)
+ lexicalHandler.endDTD();
+ }
+ }
+ reset();
+ if (opened)
+ in.close();
+ }
+ catch (Exception e)
+ {
+ SAXParseException e2 = new SAXParseException(e.getMessage(), this);
+ e2.initCause(e);
+ try
+ {
+ if (!startDocumentDone && contentHandler != null)
+ contentHandler.startDocument();
+ if (errorHandler != null)
+ errorHandler.fatalError(e2);
+ if (contentHandler != null)
+ contentHandler.endDocument();
+ }
+ catch (SAXException sex)
+ {
+ // Ignored, we will rethrow the original exception.
+ }
+ reset();
+ if (opened)
+ in.close();
+ if (e instanceof SAXException)
+ throw (SAXException) e;
+ if (e instanceof IOException)
+ throw (IOException) e;
+ else
+ throw e2;
+ }
+ }
+
+ /**
+ * Indicates whether the specified characters are ignorable whitespace.
+ */
+ private boolean isIgnorableWhitespace(XMLParser reader, char[] b,
+ boolean testCharacters)
+ throws XMLStreamException
+ {
+ XMLParser.Doctype doctype = reader.doctype;
+ if (doctype == null)
+ return false;
+ String currentElement = reader.getCurrentElement();
+ // check for xml:space
+ int ac = reader.getAttributeCount();
+ for (int i = 0; i < ac; i++)
+ {
+ QName aname = reader.getAttributeName(i);
+ if ("space".equals(aname.getLocalPart()) &&
+ XMLConstants.XML_NS_URI.equals(aname.getNamespaceURI()))
+ {
+ if ("preserve".equals(reader.getAttributeValue(i)))
+ return false;
+ }
+ }
+ XMLParser.ContentModel model = doctype.getElementModel(currentElement);
+ if (model == null || model.type != XMLParser.ContentModel.ELEMENT)
+ return false;
+ if (model.external && xmlStandalone)
+ return false;
+ boolean white = true;
+ if (testCharacters)
+ {
+ for (int i = 0; i < b.length; i++)
+ {
+ if (b[i] != ' ' && b[i] != '\t' && b[i] != '\n' && b[i] != '\r')
+ {
+ white = false;
+ break;
+ }
+ }
+ }
+ return white;
+ }
+
+ public void parse(String systemId)
+ throws IOException, SAXException
+ {
+ parse(new InputSource(systemId));
+ }
+
+ // -- Attributes2 --
+
+ public int getIndex(String qName)
+ {
+ int len = reader.getAttributeCount();
+ for (int i = 0; i < len; i++)
+ {
+ QName q = reader.getAttributeName(i);
+ String localName = q.getLocalPart();
+ String prefix = q.getPrefix();
+ String qn = ("".equals(prefix)) ? localName : prefix + ":" + localName;
+ if (qName.equals(qn))
+ return i;
+ }
+ return -1;
+ }
+
+ public int getIndex(String uri, String localName)
+ {
+ int len = reader.getAttributeCount();
+ for (int i = 0; i < len; i++)
+ {
+ QName q = reader.getAttributeName(i);
+ String ln = q.getLocalPart();
+ String u = q.getNamespaceURI();
+ if (u == null && uri != null)
+ continue;
+ if (u != null && !u.equals(uri))
+ continue;
+ if (ln.equals(localName))
+ return i;
+ }
+ return -1;
+ }
+
+ public int getLength()
+ {
+ return reader.getAttributeCount();
+ }
+
+ public String getLocalName(int index)
+ {
+ return reader.getAttributeLocalName(index);
+ }
+
+ public String getQName(int index)
+ {
+ QName q = reader.getAttributeName(index);
+ String localName = q.getLocalPart();
+ String prefix = q.getPrefix();
+ return ("".equals(prefix)) ? localName : prefix + ":" + localName;
+ }
+
+ public String getType(int index)
+ {
+ String ret = reader.getAttributeType(index);
+ // SAX doesn't permit ENUMERATION?
+ return ("ENUMERATION".equals(ret)) ? "NMTOKEN" : ret;
+ }
+
+ public String getType(String qName)
+ {
+ int index = getIndex(qName);
+ return (index == -1) ? null : getType(index);
+ }
+
+ public String getType(String uri, String localName)
+ {
+ int index = getIndex(uri, localName);
+ return (index == -1) ? null : getType(index);
+ }
+
+ public String getURI(int index)
+ {
+ String ret = reader.getAttributeNamespace(index);
+ return (ret == null) ? "" : ret;
+ }
+
+ public String getValue(int index)
+ {
+ return reader.getAttributeValue(index);
+ }
+
+ public String getValue(String qName)
+ {
+ int index = getIndex(qName);
+ return (index == -1) ? null : getValue(index);
+ }
+
+ public String getValue(String uri, String localName)
+ {
+ int index = getIndex(uri, localName);
+ return (index == -1) ? null : getValue(index);
+ }
+
+ public boolean isDeclared(int index)
+ {
+ return parser.isAttributeDeclared(index);
+ }
+
+ public boolean isDeclared(String qName)
+ {
+ int index = getIndex(qName);
+ return (index == -1) ? false : isDeclared(index);
+ }
+
+ public boolean isDeclared(String uri, String localName)
+ {
+ int index = getIndex(uri, localName);
+ return (index == -1) ? false : isDeclared(index);
+ }
+
+ public boolean isSpecified(int index)
+ {
+ return reader.isAttributeSpecified(index);
+ }
+
+ public boolean isSpecified(String qName)
+ {
+ int index = getIndex(qName);
+ return (index == -1) ? false : isSpecified(index);
+ }
+
+ public boolean isSpecified(String uri, String localName)
+ {
+ int index = getIndex(uri, localName);
+ return (index == -1) ? false : isSpecified(index);
+ }
+
+ // -- Locator2 --
+
+ public int getColumnNumber()
+ {
+ Location l = reader.getLocation();
+ return l.getColumnNumber();
+ }
+
+ public int getLineNumber()
+ {
+ Location l = reader.getLocation();
+ return l.getLineNumber();
+ }
+
+ public String getPublicId()
+ {
+ Location l = reader.getLocation();
+ return l.getPublicId();
+ }
+
+ public String getSystemId()
+ {
+ Location l = reader.getLocation();
+ return l.getSystemId();
+ }
+
+ public String getEncoding()
+ {
+ return encoding;
+ }
+
+ public String getXMLVersion()
+ {
+ return xmlVersion;
+ }
+
+ // -- XMLResolver --
+
+ public Object resolveEntity(String publicId, String systemId,
+ String baseURI, String namespace)
+ throws XMLStreamException
+ {
+ if (entityResolver != null)
+ {
+ try
+ {
+ InputSource input =
+ entityResolver.resolveEntity(publicId, systemId);
+ if (input != null)
+ {
+ InputStream in = input.getByteStream();
+ if (in == null)
+ {
+ String newSystemId = input.getSystemId();
+ if (newSystemId != null && !newSystemId.equals(systemId))
+ in = XMLParser.resolve(newSystemId);
+ }
+ return in;
+ }
+ }
+ catch (SAXException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+ return null;
+ }
+
+ public XMLEventReader resolveAsXMLEventReader(String uri)
+ throws XMLStreamException
+ {
+ // unused
+ return null;
+ }
+
+ public XMLStreamReader resolveAsXMLStreamReader(String uri)
+ throws XMLStreamException
+ {
+ // unused
+ return null;
+ }
+
+ // -- XMLReporter --
+
+ public void report(String message, String errorType,
+ Object relatedInformation, Location location)
+ throws XMLStreamException
+ {
+ if (errorHandler != null)
+ {
+ try
+ {
+ errorHandler.warning(new SAXParseException(message, this));
+ }
+ catch (SAXException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+ }
+
+ public static void main(String[] args)
+ throws Exception
+ {
+ boolean validating = false;
+ boolean namespaceAware = false;
+ boolean xIncludeAware = false;
+ boolean expectCallbackClass = false;
+ String callbackClass = null;
+ int pos = 0;
+ while (pos < args.length && (args[pos].startsWith("-") || expectCallbackClass))
+ {
+ if ("-x".equals(args[pos]))
+ xIncludeAware = true;
+ else if ("-v".equals(args[pos]))
+ validating = true;
+ else if ("-n".equals(args[pos]))
+ namespaceAware = true;
+ else if ("-c".equals(args[pos]))
+ expectCallbackClass = true;
+ else if (expectCallbackClass)
+ {
+ callbackClass = args[pos];
+ expectCallbackClass = false;
+ }
+ pos++;
+ }
+ if (pos >= args.length || expectCallbackClass)
+ {
+ System.out.println("Syntax: SAXParser [-n] [-v] [-x] [-c <class>] <file> [<file2> [...]]");
+ System.out.println("\t-n: use namespace aware mode");
+ System.out.println("\t-v: use validating parser");
+ System.out.println("\t-x: use XInclude aware mode");
+ System.out.println("\t-c <class>: use specified class as callback handler (must have a no-arg public constructor)");
+ System.exit(2);
+ }
+ while (pos < args.length)
+ {
+ ContentHandler handler = null;
+ if (callbackClass != null)
+ {
+ Class t = Class.forName(callbackClass);
+ handler = (ContentHandler) t.newInstance();
+ }
+ else
+ handler = new org.xml.sax.helpers.DefaultHandler();
+ SAXParser parser = new SAXParser(validating, namespaceAware,
+ xIncludeAware);
+ InputSource input = new InputSource(args[pos]);
+ java.io.FileReader fr = new java.io.FileReader(args[pos]);
+ input.setCharacterStream(fr);
+ try
+ {
+ XMLReader reader = parser.getXMLReader();
+ reader.setContentHandler(handler);
+ reader.parse(input);
+ }
+ finally
+ {
+ fr.close();
+ }
+ pos++;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/SAXParserFactory.java b/libjava/classpath/gnu/xml/stream/SAXParserFactory.java
new file mode 100644
index 000000000..ebaeb3cb2
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/SAXParserFactory.java
@@ -0,0 +1,104 @@
+/* SAXParserFactory.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+
+/**
+ * SAX parser factory providing a SAX compatibility layer on top of StAX.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class SAXParserFactory
+ extends javax.xml.parsers.SAXParserFactory
+{
+
+ static final Set FEATURE_NAMES = new HashSet();
+ static
+ {
+ FEATURE_NAMES.add("http://xml.org/sax/features/namespaces");
+ FEATURE_NAMES.add("http://xml.org/sax/features/string-interning");
+ FEATURE_NAMES.add("http://xml.org/sax/features/validation");
+ }
+
+ Map features = new HashMap();
+
+ public javax.xml.parsers.SAXParser newSAXParser()
+ throws ParserConfigurationException, SAXException
+ {
+ boolean validating = isValidating();
+ boolean namespaceAware = isNamespaceAware();
+ boolean xIncludeAware = isXIncludeAware();
+ SAXParser ret = new SAXParser(validating, namespaceAware, xIncludeAware);
+ for (Iterator i = features.entrySet().iterator(); i.hasNext(); )
+ {
+ Map.Entry entry = (Map.Entry) i.next();
+ String name = (String) entry.getKey();
+ Boolean value = (Boolean) entry.getValue();
+ ret.setFeature(name, value.booleanValue());
+ }
+ return ret;
+ }
+
+ public void setFeature(String name, boolean value)
+ throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException
+ {
+ if (!FEATURE_NAMES.contains(name))
+ throw new SAXNotSupportedException(name);
+ features.put(name, value ? Boolean.TRUE : Boolean.FALSE);
+ }
+
+ public boolean getFeature(String name)
+ throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException
+ {
+ if (!FEATURE_NAMES.contains(name))
+ throw new SAXNotSupportedException(name);
+ Boolean value = (Boolean) features.get(name);
+ return (value == null) ? false : value.booleanValue();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/StartDocumentImpl.java b/libjava/classpath/gnu/xml/stream/StartDocumentImpl.java
new file mode 100644
index 000000000..d2e590cfc
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/StartDocumentImpl.java
@@ -0,0 +1,143 @@
+/* StartDocumentImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.StartDocument;
+
+/**
+ * A start-document event.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class StartDocumentImpl
+ extends XMLEventImpl
+ implements StartDocument
+{
+
+ protected final String systemId;
+ protected final String encoding;
+ protected final String xmlVersion;
+ protected final boolean xmlStandalone;
+ protected final boolean standaloneDeclared;
+ protected final boolean encodingDeclared;
+
+ protected StartDocumentImpl(Location location,
+ String systemId, String encoding,
+ String xmlVersion, boolean xmlStandalone,
+ boolean standaloneDeclared,
+ boolean encodingDeclared)
+ {
+ super(location);
+ this.systemId = systemId;
+ this.encoding = encoding;
+ this.xmlVersion = xmlVersion;
+ this.xmlStandalone = xmlStandalone;
+ this.standaloneDeclared = standaloneDeclared;
+ this.encodingDeclared = encodingDeclared;
+ }
+
+ public int getEventType()
+ {
+ return START_DOCUMENT;
+ }
+
+ public String getSystemId()
+ {
+ return systemId;
+ }
+
+ public String getCharacterEncodingScheme()
+ {
+ return encoding;
+ }
+
+ public boolean encodingSet()
+ {
+ return encodingDeclared;
+ }
+
+ public boolean isStandalone()
+ {
+ return xmlStandalone;
+ }
+
+ public boolean standaloneSet()
+ {
+ return standaloneDeclared;
+ }
+
+ public String getVersion()
+ {
+ return xmlVersion;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("<?xml version='");
+ writer.write(xmlVersion);
+ writer.write('\'');
+ if (standaloneDeclared)
+ {
+ writer.write(" standalone='");
+ writer.write(xmlStandalone ? "yes" : "no");
+ writer.write('\'');
+ }
+ if (encodingDeclared)
+ {
+ writer.write(" encoding='");
+ writer.write(encoding);
+ writer.write('\'');
+ }
+ writer.write("?>");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/StartElementImpl.java b/libjava/classpath/gnu/xml/stream/StartElementImpl.java
new file mode 100644
index 000000000..66e68d560
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/StartElementImpl.java
@@ -0,0 +1,152 @@
+/* StartElementImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.Namespace;
+import javax.xml.stream.events.StartElement;
+
+/**
+ * A start-element event.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class StartElementImpl
+ extends XMLEventImpl
+ implements StartElement
+{
+
+ protected final QName name;
+ protected final List attributes;
+ protected final List namespaces;
+ protected final NamespaceContext namespaceContext;
+
+ protected StartElementImpl(Location location,
+ QName name, List attributes, List namespaces,
+ NamespaceContext namespaceContext)
+ {
+ super(location);
+ this.name = name;
+ this.attributes = attributes;
+ this.namespaces = namespaces;
+ this.namespaceContext = namespaceContext;
+ }
+
+ public int getEventType()
+ {
+ return START_ELEMENT;
+ }
+
+ public QName getName()
+ {
+ return name;
+ }
+
+ public Iterator getAttributes()
+ {
+ return attributes.iterator();
+ }
+
+ public Iterator getNamespaces()
+ {
+ return namespaces.iterator();
+ }
+
+ public Attribute getAttributeByName(QName name)
+ {
+ for (Iterator i = attributes.iterator(); i.hasNext(); )
+ {
+ Attribute attr = (Attribute) i.next();
+ if (name.equals(attr.getName()))
+ return attr;
+ }
+ return null;
+ }
+
+ public NamespaceContext getNamespaceContext()
+ {
+ return namespaceContext;
+ }
+
+ public String getNamespaceURI(String prefix)
+ {
+ return namespaceContext.getNamespaceURI(prefix);
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write('<');
+ String prefix = name.getPrefix();
+ if (prefix != null && !"".equals(prefix))
+ {
+ writer.write(prefix);
+ writer.write(':');
+ }
+ writer.write(name.getLocalPart());
+ for (Iterator i = namespaces.iterator(); i.hasNext(); )
+ {
+ writer.write(' ');
+ ((Namespace) i.next()).writeAsEncodedUnicode(writer);
+ }
+ for (Iterator i = attributes.iterator(); i.hasNext(); )
+ {
+ writer.write(' ');
+ ((Attribute) i.next()).writeAsEncodedUnicode(writer);
+ }
+ writer.write('>');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/UnicodeReader.java b/libjava/classpath/gnu/xml/stream/UnicodeReader.java
new file mode 100644
index 000000000..aa2a95422
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/UnicodeReader.java
@@ -0,0 +1,205 @@
+/* UnicodeReader.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * A reader that converts UTF-16 characters to Unicode code points.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class UnicodeReader
+{
+
+ final Reader in;
+
+ UnicodeReader(Reader in)
+ {
+ this.in = in;
+ }
+
+ public void mark(int limit)
+ throws IOException
+ {
+ in.mark(limit * 2);
+ }
+
+ public void reset()
+ throws IOException
+ {
+ in.reset();
+ }
+
+ public int read()
+ throws IOException
+ {
+ int ret = in.read();
+ if (ret == -1)
+ return ret;
+ if (ret >= 0xd800 && ret < 0xdc00)
+ {
+ // Unicode surrogate?
+ int low = in.read();
+ if (low >= 0xdc00 && low < 0xe000)
+ ret = Character.toCodePoint((char) ret, (char) low);
+ else
+ throw new IOException("unpaired surrogate: U+" +
+ Integer.toHexString(ret));
+ }
+ else if (ret >= 0xdc00 && ret < 0xe000)
+ throw new IOException("unpaired surrogate: U+" +
+ Integer.toHexString(ret));
+ return ret;
+ }
+
+ public int read(int[] buf, int off, int len)
+ throws IOException
+ {
+ if (len == 0)
+ return 0;
+ char[] b2 = new char[len];
+ int ret = in.read(b2, 0, len);
+ if (ret <= 0)
+ return ret;
+ int l = ret - 1;
+ int i = 0, j = off;
+ for (; i < l; i++)
+ {
+ char c = b2[i];
+ if (c >= 0xd800 && c < 0xdc00)
+ {
+ // Unicode surrogate?
+ char d = b2[i + 1];
+ if (d >= 0xdc00 && d < 0xe000)
+ {
+ buf[j++] = Character.toCodePoint(c, d);
+ i++;
+ continue;
+ }
+ else
+ throw new IOException("unpaired surrogate: U+" +
+ Integer.toHexString(c));
+ }
+ else if (c >= 0xdc00 && c < 0xe000)
+ throw new IOException("unpaired surrogate: U+" +
+ Integer.toHexString(c));
+ buf[j++] = (int) c;
+ }
+ if (i == l)
+ {
+ // last char
+ char c = b2[l];
+ if (c >= 0xd800 && c < 0xdc00)
+ {
+ int low = in.read();
+ if (low >= 0xdc00 && low < 0xe000)
+ {
+ buf[j++] = Character.toCodePoint(c, (char) low);
+ return j;
+ }
+ else
+ throw new IOException("unpaired surrogate: U+" +
+ Integer.toHexString(c));
+ }
+ else if (c >= 0xdc00 && c < 0xe000)
+ throw new IOException("unpaired surrogate: U+" +
+ Integer.toHexString(c));
+ buf[j++] = (int) c;
+ }
+ return j;
+ }
+
+ public void close()
+ throws IOException
+ {
+ in.close();
+ }
+
+ /**
+ * Returns the specified UTF-16 char array as an array of Unicode code
+ * points.
+ */
+ public static int[] toCodePointArray(String text)
+ throws IOException
+ {
+ char[] b2 = text.toCharArray();
+ int[] buf = new int[b2.length];
+ if (b2.length > 0)
+ {
+ int l = b2.length - 1;
+ int i = 0, j = 0;
+ for (; i < l; i++)
+ {
+ char c = b2[i];
+ if (c >= 0xd800 && c < 0xdc00)
+ {
+ // Unicode surrogate?
+ char d = b2[i + 1];
+ if (d >= 0xdc00 && d < 0xe000)
+ {
+ buf[j++] = Character.toCodePoint(c, d);
+ i++;
+ continue;
+ }
+ else
+ throw new IOException("unpaired surrogate: U+" +
+ Integer.toHexString(c));
+ }
+ else if (c >= 0xdc00 && c < 0xe000)
+ throw new IOException("unpaired surrogate: U+" +
+ Integer.toHexString(c));
+ buf[j++] = (int) c;
+ }
+ if (i == l)
+ {
+ // last char
+ buf[j++] = (int) b2[l];
+ if (j < buf.length)
+ {
+ int[] buf2 = new int[j];
+ System.arraycopy(buf, 0, buf2, 0, j);
+ buf = buf2;
+ }
+ }
+ }
+ return buf;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/XIncludeFilter.java b/libjava/classpath/gnu/xml/stream/XIncludeFilter.java
new file mode 100644
index 000000000..6d955c492
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/XIncludeFilter.java
@@ -0,0 +1,931 @@
+/* XIncludeFilter.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.HashSet;
+import java.util.NoSuchElementException;
+import java.util.StringTokenizer;
+import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.util.StreamReaderDelegate;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.ProcessingInstruction;
+import org.w3c.dom.TypeInfo;
+import org.w3c.dom.traversal.DocumentTraversal;
+import org.w3c.dom.traversal.NodeFilter;
+import org.w3c.dom.traversal.TreeWalker;
+import org.w3c.dom.xpath.XPathEvaluator;
+import org.w3c.dom.xpath.XPathNSResolver;
+import org.w3c.dom.xpath.XPathResult;
+import org.xml.sax.SAXException;
+
+/**
+ * StAX filter for performing XInclude processing.
+ *
+ * @see http://www.w3.org/TR/xinclude/
+ * @see http://www.w3.org/TR/xptr-framework/
+ * @see http://www.w3.org/TR/xptr-element/
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class XIncludeFilter
+ extends StreamReaderDelegate
+{
+
+ static final String XINCLUDE_NS_URI = "http://www.w3.org/2001/XInclude";
+ static final int SHOW_FLAGS =
+ NodeFilter.SHOW_CDATA_SECTION |
+ NodeFilter.SHOW_COMMENT |
+ NodeFilter.SHOW_ELEMENT |
+ NodeFilter.SHOW_ENTITY_REFERENCE |
+ NodeFilter.SHOW_PROCESSING_INSTRUCTION |
+ NodeFilter.SHOW_TEXT;
+
+ final String systemId;
+ final boolean namespaceAware;
+ final boolean validating;
+ final boolean expandERefs;
+ String href;
+ int event;
+ boolean included;
+ XPathResult result;
+ int snapshotIndex;
+ Node current;
+ TreeWalker walker;
+ HashSet seen = new HashSet();
+ boolean backtracking;
+ boolean lookahead;
+
+ Reader includedText;
+ char[] buf;
+ int len = -1;
+ boolean inInclude, inFallback, seenFallback;
+
+ DocumentBuilder builder;
+
+ XIncludeFilter(XMLStreamReader reader, String systemId,
+ boolean namespaceAware, boolean validating,
+ boolean expandERefs)
+ {
+ super(reader);
+ this.systemId = XMLParser.absolutize(null, systemId);
+ this.namespaceAware = namespaceAware;
+ this.validating = validating;
+ this.expandERefs = expandERefs;
+ }
+
+ public int getAttributeCount()
+ {
+ if (current != null)
+ {
+ NamedNodeMap attrs = current.getAttributes();
+ return (attrs == null) ? 0 : attrs.getLength();
+ }
+ return super.getAttributeCount();
+ }
+
+ public String getAttributeLocalName(int index)
+ {
+ if (current != null)
+ {
+ NamedNodeMap attrs = current.getAttributes();
+ if (attrs == null)
+ return null;
+ Node attr = attrs.item(index);
+ return attr.getLocalName();
+ }
+ return super.getAttributeLocalName(index);
+ }
+
+ public String getAttributeNamespace(int index)
+ {
+ if (current != null)
+ {
+ NamedNodeMap attrs = current.getAttributes();
+ if (attrs == null)
+ return null;
+ Node attr = attrs.item(index);
+ return attr.getNamespaceURI();
+ }
+ return super.getAttributeNamespace(index);
+ }
+
+ public String getAttributePrefix(int index)
+ {
+ if (current != null)
+ {
+ NamedNodeMap attrs = current.getAttributes();
+ if (attrs == null)
+ return null;
+ Node attr = attrs.item(index);
+ return attr.getPrefix();
+ }
+ return super.getAttributePrefix(index);
+ }
+
+ public QName getAttributeName(int index)
+ {
+ if (current != null)
+ {
+ NamedNodeMap attrs = current.getAttributes();
+ if (attrs == null)
+ return null;
+ Node attr = attrs.item(index);
+ String localName = attr.getLocalName();
+ String uri = attr.getNamespaceURI();
+ String prefix = attr.getPrefix();
+ return new QName(uri, localName, prefix);
+ }
+ return super.getAttributeName(index);
+ }
+
+ public String getAttributeType(int index)
+ {
+ if (current != null)
+ {
+ NamedNodeMap attrs = current.getAttributes();
+ if (attrs == null)
+ return null;
+ Attr attr = (Attr) attrs.item(index);
+ TypeInfo ti = attr.getSchemaTypeInfo();
+ return (ti == null) ? "CDATA" : ti.getTypeName();
+ }
+ return super.getAttributeType(index);
+ }
+
+ public boolean isAttributeSpecified(int index)
+ {
+ if (current != null)
+ {
+ NamedNodeMap attrs = current.getAttributes();
+ if (attrs == null)
+ return false;
+ Attr attr = (Attr) attrs.item(index);
+ return attr.getSpecified();
+ }
+ return super.isAttributeSpecified(index);
+ }
+
+ public String getAttributeValue(int index)
+ {
+ if (current != null)
+ {
+ NamedNodeMap attrs = current.getAttributes();
+ if (attrs == null)
+ return null;
+ Node attr = attrs.item(index);
+ return attr.getNodeValue();
+ }
+ return super.getAttributeValue(index);
+ }
+
+ public String getAttributeValue(String uri, String localName)
+ {
+ if (current != null)
+ {
+ NamedNodeMap attrs = current.getAttributes();
+ if (attrs == null)
+ return null;
+ Node attr = attrs.getNamedItemNS(uri, localName);
+ return (attr == null) ? null : attr.getNodeValue();
+ }
+ return super.getAttributeValue(uri, localName);
+ }
+
+ public String getElementText()
+ throws XMLStreamException
+ {
+ if (current != null)
+ return current.getTextContent();
+ return super.getElementText();
+ }
+
+ public int getEventType()
+ {
+ return event;
+ }
+
+ public String getLocalName()
+ {
+ if (current != null)
+ return current.getLocalName();
+ return super.getLocalName();
+ }
+
+ public QName getName()
+ {
+ if (current != null)
+ {
+ String localName = current.getLocalName();
+ String uri = current.getNamespaceURI();
+ String prefix = current.getPrefix();
+ return new QName(uri, localName, prefix);
+ }
+ return super.getName();
+ }
+
+ public String getNamespaceURI()
+ {
+ if (current != null)
+ return current.getNamespaceURI();
+ return super.getNamespaceURI();
+ }
+
+ // TODO namespaces
+
+ public String getPIData()
+ {
+ if (current != null)
+ return ((ProcessingInstruction) current).getData();
+ return super.getPIData();
+ }
+
+ public String getPITarget()
+ {
+ if (current != null)
+ return ((ProcessingInstruction) current).getTarget();
+ return super.getPITarget();
+ }
+
+ public String getPrefix()
+ {
+ if (current != null)
+ return current.getPrefix();
+ return super.getPrefix();
+ }
+
+ public String getText()
+ {
+ if (current != null)
+ return current.getNodeValue();
+ if (walker != null)
+ {
+ Node n = walker.getCurrentNode();
+ if (n != null)
+ return n.getTextContent();
+ }
+ if (buf != null)
+ return new String(buf, 0, len);
+ return super.getText();
+ }
+
+ public char[] getTextCharacters()
+ {
+ if (current != null)
+ {
+ buf = current.getNodeValue().toCharArray();
+ len = buf.length;
+ }
+ if (buf != null)
+ return buf;
+ return super.getTextCharacters();
+ }
+
+ public int getTextCharacters(int sourceStart, char[] target,
+ int targetStart, int length)
+ throws XMLStreamException
+ {
+ if (current != null)
+ {
+ buf = current.getNodeValue().toCharArray();
+ len = buf.length;
+ }
+ if (buf != null)
+ {
+ int max = Math.min(len - sourceStart, length);
+ if (max > 0)
+ System.arraycopy(buf, sourceStart, target, targetStart, max);
+ return max;
+ }
+ return super.getTextCharacters(sourceStart, target, targetStart, length);
+ }
+
+ public int getTextLength()
+ {
+ if (current != null)
+ {
+ buf = current.getNodeValue().toCharArray();
+ len = buf.length;
+ }
+ if (buf != null)
+ return len;
+ return super.getTextLength();
+ }
+
+ public int getTextStart()
+ {
+ if (current != null)
+ {
+ buf = current.getNodeValue().toCharArray();
+ len = buf.length;
+ }
+ if (buf != null)
+ return 0;
+ return super.getTextStart();
+ }
+
+ public boolean hasNext()
+ throws XMLStreamException
+ {
+ if (!lookahead)
+ {
+ try
+ {
+ next();
+ }
+ catch (NoSuchElementException e)
+ {
+ event = -1;
+ }
+ lookahead = true;
+ }
+ return (event != -1);
+ }
+
+ public int next()
+ throws XMLStreamException
+ {
+ if (lookahead)
+ {
+ lookahead = false;
+ return event;
+ }
+ buf = null;
+ len = 0;
+ if (walker != null)
+ {
+ Node c = walker.getCurrentNode();
+ Node n = null;
+ if (c.getNodeType() == Node.ELEMENT_NODE)
+ {
+ boolean isStartElement = !seen.contains(c);
+ if (isStartElement)
+ {
+ seen.add(c);
+ current = c;
+ event = XMLStreamConstants.START_ELEMENT;
+ return event;
+ }
+ else if (backtracking)
+ {
+ n = walker.nextSibling();
+ if (n != null)
+ backtracking = false;
+ }
+ else
+ {
+ n = walker.firstChild();
+ if (n == null)
+ n = walker.nextSibling();
+ }
+ }
+ else
+ {
+ n = walker.firstChild();
+ if (n == null)
+ n = walker.nextSibling();
+ }
+ if (n == null)
+ {
+ current = walker.parentNode();
+ if (current != null && current.getNodeType() == Node.ELEMENT_NODE)
+ {
+ // end-element
+ backtracking = true;
+ event = XMLStreamConstants.END_ELEMENT;
+ return event;
+ }
+ else
+ {
+ walker = null;
+ current = null;
+ }
+ }
+ else
+ {
+ current = n;
+ switch (n.getNodeType())
+ {
+ case Node.ELEMENT_NODE:
+ return next();
+ case Node.TEXT_NODE:
+ String text = n.getNodeValue();
+ buf = text.toCharArray();
+ len = buf.length;
+ event = isSpace(buf, len) ?
+ XMLStreamConstants.SPACE :
+ XMLStreamConstants.CHARACTERS;
+ return event;
+ case Node.CDATA_SECTION_NODE:
+ event = XMLStreamConstants.CDATA;
+ return event;
+ case Node.COMMENT_NODE:
+ event = XMLStreamConstants.COMMENT;
+ return event;
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ event = XMLStreamConstants.PROCESSING_INSTRUCTION;
+ return event;
+ case Node.ENTITY_REFERENCE_NODE:
+ event = XMLStreamConstants.ENTITY_REFERENCE;
+ return event;
+ default:
+ throw new IllegalStateException();
+ }
+ }
+ }
+ if (result != null)
+ {
+ switch (result.getResultType())
+ {
+ case XPathResult.BOOLEAN_TYPE:
+ boolean bval = result.getBooleanValue();
+ String btext = bval ? "true" : "false";
+ buf = btext.toCharArray();
+ len = buf.length;
+ result = null;
+ event = XMLStreamConstants.CHARACTERS;
+ return event;
+ case XPathResult.NUMBER_TYPE:
+ double nval = result.getNumberValue();
+ String ntext = Double.toString(nval);
+ buf = ntext.toCharArray();
+ len = buf.length;
+ result = null;
+ event = XMLStreamConstants.CHARACTERS;
+ return event;
+ case XPathResult.STRING_TYPE:
+ String stext = result.getStringValue();
+ buf = stext.toCharArray();
+ len = buf.length;
+ result = null;
+ event = isSpace(buf, len) ?
+ XMLStreamConstants.SPACE :
+ XMLStreamConstants.CHARACTERS;
+ return event;
+ case XPathResult.ANY_UNORDERED_NODE_TYPE:
+ case XPathResult.FIRST_ORDERED_NODE_TYPE:
+ Node n1 = result.getSingleNodeValue();
+ Document d1 = getDocument(n1);
+ walker = getDocumentTraversal(d1)
+ .createTreeWalker(n1, SHOW_FLAGS, null, expandERefs);
+ result = null;
+ return next();
+ case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
+ Node n2 = result.iterateNext();
+ if (n2 == null)
+ {
+ result = null;
+ return next();
+ }
+ Document d2 = getDocument(n2);
+ walker = getDocumentTraversal(d2)
+ .createTreeWalker(n2, SHOW_FLAGS, null, expandERefs);
+ return next();
+ case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
+ case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
+ Node n3 = result.snapshotItem(snapshotIndex++);
+ if (n3 == null)
+ {
+ result = null;
+ return next();
+ }
+ Document d3 = getDocument(n3);
+ walker = getDocumentTraversal(d3)
+ .createTreeWalker(n3, SHOW_FLAGS, null, expandERefs);
+ return next();
+ default:
+ throw new IllegalStateException();
+ }
+ }
+ if (includedText != null)
+ {
+ // fill buffer
+ if (buf == null)
+ buf = new char[2048];
+ try
+ {
+ len = includedText.read(buf, 0, buf.length);
+ if (len == -1)
+ {
+ includedText = null;
+ buf = null;
+ return next();
+ }
+ // chars or space?
+ return (event = isSpace(buf, len) ?
+ XMLStreamConstants.SPACE :
+ XMLStreamConstants.CHARACTERS);
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+ event = super.next();
+ switch (event)
+ {
+ case XMLStreamConstants.START_ELEMENT:
+ String uri = getNamespaceURI();
+ if (XINCLUDE_NS_URI.equals(uri))
+ {
+ String localName = getLocalName();
+ if ("include".equals(localName))
+ {
+ href = getAttributeValue(null, "href");
+ String parse = getAttributeValue(null, "parse");
+ String xpointer = getAttributeValue(null, "xpointer");
+ String encoding = getAttributeValue(null, "encoding");
+ String accept = getAttributeValue(null, "accept");
+ String acceptLanguage = getAttributeValue(null,
+ "accept-language");
+ if (includeResource(href, parse, xpointer, encoding,
+ accept, acceptLanguage))
+ {
+ // Skip to xi:include end-element event
+ int depth = 0;
+ while (depth >= 0)
+ {
+ event = super.next();
+ switch (event)
+ {
+ case XMLStreamConstants.START_ELEMENT:
+ depth++;
+ break;
+ case XMLStreamConstants.END_ELEMENT:
+ depth--;
+ }
+ }
+ }
+ else
+ inInclude = true;
+ }
+ else if (inInclude && "fallback".equals(localName))
+ {
+ if (!seenFallback)
+ inFallback = seenFallback = true;
+ else
+ throw new XMLStreamException("duplicate xi:fallback element");
+ }
+ else if (inInclude)
+ {
+ throw new XMLStreamException("illegal xi element '" +
+ localName + "'");
+ }
+ return next();
+ }
+ break;
+ case XMLStreamConstants.END_ELEMENT:
+ String uri2 = getNamespaceURI();
+ if (XINCLUDE_NS_URI.equals(uri2))
+ {
+ String localName = getLocalName();
+ if ("include".equals(localName))
+ {
+ if (!seenFallback && included)
+ {
+ String msg = "Unable to read " + href +
+ " and no xi:fallback element present";
+ throw new XMLStreamException(msg);
+ }
+ included = false;
+ href = null;
+ inInclude = inFallback = seenFallback = false;
+ }
+ else if ("fallback".equals(localName))
+ inFallback = false;
+ return next();
+ }
+ break;
+ }
+ if (inInclude && !inFallback)
+ return next();
+ return event;
+ }
+
+ boolean isSpace(char[] text, int len)
+ {
+ boolean space = true;
+ for (int i = 0; i < len; i++)
+ {
+ char c = text[i];
+ if (c != ' ' && c != '\t' && c != '\n' && c != '\r')
+ {
+ space = false;
+ break;
+ }
+ }
+ return space;
+ }
+
+ String getBaseURI()
+ {
+ String base = (String) getParent().getProperty("gnu.xml.stream.baseURI");
+ return (base == null) ? systemId : base;
+ }
+
+ boolean includeResource(String href, String parse, String xpointer,
+ String encoding, String accept,
+ String acceptLanguage)
+ {
+ included = false;
+ try
+ {
+ if (xpointer != null)
+ throw new XMLStreamException("xpointer attribute not yet supported");
+ String base = getBaseURI();
+ if (href == null || "".equals(href))
+ href = base;
+ else
+ href = XMLParser.absolutize(base, href);
+ if (parse == null || "xml".equals(parse))
+ {
+ seen.clear();
+ result = null;
+ snapshotIndex = 0;
+ walker = null;
+ current = null;
+ backtracking = false;
+
+ URLConnection connection = getURLConnection(href, accept,
+ acceptLanguage);
+ InputStream in = connection.getInputStream();
+ Document doc = getDocumentBuilder().parse(in, href);
+ DocumentTraversal dt = getDocumentTraversal(doc);
+ if (xpointer == null)
+ {
+ result = null;
+ Node item = doc.getDocumentElement();
+ walker = dt.createTreeWalker(item, SHOW_FLAGS, null,
+ expandERefs);
+ }
+ else
+ {
+ result = null;
+ snapshotIndex = 0;
+ walker = null;
+ // shorthand or scheme-based?
+ int lpi = xpointer.indexOf('(');
+ int rpi = xpointer.indexOf(')', lpi);
+ if (lpi != -1 && rpi != -1)
+ {
+ String scheme = xpointer.substring(0, lpi);
+ if ("element".equals(scheme))
+ {
+ // element() scheme
+ String elementSchemeData =
+ xpointer.substring(lpi + 1, rpi);
+ Node item = doc;
+ int si = elementSchemeData.indexOf('/');
+ if (si == -1)
+ {
+ if (elementSchemeData.length() > 0)
+ item = doc.getElementById(elementSchemeData);
+ }
+ else
+ {
+ if (si > 0)
+ {
+ String context =
+ elementSchemeData.substring(0, si);
+ item = doc.getElementById(context);
+ elementSchemeData =
+ elementSchemeData.substring(si + 1);
+ }
+ StringTokenizer st =
+ new StringTokenizer(elementSchemeData, "/");
+ while (st.hasMoreTokens() && item != null)
+ {
+ int n = Integer.parseInt(st.nextToken());
+ Node ctx = item.getFirstChild();
+ int count = 1;
+ while (ctx != null && count++ < n)
+ ctx = ctx.getNextSibling();
+ item = ctx;
+ }
+ }
+ walker = dt.createTreeWalker(item, SHOW_FLAGS, null,
+ expandERefs);
+ included = true;
+ }
+ else if ("xpointer".equals(scheme))
+ {
+ xpointer = xpointer.substring(lpi + 1, rpi);
+ XPathEvaluator eval = getXPathEvaluator(doc);
+ XPathNSResolver resolver = eval.createNSResolver(doc);
+ result =
+ (XPathResult) eval.evaluate(xpointer, doc,
+ resolver,
+ XPathResult.ANY_TYPE,
+ null);
+ // TODO xpointer() scheme functions
+ included = true;
+ }
+ else
+ {
+ String msg = "Unknown XPointer scheme: " + scheme;
+ throw new XMLStreamException(msg);
+ }
+ }
+ else
+ {
+ Node item = doc.getElementById(xpointer);
+ walker = dt.createTreeWalker(item, SHOW_FLAGS, null,
+ expandERefs);
+ included = true;
+ }
+ }
+ }
+ else if ("text".equals(parse))
+ {
+ URLConnection connection = getURLConnection(href, accept,
+ acceptLanguage);
+ InputStream in = connection.getInputStream();
+ if (encoding == null)
+ {
+ encoding = connection.getContentEncoding();
+ if (encoding == null)
+ {
+ String contentType = connection.getContentType();
+ if (contentType != null)
+ encoding = getParameter(contentType, "charset");
+ }
+ }
+ if (encoding == null)
+ includedText = new InputStreamReader(in, "UTF-8");
+ else
+ includedText = new InputStreamReader(in, encoding);
+ included = true;
+ }
+ else
+ throw new XMLStreamException("value of 'parse' attribute must be "+
+ "'xml' or 'text'");
+ return true;
+ }
+ catch (IOException e)
+ {
+ return false;
+ }
+ catch (XMLStreamException e)
+ {
+ return false;
+ }
+ catch (SAXException e)
+ {
+ return false;
+ }
+ }
+
+ URLConnection getURLConnection(String href, String accept,
+ String acceptLanguage)
+ throws IOException
+ {
+ URL url = new URL(href);
+ URLConnection connection = url.openConnection();
+ if (connection instanceof HttpURLConnection)
+ {
+ HttpURLConnection http = (HttpURLConnection) connection;
+ http.setInstanceFollowRedirects(true);
+ if (accept != null)
+ http.setRequestProperty("Accept", accept);
+ if (acceptLanguage != null)
+ http.setRequestProperty("Accept-Language", acceptLanguage);
+ }
+ return connection;
+ }
+
+ Document getDocument(Node node)
+ {
+ if (node.getNodeType() == Node.DOCUMENT_NODE)
+ return (Document) node;
+ return node.getOwnerDocument();
+ }
+
+ DocumentBuilder getDocumentBuilder()
+ throws XMLStreamException
+ {
+ if (builder == null)
+ {
+ try
+ {
+ DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
+ f.setXIncludeAware(true);
+ f.setNamespaceAware(namespaceAware);
+ f.setValidating(validating);
+ builder = f.newDocumentBuilder();
+ }
+ catch (ParserConfigurationException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+ builder.reset();
+ return builder;
+ }
+
+ DocumentTraversal getDocumentTraversal(Document doc)
+ throws XMLStreamException
+ {
+ DOMImplementation dom = doc.getImplementation();
+ if (!dom.hasFeature("Traversal", "2.0"))
+ throw new XMLStreamException("Traversal not supported");
+ return (DocumentTraversal) doc;
+ }
+
+ XPathEvaluator getXPathEvaluator(Document doc)
+ throws XMLStreamException
+ {
+ DOMImplementation dom = doc.getImplementation();
+ if (!dom.hasFeature("XPath", "3.0"))
+ throw new XMLStreamException("XPath not supported");
+ return (XPathEvaluator) doc;
+ }
+
+ static String getParameter(String contentType, String name)
+ {
+ StringTokenizer st = new StringTokenizer(contentType, " ;");
+ if (st.hasMoreTokens())
+ st.nextToken();
+ while (st.hasMoreTokens())
+ {
+ String token = st.nextToken();
+ int ei = token.indexOf('=');
+ if (ei != -1)
+ {
+ String key = token.substring(0, ei);
+ if (key.equals(name))
+ {
+ String value = token.substring(ei + 1);
+ int len = value.length();
+ if (len > 1 &&
+ value.charAt(0) == '"' &&
+ value.charAt(len - 1) == '"')
+ value = value.substring(1, len - 1);
+ else if (len > 1 &&
+ value.charAt(0) == '\'' &&
+ value.charAt(len - 1) == '\'')
+ value = value.substring(1, len - 1);
+ return value;
+ }
+ }
+ }
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/XMLEventAllocatorImpl.java b/libjava/classpath/gnu/xml/stream/XMLEventAllocatorImpl.java
new file mode 100644
index 000000000..a1824bfc5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/XMLEventAllocatorImpl.java
@@ -0,0 +1,204 @@
+/* XMLEventAllocatorImpl.java --
+ Copyright (C) 2005,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import javax.xml.namespace.QName;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.events.EntityDeclaration;
+import javax.xml.stream.events.XMLEvent;
+import javax.xml.stream.util.XMLEventAllocator;
+import javax.xml.stream.util.XMLEventConsumer;
+
+/**
+ * Allocator for creating XML events based on a reader state.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class XMLEventAllocatorImpl
+ implements XMLEventAllocator
+{
+
+ protected Map entityDeclarations;
+
+ protected XMLEventAllocatorImpl()
+ {
+ entityDeclarations = new HashMap();
+ }
+
+ public XMLEvent allocate(XMLStreamReader reader)
+ throws XMLStreamException
+ {
+ String text;
+ boolean whitespace;
+ boolean ignorableWhitespace;
+ int len;
+ List namespaces;
+ int eventType = reader.getEventType();
+ Location location = reader.getLocation();
+ switch (eventType)
+ {
+ case XMLStreamConstants.CDATA:
+ text = reader.getText();
+ whitespace = isWhitespace(text);
+ // TODO ignorableWhitespace
+ ignorableWhitespace = whitespace && false;
+ return new CharactersImpl(location, text,
+ whitespace, true, ignorableWhitespace);
+ case XMLStreamConstants.CHARACTERS:
+ text = reader.getText();
+ whitespace = false;
+ // TODO ignorableWhitespace
+ ignorableWhitespace = whitespace && false;
+ return new CharactersImpl(location, text,
+ whitespace, false, ignorableWhitespace);
+ case XMLStreamConstants.COMMENT:
+ text = reader.getText();
+ return new CommentImpl(location, text);
+ case XMLStreamConstants.DTD:
+ text = reader.getText();
+ List notations = new LinkedList();
+ List entities = new LinkedList();
+ // TODO readDTDBody(notations, entities);
+ return new DTDImpl(location, text, null, notations, entities);
+ case XMLStreamConstants.END_DOCUMENT:
+ return new EndDocumentImpl(location);
+ case XMLStreamConstants.END_ELEMENT:
+ len = reader.getNamespaceCount();
+ namespaces = new LinkedList();
+ for (int i = 0; i < len; i++)
+ namespaces.add(new NamespaceImpl(location,
+ reader.getNamespacePrefix(i),
+ reader.getNamespaceURI(i),
+ false));
+ return new EndElementImpl(location,
+ reader.getName(),
+ namespaces);
+ case XMLStreamConstants.ENTITY_REFERENCE:
+ String name = reader.getLocalName();
+ EntityDeclaration decl =
+ (EntityDeclaration) entityDeclarations.get(name);
+ return new EntityReferenceImpl(location, decl, name);
+ case XMLStreamConstants.PROCESSING_INSTRUCTION:
+ return new ProcessingInstructionImpl(location,
+ reader.getPITarget(),
+ reader.getPIData());
+ case XMLStreamConstants.SPACE:
+ text = reader.getText();
+ whitespace = true;
+ // TODO ignorableWhitespace
+ ignorableWhitespace = whitespace && false;
+ return new CharactersImpl(location, text,
+ whitespace, false, ignorableWhitespace);
+ case XMLStreamConstants.START_DOCUMENT:
+ String systemId = location.getSystemId();
+ String encoding = reader.getCharacterEncodingScheme();
+ boolean encodingDeclared = encoding != null;
+ if (encoding == null)
+ {
+ encoding = reader.getEncoding();
+ if (encoding == null)
+ encoding = "UTF-8";
+ }
+ String xmlVersion = reader.getVersion();
+ if (xmlVersion == null)
+ xmlVersion = "1.0";
+ boolean xmlStandalone = reader.isStandalone();
+ boolean standaloneDeclared = reader.standaloneSet();
+ return new StartDocumentImpl(location,
+ systemId,
+ encoding,
+ xmlVersion,
+ xmlStandalone,
+ standaloneDeclared,
+ encodingDeclared);
+ case XMLStreamConstants.START_ELEMENT:
+ len = reader.getNamespaceCount();
+ namespaces = new LinkedList();
+ for (int i = 0; i < len; i++)
+ namespaces.add(new NamespaceImpl(location,
+ reader.getNamespacePrefix(i),
+ reader.getNamespaceURI(i),
+ false));
+ len = reader.getAttributeCount();
+ List attributes = new LinkedList();
+ for (int i = 0; i < len; i++)
+ attributes.add(new AttributeImpl(location,
+ reader.getAttributeName(i),
+ reader.getAttributeValue(i),
+ reader.getAttributeType(i),
+ reader.isAttributeSpecified(i)));
+ return new StartElementImpl(location,
+ reader.getName(),
+ attributes, namespaces,
+ reader.getNamespaceContext());
+ default:
+ throw new XMLStreamException("Unknown event type: " + eventType);
+ }
+ }
+
+ public void allocate(XMLStreamReader reader, XMLEventConsumer consumer)
+ throws XMLStreamException
+ {
+ consumer.add(allocate(reader));
+ }
+
+ public XMLEventAllocator newInstance()
+ {
+ return new XMLEventAllocatorImpl();
+ }
+
+ protected boolean isWhitespace(String text)
+ {
+ int len = text.length();
+ for (int i = 0; i < len; i++)
+ {
+ char c = text.charAt(i);
+ if (c != 0x20 && c != 0x09 && c != 0x0a && c != 0x0d)
+ return false;
+ }
+ return true;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/XMLEventFactoryImpl.java b/libjava/classpath/gnu/xml/stream/XMLEventFactoryImpl.java
new file mode 100644
index 000000000..12f7f2c79
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/XMLEventFactoryImpl.java
@@ -0,0 +1,269 @@
+/* XMLEventFactoryImpl.java --
+ Copyright (C) 2005,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLEventFactory;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.Characters;
+import javax.xml.stream.events.Comment;
+import javax.xml.stream.events.DTD;
+import javax.xml.stream.events.EndDocument;
+import javax.xml.stream.events.EndElement;
+import javax.xml.stream.events.EntityDeclaration;
+import javax.xml.stream.events.EntityReference;
+import javax.xml.stream.events.Namespace;
+import javax.xml.stream.events.ProcessingInstruction;
+import javax.xml.stream.events.StartDocument;
+import javax.xml.stream.events.StartElement;
+
+/**
+ * Factory for XML events.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class XMLEventFactoryImpl
+ extends XMLEventFactory
+{
+
+ protected Location location;
+
+ public void setLocation(Location location)
+ {
+ this.location = location;
+ }
+
+ public Attribute createAttribute(String prefix, String namespaceURI,
+ String localName, String value)
+ {
+ return new AttributeImpl(location,
+ new QName(namespaceURI, localName, prefix),
+ value, "CDATA", true);
+ }
+
+ public Attribute createAttribute(String localName, String value)
+ {
+ return new AttributeImpl(location,
+ new QName(localName),
+ value, "CDATA", true);
+ }
+
+ public Attribute createAttribute(QName name, String value)
+ {
+ return new AttributeImpl(location, name, value,
+ "CDATA", true);
+ }
+
+ public Namespace createNamespace(String namespaceURI)
+ {
+ return new NamespaceImpl(location,
+ XMLConstants.DEFAULT_NS_PREFIX,
+ namespaceURI,
+ true);
+ }
+
+ public Namespace createNamespace(String prefix, String namespaceUri)
+ {
+ return new NamespaceImpl(location, prefix, namespaceUri, true);
+ }
+
+ public StartElement createStartElement(QName name,
+ Iterator attributes,
+ Iterator namespaces)
+ {
+ return new StartElementImpl(location, name,
+ createLinkedList(attributes),
+ createLinkedList(namespaces),
+ null);
+ }
+
+ public StartElement createStartElement(String prefix,
+ String namespaceUri,
+ String localName)
+ {
+ return new StartElementImpl(location,
+ new QName(namespaceUri, localName, prefix),
+ Collections.EMPTY_LIST,
+ Collections.EMPTY_LIST,
+ null);
+ }
+
+ public StartElement createStartElement(String prefix,
+ String namespaceUri,
+ String localName,
+ Iterator attributes,
+ Iterator namespaces)
+ {
+ return new StartElementImpl(location,
+ new QName(namespaceUri, localName, prefix),
+ createLinkedList(attributes),
+ createLinkedList(namespaces),
+ null);
+ }
+
+ public StartElement createStartElement(String prefix,
+ String namespaceUri,
+ String localName,
+ Iterator attributes,
+ Iterator namespaces,
+ NamespaceContext context)
+ {
+ return new StartElementImpl(location,
+ new QName(namespaceUri, localName, prefix),
+ createLinkedList(attributes),
+ createLinkedList(namespaces),
+ context);
+ }
+
+ public EndElement createEndElement(QName name,
+ Iterator namespaces)
+ {
+ return new EndElementImpl(location, name,
+ createLinkedList(namespaces));
+ }
+
+ public EndElement createEndElement(String prefix,
+ String namespaceUri,
+ String localName)
+ {
+ return new EndElementImpl(location,
+ new QName(namespaceUri, localName, prefix),
+ Collections.EMPTY_LIST);
+ }
+
+ public EndElement createEndElement(String prefix,
+ String namespaceUri,
+ String localName,
+ Iterator namespaces)
+ {
+ return new EndElementImpl(location,
+ new QName(namespaceUri, localName, prefix),
+ createLinkedList(namespaces));
+ }
+
+ public Characters createCharacters(String content)
+ {
+ return new CharactersImpl(location, content, false, false, false);
+ }
+
+ public Characters createCData(String content)
+ {
+ return new CharactersImpl(location, content, false, true, false);
+ }
+
+ public Characters createSpace(String content)
+ {
+ return new CharactersImpl(location, content, true, false, false);
+ }
+
+ public Characters createIgnorableSpace(String content)
+ {
+ return new CharactersImpl(location, content, true, false, true);
+ }
+
+ public StartDocument createStartDocument()
+ {
+ return new StartDocumentImpl(location, null, "UTF-8", "1.0",
+ false, false, false);
+ }
+
+ public StartDocument createStartDocument(String encoding,
+ String version,
+ boolean standalone)
+ {
+ return new StartDocumentImpl(location, null, encoding, version,
+ standalone, true, true);
+ }
+
+ public StartDocument createStartDocument(String encoding,
+ String version)
+ {
+ return new StartDocumentImpl(location, null, encoding, version,
+ false, false, true);
+ }
+
+ public StartDocument createStartDocument(String encoding)
+ {
+ return new StartDocumentImpl(location, null, encoding, "1.0",
+ false, false, true);
+ }
+
+ public EndDocument createEndDocument()
+ {
+ return new EndDocumentImpl(location);
+ }
+
+ public EntityReference createEntityReference(String name,
+ EntityDeclaration declaration)
+ {
+ return new EntityReferenceImpl(location, declaration, name);
+ }
+
+ public Comment createComment(String text)
+ {
+ return new CommentImpl(location, text);
+ }
+
+ public ProcessingInstruction createProcessingInstruction(String target,
+ String data)
+ {
+ return new ProcessingInstructionImpl(location, target, data);
+ }
+
+ public DTD createDTD(String dtd)
+ {
+ return new DTDImpl(location, dtd, null,
+ Collections.EMPTY_LIST,
+ Collections.EMPTY_LIST);
+ }
+
+ LinkedList createLinkedList(Iterator i)
+ {
+ LinkedList ret = new LinkedList();
+ while (i.hasNext())
+ ret.add(i.next());
+ return ret;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/XMLEventImpl.java b/libjava/classpath/gnu/xml/stream/XMLEventImpl.java
new file mode 100644
index 000000000..2e650bf74
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/XMLEventImpl.java
@@ -0,0 +1,198 @@
+/* XMLEventImpl.java --
+ Copyright (C) 2005,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.io.Writer;
+import javax.xml.namespace.QName;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Characters;
+import javax.xml.stream.events.EndElement;
+import javax.xml.stream.events.StartElement;
+import javax.xml.stream.events.XMLEvent;
+
+/**
+ * An XML stream event.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public abstract class XMLEventImpl
+ implements XMLEvent
+{
+
+ protected final Location location;
+
+ protected XMLEventImpl(Location location)
+ {
+ this.location = location;
+ }
+
+ public abstract int getEventType();
+
+ public Location getLocation()
+ {
+ return location;
+ }
+
+ public boolean isStartElement()
+ {
+ return getEventType() == START_ELEMENT;
+ }
+
+ public boolean isAttribute()
+ {
+ return getEventType() == ATTRIBUTE;
+ }
+
+ public boolean isNamespace()
+ {
+ return getEventType() == NAMESPACE;
+ }
+
+ public boolean isEndElement()
+ {
+ return getEventType() == END_ELEMENT;
+ }
+
+ public boolean isEntityReference()
+ {
+ return getEventType() == ENTITY_REFERENCE;
+ }
+
+ public boolean isProcessingInstruction()
+ {
+ return getEventType() == PROCESSING_INSTRUCTION;
+ }
+
+ public boolean isCharacters()
+ {
+ int et = getEventType();
+ return et == CHARACTERS || et == CDATA;
+ }
+
+ public boolean isStartDocument()
+ {
+ return getEventType() == START_DOCUMENT;
+ }
+
+ public boolean isEndDocument()
+ {
+ return getEventType() == END_DOCUMENT;
+ }
+
+ public StartElement asStartElement()
+ {
+ return (StartElement) this;
+ }
+
+ public EndElement asEndElement()
+ {
+ return (EndElement) this;
+ }
+
+ public Characters asCharacters()
+ {
+ return (Characters) this;
+ }
+
+ public QName getSchemaType()
+ {
+ return null;
+ }
+
+ public abstract void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException;
+
+ protected String encode(String text, boolean inAttr)
+ {
+ int len = text.length();
+ CPStringBuilder buf = null;
+ for (int i = 0; i < len; i++)
+ {
+ char c = text.charAt(i);
+ if (c == '<')
+ {
+ if (buf == null)
+ {
+ buf = new CPStringBuilder(text.substring(0, i));
+ }
+ buf.append("&lt;");
+ }
+ else if (c == '>')
+ {
+ if (buf == null)
+ {
+ buf = new CPStringBuilder(text.substring(0, i));
+ }
+ buf.append("&gt;");
+ }
+ else if (c == '&')
+ {
+ if (buf == null)
+ {
+ buf = new CPStringBuilder(text.substring(0, i));
+ }
+ buf.append("&amp;");
+ }
+ else if (c == '\'' && inAttr)
+ {
+ if (buf == null)
+ {
+ buf = new CPStringBuilder(text.substring(0, i));
+ }
+ buf.append("&apos;");
+ }
+ else if (c == '"' && inAttr)
+ {
+ if (buf == null)
+ {
+ buf = new CPStringBuilder(text.substring(0, i));
+ }
+ buf.append("&quot;");
+ }
+ else if (buf != null)
+ {
+ buf.append(c);
+ }
+ }
+ return (buf == null) ? text : buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/XMLEventReaderImpl.java b/libjava/classpath/gnu/xml/stream/XMLEventReaderImpl.java
new file mode 100644
index 000000000..bab6d9378
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/XMLEventReaderImpl.java
@@ -0,0 +1,157 @@
+/* XMLEventReaderImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.events.XMLEvent;
+import javax.xml.stream.util.XMLEventAllocator;
+
+/**
+ * Parser using XML events.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class XMLEventReaderImpl
+ implements XMLEventReader
+{
+
+ protected final XMLStreamReader reader;
+ protected final XMLEventAllocator allocator;
+ protected final String systemId;
+ protected XMLEvent peekEvent;
+
+ protected XMLEventReaderImpl(XMLStreamReader reader,
+ XMLEventAllocator allocator,
+ String systemId)
+ {
+ this.reader = reader;
+ this.allocator = allocator;
+ this.systemId = systemId;
+ }
+
+ public XMLEvent nextEvent()
+ throws XMLStreamException
+ {
+ XMLEvent ret = peek();
+ peekEvent = null;
+ return ret;
+ }
+
+ public Object next()
+ {
+ try
+ {
+ return nextEvent();
+ }
+ catch (XMLStreamException e)
+ {
+ RuntimeException e2 = new RuntimeException();
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public boolean hasNext()
+ {
+ if (peekEvent != null)
+ return true;
+ try
+ {
+ return reader.hasNext();
+ }
+ catch (XMLStreamException e)
+ {
+ return false;
+ }
+ }
+
+ public XMLEvent peek()
+ throws XMLStreamException
+ {
+ if (peekEvent != null)
+ return peekEvent;
+ if (!reader.hasNext())
+ return null;
+ reader.next();
+ peekEvent = allocator.allocate(reader);
+ return peekEvent;
+ }
+
+ public String getElementText()
+ throws XMLStreamException
+ {
+ return reader.getElementText();
+ }
+
+ public XMLEvent nextTag()
+ throws XMLStreamException
+ {
+ if (peekEvent != null)
+ {
+ int eventType = peekEvent.getEventType();
+ if (eventType == XMLStreamConstants.START_ELEMENT ||
+ eventType == XMLStreamConstants.END_ELEMENT)
+ return peekEvent;
+ else
+ peekEvent = null;
+ }
+ reader.nextTag();
+ return allocator.allocate(reader);
+ }
+
+ public Object getProperty(String name)
+ throws IllegalArgumentException
+ {
+ return reader.getProperty(name);
+ }
+
+ public void close()
+ throws XMLStreamException
+ {
+ reader.close();
+ }
+
+ public void remove()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/XMLEventWriterImpl.java b/libjava/classpath/gnu/xml/stream/XMLEventWriterImpl.java
new file mode 100644
index 000000000..33b1dce31
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/XMLEventWriterImpl.java
@@ -0,0 +1,190 @@
+/* XMLEventWriterImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLEventWriter;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.Characters;
+import javax.xml.stream.events.Comment;
+import javax.xml.stream.events.DTD;
+import javax.xml.stream.events.Namespace;
+import javax.xml.stream.events.ProcessingInstruction;
+import javax.xml.stream.events.StartDocument;
+import javax.xml.stream.events.StartElement;
+import javax.xml.stream.events.XMLEvent;
+
+/**
+ * Writer to write events to an underlying XML stream writer.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class XMLEventWriterImpl
+ implements XMLEventWriter
+{
+
+ protected final XMLStreamWriter writer;
+
+ protected XMLEventWriterImpl(XMLStreamWriter writer)
+ {
+ this.writer = writer;
+ }
+
+ public void flush()
+ throws XMLStreamException
+ {
+ writer.flush();
+ }
+
+ public void close()
+ throws XMLStreamException
+ {
+ writer.close();
+ }
+
+ public void add(XMLEvent event)
+ throws XMLStreamException
+ {
+ QName name;
+ String uri;
+ switch (event.getEventType())
+ {
+ case XMLStreamConstants.START_ELEMENT:
+ StartElement startElement = event.asStartElement();
+ name = startElement.getName();
+ uri = name.getNamespaceURI();
+ if (uri != null && !"".equals(uri))
+ writer.writeStartElement(name.getPrefix(), name.getLocalPart(), uri);
+ else
+ writer.writeStartElement(name.getLocalPart());
+ break;
+ case XMLStreamConstants.END_ELEMENT:
+ writer.writeEndElement();
+ break;
+ case XMLStreamConstants.ATTRIBUTE:
+ Attribute attribute = (Attribute) event;
+ name = attribute.getName();
+ uri = name.getNamespaceURI();
+ if (uri != null && !"".equals(uri))
+ writer.writeAttribute(name.getPrefix(), uri, name.getLocalPart(),
+ attribute.getValue());
+ else
+ writer.writeAttribute(name.getLocalPart(), attribute.getValue());
+ break;
+ case XMLStreamConstants.NAMESPACE:
+ Namespace namespace = (Namespace) event;
+ uri = namespace.getNamespaceURI();
+ writer.writeNamespace(namespace.getPrefix(), uri);
+ break;
+ case XMLStreamConstants.PROCESSING_INSTRUCTION:
+ ProcessingInstruction pi = (ProcessingInstruction) event;
+ String data = pi.getData();
+ if (data == null)
+ writer.writeProcessingInstruction(pi.getTarget());
+ else
+ writer.writeProcessingInstruction(pi.getTarget(), data);
+ break;
+ case XMLStreamConstants.COMMENT:
+ Comment comment = (Comment) event;
+ writer.writeComment(comment.getText());
+ break;
+ case XMLStreamConstants.START_DOCUMENT:
+ StartDocument startDocument = (StartDocument) event;
+ writer.writeStartDocument(startDocument.getVersion());
+ break;
+ case XMLStreamConstants.END_DOCUMENT:
+ writer.writeEndDocument();
+ break;
+ case XMLStreamConstants.DTD:
+ DTD dtd = (DTD) event;
+ writer.writeDTD(dtd.getDocumentTypeDeclaration());
+ break;
+ case XMLStreamConstants.CHARACTERS:
+ case XMLStreamConstants.SPACE:
+ Characters characters = event.asCharacters();
+ writer.writeCharacters(characters.getData());
+ break;
+ case XMLStreamConstants.CDATA:
+ Characters cdata = event.asCharacters();
+ writer.writeCData(cdata.getData());
+ break;
+ }
+ }
+
+ public void add(XMLEventReader reader)
+ throws XMLStreamException
+ {
+ while (reader.hasNext())
+ add(reader.nextEvent());
+ }
+
+ public String getPrefix(String uri)
+ throws XMLStreamException
+ {
+ return writer.getPrefix(uri);
+ }
+
+ public void setPrefix(String prefix, String uri)
+ throws XMLStreamException
+ {
+ writer.setPrefix(prefix, uri);
+ }
+
+ public void setDefaultNamespace(String uri)
+ throws XMLStreamException
+ {
+ writer.setDefaultNamespace(uri);
+ }
+
+ public void setNamespaceContext(NamespaceContext context)
+ throws XMLStreamException
+ {
+ writer.setNamespaceContext(context);
+ }
+
+ public NamespaceContext getNamespaceContext()
+ {
+ return writer.getNamespaceContext();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/XMLInputFactoryImpl.java b/libjava/classpath/gnu/xml/stream/XMLInputFactoryImpl.java
new file mode 100644
index 000000000..87aa32107
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/XMLInputFactoryImpl.java
@@ -0,0 +1,397 @@
+/* XMLInputFactoryImpl.java --
+ Copyright (C) 2005,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.Reader;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.stream.EventFilter;
+import javax.xml.stream.StreamFilter;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLReporter;
+import javax.xml.stream.XMLResolver;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.util.XMLEventAllocator;
+
+/**
+ * Factory for creating parsers from various kinds of XML source.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class XMLInputFactoryImpl
+ extends XMLInputFactory
+{
+
+ protected XMLResolver resolver;
+ protected XMLReporter reporter;
+ protected XMLEventAllocator allocator;
+
+ protected boolean validating;
+ protected boolean namespaceAware = true;
+ protected boolean coalescing;
+ protected boolean replacingEntityReferences = true;
+ protected boolean externalEntities = true;
+ protected boolean supportDTD = true;
+ protected boolean xIncludeAware = false;
+ protected boolean baseAware = true;
+ protected boolean stringInterning = true;
+
+ public XMLInputFactoryImpl()
+ {
+ allocator = new XMLEventAllocatorImpl();
+ }
+
+ public XMLStreamReader createXMLStreamReader(Reader reader)
+ throws XMLStreamException
+ {
+ return createXMLStreamReader(null, reader);
+ }
+
+ public XMLStreamReader createXMLStreamReader(Source source)
+ throws XMLStreamException
+ {
+ String systemId = source.getSystemId();
+ InputStream in = getInputStream(source);
+ XMLParser ret = new XMLParser(in, systemId,
+ validating,
+ namespaceAware,
+ coalescing,
+ replacingEntityReferences,
+ externalEntities,
+ supportDTD,
+ baseAware,
+ stringInterning,
+ false,
+ reporter,
+ resolver);
+ if (xIncludeAware)
+ return new XIncludeFilter(ret, systemId, namespaceAware, validating,
+ replacingEntityReferences);
+ return ret;
+ }
+
+ public XMLStreamReader createXMLStreamReader(InputStream in)
+ throws XMLStreamException
+ {
+ return createXMLStreamReader(null, in);
+ }
+
+ public XMLStreamReader createXMLStreamReader(InputStream in, String encoding)
+ throws XMLStreamException
+ {
+ return createXMLStreamReader(in);
+ }
+
+ public XMLStreamReader createXMLStreamReader(String systemId, InputStream in)
+ throws XMLStreamException
+ {
+ XMLParser ret = new XMLParser(in, systemId,
+ validating,
+ namespaceAware,
+ coalescing,
+ replacingEntityReferences,
+ externalEntities,
+ supportDTD,
+ baseAware,
+ stringInterning,
+ false,
+ reporter,
+ resolver);
+ if (xIncludeAware)
+ return new XIncludeFilter(ret, null, namespaceAware, validating,
+ replacingEntityReferences);
+ return ret;
+ }
+
+ public XMLStreamReader createXMLStreamReader(String systemId, Reader reader)
+ throws XMLStreamException
+ {
+ XMLParser ret = new XMLParser(reader, systemId,
+ validating,
+ namespaceAware,
+ coalescing,
+ replacingEntityReferences,
+ externalEntities,
+ supportDTD,
+ baseAware,
+ stringInterning,
+ false,
+ reporter,
+ resolver);
+ if (xIncludeAware)
+ return new XIncludeFilter(ret, null, namespaceAware, validating,
+ replacingEntityReferences);
+ return ret;
+ }
+
+ public XMLEventReader createXMLEventReader(Reader reader)
+ throws XMLStreamException
+ {
+ XMLStreamReader sr = createXMLStreamReader(reader);
+ return new XMLEventReaderImpl(sr, allocator, null);
+ }
+
+ public XMLEventReader createXMLEventReader(String systemId, Reader reader)
+ throws XMLStreamException
+ {
+ XMLStreamReader sr = createXMLStreamReader(systemId, reader);
+ return new XMLEventReaderImpl(sr, allocator, null);
+ }
+
+ public XMLEventReader createXMLEventReader(XMLStreamReader reader)
+ throws XMLStreamException
+ {
+ return new XMLEventReaderImpl(reader, allocator, null);
+ }
+
+ public XMLEventReader createXMLEventReader(Source source)
+ throws XMLStreamException
+ {
+ XMLStreamReader sr = createXMLStreamReader(source);
+ return new XMLEventReaderImpl(sr, allocator, null);
+ }
+
+ public XMLEventReader createXMLEventReader(InputStream in)
+ throws XMLStreamException
+ {
+ XMLStreamReader sr = createXMLStreamReader(in);
+ return new XMLEventReaderImpl(sr, allocator, null);
+ }
+
+ public XMLEventReader createXMLEventReader(InputStream in, String encoding)
+ throws XMLStreamException
+ {
+ XMLStreamReader sr = createXMLStreamReader(in, encoding);
+ return new XMLEventReaderImpl(sr, allocator, null);
+ }
+
+ public XMLEventReader createXMLEventReader(String systemId, InputStream in)
+ throws XMLStreamException
+ {
+ XMLStreamReader sr = createXMLStreamReader(systemId, in);
+ return new XMLEventReaderImpl(sr, allocator, null);
+ }
+
+ public XMLStreamReader createFilteredReader(XMLStreamReader reader,
+ StreamFilter filter)
+ throws XMLStreamException
+ {
+ return new FilteredStreamReader(reader, filter);
+ }
+
+ public XMLEventReader createFilteredReader(XMLEventReader reader,
+ EventFilter filter)
+ throws XMLStreamException
+ {
+ return new FilteredEventReader(reader, filter);
+ }
+
+ public XMLResolver getXMLResolver()
+ {
+ return resolver;
+ }
+
+ public void setXMLResolver(XMLResolver resolver)
+ {
+ this.resolver = resolver;
+ }
+
+ public XMLReporter getXMLReporter()
+ {
+ return reporter;
+ }
+
+ public void setXMLReporter(XMLReporter reporter)
+ {
+ this.reporter = reporter;
+ }
+
+ public void setProperty(String name, Object value)
+ throws IllegalArgumentException
+ {
+ if (name.equals(IS_NAMESPACE_AWARE))
+ namespaceAware = ((Boolean) value).booleanValue();
+ else if (name.equals(IS_VALIDATING))
+ validating = ((Boolean) value).booleanValue();
+ else if (name.equals(IS_COALESCING))
+ coalescing = ((Boolean) value).booleanValue();
+ else if (name.equals(IS_REPLACING_ENTITY_REFERENCES))
+ replacingEntityReferences = ((Boolean) value).booleanValue();
+ else if (name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES))
+ externalEntities = ((Boolean) value).booleanValue();
+ else if (name.equals(SUPPORT_DTD))
+ supportDTD = ((Boolean) value).booleanValue();
+ else if (name.equals(REPORTER))
+ reporter = (XMLReporter) value;
+ else if (name.equals(RESOLVER))
+ resolver = (XMLResolver) value;
+ else if (name.equals(ALLOCATOR))
+ allocator = (XMLEventAllocator) value;
+ else if (name.equals("gnu.xml.stream.stringInterning"))
+ stringInterning = ((Boolean) value).booleanValue();
+ else if (name.equals("gnu.xml.stream.baseAware"))
+ baseAware = ((Boolean) value).booleanValue();
+ else if (name.equals("gnu.xml.stream.xIncludeAware"))
+ xIncludeAware = ((Boolean) value).booleanValue();
+ else
+ throw new IllegalArgumentException(name);
+ }
+
+ public Object getProperty(String name)
+ throws IllegalArgumentException
+ {
+ if (name.equals(IS_NAMESPACE_AWARE))
+ return namespaceAware ? Boolean.TRUE : Boolean.FALSE;
+ if (name.equals(IS_VALIDATING))
+ return validating ? Boolean.TRUE : Boolean.FALSE;
+ if (name.equals(IS_COALESCING))
+ return coalescing ? Boolean.TRUE : Boolean.FALSE;
+ if (name.equals(IS_REPLACING_ENTITY_REFERENCES))
+ return replacingEntityReferences ? Boolean.TRUE : Boolean.FALSE;
+ if (name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES))
+ return externalEntities ? Boolean.TRUE : Boolean.FALSE;
+ if (name.equals(SUPPORT_DTD))
+ return supportDTD ? Boolean.TRUE : Boolean.FALSE;
+ if (name.equals(REPORTER))
+ return reporter;
+ if (name.equals(RESOLVER))
+ return resolver;
+ if (name.equals(ALLOCATOR))
+ return allocator;
+ if (name.equals("gnu.xml.stream.stringInterning"))
+ return stringInterning ? Boolean.TRUE : Boolean.FALSE;
+ if (name.equals("gnu.xml.stream.baseAware"))
+ return baseAware ? Boolean.TRUE : Boolean.FALSE;
+ if (name.equals("gnu.xml.stream.xIncludeAware"))
+ return xIncludeAware ? Boolean.TRUE : Boolean.FALSE;
+ throw new IllegalArgumentException(name);
+ }
+
+ public boolean isPropertySupported(String name)
+ {
+ return name.equals(IS_NAMESPACE_AWARE) ||
+ name.equals(IS_VALIDATING) ||
+ name.equals(IS_COALESCING) ||
+ name.equals(IS_REPLACING_ENTITY_REFERENCES) ||
+ name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES) ||
+ name.equals(SUPPORT_DTD) ||
+ name.equals(REPORTER) ||
+ name.equals(RESOLVER) ||
+ name.equals(ALLOCATOR) ||
+ name.equals("gnu.xml.stream.stringInterning") ||
+ name.equals("gnu.xml.stream.baseAware") ||
+ name.equals("gnu.xml.stream.xIncludeAware");
+ }
+
+ public void setEventAllocator(XMLEventAllocator allocator)
+ {
+ this.allocator = allocator;
+ }
+
+ public XMLEventAllocator getEventAllocator()
+ {
+ return allocator;
+ }
+
+ public void setCoalescing(boolean coalescing)
+ {
+ this.coalescing = coalescing;
+ }
+
+ public boolean isCoalescing()
+ {
+ return coalescing;
+ }
+
+ protected InputStream getInputStream(Source source)
+ throws XMLStreamException
+ {
+ InputStream in = null;
+ if (source instanceof StreamSource)
+ {
+ StreamSource streamSource = (StreamSource) source;
+ in = streamSource.getInputStream();
+ }
+ if (in == null)
+ {
+ String systemId = source.getSystemId();
+ try
+ {
+ URL url = new URL(systemId);
+ try
+ {
+ in = url.openStream();
+ }
+ catch (IOException e2)
+ {
+ XMLStreamException e3 = new XMLStreamException(e2);
+ e3.initCause(e2);
+ throw e3;
+ }
+ }
+ catch (MalformedURLException e)
+ {
+ // Fall back to relative file
+ if (File.separatorChar != '/')
+ systemId = systemId.replace('/', File.separatorChar);
+ try
+ {
+ in = new FileInputStream(systemId);
+ }
+ catch (FileNotFoundException e2)
+ {
+ XMLStreamException e3 = new XMLStreamException(e2);
+ e3.initCause(e2);
+ throw e3;
+ }
+ }
+ }
+ return in;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/XMLOutputFactoryImpl.java b/libjava/classpath/gnu/xml/stream/XMLOutputFactoryImpl.java
new file mode 100644
index 000000000..d849e8ba5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/XMLOutputFactoryImpl.java
@@ -0,0 +1,187 @@
+/* XMLOutputFactoryImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.io.UnsupportedEncodingException;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.stream.XMLEventWriter;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+/**
+ * Standard output factory.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class XMLOutputFactoryImpl
+ extends XMLOutputFactory
+{
+
+ protected boolean prefixDefaulting = false;
+
+ public XMLOutputFactoryImpl()
+ {
+ }
+
+ public XMLStreamWriter createXMLStreamWriter(Writer stream)
+ throws XMLStreamException
+ {
+ // XXX try to determine character encoding of writer?
+ return new XMLStreamWriterImpl(stream, null, prefixDefaulting);
+ }
+
+ public XMLStreamWriter createXMLStreamWriter(OutputStream stream)
+ throws XMLStreamException
+ {
+ return createXMLStreamWriter(stream, "UTF-8");
+ }
+
+ public XMLStreamWriter createXMLStreamWriter(OutputStream stream,
+ String encoding)
+ throws XMLStreamException
+ {
+ if (encoding == null)
+ encoding = "UTF-8";
+ try
+ {
+ Writer writer = new OutputStreamWriter(stream, encoding);
+ return new XMLStreamWriterImpl(writer, encoding, prefixDefaulting);
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public XMLStreamWriter createXMLStreamWriter(Result result)
+ throws XMLStreamException
+ {
+ if (result instanceof StreamResult)
+ {
+ StreamResult sr = (StreamResult) result;
+ OutputStream out = sr.getOutputStream();
+ if (out != null)
+ return createXMLStreamWriter(out);
+ Writer writer = sr.getWriter();
+ if (writer != null)
+ return createXMLStreamWriter(writer);
+ }
+ throw new UnsupportedOperationException();
+ }
+
+ public XMLEventWriter createXMLEventWriter(OutputStream stream)
+ throws XMLStreamException
+ {
+ XMLStreamWriter writer = createXMLStreamWriter(stream);
+ return new XMLEventWriterImpl(writer);
+ }
+
+ public XMLEventWriter createXMLEventWriter(OutputStream stream,
+ String encoding)
+ throws XMLStreamException
+ {
+ XMLStreamWriter writer = createXMLStreamWriter(stream, encoding);
+ return new XMLEventWriterImpl(writer);
+ }
+
+ public XMLEventWriter createXMLEventWriter(Writer stream)
+ throws XMLStreamException
+ {
+ XMLStreamWriter writer = createXMLStreamWriter(stream);
+ return new XMLEventWriterImpl(writer);
+ }
+
+ public XMLEventWriter createXMLEventWriter(Result result)
+ throws XMLStreamException
+ {
+ if (result instanceof StreamResult)
+ {
+ StreamResult sr = (StreamResult) result;
+ OutputStream out = sr.getOutputStream();
+ if (out != null)
+ return createXMLEventWriter(out);
+ Writer writer = sr.getWriter();
+ if (writer != null)
+ return createXMLEventWriter(writer);
+ }
+ throw new UnsupportedOperationException();
+ }
+
+ public void setProperty(String name, Object value)
+ throws IllegalArgumentException
+ {
+ if (IS_REPAIRING_NAMESPACES.equals(name))
+ prefixDefaulting = ((Boolean) value).booleanValue();
+ else
+ throw new IllegalArgumentException(name);
+ }
+
+ public Object getProperty(String name)
+ throws IllegalArgumentException
+ {
+ if (IS_REPAIRING_NAMESPACES.equals(name))
+ return new Boolean(prefixDefaulting);
+ throw new IllegalArgumentException(name);
+ }
+
+ public boolean isPropertySupported(String name)
+ {
+ if (IS_REPAIRING_NAMESPACES.equals(name))
+ return true;
+ return false;
+ }
+
+ public boolean isPrefixDefaulting()
+ {
+ return prefixDefaulting;
+ }
+
+ public void setPrefixDefaulting(boolean value)
+ {
+ prefixDefaulting = value;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/XMLParser.java b/libjava/classpath/gnu/xml/stream/XMLParser.java
new file mode 100644
index 000000000..71e876569
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/XMLParser.java
@@ -0,0 +1,5434 @@
+/* XMLParser.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version.
+
+Partly derived from code which carried the following notice:
+
+ Copyright (c) 1997, 1998 by Microstar Software Ltd.
+
+ AElfred is free for both commercial and non-commercial use and
+ redistribution, provided that Microstar's copyright and disclaimer are
+ retained intact. You are free to modify AElfred for your own use and
+ to redistribute AElfred with your modifications, provided that the
+ modifications are clearly documented.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ merchantability or fitness for a particular purpose. Please use it AT
+ YOUR OWN RISK.
+*/
+
+package gnu.xml.stream;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.io.BufferedInputStream;
+import java.io.EOFException;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.StringTokenizer;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLReporter;
+import javax.xml.stream.XMLResolver;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import gnu.java.net.CRLFInputStream;
+import gnu.classpath.debug.TeeInputStream;
+import gnu.classpath.debug.TeeReader;
+
+/**
+ * An XML parser.
+ * This parser supports the following additional StAX properties:
+ * <table>
+ * <tr><td>gnu.xml.stream.stringInterning</td>
+ * <td>Boolean</td>
+ * <td>Indicates whether markup strings will be interned</td></tr>
+ * <tr><td>gnu.xml.stream.xmlBase</td>
+ * <td>Boolean</td>
+ * <td>Indicates whether XML Base processing will be performed</td></tr>
+ * <tr><td>gnu.xml.stream.baseURI</td>
+ * <td>String</td>
+ * <td>Returns the base URI of the current event</td></tr>
+ * </table>
+ *
+ * @see http://www.w3.org/TR/REC-xml/
+ * @see http://www.w3.org/TR/xml11/
+ * @see http://www.w3.org/TR/REC-xml-names
+ * @see http://www.w3.org/TR/xml-names11
+ * @see http://www.w3.org/TR/xmlbase/
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class XMLParser
+ implements XMLStreamReader, NamespaceContext
+{
+
+ // -- parser state machine states --
+ private static final int INIT = 0; // start state
+ private static final int PROLOG = 1; // in prolog
+ private static final int CONTENT = 2; // in content
+ private static final int EMPTY_ELEMENT = 3; // empty element state
+ private static final int MISC = 4; // in Misc (after root element)
+
+ // -- parameters for parsing literals --
+ private final static int LIT_ENTITY_REF = 2;
+ private final static int LIT_NORMALIZE = 4;
+ private final static int LIT_ATTRIBUTE = 8;
+ private final static int LIT_DISABLE_PE = 16;
+ private final static int LIT_DISABLE_CREF = 32;
+ private final static int LIT_DISABLE_EREF = 64;
+ private final static int LIT_PUBID = 256;
+
+ // -- types of attribute values --
+ final static int ATTRIBUTE_DEFAULT_UNDECLARED = 30;
+ final static int ATTRIBUTE_DEFAULT_SPECIFIED = 31;
+ final static int ATTRIBUTE_DEFAULT_IMPLIED = 32;
+ final static int ATTRIBUTE_DEFAULT_REQUIRED = 33;
+ final static int ATTRIBUTE_DEFAULT_FIXED = 34;
+
+ // -- additional event types --
+ final static int START_ENTITY = 50;
+ final static int END_ENTITY = 51;
+
+ /**
+ * The current input.
+ */
+ private Input input;
+
+ /**
+ * Stack of inputs representing XML general entities.
+ * The input representing the XML input stream or reader is always the
+ * first element in this stack.
+ */
+ private LinkedList inputStack = new LinkedList();
+
+ /**
+ * Stack of start-entity events to be reported.
+ */
+ private LinkedList startEntityStack = new LinkedList();
+
+ /**
+ * Stack of end-entity events to be reported.
+ */
+ private LinkedList endEntityStack = new LinkedList();
+
+ /**
+ * Current parser state within the main state machine.
+ */
+ private int state = INIT;
+
+ /**
+ * The (type of the) current event.
+ */
+ private int event;
+
+ /**
+ * The element name stack. The first element in this stack will be the
+ * root element.
+ */
+ private LinkedList stack = new LinkedList();
+
+ /**
+ * Stack of namespace contexts. These are maps specifying prefix-to-URI
+ * mappings. The first element in this stack is the most recent namespace
+ * context (i.e. the other way around from the element name stack).
+ */
+ private LinkedList namespaces = new LinkedList();
+
+ /**
+ * The base-URI stack. This holds the base URI context for each element.
+ * The first element in this stack is the most recent context (i.e. the
+ * other way around from the element name stack).
+ */
+ private LinkedList bases = new LinkedList();
+
+ /**
+ * The list of attributes for the current element, in the order defined in
+ * the XML stream.
+ */
+ private ArrayList attrs = new ArrayList();
+
+ /**
+ * Buffer for text and character data.
+ */
+ private StringBuffer buf = new StringBuffer();
+
+ /**
+ * Buffer for NMTOKEN strings (markup).
+ */
+ private StringBuffer nmtokenBuf = new StringBuffer();
+
+ /**
+ * Buffer for string literals. (e.g. attribute values)
+ */
+ private StringBuffer literalBuf = new StringBuffer();
+
+ /**
+ * Temporary Unicode character buffer used during character data reads.
+ */
+ private int[] tmpBuf = new int[1024];
+
+ /**
+ * The element content model for the current element.
+ */
+ private ContentModel currentContentModel;
+
+ /**
+ * The validation stack. This holds lists of the elements seen for each
+ * element, in order to determine whether the names and order of these
+ * elements match the content model for the element. The last entry in
+ * this stack represents the current element.
+ */
+ private LinkedList validationStack;
+
+ /**
+ * These sets contain the IDs and the IDREFs seen in the document, to
+ * ensure that IDs are unique and that each IDREF refers to an ID in the
+ * document.
+ */
+ private HashSet ids, idrefs;
+
+ /**
+ * The target and data associated with the current processing instruction
+ * event.
+ */
+ private String piTarget, piData;
+
+ /**
+ * The XML version declared in the XML declaration.
+ */
+ private String xmlVersion;
+
+ /**
+ * The encoding declared in the XML declaration.
+ */
+ private String xmlEncoding;
+
+ /**
+ * The standalone value declared in the XML declaration.
+ */
+ private Boolean xmlStandalone;
+
+ /**
+ * The document type definition.
+ */
+ Doctype doctype;
+
+ /**
+ * State variables for determining parameter-entity expansion.
+ */
+ private boolean expandPE, peIsError;
+
+ /**
+ * Whether this is a validating parser.
+ */
+ private final boolean validating;
+
+ /**
+ * Whether strings representing markup will be interned.
+ */
+ private final boolean stringInterning;
+
+ /**
+ * If true, CDATA sections will be merged with adjacent text nodes into a
+ * single event.
+ */
+ private final boolean coalescing;
+
+ /**
+ * Whether to replace general entity references with their replacement
+ * text automatically during parsing.
+ * Otherwise entity-reference events will be issued.
+ */
+ private final boolean replaceERefs;
+
+ /**
+ * Whether to support external entities.
+ */
+ private final boolean externalEntities;
+
+ /**
+ * Whether to support DTDs.
+ */
+ private final boolean supportDTD;
+
+ /**
+ * Whether to support XML namespaces. If true, namespace information will
+ * be available. Otherwise namespaces will simply be reported as ordinary
+ * attributes.
+ */
+ private final boolean namespaceAware;
+
+ /**
+ * Whether to support XML Base. If true, URIs specified in xml:base
+ * attributes will be honoured when resolving external entities.
+ */
+ private final boolean baseAware;
+
+ /**
+ * Whether to report extended event types (START_ENTITY and END_ENTITY)
+ * in addition to the standard event types. Used by the SAX parser.
+ */
+ private final boolean extendedEventTypes;
+
+ /**
+ * The reporter to receive parsing warnings.
+ */
+ final XMLReporter reporter;
+
+ /**
+ * Callback interface for resolving external entities.
+ */
+ final XMLResolver resolver;
+
+ // -- Constants for testing the next kind of markup event --
+ private static final String TEST_START_ELEMENT = "<";
+ private static final String TEST_END_ELEMENT = "</";
+ private static final String TEST_COMMENT = "<!--";
+ private static final String TEST_PI = "<?";
+ private static final String TEST_CDATA = "<![CDATA[";
+ private static final String TEST_XML_DECL = "<?xml";
+ private static final String TEST_DOCTYPE_DECL = "<!DOCTYPE";
+ private static final String TEST_ELEMENT_DECL = "<!ELEMENT";
+ private static final String TEST_ATTLIST_DECL = "<!ATTLIST";
+ private static final String TEST_ENTITY_DECL = "<!ENTITY";
+ private static final String TEST_NOTATION_DECL = "<!NOTATION";
+ private static final String TEST_KET = ">";
+ private static final String TEST_END_COMMENT = "--";
+ private static final String TEST_END_PI = "?>";
+ private static final String TEST_END_CDATA = "]]>";
+
+ /**
+ * The general entities predefined by the XML specification.
+ */
+ private static final LinkedHashMap PREDEFINED_ENTITIES = new LinkedHashMap();
+ static
+ {
+ PREDEFINED_ENTITIES.put("amp", "&");
+ PREDEFINED_ENTITIES.put("lt", "<");
+ PREDEFINED_ENTITIES.put("gt", ">");
+ PREDEFINED_ENTITIES.put("apos", "'");
+ PREDEFINED_ENTITIES.put("quot", "\"");
+ }
+
+ /**
+ * Creates a new XML parser for the given input stream.
+ * This constructor should be used where possible, as it allows the
+ * encoding of the XML data to be correctly determined from the stream.
+ * @param in the input stream
+ * @param systemId the URL from which the input stream was retrieved
+ * (necessary if there are external entities to be resolved)
+ * @param validating if the parser is to be a validating parser
+ * @param namespaceAware if the parser should support XML Namespaces
+ * @param coalescing if CDATA sections should be merged into adjacent text
+ * nodes
+ * @param replaceERefs if entity references should be automatically
+ * replaced by their replacement text (otherwise they will be reported as
+ * entity-reference events)
+ * @param externalEntities if external entities should be loaded
+ * @param supportDTD if support for the XML DTD should be enabled
+ * @param baseAware if the parser should support XML Base to resolve
+ * external entities
+ * @param stringInterning whether strings will be interned during parsing
+ * @param reporter the reporter to receive warnings during processing
+ * @param resolver the callback interface used to resolve external
+ * entities
+ */
+ public XMLParser(InputStream in, String systemId,
+ boolean validating,
+ boolean namespaceAware,
+ boolean coalescing,
+ boolean replaceERefs,
+ boolean externalEntities,
+ boolean supportDTD,
+ boolean baseAware,
+ boolean stringInterning,
+ boolean extendedEventTypes,
+ XMLReporter reporter,
+ XMLResolver resolver)
+ {
+ this.validating = validating;
+ this.namespaceAware = namespaceAware;
+ this.coalescing = coalescing;
+ this.replaceERefs = replaceERefs;
+ this.externalEntities = externalEntities;
+ this.supportDTD = supportDTD;
+ this.baseAware = baseAware;
+ this.stringInterning = stringInterning;
+ this.extendedEventTypes = extendedEventTypes;
+ this.reporter = reporter;
+ this.resolver = resolver;
+ if (validating)
+ {
+ validationStack = new LinkedList();
+ ids = new HashSet();
+ idrefs = new HashSet();
+ }
+ String debug = System.getProperty("gnu.xml.debug.input");
+ if (debug != null)
+ {
+ try
+ {
+ File file = File.createTempFile(debug, ".xml");
+ in = new TeeInputStream(in, new FileOutputStream(file));
+ }
+ catch (IOException e)
+ {
+ RuntimeException e2 = new RuntimeException();
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+ systemId = canonicalize(systemId);
+ pushInput(new Input(in, null, null, systemId, null, null, false, true));
+ }
+
+ /**
+ * Creates a new XML parser for the given character stream.
+ * This constructor is only available for compatibility with the JAXP
+ * APIs, which permit XML to be parsed from a character stream. Because
+ * the encoding specified by the character stream may conflict with that
+ * specified in the XML declaration, this method should be avoided where
+ * possible.
+ * @param in the input stream
+ * @param systemId the URL from which the input stream was retrieved
+ * (necessary if there are external entities to be resolved)
+ * @param validating if the parser is to be a validating parser
+ * @param namespaceAware if the parser should support XML Namespaces
+ * @param coalescing if CDATA sections should be merged into adjacent text
+ * nodes
+ * @param replaceERefs if entity references should be automatically
+ * replaced by their replacement text (otherwise they will be reported as
+ * entity-reference events)
+ * @param externalEntities if external entities should be loaded
+ * @param supportDTD if support for the XML DTD should be enabled
+ * @param baseAware if the parser should support XML Base to resolve
+ * external entities
+ * @param stringInterning whether strings will be interned during parsing
+ * @param reporter the reporter to receive warnings during processing
+ * @param resolver the callback interface used to resolve external
+ * entities
+ */
+ public XMLParser(Reader reader, String systemId,
+ boolean validating,
+ boolean namespaceAware,
+ boolean coalescing,
+ boolean replaceERefs,
+ boolean externalEntities,
+ boolean supportDTD,
+ boolean baseAware,
+ boolean stringInterning,
+ boolean extendedEventTypes,
+ XMLReporter reporter,
+ XMLResolver resolver)
+ {
+ this.validating = validating;
+ this.namespaceAware = namespaceAware;
+ this.coalescing = coalescing;
+ this.replaceERefs = replaceERefs;
+ this.externalEntities = externalEntities;
+ this.supportDTD = supportDTD;
+ this.baseAware = baseAware;
+ this.stringInterning = stringInterning;
+ this.extendedEventTypes = extendedEventTypes;
+ this.reporter = reporter;
+ this.resolver = resolver;
+ if (validating)
+ {
+ validationStack = new LinkedList();
+ ids = new HashSet();
+ idrefs = new HashSet();
+ }
+ String debug = System.getProperty("gnu.xml.debug.input");
+ if (debug != null)
+ {
+ try
+ {
+ File file = File.createTempFile(debug, ".xml");
+ reader = new TeeReader(reader, new FileWriter(file));
+ }
+ catch (IOException e)
+ {
+ RuntimeException e2 = new RuntimeException();
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+ systemId = canonicalize(systemId);
+ pushInput(new Input(null, reader, null, systemId, null, null, false, true));
+ }
+
+ // -- NamespaceContext --
+
+ public String getNamespaceURI(String prefix)
+ {
+ if (XMLConstants.XML_NS_PREFIX.equals(prefix))
+ return XMLConstants.XML_NS_URI;
+ if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
+ return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
+ for (Iterator i = namespaces.iterator(); i.hasNext(); )
+ {
+ LinkedHashMap ctx = (LinkedHashMap) i.next();
+ String namespaceURI = (String) ctx.get(prefix);
+ if (namespaceURI != null)
+ return namespaceURI;
+ }
+ return null;
+ }
+
+ public String getPrefix(String namespaceURI)
+ {
+ if (XMLConstants.XML_NS_URI.equals(namespaceURI))
+ return XMLConstants.XML_NS_PREFIX;
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI))
+ return XMLConstants.XMLNS_ATTRIBUTE;
+ for (Iterator i = namespaces.iterator(); i.hasNext(); )
+ {
+ LinkedHashMap ctx = (LinkedHashMap) i.next();
+ if (ctx.containsValue(namespaceURI))
+ {
+ for (Iterator j = ctx.entrySet().iterator(); j.hasNext(); )
+ {
+ Map.Entry entry = (Map.Entry) i.next();
+ String uri = (String) entry.getValue();
+ if (uri.equals(namespaceURI))
+ return (String) entry.getKey();
+ }
+ }
+ }
+ return null;
+ }
+
+ public Iterator getPrefixes(String namespaceURI)
+ {
+ if (XMLConstants.XML_NS_URI.equals(namespaceURI))
+ return Collections.singleton(XMLConstants.XML_NS_PREFIX).iterator();
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI))
+ return Collections.singleton(XMLConstants.XMLNS_ATTRIBUTE).iterator();
+ LinkedList acc = new LinkedList();
+ for (Iterator i = namespaces.iterator(); i.hasNext(); )
+ {
+ LinkedHashMap ctx = (LinkedHashMap) i.next();
+ if (ctx.containsValue(namespaceURI))
+ {
+ for (Iterator j = ctx.entrySet().iterator(); j.hasNext(); )
+ {
+ Map.Entry entry = (Map.Entry) i.next();
+ String uri = (String) entry.getValue();
+ if (uri.equals(namespaceURI))
+ acc.add(entry.getKey());
+ }
+ }
+ }
+ return acc.iterator();
+ }
+
+ // -- XMLStreamReader --
+
+ public void close()
+ throws XMLStreamException
+ {
+ stack = null;
+ namespaces = null;
+ bases = null;
+ buf = null;
+ attrs = null;
+ doctype = null;
+
+ inputStack = null;
+ validationStack = null;
+ ids = null;
+ idrefs = null;
+ }
+
+ public NamespaceContext getNamespaceContext()
+ {
+ return this;
+ }
+
+ public int getAttributeCount()
+ {
+ return attrs.size();
+ }
+
+ public String getAttributeLocalName(int index)
+ {
+ Attribute a = (Attribute) attrs.get(index);
+ return a.localName;
+ }
+
+ public String getAttributeNamespace(int index)
+ {
+ String prefix = getAttributePrefix(index);
+ return getNamespaceURI(prefix);
+ }
+
+ public String getAttributePrefix(int index)
+ {
+ Attribute a = (Attribute) attrs.get(index);
+ return a.prefix;
+ }
+
+ public QName getAttributeName(int index)
+ {
+ Attribute a = (Attribute) attrs.get(index);
+ String namespaceURI = getNamespaceURI(a.prefix);
+ return new QName(namespaceURI, a.localName, a.prefix);
+ }
+
+ public String getAttributeType(int index)
+ {
+ Attribute a = (Attribute) attrs.get(index);
+ return a.type;
+ }
+
+ private String getAttributeType(String elementName, String attName)
+ {
+ if (doctype != null)
+ {
+ AttributeDecl att = doctype.getAttributeDecl(elementName, attName);
+ if (att != null)
+ return att.type;
+ }
+ return "CDATA";
+ }
+
+ public String getAttributeValue(int index)
+ {
+ Attribute a = (Attribute) attrs.get(index);
+ return a.value;
+ }
+
+ public String getAttributeValue(String namespaceURI, String localName)
+ {
+ for (Iterator i = attrs.iterator(); i.hasNext(); )
+ {
+ Attribute a = (Attribute) i.next();
+ if (a.localName.equals(localName))
+ {
+ String uri = getNamespaceURI(a.prefix);
+ if ((uri == null && namespaceURI == null) ||
+ (uri != null && uri.equals(namespaceURI)))
+ return a.value;
+ }
+ }
+ return null;
+ }
+
+ boolean isAttributeDeclared(int index)
+ {
+ if (doctype == null)
+ return false;
+ Attribute a = (Attribute) attrs.get(index);
+ String qn = ("".equals(a.prefix)) ? a.localName :
+ a.prefix + ":" + a.localName;
+ String elementName = buf.toString();
+ return doctype.isAttributeDeclared(elementName, qn);
+ }
+
+ public String getCharacterEncodingScheme()
+ {
+ return xmlEncoding;
+ }
+
+ public String getElementText()
+ throws XMLStreamException
+ {
+ if (event != XMLStreamConstants.START_ELEMENT)
+ throw new XMLStreamException("current event must be START_ELEMENT");
+ CPStringBuilder elementText = new CPStringBuilder();
+ int depth = stack.size();
+ while (event != XMLStreamConstants.END_ELEMENT || stack.size() > depth)
+ {
+ switch (next())
+ {
+ case XMLStreamConstants.CHARACTERS:
+ case XMLStreamConstants.SPACE:
+ elementText.append(buf.toString());
+ }
+ }
+ return elementText.toString();
+ }
+
+ public String getEncoding()
+ {
+ return (input.inputEncoding == null) ? "UTF-8" : input.inputEncoding;
+ }
+
+ public int getEventType()
+ {
+ return event;
+ }
+
+ public String getLocalName()
+ {
+ switch (event)
+ {
+ case XMLStreamConstants.START_ELEMENT:
+ case XMLStreamConstants.END_ELEMENT:
+ String qName = buf.toString();
+ int ci = qName.indexOf(':');
+ String localName = (ci == -1) ? qName : qName.substring(ci + 1);
+ if (stringInterning)
+ localName = localName.intern();
+ return localName;
+ default:
+ return null;
+ }
+ }
+
+ public Location getLocation()
+ {
+ return input;
+ }
+
+ public QName getName()
+ {
+ switch (event)
+ {
+ case XMLStreamConstants.START_ELEMENT:
+ case XMLStreamConstants.END_ELEMENT:
+ String qName = buf.toString();
+ int ci = qName.indexOf(':');
+ String localName = (ci == -1) ? qName : qName.substring(ci + 1);
+ if (stringInterning)
+ localName = localName.intern();
+ String prefix = (ci == -1) ?
+ (namespaceAware ? XMLConstants.DEFAULT_NS_PREFIX : null) :
+ qName.substring(0, ci);
+ if (stringInterning && prefix != null)
+ prefix = prefix.intern();
+ String namespaceURI = getNamespaceURI(prefix);
+ return new QName(namespaceURI, localName, prefix);
+ default:
+ return null;
+ }
+ }
+
+ public int getNamespaceCount()
+ {
+ if (!namespaceAware || namespaces.isEmpty())
+ return 0;
+ switch (event)
+ {
+ case XMLStreamConstants.START_ELEMENT:
+ case XMLStreamConstants.END_ELEMENT:
+ LinkedHashMap ctx = (LinkedHashMap) namespaces.getFirst();
+ return ctx.size();
+ default:
+ return 0;
+ }
+ }
+
+ public String getNamespacePrefix(int index)
+ {
+ LinkedHashMap ctx = (LinkedHashMap) namespaces.getFirst();
+ int count = 0;
+ for (Iterator i = ctx.keySet().iterator(); i.hasNext(); )
+ {
+ String prefix = (String) i.next();
+ if (count++ == index)
+ return prefix;
+ }
+ return null;
+ }
+
+ public String getNamespaceURI()
+ {
+ switch (event)
+ {
+ case XMLStreamConstants.START_ELEMENT:
+ case XMLStreamConstants.END_ELEMENT:
+ String qName = buf.toString();
+ int ci = qName.indexOf(':');
+ if (ci == -1)
+ return null;
+ String prefix = qName.substring(0, ci);
+ return getNamespaceURI(prefix);
+ default:
+ return null;
+ }
+ }
+
+ public String getNamespaceURI(int index)
+ {
+ LinkedHashMap ctx = (LinkedHashMap) namespaces.getFirst();
+ int count = 0;
+ for (Iterator i = ctx.values().iterator(); i.hasNext(); )
+ {
+ String uri = (String) i.next();
+ if (count++ == index)
+ return uri;
+ }
+ return null;
+ }
+
+ public String getPIData()
+ {
+ return piData;
+ }
+
+ public String getPITarget()
+ {
+ return piTarget;
+ }
+
+ public String getPrefix()
+ {
+ switch (event)
+ {
+ case XMLStreamConstants.START_ELEMENT:
+ case XMLStreamConstants.END_ELEMENT:
+ String qName = buf.toString();
+ int ci = qName.indexOf(':');
+ String prefix = (ci == -1) ?
+ (namespaceAware ? XMLConstants.DEFAULT_NS_PREFIX : null) :
+ qName.substring(0, ci);
+ if (stringInterning && prefix != null)
+ prefix = prefix.intern();
+ return prefix;
+ default:
+ return null;
+ }
+ }
+
+ public Object getProperty(String name)
+ throws IllegalArgumentException
+ {
+ if (name == null)
+ throw new IllegalArgumentException("name is null");
+ if (XMLInputFactory.ALLOCATOR.equals(name))
+ return null;
+ if (XMLInputFactory.IS_COALESCING.equals(name))
+ return coalescing ? Boolean.TRUE : Boolean.FALSE;
+ if (XMLInputFactory.IS_NAMESPACE_AWARE.equals(name))
+ return namespaceAware ? Boolean.TRUE : Boolean.FALSE;
+ if (XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES.equals(name))
+ return replaceERefs ? Boolean.TRUE : Boolean.FALSE;
+ if (XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES.equals(name))
+ return externalEntities ? Boolean.TRUE : Boolean.FALSE;
+ if (XMLInputFactory.IS_VALIDATING.equals(name))
+ return Boolean.FALSE;
+ if (XMLInputFactory.REPORTER.equals(name))
+ return reporter;
+ if (XMLInputFactory.RESOLVER.equals(name))
+ return resolver;
+ if (XMLInputFactory.SUPPORT_DTD.equals(name))
+ return supportDTD ? Boolean.TRUE : Boolean.FALSE;
+ if ("gnu.xml.stream.stringInterning".equals(name))
+ return stringInterning ? Boolean.TRUE : Boolean.FALSE;
+ if ("gnu.xml.stream.xmlBase".equals(name))
+ return baseAware ? Boolean.TRUE : Boolean.FALSE;
+ if ("gnu.xml.stream.baseURI".equals(name))
+ return getXMLBase();
+ return null;
+ }
+
+ public String getText()
+ {
+ return buf.toString();
+ }
+
+ public char[] getTextCharacters()
+ {
+ return buf.toString().toCharArray();
+ }
+
+ public int getTextCharacters(int sourceStart, char[] target,
+ int targetStart, int length)
+ throws XMLStreamException
+ {
+ length = Math.min(sourceStart + buf.length(), length);
+ int sourceEnd = sourceStart + length;
+ buf.getChars(sourceStart, sourceEnd, target, targetStart);
+ return length;
+ }
+
+ public int getTextLength()
+ {
+ return buf.length();
+ }
+
+ public int getTextStart()
+ {
+ return 0;
+ }
+
+ public String getVersion()
+ {
+ return (xmlVersion == null) ? "1.0" : xmlVersion;
+ }
+
+ public boolean hasName()
+ {
+ switch (event)
+ {
+ case XMLStreamConstants.START_ELEMENT:
+ case XMLStreamConstants.END_ELEMENT:
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ public boolean hasText()
+ {
+ switch (event)
+ {
+ case XMLStreamConstants.CHARACTERS:
+ case XMLStreamConstants.SPACE:
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ public boolean isAttributeSpecified(int index)
+ {
+ Attribute a = (Attribute) attrs.get(index);
+ return a.specified;
+ }
+
+ public boolean isCharacters()
+ {
+ return (event == XMLStreamConstants.CHARACTERS);
+ }
+
+ public boolean isEndElement()
+ {
+ return (event == XMLStreamConstants.END_ELEMENT);
+ }
+
+ public boolean isStandalone()
+ {
+ return Boolean.TRUE.equals(xmlStandalone);
+ }
+
+ public boolean isStartElement()
+ {
+ return (event == XMLStreamConstants.START_ELEMENT);
+ }
+
+ public boolean isWhiteSpace()
+ {
+ return (event == XMLStreamConstants.SPACE);
+ }
+
+ public int nextTag()
+ throws XMLStreamException
+ {
+ do
+ {
+ switch (next())
+ {
+ case XMLStreamConstants.START_ELEMENT:
+ case XMLStreamConstants.END_ELEMENT:
+ case XMLStreamConstants.CHARACTERS:
+ case XMLStreamConstants.SPACE:
+ case XMLStreamConstants.COMMENT:
+ case XMLStreamConstants.PROCESSING_INSTRUCTION:
+ break;
+ default:
+ throw new XMLStreamException("Unexpected event type: " + event);
+ }
+ }
+ while (event != XMLStreamConstants.START_ELEMENT &&
+ event != XMLStreamConstants.END_ELEMENT);
+ return event;
+ }
+
+ public void require(int type, String namespaceURI, String localName)
+ throws XMLStreamException
+ {
+ if (event != type)
+ throw new XMLStreamException("Current event type is " + event);
+ if (event == XMLStreamConstants.START_ELEMENT ||
+ event == XMLStreamConstants.END_ELEMENT)
+ {
+ String ln = getLocalName();
+ if (!ln.equals(localName))
+ throw new XMLStreamException("Current local-name is " + ln);
+ String uri = getNamespaceURI();
+ if ((uri == null && namespaceURI != null) ||
+ (uri != null && !uri.equals(namespaceURI)))
+ throw new XMLStreamException("Current namespace URI is " + uri);
+ }
+ }
+
+ public boolean standaloneSet()
+ {
+ return (xmlStandalone != null);
+ }
+
+ public boolean hasNext()
+ throws XMLStreamException
+ {
+ return (event != XMLStreamConstants.END_DOCUMENT && event != -1);
+ }
+
+ public int next()
+ throws XMLStreamException
+ {
+ if (event == XMLStreamConstants.END_ELEMENT)
+ {
+ // Pop namespace context
+ if (namespaceAware && !namespaces.isEmpty())
+ namespaces.removeFirst();
+ // Pop base context
+ if (baseAware && !bases.isEmpty())
+ bases.removeFirst();
+ }
+ if (!startEntityStack.isEmpty())
+ {
+ String entityName = (String) startEntityStack.removeFirst();
+ buf.setLength(0);
+ buf.append(entityName);
+ event = START_ENTITY;
+ return extendedEventTypes ? event : next();
+ }
+ else if (!endEntityStack.isEmpty())
+ {
+ String entityName = (String) endEntityStack.removeFirst();
+ buf.setLength(0);
+ buf.append(entityName);
+ event = END_ENTITY;
+ return extendedEventTypes ? event : next();
+ }
+ try
+ {
+ if (!input.initialized)
+ input.init();
+ switch (state)
+ {
+ case CONTENT:
+ if (tryRead(TEST_END_ELEMENT))
+ {
+ readEndElement();
+ if (stack.isEmpty())
+ state = MISC;
+ event = XMLStreamConstants.END_ELEMENT;
+ }
+ else if (tryRead(TEST_COMMENT))
+ {
+ readComment(false);
+ event = XMLStreamConstants.COMMENT;
+ }
+ else if (tryRead(TEST_PI))
+ {
+ readPI(false);
+ event = XMLStreamConstants.PROCESSING_INSTRUCTION;
+ }
+ else if (tryRead(TEST_CDATA))
+ {
+ readCDSect();
+ event = XMLStreamConstants.CDATA;
+ }
+ else if (tryRead(TEST_START_ELEMENT))
+ {
+ state = readStartElement();
+ event = XMLStreamConstants.START_ELEMENT;
+ }
+ else
+ {
+ // Check for character reference or predefined entity
+ mark(8);
+ int c = readCh();
+ if (c == 0x26) // '&'
+ {
+ c = readCh();
+ if (c == 0x23) // '#'
+ {
+ reset();
+ event = readCharData(null);
+ }
+ else
+ {
+ // entity reference
+ reset();
+ readCh(); // &
+ readReference();
+ String ref = buf.toString();
+ String text = (String) PREDEFINED_ENTITIES.get(ref);
+ if (text != null)
+ {
+ event = readCharData(text);
+ }
+ else if (replaceERefs && !isUnparsedEntity(ref))
+ {
+ // this will report a start-entity event
+ boolean external = false;
+ if (doctype != null)
+ {
+ Object entity = doctype.getEntity(ref);
+ if (entity instanceof ExternalIds)
+ external = true;
+ }
+ expandEntity(ref, false, external);
+ event = next();
+ }
+ else
+ {
+ event = XMLStreamConstants.ENTITY_REFERENCE;
+ }
+ }
+ }
+ else
+ {
+ reset();
+ event = readCharData(null);
+ if (validating && doctype != null)
+ validatePCData(buf.toString());
+ }
+ }
+ break;
+ case EMPTY_ELEMENT:
+ String elementName = (String) stack.removeLast();
+ buf.setLength(0);
+ buf.append(elementName);
+ state = stack.isEmpty() ? MISC : CONTENT;
+ event = XMLStreamConstants.END_ELEMENT;
+ if (validating && doctype != null)
+ endElementValidationHook();
+ break;
+ case INIT: // XMLDecl?
+ if (tryRead(TEST_XML_DECL))
+ readXMLDecl();
+ input.finalizeEncoding();
+ event = XMLStreamConstants.START_DOCUMENT;
+ state = PROLOG;
+ break;
+ case PROLOG: // Misc* (doctypedecl Misc*)?
+ skipWhitespace();
+ if (doctype == null && tryRead(TEST_DOCTYPE_DECL))
+ {
+ readDoctypeDecl();
+ event = XMLStreamConstants.DTD;
+ }
+ else if (tryRead(TEST_COMMENT))
+ {
+ readComment(false);
+ event = XMLStreamConstants.COMMENT;
+ }
+ else if (tryRead(TEST_PI))
+ {
+ readPI(false);
+ event = XMLStreamConstants.PROCESSING_INSTRUCTION;
+ }
+ else if (tryRead(TEST_START_ELEMENT))
+ {
+ state = readStartElement();
+ event = XMLStreamConstants.START_ELEMENT;
+ }
+ else
+ {
+ int c = readCh();
+ error("no root element: U+" + Integer.toHexString(c));
+ }
+ break;
+ case MISC: // Comment | PI | S
+ skipWhitespace();
+ if (tryRead(TEST_COMMENT))
+ {
+ readComment(false);
+ event = XMLStreamConstants.COMMENT;
+ }
+ else if (tryRead(TEST_PI))
+ {
+ readPI(false);
+ event = XMLStreamConstants.PROCESSING_INSTRUCTION;
+ }
+ else
+ {
+ if (event == XMLStreamConstants.END_DOCUMENT)
+ throw new NoSuchElementException();
+ int c = readCh();
+ if (c != -1)
+ error("Only comments and PIs may appear after " +
+ "the root element");
+ event = XMLStreamConstants.END_DOCUMENT;
+ }
+ break;
+ default:
+ event = -1;
+ }
+ return event;
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException();
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ // package private
+
+ /**
+ * Returns the current element name.
+ */
+ String getCurrentElement()
+ {
+ return (String) stack.getLast();
+ }
+
+ // private
+
+ private void mark(int limit)
+ throws IOException
+ {
+ input.mark(limit);
+ }
+
+ private void reset()
+ throws IOException
+ {
+ input.reset();
+ }
+
+ private int read()
+ throws IOException
+ {
+ return input.read();
+ }
+
+ private int read(int[] b, int off, int len)
+ throws IOException
+ {
+ return input.read(b, off, len);
+ }
+
+ /**
+ * Parsed character read.
+ */
+ private int readCh()
+ throws IOException, XMLStreamException
+ {
+ int c = read();
+ if (expandPE && c == 0x25) // '%'
+ {
+ if (peIsError)
+ error("PE reference within decl in internal subset.");
+ expandPEReference();
+ return readCh();
+ }
+ return c;
+ }
+
+ /**
+ * Reads the next character, ensuring it is the character specified.
+ * @param delim the character to match
+ * @exception XMLStreamException if the next character is not the
+ * specified one
+ */
+ private void require(char delim)
+ throws IOException, XMLStreamException
+ {
+ mark(1);
+ int c = readCh();
+ if (delim != c)
+ {
+ reset();
+ error("required character (got U+" + Integer.toHexString(c) + ")",
+ new Character(delim));
+ }
+ }
+
+ /**
+ * Reads the next few characters, ensuring they match the string specified.
+ * @param delim the string to match
+ * @exception XMLStreamException if the next characters do not match the
+ * specified string
+ */
+ private void require(String delim)
+ throws IOException, XMLStreamException
+ {
+ char[] chars = delim.toCharArray();
+ int len = chars.length;
+ mark(len);
+ int off = 0;
+ do
+ {
+ int l2 = read(tmpBuf, off, len - off);
+ if (l2 == -1)
+ {
+ reset();
+ error("EOF before required string", delim);
+ }
+ off += l2;
+ }
+ while (off < len);
+ for (int i = 0; i < chars.length; i++)
+ {
+ if (chars[i] != tmpBuf[i])
+ {
+ reset();
+ error("required string", delim);
+ }
+ }
+ }
+
+ /**
+ * Try to read a single character. On failure, reset the stream.
+ * @param delim the character to test
+ * @return true if the character matched delim, false otherwise.
+ */
+ private boolean tryRead(char delim)
+ throws IOException, XMLStreamException
+ {
+ mark(1);
+ int c = readCh();
+ if (delim != c)
+ {
+ reset();
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Tries to read the specified characters.
+ * If successful, the stream is positioned after the last character,
+ * otherwise it is reset.
+ * @param test the string to test
+ * @return true if the characters matched the test string, false otherwise.
+ */
+ private boolean tryRead(String test)
+ throws IOException
+ {
+ char[] chars = test.toCharArray();
+ int len = chars.length;
+ mark(len);
+ int count = 0;
+ int l2 = read(tmpBuf, 0, len);
+ if (l2 == -1)
+ {
+ reset();
+ return false;
+ }
+ count += l2;
+ // check the characters we received first before doing additional reads
+ for (int i = 0; i < count; i++)
+ {
+ if (chars[i] != tmpBuf[i])
+ {
+ reset();
+ return false;
+ }
+ }
+ while (count < len)
+ {
+ // force read
+ int c = read();
+ if (c == -1)
+ {
+ reset();
+ return false;
+ }
+ tmpBuf[count] = (char) c;
+ // check each character as it is read
+ if (chars[count] != tmpBuf[count])
+ {
+ reset();
+ return false;
+ }
+ count++;
+ }
+ return true;
+ }
+
+ /**
+ * Reads characters until the specified test string is encountered.
+ * @param delim the string delimiting the end of the characters
+ */
+ private void readUntil(String delim)
+ throws IOException, XMLStreamException
+ {
+ int startLine = input.line;
+ try
+ {
+ while (!tryRead(delim))
+ {
+ int c = readCh();
+ if (c == -1)
+ throw new EOFException();
+ else if (input.xml11)
+ {
+ if (!isXML11Char(c) || isXML11RestrictedChar(c))
+ error("illegal XML 1.1 character",
+ "U+" + Integer.toHexString(c));
+ }
+ else if (!isChar(c))
+ error("illegal XML character",
+ "U+" + Integer.toHexString(c));
+ buf.append(Character.toChars(c));
+ }
+ }
+ catch (EOFException e)
+ {
+ error("end of input while looking for delimiter "+
+ "(started on line " + startLine + ')', delim);
+ }
+ }
+
+ /**
+ * Reads any whitespace characters.
+ * @return true if whitespace characters were read, false otherwise
+ */
+ private boolean tryWhitespace()
+ throws IOException, XMLStreamException
+ {
+ boolean white;
+ boolean ret = false;
+ do
+ {
+ mark(1);
+ int c = readCh();
+ while (c == -1 && inputStack.size() > 1)
+ {
+ popInput();
+ c = readCh();
+ }
+ white = (c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d);
+ if (white)
+ ret = true;
+ }
+ while (white);
+ reset();
+ return ret;
+ }
+
+ /**
+ * Skip over any whitespace characters.
+ */
+ private void skipWhitespace()
+ throws IOException, XMLStreamException
+ {
+ boolean white;
+ do
+ {
+ mark(1);
+ int c = readCh();
+ while (c == -1 && inputStack.size() > 1)
+ {
+ popInput();
+ c = readCh();
+ }
+ white = (c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d);
+ }
+ while (white);
+ reset();
+ }
+
+ /**
+ * Try to read as many whitespace characters as are available.
+ * @exception XMLStreamException if no whitespace characters were seen
+ */
+ private void requireWhitespace()
+ throws IOException, XMLStreamException
+ {
+ if (!tryWhitespace())
+ error("whitespace required");
+ }
+
+ /**
+ * Returns the current base URI for resolving external entities.
+ */
+ String getXMLBase()
+ {
+ if (baseAware)
+ {
+ for (Iterator i = bases.iterator(); i.hasNext(); )
+ {
+ String base = (String) i.next();
+ if (base != null)
+ return base;
+ }
+ }
+ return input.systemId;
+ }
+
+ /**
+ * Push the specified text input source.
+ */
+ private void pushInput(String name, String text, boolean report,
+ boolean normalize)
+ throws IOException, XMLStreamException
+ {
+ // Check for recursion
+ if (name != null && !"".equals(name))
+ {
+ for (Iterator i = inputStack.iterator(); i.hasNext(); )
+ {
+ Input ctx = (Input) i.next();
+ if (name.equals(ctx.name))
+ error("entities may not be self-recursive", name);
+ }
+ }
+ else
+ report = false;
+ pushInput(new Input(null, new StringReader(text), input.publicId,
+ input.systemId, name, input.inputEncoding, report,
+ normalize));
+ }
+
+ /**
+ * Push the specified external input source.
+ */
+ private void pushInput(String name, ExternalIds ids, boolean report,
+ boolean normalize)
+ throws IOException, XMLStreamException
+ {
+ if (!externalEntities)
+ return;
+ String url = canonicalize(absolutize(input.systemId, ids.systemId));
+ // Check for recursion
+ for (Iterator i = inputStack.iterator(); i.hasNext(); )
+ {
+ Input ctx = (Input) i.next();
+ if (url.equals(ctx.systemId))
+ error("entities may not be self-recursive", url);
+ if (name != null && !"".equals(name) && name.equals(ctx.name))
+ error("entities may not be self-recursive", name);
+ }
+ if (name == null || "".equals(name))
+ report = false;
+ InputStream in = null;
+ if (resolver != null)
+ {
+ Object obj = resolver.resolveEntity(ids.publicId, url, getXMLBase(),
+ null);
+ if (obj instanceof InputStream)
+ in = (InputStream) obj;
+ }
+ if (in == null)
+ in = resolve(url);
+ if (in == null)
+ error("unable to resolve external entity",
+ (ids.systemId != null) ? ids.systemId : ids.publicId);
+ pushInput(new Input(in, null, ids.publicId, url, name, null, report,
+ normalize));
+ input.init();
+ if (tryRead(TEST_XML_DECL))
+ readTextDecl();
+ input.finalizeEncoding();
+ }
+
+ /**
+ * Push the specified input source (general entity) onto the input stack.
+ */
+ private void pushInput(Input input)
+ {
+ if (input.report)
+ startEntityStack.addFirst(input.name);
+ inputStack.addLast(input);
+ if (this.input != null)
+ input.xml11 = this.input.xml11;
+ this.input = input;
+ }
+
+ /**
+ * Returns a canonicalized version of the specified URL.
+ * This is largely to work around a problem with the specification of
+ * file URLs.
+ */
+ static String canonicalize(String url)
+ {
+ if (url == null)
+ return null;
+ if (url.startsWith("file:") && !url.startsWith("file://"))
+ url = "file://" + url.substring(5);
+ return url;
+ }
+
+ /**
+ * "Absolutize" a URL. This resolves a relative URL into an absolute one.
+ * @param base the current base URL
+ * @param href the (absolute or relative) URL to resolve
+ */
+ public static String absolutize(String base, String href)
+ {
+ if (href == null)
+ return null;
+ int ci = href.indexOf(':');
+ if (ci > 1 && isURLScheme(href.substring(0, ci)))
+ {
+ // href is absolute already
+ return href;
+ }
+ if (base == null)
+ base = "";
+ else
+ {
+ int i = base.lastIndexOf('/');
+ if (i != -1)
+ base = base.substring(0, i + 1);
+ else
+ base = "";
+ }
+ if ("".equals(base))
+ {
+ // assume file URL relative to current directory
+ base = System.getProperty("user.dir");
+ if (base.charAt(0) == '/')
+ base = base.substring(1);
+ base = "file:///" + base.replace(File.separatorChar, '/');
+ if (!base.endsWith("/"))
+ base += "/";
+ }
+ // We can't use java.net.URL here to do the parsing, as it searches for
+ // a protocol handler. A protocol handler may not be registered for the
+ // URL scheme here. Do it manually.
+ //
+ // Set aside scheme and host portion of base URL
+ String basePrefix = null;
+ ci = base.indexOf(':');
+ if (ci > 1 && isURLScheme(base.substring(0, ci)))
+ {
+ if (base.length() > (ci + 3) &&
+ base.charAt(ci + 1) == '/' &&
+ base.charAt(ci + 2) == '/')
+ {
+ int si = base.indexOf('/', ci + 3);
+ if (si == -1)
+ base = null;
+ else
+ {
+ basePrefix = base.substring(0, si);
+ base = base.substring(si);
+ }
+ }
+ else
+ base = null;
+ }
+ if (base == null) // unknown or malformed base URL, use href
+ return href;
+ if (href.startsWith("/")) // absolute href pathname
+ return (basePrefix == null) ? href : basePrefix + href;
+ // relative href pathname
+ if (!base.endsWith("/"))
+ {
+ int lsi = base.lastIndexOf('/');
+ if (lsi == -1)
+ base = "/";
+ else
+ base = base.substring(0, lsi + 1);
+ }
+ while (href.startsWith("../") || href.startsWith("./"))
+ {
+ if (href.startsWith("../"))
+ {
+ // strip last path component from base
+ int lsi = base.lastIndexOf('/', base.length() - 2);
+ if (lsi > -1)
+ base = base.substring(0, lsi + 1);
+ href = href.substring(3); // strip ../ prefix
+ }
+ else
+ {
+ href = href.substring(2); // strip ./ prefix
+ }
+ }
+ return (basePrefix == null) ? base + href : basePrefix + base + href;
+ }
+
+ /**
+ * Indicates whether the specified characters match the scheme portion of
+ * a URL.
+ * @see RFC 1738 section 2.1
+ */
+ private static boolean isURLScheme(String text)
+ {
+ int len = text.length();
+ for (int i = 0; i < len; i++)
+ {
+ char c = text.charAt(i);
+ if (c == '+' || c == '.' || c == '-')
+ continue;
+ if (c < 65 || (c > 90 && c < 97) || c > 122)
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Returns an input stream for the given URL.
+ */
+ static InputStream resolve(String url)
+ throws IOException
+ {
+ try
+ {
+ return new URL(url).openStream();
+ }
+ catch (MalformedURLException e)
+ {
+ return null;
+ }
+ catch (IOException e)
+ {
+ IOException e2 = new IOException("error resolving " + url);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ /**
+ * Pops the current input source (general entity) off the stack.
+ */
+ private void popInput()
+ {
+ Input old = (Input) inputStack.removeLast();
+ if (old.report)
+ endEntityStack.addFirst(old.name);
+ input = (Input) inputStack.getLast();
+ }
+
+ /**
+ * Parse an entity text declaration.
+ */
+ private void readTextDecl()
+ throws IOException, XMLStreamException
+ {
+ final int flags = LIT_DISABLE_CREF | LIT_DISABLE_PE | LIT_DISABLE_EREF;
+ requireWhitespace();
+ if (tryRead("version"))
+ {
+ readEq();
+ String v = readLiteral(flags, false);
+ if ("1.0".equals(v))
+ input.xml11 = false;
+ else if ("1.1".equals(v))
+ {
+ Input i1 = (Input) inputStack.getFirst();
+ if (!i1.xml11)
+ error("external entity specifies later version number");
+ input.xml11 = true;
+ }
+ else
+ throw new XMLStreamException("illegal XML version: " + v);
+ requireWhitespace();
+ }
+ require("encoding");
+ readEq();
+ String enc = readLiteral(flags, false);
+ skipWhitespace();
+ require("?>");
+ input.setInputEncoding(enc);
+ }
+
+ /**
+ * Parse the XML declaration.
+ */
+ private void readXMLDecl()
+ throws IOException, XMLStreamException
+ {
+ final int flags = LIT_DISABLE_CREF | LIT_DISABLE_PE | LIT_DISABLE_EREF;
+
+ requireWhitespace();
+ require("version");
+ readEq();
+ xmlVersion = readLiteral(flags, false);
+ if ("1.0".equals(xmlVersion))
+ input.xml11 = false;
+ else if ("1.1".equals(xmlVersion))
+ input.xml11 = true;
+ else
+ throw new XMLStreamException("illegal XML version: " + xmlVersion);
+
+ boolean white = tryWhitespace();
+
+ if (tryRead("encoding"))
+ {
+ if (!white)
+ error("whitespace required before 'encoding='");
+ readEq();
+ xmlEncoding = readLiteral(flags, false);
+ white = tryWhitespace();
+ }
+
+ if (tryRead("standalone"))
+ {
+ if (!white)
+ error("whitespace required before 'standalone='");
+ readEq();
+ String standalone = readLiteral(flags, false);
+ if ("yes".equals(standalone))
+ xmlStandalone = Boolean.TRUE;
+ else if ("no".equals(standalone))
+ xmlStandalone = Boolean.FALSE;
+ else
+ error("standalone flag must be 'yes' or 'no'", standalone);
+ }
+
+ skipWhitespace();
+ require("?>");
+ if (xmlEncoding != null)
+ input.setInputEncoding(xmlEncoding);
+ }
+
+ /**
+ * Parse the DOCTYPE declaration.
+ */
+ private void readDoctypeDecl()
+ throws IOException, XMLStreamException
+ {
+ if (!supportDTD)
+ error("parser was configured not to support DTDs");
+ requireWhitespace();
+ String rootName = readNmtoken(true);
+ skipWhitespace();
+ ExternalIds ids = readExternalIds(false, true);
+ doctype =
+ this.new Doctype(rootName, ids.publicId, ids.systemId);
+
+ // Parse internal subset first
+ skipWhitespace();
+ if (tryRead('['))
+ {
+ while (true)
+ {
+ expandPE = true;
+ skipWhitespace();
+ expandPE = false;
+ if (tryRead(']'))
+ break;
+ else
+ readMarkupdecl(false);
+ }
+ }
+ skipWhitespace();
+ require('>');
+
+ // Parse external subset
+ if (ids.systemId != null && externalEntities)
+ {
+ pushInput("", ">", false, false);
+ pushInput("[dtd]", ids, true, true);
+ // loop until we get back to ">"
+ while (true)
+ {
+ expandPE = true;
+ skipWhitespace();
+ expandPE = false;
+ mark(1);
+ int c = readCh();
+ if (c == 0x3e) // '>'
+ break;
+ else if (c == -1)
+ popInput();
+ else
+ {
+ reset();
+ expandPE = true;
+ readMarkupdecl(true);
+ expandPE = true;
+ }
+ }
+ if (inputStack.size() != 2)
+ error("external subset has unmatched '>'");
+ popInput();
+ }
+ checkDoctype();
+ if (validating)
+ validateDoctype();
+
+ // Make rootName available for reading
+ buf.setLength(0);
+ buf.append(rootName);
+ }
+
+ /**
+ * Checks the well-formedness of the DTD.
+ */
+ private void checkDoctype()
+ throws XMLStreamException
+ {
+ // TODO check entity recursion
+ }
+
+ /**
+ * Parse the markupdecl production.
+ */
+ private void readMarkupdecl(boolean inExternalSubset)
+ throws IOException, XMLStreamException
+ {
+ boolean saved = expandPE;
+ mark(1);
+ require('<');
+ reset();
+ expandPE = false;
+ if (tryRead(TEST_ELEMENT_DECL))
+ {
+ expandPE = saved;
+ readElementDecl();
+ }
+ else if (tryRead(TEST_ATTLIST_DECL))
+ {
+ expandPE = saved;
+ readAttlistDecl();
+ }
+ else if (tryRead(TEST_ENTITY_DECL))
+ {
+ expandPE = saved;
+ readEntityDecl(inExternalSubset);
+ }
+ else if (tryRead(TEST_NOTATION_DECL))
+ {
+ expandPE = saved;
+ readNotationDecl(inExternalSubset);
+ }
+ else if (tryRead(TEST_PI))
+ {
+ readPI(true);
+ expandPE = saved;
+ }
+ else if (tryRead(TEST_COMMENT))
+ {
+ readComment(true);
+ expandPE = saved;
+ }
+ else if (tryRead("<!["))
+ {
+ // conditional section
+ expandPE = saved;
+ if (inputStack.size() < 2)
+ error("conditional sections illegal in internal subset");
+ skipWhitespace();
+ if (tryRead("INCLUDE"))
+ {
+ skipWhitespace();
+ require('[');
+ skipWhitespace();
+ while (!tryRead("]]>"))
+ {
+ readMarkupdecl(inExternalSubset);
+ skipWhitespace();
+ }
+ }
+ else if (tryRead("IGNORE"))
+ {
+ skipWhitespace();
+ require('[');
+ expandPE = false;
+ for (int nesting = 1; nesting > 0; )
+ {
+ int c = readCh();
+ switch (c)
+ {
+ case 0x3c: // '<'
+ if (tryRead("!["))
+ nesting++;
+ break;
+ case 0x5d: // ']'
+ if (tryRead("]>"))
+ nesting--;
+ break;
+ case -1:
+ throw new EOFException();
+ }
+ }
+ expandPE = saved;
+ }
+ else
+ error("conditional section must begin with INCLUDE or IGNORE");
+ }
+ else
+ error("expected markup declaration");
+ }
+
+ /**
+ * Parse the elementdecl production.
+ */
+ private void readElementDecl()
+ throws IOException, XMLStreamException
+ {
+ requireWhitespace();
+ boolean saved = expandPE;
+ expandPE = (inputStack.size() > 1);
+ String name = readNmtoken(true);
+ expandPE = saved;
+ requireWhitespace();
+ readContentspec(name);
+ skipWhitespace();
+ require('>');
+ }
+
+ /**
+ * Parse the contentspec production.
+ */
+ private void readContentspec(String elementName)
+ throws IOException, XMLStreamException
+ {
+ if (tryRead("EMPTY"))
+ doctype.addElementDecl(elementName, "EMPTY", new EmptyContentModel());
+ else if (tryRead("ANY"))
+ doctype.addElementDecl(elementName, "ANY", new AnyContentModel());
+ else
+ {
+ ContentModel model;
+ CPStringBuilder acc = new CPStringBuilder();
+ require('(');
+ acc.append('(');
+ skipWhitespace();
+ if (tryRead("#PCDATA"))
+ {
+ // mixed content
+ acc.append("#PCDATA");
+ MixedContentModel mm = new MixedContentModel();
+ model = mm;
+ skipWhitespace();
+ if (tryRead(')'))
+ {
+ acc.append(")");
+ if (tryRead('*'))
+ {
+ mm.min = 0;
+ mm.max = -1;
+ }
+ }
+ else
+ {
+ while (!tryRead(")"))
+ {
+ require('|');
+ acc.append('|');
+ skipWhitespace();
+ String name = readNmtoken(true);
+ acc.append(name);
+ mm.addName(name);
+ skipWhitespace();
+ }
+ require('*');
+ acc.append(")*");
+ mm.min = 0;
+ mm.max = -1;
+ }
+ }
+ else
+ model = readElements(acc);
+ doctype.addElementDecl(elementName, acc.toString(), model);
+ }
+ }
+
+ /**
+ * Parses an element content model.
+ */
+ private ElementContentModel readElements(CPStringBuilder acc)
+ throws IOException, XMLStreamException
+ {
+ int separator;
+ ElementContentModel model = new ElementContentModel();
+
+ // Parse first content particle
+ skipWhitespace();
+ model.addContentParticle(readContentParticle(acc));
+ // End or separator
+ skipWhitespace();
+ int c = readCh();
+ switch (c)
+ {
+ case 0x29: // ')'
+ acc.append(')');
+ mark(1);
+ c = readCh();
+ switch (c)
+ {
+ case 0x3f: // '?'
+ acc.append('?');
+ model.min = 0;
+ model.max = 1;
+ break;
+ case 0x2a: // '*'
+ acc.append('*');
+ model.min = 0;
+ model.max = -1;
+ break;
+ case 0x2b: // '+'
+ acc.append('+');
+ model.min = 1;
+ model.max = -1;
+ break;
+ default:
+ reset();
+ }
+ return model; // done
+ case 0x7c: // '|'
+ model.or = true;
+ // fall through
+ case 0x2c: // ','
+ separator = c;
+ acc.append(Character.toChars(c));
+ break;
+ default:
+ error("bad separator in content model",
+ "U+" + Integer.toHexString(c));
+ return model;
+ }
+ // Parse subsequent content particles
+ while (true)
+ {
+ skipWhitespace();
+ model.addContentParticle(readContentParticle(acc));
+ skipWhitespace();
+ c = readCh();
+ if (c == 0x29) // ')'
+ {
+ acc.append(')');
+ break;
+ }
+ else if (c != separator)
+ {
+ error("bad separator in content model",
+ "U+" + Integer.toHexString(c));
+ return model;
+ }
+ else
+ acc.append(c);
+ }
+ // Check for occurrence indicator
+ mark(1);
+ c = readCh();
+ switch (c)
+ {
+ case 0x3f: // '?'
+ acc.append('?');
+ model.min = 0;
+ model.max = 1;
+ break;
+ case 0x2a: // '*'
+ acc.append('*');
+ model.min = 0;
+ model.max = -1;
+ break;
+ case 0x2b: // '+'
+ acc.append('+');
+ model.min = 1;
+ model.max = -1;
+ break;
+ default:
+ reset();
+ }
+ return model;
+ }
+
+ /**
+ * Parse a cp production.
+ */
+ private ContentParticle readContentParticle(CPStringBuilder acc)
+ throws IOException, XMLStreamException
+ {
+ ContentParticle cp = new ContentParticle();
+ if (tryRead('('))
+ {
+ acc.append('(');
+ cp.content = readElements(acc);
+ }
+ else
+ {
+ String name = readNmtoken(true);
+ acc.append(name);
+ cp.content = name;
+ mark(1);
+ int c = readCh();
+ switch (c)
+ {
+ case 0x3f: // '?'
+ acc.append('?');
+ cp.min = 0;
+ cp.max = 1;
+ break;
+ case 0x2a: // '*'
+ acc.append('*');
+ cp.min = 0;
+ cp.max = -1;
+ break;
+ case 0x2b: // '+'
+ acc.append('+');
+ cp.min = 1;
+ cp.max = -1;
+ break;
+ default:
+ reset();
+ }
+ }
+ return cp;
+ }
+
+ /**
+ * Parse an attribute-list definition.
+ */
+ private void readAttlistDecl()
+ throws IOException, XMLStreamException
+ {
+ requireWhitespace();
+ boolean saved = expandPE;
+ expandPE = (inputStack.size() > 1);
+ String elementName = readNmtoken(true);
+ expandPE = saved;
+ boolean white = tryWhitespace();
+ while (!tryRead('>'))
+ {
+ if (!white)
+ error("whitespace required before attribute definition");
+ readAttDef(elementName);
+ white = tryWhitespace();
+ }
+ }
+
+ /**
+ * Parse a single attribute definition.
+ */
+ private void readAttDef(String elementName)
+ throws IOException, XMLStreamException
+ {
+ String name = readNmtoken(true);
+ requireWhitespace();
+ CPStringBuilder acc = new CPStringBuilder();
+ HashSet values = new HashSet();
+ String type = readAttType(acc, values);
+ if (validating)
+ {
+ if ("ID".equals(type))
+ {
+ // VC: One ID per Element Type
+ for (Iterator i = doctype.attlistIterator(elementName);
+ i.hasNext(); )
+ {
+ Map.Entry entry = (Map.Entry) i.next();
+ AttributeDecl decl = (AttributeDecl) entry.getValue();
+ if ("ID".equals(decl.type))
+ error("element types must not have more than one ID " +
+ "attribute");
+ }
+ }
+ else if ("NOTATION".equals(type))
+ {
+ // VC: One Notation Per Element Type
+ for (Iterator i = doctype.attlistIterator(elementName);
+ i.hasNext(); )
+ {
+ Map.Entry entry = (Map.Entry) i.next();
+ AttributeDecl decl = (AttributeDecl) entry.getValue();
+ if ("NOTATION".equals(decl.type))
+ error("element types must not have more than one NOTATION " +
+ "attribute");
+ }
+ // VC: No Notation on Empty Element
+ ContentModel model = doctype.getElementModel(elementName);
+ if (model != null && model.type == ContentModel.EMPTY)
+ error("attributes of type NOTATION must not be declared on an " +
+ "element declared EMPTY");
+ }
+ }
+ String enumer = null;
+ if ("ENUMERATION".equals(type) || "NOTATION".equals(type))
+ enumer = acc.toString();
+ else
+ values = null;
+ requireWhitespace();
+ readDefault(elementName, name, type, enumer, values);
+ }
+
+ /**
+ * Parse an attribute type.
+ */
+ private String readAttType(CPStringBuilder acc, HashSet values)
+ throws IOException, XMLStreamException
+ {
+ if (tryRead('('))
+ {
+ readEnumeration(false, acc, values);
+ return "ENUMERATION";
+ }
+ else
+ {
+ String typeString = readNmtoken(true);
+ if ("NOTATION".equals(typeString))
+ {
+ readNotationType(acc, values);
+ return typeString;
+ }
+ else if ("CDATA".equals(typeString) ||
+ "ID".equals(typeString) ||
+ "IDREF".equals(typeString) ||
+ "IDREFS".equals(typeString) ||
+ "ENTITY".equals(typeString) ||
+ "ENTITIES".equals(typeString) ||
+ "NMTOKEN".equals(typeString) ||
+ "NMTOKENS".equals(typeString))
+ return typeString;
+ else
+ {
+ error("illegal attribute type", typeString);
+ return null;
+ }
+ }
+ }
+
+ /**
+ * Parse an enumeration.
+ */
+ private void readEnumeration(boolean isNames, CPStringBuilder acc,
+ HashSet values)
+ throws IOException, XMLStreamException
+ {
+ acc.append('(');
+ // first token
+ skipWhitespace();
+ String token = readNmtoken(isNames);
+ acc.append(token);
+ values.add(token);
+ // subsequent tokens
+ skipWhitespace();
+ while (!tryRead(')'))
+ {
+ require('|');
+ acc.append('|');
+ skipWhitespace();
+ token = readNmtoken(isNames);
+ // VC: No Duplicate Tokens
+ if (validating && values.contains(token))
+ error("duplicate token", token);
+ acc.append(token);
+ values.add(token);
+ skipWhitespace();
+ }
+ acc.append(')');
+ }
+
+ /**
+ * Parse a notation type for an attribute.
+ */
+ private void readNotationType(CPStringBuilder acc, HashSet values)
+ throws IOException, XMLStreamException
+ {
+ requireWhitespace();
+ require('(');
+ readEnumeration(true, acc, values);
+ }
+
+ /**
+ * Parse the default value for an attribute.
+ */
+ private void readDefault(String elementName, String name,
+ String type, String enumeration, HashSet values)
+ throws IOException, XMLStreamException
+ {
+ int valueType = ATTRIBUTE_DEFAULT_SPECIFIED;
+ int flags = LIT_ATTRIBUTE;
+ String value = null, defaultType = null;
+ boolean saved = expandPE;
+
+ if (!"CDATA".equals(type))
+ flags |= LIT_NORMALIZE;
+
+ expandPE = false;
+ if (tryRead('#'))
+ {
+ if (tryRead("FIXED"))
+ {
+ defaultType = "#FIXED";
+ valueType = ATTRIBUTE_DEFAULT_FIXED;
+ requireWhitespace();
+ value = readLiteral(flags, false);
+ }
+ else if (tryRead("REQUIRED"))
+ {
+ defaultType = "#REQUIRED";
+ valueType = ATTRIBUTE_DEFAULT_REQUIRED;
+ }
+ else if (tryRead("IMPLIED"))
+ {
+ defaultType = "#IMPLIED";
+ valueType = ATTRIBUTE_DEFAULT_IMPLIED;
+ }
+ else
+ error("illegal keyword for attribute default value");
+ }
+ else
+ value = readLiteral(flags, false);
+ expandPE = saved;
+ if (validating)
+ {
+ if ("ID".equals(type))
+ {
+ // VC: Attribute Default Value Syntactically Correct
+ if (value != null && !isNmtoken(value, true))
+ error("default value must match Name production", value);
+ // VC: ID Attribute Default
+ if (valueType != ATTRIBUTE_DEFAULT_REQUIRED &&
+ valueType != ATTRIBUTE_DEFAULT_IMPLIED)
+ error("ID attributes must have a declared default of " +
+ "#IMPLIED or #REQUIRED");
+ }
+ else if (value != null)
+ {
+ // VC: Attribute Default Value Syntactically Correct
+ if ("IDREF".equals(type) || "ENTITY".equals(type))
+ {
+ if (!isNmtoken(value, true))
+ error("default value must match Name production", value);
+ }
+ else if ("IDREFS".equals(type) || "ENTITIES".equals(type))
+ {
+ StringTokenizer st = new StringTokenizer(value);
+ while (st.hasMoreTokens())
+ {
+ String token = st.nextToken();
+ if (!isNmtoken(token, true))
+ error("default value must match Name production", token);
+ }
+ }
+ else if ("NMTOKEN".equals(type) || "ENUMERATION".equals(type))
+ {
+ if (!isNmtoken(value, false))
+ error("default value must match Nmtoken production", value);
+ }
+ else if ("NMTOKENS".equals(type))
+ {
+ StringTokenizer st = new StringTokenizer(value);
+ while (st.hasMoreTokens())
+ {
+ String token = st.nextToken();
+ if (!isNmtoken(token, false))
+ error("default value must match Nmtoken production",
+ token);
+ }
+ }
+ }
+ }
+ // Register attribute def
+ AttributeDecl attribute =
+ new AttributeDecl(type, value, valueType, enumeration, values,
+ inputStack.size() != 1);
+ doctype.addAttributeDecl(elementName, name, attribute);
+ }
+
+ /**
+ * Parse the EntityDecl production.
+ */
+ private void readEntityDecl(boolean inExternalSubset)
+ throws IOException, XMLStreamException
+ {
+ int flags = 0;
+ // Check if parameter entity
+ boolean peFlag = false;
+ expandPE = false;
+ requireWhitespace();
+ if (tryRead('%'))
+ {
+ peFlag = true;
+ requireWhitespace();
+ }
+ expandPE = true;
+ // Read entity name
+ String name = readNmtoken(true);
+ if (name.indexOf(':') != -1)
+ error("illegal character ':' in entity name", name);
+ if (peFlag)
+ name = "%" + name;
+ requireWhitespace();
+ mark(1);
+ int c = readCh();
+ reset();
+ if (c == 0x22 || c == 0x27) // " | '
+ {
+ // Internal entity replacement text
+ String value = readLiteral(flags | LIT_DISABLE_EREF, true);
+ int ai = value.indexOf('&');
+ while (ai != -1)
+ {
+ int sci = value.indexOf(';', ai);
+ if (sci == -1)
+ error("malformed reference in entity value", value);
+ String ref = value.substring(ai + 1, sci);
+ int[] cp = UnicodeReader.toCodePointArray(ref);
+ if (cp.length == 0)
+ error("malformed reference in entity value", value);
+ if (cp[0] == 0x23) // #
+ {
+ if (cp.length == 1)
+ error("malformed reference in entity value", value);
+ if (cp[1] == 0x78) // 'x'
+ {
+ if (cp.length == 2)
+ error("malformed reference in entity value", value);
+ for (int i = 2; i < cp.length; i++)
+ {
+ int x = cp[i];
+ if (x < 0x30 ||
+ (x > 0x39 && x < 0x41) ||
+ (x > 0x46 && x < 0x61) ||
+ x > 0x66)
+ error("malformed character reference in entity value",
+ value);
+ }
+ }
+ else
+ {
+ for (int i = 1; i < cp.length; i++)
+ {
+ int x = cp[i];
+ if (x < 0x30 || x > 0x39)
+ error("malformed character reference in entity value",
+ value);
+ }
+ }
+ }
+ else
+ {
+ if (!isNameStartCharacter(cp[0], input.xml11))
+ error("malformed reference in entity value", value);
+ for (int i = 1; i < cp.length; i++)
+ {
+ if (!isNameCharacter(cp[i], input.xml11))
+ error("malformed reference in entity value", value);
+ }
+ }
+ ai = value.indexOf('&', sci);
+ }
+ doctype.addEntityDecl(name, value, inExternalSubset);
+ }
+ else
+ {
+ ExternalIds ids = readExternalIds(false, false);
+ // Check for NDATA
+ boolean white = tryWhitespace();
+ if (!peFlag && tryRead("NDATA"))
+ {
+ if (!white)
+ error("whitespace required before NDATA");
+ requireWhitespace();
+ ids.notationName = readNmtoken(true);
+ }
+ doctype.addEntityDecl(name, ids, inExternalSubset);
+ }
+ // finish
+ skipWhitespace();
+ require('>');
+ }
+
+ /**
+ * Parse the NotationDecl production.
+ */
+ private void readNotationDecl(boolean inExternalSubset)
+ throws IOException, XMLStreamException
+ {
+ requireWhitespace();
+ String notationName = readNmtoken(true);
+ if (notationName.indexOf(':') != -1)
+ error("illegal character ':' in notation name", notationName);
+ if (validating)
+ {
+ // VC: Unique Notation Name
+ ExternalIds notation = doctype.getNotation(notationName);
+ if (notation != null)
+ error("duplicate notation name", notationName);
+ }
+ requireWhitespace();
+ ExternalIds ids = readExternalIds(true, false);
+ ids.notationName = notationName;
+ doctype.addNotationDecl(notationName, ids, inExternalSubset);
+ skipWhitespace();
+ require('>');
+ }
+
+ /**
+ * Returns a tuple {publicId, systemId}.
+ */
+ private ExternalIds readExternalIds(boolean inNotation, boolean isSubset)
+ throws IOException, XMLStreamException
+ {
+ int c;
+ int flags = LIT_DISABLE_CREF | LIT_DISABLE_PE | LIT_DISABLE_EREF;
+ ExternalIds ids = new ExternalIds();
+
+ if (tryRead("PUBLIC"))
+ {
+ requireWhitespace();
+ ids.publicId = readLiteral(LIT_NORMALIZE | LIT_PUBID | flags, false);
+ if (inNotation)
+ {
+ skipWhitespace();
+ mark(1);
+ c = readCh();
+ reset();
+ if (c == 0x22 || c == 0x27) // " | '
+ {
+ String href = readLiteral(flags, false);
+ ids.systemId = absolutize(input.systemId, href);
+ }
+ }
+ else
+ {
+ requireWhitespace();
+ String href = readLiteral(flags, false);
+ ids.systemId = absolutize(input.systemId, href);
+ }
+ // Check valid URI characters
+ for (int i = 0; i < ids.publicId.length(); i++)
+ {
+ char d = ids.publicId.charAt(i);
+ if (d >= 'a' && d <= 'z')
+ continue;
+ if (d >= 'A' && d <= 'Z')
+ continue;
+ if (" \r\n0123456789-' ()+,./:=?;!*#@$_%".indexOf(d) != -1)
+ continue;
+ error("illegal PUBLIC id character",
+ "U+" + Integer.toHexString(d));
+ }
+ }
+ else if (tryRead("SYSTEM"))
+ {
+ requireWhitespace();
+ String href = readLiteral(flags, false);
+ ids.systemId = absolutize(input.systemId, href);
+ }
+ else if (!isSubset)
+ {
+ error("missing SYSTEM or PUBLIC keyword");
+ }
+ if (ids.systemId != null && !inNotation)
+ {
+ if (ids.systemId.indexOf('#') != -1)
+ error("SYSTEM id has a URI fragment", ids.systemId);
+ }
+ return ids;
+ }
+
+ /**
+ * Parse the start of an element.
+ * @return the state of the parser afterwards (EMPTY_ELEMENT or CONTENT)
+ */
+ private int readStartElement()
+ throws IOException, XMLStreamException
+ {
+ // Read element name
+ String elementName = readNmtoken(true);
+ attrs.clear();
+ // Push namespace context
+ if (namespaceAware)
+ {
+ if (elementName.charAt(0) == ':' ||
+ elementName.charAt(elementName.length() - 1) == ':')
+ error("not a QName", elementName);
+ namespaces.addFirst(new LinkedHashMap());
+ }
+ // Read element content
+ boolean white = tryWhitespace();
+ mark(1);
+ int c = readCh();
+ while (c != 0x2f && c != 0x3e) // '/' | '>'
+ {
+ // Read attribute
+ reset();
+ if (!white)
+ error("need whitespace between attributes");
+ readAttribute(elementName);
+ white = tryWhitespace();
+ mark(1);
+ c = readCh();
+ }
+ // supply defaulted attributes
+ if (doctype != null)
+ {
+ for (Iterator i = doctype.attlistIterator(elementName); i.hasNext(); )
+ {
+ Map.Entry entry = (Map.Entry) i.next();
+ String attName = (String) entry.getKey();
+ AttributeDecl decl = (AttributeDecl) entry.getValue();
+ if (validating)
+ {
+ switch (decl.valueType)
+ {
+ case ATTRIBUTE_DEFAULT_REQUIRED:
+ // VC: Required Attribute
+ if (decl.value == null && !attributeSpecified(attName))
+ error("value for " + attName + " attribute is required");
+ break;
+ case ATTRIBUTE_DEFAULT_FIXED:
+ // VC: Fixed Attribute Default
+ for (Iterator j = attrs.iterator(); j.hasNext(); )
+ {
+ Attribute a = (Attribute) j.next();
+ if (attName.equals(a.name) &&
+ !decl.value.equals(a.value))
+ error("value for " + attName + " attribute must be " +
+ decl.value);
+ }
+ break;
+ }
+ }
+ if (namespaceAware && attName.equals("xmlns"))
+ {
+ LinkedHashMap ctx =
+ (LinkedHashMap) namespaces.getFirst();
+ if (ctx.containsKey(XMLConstants.DEFAULT_NS_PREFIX))
+ continue; // namespace was specified
+ }
+ else if (namespaceAware && attName.startsWith("xmlns:"))
+ {
+ LinkedHashMap ctx =
+ (LinkedHashMap) namespaces.getFirst();
+ if (ctx.containsKey(attName.substring(6)))
+ continue; // namespace was specified
+ }
+ else if (attributeSpecified(attName))
+ continue;
+ if (decl.value == null)
+ continue;
+ // VC: Standalone Document Declaration
+ if (validating && decl.external && xmlStandalone == Boolean.TRUE)
+ error("standalone must be 'no' if attributes inherit values " +
+ "from externally declared markup declarations");
+ Attribute attr =
+ new Attribute(attName, decl.type, false, decl.value);
+ if (namespaceAware)
+ {
+ if (!addNamespace(attr))
+ attrs.add(attr);
+ }
+ else
+ attrs.add(attr);
+ }
+ }
+ if (baseAware)
+ {
+ String uri = getAttributeValue(XMLConstants.XML_NS_URI, "base");
+ String base = getXMLBase();
+ bases.addFirst(absolutize(base, uri));
+ }
+ if (namespaceAware)
+ {
+ // check prefix bindings
+ int ci = elementName.indexOf(':');
+ if (ci != -1)
+ {
+ String prefix = elementName.substring(0, ci);
+ String uri = getNamespaceURI(prefix);
+ if (uri == null)
+ error("unbound element prefix", prefix);
+ else if (input.xml11 && "".equals(uri))
+ error("XML 1.1 unbound element prefix", prefix);
+ }
+ for (Iterator i = attrs.iterator(); i.hasNext(); )
+ {
+ Attribute attr = (Attribute) i.next();
+ if (attr.prefix != null &&
+ !XMLConstants.XMLNS_ATTRIBUTE.equals(attr.prefix))
+ {
+ String uri = getNamespaceURI(attr.prefix);
+ if (uri == null)
+ error("unbound attribute prefix", attr.prefix);
+ else if (input.xml11 && "".equals(uri))
+ error("XML 1.1 unbound attribute prefix", attr.prefix);
+ }
+ }
+ }
+ if (validating && doctype != null)
+ {
+ validateStartElement(elementName);
+ currentContentModel = doctype.getElementModel(elementName);
+ if (currentContentModel == null)
+ error("no element declaration", elementName);
+ validationStack.add(new LinkedList());
+ }
+ // make element name available for read
+ buf.setLength(0);
+ buf.append(elementName);
+ // push element onto stack
+ stack.addLast(elementName);
+ switch (c)
+ {
+ case 0x3e: // '>'
+ return CONTENT;
+ case 0x2f: // '/'
+ require('>');
+ return EMPTY_ELEMENT;
+ }
+ return -1; // to satisfy compiler
+ }
+
+ /**
+ * Indicates whether the specified attribute name was specified for the
+ * current element.
+ */
+ private boolean attributeSpecified(String attName)
+ {
+ for (Iterator j = attrs.iterator(); j.hasNext(); )
+ {
+ Attribute a = (Attribute) j.next();
+ if (attName.equals(a.name))
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Parse an attribute.
+ */
+ private void readAttribute(String elementName)
+ throws IOException, XMLStreamException
+ {
+ // Read attribute name
+ String attributeName = readNmtoken(true);
+ String type = getAttributeType(elementName, attributeName);
+ readEq();
+ // Read literal
+ final int flags = LIT_ATTRIBUTE | LIT_ENTITY_REF;
+ String value = (type == null || "CDATA".equals(type)) ?
+ readLiteral(flags, false) : readLiteral(flags | LIT_NORMALIZE, false);
+ // add attribute event
+ Attribute attr = this.new Attribute(attributeName, type, true, value);
+ if (namespaceAware)
+ {
+ if (attributeName.charAt(0) == ':' ||
+ attributeName.charAt(attributeName.length() - 1) == ':')
+ error("not a QName", attributeName);
+ else if (attributeName.equals("xmlns"))
+ {
+ LinkedHashMap ctx = (LinkedHashMap) namespaces.getFirst();
+ if (ctx.containsKey(XMLConstants.DEFAULT_NS_PREFIX))
+ error("duplicate default namespace");
+ }
+ else if (attributeName.startsWith("xmlns:"))
+ {
+ LinkedHashMap ctx = (LinkedHashMap) namespaces.getFirst();
+ if (ctx.containsKey(attributeName.substring(6)))
+ error("duplicate namespace", attributeName.substring(6));
+ }
+ else if (attrs.contains(attr))
+ error("duplicate attribute", attributeName);
+ }
+ else if (attrs.contains(attr))
+ error("duplicate attribute", attributeName);
+ if (validating && doctype != null)
+ {
+ // VC: Attribute Value Type
+ AttributeDecl decl =
+ doctype.getAttributeDecl(elementName, attributeName);
+ if (decl == null)
+ error("attribute must be declared", attributeName);
+ if ("ENUMERATION".equals(decl.type))
+ {
+ // VC: Enumeration
+ if (!decl.values.contains(value))
+ error("value does not match enumeration " + decl.enumeration,
+ value);
+ }
+ else if ("ID".equals(decl.type))
+ {
+ // VC: ID
+ if (!isNmtoken(value, true))
+ error("ID values must match the Name production");
+ if (ids.contains(value))
+ error("Duplicate ID", value);
+ ids.add(value);
+ }
+ else if ("IDREF".equals(decl.type) || "IDREFS".equals(decl.type))
+ {
+ StringTokenizer st = new StringTokenizer(value);
+ while (st.hasMoreTokens())
+ {
+ String token = st.nextToken();
+ // VC: IDREF
+ if (!isNmtoken(token, true))
+ error("IDREF values must match the Name production");
+ idrefs.add(token);
+ }
+ }
+ else if ("NMTOKEN".equals(decl.type) || "NMTOKENS".equals(decl.type))
+ {
+ StringTokenizer st = new StringTokenizer(value);
+ while (st.hasMoreTokens())
+ {
+ String token = st.nextToken();
+ // VC: Name Token
+ if (!isNmtoken(token, false))
+ error("NMTOKEN values must match the Nmtoken production");
+ }
+ }
+ else if ("ENTITY".equals(decl.type))
+ {
+ // VC: Entity Name
+ if (!isNmtoken(value, true))
+ error("ENTITY values must match the Name production");
+ Object entity = doctype.getEntity(value);
+ if (entity == null || !(entity instanceof ExternalIds) ||
+ ((ExternalIds) entity).notationName == null)
+ error("ENTITY values must match the name of an unparsed " +
+ "entity declared in the DTD");
+ }
+ else if ("NOTATION".equals(decl.type))
+ {
+ if (!decl.values.contains(value))
+ error("NOTATION values must match a declared notation name",
+ value);
+ // VC: Notation Attributes
+ ExternalIds notation = doctype.getNotation(value);
+ if (notation == null)
+ error("NOTATION values must match the name of a notation " +
+ "declared in the DTD", value);
+ }
+ }
+ if (namespaceAware)
+ {
+ if (!addNamespace(attr))
+ attrs.add(attr);
+ }
+ else
+ attrs.add(attr);
+ }
+
+ /**
+ * Determines whether the specified attribute is a namespace declaration,
+ * and adds it to the current namespace context if so. Returns false if
+ * the attribute is an ordinary attribute.
+ */
+ private boolean addNamespace(Attribute attr)
+ throws XMLStreamException
+ {
+ if ("xmlns".equals(attr.name))
+ {
+ LinkedHashMap ctx = (LinkedHashMap) namespaces.getFirst();
+ if (ctx.get(XMLConstants.DEFAULT_NS_PREFIX) != null)
+ error("Duplicate default namespace declaration");
+ if (XMLConstants.XML_NS_URI.equals(attr.value))
+ error("can't bind XML namespace");
+ ctx.put(XMLConstants.DEFAULT_NS_PREFIX, attr.value);
+ return true;
+ }
+ else if ("xmlns".equals(attr.prefix))
+ {
+ LinkedHashMap ctx = (LinkedHashMap) namespaces.getFirst();
+ if (ctx.get(attr.localName) != null)
+ error("Duplicate namespace declaration for prefix",
+ attr.localName);
+ if (XMLConstants.XML_NS_PREFIX.equals(attr.localName))
+ {
+ if (!XMLConstants.XML_NS_URI.equals(attr.value))
+ error("can't redeclare xml prefix");
+ else
+ return false; // treat as attribute
+ }
+ if (XMLConstants.XML_NS_URI.equals(attr.value))
+ error("can't bind non-xml prefix to XML namespace");
+ if (XMLConstants.XMLNS_ATTRIBUTE.equals(attr.localName))
+ error("can't redeclare xmlns prefix");
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attr.value))
+ error("can't bind non-xmlns prefix to XML Namespace namespace");
+ if ("".equals(attr.value) && !input.xml11)
+ error("illegal use of 1.1-style prefix unbinding in 1.0 document");
+ ctx.put(attr.localName, attr.value);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Parse a closing tag.
+ */
+ private void readEndElement()
+ throws IOException, XMLStreamException
+ {
+ // pop element off stack
+ String expected = (String) stack.removeLast();
+ require(expected);
+ skipWhitespace();
+ require('>');
+ // Make element name available
+ buf.setLength(0);
+ buf.append(expected);
+ if (validating && doctype != null)
+ endElementValidationHook();
+ }
+
+ /**
+ * Validate the end of an element.
+ * Called on an end-element or empty element if validating.
+ */
+ private void endElementValidationHook()
+ throws XMLStreamException
+ {
+ validateEndElement();
+ validationStack.removeLast();
+ if (stack.isEmpty())
+ currentContentModel = null;
+ else
+ {
+ String parent = (String) stack.getLast();
+ currentContentModel = doctype.getElementModel(parent);
+ }
+ }
+
+ /**
+ * Parse a comment.
+ */
+ private void readComment(boolean inDTD)
+ throws IOException, XMLStreamException
+ {
+ boolean saved = expandPE;
+ expandPE = false;
+ buf.setLength(0);
+ readUntil(TEST_END_COMMENT);
+ require('>');
+ expandPE = saved;
+ if (inDTD)
+ doctype.addComment(buf.toString());
+ }
+
+ /**
+ * Parse a processing instruction.
+ */
+ private void readPI(boolean inDTD)
+ throws IOException, XMLStreamException
+ {
+ boolean saved = expandPE;
+ expandPE = false;
+ piTarget = readNmtoken(true);
+ if (piTarget.indexOf(':') != -1)
+ error("illegal character in PI target", new Character(':'));
+ if ("xml".equalsIgnoreCase(piTarget))
+ error("illegal PI target", piTarget);
+ if (tryRead(TEST_END_PI))
+ piData = null;
+ else
+ {
+ if (!tryWhitespace())
+ error("whitespace required between PI target and data");
+ buf.setLength(0);
+ readUntil(TEST_END_PI);
+ piData = buf.toString();
+ }
+ expandPE = saved;
+ if (inDTD)
+ doctype.addPI(piTarget, piData);
+ }
+
+ /**
+ * Parse an entity reference.
+ */
+ private void readReference()
+ throws IOException, XMLStreamException
+ {
+ buf.setLength(0);
+ String entityName = readNmtoken(true);
+ require(';');
+ buf.setLength(0);
+ buf.append(entityName);
+ }
+
+ /**
+ * Read an CDATA section.
+ */
+ private void readCDSect()
+ throws IOException, XMLStreamException
+ {
+ buf.setLength(0);
+ readUntil(TEST_END_CDATA);
+ }
+
+ /**
+ * Read character data.
+ * @return the type of text read (CHARACTERS or SPACE)
+ */
+ private int readCharData(String prefix)
+ throws IOException, XMLStreamException
+ {
+ boolean white = true;
+ buf.setLength(0);
+ if (prefix != null)
+ buf.append(prefix);
+ boolean done = false;
+ boolean entities = false;
+ while (!done)
+ {
+ // Block read
+ mark(tmpBuf.length);
+ int len = read(tmpBuf, 0, tmpBuf.length);
+ if (len == -1)
+ {
+ if (inputStack.size() > 1)
+ {
+ popInput();
+ // report end-entity
+ done = true;
+ }
+ else
+ throw new EOFException();
+ }
+ for (int i = 0; i < len && !done; i++)
+ {
+ int c = tmpBuf[i];
+ switch (c)
+ {
+ case 0x20:
+ case 0x09:
+ case 0x0a:
+ case 0x0d:
+ buf.append(Character.toChars(c));
+ break; // whitespace
+ case 0x26: // '&'
+ reset();
+ read(tmpBuf, 0, i);
+ // character reference?
+ mark(3);
+ c = readCh(); // &
+ c = readCh();
+ if (c == 0x23) // '#'
+ {
+ mark(1);
+ c = readCh();
+ boolean hex = (c == 0x78); // 'x'
+ if (!hex)
+ reset();
+ char[] ch = readCharacterRef(hex ? 16 : 10);
+ buf.append(ch, 0, ch.length);
+ for (int j = 0; j < ch.length; j++)
+ {
+ switch (ch[j])
+ {
+ case 0x20:
+ case 0x09:
+ case 0x0a:
+ case 0x0d:
+ break; // whitespace
+ default:
+ white = false;
+ }
+ }
+ }
+ else
+ {
+ // entity reference
+ reset();
+ c = readCh(); // &
+ String entityName = readNmtoken(true);
+ require(';');
+ String text =
+ (String) PREDEFINED_ENTITIES.get(entityName);
+ if (text != null)
+ buf.append(text);
+ else
+ {
+ pushInput("", "&" + entityName + ";", false, false);
+ done = true;
+ break;
+ }
+ }
+ // continue processing
+ i = -1;
+ mark(tmpBuf.length);
+ len = read(tmpBuf, 0, tmpBuf.length);
+ if (len == -1)
+ {
+ if (inputStack.size() > 1)
+ {
+ popInput();
+ done = true;
+ }
+ else
+ throw new EOFException();
+ }
+ entities = true;
+ break; // end of text sequence
+ case 0x3e: // '>'
+ int l = buf.length();
+ if (l > 1 &&
+ buf.charAt(l - 1) == ']' &&
+ buf.charAt(l - 2) == ']')
+ error("Character data may not contain unescaped ']]>'");
+ buf.append(Character.toChars(c));
+ break;
+ case 0x3c: // '<'
+ reset();
+ // read i characters
+ int count = 0, remaining = i;
+ do
+ {
+ int r = read(tmpBuf, 0, remaining);
+ count += r;
+ remaining -= r;
+ }
+ while (count < i);
+ i = len;
+ if (coalescing && tryRead(TEST_CDATA))
+ readUntil(TEST_END_CDATA); // read CDATA section into buf
+ else
+ done = true; // end of text sequence
+ break;
+ default:
+ if (input.xml11)
+ {
+ if (!isXML11Char(c) || isXML11RestrictedChar(c))
+ error("illegal XML 1.1 character",
+ "U+" + Integer.toHexString(c));
+ }
+ else if (!isChar(c))
+ error("illegal XML character",
+ "U+" + Integer.toHexString(c));
+ white = false;
+ buf.append(Character.toChars(c));
+ }
+ }
+ // if text buffer >= 2MB, return it as a chunk
+ // to avoid excessive memory use
+ if (buf.length() >= 2097152)
+ done = true;
+ }
+ if (entities)
+ normalizeCRLF(buf);
+ return white ? XMLStreamConstants.SPACE : XMLStreamConstants.CHARACTERS;
+ }
+
+ /**
+ * Expands the specified entity.
+ */
+ private void expandEntity(String name, boolean inAttr, boolean normalize)
+ throws IOException, XMLStreamException
+ {
+ if (doctype != null)
+ {
+ Object value = doctype.getEntity(name);
+ if (value != null)
+ {
+ if (xmlStandalone == Boolean.TRUE)
+ {
+ // VC: Standalone Document Declaration
+ if (doctype.isEntityExternal(name))
+ error("reference to external entity in standalone document");
+ else if (value instanceof ExternalIds)
+ {
+ ExternalIds ids = (ExternalIds) value;
+ if (ids.notationName != null &&
+ doctype.isNotationExternal(ids.notationName))
+ error("reference to external notation in " +
+ "standalone document");
+ }
+ }
+ if (value instanceof String)
+ {
+ String text = (String) value;
+ if (inAttr && text.indexOf('<') != -1)
+ error("< in attribute value");
+ pushInput(name, text, !inAttr, normalize);
+ }
+ else if (inAttr)
+ error("reference to external entity in attribute value", name);
+ else
+ pushInput(name, (ExternalIds) value, !inAttr, normalize);
+ return;
+ }
+ }
+ error("reference to undeclared entity", name);
+ }
+
+ /**
+ * Indicates whether the specified entity is unparsed.
+ */
+ private boolean isUnparsedEntity(String name)
+ {
+ if (doctype != null)
+ {
+ Object value = doctype.getEntity(name);
+ if (value != null && value instanceof ExternalIds)
+ return ((ExternalIds) value).notationName != null;
+ }
+ return false;
+ }
+
+ /**
+ * Read an equals sign.
+ */
+ private void readEq()
+ throws IOException, XMLStreamException
+ {
+ skipWhitespace();
+ require('=');
+ skipWhitespace();
+ }
+
+ /**
+ * Character read for reading literals.
+ * @param recognizePEs whether to recognize parameter-entity references
+ */
+ private int literalReadCh(boolean recognizePEs)
+ throws IOException, XMLStreamException
+ {
+ int c = recognizePEs ? readCh() : read();
+ while (c == -1)
+ {
+ if (inputStack.size() > 1)
+ {
+ inputStack.removeLast();
+ input = (Input) inputStack.getLast();
+ // Don't issue end-entity
+ c = recognizePEs ? readCh() : read();
+ }
+ else
+ throw new EOFException();
+ }
+ return c;
+ }
+
+ /**
+ * Read a string literal.
+ */
+ private String readLiteral(int flags, boolean recognizePEs)
+ throws IOException, XMLStreamException
+ {
+ boolean saved = expandPE;
+ int delim = readCh();
+ if (delim != 0x27 && delim != 0x22)
+ error("expected '\"' or \"'\"", "U+" + Integer.toHexString(delim));
+ literalBuf.setLength(0);
+ if ((flags & LIT_DISABLE_PE) != 0)
+ expandPE = false;
+ boolean entities = false;
+ int inputStackSize = inputStack.size();
+ do
+ {
+ int c = literalReadCh(recognizePEs);
+ if (c == delim && inputStackSize == inputStack.size())
+ break;
+ switch (c)
+ {
+ case 0x0a:
+ case 0x0d:
+ if ((flags & (LIT_ATTRIBUTE | LIT_PUBID)) != 0)
+ c = 0x20; // normalize to space
+ break;
+ case 0x09:
+ if ((flags & LIT_ATTRIBUTE) != 0)
+ c = 0x20; // normalize to space
+ break;
+ case 0x26: // '&'
+ mark(2);
+ c = readCh();
+ if (c == 0x23) // '#'
+ {
+ if ((flags & LIT_DISABLE_CREF) != 0)
+ {
+ reset();
+ c = 0x26; // '&'
+ }
+ else
+ {
+ mark(1);
+ c = readCh();
+ boolean hex = (c == 0x78); // 'x'
+ if (!hex)
+ reset();
+ char[] ref = readCharacterRef(hex ? 16 : 10);
+ for (int i = 0; i < ref.length; i++)
+ literalBuf.append(ref[i]);
+ entities = true;
+ continue;
+ }
+ }
+ else
+ {
+ if ((flags & LIT_DISABLE_EREF) != 0)
+ {
+ reset();
+ c = 0x26; // '&'
+ }
+ else
+ {
+ reset();
+ String entityName = readNmtoken(true);
+ require(';');
+ String text =
+ (String) PREDEFINED_ENTITIES.get(entityName);
+ if (text != null)
+ literalBuf.append(text);
+ else
+ expandEntity(entityName,
+ (flags & LIT_ATTRIBUTE) != 0,
+ true);
+ entities = true;
+ continue;
+ }
+ }
+ break;
+ case 0x3c: // '<'
+ if ((flags & LIT_ATTRIBUTE) != 0)
+ error("attribute values may not contain '<'");
+ break;
+ case -1:
+ if (inputStack.size() > 1)
+ {
+ popInput();
+ continue;
+ }
+ throw new EOFException();
+ default:
+ if ((c < 0x0020 || c > 0xfffd) ||
+ (c >= 0xd800 && c < 0xdc00) ||
+ (input.xml11 && (c >= 0x007f) &&
+ (c <= 0x009f) && (c != 0x0085)))
+ error("illegal character", "U+" + Integer.toHexString(c));
+ }
+ literalBuf.append(Character.toChars(c));
+ }
+ while (true);
+ expandPE = saved;
+ if (entities)
+ normalizeCRLF(literalBuf);
+ if ((flags & LIT_NORMALIZE) > 0)
+ literalBuf = normalize(literalBuf);
+ return literalBuf.toString();
+ }
+
+ /**
+ * Performs attribute-value normalization of the text buffer.
+ * This discards leading and trailing whitespace, and replaces sequences
+ * of whitespace with a single space.
+ */
+ private StringBuffer normalize(StringBuffer buf)
+ {
+ StringBuffer acc = new StringBuffer();
+ int len = buf.length();
+ int avState = 0;
+ for (int i = 0; i < len; i++)
+ {
+ char c = buf.charAt(i);
+ if (c == ' ')
+ avState = (avState == 0) ? 0 : 1;
+ else
+ {
+ if (avState == 1)
+ acc.append(' ');
+ acc.append(c);
+ avState = 2;
+ }
+ }
+ return acc;
+ }
+
+ /**
+ * Replace any CR/LF pairs in the buffer with LF.
+ * This may be necessary if combinations of CR or LF were declared as
+ * (character) entity references in the input.
+ */
+ private void normalizeCRLF(StringBuffer buf)
+ {
+ int len = buf.length() - 1;
+ for (int i = 0; i < len; i++)
+ {
+ char c = buf.charAt(i);
+ if (c == '\r' && buf.charAt(i + 1) == '\n')
+ {
+ buf.deleteCharAt(i--);
+ len--;
+ }
+ }
+ }
+
+ /**
+ * Parse and expand a parameter entity reference.
+ */
+ private void expandPEReference()
+ throws IOException, XMLStreamException
+ {
+ String name = readNmtoken(true, new StringBuffer());
+ require(';');
+ mark(1); // ensure we don't reset to before the semicolon
+ if (doctype != null)
+ {
+ String entityName = "%" + name;
+ Object entity = doctype.getEntity(entityName);
+ if (entity != null)
+ {
+ if (xmlStandalone == Boolean.TRUE)
+ {
+ if (doctype.isEntityExternal(entityName))
+ error("reference to external parameter entity in " +
+ "standalone document");
+ }
+ if (entity instanceof String)
+ {
+ pushInput(name, (String) entity, false, input.normalize);
+ //pushInput(name, " " + (String) entity + " ");
+ }
+ else
+ {
+ //pushInput("", " ");
+ pushInput(name, (ExternalIds) entity, false, input.normalize);
+ //pushInput("", " ");
+ }
+ }
+ else
+ error("reference to undeclared parameter entity", name);
+ }
+ else
+ error("reference to parameter entity without doctype", name);
+ }
+
+ /**
+ * Parse the digits in a character reference.
+ * @param base the base of the digits (10 or 16)
+ */
+ private char[] readCharacterRef(int base)
+ throws IOException, XMLStreamException
+ {
+ CPStringBuilder b = new CPStringBuilder();
+ for (int c = readCh(); c != 0x3b && c != -1; c = readCh())
+ b.append(Character.toChars(c));
+ try
+ {
+ int ord = Integer.parseInt(b.toString(), base);
+ if (input.xml11)
+ {
+ if (!isXML11Char(ord))
+ error("illegal XML 1.1 character reference " +
+ "U+" + Integer.toHexString(ord));
+ }
+ else
+ {
+ if ((ord < 0x20 && !(ord == 0x0a || ord == 0x09 || ord == 0x0d))
+ || (ord >= 0xd800 && ord <= 0xdfff)
+ || ord == 0xfffe || ord == 0xffff
+ || ord > 0x0010ffff)
+ error("illegal XML character reference " +
+ "U+" + Integer.toHexString(ord));
+ }
+ return Character.toChars(ord);
+ }
+ catch (NumberFormatException e)
+ {
+ error("illegal characters in character reference", b.toString());
+ return null;
+ }
+ }
+
+ /**
+ * Parses an NMTOKEN or Name production.
+ * @param isName if a Name, otherwise an NMTOKEN
+ */
+ private String readNmtoken(boolean isName)
+ throws IOException, XMLStreamException
+ {
+ return readNmtoken(isName, nmtokenBuf);
+ }
+
+ /**
+ * Parses an NMTOKEN or Name production using the specified buffer.
+ * @param isName if a Name, otherwise an NMTOKEN
+ * @param buf the character buffer to use
+ */
+ private String readNmtoken(boolean isName, StringBuffer buf)
+ throws IOException, XMLStreamException
+ {
+ buf.setLength(0);
+ int c = readCh();
+ if (isName)
+ {
+ if (!isNameStartCharacter(c, input.xml11))
+ error("not a name start character",
+ "U+" + Integer.toHexString(c));
+ }
+ else
+ {
+ if (!isNameCharacter(c, input.xml11))
+ error("not a name character",
+ "U+" + Integer.toHexString(c));
+ }
+ buf.append(Character.toChars(c));
+ do
+ {
+ mark(1);
+ c = readCh();
+ switch (c)
+ {
+ case 0x25: // '%'
+ case 0x3c: // '<'
+ case 0x3e: // '>'
+ case 0x26: // '&'
+ case 0x2c: // ','
+ case 0x7c: // '|'
+ case 0x2a: // '*'
+ case 0x2b: // '+'
+ case 0x3f: // '?'
+ case 0x29: // ')'
+ case 0x3d: // '='
+ case 0x27: // '\''
+ case 0x22: // '"'
+ case 0x5b: // '['
+ case 0x20: // ' '
+ case 0x09: // '\t'
+ case 0x0a: // '\n'
+ case 0x0d: // '\r'
+ case 0x3b: // ';'
+ case 0x2f: // '/'
+ case -1:
+ reset();
+ return intern(buf.toString());
+ default:
+ if (!isNameCharacter(c, input.xml11))
+ error("not a name character",
+ "U+" + Integer.toHexString(c));
+ else
+ buf.append(Character.toChars(c));
+ }
+ }
+ while (true);
+ }
+
+ /**
+ * Indicates whether the specified Unicode character is an XML 1.1 Char.
+ */
+ public static boolean isXML11Char(int c)
+ {
+ return ((c >= 0x0001 && c <= 0xD7FF) ||
+ (c >= 0xE000 && c < 0xFFFE) ||
+ (c >= 0x10000 && c <= 0x10FFFF));
+ }
+
+ /**
+ * Indicates whether the specified Unicode character is an XML 1.1
+ * RestrictedChar.
+ */
+ public static boolean isXML11RestrictedChar(int c)
+ {
+ return ((c >= 0x0001 && c <= 0x0008) ||
+ (c >= 0x000B && c <= 0x000C) ||
+ (c >= 0x000E && c <= 0x001F) ||
+ (c >= 0x007F && c <= 0x0084) ||
+ (c >= 0x0086 && c <= 0x009F));
+ }
+
+ /**
+ * Indicates whether the specified text matches the Name or Nmtoken
+ * production.
+ */
+ private boolean isNmtoken(String text, boolean isName)
+ {
+ try
+ {
+ int[] cp = UnicodeReader.toCodePointArray(text);
+ if (cp.length == 0)
+ return false;
+ if (isName)
+ {
+ if (!isNameStartCharacter(cp[0], input.xml11))
+ return false;
+ }
+ else
+ {
+ if (!isNameCharacter(cp[0], input.xml11))
+ return false;
+ }
+ for (int i = 1; i < cp.length; i++)
+ {
+ if (!isNameCharacter(cp[i], input.xml11))
+ return false;
+ }
+ return true;
+ }
+ catch (IOException e)
+ {
+ return false;
+ }
+ }
+
+ /**
+ * Indicates whether the specified Unicode character is a Name start
+ * character.
+ */
+ public static boolean isNameStartCharacter(int c, boolean xml11)
+ {
+ if (xml11)
+ return ((c >= 0x0041 && c <= 0x005a) ||
+ (c >= 0x0061 && c <= 0x007a) ||
+ c == 0x3a |
+ c == 0x5f |
+ (c >= 0xC0 && c <= 0xD6) ||
+ (c >= 0xD8 && c <= 0xF6) ||
+ (c >= 0xF8 && c <= 0x2FF) ||
+ (c >= 0x370 && c <= 0x37D) ||
+ (c >= 0x37F && c <= 0x1FFF) ||
+ (c >= 0x200C && c <= 0x200D) ||
+ (c >= 0x2070 && c <= 0x218F) ||
+ (c >= 0x2C00 && c <= 0x2FEF) ||
+ (c >= 0x3001 && c <= 0xD7FF) ||
+ (c >= 0xF900 && c <= 0xFDCF) ||
+ (c >= 0xFDF0 && c <= 0xFFFD) ||
+ (c >= 0x10000 && c <= 0xEFFFF));
+ else
+ return (c == 0x5f || c == 0x3a || isLetter(c));
+ }
+
+ /**
+ * Indicates whether the specified Unicode character is a Name non-initial
+ * character.
+ */
+ public static boolean isNameCharacter(int c, boolean xml11)
+ {
+ if (xml11)
+ return ((c >= 0x0041 && c <= 0x005a) ||
+ (c >= 0x0061 && c <= 0x007a) ||
+ (c >= 0x0030 && c <= 0x0039) ||
+ c == 0x3a |
+ c == 0x5f |
+ c == 0x2d |
+ c == 0x2e |
+ c == 0xB7 |
+ (c >= 0xC0 && c <= 0xD6) ||
+ (c >= 0xD8 && c <= 0xF6) ||
+ (c >= 0xF8 && c <= 0x2FF) ||
+ (c >= 0x300 && c <= 0x37D) ||
+ (c >= 0x37F && c <= 0x1FFF) ||
+ (c >= 0x200C && c <= 0x200D) ||
+ (c >= 0x203F && c <= 0x2040) ||
+ (c >= 0x2070 && c <= 0x218F) ||
+ (c >= 0x2C00 && c <= 0x2FEF) ||
+ (c >= 0x3001 && c <= 0xD7FF) ||
+ (c >= 0xF900 && c <= 0xFDCF) ||
+ (c >= 0xFDF0 && c <= 0xFFFD) ||
+ (c >= 0x10000 && c <= 0xEFFFF));
+ else
+ return (c == 0x2e || c == 0x2d || c == 0x5f || c == 0x3a ||
+ isLetter(c) || isDigit(c) ||
+ isCombiningChar(c) || isExtender(c));
+ }
+
+ /**
+ * Indicates whether the specified Unicode character matches the Letter
+ * production.
+ */
+ public static boolean isLetter(int c)
+ {
+ if ((c >= 0x0041 && c <= 0x005A) ||
+ (c >= 0x0061 && c <= 0x007A) ||
+ (c >= 0x00C0 && c <= 0x00D6) ||
+ (c >= 0x00D8 && c <= 0x00F6) ||
+ (c >= 0x00F8 && c <= 0x00FF) ||
+ (c >= 0x0100 && c <= 0x0131) ||
+ (c >= 0x0134 && c <= 0x013E) ||
+ (c >= 0x0141 && c <= 0x0148) ||
+ (c >= 0x014A && c <= 0x017E) ||
+ (c >= 0x0180 && c <= 0x01C3) ||
+ (c >= 0x01CD && c <= 0x01F0) ||
+ (c >= 0x01F4 && c <= 0x01F5) ||
+ (c >= 0x01FA && c <= 0x0217) ||
+ (c >= 0x0250 && c <= 0x02A8) ||
+ (c >= 0x02BB && c <= 0x02C1) ||
+ c == 0x0386 ||
+ (c >= 0x0388 && c <= 0x038A) ||
+ c == 0x038C ||
+ (c >= 0x038E && c <= 0x03A1) ||
+ (c >= 0x03A3 && c <= 0x03CE) ||
+ (c >= 0x03D0 && c <= 0x03D6) ||
+ c == 0x03DA ||
+ c == 0x03DC ||
+ c == 0x03DE ||
+ c == 0x03E0 ||
+ (c >= 0x03E2 && c <= 0x03F3) ||
+ (c >= 0x0401 && c <= 0x040C) ||
+ (c >= 0x040E && c <= 0x044F) ||
+ (c >= 0x0451 && c <= 0x045C) ||
+ (c >= 0x045E && c <= 0x0481) ||
+ (c >= 0x0490 && c <= 0x04C4) ||
+ (c >= 0x04C7 && c <= 0x04C8) ||
+ (c >= 0x04CB && c <= 0x04CC) ||
+ (c >= 0x04D0 && c <= 0x04EB) ||
+ (c >= 0x04EE && c <= 0x04F5) ||
+ (c >= 0x04F8 && c <= 0x04F9) ||
+ (c >= 0x0531 && c <= 0x0556) ||
+ c == 0x0559 ||
+ (c >= 0x0561 && c <= 0x0586) ||
+ (c >= 0x05D0 && c <= 0x05EA) ||
+ (c >= 0x05F0 && c <= 0x05F2) ||
+ (c >= 0x0621 && c <= 0x063A) ||
+ (c >= 0x0641 && c <= 0x064A) ||
+ (c >= 0x0671 && c <= 0x06B7) ||
+ (c >= 0x06BA && c <= 0x06BE) ||
+ (c >= 0x06C0 && c <= 0x06CE) ||
+ (c >= 0x06D0 && c <= 0x06D3) ||
+ c == 0x06D5 ||
+ (c >= 0x06E5 && c <= 0x06E6) ||
+ (c >= 0x0905 && c <= 0x0939) ||
+ c == 0x093D ||
+ (c >= 0x0958 && c <= 0x0961) ||
+ (c >= 0x0985 && c <= 0x098C) ||
+ (c >= 0x098F && c <= 0x0990) ||
+ (c >= 0x0993 && c <= 0x09A8) ||
+ (c >= 0x09AA && c <= 0x09B0) ||
+ c == 0x09B2 ||
+ (c >= 0x09B6 && c <= 0x09B9) ||
+ (c >= 0x09DC && c <= 0x09DD) ||
+ (c >= 0x09DF && c <= 0x09E1) ||
+ (c >= 0x09F0 && c <= 0x09F1) ||
+ (c >= 0x0A05 && c <= 0x0A0A) ||
+ (c >= 0x0A0F && c <= 0x0A10) ||
+ (c >= 0x0A13 && c <= 0x0A28) ||
+ (c >= 0x0A2A && c <= 0x0A30) ||
+ (c >= 0x0A32 && c <= 0x0A33) ||
+ (c >= 0x0A35 && c <= 0x0A36) ||
+ (c >= 0x0A38 && c <= 0x0A39) ||
+ (c >= 0x0A59 && c <= 0x0A5C) ||
+ c == 0x0A5E ||
+ (c >= 0x0A72 && c <= 0x0A74) ||
+ (c >= 0x0A85 && c <= 0x0A8B) ||
+ c == 0x0A8D ||
+ (c >= 0x0A8F && c <= 0x0A91) ||
+ (c >= 0x0A93 && c <= 0x0AA8) ||
+ (c >= 0x0AAA && c <= 0x0AB0) ||
+ (c >= 0x0AB2 && c <= 0x0AB3) ||
+ (c >= 0x0AB5 && c <= 0x0AB9) ||
+ c == 0x0ABD ||
+ c == 0x0AE0 ||
+ (c >= 0x0B05 && c <= 0x0B0C) ||
+ (c >= 0x0B0F && c <= 0x0B10) ||
+ (c >= 0x0B13 && c <= 0x0B28) ||
+ (c >= 0x0B2A && c <= 0x0B30) ||
+ (c >= 0x0B32 && c <= 0x0B33) ||
+ (c >= 0x0B36 && c <= 0x0B39) ||
+ c == 0x0B3D ||
+ (c >= 0x0B5C && c <= 0x0B5D) ||
+ (c >= 0x0B5F && c <= 0x0B61) ||
+ (c >= 0x0B85 && c <= 0x0B8A) ||
+ (c >= 0x0B8E && c <= 0x0B90) ||
+ (c >= 0x0B92 && c <= 0x0B95) ||
+ (c >= 0x0B99 && c <= 0x0B9A) ||
+ c == 0x0B9C ||
+ (c >= 0x0B9E && c <= 0x0B9F) ||
+ (c >= 0x0BA3 && c <= 0x0BA4) ||
+ (c >= 0x0BA8 && c <= 0x0BAA) ||
+ (c >= 0x0BAE && c <= 0x0BB5) ||
+ (c >= 0x0BB7 && c <= 0x0BB9) ||
+ (c >= 0x0C05 && c <= 0x0C0C) ||
+ (c >= 0x0C0E && c <= 0x0C10) ||
+ (c >= 0x0C12 && c <= 0x0C28) ||
+ (c >= 0x0C2A && c <= 0x0C33) ||
+ (c >= 0x0C35 && c <= 0x0C39) ||
+ (c >= 0x0C60 && c <= 0x0C61) ||
+ (c >= 0x0C85 && c <= 0x0C8C) ||
+ (c >= 0x0C8E && c <= 0x0C90) ||
+ (c >= 0x0C92 && c <= 0x0CA8) ||
+ (c >= 0x0CAA && c <= 0x0CB3) ||
+ (c >= 0x0CB5 && c <= 0x0CB9) ||
+ c == 0x0CDE ||
+ (c >= 0x0CE0 && c <= 0x0CE1) ||
+ (c >= 0x0D05 && c <= 0x0D0C) ||
+ (c >= 0x0D0E && c <= 0x0D10) ||
+ (c >= 0x0D12 && c <= 0x0D28) ||
+ (c >= 0x0D2A && c <= 0x0D39) ||
+ (c >= 0x0D60 && c <= 0x0D61) ||
+ (c >= 0x0E01 && c <= 0x0E2E) ||
+ c == 0x0E30 ||
+ (c >= 0x0E32 && c <= 0x0E33) ||
+ (c >= 0x0E40 && c <= 0x0E45) ||
+ (c >= 0x0E81 && c <= 0x0E82) ||
+ c == 0x0E84 ||
+ (c >= 0x0E87 && c <= 0x0E88) ||
+ c == 0x0E8A ||
+ c == 0x0E8D ||
+ (c >= 0x0E94 && c <= 0x0E97) ||
+ (c >= 0x0E99 && c <= 0x0E9F) ||
+ (c >= 0x0EA1 && c <= 0x0EA3) ||
+ c == 0x0EA5 ||
+ c == 0x0EA7 ||
+ (c >= 0x0EAA && c <= 0x0EAB) ||
+ (c >= 0x0EAD && c <= 0x0EAE) ||
+ c == 0x0EB0 ||
+ (c >= 0x0EB2 && c <= 0x0EB3) ||
+ c == 0x0EBD ||
+ (c >= 0x0EC0 && c <= 0x0EC4) ||
+ (c >= 0x0F40 && c <= 0x0F47) ||
+ (c >= 0x0F49 && c <= 0x0F69) ||
+ (c >= 0x10A0 && c <= 0x10C5) ||
+ (c >= 0x10D0 && c <= 0x10F6) ||
+ c == 0x1100 ||
+ (c >= 0x1102 && c <= 0x1103) ||
+ (c >= 0x1105 && c <= 0x1107) ||
+ c == 0x1109 ||
+ (c >= 0x110B && c <= 0x110C) ||
+ (c >= 0x110E && c <= 0x1112) ||
+ c == 0x113C ||
+ c == 0x113E ||
+ c == 0x1140 ||
+ c == 0x114C ||
+ c == 0x114E ||
+ c == 0x1150 ||
+ (c >= 0x1154 && c <= 0x1155) ||
+ c == 0x1159 ||
+ (c >= 0x115F && c <= 0x1161) ||
+ c == 0x1163 ||
+ c == 0x1165 ||
+ c == 0x1167 ||
+ c == 0x1169 ||
+ (c >= 0x116D && c <= 0x116E) ||
+ (c >= 0x1172 && c <= 0x1173) ||
+ c == 0x1175 ||
+ c == 0x119E ||
+ c == 0x11A8 ||
+ c == 0x11AB ||
+ (c >= 0x11AE && c <= 0x11AF) ||
+ (c >= 0x11B7 && c <= 0x11B8) ||
+ c == 0x11BA ||
+ (c >= 0x11BC && c <= 0x11C2) ||
+ c == 0x11EB ||
+ c == 0x11F0 ||
+ c == 0x11F9 ||
+ (c >= 0x1E00 && c <= 0x1E9B) ||
+ (c >= 0x1EA0 && c <= 0x1EF9) ||
+ (c >= 0x1F00 && c <= 0x1F15) ||
+ (c >= 0x1F18 && c <= 0x1F1D) ||
+ (c >= 0x1F20 && c <= 0x1F45) ||
+ (c >= 0x1F48 && c <= 0x1F4D) ||
+ (c >= 0x1F50 && c <= 0x1F57) ||
+ c == 0x1F59 ||
+ c == 0x1F5B ||
+ c == 0x1F5D ||
+ (c >= 0x1F5F && c <= 0x1F7D) ||
+ (c >= 0x1F80 && c <= 0x1FB4) ||
+ (c >= 0x1FB6 && c <= 0x1FBC) ||
+ c == 0x1FBE ||
+ (c >= 0x1FC2 && c <= 0x1FC4) ||
+ (c >= 0x1FC6 && c <= 0x1FCC) ||
+ (c >= 0x1FD0 && c <= 0x1FD3) ||
+ (c >= 0x1FD6 && c <= 0x1FDB) ||
+ (c >= 0x1FE0 && c <= 0x1FEC) ||
+ (c >= 0x1FF2 && c <= 0x1FF4) ||
+ (c >= 0x1FF6 && c <= 0x1FFC) ||
+ c == 0x2126 ||
+ (c >= 0x212A && c <= 0x212B) ||
+ c == 0x212E ||
+ (c >= 0x2180 && c <= 0x2182) ||
+ (c >= 0x3041 && c <= 0x3094) ||
+ (c >= 0x30A1 && c <= 0x30FA) ||
+ (c >= 0x3105 && c <= 0x312C) ||
+ (c >= 0xAC00 && c <= 0xD7A3))
+ return true; // BaseChar
+ if ((c >= 0x4e00 && c <= 0x9fa5) ||
+ c == 0x3007 ||
+ (c >= 0x3021 && c <= 0x3029))
+ return true; // Ideographic
+ return false;
+ }
+
+ /**
+ * Indicates whether the specified Unicode character matches the Digit
+ * production.
+ */
+ public static boolean isDigit(int c)
+ {
+ return ((c >= 0x0030 && c <= 0x0039) ||
+ (c >= 0x0660 && c <= 0x0669) ||
+ (c >= 0x06F0 && c <= 0x06F9) ||
+ (c >= 0x0966 && c <= 0x096F) ||
+ (c >= 0x09E6 && c <= 0x09EF) ||
+ (c >= 0x0A66 && c <= 0x0A6F) ||
+ (c >= 0x0AE6 && c <= 0x0AEF) ||
+ (c >= 0x0B66 && c <= 0x0B6F) ||
+ (c >= 0x0BE7 && c <= 0x0BEF) ||
+ (c >= 0x0C66 && c <= 0x0C6F) ||
+ (c >= 0x0CE6 && c <= 0x0CEF) ||
+ (c >= 0x0D66 && c <= 0x0D6F) ||
+ (c >= 0x0E50 && c <= 0x0E59) ||
+ (c >= 0x0ED0 && c <= 0x0ED9) ||
+ (c >= 0x0F20 && c <= 0x0F29));
+ }
+
+ /**
+ * Indicates whether the specified Unicode character matches the
+ * CombiningChar production.
+ */
+ public static boolean isCombiningChar(int c)
+ {
+ return ((c >= 0x0300 && c <= 0x0345) ||
+ (c >= 0x0360 && c <= 0x0361) ||
+ (c >= 0x0483 && c <= 0x0486) ||
+ (c >= 0x0591 && c <= 0x05A1) ||
+ (c >= 0x05A3 && c <= 0x05B9) ||
+ (c >= 0x05BB && c <= 0x05BD) ||
+ c == 0x05BF ||
+ (c >= 0x05C1 && c <= 0x05C2) ||
+ c == 0x05C4 ||
+ (c >= 0x064B && c <= 0x0652) ||
+ c == 0x0670 ||
+ (c >= 0x06D6 && c <= 0x06DC) ||
+ (c >= 0x06DD && c <= 0x06DF) ||
+ (c >= 0x06E0 && c <= 0x06E4) ||
+ (c >= 0x06E7 && c <= 0x06E8) ||
+ (c >= 0x06EA && c <= 0x06ED) ||
+ (c >= 0x0901 && c <= 0x0903) ||
+ c == 0x093C ||
+ (c >= 0x093E && c <= 0x094C) ||
+ c == 0x094D ||
+ (c >= 0x0951 && c <= 0x0954) ||
+ (c >= 0x0962 && c <= 0x0963) ||
+ (c >= 0x0981 && c <= 0x0983) ||
+ c == 0x09BC ||
+ c == 0x09BE ||
+ c == 0x09BF ||
+ (c >= 0x09C0 && c <= 0x09C4) ||
+ (c >= 0x09C7 && c <= 0x09C8) ||
+ (c >= 0x09CB && c <= 0x09CD) ||
+ c == 0x09D7 ||
+ (c >= 0x09E2 && c <= 0x09E3) ||
+ c == 0x0A02 ||
+ c == 0x0A3C ||
+ c == 0x0A3E ||
+ c == 0x0A3F ||
+ (c >= 0x0A40 && c <= 0x0A42) ||
+ (c >= 0x0A47 && c <= 0x0A48) ||
+ (c >= 0x0A4B && c <= 0x0A4D) ||
+ (c >= 0x0A70 && c <= 0x0A71) ||
+ (c >= 0x0A81 && c <= 0x0A83) ||
+ c == 0x0ABC ||
+ (c >= 0x0ABE && c <= 0x0AC5) ||
+ (c >= 0x0AC7 && c <= 0x0AC9) ||
+ (c >= 0x0ACB && c <= 0x0ACD) ||
+ (c >= 0x0B01 && c <= 0x0B03) ||
+ c == 0x0B3C ||
+ (c >= 0x0B3E && c <= 0x0B43) ||
+ (c >= 0x0B47 && c <= 0x0B48) ||
+ (c >= 0x0B4B && c <= 0x0B4D) ||
+ (c >= 0x0B56 && c <= 0x0B57) ||
+ (c >= 0x0B82 && c <= 0x0B83) ||
+ (c >= 0x0BBE && c <= 0x0BC2) ||
+ (c >= 0x0BC6 && c <= 0x0BC8) ||
+ (c >= 0x0BCA && c <= 0x0BCD) ||
+ c == 0x0BD7 ||
+ (c >= 0x0C01 && c <= 0x0C03) ||
+ (c >= 0x0C3E && c <= 0x0C44) ||
+ (c >= 0x0C46 && c <= 0x0C48) ||
+ (c >= 0x0C4A && c <= 0x0C4D) ||
+ (c >= 0x0C55 && c <= 0x0C56) ||
+ (c >= 0x0C82 && c <= 0x0C83) ||
+ (c >= 0x0CBE && c <= 0x0CC4) ||
+ (c >= 0x0CC6 && c <= 0x0CC8) ||
+ (c >= 0x0CCA && c <= 0x0CCD) ||
+ (c >= 0x0CD5 && c <= 0x0CD6) ||
+ (c >= 0x0D02 && c <= 0x0D03) ||
+ (c >= 0x0D3E && c <= 0x0D43) ||
+ (c >= 0x0D46 && c <= 0x0D48) ||
+ (c >= 0x0D4A && c <= 0x0D4D) ||
+ c == 0x0D57 ||
+ c == 0x0E31 ||
+ (c >= 0x0E34 && c <= 0x0E3A) ||
+ (c >= 0x0E47 && c <= 0x0E4E) ||
+ c == 0x0EB1 ||
+ (c >= 0x0EB4 && c <= 0x0EB9) ||
+ (c >= 0x0EBB && c <= 0x0EBC) ||
+ (c >= 0x0EC8 && c <= 0x0ECD) ||
+ (c >= 0x0F18 && c <= 0x0F19) ||
+ c == 0x0F35 ||
+ c == 0x0F37 ||
+ c == 0x0F39 ||
+ c == 0x0F3E ||
+ c == 0x0F3F ||
+ (c >= 0x0F71 && c <= 0x0F84) ||
+ (c >= 0x0F86 && c <= 0x0F8B) ||
+ (c >= 0x0F90 && c <= 0x0F95) ||
+ c == 0x0F97 ||
+ (c >= 0x0F99 && c <= 0x0FAD) ||
+ (c >= 0x0FB1 && c <= 0x0FB7) ||
+ c == 0x0FB9 ||
+ (c >= 0x20D0 && c <= 0x20DC) ||
+ c == 0x20E1 ||
+ (c >= 0x302A && c <= 0x302F) ||
+ c == 0x3099 ||
+ c == 0x309A);
+ }
+
+ /**
+ * Indicates whether the specified Unicode character matches the Extender
+ * production.
+ */
+ public static boolean isExtender(int c)
+ {
+ return (c == 0x00B7 ||
+ c == 0x02D0 ||
+ c == 0x02D1 ||
+ c == 0x0387 ||
+ c == 0x0640 ||
+ c == 0x0E46 ||
+ c == 0x0EC6 ||
+ c == 0x3005 ||
+ (c >= 0x3031 && c <= 0x3035) ||
+ (c >= 0x309D && c <= 0x309E) ||
+ (c >= 0x30FC && c <= 0x30FE));
+ }
+
+ /**
+ * Indicates whether the specified Unicode character matches the Char
+ * production.
+ */
+ public static boolean isChar(int c)
+ {
+ return (c >= 0x20 && c < 0xd800) ||
+ (c >= 0xe00 && c < 0xfffe) ||
+ (c >= 0x10000 && c < 0x110000) ||
+ c == 0xa || c == 0x9 || c == 0xd;
+ }
+
+ /**
+ * Interns the specified text or not, depending on the value of
+ * stringInterning.
+ */
+ private String intern(String text)
+ {
+ return stringInterning ? text.intern() : text;
+ }
+
+ /**
+ * Report a parsing error.
+ */
+ private void error(String message)
+ throws XMLStreamException
+ {
+ error(message, null);
+ }
+
+ /**
+ * Report a parsing error.
+ */
+ private void error(String message, Object info)
+ throws XMLStreamException
+ {
+ if (info != null)
+ {
+ if (info instanceof String)
+ message += ": \"" + ((String) info) + "\"";
+ else if (info instanceof Character)
+ message += ": '" + ((Character) info) + "'";
+ }
+ throw new XMLStreamException(message);
+ }
+
+ /**
+ * Perform validation of a start-element event.
+ */
+ private void validateStartElement(String elementName)
+ throws XMLStreamException
+ {
+ if (currentContentModel == null)
+ {
+ // root element
+ // VC: Root Element Type
+ if (!elementName.equals(doctype.rootName))
+ error("root element name must match name in DTD");
+ return;
+ }
+ // VC: Element Valid
+ switch (currentContentModel.type)
+ {
+ case ContentModel.EMPTY:
+ error("child element found in empty element", elementName);
+ break;
+ case ContentModel.ELEMENT:
+ LinkedList ctx = (LinkedList) validationStack.getLast();
+ ctx.add(elementName);
+ break;
+ case ContentModel.MIXED:
+ MixedContentModel mm = (MixedContentModel) currentContentModel;
+ if (!mm.containsName(elementName))
+ error("illegal element for content model", elementName);
+ break;
+ }
+ }
+
+ /**
+ * Perform validation of an end-element event.
+ */
+ private void validateEndElement()
+ throws XMLStreamException
+ {
+ if (currentContentModel == null)
+ {
+ // root element
+ // VC: IDREF
+ if (!idrefs.containsAll(ids))
+ error("IDREF values must match the value of some ID attribute");
+ return;
+ }
+ // VC: Element Valid
+ switch (currentContentModel.type)
+ {
+ case ContentModel.ELEMENT:
+ LinkedList ctx = (LinkedList) validationStack.getLast();
+ ElementContentModel ecm = (ElementContentModel) currentContentModel;
+ validateElementContent(ecm, ctx);
+ break;
+ }
+ }
+
+ /**
+ * Perform validation of character data.
+ */
+ private void validatePCData(String text)
+ throws XMLStreamException
+ {
+ // VC: Element Valid
+ switch (currentContentModel.type)
+ {
+ case ContentModel.EMPTY:
+ error("character data found in empty element", text);
+ break;
+ case ContentModel.ELEMENT:
+ boolean white = true;
+ int len = text.length();
+ for (int i = 0; i < len; i++)
+ {
+ char c = text.charAt(i);
+ if (c != ' ' && c != '\t' && c != '\n' && c != '\r')
+ {
+ white = false;
+ break;
+ }
+ }
+ if (!white)
+ error("character data found in element with element content", text);
+ else if (xmlStandalone == Boolean.TRUE && currentContentModel.external)
+ // VC: Standalone Document Declaration
+ error("whitespace in element content of externally declared " +
+ "element in standalone document");
+ break;
+ }
+ }
+
+ /**
+ * Validates the specified validation context (list of child elements)
+ * against the element content model for the current element.
+ */
+ private void validateElementContent(ElementContentModel model,
+ LinkedList children)
+ throws XMLStreamException
+ {
+ // Use regular expression
+ CPStringBuilder buf = new CPStringBuilder();
+ for (Iterator i = children.iterator(); i.hasNext(); )
+ {
+ buf.append((String) i.next());
+ buf.append(' ');
+ }
+ String c = buf.toString();
+ String regex = createRegularExpression(model);
+ if (!c.matches(regex))
+ error("element content "+model.text+" does not match expression "+regex, c);
+ }
+
+ /**
+ * Creates the regular expression used to validate an element content
+ * model.
+ */
+ private String createRegularExpression(ElementContentModel model)
+ {
+ if (model.regex == null)
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ buf.append('(');
+ for (Iterator i = model.contentParticles.iterator(); i.hasNext(); )
+ {
+ ContentParticle cp = (ContentParticle) i.next();
+ if (cp.content instanceof String)
+ {
+ buf.append('(');
+ buf.append((String) cp.content);
+ buf.append(' ');
+ buf.append(')');
+ if (cp.max == -1)
+ {
+ if (cp.min == 0)
+ buf.append('*');
+ else
+ buf.append('+');
+ }
+ else if (cp.min == 0)
+ buf.append('?');
+ }
+ else
+ {
+ ElementContentModel ecm = (ElementContentModel) cp.content;
+ buf.append(createRegularExpression(ecm));
+ }
+ if (model.or && i.hasNext())
+ buf.append('|');
+ }
+ buf.append(')');
+ if (model.max == -1)
+ {
+ if (model.min == 0)
+ buf.append('*');
+ else
+ buf.append('+');
+ }
+ else if (model.min == 0)
+ buf.append('?');
+ model.regex = buf.toString();
+ }
+ return model.regex;
+ }
+
+ /**
+ * Performs validation of a document type declaration event.
+ */
+ void validateDoctype()
+ throws XMLStreamException
+ {
+ for (Iterator i = doctype.entityIterator(); i.hasNext(); )
+ {
+ Map.Entry entry = (Map.Entry) i.next();
+ Object entity = entry.getValue();
+ if (entity instanceof ExternalIds)
+ {
+ ExternalIds ids = (ExternalIds) entity;
+ if (ids.notationName != null)
+ {
+ // VC: Notation Declared
+ ExternalIds notation = doctype.getNotation(ids.notationName);
+ if (notation == null)
+ error("Notation name must match the declared name of a " +
+ "notation", ids.notationName);
+ }
+ }
+ }
+ }
+
+ /**
+ * Simple test harness for reading an XML file.
+ * args[0] is the filename of the XML file
+ * If args[1] is "-x", enable XInclude processing
+ */
+ public static void main(String[] args)
+ throws Exception
+ {
+ boolean validating = false;
+ boolean namespaceAware = false;
+ boolean xIncludeAware = false;
+ int pos = 0;
+ while (pos < args.length && args[pos].startsWith("-"))
+ {
+ if ("-x".equals(args[pos]))
+ xIncludeAware = true;
+ else if ("-v".equals(args[pos]))
+ validating = true;
+ else if ("-n".equals(args[pos]))
+ namespaceAware = true;
+ pos++;
+ }
+ if (pos >= args.length)
+ {
+ System.out.println("Syntax: XMLParser [-n] [-v] [-x] <file> [<file2> [...]]");
+ System.out.println("\t-n: use namespace aware mode");
+ System.out.println("\t-v: use validating parser");
+ System.out.println("\t-x: use XInclude aware mode");
+ System.exit(2);
+ }
+ while (pos < args.length)
+ {
+ XMLParser p = new XMLParser(new java.io.FileInputStream(args[pos]),
+ absolutize(null, args[pos]),
+ validating, // validating
+ namespaceAware, // namespaceAware
+ true, // coalescing,
+ true, // replaceERefs
+ true, // externalEntities
+ true, // supportDTD
+ true, // baseAware
+ true, // stringInterning
+ true, // extendedEventTypes
+ null,
+ null);
+ XMLStreamReader reader = p;
+ if (xIncludeAware)
+ reader = new XIncludeFilter(p, args[pos], true, true, true);
+ try
+ {
+ int event;
+ //do
+ while (reader.hasNext())
+ {
+ event = reader.next();
+ Location loc = reader.getLocation();
+ System.out.print(loc.getLineNumber() + ":" +
+ loc.getColumnNumber() + " ");
+ switch (event)
+ {
+ case XMLStreamConstants.START_DOCUMENT:
+ System.out.println("START_DOCUMENT version=" +
+ reader.getVersion() +
+ " encoding=" +
+ reader.getEncoding());
+ break;
+ case XMLStreamConstants.END_DOCUMENT:
+ System.out.println("END_DOCUMENT");
+ break;
+ case XMLStreamConstants.START_ELEMENT:
+ System.out.println("START_ELEMENT " +
+ reader.getName());
+ int l = reader.getNamespaceCount();
+ for (int i = 0; i < l; i++)
+ System.out.println("\tnamespace " +
+ reader.getNamespacePrefix(i) + "='" +
+ reader.getNamespaceURI(i)+"'");
+ l = reader.getAttributeCount();
+ for (int i = 0; i < l; i++)
+ System.out.println("\tattribute " +
+ reader.getAttributeName(i) + "='" +
+ reader.getAttributeValue(i) + "'");
+ break;
+ case XMLStreamConstants.END_ELEMENT:
+ System.out.println("END_ELEMENT " + reader.getName());
+ break;
+ case XMLStreamConstants.CHARACTERS:
+ System.out.println("CHARACTERS '" +
+ encodeText(reader.getText()) + "'");
+ break;
+ case XMLStreamConstants.CDATA:
+ System.out.println("CDATA '" +
+ encodeText(reader.getText()) + "'");
+ break;
+ case XMLStreamConstants.SPACE:
+ System.out.println("SPACE '" +
+ encodeText(reader.getText()) + "'");
+ break;
+ case XMLStreamConstants.DTD:
+ System.out.println("DTD " + reader.getText());
+ break;
+ case XMLStreamConstants.ENTITY_REFERENCE:
+ System.out.println("ENTITY_REFERENCE " + reader.getText());
+ break;
+ case XMLStreamConstants.COMMENT:
+ System.out.println("COMMENT '" +
+ encodeText(reader.getText()) + "'");
+ break;
+ case XMLStreamConstants.PROCESSING_INSTRUCTION:
+ System.out.println("PROCESSING_INSTRUCTION " +
+ reader.getPITarget() + " " +
+ reader.getPIData());
+ break;
+ case START_ENTITY:
+ System.out.println("START_ENTITY " + reader.getText());
+ break;
+ case END_ENTITY:
+ System.out.println("END_ENTITY " + reader.getText());
+ break;
+ default:
+ System.out.println("Unknown event: " + event);
+ }
+ }
+ }
+ catch (XMLStreamException e)
+ {
+ Location l = reader.getLocation();
+ System.out.println("At line "+l.getLineNumber()+
+ ", column "+l.getColumnNumber()+
+ " of "+l.getSystemId());
+ throw e;
+ }
+ pos++;
+ }
+ }
+
+ /**
+ * Escapes control characters in the specified text. For debugging.
+ */
+ private static String encodeText(String text)
+ {
+ CPStringBuilder b = new CPStringBuilder();
+ int len = text.length();
+ for (int i = 0; i < len; i++)
+ {
+ char c = text.charAt(i);
+ switch (c)
+ {
+ case '\t':
+ b.append("\\t");
+ break;
+ case '\n':
+ b.append("\\n");
+ break;
+ case '\r':
+ b.append("\\r");
+ break;
+ default:
+ b.append(c);
+ }
+ }
+ return b.toString();
+ }
+
+ /**
+ * An attribute instance.
+ */
+ class Attribute
+ {
+
+ /**
+ * Attribute name.
+ */
+ final String name;
+
+ /**
+ * Attribute type as declared in the DTD, or CDATA otherwise.
+ */
+ final String type;
+
+ /**
+ * Whether the attribute was specified or defaulted.
+ */
+ final boolean specified;
+
+ /**
+ * The attribute value.
+ */
+ final String value;
+
+ /**
+ * The namespace prefix.
+ */
+ final String prefix;
+
+ /**
+ * The namespace local-name.
+ */
+ final String localName;
+
+ Attribute(String name, String type, boolean specified, String value)
+ {
+ this.name = name;
+ this.type = type;
+ this.specified = specified;
+ this.value = value;
+ int ci = name.indexOf(':');
+ if (ci == -1)
+ {
+ prefix = null;
+ localName = intern(name);
+ }
+ else
+ {
+ prefix = intern(name.substring(0, ci));
+ localName = intern(name.substring(ci + 1));
+ }
+ }
+
+ public boolean equals(Object other)
+ {
+ if (other instanceof Attribute)
+ {
+ Attribute a = (Attribute) other;
+ if (namespaceAware)
+ {
+ if (!a.localName.equals(localName))
+ return false;
+ String auri = getNamespaceURI(a.prefix);
+ String uri = getNamespaceURI(prefix);
+ if (uri == null && (auri == null ||
+ (input.xml11 && "".equals(auri))))
+ return true;
+ if (uri != null)
+ {
+ if ("".equals(uri) && input.xml11 && "".equals(auri))
+ return true;
+ return uri.equals(auri);
+ }
+ return false;
+ }
+ else
+ return a.name.equals(name);
+ }
+ return false;
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder(getClass().getName());
+ buf.append('[');
+ buf.append("name=");
+ buf.append(name);
+ if (value != null)
+ {
+ buf.append(",value=");
+ buf.append(value);
+ }
+ if (type != null)
+ {
+ buf.append(",type=");
+ buf.append(type);
+ }
+ if (specified)
+ buf.append(",specified");
+ buf.append(']');
+ return buf.toString();
+ }
+
+ }
+
+ /**
+ * Representation of a DTD.
+ */
+ class Doctype
+ {
+
+ /**
+ * Name of the root element.
+ */
+ final String rootName;
+
+ /**
+ * Public ID, if any, of external subset.
+ */
+ final String publicId;
+
+ /**
+ * System ID (URL), if any, of external subset.
+ */
+ final String systemId;
+
+ /**
+ * Map of element names to content models.
+ */
+ private final LinkedHashMap elements = new LinkedHashMap();
+
+ /**
+ * Map of element names to maps of attribute declarations.
+ */
+ private final LinkedHashMap attlists = new LinkedHashMap();
+
+ /**
+ * Map of entity names to entities (String or ExternalIds).
+ */
+ private final LinkedHashMap entities = new LinkedHashMap();
+
+ /**
+ * Map of notation names to ExternalIds.
+ */
+ private final LinkedHashMap notations = new LinkedHashMap();
+
+ /**
+ * Map of anonymous keys to comments.
+ */
+ private final LinkedHashMap comments = new LinkedHashMap();
+
+ /**
+ * Map of anonymous keys to processing instructions (String[2]
+ * containing {target, data}).
+ */
+ private final LinkedHashMap pis = new LinkedHashMap();
+
+ /**
+ * List of keys to all markup entries in the DTD.
+ */
+ private final LinkedList entries = new LinkedList();
+
+ /**
+ * Set of the entities defined in the external subset.
+ */
+ private final HashSet externalEntities = new HashSet();
+
+ /**
+ * Set of the notations defined in the external subset.
+ */
+ private final HashSet externalNotations = new HashSet();
+
+ /**
+ * Counter for making anonymous keys.
+ */
+ private int anon = 1;
+
+ /**
+ * Constructor.
+ */
+ Doctype(String rootName, String publicId, String systemId)
+ {
+ this.rootName = rootName;
+ this.publicId = publicId;
+ this.systemId = systemId;
+ }
+
+ /**
+ * Adds an element declaration.
+ * @param name the element name
+ * @param text the content model text
+ * @param model the parsed content model
+ */
+ void addElementDecl(String name, String text, ContentModel model)
+ {
+ if (elements.containsKey(name))
+ return;
+ model.text = text;
+ model.external = (inputStack.size() != 1);
+ elements.put(name, model);
+ entries.add("E" + name);
+ }
+
+ /**
+ * Adds an attribute declaration.
+ * @param ename the element name
+ * @param aname the attribute name
+ * @param decl the attribute declaration details
+ */
+ void addAttributeDecl(String ename, String aname, AttributeDecl decl)
+ {
+ LinkedHashMap attlist = (LinkedHashMap) attlists.get(ename);
+ if (attlist == null)
+ {
+ attlist = new LinkedHashMap();
+ attlists.put(ename, attlist);
+ }
+ else if (attlist.containsKey(aname))
+ return;
+ attlist.put(aname, decl);
+ String key = "A" + ename;
+ if (!entries.contains(key))
+ entries.add(key);
+ }
+
+ /**
+ * Adds an entity declaration.
+ * @param name the entity name
+ * @param text the entity replacement text
+ * @param inExternalSubset if we are in the exernal subset
+ */
+ void addEntityDecl(String name, String text, boolean inExternalSubset)
+ {
+ if (entities.containsKey(name))
+ return;
+ entities.put(name, text);
+ entries.add("e" + name);
+ if (inExternalSubset)
+ externalEntities.add(name);
+ }
+
+ /**
+ * Adds an entity declaration.
+ * @param name the entity name
+ * @param ids the external IDs
+ * @param inExternalSubset if we are in the exernal subset
+ */
+ void addEntityDecl(String name, ExternalIds ids, boolean inExternalSubset)
+ {
+ if (entities.containsKey(name))
+ return;
+ entities.put(name, ids);
+ entries.add("e" + name);
+ if (inExternalSubset)
+ externalEntities.add(name);
+ }
+
+ /**
+ * Adds a notation declaration.
+ * @param name the notation name
+ * @param ids the external IDs
+ * @param inExternalSubset if we are in the exernal subset
+ */
+ void addNotationDecl(String name, ExternalIds ids, boolean inExternalSubset)
+ {
+ if (notations.containsKey(name))
+ return;
+ notations.put(name, ids);
+ entries.add("n" + name);
+ if (inExternalSubset)
+ externalNotations.add(name);
+ }
+
+ /**
+ * Adds a comment.
+ */
+ void addComment(String text)
+ {
+ String key = Integer.toString(anon++);
+ comments.put(key, text);
+ entries.add("c" + key);
+ }
+
+ /**
+ * Adds a processing instruction.
+ */
+ void addPI(String target, String data)
+ {
+ String key = Integer.toString(anon++);
+ pis.put(key, new String[] {target, data});
+ entries.add("p" + key);
+ }
+
+ /**
+ * Returns the content model for the specified element.
+ * @param name the element name
+ */
+ ContentModel getElementModel(String name)
+ {
+ return (ContentModel) elements.get(name);
+ }
+
+ /**
+ * Returns the attribute definition for the given attribute
+ * @param ename the element name
+ * @param aname the attribute name
+ */
+ AttributeDecl getAttributeDecl(String ename, String aname)
+ {
+ LinkedHashMap attlist = (LinkedHashMap) attlists.get(ename);
+ return (attlist == null) ? null : (AttributeDecl) attlist.get(aname);
+ }
+
+ /**
+ * Indicates whether the specified attribute was declared in the DTD.
+ * @param ename the element name
+ * @param aname the attribute name
+ */
+ boolean isAttributeDeclared(String ename, String aname)
+ {
+ LinkedHashMap attlist = (LinkedHashMap) attlists.get(ename);
+ return (attlist == null) ? false : attlist.containsKey(aname);
+ }
+
+ /**
+ * Returns an iterator over the entries in the attribute list for the
+ * given element.
+ * @param ename the element name
+ */
+ Iterator attlistIterator(String ename)
+ {
+ LinkedHashMap attlist = (LinkedHashMap) attlists.get(ename);
+ return (attlist == null) ? Collections.EMPTY_LIST.iterator() :
+ attlist.entrySet().iterator();
+ }
+
+ /**
+ * Returns the entity (String or ExternalIds) for the given entity name.
+ */
+ Object getEntity(String name)
+ {
+ return entities.get(name);
+ }
+
+ /**
+ * Indicates whether the specified entity was declared in the external
+ * subset.
+ */
+ boolean isEntityExternal(String name)
+ {
+ return externalEntities.contains(name);
+ }
+
+ /**
+ * Returns an iterator over the entity map entries.
+ */
+ Iterator entityIterator()
+ {
+ return entities.entrySet().iterator();
+ }
+
+ /**
+ * Returns the notation IDs for the given notation name.
+ */
+ ExternalIds getNotation(String name)
+ {
+ return (ExternalIds) notations.get(name);
+ }
+
+ /**
+ * Indicates whether the specified notation was declared in the external
+ * subset.
+ */
+ boolean isNotationExternal(String name)
+ {
+ return externalNotations.contains(name);
+ }
+
+ /**
+ * Returns the comment associated with the specified (anonymous) key.
+ */
+ String getComment(String key)
+ {
+ return (String) comments.get(key);
+ }
+
+ /**
+ * Returns the processing instruction associated with the specified
+ * (anonymous) key.
+ */
+ String[] getPI(String key)
+ {
+ return (String[]) pis.get(key);
+ }
+
+ /**
+ * Returns an iterator over the keys of the markup entries in this DTD,
+ * in the order declared.
+ */
+ Iterator entryIterator()
+ {
+ return entries.iterator();
+ }
+
+ }
+
+ /**
+ * Combination of an ExternalID and an optional NDataDecl.
+ */
+ class ExternalIds
+ {
+
+ /**
+ * The public ID.
+ */
+ String publicId;
+
+ /**
+ * The system ID.
+ */
+ String systemId;
+
+ /**
+ * The notation name declared with the NDATA keyword.
+ */
+ String notationName;
+ }
+
+ /**
+ * A content model.
+ */
+ abstract class ContentModel
+ {
+ static final int EMPTY = 0;
+ static final int ANY = 1;
+ static final int ELEMENT = 2;
+ static final int MIXED = 3;
+
+ int min;
+ int max;
+ final int type;
+ String text;
+ boolean external;
+
+ ContentModel(int type)
+ {
+ this.type = type;
+ min = 1;
+ max = 1;
+ }
+
+ }
+
+ /**
+ * The EMPTY content model.
+ */
+ class EmptyContentModel
+ extends ContentModel
+ {
+
+ EmptyContentModel()
+ {
+ super(ContentModel.EMPTY);
+ min = 0;
+ max = 0;
+ }
+
+ }
+
+ /**
+ * The ANY content model.
+ */
+ class AnyContentModel
+ extends ContentModel
+ {
+
+ AnyContentModel()
+ {
+ super(ContentModel.ANY);
+ min = 0;
+ max = -1;
+ }
+
+ }
+
+ /**
+ * An element content model.
+ */
+ class ElementContentModel
+ extends ContentModel
+ {
+
+ LinkedList contentParticles;
+ boolean or;
+ String regex; // regular expression cache
+
+ ElementContentModel()
+ {
+ super(ContentModel.ELEMENT);
+ contentParticles = new LinkedList();
+ }
+
+ void addContentParticle(ContentParticle cp)
+ {
+ contentParticles.add(cp);
+ }
+
+ }
+
+ class ContentParticle
+ {
+
+ int min = 1;
+ int max = 1;
+ Object content; // Name (String) or ElementContentModel
+
+ }
+
+ /**
+ * A mixed content model.
+ */
+ class MixedContentModel
+ extends ContentModel
+ {
+
+ private HashSet names;
+
+ MixedContentModel()
+ {
+ super(ContentModel.MIXED);
+ names = new HashSet();
+ }
+
+ void addName(String name)
+ {
+ names.add(name);
+ }
+
+ boolean containsName(String name)
+ {
+ return names.contains(name);
+ }
+
+ }
+
+ /**
+ * An attribute definition.
+ */
+ class AttributeDecl
+ {
+
+ /**
+ * The attribute type (CDATA, ID, etc).
+ */
+ final String type;
+
+ /**
+ * The default value.
+ */
+ final String value;
+
+ /**
+ * The value type (#FIXED, #IMPLIED, etc).
+ */
+ final int valueType;
+
+ /**
+ * The enumeration text.
+ */
+ final String enumeration;
+
+ /**
+ * The enumeration tokens.
+ */
+ final HashSet values;
+
+ /**
+ * Whether this attribute declaration occurred in the external subset.
+ */
+ final boolean external;
+
+ AttributeDecl(String type, String value,
+ int valueType, String enumeration,
+ HashSet values, boolean external)
+ {
+ this.type = type;
+ this.value = value;
+ this.valueType = valueType;
+ this.enumeration = enumeration;
+ this.values = values;
+ this.external = external;
+ }
+
+ }
+
+ /**
+ * An XML input source.
+ */
+ static class Input
+ implements Location
+ {
+
+ int line = 1, markLine;
+ int column, markColumn;
+ int offset, markOffset;
+ final String publicId, systemId, name;
+ final boolean report; // report start- and end-entity
+ final boolean normalize; // normalize CR, etc to LF
+
+ InputStream in;
+ Reader reader;
+ UnicodeReader unicodeReader;
+ boolean initialized;
+ boolean encodingDetected;
+ String inputEncoding;
+ boolean xml11;
+
+ Input(InputStream in, Reader reader, String publicId, String systemId,
+ String name, String inputEncoding, boolean report,
+ boolean normalize)
+ {
+ if (inputEncoding == null)
+ inputEncoding = "UTF-8";
+ this.inputEncoding = inputEncoding;
+ this.publicId = publicId;
+ this.systemId = systemId;
+ this.name = name;
+ this.report = report;
+ this.normalize = normalize;
+ if (in != null)
+ {
+ if (reader != null)
+ throw new IllegalStateException("both byte and char streams "+
+ "specified");
+ if (normalize)
+ in = new CRLFInputStream(in);
+ in = new BufferedInputStream(in);
+ this.in = in;
+ }
+ else
+ {
+ this.reader = normalize ? new CRLFReader(reader) : reader;
+ unicodeReader = new UnicodeReader(this.reader);
+ }
+ initialized = false;
+ }
+
+ // -- Location --
+
+ public int getCharacterOffset()
+ {
+ return offset;
+ }
+
+ public int getColumnNumber()
+ {
+ return column;
+ }
+
+ public int getLineNumber()
+ {
+ return line;
+ }
+
+ public String getPublicId()
+ {
+ return publicId;
+ }
+
+ public String getSystemId()
+ {
+ return systemId;
+ }
+
+ void init()
+ throws IOException
+ {
+ if (initialized)
+ return;
+ if (in != null)
+ detectEncoding();
+ initialized = true;
+ }
+
+ void mark(int len)
+ throws IOException
+ {
+ markOffset = offset;
+ markLine = line;
+ markColumn = column;
+ if (unicodeReader != null)
+ unicodeReader.mark(len);
+ else
+ in.mark(len);
+ }
+
+ /**
+ * Character read.
+ */
+ int read()
+ throws IOException
+ {
+ offset++;
+ int ret = (unicodeReader != null) ? unicodeReader.read() : in.read();
+ if (normalize &&
+ (ret == 0x0d || (xml11 && (ret == 0x85 || ret == 0x2028))))
+ {
+ // Normalize CR etc to LF
+ ret = 0x0a;
+ }
+ // Locator handling
+ if (ret == 0x0a)
+ {
+ line++;
+ column = 0;
+ }
+ else
+ column++;
+ return ret;
+ }
+
+ /**
+ * Block read.
+ */
+ int read(int[] b, int off, int len)
+ throws IOException
+ {
+ int ret;
+ if (unicodeReader != null)
+ {
+ ret = unicodeReader.read(b, off, len);
+ }
+ else
+ {
+ byte[] b2 = new byte[len];
+ ret = in.read(b2, 0, len);
+ if (ret != -1)
+ {
+ String s = new String(b2, 0, ret, inputEncoding);
+ int[] c = UnicodeReader.toCodePointArray(s);
+ ret = c.length;
+ System.arraycopy(c, 0, b, off, ret);
+ }
+ }
+ if (ret != -1)
+ {
+ // Locator handling
+ for (int i = 0; i < ret; i++)
+ {
+ int c = b[off + i];
+ if (normalize &&
+ (c == 0x0d || (xml11 && (c == 0x85 || c == 0x2028))))
+ {
+ // Normalize CR etc to LF
+ c = 0x0a;
+ b[off + i] = c;
+ }
+ if (c == 0x0a)
+ {
+ line++;
+ column = 0;
+ }
+ else
+ column++;
+ }
+ }
+ return ret;
+ }
+
+ void reset()
+ throws IOException
+ {
+ if (unicodeReader != null)
+ unicodeReader.reset();
+ else
+ in.reset();
+ offset = markOffset;
+ line = markLine;
+ column = markColumn;
+ }
+
+ // Detection of input encoding
+
+ private static final int[] SIGNATURE_UCS_4_1234 =
+ new int[] { 0x00, 0x00, 0x00, 0x3c };
+ private static final int[] SIGNATURE_UCS_4_4321 =
+ new int[] { 0x3c, 0x00, 0x00, 0x00 };
+ private static final int[] SIGNATURE_UCS_4_2143 =
+ new int[] { 0x00, 0x00, 0x3c, 0x00 };
+ private static final int[] SIGNATURE_UCS_4_3412 =
+ new int[] { 0x00, 0x3c, 0x00, 0x00 };
+ private static final int[] SIGNATURE_UCS_2_12 =
+ new int[] { 0xfe, 0xff };
+ private static final int[] SIGNATURE_UCS_2_21 =
+ new int[] { 0xff, 0xfe };
+ private static final int[] SIGNATURE_UCS_2_12_NOBOM =
+ new int[] { 0x00, 0x3c, 0x00, 0x3f };
+ private static final int[] SIGNATURE_UCS_2_21_NOBOM =
+ new int[] { 0x3c, 0x00, 0x3f, 0x00 };
+ private static final int[] SIGNATURE_UTF_8 =
+ new int[] { 0x3c, 0x3f, 0x78, 0x6d };
+ private static final int[] SIGNATURE_UTF_8_BOM =
+ new int[] { 0xef, 0xbb, 0xbf };
+
+ /**
+ * Detect the input encoding.
+ */
+ private void detectEncoding()
+ throws IOException
+ {
+ int[] signature = new int[4];
+ in.mark(4);
+ for (int i = 0; i < 4; i++)
+ signature[i] = in.read();
+ in.reset();
+
+ // 4-byte encodings
+ if (equals(SIGNATURE_UCS_4_1234, signature))
+ {
+ in.read();
+ in.read();
+ in.read();
+ in.read();
+ setInputEncoding("UTF-32BE");
+ encodingDetected = true;
+ }
+ else if (equals(SIGNATURE_UCS_4_4321, signature))
+ {
+ in.read();
+ in.read();
+ in.read();
+ in.read();
+ setInputEncoding("UTF-32LE");
+ encodingDetected = true;
+ }
+ else if (equals(SIGNATURE_UCS_4_2143, signature) ||
+ equals(SIGNATURE_UCS_4_3412, signature))
+ throw new UnsupportedEncodingException("unsupported UCS-4 byte ordering");
+
+ // 2-byte encodings
+ else if (equals(SIGNATURE_UCS_2_12, signature))
+ {
+ in.read();
+ in.read();
+ setInputEncoding("UTF-16BE");
+ encodingDetected = true;
+ }
+ else if (equals(SIGNATURE_UCS_2_21, signature))
+ {
+ in.read();
+ in.read();
+ setInputEncoding("UTF-16LE");
+ encodingDetected = true;
+ }
+ else if (equals(SIGNATURE_UCS_2_12_NOBOM, signature))
+ {
+ //setInputEncoding("UTF-16BE");
+ throw new UnsupportedEncodingException("no byte-order mark for UCS-2 entity");
+ }
+ else if (equals(SIGNATURE_UCS_2_21_NOBOM, signature))
+ {
+ //setInputEncoding("UTF-16LE");
+ throw new UnsupportedEncodingException("no byte-order mark for UCS-2 entity");
+ }
+ // ASCII-derived encodings
+ else if (equals(SIGNATURE_UTF_8, signature))
+ {
+ // UTF-8 input encoding implied, TextDecl
+ }
+ else if (equals(SIGNATURE_UTF_8_BOM, signature))
+ {
+ in.read();
+ in.read();
+ in.read();
+ setInputEncoding("UTF-8");
+ encodingDetected = true;
+ }
+ }
+
+ private static boolean equals(int[] b1, int[] b2)
+ {
+ for (int i = 0; i < b1.length; i++)
+ {
+ if (b1[i] != b2[i])
+ return false;
+ }
+ return true;
+ }
+
+ void setInputEncoding(String encoding)
+ throws IOException
+ {
+ if (encoding.equals(inputEncoding))
+ return;
+ if ("UTF-16".equalsIgnoreCase(encoding) &&
+ inputEncoding.startsWith("UTF-16"))
+ return;
+ if (encodingDetected)
+ throw new UnsupportedEncodingException("document is not in its " +
+ "declared encoding " +
+ inputEncoding +
+ ": " + encoding);
+ inputEncoding = encoding;
+ finalizeEncoding();
+ }
+
+ void finalizeEncoding()
+ throws IOException
+ {
+ if (reader != null)
+ return;
+ reader = new BufferedReader(new InputStreamReader(in, inputEncoding));
+ unicodeReader = new UnicodeReader(reader);
+ mark(1);
+ }
+
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/XMLStreamWriterImpl.java b/libjava/classpath/gnu/xml/stream/XMLStreamWriterImpl.java
new file mode 100644
index 000000000..83b8f7239
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/XMLStreamWriterImpl.java
@@ -0,0 +1,1016 @@
+/* XMLStreamWriterImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.Set;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.xml.sax.helpers.NamespaceSupport;
+
+/**
+ * Simple XML stream writer.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class XMLStreamWriterImpl
+ implements XMLStreamWriter
+{
+
+ /**
+ * The underlying character stream to write to.
+ */
+ protected final Writer writer;
+
+ /**
+ * The encoding being used.
+ * Note that this must match the encoding of the character stream.
+ */
+ protected final String encoding;
+
+ /**
+ * Whether prefix defaulting is being used.
+ * If true and a prefix has not been defined for a namespace specified on
+ * an element or an attribute, a new prefix and namespace declaration will
+ * be created.
+ */
+ protected final boolean prefixDefaulting;
+
+ /**
+ * The namespace context used to determine the namespace-prefix mappings
+ * in scope.
+ */
+ protected NamespaceContext namespaceContext;
+
+ /**
+ * The stack of elements in scope.
+ * Used to close the remaining elements.
+ */
+ private LinkedList elements;
+
+ /**
+ * Whether a start element has been opened but not yet closed.
+ */
+ private boolean inStartElement;
+
+ /**
+ * Whether we are in an empty element.
+ */
+ private boolean emptyElement;
+
+ private NamespaceSupport namespaces;
+ private int count = 0;
+
+ private boolean xml11;
+ private boolean hasXML11RestrictedChars;
+
+ /**
+ * Constructor.
+ * @see #writer
+ * @see #encoding
+ * @see #prefixDefaulting
+ */
+ protected XMLStreamWriterImpl(Writer writer, String encoding,
+ boolean prefixDefaulting)
+ {
+ this.writer = writer;
+ this.encoding = encoding;
+ this.prefixDefaulting = prefixDefaulting;
+ elements = new LinkedList();
+ namespaces = new NamespaceSupport();
+ }
+
+ /**
+ * Write the end of a start-element event.
+ * This will close the element if it was defined to be an empty element.
+ */
+ private void endStartElement()
+ throws IOException
+ {
+ if (!inStartElement)
+ return;
+ if (emptyElement)
+ {
+ writer.write('/');
+ elements.removeLast();
+ namespaces.popContext();
+ emptyElement = false;
+ }
+ writer.write('>');
+ inStartElement = false;
+ }
+
+ public void writeStartElement(String localName)
+ throws XMLStreamException
+ {
+ try
+ {
+ if (!isName(localName))
+ throw new IllegalArgumentException("illegal Name: " + localName);
+
+ endStartElement();
+ namespaces.pushContext();
+
+ writer.write('<');
+ writer.write(localName);
+
+ elements.addLast(new String[] { null, localName });
+ inStartElement = true;
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeStartElement(String namespaceURI, String localName)
+ throws XMLStreamException
+ {
+ try
+ {
+ if (namespaceURI != null && !isURI(namespaceURI))
+ throw new IllegalArgumentException("illegal URI: " + namespaceURI);
+ if (!isName(localName))
+ throw new IllegalArgumentException("illegal Name: " + localName);
+
+ endStartElement();
+ namespaces.pushContext();
+
+ String prefix = getPrefix(namespaceURI);
+ boolean isDeclared = (prefix != null);
+ if (!isDeclared)
+ {
+ if (prefixDefaulting)
+ prefix = createPrefix(namespaceURI);
+ else
+ throw new XMLStreamException("namespace " + namespaceURI +
+ " has not been declared");
+ }
+ writer.write('<');
+ if (!"".equals(prefix))
+ {
+ writer.write(prefix);
+ writer.write(':');
+ }
+ writer.write(localName);
+ inStartElement = true;
+ if (!isDeclared)
+ {
+ writeNamespaceImpl(prefix, namespaceURI);
+ }
+
+ elements.addLast(new String[] { prefix, localName });
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ /**
+ * Creates a new unique prefix in the document.
+ * Subclasses may override this method to provide a suitably unique prefix
+ * for the given namespace.
+ * @param namespaceURI the namespace URI
+ */
+ protected String createPrefix(String namespaceURI)
+ {
+ Set prefixes = new HashSet();
+ for (Enumeration e = namespaces.getPrefixes(); e.hasMoreElements(); )
+ prefixes.add(e.nextElement());
+ String ret;
+ do
+ {
+ ret = "ns" + (count++);
+ }
+ while (prefixes.contains(ret));
+ return ret;
+ }
+
+ public void writeStartElement(String prefix, String localName,
+ String namespaceURI)
+ throws XMLStreamException
+ {
+ try
+ {
+ if (namespaceURI != null && !isURI(namespaceURI))
+ throw new IllegalArgumentException("illegal URI: " + namespaceURI);
+ if (prefix != null && !isPrefix(prefix))
+ throw new IllegalArgumentException("illegal NCName: " + prefix);
+ if (!isNCName(localName))
+ throw new IllegalArgumentException("illegal NCName: " + localName);
+
+ endStartElement();
+ namespaces.pushContext();
+
+ String currentPrefix = getPrefix(namespaceURI);
+ boolean isCurrent = prefix.equals(currentPrefix);
+ writer.write('<');
+ if (!"".equals(prefix))
+ {
+ writer.write(prefix);
+ writer.write(':');
+ }
+ writer.write(localName);
+ if (prefixDefaulting && !isCurrent)
+ {
+ writeNamespaceImpl(prefix, namespaceURI);
+ }
+
+ elements.addLast(new String[] { prefix, localName });
+ inStartElement = true;
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeEmptyElement(String namespaceURI, String localName)
+ throws XMLStreamException
+ {
+ writeStartElement(namespaceURI, localName);
+ emptyElement = true;
+ }
+
+ public void writeEmptyElement(String prefix, String localName,
+ String namespaceURI)
+ throws XMLStreamException
+ {
+ writeStartElement(prefix, localName, namespaceURI);
+ emptyElement = true;
+ }
+
+ public void writeEmptyElement(String localName)
+ throws XMLStreamException
+ {
+ writeStartElement(localName);
+ emptyElement = true;
+ }
+
+ public void writeEndElement()
+ throws XMLStreamException
+ {
+ if (elements.isEmpty())
+ throw new IllegalStateException("no matching start element");
+ try
+ {
+ endStartElement();
+ String[] element = (String[]) elements.removeLast();
+ namespaces.popContext();
+
+ writer.write('<');
+ writer.write('/');
+ if (element[0] != null && !"".equals(element[0]))
+ {
+ writer.write(element[0]);
+ writer.write(':');
+ }
+ writer.write(element[1]);
+ writer.write('>');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeEndDocument()
+ throws XMLStreamException
+ {
+ while (!elements.isEmpty())
+ writeEndElement();
+ }
+
+ public void close()
+ throws XMLStreamException
+ {
+ flush();
+ }
+
+ public void flush()
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.flush();
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeAttribute(String localName, String value)
+ throws XMLStreamException
+ {
+ if (!inStartElement)
+ throw new IllegalStateException();
+ try
+ {
+ if (!isName(localName))
+ throw new IllegalArgumentException("illegal Name: " + localName);
+ if (!isChars(value))
+ throw new IllegalArgumentException("illegal character: " + value);
+
+ writer.write(' ');
+ writer.write(localName);
+ writer.write('=');
+ writer.write('"');
+ if (hasXML11RestrictedChars)
+ writeEncodedWithRestrictedChars(value, true);
+ else
+ writeEncoded(value, true);
+ writer.write('"');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeAttribute(String prefix, String namespaceURI,
+ String localName, String value)
+ throws XMLStreamException
+ {
+ if (!inStartElement)
+ throw new IllegalStateException();
+ try
+ {
+ if (namespaceURI != null && !isURI(namespaceURI))
+ throw new IllegalArgumentException("illegal URI: " + namespaceURI);
+ if (prefix != null && !isPrefix(prefix))
+ throw new IllegalArgumentException("illegal NCName: " + prefix);
+ if (!isNCName(localName))
+ throw new IllegalArgumentException("illegal NCName: " + localName);
+ if (!isChars(value))
+ throw new IllegalArgumentException("illegal character: " + value);
+
+ String currentPrefix = getPrefix(namespaceURI);
+ if (currentPrefix == null)
+ {
+ if (prefixDefaulting)
+ writeNamespaceImpl(prefix, namespaceURI);
+ else
+ throw new XMLStreamException("namespace " + namespaceURI +
+ " is not bound");
+ }
+ else if (!currentPrefix.equals(prefix))
+ throw new XMLStreamException("namespace " + namespaceURI +
+ " is bound to prefix " +
+ currentPrefix);
+ writer.write(' ');
+ if (!"".equals(prefix))
+ {
+ writer.write(prefix);
+ writer.write(':');
+ }
+ writer.write(localName);
+ writer.write('=');
+ writer.write('"');
+ if (hasXML11RestrictedChars)
+ writeEncodedWithRestrictedChars(value, true);
+ else
+ writeEncoded(value, true);
+ writer.write('"');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeAttribute(String namespaceURI, String localName,
+ String value)
+ throws XMLStreamException
+ {
+ if (!inStartElement)
+ throw new IllegalStateException();
+ try
+ {
+ if (namespaceURI != null && !isURI(namespaceURI))
+ throw new IllegalArgumentException("illegal URI: " + namespaceURI);
+ if (!isName(localName))
+ throw new IllegalArgumentException("illegal Name: " + localName);
+ if (!isChars(value))
+ throw new IllegalArgumentException("illegal character: " + value);
+
+ String prefix = getPrefix(namespaceURI);
+ if (prefix == null)
+ {
+ if (prefixDefaulting)
+ {
+ prefix = XMLConstants.DEFAULT_NS_PREFIX;
+ writeNamespaceImpl(prefix, namespaceURI);
+ }
+ else
+ throw new XMLStreamException("namespace " + namespaceURI +
+ " is not bound");
+ }
+ writer.write(' ');
+ if (!"".equals(prefix))
+ {
+ writer.write(prefix);
+ writer.write(':');
+ }
+ writer.write(localName);
+ writer.write('=');
+ writer.write('"');
+ if (hasXML11RestrictedChars)
+ writeEncodedWithRestrictedChars(value, true);
+ else
+ writeEncoded(value, true);
+ writer.write('"');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeNamespace(String prefix, String namespaceURI)
+ throws XMLStreamException
+ {
+ if (prefix == null || "".equals(prefix) || "xmlns".equals(prefix))
+ {
+ writeDefaultNamespace(namespaceURI);
+ return;
+ }
+ if (!inStartElement)
+ throw new IllegalStateException();
+ try
+ {
+ if (!isURI(namespaceURI))
+ throw new IllegalArgumentException("illegal URI: " + namespaceURI);
+ if (!isPrefix(prefix))
+ throw new IllegalArgumentException("illegal NCName: " + prefix);
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ writeNamespaceImpl(prefix, namespaceURI);
+ }
+
+ private void writeNamespaceImpl(String prefix, String namespaceURI)
+ throws XMLStreamException
+ {
+ try
+ {
+ if (prefix == null)
+ prefix = XMLConstants.DEFAULT_NS_PREFIX;
+
+ setPrefix(prefix, namespaceURI);
+
+ writer.write(' ');
+ writer.write("xmlns");
+ if (!XMLConstants.DEFAULT_NS_PREFIX.equals(prefix))
+ {
+ writer.write(':');
+ writer.write(prefix);
+ }
+ writer.write('=');
+ writer.write('"');
+ writer.write(namespaceURI);
+ writer.write('"');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeDefaultNamespace(String namespaceURI)
+ throws XMLStreamException
+ {
+ if (!inStartElement)
+ throw new IllegalStateException();
+ if (!isURI(namespaceURI))
+ throw new IllegalArgumentException("illegal URI: " + namespaceURI);
+ writeNamespaceImpl(XMLConstants.DEFAULT_NS_PREFIX, namespaceURI);
+ }
+
+ public void writeComment(String data)
+ throws XMLStreamException
+ {
+ if (data == null)
+ return;
+ try
+ {
+ if (!isChars(data))
+ throw new IllegalArgumentException("illegal XML character: " + data);
+ if (data.indexOf("--") != -1)
+ throw new IllegalArgumentException("illegal comment: " + data);
+
+ endStartElement();
+
+ writer.write("<!--");
+ if (hasXML11RestrictedChars)
+ {
+ int[] seq = UnicodeReader.toCodePointArray(data);
+ for (int i = 0; i < seq.length; i++)
+ {
+ int c = seq[i];
+ if (XMLParser.isXML11RestrictedChar(c))
+ writer.write("&#x" + Integer.toHexString(c) + ";");
+ else
+ writer.write(Character.toChars(i));
+ }
+ }
+ else
+ writer.write(data);
+ writer.write("-->");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeProcessingInstruction(String target)
+ throws XMLStreamException
+ {
+ writeProcessingInstruction(target, null);
+ }
+
+ public void writeProcessingInstruction(String target, String data)
+ throws XMLStreamException
+ {
+ try
+ {
+ if (!isName(target) || "xml".equalsIgnoreCase(target))
+ throw new IllegalArgumentException("illegal PITarget: " + target);
+ if (data != null && !isChars(data))
+ throw new IllegalArgumentException("illegal XML character: " + data);
+
+ endStartElement();
+
+ writer.write('<');
+ writer.write('?');
+ writer.write(target);
+ if (data != null)
+ {
+ writer.write(' ');
+ if (hasXML11RestrictedChars)
+ {
+ int[] seq = UnicodeReader.toCodePointArray(data);
+ for (int i = 0; i < seq.length; i++)
+ {
+ int c = seq[i];
+ if (XMLParser.isXML11RestrictedChar(c))
+ writer.write("&#x" + Integer.toHexString(c) + ";");
+ else
+ writer.write(Character.toChars(i));
+ }
+ }
+ else
+ writer.write(data);
+ }
+ writer.write('?');
+ writer.write('>');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeCData(String data)
+ throws XMLStreamException
+ {
+ try
+ {
+ if (!isChars(data) || hasXML11RestrictedChars)
+ throw new IllegalArgumentException("illegal XML character: " + data);
+ if (data.indexOf("]]") != -1)
+ throw new IllegalArgumentException("illegal CDATA section: " + data);
+
+ endStartElement();
+
+ writer.write("<![CDATA[");
+ writer.write(data);
+ writer.write("]]>");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeDTD(String dtd)
+ throws XMLStreamException
+ {
+ try
+ {
+ // XXX: Should we parse the doctypedecl at this point to ensure
+ // wellformedness?
+ writer.write("<!DOCTYPE ");
+ writer.write(dtd);
+ writer.write('>');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeEntityRef(String name)
+ throws XMLStreamException
+ {
+ try
+ {
+ if (!isName(name))
+ throw new IllegalArgumentException("illegal Name: " + name);
+
+ endStartElement();
+
+ writer.write('&');
+ writer.write(name);
+ writer.write(';');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeStartDocument()
+ throws XMLStreamException
+ {
+ writeStartDocument(null, null);
+ }
+
+ public void writeStartDocument(String version)
+ throws XMLStreamException
+ {
+ writeStartDocument(null, version);
+ }
+
+ public void writeStartDocument(String encoding, String version)
+ throws XMLStreamException
+ {
+ if (version == null)
+ version = "1.0";
+ else if ("1.1".equals(version))
+ xml11 = true;
+ encoding = this.encoding; // YES: the parameter must be ignored
+ if (encoding == null)
+ encoding = "UTF-8";
+ if (!"1.0".equals(version) && !"1.1".equals(version))
+ throw new IllegalArgumentException(version);
+ try
+ {
+ writer.write("<?xml version=\"");
+ writer.write(version);
+ writer.write("\" encoding=\"");
+ writer.write(encoding);
+ writer.write("\"?>");
+ writer.write(System.getProperty("line.separator"));
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeCharacters(String text)
+ throws XMLStreamException
+ {
+ if (text == null)
+ return;
+ try
+ {
+ if (!isChars(text))
+ throw new IllegalArgumentException("illegal XML character: " + text);
+
+ endStartElement();
+
+ if (hasXML11RestrictedChars)
+ writeEncodedWithRestrictedChars(text, false);
+ else
+ writeEncoded(text, false);
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeCharacters(char[] text, int start, int len)
+ throws XMLStreamException
+ {
+ writeCharacters(new String(text, start, len));
+ }
+
+ public String getPrefix(String uri)
+ throws XMLStreamException
+ {
+ String prefix = namespaces.getPrefix(uri);
+ if (prefix == null && namespaceContext != null)
+ prefix = namespaceContext.getPrefix(uri);
+ return prefix;
+ }
+
+ public void setPrefix(String prefix, String uri)
+ throws XMLStreamException
+ {
+ try
+ {
+ if (!isURI(uri))
+ throw new IllegalArgumentException("illegal URI: " + uri);
+ if (!isPrefix(prefix))
+ throw new IllegalArgumentException("illegal NCName: " + prefix);
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ if (!namespaces.declarePrefix(prefix, uri))
+ throw new XMLStreamException("illegal prefix " + prefix);
+ }
+
+ public void setDefaultNamespace(String uri)
+ throws XMLStreamException
+ {
+ if (!isURI(uri))
+ throw new IllegalArgumentException("illegal URI: " + uri);
+ if (!namespaces.declarePrefix(XMLConstants.DEFAULT_NS_PREFIX, uri))
+ throw new XMLStreamException("illegal default namespace prefix");
+ }
+
+ public void setNamespaceContext(NamespaceContext context)
+ throws XMLStreamException
+ {
+ namespaceContext = context;
+ }
+
+ public NamespaceContext getNamespaceContext()
+ {
+ return namespaceContext;
+ }
+
+ public Object getProperty(String name)
+ throws IllegalArgumentException
+ {
+ throw new IllegalArgumentException(name);
+ }
+
+ /**
+ * Write the specified text, ensuring that the content is suitably encoded
+ * for XML.
+ * @param text the text to write
+ * @param inAttr whether we are in an attribute value
+ */
+ private void writeEncoded(String text, boolean inAttr)
+ throws IOException
+ {
+ char[] chars = text.toCharArray();
+ int start = 0;
+ int end = chars.length;
+ int len = 0;
+ for (int i = start; i < end; i++)
+ {
+ char c = chars[i];
+ if (c == '<' || c == '>' || c == '&')
+ {
+ writer.write(chars, start, len);
+ if (c == '<')
+ writer.write("&lt;");
+ else if (c == '>')
+ writer.write("&gt;");
+ else
+ writer.write("&amp;");
+ start = i + 1;
+ len = 0;
+ }
+ else if (inAttr && (c == '"' || c == '\''))
+ {
+ writer.write(chars, start, len);
+ if (c == '"')
+ writer.write("&quot;");
+ else
+ writer.write("&apos;");
+ start = i + 1;
+ len = 0;
+ }
+ else
+ len++;
+ }
+ if (len > 0)
+ writer.write(chars, start, len);
+ }
+
+ /**
+ * Writes the specified text, in the knowledge that some of the
+ * characters are XML 1.1 restricted characters.
+ */
+ private void writeEncodedWithRestrictedChars(String text, boolean inAttr)
+ throws IOException
+ {
+ int[] seq = UnicodeReader.toCodePointArray(text);
+ for (int i = 0; i < seq.length; i++)
+ {
+ int c = seq[i];
+ switch (c)
+ {
+ case 0x3c: // '<'
+ writer.write("&lt;");
+ break;
+ case 0x3e: // '>'
+ writer.write("&gt;");
+ break;
+ case 0x26: // '&'
+ writer.write("&amp;");
+ break;
+ case 0x22: // '"'
+ if (inAttr)
+ writer.write("&quot;");
+ else
+ writer.write(c);
+ break;
+ case 0x27: // '\''
+ if (inAttr)
+ writer.write("&apos;");
+ else
+ writer.write(c);
+ break;
+ default:
+ if (XMLParser.isXML11RestrictedChar(c))
+ writer.write("&#x" + Integer.toHexString(c) + ";");
+ else
+ {
+ char[] chars = Character.toChars(c);
+ writer.write(chars, 0, chars.length);
+ }
+ }
+ }
+ }
+
+ private boolean isName(String text)
+ throws IOException
+ {
+ if (text == null)
+ return false;
+ int[] seq = UnicodeReader.toCodePointArray(text);
+ if (seq.length < 1)
+ return false;
+ if (!XMLParser.isNameStartCharacter(seq[0], xml11))
+ return false;
+ for (int i = 1; i < seq.length; i++)
+ {
+ if (!XMLParser.isNameCharacter(seq[i], xml11))
+ return false;
+ }
+ return true;
+ }
+
+ private boolean isPrefix(String text)
+ throws IOException
+ {
+ if (XMLConstants.DEFAULT_NS_PREFIX.equals(text)) {
+ return true;
+ }
+ return isNCName(text);
+ }
+
+ private boolean isNCName(String text)
+ throws IOException
+ {
+ if (text == null)
+ return false;
+ int[] seq = UnicodeReader.toCodePointArray(text);
+ if (seq.length < 1)
+ return false;
+ if (!XMLParser.isNameStartCharacter(seq[0], xml11) || seq[0] == 0x3a)
+ return false;
+ for (int i = 1; i < seq.length; i++)
+ {
+ if (!XMLParser.isNameCharacter(seq[i], xml11) || seq[i] == 0x3a)
+ return false;
+ }
+ return true;
+ }
+
+ private boolean isChars(String text)
+ throws IOException
+ {
+ if (text == null)
+ return false;
+ int[] seq = UnicodeReader.toCodePointArray(text);
+ hasXML11RestrictedChars = false;
+ if (xml11)
+ {
+ for (int i = 0; i < seq.length; i++)
+ {
+ if (!XMLParser.isXML11Char(seq[i]))
+ return false;
+ if (XMLParser.isXML11RestrictedChar(seq[i]))
+ hasXML11RestrictedChars = true;
+ }
+ }
+ else
+ {
+ for (int i = 0; i < seq.length; i++)
+ {
+ if (!XMLParser.isChar(seq[i]))
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private boolean isURI(String text)
+ {
+ if (text == null)
+ return false;
+ char[] chars = text.toCharArray();
+ if (chars.length < 1)
+ return false;
+ for (int i = 0; i < chars.length; i++)
+ {
+ if (chars[i] < 0x20 || chars[i] >= 0x7f)
+ return false;
+ }
+ return true;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/AbstractNumberNode.java b/libjava/classpath/gnu/xml/transform/AbstractNumberNode.java
new file mode 100644
index 000000000..6e478bdc4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/AbstractNumberNode.java
@@ -0,0 +1,330 @@
+/* AbstractNumberNode.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing the XSL <code>number</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+abstract class AbstractNumberNode
+ extends TemplateNode
+{
+
+ static final int ALPHABETIC = 0;
+ static final int TRADITIONAL = 1;
+
+ final TemplateNode format;
+ final String lang;
+ final int letterValue;
+ final String groupingSeparator;
+ final int groupingSize;
+
+ AbstractNumberNode(TemplateNode format, String lang,
+ int letterValue, String groupingSeparator,
+ int groupingSize)
+ {
+ this.format = format;
+ this.lang = lang;
+ this.letterValue = letterValue;
+ this.groupingSeparator = groupingSeparator;
+ this.groupingSize = groupingSize;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ Document doc = (parent instanceof Document) ? (Document) parent :
+ parent.getOwnerDocument();
+ DocumentFragment fragment = doc.createDocumentFragment();
+ format.apply(stylesheet, mode, context, pos, len, fragment, null);
+ String f = Expr._string(context, Collections.singleton(fragment));
+ String value = format(f, compute(stylesheet, context, pos, len));
+ Text text = doc.createTextNode(value);
+ if (nextSibling != null)
+ {
+ parent.insertBefore(text, nextSibling);
+ }
+ else
+ {
+ parent.appendChild(text);
+ }
+ // xsl:number doesn't process children
+ if (next != null)
+ {
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+ }
+
+ String format(String format, int[] number)
+ {
+ if (number.length == 0)
+ {
+ return "";
+ }
+ int start = 0, end = 0, len = format.length(); // region of format
+ // Tokenize
+ List tokens = new ArrayList((number.length * 2) + 1);
+ List types = new ArrayList(tokens.size());
+ while (end < len)
+ {
+ while (end < len && !isAlphanumeric(format.charAt(end)))
+ {
+ end++;
+ }
+ if (end > start)
+ {
+ tokens.add(format.substring(start, end));
+ types.add(Boolean.FALSE);
+ }
+ start = end;
+ while (end < len && isAlphanumeric(format.charAt(end)))
+ {
+ end++;
+ }
+ if (end > start)
+ {
+ tokens.add(format.substring(start, end));
+ types.add(Boolean.TRUE);
+ }
+ start = end;
+ }
+ // Process tokens
+ CPStringBuilder buf = new CPStringBuilder();
+ len = tokens.size();
+ int pos = 0;
+ for (int i = 0; i < len; i++)
+ {
+ String token = (i < 0) ? "." : (String) tokens.get(i);
+ boolean alpha = (i < 0) ? true :
+ ((Boolean) types.get(i)).booleanValue();
+ if (!alpha)
+ {
+ buf.append(token);
+ }
+ else
+ {
+ if (pos < number.length)
+ {
+ format(buf, number[pos++], token);
+ if (((i + 1 == len) || (i + 2 == len)) &&
+ (pos < number.length))
+ {
+ // More numbers than tokens, reuse last token
+ i -= 2;
+ }
+ }
+ if (pos == number.length && i < (len - 2))
+ {
+ // No more numbers. Skip to the end...
+ i = len - 2;
+ if (((Boolean) types.get(i + 1)).booleanValue())
+ {
+ // number formatting token, ignore
+ i++;
+ }
+ }
+ }
+ }
+ //System.err.println("format: '"+format+"' "+asList(number)+" = '"+buf.toString()+"'");
+ return buf.toString();
+ }
+
+ /*List asList(int[] number)
+ {
+ List l = new ArrayList();
+ for (int i = 0; i < number.length; i++)
+ l.add(new Integer(number[i]));
+ return l;
+ }*/
+
+ void format(CPStringBuilder buf, int number, String formatToken)
+ {
+ int len = formatToken.length();
+ char c = formatToken.charAt(len - 1);
+ if (Character.digit(c, 10) == 1)
+ {
+ // Check preceding characters
+ for (int i = len - 2; i >= 0; i--)
+ {
+ if (formatToken.charAt(i) != (c - 1))
+ {
+ format(buf, number, "1");
+ return;
+ }
+ }
+ // Decimal representation
+ String val = Integer.toString(number);
+ for (int d = len - val.length(); d > 0; d--)
+ {
+ buf.append('0');
+ }
+ buf.append(val);
+ }
+ else if ("A".equals(formatToken))
+ {
+ buf.append(alphabetic('@', number));
+ }
+ else if ("a".equals(formatToken))
+ {
+ buf.append(alphabetic('`', number));
+ }
+ else if ("i".equals(formatToken))
+ {
+ buf.append(roman(false, number));
+ }
+ else if ("I".equals(formatToken))
+ {
+ buf.append(roman(true, number));
+ }
+ else
+ {
+ // Unknown numbering sequence
+ format(buf, number, "1");
+ }
+ }
+
+ static final boolean isAlphanumeric(char c)
+ {
+ switch (Character.getType(c))
+ {
+ case Character.DECIMAL_DIGIT_NUMBER: // Nd
+ case Character.LETTER_NUMBER: // Nl
+ case Character.OTHER_NUMBER: // No
+ case Character.UPPERCASE_LETTER: // Lu
+ case Character.LOWERCASE_LETTER: // Ll
+ case Character.TITLECASE_LETTER: // Lt
+ case Character.MODIFIER_LETTER: // Lm
+ case Character.OTHER_LETTER: // Lo
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ static final String alphabetic(char offset, int number)
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ while (number > 0)
+ {
+ int r = number % 26;
+ number = number / 26;
+ buf.insert(0, (char) (offset + r));
+ }
+ return buf.toString();
+ }
+
+ static final int[] roman_numbers = {1, 5, 10, 50, 100, 500, 1000};
+ static final char[] roman_chars = {'i', 'v', 'x', 'l', 'c', 'd', 'm'};
+
+ static final String roman(boolean upper, int number)
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ for (int pos = roman_numbers.length - 1; pos >= 0; pos -= 2)
+ {
+ int f = number / roman_numbers[pos];
+ if (f != 0)
+ {
+ number = number % (f * roman_numbers[pos]);
+ }
+ if (f > 4 && f < 9)
+ {
+ buf.append(roman_chars[pos + 1]);
+ f -= 5;
+ }
+ if (f == 4)
+ {
+ buf.append(roman_chars[pos]);
+ buf.append(roman_chars[pos + 1]);
+ }
+ else if (f == 9)
+ {
+ buf.append(roman_chars[pos]);
+ buf.append(roman_chars[pos + 2]);
+ }
+ else
+ {
+ for (; f > 0; f--)
+ {
+ buf.append(roman_chars[pos]);
+ }
+ }
+ }
+ return upper ? buf.toString().toUpperCase() : buf.toString();
+ }
+
+ abstract int[] compute(Stylesheet stylesheet, Node context, int pos, int len)
+ throws TransformerException;
+
+ public boolean references(QName var)
+ {
+ if (format.references(var))
+ {
+ return true;
+ }
+ return super.references(var);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("number");
+ buf.append('[');
+ buf.append("format=");
+ buf.append(format);
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/ApplyImportsNode.java b/libjava/classpath/gnu/xml/transform/ApplyImportsNode.java
new file mode 100644
index 000000000..298c49da4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/ApplyImportsNode.java
@@ -0,0 +1,82 @@
+/* ApplyImportsNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Node;
+
+/**
+ * A template node representing an XSLT <code>apply-imports</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class ApplyImportsNode
+ extends TemplateNode
+{
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new ApplyImportsNode();
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ TemplateNode t = stylesheet.getTemplate(mode, context, true);
+ if (t != null)
+ t.apply(stylesheet, mode, context, pos, len,
+ parent, nextSibling);
+ if (next != null)
+ next.apply(stylesheet, mode, context, pos, len,
+ parent, nextSibling);
+ }
+
+ public String toString()
+ {
+ return "apply-imports";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/ApplyTemplatesNode.java b/libjava/classpath/gnu/xml/transform/ApplyTemplatesNode.java
new file mode 100644
index 000000000..6aa36954a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/ApplyTemplatesNode.java
@@ -0,0 +1,219 @@
+/* ApplyTemplatesNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing the XSL <code>apply-templates</code>
+ * instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class ApplyTemplatesNode
+ extends TemplateNode
+{
+
+ final Expr select;
+ final QName mode;
+ final List sortKeys;
+ final List withParams;
+ final boolean isDefault;
+
+ ApplyTemplatesNode(Expr select, QName mode,
+ List sortKeys, List withParams, boolean isDefault)
+ {
+ this.select = select;
+ this.mode = mode;
+ this.sortKeys = sortKeys;
+ this.withParams = withParams;
+ this.isDefault = isDefault;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ int len = sortKeys != null ? sortKeys.size() : 0;
+ List sortKeys2 = new ArrayList(len);
+ for (int i = 0; i < len; i++)
+ sortKeys2.add(((Key) sortKeys.get(i)).clone(stylesheet));
+ len = withParams != null ? withParams.size() : 0;
+ List withParams2 = new ArrayList(len);
+ for (int i = 0; i < len; i++)
+ withParams2.add(((WithParam) withParams.get(i)).clone(stylesheet));
+ TemplateNode ret = new ApplyTemplatesNode(select.clone(stylesheet),
+ mode, sortKeys2, withParams2,
+ isDefault);
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ Object ret = select.evaluate(context, pos, len);
+ if (ret != null && ret instanceof Collection)
+ {
+ if (withParams != null)
+ {
+ // compute the parameter values
+ LinkedList values = new LinkedList();
+ for (Iterator i = withParams.iterator(); i.hasNext(); )
+ {
+ WithParam p = (WithParam) i.next();
+ Object value = p.getValue(stylesheet, mode, context, pos, len);
+ Object[] pair = new Object[2];
+ pair[0] = p.name;
+ pair[1] = value;
+ values.add(pair);
+ }
+ // push the parameter context
+ stylesheet.bindings.push(Bindings.WITH_PARAM);
+ // set the parameters
+ for (Iterator i = values.iterator(); i.hasNext(); )
+ {
+ Object[] pair = (Object[]) i.next();
+ QName name = (QName) pair[0];
+ Object value = pair[1];
+ stylesheet.bindings.set(name, value, Bindings.WITH_PARAM);
+ }
+ }
+ Collection ns = (Collection) ret;
+ List nodes = new ArrayList(ns);
+ if (sortKeys != null)
+ {
+ for (Iterator i = sortKeys.iterator(); i.hasNext(); )
+ {
+ SortKey sortKey = (SortKey) i.next();
+ sortKey.init(stylesheet, mode, context, pos, len, parent,
+ nextSibling);
+ }
+ Collections.sort(nodes, new XSLComparator(sortKeys));
+ }
+ else
+ Collections.sort(nodes, documentOrderComparator);
+ int l = nodes.size();
+ QName effectiveMode = isDefault ? mode : this.mode;
+ for (int i = 0; i < l; i++)
+ {
+ Node node = (Node) nodes.get(i);
+ TemplateNode t = stylesheet.getTemplate(effectiveMode, node,
+ false);
+ if (t != null)
+ {
+ stylesheet.current = node;
+ t.apply(stylesheet, effectiveMode, node, i + 1, l,
+ parent, nextSibling);
+ }
+ }
+ if (withParams != null)
+ {
+ // pop the variable context
+ stylesheet.bindings.pop(Bindings.WITH_PARAM);
+ }
+ }
+ // apply-templates doesn't have processable children
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ public boolean references(QName var)
+ {
+ if (select != null && select.references(var))
+ return true;
+ if (withParams != null)
+ {
+ for (Iterator i = withParams.iterator(); i.hasNext(); )
+ {
+ if (((WithParam) i.next()).references(var))
+ return true;
+ }
+ }
+ if (sortKeys != null)
+ {
+ for (Iterator i = sortKeys.iterator(); i.hasNext(); )
+ {
+ if (((SortKey) i.next()).references(var))
+ return true;
+ }
+ }
+ return super.references(var);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("apply-templates");
+ buf.append('[');
+ boolean o = false;
+ if (select != null)
+ {
+ buf.append("select=");
+ buf.append(select);
+ o = true;
+ }
+ if (mode != null)
+ {
+ if (o)
+ {
+ buf.append(',');
+ }
+ buf.append("mode=");
+ buf.append(mode);
+ }
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/AttributeNode.java b/libjava/classpath/gnu/xml/transform/AttributeNode.java
new file mode 100644
index 000000000..4c71f6a92
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/AttributeNode.java
@@ -0,0 +1,244 @@
+/* AttributeNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Attr;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing an XSL <code>attribute</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class AttributeNode
+ extends TemplateNode
+{
+
+ final TemplateNode name;
+ final TemplateNode namespace;
+ final Node source;
+
+ AttributeNode(TemplateNode name,
+ TemplateNode namespace, Node source)
+ {
+ this.name = name;
+ this.namespace = namespace;
+ this.source = source;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new AttributeNode(name.clone(stylesheet),
+ (namespace == null) ? null :
+ namespace.clone(stylesheet),
+ source);
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ Document doc = (parent instanceof Document) ? (Document) parent :
+ parent.getOwnerDocument();
+ // Create a document fragment to hold the name
+ DocumentFragment fragment = doc.createDocumentFragment();
+ // Apply name to the fragment
+ name.apply(stylesheet, mode,
+ context, pos, len,
+ fragment, null);
+ // Use XPath string-value of fragment
+ String nameValue = Expr.stringValue(fragment);
+
+ String namespaceValue = null;
+ if (namespace != null)
+ {
+ // Create a document fragment to hold the namespace
+ fragment = doc.createDocumentFragment();
+ // Apply namespace to the fragment
+ namespace.apply(stylesheet, mode,
+ context, pos, len,
+ fragment, null);
+ // Use XPath string-value of fragment
+ namespaceValue = Expr.stringValue(fragment);
+ if (namespaceValue.length() == 0)
+ namespaceValue = null;
+ }
+
+ String prefix = getPrefix(nameValue);
+ if (namespaceValue == null)
+ {
+ if (prefix != null)
+ {
+ if (XMLConstants.XML_NS_PREFIX.equals(prefix))
+ namespaceValue = XMLConstants.XML_NS_URI;
+ else
+ {
+ // Resolve namespace for this prefix
+ namespaceValue = source.lookupNamespaceURI(prefix);
+ }
+ }
+ }
+ else
+ {
+ if (prefix != null)
+ {
+ String ns2 = source.lookupNamespaceURI(prefix);
+ if (ns2 != null && !ns2.equals(namespaceValue))
+ {
+ // prefix clashes, reset it
+ prefix = null;
+ int ci = nameValue.indexOf(':');
+ nameValue = nameValue.substring(ci + 1);
+ }
+ }
+ }
+ if (prefix == null)
+ {
+ // Resolve prefix for this namespace
+ prefix = source.lookupPrefix(namespaceValue);
+ if (prefix != null)
+ nameValue = prefix + ":" + nameValue;
+ else
+ {
+ if (namespaceValue != null)
+ {
+ // Must invent a prefix
+ prefix = inventPrefix(parent);
+ nameValue = prefix + ":" + nameValue;
+ }
+ }
+ }
+ NamedNodeMap attrs = parent.getAttributes();
+ boolean insert = true;
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceValue) ||
+ XMLConstants.XMLNS_ATTRIBUTE.equals(nameValue) ||
+ nameValue.startsWith("xmlns:"))
+ {
+ // Namespace declaration, do not output
+ insert = false;
+ }
+ if (prefix != null && namespaceValue == null)
+ {
+ // Not a QName
+ insert = false;
+ }
+ if (parent.getNodeType() == Node.ELEMENT_NODE &&
+ parent.getFirstChild() != null)
+ {
+ // XSLT 7.1.3 Adding an attribute to an element after children have
+ // been added to it is an error
+ insert = false;
+ }
+ if (insert)
+ {
+ // Insert attribute
+ Attr attr = (namespaceValue != null) ?
+ doc.createAttributeNS(namespaceValue, nameValue) :
+ doc.createAttribute(nameValue);
+ if (attrs != null)
+ {
+ if (namespace != null)
+ attrs.setNamedItemNS(attr);
+ else
+ attrs.setNamedItem(attr);
+ }
+ if (children != null)
+ children.apply(stylesheet, mode,
+ context, pos, len,
+ attr, null);
+ }
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ final String getPrefix(String name)
+ {
+ int ci = name.indexOf(':');
+ return (ci == -1) ? null : name.substring(0, ci);
+ }
+
+ final String inventPrefix(Node parent)
+ {
+ String base = "ns";
+ int count = 0;
+ String ret = base + Integer.toString(count);
+ while (parent.lookupNamespaceURI(ret) != null)
+ {
+ count++;
+ ret = base + Integer.toString(count);
+ }
+ return ret;
+ }
+
+ public boolean references(QName var)
+ {
+ if (name != null && name.references(var))
+ return true;
+ if (namespace != null && namespace.references(var))
+ return true;
+ return super.references(var);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("attribute");
+ buf.append('[');
+ buf.append("name=");
+ buf.append(name);
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/AttributeSet.java b/libjava/classpath/gnu/xml/transform/AttributeSet.java
new file mode 100644
index 000000000..ced50f11c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/AttributeSet.java
@@ -0,0 +1,66 @@
+/* AttributeSet.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+/**
+ * An attribute-set entry in a stylesheet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class AttributeSet
+{
+
+ final TemplateNode children;
+ final String name;
+ final String uas;
+
+ AttributeSet(TemplateNode children, String name, String uas)
+ {
+ this.children = children;
+ this.name = name;
+ this.uas = uas;
+ }
+
+ AttributeSet clone(Stylesheet stylesheet)
+ {
+ return new AttributeSet((children == null) ? null :
+ children.clone(stylesheet),
+ name, uas);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/Bindings.java b/libjava/classpath/gnu/xml/transform/Bindings.java
new file mode 100644
index 000000000..37d45880c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/Bindings.java
@@ -0,0 +1,346 @@
+/* Bindings.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Map;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathVariableResolver;
+import org.w3c.dom.Node;
+
+/**
+ * The set of variable bindings in effect for a stylesheet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class Bindings
+ implements XPathVariableResolver, Cloneable
+{
+
+ static final int VARIABLE = 0;
+ static final int PARAM = 1;
+ static final int WITH_PARAM = 2;
+
+ final Stylesheet stylesheet;
+
+ /**
+ * Global variables.
+ */
+ final LinkedList<Map<QName,Object>> variables;
+
+ /**
+ * Parameter value stack.
+ */
+ final LinkedList<Map<QName,Object>> parameters;
+
+ /**
+ * Argument (with-param) value stack.
+ */
+ final LinkedList<Map<QName,Object>> withParameters;
+
+ /**
+ * Only search globals.
+ */
+ boolean global;
+
+ Bindings(Stylesheet stylesheet)
+ {
+ this.stylesheet = stylesheet;
+ variables = new LinkedList<Map<QName,Object>>();
+ parameters = new LinkedList<Map<QName,Object>>();
+ withParameters = new LinkedList<Map<QName,Object>>();
+ for (int i = 0; i < 3; i++)
+ {
+ push(i);
+ }
+ }
+
+ public Object clone()
+ {
+ try
+ {
+ return (Bindings) super.clone();
+ }
+ catch (CloneNotSupportedException e)
+ {
+ throw new Error(e.getMessage());
+ }
+ }
+
+ void push(int type)
+ {
+ switch (type)
+ {
+ case VARIABLE:
+ variables.addFirst(new HashMap<QName,Object>());
+ break;
+ case PARAM:
+ parameters.addFirst(new HashMap<QName,Object>());
+ break;
+ case WITH_PARAM:
+ withParameters.addFirst(new HashMap<QName,Object>());
+ break;
+ }
+ }
+
+ void pop(int type)
+ {
+ switch (type)
+ {
+ case VARIABLE:
+ variables.removeFirst();
+ break;
+ case PARAM:
+ parameters.removeFirst();
+ break;
+ case WITH_PARAM:
+ withParameters.removeFirst();
+ break;
+ }
+ }
+
+ public boolean containsKey(QName name, int type)
+ {
+ if (global)
+ {
+ Map<QName,Object> ctx1 = variables.getLast();
+ Map<QName,Object> ctx2 = parameters.getLast();
+ return (ctx1.containsKey(name) || ctx2.containsKey(name));
+ }
+ Iterator<Map<QName,Object>> i = null;
+ switch (type)
+ {
+ case VARIABLE:
+ i = variables.iterator();
+ break;
+ case PARAM:
+ i = parameters.iterator();
+ break;
+ case WITH_PARAM:
+ Map<QName,Object> ctx = withParameters.getFirst();
+ return ctx.containsKey(name);
+ }
+ if (i != null)
+ {
+ while (i.hasNext())
+ {
+ Map<QName,Object> ctx = i.next();
+ if (ctx.containsKey(name))
+ {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ public Object get(QName name, Node context, int pos, int len)
+ {
+ if (global)
+ {
+ Map<QName,Object> ctx = variables.getLast();
+ Object ret = ctx.get(name);
+ if (ret == null)
+ {
+ ctx = parameters.getLast();
+ ret = ctx.get(name);
+ }
+ return ret;
+ }
+ //System.err.println("bindings.get: "+name);
+ //System.err.println("\t"+toString());
+ Object ret = null;
+ //if (parameters.size() > 1 && containsKey(name, PARAM))
+ // check that template defines parameter
+ {
+ Map<QName,Object> cwp = withParameters.getFirst();
+ ret = cwp.get(name);
+ //System.err.println("\twith-param: ret="+ret);
+ }
+ if (ret == null)
+ {
+ for (Iterator<Map<QName,Object>> i = variables.iterator();
+ i.hasNext() && ret == null; )
+ {
+ Map<QName,Object> vctx = i.next();
+ ret = vctx.get(name);
+ }
+ //System.err.println("\tvariable: ret="+ret);
+ }
+ if (ret == null)
+ {
+ for (Iterator<Map<QName,Object>> i = parameters.iterator();
+ i.hasNext() && ret == null; )
+ {
+ Map<QName,Object> pctx = i.next();
+ ret = pctx.get(name);
+ }
+ //System.err.println("\tparam: ret="+ret);
+ }
+ /*if (ret instanceof Expr && context != null)
+ {
+ Expr expr = (Expr) ret;
+ ret = expr.evaluate(context, 1, 1);
+ }*/
+ if (ret instanceof Node)
+ {
+ ret = Collections.singleton(ret);
+ }
+ if (ret == null)
+ {
+ ret = "";
+ }
+ //System.err.println("\tret="+ret);
+ return ret;
+ }
+
+ void set(QName name, Object value, int type)
+ {
+ switch (type)
+ {
+ case VARIABLE:
+ Map<QName,Object> vctx = variables.getFirst();
+ vctx.put(name, value);
+ break;
+ case PARAM:
+ Map<QName,Object> pctx = parameters.getFirst();
+ pctx.put(name, value);
+ break;
+ case WITH_PARAM:
+ Map<QName,Object> wctx = withParameters.getFirst();
+ wctx.put(name, value);
+ break;
+ }
+ //System.err.println("Set "+name+"="+value);
+ }
+
+ public Object resolveVariable(QName qName)
+ {
+ return get(qName, null, 1, 1);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ boolean next = false;
+ Collection<QName> seen = new HashSet<QName>();
+ Map<QName,Object> wctx = withParameters.getFirst();
+ buf.append('(');
+ for (Map.Entry<QName,Object> entry : wctx.entrySet())
+ {
+ if (next)
+ {
+ buf.append(',');
+ }
+ else
+ {
+ next = true;
+ }
+ QName key = entry.getKey();
+ if (!seen.contains(key))
+ {
+ buf.append(key);
+ buf.append('=');
+ buf.append(entry.getValue());
+ seen.add(key);
+ }
+ }
+ buf.append(')');
+ next = false;
+ seen.clear();
+ buf.append('{');
+ for (Map<QName,Object> ctx : variables)
+ {
+ for (Map.Entry<QName,Object> entry : ctx.entrySet())
+ {
+ if (next)
+ {
+ buf.append(',');
+ }
+ else
+ {
+ next = true;
+ }
+ QName key = entry.getKey();
+ if (!seen.contains(key))
+ {
+ buf.append(key);
+ buf.append('=');
+ buf.append(entry.getValue());
+ seen.add(key);
+ }
+ }
+ }
+ buf.append('}');
+ next = false;
+ seen.clear();
+ buf.append('[');
+ for (Map<QName,Object> ctx : parameters)
+ {
+ for (Map.Entry<QName,Object> entry : ctx.entrySet())
+ {
+ if (next)
+ {
+ buf.append(',');
+ }
+ else
+ {
+ next = true;
+ }
+ QName key = entry.getKey();
+ if (!seen.contains(key))
+ {
+ buf.append(key);
+ buf.append('=');
+ buf.append(entry.getValue());
+ seen.add(key);
+ }
+ }
+ }
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/CallTemplateNode.java b/libjava/classpath/gnu/xml/transform/CallTemplateNode.java
new file mode 100644
index 000000000..cc08d93e3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/CallTemplateNode.java
@@ -0,0 +1,156 @@
+/* CallTemplateNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Node;
+
+/**
+ * A template node representing the XSL <code>call-template</code>
+ * instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class CallTemplateNode
+ extends TemplateNode
+{
+
+ final QName name;
+ final List withParams;
+
+ CallTemplateNode(QName name, List withParams)
+ {
+ this.name = name;
+ this.withParams = withParams;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ int len = withParams.size();
+ List withParams2 = new ArrayList(len);
+ for (int i = 0; i < len; i++)
+ withParams2.add(((WithParam) withParams.get(i)).clone(stylesheet));
+ TemplateNode ret = new CallTemplateNode(name, withParams2);
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ TemplateNode t = stylesheet.getTemplate(mode, name);
+ if (t != null)
+ {
+ if (!withParams.isEmpty())
+ {
+ // compute the parameter values
+ LinkedList values = new LinkedList();
+ for (Iterator i = withParams.iterator(); i.hasNext(); )
+ {
+ WithParam p = (WithParam) i.next();
+ if (t.hasParam(p.name)) // ignore parameters not specified
+ {
+ Object value = p.getValue(stylesheet, mode, context,
+ pos, len);
+ Object[] pair = new Object[2];
+ pair[0] = p.name;
+ pair[1] = value;
+ values.add(pair);
+ }
+ }
+ // push the parameter context
+ stylesheet.bindings.push(Bindings.WITH_PARAM);
+ // set the parameters
+ for (Iterator i = values.iterator(); i.hasNext(); )
+ {
+ Object[] pair = (Object[]) i.next();
+ QName name = (QName) pair[0];
+ Object value = pair[1];
+ stylesheet.bindings.set(name, value, Bindings.WITH_PARAM);
+ if (stylesheet.debug)
+ System.err.println("with-param: " + name + " = " + value);
+ }
+ }
+ t.apply(stylesheet, mode, context, pos, len,
+ parent, nextSibling);
+ if (!withParams.isEmpty())
+ {
+ // pop the variable context
+ stylesheet.bindings.pop(Bindings.WITH_PARAM);
+ }
+ }
+ // call-template doesn't have processable children
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ public boolean references(QName var)
+ {
+ for (Iterator i = withParams.iterator(); i.hasNext(); )
+ {
+ if (((WithParam) i.next()).references(var))
+ return true;
+ }
+ return super.references(var);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("call-template");
+ buf.append('[');
+ buf.append("name=");
+ buf.append(name);
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/ChooseNode.java b/libjava/classpath/gnu/xml/transform/ChooseNode.java
new file mode 100644
index 000000000..262cde144
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/ChooseNode.java
@@ -0,0 +1,88 @@
+/* ChooseNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Node;
+
+/**
+ * A template node representing an XSL <code>choose</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class ChooseNode
+ extends TemplateNode
+{
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new ChooseNode();
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ if (children != null)
+ children.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("choose");
+ buf.append('[');
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/CommentNode.java b/libjava/classpath/gnu/xml/transform/CommentNode.java
new file mode 100644
index 000000000..1c14e5fa8
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/CommentNode.java
@@ -0,0 +1,103 @@
+/* CommentNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing the XSL <code>comment</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class CommentNode
+ extends TemplateNode
+{
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new CommentNode();
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ String value = "";
+ Document doc = (parent instanceof Document) ? (Document) parent :
+ parent.getOwnerDocument();
+ if (children != null)
+ {
+ // Create a document fragment to hold the text
+ DocumentFragment fragment = doc.createDocumentFragment();
+ // Apply children to the fragment
+ children.apply(stylesheet, mode,
+ context, pos, len,
+ fragment, null);
+ // Use XPath string-value of fragment
+ value = Expr.stringValue(fragment);
+ }
+ Comment comment = doc.createComment(value);
+ // Insert into result tree
+ if (nextSibling != null)
+ parent.insertBefore(comment, nextSibling);
+ else
+ parent.appendChild(comment);
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ public String toString()
+ {
+ return "comment";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/CopyNode.java b/libjava/classpath/gnu/xml/transform/CopyNode.java
new file mode 100644
index 000000000..9cc2fdecd
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/CopyNode.java
@@ -0,0 +1,168 @@
+/* CopyNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.Iterator;
+import java.util.StringTokenizer;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+/**
+ * A template node representing the XSL <code>copy</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class CopyNode
+ extends TemplateNode
+{
+
+ final String uas;
+
+ CopyNode(String uas)
+ {
+ this.uas = uas;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new CopyNode(uas);
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ Node copy = parent;
+ switch (context.getNodeType())
+ {
+ case Node.TEXT_NODE:
+ case Node.ATTRIBUTE_NODE:
+ case Node.ELEMENT_NODE:
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ case Node.COMMENT_NODE:
+ Document doc = (parent instanceof Document) ? (Document) parent :
+ parent.getOwnerDocument();
+ copy = context.cloneNode(false);
+ copy = doc.adoptNode(copy);
+ if (copy.getNodeType() == Node.ATTRIBUTE_NODE)
+ {
+ if (parent.getFirstChild() != null)
+ {
+ // Ignore attempt to add attribute after children
+ }
+ else
+ {
+ NamedNodeMap attrs = parent.getAttributes();
+ if (attrs != null)
+ attrs.setNamedItemNS(copy);
+ }
+ }
+ else
+ {
+ if (nextSibling != null)
+ parent.insertBefore(copy, nextSibling);
+ else
+ parent.appendChild(copy);
+ }
+ }
+ if (uas != null)
+ {
+ StringTokenizer st = new StringTokenizer(uas, " ");
+ while (st.hasMoreTokens())
+ addAttributeSet(stylesheet, mode, context, pos, len,
+ copy, null, st.nextToken());
+ }
+ if (children != null)
+ children.apply(stylesheet, mode,
+ context, pos, len,
+ copy, null);
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ void addAttributeSet(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling, String attributeSet)
+ throws TransformerException
+ {
+ for (Iterator i = stylesheet.attributeSets.iterator(); i.hasNext(); )
+ {
+ AttributeSet as = (AttributeSet) i.next();
+ if (!as.name.equals(attributeSet))
+ continue;
+ if (as.uas != null)
+ {
+ StringTokenizer st = new StringTokenizer(as.uas, " ");
+ while (st.hasMoreTokens())
+ addAttributeSet(stylesheet, mode, context, pos, len,
+ parent, nextSibling, st.nextToken());
+ }
+ if (as.children != null)
+ as.children.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("copy");
+ if (uas != null)
+ {
+ buf.append('[');
+ buf.append("uas=");
+ buf.append(uas);
+ buf.append(']');
+ }
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/CopyOfNode.java b/libjava/classpath/gnu/xml/transform/CopyOfNode.java
new file mode 100644
index 000000000..2b06fed4d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/CopyOfNode.java
@@ -0,0 +1,174 @@
+/* CopyOfNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing an XSLT <code>copy-of</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class CopyOfNode
+ extends TemplateNode
+{
+
+ final Expr select;
+
+ CopyOfNode(Expr select)
+ {
+ this.select = select;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new CopyOfNode(select.clone(stylesheet));
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ Object ret = select.evaluate(context, pos, len);
+ Document doc = (parent instanceof Document) ? (Document) parent :
+ parent.getOwnerDocument();
+ if (ret instanceof Collection)
+ {
+ Collection ns = (Collection) ret;
+ List list = new ArrayList(ns);
+ Collections.sort(list, documentOrderComparator);
+ for (Iterator i = list.iterator(); i.hasNext(); )
+ {
+ Node src = (Node) i.next();
+ short nodeType = src.getNodeType();
+ if (nodeType == Node.DOCUMENT_NODE)
+ {
+ // Use document element
+ src = ((Document) src).getDocumentElement();
+ if (src == null)
+ continue;
+ nodeType = Node.ELEMENT_NODE;
+ }
+ else if (nodeType == Node.ATTRIBUTE_NODE)
+ {
+ if (parent.getFirstChild() != null)
+ {
+ // Ignore attempt to add attribute after children
+ continue;
+ }
+ }
+ if (parent.getNodeType() == Node.ATTRIBUTE_NODE &&
+ nodeType != Node.TEXT_NODE &&
+ nodeType != Node.ENTITY_REFERENCE_NODE)
+ {
+ // Ignore
+ continue;
+ }
+ Node node = src.cloneNode(true);
+ node = doc.adoptNode(node);
+ if (nodeType == Node.ATTRIBUTE_NODE)
+ {
+ NamedNodeMap attrs = parent.getAttributes();
+ if (attrs != null)
+ attrs.setNamedItemNS(node);
+ }
+ else
+ {
+ if (nextSibling != null)
+ parent.insertBefore(node, nextSibling);
+ else
+ parent.appendChild(node);
+ }
+ }
+ }
+ else
+ {
+ String value = Expr._string(context, ret);
+ if (value != null && value.length() > 0)
+ {
+ Text textNode = doc.createTextNode(value);
+ if (nextSibling != null)
+ parent.insertBefore(textNode, nextSibling);
+ else
+ parent.appendChild(textNode);
+ }
+ }
+ // copy-of doesn't process children
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ public boolean references(QName var)
+ {
+ if (select != null && select.references(var))
+ return true;
+ return super.references(var);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("copy-of");
+ buf.append('[');
+ buf.append("select=");
+ buf.append(select);
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/CurrentFunction.java b/libjava/classpath/gnu/xml/transform/CurrentFunction.java
new file mode 100644
index 000000000..96899679a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/CurrentFunction.java
@@ -0,0 +1,103 @@
+/* CurrentFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.util.Collections;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+import gnu.xml.xpath.Function;
+
+/**
+ * The XSLT <code>current()</code>function.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class CurrentFunction
+ extends Expr
+ implements Function, XPathFunction
+{
+
+ final Stylesheet stylesheet;
+
+ CurrentFunction(Stylesheet stylesheet)
+ {
+ this.stylesheet = stylesheet;
+ }
+
+ public Object evaluate(List args)
+ throws XPathFunctionException
+ {
+ // We can't do anything useful here.
+ // So much for the JAXP API...
+ return Collections.EMPTY_SET;
+ }
+
+ public void setArguments(List args)
+ {
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ return Collections.singleton(stylesheet.current);
+ }
+
+ public Expr clone(Object context)
+ {
+ Stylesheet s = stylesheet;
+ if (context instanceof Stylesheet)
+ {
+ s = (Stylesheet) context;
+ }
+ return new CurrentFunction(s);
+ }
+
+ public boolean references(QName var)
+ {
+ return false;
+ }
+
+ public String toString()
+ {
+ return "current()";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/DOMSourceLocator.java b/libjava/classpath/gnu/xml/transform/DOMSourceLocator.java
new file mode 100644
index 000000000..fbf31de55
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/DOMSourceLocator.java
@@ -0,0 +1,84 @@
+/* DOMSourceLocator.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import javax.xml.transform.dom.DOMLocator;
+import org.w3c.dom.Node;
+
+/**
+ * Simple DOMLocator implementation.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class DOMSourceLocator
+ implements DOMLocator
+{
+
+ final Node node;
+
+ DOMSourceLocator(Node node)
+ {
+ this.node = node;
+ }
+
+ public Node getOriginatingNode()
+ {
+ return node;
+ }
+
+ public String getPublicId()
+ {
+ return null;
+ }
+
+ public String getSystemId()
+ {
+ return null;
+ }
+
+ public int getLineNumber()
+ {
+ return -1;
+ }
+
+ public int getColumnNumber()
+ {
+ return -1;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/DocumentFunction.java b/libjava/classpath/gnu/xml/transform/DocumentFunction.java
new file mode 100644
index 000000000..d0aee25b3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/DocumentFunction.java
@@ -0,0 +1,244 @@
+/* DocumentFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.TreeSet;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Constant;
+import gnu.xml.xpath.Expr;
+import gnu.xml.xpath.Function;
+import gnu.xml.xpath.IdFunction;
+
+/**
+ * The XSLT <code>document()</code>function.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class DocumentFunction
+ extends Expr
+ implements Function, XPathFunction
+{
+
+ final Stylesheet stylesheet;
+ final Node base;
+ List args;
+ List values;
+
+ DocumentFunction(Stylesheet stylesheet, Node base)
+ {
+ this.stylesheet = stylesheet;
+ this.base = base;
+ }
+
+ public Object evaluate(List args)
+ throws XPathFunctionException
+ {
+ values = args;
+ return evaluate(null, 1, 1);
+ }
+
+ public void setArguments(List args)
+ {
+ this.args = args;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ int arity = args.size();
+ if (values == null)
+ {
+ values = new ArrayList(arity);
+ for (int i = 0; i < arity; i++)
+ {
+ Expr arg = (Expr) args.get(i);
+ values.add(arg.evaluate(context, pos, len));
+ }
+ }
+ Object ret;
+ switch (arity)
+ {
+ case 1:
+ Object arg = values.get(0);
+ if (arg instanceof Collection)
+ {
+ Collection ns = (Collection) arg;
+ Collection acc = new TreeSet();
+ for (Iterator i = ns.iterator(); i.hasNext(); )
+ {
+ Node node = (Node) i.next();
+ String s = Expr.stringValue(node);
+ acc.addAll(document(s, node.getBaseURI()));
+ }
+ ret = acc;
+ }
+ else
+ {
+ String s = Expr._string(context, arg);
+ ret = document(s, base.getBaseURI());
+ }
+ break;
+ case 2:
+ Object arg1 = values.get(0);
+ Object arg2 = values.get(1);
+ if (!(arg2 instanceof Collection))
+ throw new RuntimeException("second argument is not a node-set");
+ Collection arg2ns = (Collection) arg2;
+ String base2 = arg2ns.isEmpty() ? null :
+ ((Node) arg2ns.iterator().next()).getBaseURI();
+ if (arg1 instanceof Collection)
+ {
+ Collection arg1ns = (Collection) arg1;
+ Collection acc = new TreeSet();
+ for (Iterator i = arg1ns.iterator(); i.hasNext(); )
+ {
+ Node node = (Node) i.next();
+ String s = Expr.stringValue(node);
+ acc.addAll(document(s, base2));
+ }
+ ret = acc;
+ }
+ else
+ {
+ String s = Expr._string(context, arg1);
+ ret = document(s, base2);
+ }
+ break;
+ default:
+ throw new RuntimeException("invalid arity");
+ }
+ values = null;
+ return ret;
+ }
+
+ /**
+ * The XSL <code>document</code> function.
+ * @see XSLT 12.1
+ * @param uri the URI from which to retrieve nodes
+ * @param base the base URI for relative URIs
+ */
+ Collection document(String uri, String base)
+ {
+ if ("".equals(uri) || uri == null)
+ uri = this.base.getBaseURI();
+
+ // Get fragment
+ Expr fragment = null;
+ int hi = uri.indexOf('#');
+ if (hi != -1)
+ {
+ String f = uri.substring(hi + 1);
+ uri = uri.substring(0, hi);
+ // TODO handle xpointer() here
+ // this only handles IDs
+ fragment = new IdFunction(new Constant(f));
+ }
+
+ // Get document source
+ try
+ {
+ DOMSource source;
+ XSLURIResolver resolver = stylesheet.factory.resolver;
+ synchronized (resolver)
+ {
+ if (stylesheet.transformer != null)
+ {
+ resolver.setUserResolver(stylesheet.transformer.uriResolver);
+ resolver.setUserListener(stylesheet.transformer.errorListener);
+ }
+ source = resolver.resolveDOM(null, base, uri);
+ }
+ Node node = source.getNode();
+ // Strip whitespace
+ TransformerImpl.strip(stylesheet, node);
+ if (fragment == null)
+ return Collections.singleton(node);
+ else
+ {
+ Object ret = fragment.evaluate(node, 1, 1);
+ if (!(ret instanceof Collection))
+ {
+ // XXX Report error?
+ return Collections.EMPTY_SET;
+ }
+ return (Collection) ret;
+ }
+ }
+ catch (TransformerException e)
+ {
+ String msg = "can't open " + uri;
+ if (base != null)
+ msg += " with base " + base;
+ throw new RuntimeException(msg);
+ }
+ }
+
+ public Expr clone(Object context)
+ {
+ Stylesheet s = stylesheet;
+ if (context instanceof Stylesheet)
+ s = (Stylesheet) context;
+ DocumentFunction f = new DocumentFunction(s, base);
+ int len = args.size();
+ List args2 = new ArrayList(len);
+ for (int i = 0; i < len; i++)
+ args2.add(((Expr) args.get(i)).clone(context));
+ f.setArguments(args2);
+ return f;
+ }
+
+ public boolean references(QName var)
+ {
+ for (Iterator i = args.iterator(); i.hasNext(); )
+ {
+ if (((Expr) i.next()).references(var))
+ return true;
+ }
+ return false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/ElementAvailableFunction.java b/libjava/classpath/gnu/xml/transform/ElementAvailableFunction.java
new file mode 100644
index 000000000..6a9d256be
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/ElementAvailableFunction.java
@@ -0,0 +1,181 @@
+/* ElementAvailableFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.TreeSet;
+import javax.xml.namespace.QName;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+import gnu.xml.xpath.Function;
+
+/**
+ * The XSLT <code>element-available</code> function.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class ElementAvailableFunction
+ extends Expr
+ implements Function, XPathFunction
+{
+
+ static final Collection elements;
+ static
+ {
+ TreeSet acc = new TreeSet();
+ acc.add("stylesheet");
+ acc.add("template");
+ acc.add("param");
+ acc.add("variable");
+ acc.add("include");
+ acc.add("import");
+ acc.add("output");
+ acc.add("preserve-space");
+ acc.add("strip-space");
+ acc.add("key");
+ acc.add("decimal-format");
+ acc.add("namespace-alias");
+ acc.add("attribute-set");
+ acc.add("apply-templates");
+ acc.add("call-template");
+ acc.add("value-of");
+ acc.add("for-each");
+ acc.add("if");
+ acc.add("choose");
+ acc.add("when");
+ acc.add("otherwise");
+ acc.add("element");
+ acc.add("attribute");
+ acc.add("text");
+ acc.add("copy");
+ acc.add("processing-instruction");
+ acc.add("comment");
+ acc.add("number");
+ acc.add("copy-of");
+ acc.add("message");
+ acc.add("sort");
+ acc.add("with-param");
+ acc.add("fallback");
+ acc.add("apply-imports");
+ elements = Collections.unmodifiableSet(acc);
+ }
+
+ final NamespaceContext nsctx;
+ List args;
+
+ ElementAvailableFunction(NamespaceContext nsctx)
+ {
+ this.nsctx = nsctx;
+ }
+
+ public Object evaluate(List args)
+ throws XPathFunctionException
+ {
+ // Useless...
+ return Collections.EMPTY_SET;
+ }
+
+ public void setArguments(List args)
+ {
+ this.args = args;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Expr arg = (Expr) args.get(0);
+ Object val = arg.evaluate(context, pos, len);
+ String name = _string(context, val);
+ String prefix, localName, uri;
+ int ci = name.indexOf(':');
+ if (ci == -1)
+ {
+ prefix = null;
+ localName = name;
+ }
+ else
+ {
+ prefix = name.substring(0, ci);
+ localName = name.substring(ci + 1);
+ }
+ uri = nsctx.getNamespaceURI(prefix);
+ if (Stylesheet.XSL_NS.equals(uri))
+ {
+ return elements.contains(localName) ?
+ Boolean.TRUE : Boolean.FALSE;
+ }
+ // TODO extension elements
+ return Boolean.FALSE;
+ }
+
+ public Expr clone(Object context)
+ {
+ NamespaceContext n = nsctx;
+ if (context instanceof NamespaceContext)
+ n = (NamespaceContext) context;
+ ElementAvailableFunction f = new ElementAvailableFunction(n);
+ int len = args.size();
+ List args2 = new ArrayList(len);
+ for (int i = 0; i < len; i++)
+ args2.add(((Expr) args.get(i)).clone(context));
+ f.setArguments(args2);
+ return f;
+ }
+
+ public boolean references(QName var)
+ {
+ for (Iterator i = args.iterator(); i.hasNext(); )
+ {
+ if (((Expr) i.next()).references(var))
+ return true;
+ }
+ return false;
+ }
+
+ public String toString()
+ {
+ return "element-available(" + args.get(0) + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/ElementNode.java b/libjava/classpath/gnu/xml/transform/ElementNode.java
new file mode 100644
index 000000000..78a2007b7
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/ElementNode.java
@@ -0,0 +1,261 @@
+/* ElementNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.StringTokenizer;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing an XSL <code>element</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class ElementNode
+ extends TemplateNode
+{
+
+ final TemplateNode name;
+ final TemplateNode namespace;
+ final String uas;
+ final Node source;
+ final Collection elementExcludeResultPrefixes;
+
+ ElementNode(TemplateNode name,
+ TemplateNode namespace, String uas, Node source)
+ {
+ this.name = name;
+ this.namespace = namespace;
+ this.uas = uas;
+ this.source = source;
+ NamedNodeMap attrs = source.getAttributes();
+ Node attr = attrs.getNamedItemNS(Stylesheet.XSL_NS,
+ "exclude-result-prefixes");
+ if (attr != null)
+ {
+ elementExcludeResultPrefixes = new HashSet();
+ StringTokenizer st = new StringTokenizer(attr.getNodeValue());
+ while (st.hasMoreTokens())
+ elementExcludeResultPrefixes.add(st.nextToken());
+ }
+ else
+ elementExcludeResultPrefixes = Collections.EMPTY_SET;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new ElementNode(name.clone(stylesheet),
+ (namespace == null) ? null :
+ namespace.clone(stylesheet),
+ uas, source);
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ Document doc = (parent instanceof Document) ? (Document) parent :
+ parent.getOwnerDocument();
+ // Create a document fragment to hold the name
+ DocumentFragment fragment = doc.createDocumentFragment();
+ // Apply name to the fragment
+ name.apply(stylesheet, mode,
+ context, pos, len,
+ fragment, null);
+ // Use XPath string-value of fragment
+ String nameValue = Expr.stringValue(fragment);
+
+ String namespaceValue = null;
+ if (namespace != null)
+ {
+ // Create a document fragment to hold the namespace
+ fragment = doc.createDocumentFragment();
+ // Apply namespace to the fragment
+ namespace.apply(stylesheet, mode,
+ context, pos, len,
+ fragment, null);
+ // Use XPath string-value of fragment
+ namespaceValue = Expr.stringValue(fragment);
+ if (namespaceValue.length() == 0)
+ namespaceValue = null;
+ }
+ else
+ {
+ String prefix = getPrefix(nameValue);
+ if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
+ {
+ int ci = nameValue.indexOf(':');
+ nameValue = nameValue.substring(ci + 1);
+ }
+ else
+ {
+ // Namespace aliasing
+ if (prefix == null)
+ prefix = "#default";
+ String resultPrefix =
+ (String) stylesheet.namespaceAliases.get(prefix);
+ if (resultPrefix != null)
+ {
+ if ("#default".equals(resultPrefix))
+ resultPrefix = null;
+ namespaceValue = source.lookupNamespaceURI(resultPrefix);
+ }
+ if (prefix == "#default")
+ prefix = null;
+ // Look up ordinary namespace for this prefix
+ if (namespaceValue == null)
+ {
+ if (XMLConstants.XML_NS_PREFIX.equals(prefix))
+ namespaceValue = XMLConstants.XML_NS_URI;
+ else
+ {
+ // Resolve namespace for this prefix
+ namespaceValue = source.lookupNamespaceURI(prefix);
+ }
+ }
+ }
+ }
+
+ // Create element
+ Element element = (namespaceValue != null) ?
+ doc.createElementNS(namespaceValue, nameValue) :
+ doc.createElement(nameValue);
+ if (nextSibling != null)
+ parent.insertBefore(element, nextSibling);
+ else
+ parent.appendChild(element);
+ stylesheet.addNamespaceNodes(source, element, doc,
+ elementExcludeResultPrefixes);
+ if (uas != null)
+ {
+ StringTokenizer st = new StringTokenizer(uas, " ");
+ while (st.hasMoreTokens())
+ addAttributeSet(stylesheet, mode, context, pos, len,
+ element, null, st.nextToken());
+ }
+ if (children != null)
+ children.apply(stylesheet, mode,
+ context, pos, len,
+ element, null);
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ final String getPrefix(String name)
+ {
+ int ci = name.indexOf(':');
+ return (ci == -1) ? null : name.substring(0, ci);
+ }
+
+ void addAttributeSet(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling, String attributeSet)
+ throws TransformerException
+ {
+ stylesheet.bindings.global = true;
+ for (Iterator i = stylesheet.attributeSets.iterator(); i.hasNext(); )
+ {
+ AttributeSet as = (AttributeSet) i.next();
+ if (!as.name.equals(attributeSet))
+ continue;
+ if (as.uas != null)
+ {
+ StringTokenizer st = new StringTokenizer(as.uas, " ");
+ while (st.hasMoreTokens())
+ addAttributeSet(stylesheet, mode, context, pos, len,
+ parent, nextSibling, st.nextToken());
+ }
+ if (as.children != null)
+ as.children.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+ stylesheet.bindings.global = false;
+ }
+
+ public boolean references(QName var)
+ {
+ if (name != null && name.references(var))
+ return true;
+ if (namespace != null && namespace.references(var))
+ return true;
+ return super.references(var);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("element");
+ buf.append('[');
+ buf.append("name=");
+ if (namespace != null)
+ {
+ buf.append(",namespace=");
+ buf.append(namespace);
+ }
+ buf.append(name);
+ if (uas != null)
+ {
+ buf.append(",uas=");
+ buf.append(uas);
+ }
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/ErrorListenerErrorHandler.java b/libjava/classpath/gnu/xml/transform/ErrorListenerErrorHandler.java
new file mode 100644
index 000000000..8a28795c4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/ErrorListenerErrorHandler.java
@@ -0,0 +1,101 @@
+/* ErrorListenerErrorHandler.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import javax.xml.transform.ErrorListener;
+import javax.xml.transform.TransformerException;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+/**
+ * An ErrorHandler that wraps an ErrorListener.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class ErrorListenerErrorHandler
+ implements ErrorHandler
+{
+
+ final ErrorListener listener;
+
+ ErrorListenerErrorHandler(ErrorListener listener)
+ {
+ this.listener = listener;
+ }
+
+ public void warning(SAXParseException e)
+ throws SAXException
+ {
+ try
+ {
+ listener.warning(new TransformerException(e));
+ }
+ catch (TransformerException e2)
+ {
+ throw new SAXException(e2);
+ }
+ }
+
+ public void error(SAXParseException e)
+ throws SAXException
+ {
+ try
+ {
+ listener.error(new TransformerException(e));
+ }
+ catch (TransformerException e2)
+ {
+ throw new SAXException(e2);
+ }
+ }
+
+ public void fatalError(SAXParseException e)
+ throws SAXException
+ {
+ try
+ {
+ listener.fatalError(new TransformerException(e));
+ }
+ catch (TransformerException e2)
+ {
+ throw new SAXException(e2);
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/ForEachNode.java b/libjava/classpath/gnu/xml/transform/ForEachNode.java
new file mode 100644
index 000000000..1613f768b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/ForEachNode.java
@@ -0,0 +1,159 @@
+/* ForEachNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing an XSLT <code>for-each</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class ForEachNode
+ extends TemplateNode
+{
+
+ final Expr select;
+ final List<SortKey> sortKeys;
+
+ ForEachNode(Expr select, List<SortKey> sortKeys)
+ {
+ this.select = select;
+ this.sortKeys = sortKeys;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ int len = sortKeys.size();
+ List<SortKey> sortKeys2 = new ArrayList<SortKey>(len);
+ for (int i = 0; i < len; i++)
+ sortKeys2.add(sortKeys.get(i).clone(stylesheet));
+ TemplateNode ret = new ForEachNode(select.clone(stylesheet),
+ sortKeys2);
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ @Override
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ if (children != null)
+ {
+ // Set current template to null
+ Template saved = stylesheet.currentTemplate;
+ stylesheet.currentTemplate = null;
+ Object ret = select.evaluate(context, pos, len);
+ //System.err.println(toString() + ": " + context+" -> "+ret);
+ if (ret instanceof Collection)
+ {
+ /* Suppression is safe, as we know context produces Collection<Node> */
+ @SuppressWarnings("unchecked")
+ Collection<Node> ns = (Collection<Node>) ret;
+ List<Node> list = new ArrayList<Node>(ns);
+ if (!sortKeys.isEmpty())
+ {
+ for (SortKey sortKey : sortKeys)
+ {
+ sortKey.init(stylesheet, mode, context, pos, len, parent,
+ nextSibling);
+ }
+ Collections.sort(list, new XSLComparator(sortKeys));
+ }
+ else
+ Collections.sort(list, documentOrderComparator);
+ // Perform children for each node
+ int l = list.size();
+ int p = 1;
+ for (Node node : list)
+ {
+ stylesheet.current = node;
+ children.apply(stylesheet, mode,
+ node, p++, l,
+ parent, nextSibling);
+ }
+ }
+ // Restore current template
+ stylesheet.currentTemplate = saved;
+ }
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ @Override
+ public boolean references(QName var)
+ {
+ if (select != null && select.references(var))
+ return true;
+ for (Iterator<SortKey> i = sortKeys.iterator(); i.hasNext(); )
+ {
+ if (i.next().references(var))
+ return true;
+ }
+ return super.references(var);
+ }
+
+ @Override
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("for-each");
+ buf.append('[');
+ buf.append("select=");
+ buf.append(select);
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/FormatNumberFunction.java b/libjava/classpath/gnu/xml/transform/FormatNumberFunction.java
new file mode 100644
index 000000000..82872be77
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/FormatNumberFunction.java
@@ -0,0 +1,145 @@
+/* FormatNumberFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.text.DecimalFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+import gnu.xml.xpath.Function;
+
+/**
+ * The XSLT <code>format-number()</code>function.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class FormatNumberFunction
+ extends Expr
+ implements XPathFunction, Function
+{
+
+ final Stylesheet stylesheet;
+ List args;
+
+ FormatNumberFunction(Stylesheet stylesheet)
+ {
+ this.stylesheet = stylesheet;
+ }
+
+ public Object evaluate(List args)
+ throws XPathFunctionException
+ {
+ // Useless...
+ return Collections.EMPTY_SET;
+ }
+
+ public void setArguments(List args)
+ {
+ this.args = args;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ int arity = args.size();
+ List values = new ArrayList(arity);
+ for (int i = 0; i < arity; i++)
+ {
+ Expr arg = (Expr) args.get(i);
+ values.add(arg.evaluate(context, pos, len));
+ }
+ double number = _number(context, values.get(0));
+ String pattern = _string(context, values.get(1));
+ // Currency symbol &#x00a4; is not supposed to be present
+ if (pattern.indexOf('\u00a4') != -1)
+ {
+ // Replace with $ (Xalan does this)
+ pattern = pattern.replace('\u00a4', '$');
+ }
+ String dfName = null;
+ if (arity > 2)
+ {
+ dfName = _string(context, values.get(2));
+ // otherwise the default decimal-format will be used
+ }
+ DecimalFormat df = (DecimalFormat) stylesheet.decimalFormats.get(dfName);
+ if (df == null)
+ {
+ throw new IllegalArgumentException("No such decimal-format: " +
+ dfName);
+ }
+ df.applyLocalizedPattern(pattern);
+ return df.format(number);
+ }
+
+ public Expr clone(Object context)
+ {
+ Stylesheet s = stylesheet;
+ if (context instanceof Stylesheet)
+ {
+ s = (Stylesheet) context;
+ }
+ FormatNumberFunction f = new FormatNumberFunction(s);
+ int len = args.size();
+ List args2 = new ArrayList(len);
+ for (int i = 0; i < len; i++)
+ {
+ args2.add(((Expr) args.get(i)).clone(context));
+ }
+ f.setArguments(args2);
+ return f;
+ }
+
+ public boolean references(QName var)
+ {
+ for (Iterator i = args.iterator(); i.hasNext(); )
+ {
+ if (((Expr) i.next()).references(var))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/FunctionAvailableFunction.java b/libjava/classpath/gnu/xml/transform/FunctionAvailableFunction.java
new file mode 100644
index 000000000..1ac50327e
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/FunctionAvailableFunction.java
@@ -0,0 +1,192 @@
+/* FunctionAvailableFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.TreeSet;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+import gnu.xml.xpath.Function;
+
+/**
+ * The XSLT <code>function-available</code> function.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class FunctionAvailableFunction
+ extends Expr
+ implements Function, XPathFunction
+{
+
+ static final Collection xsltFunctions;
+ static final Collection xpathFunctions;
+ static
+ {
+ TreeSet acc = new TreeSet();
+ acc.add("document");
+ acc.add("key");
+ acc.add("format-number");
+ acc.add("current");
+ acc.add("unparsed-entity-uri");
+ acc.add("generate-id");
+ acc.add("system-property");
+ acc.add("element-available");
+ acc.add("function-available");
+ xsltFunctions = Collections.unmodifiableSet(acc);
+ acc = new TreeSet();
+ acc.add("boolean");
+ acc.add("ceiling");
+ acc.add("concat");
+ acc.add("contains");
+ acc.add("count");
+ acc.add("false");
+ acc.add("floor");
+ acc.add("id");
+ acc.add("lang");
+ acc.add("last");
+ acc.add("local-name");
+ acc.add("name");
+ acc.add("namespace-uri");
+ acc.add("normalize-space");
+ acc.add("not");
+ acc.add("number");
+ acc.add("position");
+ acc.add("round");
+ acc.add("starts-with");
+ acc.add("string");
+ acc.add("string-length");
+ acc.add("substring-after");
+ acc.add("substring-before");
+ acc.add("substring");
+ acc.add("sum");
+ acc.add("translate");
+ acc.add("true");
+ xpathFunctions = Collections.unmodifiableSet(acc);
+ }
+
+ final NamespaceContext nsctx;
+ List args;
+
+ FunctionAvailableFunction(NamespaceContext nsctx)
+ {
+ this.nsctx = nsctx;
+ }
+
+ public Object evaluate(List args)
+ throws XPathFunctionException
+ {
+ // Useless...
+ return Collections.EMPTY_SET;
+ }
+
+ public void setArguments(List args)
+ {
+ this.args = args;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Expr arg = (Expr) args.get(0);
+ Object val = arg.evaluate(context, pos, len);
+ String name = _string(context, val);
+ String prefix, localName, uri;
+ int ci = name.indexOf(':');
+ if (ci == -1)
+ {
+ prefix = null;
+ localName = name;
+ }
+ else
+ {
+ prefix = name.substring(0, ci);
+ localName = name.substring(ci + 1);
+ }
+ uri = nsctx.getNamespaceURI(prefix);
+ if (uri == null)
+ {
+ return (xpathFunctions.contains(localName) ||
+ xsltFunctions.contains(localName)) ?
+ Boolean.TRUE : Boolean.FALSE;
+ }
+ else if (Stylesheet.XSL_NS.equals(uri))
+ {
+ return xsltFunctions.contains(localName) ?
+ Boolean.TRUE : Boolean.FALSE;
+ }
+ // TODO extension functions
+ return Boolean.FALSE;
+ }
+
+ public Expr clone(Object context)
+ {
+ NamespaceContext n = nsctx;
+ if (context instanceof NamespaceContext)
+ n = (NamespaceContext) context;
+ FunctionAvailableFunction f = new FunctionAvailableFunction(n);
+ int len = args.size();
+ List args2 = new ArrayList(len);
+ for (int i = 0; i < len; i++)
+ args2.add(((Expr) args.get(i)).clone(context));
+ f.setArguments(args2);
+ return f;
+ }
+
+ public boolean references(QName var)
+ {
+ for (Iterator i = args.iterator(); i.hasNext(); )
+ {
+ if (((Expr) i.next()).references(var))
+ return true;
+ }
+ return false;
+ }
+
+ public String toString()
+ {
+ return "function-available(" + args.get(0) + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/GenerateIdFunction.java b/libjava/classpath/gnu/xml/transform/GenerateIdFunction.java
new file mode 100644
index 000000000..efc583cc8
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/GenerateIdFunction.java
@@ -0,0 +1,139 @@
+/* GenerateIdFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+import gnu.xml.xpath.Function;
+
+/**
+ * The XSLT <code>generate-id()</code>function.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class GenerateIdFunction
+ extends Expr
+ implements XPathFunction, Function
+{
+
+ List args;
+
+ public Object evaluate(List args)
+ throws XPathFunctionException
+ {
+ // Useless...
+ return Collections.EMPTY_SET;
+ }
+
+ public void setArguments(List args)
+ {
+ this.args = args;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ int arity = args.size();
+ List values = new ArrayList(arity);
+ for (int i = 0; i < arity; i++)
+ {
+ Expr arg = (Expr) args.get(i);
+ values.add(arg.evaluate(context, pos, len));
+ }
+ Node node;
+ Collection ns = (arity == 0) ? Collections.EMPTY_SET :
+ (Collection) values.get(0);
+ if (ns.isEmpty())
+ {
+ node = context;
+ }
+ else
+ {
+ List list = new ArrayList(ns);
+ Collections.sort(list, documentOrderComparator);
+ node = (Node) list.get(0);
+ }
+
+ String name = node.getNodeName();
+ int index = 0, depth = 0;
+ for (Node ctx = node.getPreviousSibling(); ctx != null;
+ ctx = ctx.getPreviousSibling())
+ {
+ index++;
+ }
+ for (Node ctx = node.getParentNode(); ctx != null;
+ ctx = ctx.getParentNode())
+ {
+ depth++;
+ }
+ return name + "-" + index + "-" + depth;
+ }
+
+ public Expr clone(Object context)
+ {
+ GenerateIdFunction f = new GenerateIdFunction();
+ int len = args.size();
+ List args2 = new ArrayList(len);
+ for (int i = 0; i < len; i++)
+ {
+ args2.add(((Expr) args.get(i)).clone(context));
+ }
+ f.setArguments(args2);
+ return f;
+ }
+
+ public boolean references(QName var)
+ {
+ for (Iterator i = args.iterator(); i.hasNext(); )
+ {
+ if (((Expr) i.next()).references(var))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/IfNode.java b/libjava/classpath/gnu/xml/transform/IfNode.java
new file mode 100644
index 000000000..f31fed555
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/IfNode.java
@@ -0,0 +1,112 @@
+/* IfNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing an XSL <code>if</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class IfNode
+ extends TemplateNode
+{
+
+ final Expr test;
+
+ IfNode(Expr test)
+ {
+ this.test = test;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new IfNode(test.clone(stylesheet));
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ Object ret = test.evaluate(context, pos, len);
+ boolean success = (ret instanceof Boolean) ?
+ ((Boolean) ret).booleanValue() :
+ Expr._boolean(context, ret);
+ if (success)
+ {
+ if (children != null)
+ children.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ public boolean references(QName var)
+ {
+ if (test != null && test.references(var))
+ return true;
+ return super.references(var);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("if");
+ buf.append('[');
+ buf.append("test=");
+ buf.append(test);
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/Key.java b/libjava/classpath/gnu/xml/transform/Key.java
new file mode 100644
index 000000000..18d83394f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/Key.java
@@ -0,0 +1,70 @@
+/* Key.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import javax.xml.namespace.QName;
+import gnu.xml.xpath.Expr;
+import gnu.xml.xpath.Pattern;
+
+/**
+ * An XSL key.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class Key
+{
+
+ final QName name;
+ final Pattern match;
+ final Expr use;
+
+ Key(QName name, Pattern match, Expr use)
+ {
+ this.name = name;
+ this.match = match;
+ this.use = use;
+ }
+
+ Key clone(Stylesheet stylesheet)
+ {
+ return new Key(name,
+ (Pattern) match.clone(stylesheet),
+ use.clone(stylesheet));
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/KeyFunction.java b/libjava/classpath/gnu/xml/transform/KeyFunction.java
new file mode 100644
index 000000000..b962626c9
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/KeyFunction.java
@@ -0,0 +1,227 @@
+/* KeyFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+import gnu.xml.xpath.Function;
+import gnu.xml.xpath.Pattern;
+
+/**
+ * The XSLT <code>key()</code>function.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class KeyFunction
+ extends Pattern
+ implements XPathFunction, Function
+{
+
+ final Stylesheet stylesheet;
+ List args;
+
+ KeyFunction(Stylesheet stylesheet)
+ {
+ this.stylesheet = stylesheet;
+ }
+
+ public Object evaluate(List args)
+ throws XPathFunctionException
+ {
+ // Useless...
+ return Collections.EMPTY_SET;
+ }
+
+ public void setArguments(List args)
+ {
+ this.args = args;
+ }
+
+ public boolean matches(Node context)
+ {
+ Object ret = evaluate(context, 1, 1);
+ return !((Collection) ret).isEmpty();
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ // Evaluate arguments
+ int arity = args.size();
+ List values = new ArrayList(arity);
+ for (int i = 0; i < arity; i++)
+ {
+ Expr arg = (Expr) args.get(i);
+ values.add(arg.evaluate(context, pos, len));
+ }
+ // Get key name
+ QName keyName = QName.valueOf(_string(context, values.get(0)));
+ // Expand qualified name
+ String uri = keyName.getNamespaceURI();
+ String prefix = keyName.getPrefix();
+ if ((uri == null || uri.length() == 0) &&
+ (prefix != null && prefix.length() > 0))
+ {
+ uri = stylesheet.getNamespaceURI(prefix);
+ if (uri != null && uri.length() > 0)
+ {
+ String localName = keyName.getLocalPart();
+ keyName = new QName(uri, localName, prefix);
+ }
+ }
+ // Compute matching key set
+ Collection keySet = new LinkedList();
+ for (Iterator i = stylesheet.keys.iterator(); i.hasNext(); )
+ {
+ Key key = (Key) i.next();
+ if (key.name.equals(keyName))
+ {
+ keySet.add(key);
+ }
+ }
+ // Get target
+ Object target = values.get(1);
+ Collection acc = new LinkedHashSet();
+ Document doc = (context instanceof Document) ? (Document) context :
+ context.getOwnerDocument();
+ if (target instanceof Collection)
+ {
+ for (Iterator i = ((Collection) target).iterator(); i.hasNext(); )
+ {
+ String val = Expr.stringValue((Node) i.next());
+ addKeyNodes(doc, keySet, val, acc);
+ }
+ }
+ else
+ {
+ String val = Expr._string(context, target);
+ addKeyNodes(doc, keySet, val, acc);
+ }
+ List ret = new ArrayList(acc);
+ Collections.sort(ret, documentOrderComparator);
+ return ret;
+ }
+
+ final void addKeyNodes(Node node, Collection keySet,
+ String value, Collection acc)
+ {
+ addKeyNodeIfMatch(node, keySet, value, acc);
+ // Apply children
+ for (Node ctx = node.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ addKeyNodes(ctx, keySet, value, acc);
+ }
+ }
+
+ final void addKeyNodeIfMatch(Node node, Collection keySet,
+ String value, Collection acc)
+ {
+ for (Iterator i = keySet.iterator(); i.hasNext(); )
+ {
+ Key key = (Key) i.next();
+ if (key.match.matches(node))
+ {
+ Object eval = key.use.evaluate(node, 1, 1);
+ if (eval instanceof Collection)
+ {
+ for (Iterator j = ((Collection) eval).iterator();
+ j.hasNext(); )
+ {
+ String keyValue = Expr.stringValue((Node) j.next());
+ if (value.equals(keyValue))
+ {
+ acc.add(node);
+ return;
+ }
+ }
+ }
+ else
+ {
+ String keyValue = Expr._string(node, eval);
+ if (value.equals(keyValue))
+ {
+ acc.add(node);
+ return;
+ }
+ }
+ }
+ }
+ }
+
+ public Expr clone(Object context)
+ {
+ Stylesheet s = stylesheet;
+ if (context instanceof Stylesheet)
+ {
+ s = (Stylesheet) context;
+ }
+ KeyFunction f = new KeyFunction(s);
+ int len = args.size();
+ List args2 = new ArrayList(len);
+ for (int i = 0; i < len; i++)
+ {
+ args2.add(((Expr) args.get(i)).clone(context));
+ }
+ f.setArguments(args2);
+ return f;
+ }
+
+ public boolean references(QName var)
+ {
+ for (Iterator i = args.iterator(); i.hasNext(); )
+ {
+ if (((Expr) i.next()).references(var))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/LiteralNode.java b/libjava/classpath/gnu/xml/transform/LiteralNode.java
new file mode 100644
index 000000000..3405b3364
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/LiteralNode.java
@@ -0,0 +1,202 @@
+/* LiteralNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.StringTokenizer;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+/**
+ * A template node that copies a DOM node in the template to the result
+ * tree.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class LiteralNode
+ extends TemplateNode
+{
+
+ /**
+ * The source node in the XSL template.
+ */
+ final Node source;
+
+ final Collection elementExcludeResultPrefixes;
+
+ LiteralNode(Node source)
+ {
+ this.source = source;
+ if (source.getNodeType() == Node.ELEMENT_NODE)
+ {
+ NamedNodeMap attrs = source.getAttributes();
+ Node attr = attrs.getNamedItemNS(Stylesheet.XSL_NS,
+ "exclude-result-prefixes");
+ if (attr != null)
+ {
+ elementExcludeResultPrefixes = new HashSet();
+ StringTokenizer st = new StringTokenizer(attr.getNodeValue());
+ while (st.hasMoreTokens())
+ elementExcludeResultPrefixes.add(st.nextToken());
+ }
+ else
+ elementExcludeResultPrefixes = Collections.EMPTY_SET;
+ }
+ else
+ elementExcludeResultPrefixes = null;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new LiteralNode(source);
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ Node result = null;
+ Document doc = (parent instanceof Document) ? (Document) parent :
+ parent.getOwnerDocument();
+ short nodeType = source.getNodeType();
+ if (nodeType == Node.ATTRIBUTE_NODE &&
+ parent.getFirstChild() != null)
+ {
+ // Ignore attributes added after child elements
+ }
+ else
+ {
+ // Namespace aliasing
+ if (nodeType == Node.ELEMENT_NODE)
+ {
+ String prefix = source.getPrefix();
+ if (prefix == null)
+ prefix = "#default";
+ String resultPrefix =
+ (String) stylesheet.namespaceAliases.get(prefix);
+ if (resultPrefix != null)
+ {
+ if ("#default".equals(resultPrefix))
+ resultPrefix = null;
+ String uri = source.lookupNamespaceURI(resultPrefix);
+ String name = source.getNodeName();
+ // Create a new element node in the result document
+ result = doc.createElementNS(uri, name);
+ // copy attributes
+ NamedNodeMap srcAttrs = source.getAttributes();
+ NamedNodeMap dstAttrs = result.getAttributes();
+ int l = srcAttrs.getLength();
+ for (int i = 0; i < l; i++)
+ {
+ Node attr = srcAttrs.item(i);
+ if (!Stylesheet.XSL_NS.equals(attr.getNamespaceURI()))
+ {
+ attr = attr.cloneNode(true);
+ attr = doc.adoptNode(attr);
+ dstAttrs.setNamedItemNS(attr);
+ }
+ }
+ }
+ }
+ if (result == null)
+ {
+ // Create result node
+ result = source.cloneNode(false);
+ // Remove any XSL attributes
+ NamedNodeMap attrs = result.getAttributes();
+ if (attrs != null)
+ {
+ int l = attrs.getLength();
+ for (int i = 0; i < l; i++)
+ {
+ Node attr = attrs.item(i);
+ if (Stylesheet.XSL_NS.equals(attr.getNamespaceURI()))
+ {
+ attrs.removeNamedItem(attr.getNodeName());
+ i--;
+ l--;
+ }
+ }
+ }
+ Node result2 = doc.adoptNode(result);
+ if (result2 == null)
+ {
+ String msg = "Error adopting node to result tree: " +
+ result + " (" + result.getClass().getName() + ")";
+ DOMSourceLocator l = new DOMSourceLocator(context);
+ throw new TransformerException(msg, l);
+ }
+ result = result2;
+ }
+ if (nextSibling != null)
+ parent.insertBefore(result, nextSibling);
+ else
+ parent.appendChild(result);
+ if (nodeType == Node.ELEMENT_NODE)
+ stylesheet.addNamespaceNodes(source, result, doc,
+ elementExcludeResultPrefixes);
+ // children
+ if (children != null)
+ children.apply(stylesheet, mode,
+ context, pos, len,
+ result, null);
+ }
+ // next sibling
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ public String toString()
+ {
+ return source.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/MessageNode.java b/libjava/classpath/gnu/xml/transform/MessageNode.java
new file mode 100644
index 000000000..3e4899aeb
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/MessageNode.java
@@ -0,0 +1,110 @@
+/* MessageNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.logging.Logger;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * An XSL <code>message</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class MessageNode
+ extends TemplateNode
+{
+
+ static final Logger logger = Logger.getLogger("gnu.xml.transform");
+
+ final boolean terminate;
+
+ MessageNode(boolean terminate)
+ {
+ this.terminate = terminate;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new MessageNode(terminate);
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ if (children != null)
+ {
+ Document doc = (parent instanceof Document) ? (Document) parent :
+ parent.getOwnerDocument();
+ DocumentFragment fragment = doc.createDocumentFragment();
+ children.apply(stylesheet, mode, context, pos, len, fragment, null);
+ String message = Expr.stringValue(fragment);
+ logger.info(message);
+ if (terminate)
+ stylesheet.terminated = true;
+ }
+ if (next != null && !terminate)
+ next.apply(stylesheet, mode, context, pos, len, parent, nextSibling);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("message");
+ if (terminate)
+ {
+ buf.append('[');
+ buf.append("terminate");
+ buf.append(']');
+ }
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/NamespaceProxy.java b/libjava/classpath/gnu/xml/transform/NamespaceProxy.java
new file mode 100644
index 000000000..695fbeb52
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/NamespaceProxy.java
@@ -0,0 +1,77 @@
+/* NamespaceProxy.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.util.Collections;
+import java.util.Iterator;
+import javax.xml.namespace.NamespaceContext;
+import org.w3c.dom.Node;
+
+/**
+ * A namespace context using a DOM node to resolve the namespace.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class NamespaceProxy
+ implements NamespaceContext
+{
+
+ private final Node node;
+
+ NamespaceProxy(Node node)
+ {
+ this.node = node;
+ }
+
+ public String getNamespaceURI(String prefix)
+ {
+ return (node == null) ? null : node.lookupNamespaceURI(prefix);
+ }
+
+ public String getPrefix(String namespaceURI)
+ {
+ return (node == null) ? null : node.lookupPrefix(namespaceURI);
+ }
+
+ public Iterator getPrefixes(String namespaceURI)
+ {
+ // TODO
+ return Collections.singleton(getPrefix(namespaceURI)).iterator();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/NodeNumberNode.java b/libjava/classpath/gnu/xml/transform/NodeNumberNode.java
new file mode 100644
index 000000000..6663db371
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/NodeNumberNode.java
@@ -0,0 +1,269 @@
+/* NodeNumberNode.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+import gnu.xml.xpath.Pattern;
+import gnu.xml.xpath.Selector;
+import gnu.xml.xpath.UnionExpr;
+
+/**
+ * A template node representing the XSL <code>number</code> instruction
+ * with no <code>value</code> expression, i.e. the value is computed from
+ * the document position of the context node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NodeNumberNode
+ extends AbstractNumberNode
+{
+
+ static final int SINGLE = 0;
+ static final int MULTIPLE = 1;
+ static final int ANY = 2;
+
+ final int level;
+ final Pattern count;
+ final Pattern from;
+
+ NodeNumberNode(int level, Pattern count, Pattern from,
+ TemplateNode format, String lang,
+ int letterValue, String groupingSeparator, int groupingSize)
+ {
+ super(format, lang, letterValue, groupingSeparator, groupingSize);
+ this.level = level;
+ this.count = count;
+ this.from = from;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new NodeNumberNode(level,
+ (count == null) ? null :
+ (Pattern) count.clone(stylesheet),
+ (from == null) ? from :
+ (Pattern) from.clone(stylesheet),
+ format, lang, letterValue,
+ groupingSeparator, groupingSize);
+ if (children != null)
+ {
+ ret.children = children.clone(stylesheet);
+ }
+ if (next != null)
+ {
+ ret.next = next.clone(stylesheet);
+ }
+ return ret;
+ }
+
+ int[] compute(Stylesheet stylesheet, Node context, int pos, int len)
+ throws TransformerException
+ {
+ /*if (from != null)
+ {
+ Object ret = from.evaluate(context, pos, len);
+ if (ret instanceof Collection)
+ {
+ Collection ns = (Collection) ret;
+ if (ns.size() > 0)
+ {
+ List list = new ArrayList(ns);
+ Collections.sort(list, documentOrderComparator);
+ context = (Node) list.get(0);
+ }
+ else
+ {
+ return new int[0];
+ }
+ }
+ else
+ {
+ return new int[0];
+ }
+ }*/
+ Node current = context;
+ switch (level)
+ {
+ case SINGLE:
+ if (from == null)
+ {
+ while (context != null && !countMatches(current, context))
+ {
+ context = context.getParentNode();
+ }
+ }
+ else
+ {
+ while (context != null && !countMatches(current, context) &&
+ !fromMatches(context))
+ {
+ context = context.getParentNode();
+ }
+ }
+ return (context == null) ? new int[0] :
+ new int[] { (context == current) ? pos : getIndex(current, context) };
+ case MULTIPLE:
+ List ancestors = new ArrayList();
+ while (context != null)
+ {
+ if (countMatches(current, context))
+ {
+ if (from == null || fromMatches(context))
+ {
+ ancestors.add(context);
+ }
+ }
+ context = context.getParentNode();
+ }
+ Collections.sort(ancestors, documentOrderComparator);
+ int[] ret = new int[ancestors.size()];
+ for (int i = 0; i < ret.length; i++)
+ {
+ ret[i] = getIndex(current, (Node) ancestors.get(i));
+ }
+ return ret;
+ case ANY:
+ Expr preceding = new Selector(Selector.PRECEDING,
+ Collections.EMPTY_LIST);
+ Expr ancestorOrSelf = new Selector(Selector.ANCESTOR_OR_SELF,
+ Collections.EMPTY_LIST);
+ Expr any = new UnionExpr(preceding, ancestorOrSelf);
+ Object eval = any.evaluate(context, pos, len);
+ if (eval instanceof Collection)
+ {
+ Collection ns = (Collection) eval;
+ List candidates = new ArrayList();
+ for (Iterator i = ns.iterator(); i.hasNext(); )
+ {
+ Node candidate = (Node) i.next();
+ if (countMatches(current, candidate))
+ {
+ candidates.add(candidate);
+ if (from != null && from.matches(candidate))
+ {
+ break;
+ }
+ }
+ }
+ return new int[] { candidates.size() };
+ }
+ return new int[0];
+ default:
+ throw new TransformerException("invalid level");
+ }
+ }
+
+ boolean countMatches(Node current, Node node)
+ {
+ if (count == null)
+ {
+ int cnt = current.getNodeType();
+ int nnt = node.getNodeType();
+ if (cnt != nnt)
+ {
+ return false;
+ }
+ if (nnt == Node.ELEMENT_NODE || nnt == Node.ATTRIBUTE_NODE)
+ {
+ String curi = current.getNamespaceURI();
+ String nuri = node.getNamespaceURI();
+ if ((curi == null && nuri != null) ||
+ (curi != null && !curi.equals(nuri)))
+ {
+ return false;
+ }
+ String cn = current.getLocalName();
+ if (cn == null)
+ {
+ cn = current.getNodeName();
+ }
+ String nn = node.getLocalName();
+ if (nn == null)
+ {
+ nn = node.getNodeName();
+ }
+ if (!cn.equals(nn))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+ else
+ {
+ return count.matches(node);
+ }
+ }
+
+ boolean fromMatches(Node node)
+ {
+ for (Node ctx = node.getParentNode(); ctx != null;
+ ctx = ctx.getParentNode())
+ {
+ if (from.matches(ctx))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ int getIndex(Node current, Node node)
+ {
+ int index = 0;
+ do
+ {
+ do
+ {
+ node = node.getPreviousSibling();
+ }
+ while (node != null && !countMatches(current, node));
+ index++;
+ }
+ while (node != null);
+ return index;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/NumberNode.java b/libjava/classpath/gnu/xml/transform/NumberNode.java
new file mode 100644
index 000000000..cec594b99
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/NumberNode.java
@@ -0,0 +1,88 @@
+/* NumberNode.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing the XSL <code>number</code> instruction
+ * with a <code>value</code> expression.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NumberNode
+ extends AbstractNumberNode
+{
+
+ final Expr value;
+
+ NumberNode(Expr value, TemplateNode format, String lang,
+ int letterValue, String groupingSeparator, int groupingSize)
+ {
+ super(format, lang, letterValue, groupingSeparator, groupingSize);
+ this.value = value;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new NumberNode(value.clone(stylesheet),
+ format, lang, letterValue,
+ groupingSeparator, groupingSize);
+ if (children != null)
+ {
+ ret.children = children.clone(stylesheet);
+ }
+ if (next != null)
+ {
+ ret.next = next.clone(stylesheet);
+ }
+ return ret;
+ }
+
+ int[] compute(Stylesheet stylesheet, Node context, int pos, int len)
+ throws TransformerException
+ {
+ Object ret = value.evaluate(context, pos, len);
+ Double d = (ret instanceof Double) ? ((Double) ret) :
+ new Double(Expr._number(context, ret));
+ return new int[] { d.intValue() };
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/OtherwiseNode.java b/libjava/classpath/gnu/xml/transform/OtherwiseNode.java
new file mode 100644
index 000000000..09057840d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/OtherwiseNode.java
@@ -0,0 +1,83 @@
+/* OtherwiseNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Node;
+
+/**
+ * A template node representing an XSL <code>otherwise</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class OtherwiseNode
+ extends TemplateNode
+{
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new OtherwiseNode();
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ if (children != null)
+ children.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ public String toString()
+ {
+ return "otherwise";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/ParameterNode.java b/libjava/classpath/gnu/xml/transform/ParameterNode.java
new file mode 100644
index 000000000..4f3dc7569
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/ParameterNode.java
@@ -0,0 +1,172 @@
+/* ParameterNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.Collections;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node that sets a variable or parameter during template
+ * processing.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class ParameterNode
+ extends TemplateNode
+ implements Comparable<ParameterNode>
+{
+
+ final QName name;
+ final Expr select;
+ final int type;
+
+ ParameterNode(QName name, Expr select, int type)
+ {
+ this.name = name;
+ this.select = select;
+ this.type = type;
+ }
+
+ @Override
+ ParameterNode clone(Stylesheet stylesheet)
+ {
+ ParameterNode ret = new ParameterNode(name,
+ select == null ? null : select.clone(stylesheet),
+ type);
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ // push the variable context
+ stylesheet.bindings.push(type);
+ // set the variable
+ Object value = getValue(stylesheet, mode, context, pos, len);
+ if (value != null)
+ {
+ stylesheet.bindings.set(name, value, type);
+ if (stylesheet.debug)
+ System.err.println(this + ": set to " + value);
+ }
+ // variable and param don't process children as such
+ // all subsequent instructions are processed with that variable context
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ // pop the variable context
+ stylesheet.bindings.pop(type);
+ }
+
+ Object getValue(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len)
+ throws TransformerException
+ {
+ if (select != null)
+ return select.evaluate(context, pos, len);
+ else if (children != null)
+ {
+ Document doc = (context instanceof Document) ? (Document) context :
+ context.getOwnerDocument();
+ DocumentFragment fragment = doc.createDocumentFragment();
+ children.apply(stylesheet, mode, context, pos, len, fragment, null);
+ return Collections.singleton(fragment);
+ }
+ else
+ return null;
+ }
+
+ public boolean references(QName var)
+ {
+ if (select != null && select.references(var))
+ return true;
+ return super.references(var);
+ }
+
+ public int compareTo(ParameterNode pn)
+ {
+ boolean r1 = references(pn.name);
+ boolean r2 = pn.references(name);
+ if (r1 && r2)
+ throw new IllegalArgumentException("circular definitions");
+ if (r1)
+ return 1;
+ if (r2)
+ return -1;
+ return 0;
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ switch (type)
+ {
+ case Bindings.VARIABLE:
+ buf.append("variable");
+ break;
+ case Bindings.PARAM:
+ buf.append("param");
+ break;
+ case Bindings.WITH_PARAM:
+ buf.append("with-param");
+ break;
+ }
+ buf.append('[');
+ buf.append("name=");
+ buf.append(name);
+ buf.append(",select=");
+ buf.append(select);
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/ProcessingInstructionNode.java b/libjava/classpath/gnu/xml/transform/ProcessingInstructionNode.java
new file mode 100644
index 000000000..1e723a8d4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/ProcessingInstructionNode.java
@@ -0,0 +1,118 @@
+/* ProcessingInstructionNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Node;
+import org.w3c.dom.ProcessingInstruction;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing the XSL <code>processing-instruction</code>
+ * instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class ProcessingInstructionNode
+ extends TemplateNode
+{
+
+ final String name;
+
+ ProcessingInstructionNode(String name)
+ {
+ this.name = name;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new ProcessingInstructionNode(name);
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ String data = null;
+ Document doc = (parent instanceof Document) ? (Document) parent :
+ parent.getOwnerDocument();
+ if (children != null)
+ {
+ // Create a document fragment to hold the text
+ DocumentFragment fragment = doc.createDocumentFragment();
+ // Apply children to the fragment
+ children.apply(stylesheet, mode,
+ context, pos, len,
+ fragment, null);
+ // Use XPath string-value of fragment
+ data = Expr.stringValue(fragment);
+ }
+ ProcessingInstruction pi = doc.createProcessingInstruction(name, data);
+ // Insert into result tree
+ if (nextSibling != null)
+ parent.insertBefore(pi, nextSibling);
+ else
+ parent.appendChild(pi);
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("processing-instruction");
+ buf.append('[');
+ buf.append("name=");
+ buf.append(name);
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/SAXSerializer.java b/libjava/classpath/gnu/xml/transform/SAXSerializer.java
new file mode 100644
index 000000000..2bd1f97ab
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/SAXSerializer.java
@@ -0,0 +1,305 @@
+/* SAXSerializer.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import org.w3c.dom.Attr;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.ext.LexicalHandler;
+
+/**
+ * Serializes a DOM node to a sequence of SAX events.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class SAXSerializer
+ implements Attributes
+{
+
+ transient NamedNodeMap attrs;
+ transient LinkedList namespaces = new LinkedList();
+
+ boolean isDefined(String prefix, String uri)
+ {
+ for (Iterator i = namespaces.iterator(); i.hasNext(); )
+ {
+ HashMap ctx = (HashMap) i.next();
+ if (uri.equals(ctx.get(prefix)))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ void define(String prefix, String uri)
+ {
+ for (Iterator i = namespaces.iterator(); i.hasNext(); )
+ {
+ HashMap ctx = (HashMap) i.next();
+ if (ctx.containsKey(prefix))
+ {
+ HashMap newCtx = new HashMap();
+ newCtx.put(prefix, uri);
+ namespaces.addFirst(newCtx);
+ return;
+ }
+ }
+ HashMap ctx;
+ if (namespaces.isEmpty())
+ {
+ ctx = new HashMap();
+ namespaces.add(ctx);
+ }
+ else
+ {
+ ctx = (HashMap) namespaces.getFirst();
+ }
+ ctx.put(prefix, uri);
+ }
+
+ void undefine(String prefix, String uri)
+ {
+ for (Iterator i = namespaces.iterator(); i.hasNext(); )
+ {
+ HashMap ctx = (HashMap) i.next();
+ if (uri.equals(ctx.get(prefix)))
+ {
+ ctx.remove(prefix);
+ if (ctx.isEmpty())
+ {
+ namespaces.remove(ctx);
+ }
+ return;
+ }
+ }
+ }
+
+ public int getLength()
+ {
+ return attrs.getLength();
+ }
+
+ public String getURI(int index)
+ {
+ return attrs.item(index).getNamespaceURI();
+ }
+
+ public String getLocalName(int index)
+ {
+ return attrs.item(index).getLocalName();
+ }
+
+ public String getQName(int index)
+ {
+ return attrs.item(index).getNodeName();
+ }
+
+ public String getType(int index)
+ {
+ Attr attr = (Attr) attrs.item(index);
+ return attr.isId() ? "ID" : "CDATA";
+ }
+
+ public String getValue(int index)
+ {
+ return attrs.item(index).getNodeValue();
+ }
+
+ public int getIndex(String uri, String localName)
+ {
+ int len = attrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Node attr = attrs.item(i);
+ String a_uri = attr.getNamespaceURI();
+ String a_localName = attr.getLocalName();
+ if (((a_uri == null && uri == null) ||
+ (a_uri != null && a_uri.equals(uri))) &&
+ a_localName.equals(localName))
+ {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ public int getIndex(String qName)
+ {
+ int len = attrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Node attr = attrs.item(i);
+ String a_name = attr.getNodeName();
+ if (a_name.equals(qName))
+ {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ public String getType(String uri, String localName)
+ {
+ Attr attr = (Attr) attrs.getNamedItemNS(uri, localName);
+ return attr.isId() ? "ID" : "CDATA";
+ }
+
+ public String getType(String qName)
+ {
+ Attr attr = (Attr) attrs.getNamedItem(qName);
+ return attr.isId() ? "ID" : "CDATA";
+ }
+
+ public String getValue(String uri, String localName)
+ {
+ return attrs.getNamedItemNS(uri, localName).getNodeValue();
+ }
+
+ public String getValue(String qName)
+ {
+ return attrs.getNamedItem(qName).getNodeValue();
+ }
+
+ void serialize(Node node, ContentHandler ch, LexicalHandler lh)
+ throws SAXException
+ {
+ attrs = node.getAttributes();
+ Node children;
+ Node next = node.getNextSibling();
+ switch (node.getNodeType())
+ {
+ case Node.ELEMENT_NODE:
+ String uri = node.getNamespaceURI();
+ String prefix = node.getPrefix();
+ boolean defined = isDefined(prefix, uri);
+ if (!defined)
+ {
+ define(prefix, uri);
+ ch.startPrefixMapping(prefix, uri);
+ }
+ String localName = node.getLocalName();
+ String qName = node.getNodeName();
+ ch.startElement(uri, localName, qName, this);
+ children = node.getFirstChild();
+ if (children != null)
+ {
+ serialize(children, ch, lh);
+ }
+ ch.endElement(uri, localName, qName);
+ if (!defined)
+ {
+ ch.endPrefixMapping(prefix);
+ undefine(prefix, uri);
+ }
+ break;
+ case Node.TEXT_NODE:
+ char[] chars = node.getNodeValue().toCharArray();
+ ch.characters(chars, 0, chars.length);
+ break;
+ case Node.CDATA_SECTION_NODE:
+ char[] cdata = node.getNodeValue().toCharArray();
+ if (lh != null)
+ {
+ lh.startCDATA();
+ ch.characters(cdata, 0, cdata.length);
+ lh.endCDATA();
+ }
+ else
+ {
+ ch.characters(cdata, 0, cdata.length);
+ }
+ break;
+ case Node.COMMENT_NODE:
+ if (lh != null)
+ {
+ char[] comment = node.getNodeValue().toCharArray();
+ lh.comment(comment, 0, comment.length);
+ }
+ break;
+ case Node.DOCUMENT_NODE:
+ case Node.DOCUMENT_FRAGMENT_NODE:
+ ch.startDocument();
+ children = node.getFirstChild();
+ if (children != null)
+ {
+ serialize(children, ch, lh);
+ }
+ ch.endDocument();
+ break;
+ case Node.DOCUMENT_TYPE_NODE:
+ if (lh != null)
+ {
+ DocumentType doctype = (DocumentType) node;
+ String publicId = doctype.getPublicId();
+ String systemId = doctype.getSystemId();
+ lh.startDTD(node.getNodeName(), publicId, systemId);
+ NamedNodeMap entities = doctype.getEntities();
+ int len = entities.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Node entity = entities.item(i);
+ String entityName = entity.getNodeName();
+ lh.startEntity(entityName);
+ lh.endEntity(entityName);
+ }
+ lh.endDTD();
+ }
+ break;
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ ch.processingInstruction(node.getNodeName(), node.getNodeValue());
+ break;
+ case Node.ENTITY_REFERENCE_NODE:
+ ch.skippedEntity(node.getNodeName());
+ break;
+ }
+ attrs = null;
+ if (next != null)
+ {
+ serialize(next, ch, lh);
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/SAXTemplatesHandler.java b/libjava/classpath/gnu/xml/transform/SAXTemplatesHandler.java
new file mode 100644
index 000000000..24464c067
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/SAXTemplatesHandler.java
@@ -0,0 +1,97 @@
+/* SAXTemplatesHandler.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import javax.xml.transform.Templates;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.TemplatesHandler;
+import org.w3c.dom.Document;
+import gnu.xml.dom.ls.SAXEventSink;
+
+/**
+ * A content handler that acts as a sink for SAX parse events,
+ * constructing an XSL stylesheet.
+ * Internally, this class simply creates a DOM tree from the events,
+ * and then parses the DOM into a Templates object.
+ *
+ * @author Chris Burdess (dog@gnu.org)
+ */
+class SAXTemplatesHandler
+ extends SAXEventSink
+ implements TemplatesHandler
+{
+
+ final TransformerFactoryImpl factory;
+ String systemId;
+
+ SAXTemplatesHandler(TransformerFactoryImpl factory)
+ {
+ this.factory = factory;
+ }
+
+ public String getSystemId()
+ {
+ return systemId;
+ }
+
+ public void setSystemId(String systemId)
+ {
+ this.systemId = systemId;
+ }
+
+ public Templates getTemplates()
+ {
+ Document doc = getDocument();
+ if (doc == null)
+ throw new IllegalStateException("Parsing of stylesheet incomplete");
+ DOMSource ds = new DOMSource(doc, systemId);
+ try
+ {
+ return factory.newTemplates(ds);
+ }
+ catch (TransformerConfigurationException e)
+ {
+ String msg = "Unable to construct templates from this event stream";
+ IllegalStateException e2 = new IllegalStateException(msg);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/SAXTransformerHandler.java b/libjava/classpath/gnu/xml/transform/SAXTransformerHandler.java
new file mode 100644
index 000000000..890e15100
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/SAXTransformerHandler.java
@@ -0,0 +1,111 @@
+/* SAXTransformerHandler.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.TransformerHandler;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+import gnu.xml.dom.ls.SAXEventSink;
+
+/**
+ * A SAX event sink that processes an XML source represented as a stream of
+ * SAX events into a result tree.
+ * This works by simply buffering all the events into a DOM tree and then
+ * using this DOM tree as the source of the transformation.
+ *
+ * @author Chris Burdess (dog@gnu.org)
+ */
+class SAXTransformerHandler
+ extends SAXEventSink
+ implements TransformerHandler
+{
+
+ final TransformerFactoryImpl factory;
+ final Transformer transformer;
+ String systemId;
+ Result result;
+
+ SAXTransformerHandler(TransformerFactoryImpl factory, Transformer transformer)
+ {
+ this.factory = factory;
+ this.transformer = transformer;
+ }
+
+ public String getSystemId()
+ {
+ return systemId;
+ }
+
+ public void setSystemId(String systemId)
+ {
+ this.systemId = systemId;
+ }
+
+ public Transformer getTransformer()
+ {
+ return transformer;
+ }
+
+ public void setResult(Result result)
+ {
+ this.result = result;
+ }
+
+ public void endDocument()
+ throws SAXException
+ {
+ super.endDocument();
+ try
+ {
+ Document doc = getDocument();
+ DOMSource ds = new DOMSource(doc, systemId);
+ transformer.transform(ds, result);
+ }
+ catch (TransformerException e)
+ {
+ SAXException e2 = new SAXException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/SortKey.java b/libjava/classpath/gnu/xml/transform/SortKey.java
new file mode 100644
index 000000000..dd57df986
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/SortKey.java
@@ -0,0 +1,239 @@
+/* SortKey.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * <p>
+ * An XSL sort key, as specified by section 10 of the XSL
+ * Transformations specification. This takes the form:
+ * </p>
+ * <pre>
+ * &lt;xsl:sort
+ * select = string-expression
+ * lang = { nmtoken }
+ * data-type = { "text" | "number" | qname-but-not-ncname }
+ * order = { "ascending" | "descending" }
+ * case-order = { "upper-first" | "lower-first" } /&rt;
+ * </pre>
+ * <p>
+ * Note that all but the selection expression are optional,
+ * and so may be {@code null}.
+ * </p>
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class SortKey
+{
+
+ static final int DEFAULT = 0;
+ static final int UPPER_FIRST = 1;
+ static final int LOWER_FIRST = 2;
+
+ final Expr select;
+ final TemplateNode langTemplate;
+ final TemplateNode dataTypeTemplate;
+ final TemplateNode orderTemplate;
+ final TemplateNode caseOrderTemplate;
+
+ transient String lang;
+ transient String dataType;
+ transient boolean descending;
+ transient int caseOrder;
+
+ /**
+ * Constructs a new {@link SortKey} to represent an &lt;xsl:sort&rt;
+ * tag.
+ *
+ * @param select the XPath expression which selects the nodes to be sorted.
+ * @param lang the language of the sort keys or {@code null} if unspecified.
+ * @param dataType the data type of the strings. May be "string", "number",
+ * a QName or {@code null} if unspecified.
+ * @param order the ordering of the nodes, which may be "ascending", "descending"
+ * or {@code null} if unspecified.
+ * @param caseOrder the treatment of case when the data type is a string. This
+ * may be "upper-first", "lower-first" or {@code null} if
+ * unspecified.
+ */
+ SortKey(Expr select, TemplateNode lang, TemplateNode dataType,
+ TemplateNode order, TemplateNode caseOrder)
+ {
+ this.select = select;
+ this.langTemplate = lang;
+ this.dataTypeTemplate = dataType;
+ this.orderTemplate = order;
+ this.caseOrderTemplate = caseOrder;
+ }
+
+ String key(Node node)
+ {
+ Object ret = select.evaluate(node, 1, 1);
+ if (ret instanceof String)
+ {
+ return (String) ret;
+ }
+ else
+ {
+ return Expr._string(node, ret);
+ }
+ }
+
+ /**
+ * Prepare for a sort.
+ * This sets all transient variables from their AVTs.
+ */
+ void init(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ Document doc = (context instanceof Document) ? (Document) context :
+ context.getOwnerDocument();
+ if (langTemplate == null)
+ {
+ lang = null;
+ }
+ else
+ {
+ DocumentFragment fragment = doc.createDocumentFragment();
+ langTemplate.apply(stylesheet, mode, context, pos, len,
+ fragment, null);
+ lang = Expr.stringValue(fragment);
+ }
+ if (dataTypeTemplate == null)
+ {
+ dataType = "text";
+ }
+ else
+ {
+ DocumentFragment fragment = doc.createDocumentFragment();
+ dataTypeTemplate.apply(stylesheet, mode, context, pos, len,
+ fragment, null);
+ dataType = Expr.stringValue(fragment);
+ }
+ if (orderTemplate == null)
+ {
+ descending = false;
+ }
+ else
+ {
+ DocumentFragment fragment = doc.createDocumentFragment();
+ orderTemplate.apply(stylesheet, mode, context, pos, len,
+ fragment, null);
+ String order = Expr.stringValue(fragment);
+ descending = "descending".equals(order);
+ }
+ if (caseOrderTemplate == null)
+ {
+ caseOrder = DEFAULT;
+ }
+ else
+ {
+ DocumentFragment fragment = doc.createDocumentFragment();
+ caseOrderTemplate.apply(stylesheet, mode, context, pos, len,
+ fragment, null);
+ String co = Expr.stringValue(fragment);
+ caseOrder = "upper-first".equals(co) ? UPPER_FIRST :
+ "lower-first".equals(co) ? LOWER_FIRST :
+ DEFAULT;
+ }
+ }
+
+ boolean references(QName var)
+ {
+ if (select != null && select.references(var))
+ {
+ return true;
+ }
+ if (langTemplate != null && langTemplate.references(var))
+ {
+ return true;
+ }
+ if (dataTypeTemplate != null && dataTypeTemplate.references(var))
+ {
+ return true;
+ }
+ if (orderTemplate != null && orderTemplate.references(var))
+ {
+ return true;
+ }
+ if (caseOrderTemplate != null && caseOrderTemplate.references(var))
+ {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Provides a clone of this {@link SortKey}, using the given
+ * stylesheet as a context.
+ *
+ * @param stylesheet the stylesheet which provides context for the cloning.
+ * @return a clone of this instance.
+ */
+ SortKey clone(Stylesheet stylesheet)
+ {
+ return new SortKey(select.clone(stylesheet),
+ langTemplate == null ? null : cloneAttributeValueTemplate(langTemplate, stylesheet),
+ dataTypeTemplate == null ? null : cloneAttributeValueTemplate(dataTypeTemplate, stylesheet),
+ orderTemplate == null ? null : cloneAttributeValueTemplate(orderTemplate, stylesheet),
+ caseOrderTemplate == null ? null : cloneAttributeValueTemplate(caseOrderTemplate, stylesheet));
+ }
+
+ /**
+ * Clones an attribute value template as created by
+ * {@link Stylesheet#parseAttributeValueTemplate(String, Node)}.
+ * The node may either by a literal node or an xsl:value-of expression.
+ *
+ * @param node the node to clone.
+ * @param stylesheet the stylesheet which provides context for the cloning.
+ * @return the cloned node.
+ */
+ private TemplateNode cloneAttributeValueTemplate(TemplateNode node, Stylesheet stylesheet)
+ {
+ if (node instanceof ValueOfNode)
+ return ((ValueOfNode) node).clone(stylesheet);
+ return ((LiteralNode) node).clone(stylesheet);
+ }
+}
diff --git a/libjava/classpath/gnu/xml/transform/StreamSerializer.java b/libjava/classpath/gnu/xml/transform/StreamSerializer.java
new file mode 100644
index 000000000..a5705f891
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/StreamSerializer.java
@@ -0,0 +1,854 @@
+/* StreamSerializer.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Map;
+import javax.xml.XMLConstants;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+/**
+ * Serializes a DOM node to an output stream.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class StreamSerializer
+{
+
+ static final int SPACE = 0x20;
+ static final int BANG = 0x21; // !
+ static final int APOS = 0x27; // '
+ static final int SLASH = 0x2f; // /
+ static final int BRA = 0x3c; // <
+ static final int KET = 0x3e; // >
+ static final int EQ = 0x3d; // =
+
+ /**
+ * HTML 4.01 boolean attributes
+ */
+ static final Map HTML_BOOLEAN_ATTRIBUTES = new HashMap();
+ static
+ {
+ HashSet set;
+
+ set = new HashSet();
+ set.add("nohref");
+ HTML_BOOLEAN_ATTRIBUTES.put("area", set);
+
+ set = new HashSet();
+ set.add("ismap");
+ HTML_BOOLEAN_ATTRIBUTES.put("img", set);
+
+ set = new HashSet();
+ set.add("declare");
+ HTML_BOOLEAN_ATTRIBUTES.put("object", set);
+
+ set = new HashSet();
+ set.add("noshade");
+ HTML_BOOLEAN_ATTRIBUTES.put("hr", set);
+
+ set = new HashSet();
+ set.add("compact");
+ HTML_BOOLEAN_ATTRIBUTES.put("dl", set);
+ HTML_BOOLEAN_ATTRIBUTES.put("ol", set);
+ HTML_BOOLEAN_ATTRIBUTES.put("ul", set);
+ HTML_BOOLEAN_ATTRIBUTES.put("dir", set);
+ HTML_BOOLEAN_ATTRIBUTES.put("menu", set);
+
+ set = new HashSet();
+ set.add("checked");
+ set.add("disabled");
+ set.add("readonly");
+ set.add("ismap");
+ HTML_BOOLEAN_ATTRIBUTES.put("input", set);
+
+ set = new HashSet();
+ set.add("multiple");
+ set.add("disabled");
+ HTML_BOOLEAN_ATTRIBUTES.put("select", set);
+
+ set = new HashSet();
+ set.add("disabled");
+ HTML_BOOLEAN_ATTRIBUTES.put("optgroup", set);
+
+ set = new HashSet();
+ set.add("selected");
+ set.add("disabled");
+ HTML_BOOLEAN_ATTRIBUTES.put("option", set);
+
+ set = new HashSet();
+ set.add("disabled");
+ set.add("readonly");
+ HTML_BOOLEAN_ATTRIBUTES.put("textarea", set);
+
+ set = new HashSet();
+ set.add("disabled");
+ HTML_BOOLEAN_ATTRIBUTES.put("button", set);
+
+ set = new HashSet();
+ set.add("nowrap");
+ HTML_BOOLEAN_ATTRIBUTES.put("th", set);
+ HTML_BOOLEAN_ATTRIBUTES.put("td", set);
+
+ set = new HashSet();
+ set.add("noresize");
+ HTML_BOOLEAN_ATTRIBUTES.put("frame", set);
+
+ set = new HashSet();
+ set.add("defer");
+ HTML_BOOLEAN_ATTRIBUTES.put("script", set);
+ }
+
+ // HTML namespace URIs
+ static final HashSet HTML_URIS = new HashSet();
+ static {
+ HTML_URIS.add("http://www.w3.org/1999/xhtml");
+ }
+
+ protected final String encoding;
+ final Charset charset;
+ final CharsetEncoder encoder;
+ final int mode;
+ final LinkedList namespaces;
+ protected String eol;
+ Collection cdataSectionElements = Collections.EMPTY_SET;
+
+ protected boolean discardDefaultContent;
+ protected boolean xmlDeclaration = true;
+
+ // has a META element with the encoding been added?
+ private boolean htmlEncoded;
+
+ public StreamSerializer()
+ {
+ this(Stylesheet.OUTPUT_XML, null, null);
+ }
+
+ public StreamSerializer(String encoding)
+ {
+ this(Stylesheet.OUTPUT_XML, encoding, null);
+ }
+
+ public StreamSerializer(int mode, String encoding, String eol)
+ {
+ this.mode = mode;
+ if (encoding == null)
+ encoding = (mode == Stylesheet.OUTPUT_HTML) ? "ISO-8859-1" : "UTF-8";
+ this.encoding = encoding.intern();
+ charset = Charset.forName(this.encoding);
+ encoder = charset.newEncoder();
+ this.eol = (eol != null) ? eol : System.getProperty("line.separator");
+ namespaces = new LinkedList();
+ }
+
+ void setCdataSectionElements(Collection c)
+ {
+ cdataSectionElements = c;
+ }
+
+ public void serialize(final Node node, final OutputStream out)
+ throws IOException
+ {
+ serialize(node, out, false);
+ }
+
+ void serialize(Node node, final OutputStream out,
+ boolean convertToCdata)
+ throws IOException
+ {
+ while (node != null)
+ {
+ Node next = node.getNextSibling();
+ doSerialize(node, out, convertToCdata);
+ node = next;
+ }
+ }
+
+ private void doSerialize(final Node node, final OutputStream out,
+ boolean convertToCdata)
+ throws IOException
+ {
+ if (out == null)
+ throw new NullPointerException("no output stream");
+ htmlEncoded = false;
+ String value, prefix;
+ Node children;
+ String uri = node.getNamespaceURI();
+ short nt = node.getNodeType();
+ if (convertToCdata && nt == Node.TEXT_NODE)
+ nt = Node.CDATA_SECTION_NODE;
+ switch (nt)
+ {
+ case Node.ATTRIBUTE_NODE:
+ prefix = node.getPrefix();
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri) ||
+ XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) ||
+ (prefix != null && prefix.startsWith("xmlns:")))
+ {
+ String nsuri = node.getNodeValue();
+ if (isDefined(nsuri, prefix))
+ break;
+ String name = node.getLocalName();
+ if (name == null)
+ {
+ // Namespace-unaware
+ name = node.getNodeName();
+ int ci = name.indexOf(':');
+ if (ci != -1)
+ name = name.substring(ci + 1);
+ }
+ define(nsuri, name);
+ }
+ else if (uri != null && !isDefined(uri, prefix))
+ {
+ prefix = define(uri, prefix);
+ String nsname = (prefix == null) ? "xmlns" : "xmlns:" + prefix;
+ out.write(SPACE);
+ out.write(encodeText(nsname));
+ out.write(EQ);
+ String nsvalue = "\"" + encode(uri, true, true) + "\"";
+ out.write(nsvalue.getBytes(encoding));
+ }
+ out.write(SPACE);
+ String a_nodeName = node.getNodeName();
+ out.write(encodeText(a_nodeName));
+ String a_nodeValue = node.getNodeValue();
+ if (mode == Stylesheet.OUTPUT_HTML &&
+ a_nodeName.equals(a_nodeValue) &&
+ isHTMLBoolean((Attr) node, a_nodeName))
+ break;
+ out.write(EQ);
+ value = "\"" + encode(a_nodeValue, true, true) + "\"";
+ out.write(encodeText(value));
+ break;
+ case Node.ELEMENT_NODE:
+ pushNamespaceContext();
+ value = node.getNodeName();
+ out.write(BRA);
+ out.write(encodeText(value));
+ prefix = node.getPrefix();
+ if (uri != null && !isDefined(uri, prefix))
+ {
+ prefix = define(uri, prefix);
+ String nsname = (prefix == null) ? "xmlns" : "xmlns:" + prefix;
+ out.write(SPACE);
+ out.write(encodeText(nsname));
+ out.write(EQ);
+ String nsvalue = "\"" + encode(uri, true, true) + "\"";
+ out.write(encodeText(nsvalue));
+ }
+ NamedNodeMap attrs = node.getAttributes();
+ if (attrs != null)
+ {
+ int len = attrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Attr attr = (Attr) attrs.item(i);
+ if (discardDefaultContent && !attr.getSpecified())
+ {
+ // NOOP
+ }
+ else
+ serialize(attr, out, false);
+ }
+ }
+ convertToCdata = cdataSectionElements.contains(value);
+ children = node.getFirstChild();
+ if (children == null)
+ {
+ out.write(SLASH);
+ out.write(KET);
+ }
+ else
+ {
+ out.write(KET);
+ serialize(children, out, convertToCdata);
+ out.write(BRA);
+ out.write(SLASH);
+ out.write(encodeText(value));
+ out.write(KET);
+ }
+ popNamespaceContext();
+ break;
+ case Node.TEXT_NODE:
+ value = node.getNodeValue();
+ if (!"yes".equals(node.getUserData("disable-output-escaping")) &&
+ mode != Stylesheet.OUTPUT_TEXT)
+ value = encode(value, false, false);
+ out.write(encodeText(value));
+ break;
+ case Node.CDATA_SECTION_NODE:
+ value = node.getNodeValue();
+ // Where any instanceof of ]]> occur, split into multiple CDATA
+ // sections
+ int bbk = value.indexOf("]]>");
+ while (bbk != -1)
+ {
+ String head = value.substring(0, bbk + 2);
+ out.write(encodeText("<![CDATA[" + head + "]]>"));
+ value = value.substring(bbk + 2);
+ bbk = value.indexOf("]]>");
+ }
+ // Write final tail value
+ out.write(encodeText("<![CDATA[" + value + "]]>"));
+ break;
+ case Node.COMMENT_NODE:
+ value = "<!--" + node.getNodeValue() + "-->";
+ out.write(encodeText(value));
+ Node cp = node.getParentNode();
+ if (cp != null && cp.getNodeType() == Node.DOCUMENT_NODE)
+ out.write(encodeText(eol));
+ break;
+ case Node.DOCUMENT_NODE:
+ case Node.DOCUMENT_FRAGMENT_NODE:
+ if (mode == Stylesheet.OUTPUT_XML)
+ {
+ if ("UTF-16".equalsIgnoreCase(encoding))
+ {
+ out.write(0xfe);
+ out.write(0xff);
+ }
+ if (!"yes".equals(node.getUserData("omit-xml-declaration")) &&
+ xmlDeclaration)
+ {
+ Document doc = (node instanceof Document) ?
+ (Document) node : null;
+ String version = (doc != null) ? doc.getXmlVersion() : null;
+ if (version == null)
+ version = (String) node.getUserData("version");
+ if (version == null)
+ version = "1.0";
+ out.write(BRA);
+ out.write(0x3f);
+ out.write("xml version=\"".getBytes("US-ASCII"));
+ out.write(version.getBytes("US-ASCII"));
+ out.write(0x22);
+ if (!("UTF-8".equalsIgnoreCase(encoding)))
+ {
+ out.write(" encoding=\"".getBytes("US-ASCII"));
+ out.write(encoding.getBytes("US-ASCII"));
+ out.write(0x22);
+ }
+ if ((doc != null && doc.getXmlStandalone()) ||
+ "yes".equals(node.getUserData("standalone")))
+ out.write(" standalone=\"yes\"".getBytes("US-ASCII"));
+ out.write(0x3f);
+ out.write(KET);
+ out.write(encodeText(eol));
+ }
+ // TODO warn if not outputting the declaration would be a
+ // problem
+ }
+ else if (mode == Stylesheet.OUTPUT_HTML)
+ {
+ // Ensure that encoding is accessible if head element is present
+ String mediaType = (String) node.getUserData("media-type");
+ if (mediaType == null)
+ mediaType = "text/html";
+ String contentType = mediaType + "; charset=" +
+ ((encoding.indexOf(' ') != -1) ?
+ "\"" + encoding + "\"" :
+ encoding);
+ Document doc = (node instanceof Document) ? (Document) node :
+ node.getOwnerDocument();
+ Node html = null;
+ for (Node ctx = node.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if (ctx.getNodeType() == Node.ELEMENT_NODE &&
+ isHTMLElement(ctx, "html"))
+ {
+ html = ctx;
+ break;
+ }
+ }
+ if (html != null)
+ {
+ Node head = null;
+ for (Node ctx = html.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if (isHTMLElement(ctx, "head"))
+ {
+ head = ctx;
+ break;
+ }
+ }
+ if (head != null)
+ {
+ Node meta = null;
+ Node metaContent = null;
+ for (Node ctx = head.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if (isHTMLElement(ctx, "meta"))
+ {
+ NamedNodeMap metaAttrs = ctx.getAttributes();
+ int len = metaAttrs.getLength();
+ String httpEquiv = null;
+ Node content = null;
+ for (int i = 0; i < len; i++)
+ {
+ Node attr = metaAttrs.item(i);
+ String attrName = attr.getNodeName();
+ if ("http-equiv".equalsIgnoreCase(attrName))
+ httpEquiv = attr.getNodeValue();
+ else if ("content".equalsIgnoreCase(attrName))
+ content = attr;
+ }
+ if ("Content-Type".equalsIgnoreCase(httpEquiv))
+ {
+ meta = ctx;
+ metaContent = content;
+ break;
+ }
+ }
+ }
+ if (meta == null)
+ {
+ meta = doc.createElement("meta");
+ // Insert first
+ Node first = head.getFirstChild();
+ if (first == null)
+ head.appendChild(meta);
+ else
+ head.insertBefore(meta, first);
+ Node metaHttpEquiv = doc.createAttribute("http-equiv");
+ meta.getAttributes().setNamedItem(metaHttpEquiv);
+ metaHttpEquiv.setNodeValue("Content-Type");
+ }
+ if (metaContent == null)
+ {
+ metaContent = doc.createAttribute("content");
+ meta.getAttributes().setNamedItem(metaContent);
+ }
+ metaContent.setNodeValue(contentType);
+ htmlEncoded = true;
+ }
+ }
+ }
+ children = node.getFirstChild();
+ if (children != null)
+ serialize(children, out, convertToCdata);
+ break;
+ case Node.DOCUMENT_TYPE_NODE:
+ DocumentType doctype = (DocumentType) node;
+ out.write(BRA);
+ out.write(BANG);
+ out.write(encodeText("DOCTYPE "));
+ value = doctype.getNodeName();
+ out.write(encodeText(value));
+ String publicId = doctype.getPublicId();
+ if (publicId != null)
+ {
+ out.write(encodeText(" PUBLIC "));
+ out.write(APOS);
+ out.write(encodeText(publicId));
+ out.write(APOS);
+ }
+ String systemId = doctype.getSystemId();
+ if (systemId != null)
+ {
+ out.write(encodeText(" SYSTEM "));
+ out.write(APOS);
+ out.write(encodeText(systemId));
+ out.write(APOS);
+ }
+ String internalSubset = doctype.getInternalSubset();
+ if (internalSubset != null)
+ {
+ out.write(encodeText(internalSubset));
+ }
+ out.write(KET);
+ out.write(eol.getBytes(encoding));
+ break;
+ case Node.ENTITY_REFERENCE_NODE:
+ value = "&" + node.getNodeValue() + ";";
+ out.write(encodeText(value));
+ break;
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ value = "<?" + node.getNodeName() + " " + node.getNodeValue() + "?>";
+ out.write(encodeText(value));
+ Node pp = node.getParentNode();
+ if (pp != null && pp.getNodeType() == Node.DOCUMENT_NODE)
+ {
+ out.write(encodeText(eol));
+ }
+ break;
+ default:
+ System.err.println("Unhandled node type: "+nt);
+ }
+ }
+
+ boolean isHTMLElement(Node node, String name)
+ {
+ if (node.getNodeType() != Node.ELEMENT_NODE)
+ return false;
+ String localName = node.getLocalName();
+ if (localName == null)
+ localName = node.getNodeName();
+ if (!name.equalsIgnoreCase(localName))
+ return false;
+ String uri = node.getNamespaceURI();
+ return (uri == null || HTML_URIS.contains(uri));
+ }
+
+ boolean isDefined(String uri, String prefix)
+ {
+ if (XMLConstants.XML_NS_URI.equals(uri))
+ return "xml".equals(prefix);
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri))
+ return "xmlns".equals(prefix);
+ if (prefix == null)
+ prefix = "";
+ for (Iterator i = namespaces.iterator(); i.hasNext(); )
+ {
+ Map ctx = (Map) i.next();
+ String val = (String) ctx.get(uri);
+ if (val != null && val.equals(prefix))
+ return true;
+ }
+ return false;
+ }
+
+ void pushNamespaceContext()
+ {
+ namespaces.addFirst(new HashMap());
+ }
+
+ String define(String uri, String prefix)
+ {
+ if (namespaces.isEmpty())
+ return prefix;
+ HashMap ctx = (HashMap) namespaces.getFirst();
+ while (ctx.containsValue(prefix))
+ {
+ // Fabricate new prefix
+ prefix = prefix + "_";
+ }
+ ctx.put(uri, prefix);
+ return prefix;
+ }
+
+ void popNamespaceContext()
+ {
+ namespaces.removeFirst();
+ }
+
+ final byte[] encodeText(String text)
+ throws IOException
+ {
+ encoder.reset();
+ boolean htmlNeedingEncoding =
+ (mode == Stylesheet.OUTPUT_HTML && !htmlEncoded);
+ if (!encoder.canEncode(text) || htmlNeedingEncoding)
+ {
+ // Check each character
+ CPStringBuilder buf = new CPStringBuilder();
+ int len = text.length();
+ for (int i = 0; i < len; i++)
+ {
+ char c = text.charAt(i);
+ if (!encoder.canEncode(c))
+ {
+ // Replace with character entity reference
+ String hex = Integer.toHexString((int) c);
+ buf.append("&#x");
+ buf.append(hex);
+ buf.append(';');
+ }
+ else if (htmlNeedingEncoding)
+ {
+ String entityName = getHTMLCharacterEntity(c);
+ if (entityName != null)
+ {
+ buf.append('&');
+ buf.append(entityName);
+ buf.append(';');
+ }
+ else
+ buf.append(c);
+ }
+ else
+ buf.append(c);
+ }
+ text = buf.toString();
+ }
+ ByteBuffer encoded = encoder.encode(CharBuffer.wrap(text));
+ int len = encoded.limit() - encoded.position();
+ if (encoded.hasArray())
+ {
+ byte[] ret = encoded.array();
+ if (ret.length > len)
+ {
+ // Why?
+ byte[] ret2 = new byte[len];
+ System.arraycopy(ret, 0, ret2, 0, len);
+ ret = ret2;
+ }
+ return ret;
+ }
+ encoded.flip();
+ byte[] ret = new byte[len];
+ encoded.get(ret, 0, len);
+ return ret;
+ }
+
+ String encode(String text, boolean encodeCtl, boolean inAttr)
+ {
+ int len = text.length();
+ CPStringBuilder buf = null;
+ for (int i = 0; i < len; i++)
+ {
+ char c = text.charAt(i);
+ if (c == '<')
+ {
+ if (buf == null)
+ buf = new CPStringBuilder(text.substring(0, i));
+ buf.append("&lt;");
+ }
+ else if (c == '>')
+ {
+ if (buf == null)
+ buf = new CPStringBuilder(text.substring(0, i));
+ buf.append("&gt;");
+ }
+ else if (c == '&')
+ {
+ if (mode == Stylesheet.OUTPUT_HTML && (i + 1) < len &&
+ text.charAt(i + 1) == '{')
+ {
+ if (buf != null)
+ buf.append(c);
+ }
+ else
+ {
+ if (buf == null)
+ buf = new CPStringBuilder(text.substring(0, i));
+ buf.append("&amp;");
+ }
+ }
+ else if (c == '\'' && inAttr)
+ {
+ if (buf == null)
+ buf = new CPStringBuilder(text.substring(0, i));
+ if (mode == Stylesheet.OUTPUT_HTML)
+ // HTML does not define &apos;, use character entity ref
+ buf.append("&#x27;");
+ else
+ buf.append("&apos;");
+ }
+ else if (c == '"' && inAttr)
+ {
+ if (buf == null)
+ buf = new CPStringBuilder(text.substring(0, i));
+ buf.append("&quot;");
+ }
+ else if (encodeCtl)
+ {
+ if (c < 0x20)
+ {
+ if (buf == null)
+ buf = new CPStringBuilder(text.substring(0, i));
+ buf.append('&');
+ buf.append('#');
+ buf.append((int) c);
+ buf.append(';');
+ }
+ else if (buf != null)
+ buf.append(c);
+ }
+ else if (buf != null)
+ buf.append(c);
+ }
+ return (buf == null) ? text : buf.toString();
+ }
+
+ String toString(Node node)
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ try
+ {
+ serialize(node, out);
+ return new String(out.toByteArray(), encoding);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+
+ boolean isHTMLBoolean(Attr attr, String attrName)
+ {
+ attrName = attrName.toLowerCase();
+ Node element = attr.getOwnerElement();
+ String elementName = element.getLocalName();
+ if (elementName == null)
+ {
+ elementName = element.getNodeName();
+ }
+ elementName = elementName.toLowerCase();
+ Collection attributes =
+ (Collection) HTML_BOOLEAN_ATTRIBUTES.get(elementName);
+ return (attributes != null && attributes.contains(attrName));
+ }
+
+ static String getHTMLCharacterEntity(char c)
+ {
+ // Hardcode these here to avoid loading the HTML DTD
+ switch (c)
+ {
+ case 160: return "nbsp";
+ case 161: return "iexcl";
+ case 162: return "cent";
+ case 163: return "pound";
+ case 164: return "curren";
+ case 165: return "yen";
+ case 166: return "brvbar";
+ case 167: return "sect";
+ case 168: return "uml";
+ case 169: return "copy";
+ case 170: return "ordf";
+ case 171: return "laquo";
+ case 172: return "not";
+ case 173: return "shy";
+ case 174: return "reg";
+ case 175: return "macr";
+ case 176: return "deg";
+ case 177: return "plusmn";
+ case 178: return "sup2";
+ case 179: return "sup3";
+ case 180: return "acute";
+ case 181: return "micro";
+ case 182: return "para";
+ case 183: return "middot";
+ case 184: return "cedil";
+ case 185: return "sup1";
+ case 186: return "ordm";
+ case 187: return "raquo";
+ case 188: return "frac14";
+ case 189: return "frac12";
+ case 190: return "frac34";
+ case 191: return "iquest";
+ case 192: return "Agrave";
+ case 193: return "Aacute";
+ case 194: return "Acirc";
+ case 195: return "Atilde";
+ case 196: return "Auml";
+ case 197: return "Aring";
+ case 198: return "AElig";
+ case 199: return "Ccedil";
+ case 200: return "Egrave";
+ case 201: return "Eacute";
+ case 202: return "Ecirc";
+ case 203: return "Euml";
+ case 204: return "Igrave";
+ case 205: return "Iacute";
+ case 206: return "Icirc";
+ case 207: return "Iuml";
+ case 208: return "ETH";
+ case 209: return "Ntilde";
+ case 210: return "Ograve";
+ case 211: return "Oacute";
+ case 212: return "Ocirc";
+ case 213: return "Otilde";
+ case 214: return "Ouml";
+ case 215: return "times";
+ case 216: return "Oslash";
+ case 217: return "Ugrave";
+ case 218: return "Uacute";
+ case 219: return "Ucirc";
+ case 220: return "Uuml";
+ case 221: return "Yacute";
+ case 222: return "THORN";
+ case 223: return "szlig";
+ case 224: return "agrave";
+ case 225: return "aacute";
+ case 226: return "acirc";
+ case 227: return "atilde";
+ case 228: return "auml";
+ case 229: return "aring";
+ case 230: return "aelig";
+ case 231: return "ccedil";
+ case 232: return "egrave";
+ case 233: return "eacute";
+ case 234: return "ecirc";
+ case 235: return "euml";
+ case 236: return "igrave";
+ case 237: return "iacute";
+ case 238: return "icirc";
+ case 239: return "iuml";
+ case 240: return "eth";
+ case 241: return "ntilde";
+ case 242: return "ograve";
+ case 243: return "oacute";
+ case 244: return "ocirc";
+ case 245: return "otilde";
+ case 246: return "ouml";
+ case 247: return "divide";
+ case 248: return "oslash";
+ case 249: return "ugrave";
+ case 250: return "uacute";
+ case 251: return "ucirc";
+ case 252: return "uuml";
+ case 253: return "yacute";
+ case 254: return "thorn";
+ case 255: return "yuml";
+ default: return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/StrippingInstruction.java b/libjava/classpath/gnu/xml/transform/StrippingInstruction.java
new file mode 100644
index 000000000..27e9fc8ca
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/StrippingInstruction.java
@@ -0,0 +1,73 @@
+/* StrippingInstruction.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.xml.xpath.NameTest;
+
+/**
+ * An entry in a strip-space or preserve-space list.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class StrippingInstruction
+{
+
+ final NameTest element;
+ final int precedence;
+
+ StrippingInstruction(NameTest element, int precedence)
+ {
+ this.element = element;
+ this.precedence = precedence;
+ }
+
+ /**
+ * Returns the <i>default priority</i> of the element name test.
+ * @see http://www.w3.org/TR/xslt#dt-default-priority
+ */
+ float getPriority()
+ {
+ if (element.matchesAny())
+ return -0.5f;
+ else if (element.matchesAnyLocalName())
+ return -0.25f;
+ else
+ return 0.0f;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/Stylesheet.java b/libjava/classpath/gnu/xml/transform/Stylesheet.java
new file mode 100644
index 000000000..786b258f5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/Stylesheet.java
@@ -0,0 +1,1772 @@
+/* Stylesheet.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.StringTokenizer;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionResolver;
+import javax.xml.xpath.XPathExpressionException;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+import org.w3c.dom.UserDataHandler;
+import gnu.xml.xpath.Expr;
+import gnu.xml.xpath.NameTest;
+import gnu.xml.xpath.NodeTypeTest;
+import gnu.xml.xpath.Pattern;
+import gnu.xml.xpath.Selector;
+import gnu.xml.xpath.Root;
+import gnu.xml.xpath.Test;
+import gnu.xml.xpath.XPathImpl;
+
+/**
+ * An XSL stylesheet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class Stylesheet
+ implements NamespaceContext, XPathFunctionResolver, UserDataHandler, Cloneable
+{
+
+ static final String XSL_NS = "http://www.w3.org/1999/XSL/Transform";
+ private static final NameTest STYLESHEET_PRESERVE_TEXT =
+ new NameTest(new QName(XSL_NS, "text"), false, false);
+
+ static final int OUTPUT_XML = 0;
+ static final int OUTPUT_HTML = 1;
+ static final int OUTPUT_TEXT = 2;
+
+ final TransformerFactoryImpl factory;
+ TransformerImpl transformer;
+ Stylesheet parent;
+ final XPathImpl xpath;
+ final String systemId;
+ final int precedence;
+
+ final boolean debug;
+
+ /**
+ * Version of XSLT.
+ */
+ String version;
+
+ Collection<String> extensionElementPrefixes;
+ Collection<String> excludeResultPrefixes;
+
+ /**
+ * Set of element names for which we should strip whitespace.
+ */
+ Set<StrippingInstruction> stripSpace;
+
+ /**
+ * Set of element names for which we should preserve whitespace.
+ */
+ Set<StrippingInstruction> preserveSpace;
+
+ /**
+ * Output options.
+ */
+ Node output;
+ int outputMethod;
+ String outputVersion;
+ String outputEncoding;
+ boolean outputOmitXmlDeclaration;
+ boolean outputStandalone;
+ String outputPublicId;
+ String outputSystemId;
+ Collection<String> outputCdataSectionElements;
+ boolean outputIndent;
+ String outputMediaType;
+
+ /**
+ * Keys.
+ */
+ Collection<Key> keys;
+
+ /**
+ * Decimal formats.
+ */
+ Map<String,DecimalFormat> decimalFormats;
+
+ /**
+ * Namespace aliases.
+ */
+ Map<String,String> namespaceAliases;
+
+ /**
+ * Attribute-sets.
+ */
+ List<AttributeSet> attributeSets;
+
+ /**
+ * Variables.
+ */
+ List<ParameterNode> variables;
+
+ /**
+ * Variable and parameter bindings.
+ */
+ Bindings bindings;
+
+ /**
+ * Templates.
+ */
+ LinkedList<Template> templates;
+
+ TemplateNode builtInNodeTemplate;
+ TemplateNode builtInTextTemplate;
+
+ /**
+ * Holds the current node while parsing.
+ * Necessary to associate the document function with its declaring node,
+ * to resolve namespaces, and to maintain the current node for the
+ * current() function.
+ */
+ Node current;
+
+ /**
+ * Set by a terminating message.
+ */
+ transient boolean terminated;
+
+ /**
+ * Current template in force.
+ */
+ transient Template currentTemplate;
+
+ Stylesheet(TransformerFactoryImpl factory,
+ Stylesheet parent,
+ Document doc,
+ String systemId,
+ int precedence)
+ throws TransformerConfigurationException
+ {
+ this.factory = factory;
+ this.systemId = systemId;
+ this.precedence = precedence;
+ this.parent = parent;
+ extensionElementPrefixes = new HashSet<String>();
+ excludeResultPrefixes = new HashSet<String>();
+ stripSpace = new LinkedHashSet<StrippingInstruction>();
+ preserveSpace = new LinkedHashSet<StrippingInstruction>();
+ outputCdataSectionElements = new LinkedHashSet<String>();
+ xpath = (XPathImpl) factory.xpathFactory.newXPath();
+ xpath.setNamespaceContext(this);
+ if (parent == null)
+ {
+ bindings = new Bindings(this);
+ attributeSets = new LinkedList<AttributeSet>();
+ variables = new LinkedList<ParameterNode>();
+ namespaceAliases = new LinkedHashMap<String,String>();
+ templates = new LinkedList<Template>();
+ keys = new LinkedList<Key>();
+ decimalFormats = new LinkedHashMap<String,DecimalFormat>();
+ initDefaultDecimalFormat();
+ xpath.setXPathFunctionResolver(this);
+ }
+ else
+ {
+ /* Test for import circularity */
+ for (Stylesheet ctx = this; ctx.parent != null; ctx = ctx.parent)
+ {
+ if (systemId != null && systemId.equals(ctx.parent.systemId))
+ {
+ String msg = "circularity importing " + systemId;
+ throw new TransformerConfigurationException(msg);
+ }
+ }
+ /* OK */
+ Stylesheet root = getRootStylesheet();
+ bindings = root.bindings;
+ attributeSets = root.attributeSets;
+ variables = root.variables;
+ namespaceAliases = root.namespaceAliases;
+ templates = root.templates;
+ keys = root.keys;
+ decimalFormats = root.decimalFormats;
+ xpath.setXPathFunctionResolver(root);
+ }
+ xpath.setXPathVariableResolver(bindings);
+
+ Test anyNode = new NodeTypeTest((short) 0);
+ List<Test> tests = Collections.singletonList(anyNode);
+ builtInNodeTemplate =
+ new ApplyTemplatesNode(new Selector(Selector.CHILD, tests),
+ null, null, null, true);
+ builtInTextTemplate =
+ new ValueOfNode(new Selector(Selector.SELF, tests),
+ false);
+
+ parse(doc.getDocumentElement(), true);
+ current = doc; // Alow namespace resolution during processing
+
+ debug = ("yes".equals(System.getProperty("xsl.debug")));
+
+ if (debug)
+ {
+ System.err.println("Stylesheet: " + doc.getDocumentURI());
+ for (Template t : templates)
+ {
+ t.list(System.err);
+ System.err.println("--------------------");
+ }
+ }
+ }
+
+ Stylesheet getRootStylesheet()
+ {
+ Stylesheet stylesheet = this;
+ while (stylesheet.parent != null)
+ stylesheet = stylesheet.parent;
+ return stylesheet;
+ }
+
+ void initDefaultDecimalFormat()
+ {
+ DecimalFormat defaultDecimalFormat = new DecimalFormat();
+ DecimalFormatSymbols symbols = new DecimalFormatSymbols();
+ symbols.setDecimalSeparator('.');
+ symbols.setGroupingSeparator(',');
+ symbols.setPercent('%');
+ symbols.setPerMill('\u2030');
+ symbols.setZeroDigit('0');
+ symbols.setDigit('#');
+ symbols.setPatternSeparator(';');
+ symbols.setInfinity("Infinity");
+ symbols.setNaN("NaN");
+ symbols.setMinusSign('-');
+ defaultDecimalFormat.setDecimalFormatSymbols(symbols);
+ decimalFormats.put(null, defaultDecimalFormat);
+ }
+
+ // -- Cloneable --
+
+ public Object clone()
+ {
+ try
+ {
+ Stylesheet clone = (Stylesheet) super.clone();
+ clone.bindings = (Bindings) bindings.clone();
+
+ LinkedList<Template> templates2 = new LinkedList<Template>();
+ for (Template t : templates)
+ {
+ templates2.add(t.clone(clone));
+ }
+ clone.templates = templates2;
+
+ LinkedList<AttributeSet> attributeSets2 = new LinkedList<AttributeSet>();
+ for (AttributeSet as : attributeSets)
+ {
+ attributeSets2.add(as.clone(clone));
+ }
+ clone.attributeSets = attributeSets2;
+
+ LinkedList<ParameterNode> variables2 = new LinkedList<ParameterNode>();
+ for (ParameterNode var : variables)
+ {
+ variables2.add(var.clone(clone));
+ }
+ clone.variables = variables2;
+
+ LinkedList<Key> keys2 = new LinkedList<Key>();
+ for (Key k : keys)
+ {
+ keys2.add(k.clone(clone));
+ }
+ clone.keys = keys2;
+
+ return clone;
+ }
+ catch (CloneNotSupportedException e)
+ {
+ throw new Error(e.getMessage());
+ }
+ }
+
+ // -- Variable evaluation --
+
+ void initTopLevelVariables(Node context)
+ throws TransformerException
+ {
+ current = context;
+ // Sort the variables into order
+ // See XSLT 11.4: "If the template or expression specifying the value of
+ // a global variable x references a global variable y, then the value
+ // for y must be computed before the value of x."
+ List<ParameterNode> topLevel = new ArrayList<ParameterNode>(variables);
+ Collections.sort(topLevel);
+ for (ParameterNode var : topLevel)
+ {
+ bindings.set(var.name,
+ var.getValue(this, null, context, 1, 1),
+ var.type);
+ }
+ current = null;
+ }
+
+ // -- NamespaceContext --
+
+ public String getNamespaceURI(String prefix)
+ {
+ return (current == null) ? null : current.lookupNamespaceURI(prefix);
+ }
+
+ public String getPrefix(String namespaceURI)
+ {
+ return (current == null) ? null : current.lookupPrefix(namespaceURI);
+ }
+
+ public Iterator<String> getPrefixes(String namespaceURI)
+ {
+ // TODO
+ return Collections.singleton(getPrefix(namespaceURI)).iterator();
+ }
+
+ final QName getQName(String name)
+ {
+ String localName = name, uri = null, prefix = null;
+ int ci = name.indexOf(':');
+ if (ci != -1)
+ {
+ prefix = name.substring(0, ci);
+ localName = name.substring(ci + 1);
+ uri = getNamespaceURI(prefix);
+ }
+ return new QName(uri, localName, prefix);
+ }
+
+ // -- Template selection --
+
+ TemplateNode getTemplate(QName mode, Node context, boolean applyImports)
+ throws TransformerException
+ {
+ if (debug)
+ System.err.println("getTemplate: mode="+mode+" context="+context);
+ Template selected = null;
+ for (Template t : templates)
+ {
+ boolean isMatch = t.matches(mode, context);
+ if (applyImports)
+ {
+ if (currentTemplate == null)
+ {
+ String msg = "current template may not be null " +
+ "during apply-imports";
+ throw new TransformerException(msg);
+ }
+ if (!currentTemplate.imports(t))
+ isMatch = false;
+ }
+ //System.err.println("\t"+context+" "+t+"="+isMatch);
+ if (isMatch)
+ {
+ // Conflict resolution
+ // @see http://www.w3.org/TR/xslt#conflict
+ if (selected == null)
+ selected = t;
+ else
+ {
+ if (t.precedence < selected.precedence ||
+ t.priority < selected.priority)
+ continue;
+ selected = t;
+ }
+ }
+ }
+ if (selected == null)
+ {
+ // Apply built-in template
+ // Current template is unchanged
+ if (debug)
+ System.err.println("\tbuiltInTemplate context="+context);
+ switch (context.getNodeType())
+ {
+ case Node.ELEMENT_NODE:
+ case Node.DOCUMENT_NODE:
+ case Node.DOCUMENT_FRAGMENT_NODE:
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ case Node.COMMENT_NODE:
+ return builtInNodeTemplate;
+ case Node.TEXT_NODE:
+ case Node.CDATA_SECTION_NODE:
+ case Node.ATTRIBUTE_NODE:
+ return builtInTextTemplate;
+ default:
+ return null;
+ }
+ }
+ // Set current template
+ currentTemplate = selected;
+ if (debug)
+ System.err.println("\ttemplate="+currentTemplate+" context="+context);
+ return currentTemplate.node;
+ }
+
+ TemplateNode getTemplate(QName mode, QName name)
+ throws TransformerException
+ {
+ Template selected = null;
+ for (Template t : templates)
+ {
+ boolean isMatch = t.matches(name);
+ if (isMatch)
+ {
+ // Conflict resolution
+ // @see http://www.w3.org/TR/xslt#conflict
+ if (selected == null)
+ selected = t;
+ else
+ {
+ if (t.precedence < selected.precedence ||
+ t.priority < selected.priority)
+ continue;
+ selected = t;
+ }
+ }
+ }
+ if (selected == null)
+ return null;
+ return selected.node;
+ }
+
+ /**
+ * template
+ */
+ final Template parseTemplate(Node node, NamedNodeMap attrs)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ String n = getAttribute(attrs, "name");
+ QName name = (n == null) ? null : getQName(n);
+ String m = getAttribute(attrs, "match");
+ Pattern match = null;
+ if (m != null)
+ {
+ try
+ {
+ match = (Pattern) xpath.compile(m);
+ }
+ catch (ClassCastException e)
+ {
+ String msg = "illegal pattern: " + m;
+ throw new TransformerConfigurationException(msg);
+ }
+ }
+ String p = getAttribute(attrs, "priority");
+ String mm = getAttribute(attrs, "mode");
+ QName mode = (mm == null) ? null : getQName(mm);
+ Node children = node.getFirstChild();
+ return new Template(this, name, match, parse(children),
+ precedence, p, mode);
+ }
+
+ /**
+ * output
+ */
+ final void parseOutput(Node node, NamedNodeMap attrs)
+ throws TransformerConfigurationException
+ {
+ output = node;
+ String method = getAttribute(attrs, "method");
+ if ("xml".equals(method) || method == null)
+ outputMethod = OUTPUT_XML;
+ else if ("html".equals(method))
+ outputMethod = OUTPUT_HTML;
+ else if ("text".equals(method))
+ outputMethod = OUTPUT_TEXT;
+ else
+ {
+ String msg = "unsupported output method: " + method;
+ DOMSourceLocator l = new DOMSourceLocator(node);
+ throw new TransformerConfigurationException(msg, l);
+ }
+ outputPublicId = getAttribute(attrs, "doctype-public");
+ outputSystemId = getAttribute(attrs, "doctype-system");
+ outputEncoding = getAttribute(attrs, "encoding");
+ String indent = getAttribute(attrs, "indent");
+ if (indent != null)
+ outputIndent = "yes".equals(indent);
+ outputVersion = getAttribute(attrs, "version");
+ String omitXmlDecl = getAttribute(attrs, "omit-xml-declaration");
+ if (omitXmlDecl != null)
+ outputOmitXmlDeclaration = "yes".equals(omitXmlDecl);
+ String standalone = getAttribute(attrs, "standalone");
+ if (standalone != null)
+ outputStandalone = "yes".equals(standalone);
+ outputMediaType = getAttribute(attrs, "media-type");
+ String cdataSectionElements =
+ getAttribute(attrs, "cdata-section-elements");
+ if (cdataSectionElements != null)
+ {
+ StringTokenizer st = new StringTokenizer(cdataSectionElements, " ");
+ while (st.hasMoreTokens())
+ outputCdataSectionElements.add(st.nextToken());
+ }
+ }
+
+ /**
+ * key
+ */
+ final void parseKey(Node node, NamedNodeMap attrs)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ String n = getRequiredAttribute(attrs, "name", node);
+ String m = getRequiredAttribute(attrs, "match", node);
+ String u = getRequiredAttribute(attrs, "use", node);
+ QName name = getQName(n);
+ Expr use = (Expr) xpath.compile(u);
+ try
+ {
+ Pattern match = (Pattern) xpath.compile(m);
+ Key key = new Key(name, match, use);
+ keys.add(key);
+ }
+ catch (ClassCastException e)
+ {
+ throw new TransformerConfigurationException("invalid pattern: " + m);
+ }
+ }
+
+ /**
+ * decimal-format
+ */
+ final void parseDecimalFormat(Node node, NamedNodeMap attrs)
+ throws TransformerConfigurationException
+ {
+ String dfName = getAttribute(attrs, "name");
+ DecimalFormat df = new DecimalFormat();
+ DecimalFormatSymbols symbols = new DecimalFormatSymbols();
+ symbols.setDecimalSeparator(parseDFChar(attrs, "decimal-separator", '.'));
+ symbols.setGroupingSeparator(parseDFChar(attrs, "grouping-separator", ','));
+ symbols.setInfinity(parseDFString(attrs, "infinity", "Infinity"));
+ symbols.setMinusSign(parseDFChar(attrs, "minus-sign", '-'));
+ symbols.setNaN(parseDFString(attrs, "NaN", "NaN"));
+ symbols.setPercent(parseDFChar(attrs, "percent", '%'));
+ symbols.setPerMill(parseDFChar(attrs, "per-mille", '\u2030'));
+ symbols.setZeroDigit(parseDFChar(attrs, "zero-digit", '0'));
+ symbols.setDigit(parseDFChar(attrs, "digit", '#'));
+ symbols.setPatternSeparator(parseDFChar(attrs, "pattern-separator", ';'));
+ df.setDecimalFormatSymbols(symbols);
+ decimalFormats.put(dfName, df);
+ }
+
+ private final char parseDFChar(NamedNodeMap attrs, String name, char def)
+ throws TransformerConfigurationException
+ {
+ Node attr = attrs.getNamedItem(name);
+ try
+ {
+ return (attr == null) ? def : attr.getNodeValue().charAt(0);
+ }
+ catch (StringIndexOutOfBoundsException e)
+ {
+ throw new TransformerConfigurationException("empty attribute '" +
+ name +
+ "' in decimal-format", e);
+ }
+ }
+
+ private final String parseDFString(NamedNodeMap attrs, String name,
+ String def)
+ {
+ Node attr = attrs.getNamedItem(name);
+ return (attr == null) ? def : attr.getNodeValue();
+ }
+
+ /**
+ * namespace-alias
+ */
+ final void parseNamespaceAlias(Node node, NamedNodeMap attrs)
+ throws TransformerConfigurationException
+ {
+ String sp = getRequiredAttribute(attrs, "stylesheet-prefix", node);
+ String rp = getRequiredAttribute(attrs, "result-prefix", node);
+ namespaceAliases.put(sp, rp);
+ }
+
+ /**
+ * attribute-set
+ */
+ final void parseAttributeSet(Node node, NamedNodeMap attrs)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ TemplateNode children = parse(node.getFirstChild());
+ String name = getRequiredAttribute(attrs, "name", node);
+ String uas = getAttribute(attrs, "use-attribute-sets");
+ attributeSets.add(new AttributeSet(children, name, uas));
+ }
+
+ /**
+ * Parse top-level elements.
+ */
+ void parse(Node node, boolean root)
+ throws TransformerConfigurationException
+ {
+ while (node != null)
+ {
+ current = node;
+ doParse(node, root);
+ node = node.getNextSibling();
+ }
+ }
+
+ void doParse(Node node, boolean root)
+ throws TransformerConfigurationException
+ {
+ try
+ {
+ String namespaceUri = node.getNamespaceURI();
+ if (XSL_NS.equals(namespaceUri) &&
+ node.getNodeType() == Node.ELEMENT_NODE)
+ {
+ String name = node.getLocalName();
+ NamedNodeMap attrs = node.getAttributes();
+ if ("stylesheet".equals(name))
+ {
+ version = getAttribute(attrs, "version");
+ String eep = getAttribute(attrs, "extension-element-prefixes");
+ if (eep != null)
+ {
+ StringTokenizer st = new StringTokenizer(eep);
+ while (st.hasMoreTokens())
+ {
+ extensionElementPrefixes.add(st.nextToken());
+ }
+ }
+ String erp = getAttribute(attrs, "exclude-result-prefixes");
+ if (erp != null)
+ {
+ StringTokenizer st = new StringTokenizer(erp);
+ while (st.hasMoreTokens())
+ {
+ excludeResultPrefixes.add(st.nextToken());
+ }
+ }
+ parse(node.getFirstChild(), false);
+ }
+ else if ("template".equals(name))
+ templates.add(parseTemplate(node, attrs));
+ else if ("param".equals(name) ||
+ "variable".equals(name))
+ {
+ int type = "variable".equals(name) ?
+ Bindings.VARIABLE : Bindings.PARAM;
+ TemplateNode content = parse(node.getFirstChild());
+ QName paramName =
+ getQName(getRequiredAttribute(attrs, "name", node));
+ String select = getAttribute(attrs, "select");
+ ParameterNode param;
+ if (select != null && select.length() > 0)
+ {
+ if (content != null)
+ {
+ String msg = "parameter '" + paramName +
+ "' has both select and content";
+ DOMSourceLocator l = new DOMSourceLocator(node);
+ throw new TransformerConfigurationException(msg, l);
+ }
+ Expr expr = (Expr) xpath.compile(select);
+ param = new ParameterNode(paramName, expr, type);
+ }
+ else
+ {
+ param = new ParameterNode(paramName, null, type);
+ param.children = content;
+ }
+ variables.add(param);
+ }
+ else if ("include".equals(name) || "import".equals(name))
+ {
+ int delta = "import".equals(name) ? -1 : 0;
+ String href = getRequiredAttribute(attrs, "href", node);
+ Source source;
+ synchronized (factory.resolver)
+ {
+ if (transformer != null)
+ {
+ factory.resolver
+ .setUserResolver(transformer.getURIResolver());
+ factory.resolver
+ .setUserListener(transformer.getErrorListener());
+ }
+ source = factory.resolver.resolve(systemId, href);
+ }
+ factory.newStylesheet(source, precedence + delta, this);
+ }
+ else if ("output".equals(name))
+ parseOutput(node, attrs);
+ else if ("preserve-space".equals(name))
+ {
+ String elements =
+ getRequiredAttribute(attrs, "elements", node);
+ StringTokenizer st = new StringTokenizer(elements,
+ " \t\n\r");
+ while (st.hasMoreTokens())
+ {
+ NameTest element = parseNameTest(st.nextToken());
+ preserveSpace.add(new StrippingInstruction(element,
+ precedence));
+ }
+ }
+ else if ("strip-space".equals(name))
+ {
+ String elements =
+ getRequiredAttribute(attrs, "elements", node);
+ StringTokenizer st = new StringTokenizer(elements,
+ " \t\n\r");
+ while (st.hasMoreTokens())
+ {
+ NameTest element = parseNameTest(st.nextToken());
+ stripSpace.add(new StrippingInstruction(element,
+ precedence));
+ }
+ }
+ else if ("key".equals(name))
+ parseKey(node, attrs);
+ else if ("decimal-format".equals(name))
+ parseDecimalFormat(node, attrs);
+ else if ("namespace-alias".equals(name))
+ parseNamespaceAlias(node, attrs);
+ else if ("attribute-set".equals(name))
+ parseAttributeSet(node, attrs);
+ }
+ else if (root)
+ {
+ // Literal document element
+ Attr versionNode =
+ ((Element)node).getAttributeNodeNS(XSL_NS, "version");
+ if (versionNode == null)
+ {
+ String msg = "no xsl:version attribute on literal result node";
+ DOMSourceLocator l = new DOMSourceLocator(node);
+ throw new TransformerConfigurationException(msg, l);
+ }
+ version = versionNode.getValue();
+ Node rootClone = node.cloneNode(true);
+ NamedNodeMap attrs = rootClone.getAttributes();
+ attrs.removeNamedItemNS(XSL_NS, "version");
+ templates.add(new Template(this, null, new Root(),
+ parse(rootClone),
+ precedence,
+ null,
+ null));
+ }
+ else
+ {
+ // Skip unknown elements, text, comments, etc
+ }
+ }
+ catch (TransformerException e)
+ {
+ DOMSourceLocator l = new DOMSourceLocator(node);
+ throw new TransformerConfigurationException(e.getMessage(), l, e);
+ }
+ catch (DOMException e)
+ {
+ DOMSourceLocator l = new DOMSourceLocator(node);
+ throw new TransformerConfigurationException(e.getMessage(), l, e);
+ }
+ catch (XPathExpressionException e)
+ {
+ DOMSourceLocator l = new DOMSourceLocator(node);
+ throw new TransformerConfigurationException(e.getMessage(), l, e);
+ }
+ }
+
+ final NameTest parseNameTest(String token)
+ {
+ if ("*".equals(token))
+ return new NameTest(null, true, true);
+ else if (token.endsWith(":*"))
+ {
+ QName qName = getQName(token);
+ return new NameTest(qName, true, false);
+ }
+ else
+ {
+ QName qName = getQName(token);
+ return new NameTest(qName, false, false);
+ }
+ }
+
+ final TemplateNode parseAttributeValueTemplate(String value, Node source)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ current = source;
+ // Tokenize
+ int len = value.length();
+ int off = 0;
+ List<String> tokens = new ArrayList<String>(); // text tokens
+ List<Boolean> types = new ArrayList<Boolean>(); // literal or expression
+ int depth = 0;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == '{')
+ {
+ if (i < (len - 1) && value.charAt(i + 1) == '{')
+ {
+ tokens.add(value.substring(off, i + 1));
+ types.add(Boolean.FALSE);
+ i++;
+ off = i + 1;
+ continue;
+ }
+ if (depth == 0)
+ {
+ if (i - off > 0)
+ {
+ tokens.add(value.substring(off, i));
+ types.add(Boolean.FALSE);
+ }
+ off = i + 1;
+ }
+ depth++;
+ }
+ else if (c == '}')
+ {
+ if (i < (len - 1) && value.charAt(i + 1) == '}')
+ {
+ tokens.add(value.substring(off, i + 1));
+ types.add(Boolean.FALSE);
+ i++;
+ off = i + 1;
+ continue;
+ }
+ if (depth == 1)
+ {
+ if (i - off > 0)
+ {
+ tokens.add(value.substring(off, i));
+ types.add(Boolean.TRUE);
+ }
+ else
+ {
+ String msg = "attribute value template " +
+ "must contain expression: " + value;
+ DOMSourceLocator l = new DOMSourceLocator(source);
+ throw new TransformerConfigurationException(msg, l);
+ }
+ off = i + 1;
+ }
+ depth--;
+ }
+ }
+ if (depth > 0)
+ {
+ String msg = "invalid attribute value template: " + value;
+ throw new TransformerConfigurationException(msg);
+ }
+ if (len - off > 0)
+ {
+ // Trailing text
+ tokens.add(value.substring(off));
+ types.add(Boolean.FALSE);
+ }
+
+ // Construct template node tree
+ TemplateNode ret = null;
+ Document doc = source.getOwnerDocument();
+ len = tokens.size();
+ for (int i = len - 1; i >= 0; i--)
+ {
+ String token = tokens.get(i);
+ Boolean type = types.get(i);
+ if (type == Boolean.TRUE)
+ {
+ // Expression text
+ Expr select = (Expr) xpath.compile(token);
+ TemplateNode ret2 = new ValueOfNode(select, false);
+ ret2.next = ret;
+ ret = ret2;
+ }
+ else
+ {
+ // Verbatim text
+ TemplateNode ret2 = new LiteralNode(doc.createTextNode(token));
+ ret2.next = ret;
+ ret = ret2;
+ }
+ }
+ return ret;
+ }
+
+ /**
+ * Whitespace stripping.
+ * @param text the text node
+ * @param source true if a source node, false if a stylesheet text node
+ * @see http://www.w3.org/TR/xslt#strip
+ */
+ boolean isPreserved(Text text, boolean source)
+ throws TransformerConfigurationException
+ {
+ // Check characters in text
+ String value = text.getData();
+ if (value != null)
+ {
+ int len = value.length();
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c != 0x20 && c != 0x09 && c != 0x0a && c != 0x0d)
+ return true;
+ }
+ }
+ // Check parent node
+ Node ctx = text.getParentNode();
+ if (source)
+ {
+ // Source document text node
+ boolean preserve = true;
+ float psPriority = 0.0f, ssPriority = 0.0f;
+ if (!stripSpace.isEmpty())
+ {
+ // Conflict resolution
+ StrippingInstruction ssi = null, psi = null;
+ for (StrippingInstruction si : stripSpace)
+ {
+ if (si.element.matches(ctx, 1, 1))
+ {
+ if (ssi != null)
+ {
+ if (si.precedence < ssi.precedence)
+ continue;
+ float p = si.getPriority();
+ if (p < ssPriority)
+ continue;
+ }
+ ssi = si;
+ }
+ }
+ for (StrippingInstruction si : preserveSpace)
+ {
+ if (si.element.matches(ctx, 1, 1))
+ {
+ if (psi != null)
+ {
+ if (si.precedence < psi.precedence)
+ continue;
+ float p = si.getPriority();
+ if (p < psPriority)
+ continue;
+ }
+ psi = si;
+ }
+ }
+ if (ssi != null)
+ {
+ if (psi != null)
+ {
+ if (psi.precedence < ssi.precedence)
+ preserve = false;
+ else if (psPriority < ssPriority)
+ preserve = false;
+ }
+ else
+ preserve = false;
+ }
+ }
+ if (preserve)
+ return true;
+ }
+ else
+ {
+ // Stylesheet text node
+ if (STYLESHEET_PRESERVE_TEXT.matches(ctx, 1, 1))
+ return true;
+ }
+ // Check whether any ancestor specified xml:space
+ while (ctx != null)
+ {
+ if (ctx.getNodeType() == Node.ELEMENT_NODE)
+ {
+ Element element = (Element) ctx;
+ String xmlSpace = element.getAttribute("xml:space");
+ if ("default".equals(xmlSpace))
+ break;
+ else if ("preserve".equals(xmlSpace))
+ return true;
+ else if (xmlSpace.length() > 0)
+ {
+ String msg = "Illegal value for xml:space: " + xmlSpace;
+ throw new TransformerConfigurationException(msg);
+ }
+ }
+ ctx = ctx.getParentNode();
+ }
+ return false;
+ }
+
+ public XPathFunction resolveFunction(QName name, int arity)
+ {
+ String uri = name.getNamespaceURI();
+ if (XSL_NS.equals(uri) || uri == null || uri.length() == 0)
+ {
+ String localName = name.getLocalPart();
+ if ("document".equals(localName) && (arity == 1 || arity == 2))
+ {
+ if (current == null)
+ throw new RuntimeException("current is null");
+ return new DocumentFunction(getRootStylesheet(), current);
+ }
+ else if ("key".equals(localName) && (arity == 2))
+ return new KeyFunction(getRootStylesheet());
+ else if ("format-number".equals(localName) &&
+ (arity == 2 || arity == 3))
+ return new FormatNumberFunction(getRootStylesheet());
+ else if ("current".equals(localName) && (arity == 0))
+ return new CurrentFunction(getRootStylesheet());
+ else if ("unparsed-entity-uri".equals(localName) && (arity == 1))
+ return new UnparsedEntityUriFunction();
+ else if ("generate-id".equals(localName) &&
+ (arity == 1 || arity == 0))
+ return new GenerateIdFunction();
+ else if ("system-property".equals(localName) && (arity == 1))
+ return new SystemPropertyFunction();
+ else if ("element-available".equals(localName) && (arity == 1))
+ return new ElementAvailableFunction(new NamespaceProxy(current));
+ else if ("function-available".equals(localName) && (arity == 1))
+ return new FunctionAvailableFunction(new NamespaceProxy(current));
+ }
+ return null;
+ }
+
+ // -- Parsing --
+
+ /**
+ * apply-templates
+ */
+ final TemplateNode parseApplyTemplates(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String m = getAttribute(attrs, "mode");
+ QName mode = (m == null) ? null : getQName(m);
+ String s = getAttribute(attrs, "select");
+ if (s == null)
+ s = "child::node()";
+ Node children = node.getFirstChild();
+ List<SortKey> sortKeys = parseSortKeys(children);
+ List<WithParam> withParams = parseWithParams(children);
+ Expr select = (Expr) xpath.compile(s);
+ return new ApplyTemplatesNode(select, mode,
+ sortKeys, withParams, false);
+ }
+
+ /**
+ * call-template
+ */
+ final TemplateNode parseCallTemplate(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String n = getRequiredAttribute(attrs, "name", node);
+ QName name = getQName(n);
+ Node children = node.getFirstChild();
+ List<WithParam> withParams = parseWithParams(children);
+ return new CallTemplateNode(name, withParams);
+ }
+
+ /**
+ * value-of
+ */
+ final TemplateNode parseValueOf(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String s = getRequiredAttribute(attrs, "select", node);
+ String doe = getAttribute(attrs, "disable-output-escaping");
+ boolean d = "yes".equals(doe);
+ Expr select = (Expr) xpath.compile(s);
+ return new ValueOfNode(select, d);
+ }
+
+ /**
+ * for-each
+ */
+ final TemplateNode parseForEach(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String s = getRequiredAttribute(attrs, "select", node);
+ Node children = node.getFirstChild();
+ List<SortKey> sortKeys = parseSortKeys(children);
+ Expr select = (Expr) xpath.compile(s);
+ ForEachNode ret = new ForEachNode(select, sortKeys);
+ ret.children = parse(children);
+ return ret;
+ }
+
+ /**
+ * if
+ */
+ final TemplateNode parseIf(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String t = getRequiredAttribute(attrs, "test", node);
+ Expr test = (Expr) xpath.compile(t);
+ Node children = node.getFirstChild();
+ IfNode ret = new IfNode(test);
+ ret.children = parse(children);
+ return ret;
+ }
+
+ /**
+ * when
+ */
+ final TemplateNode parseWhen(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String t = getRequiredAttribute(attrs, "test", node);
+ Expr test = (Expr) xpath.compile(t);
+ Node children = node.getFirstChild();
+ WhenNode ret = new WhenNode(test);
+ ret.children = parse(children);
+ return ret;
+ }
+
+ /**
+ * element
+ */
+ final TemplateNode parseElement(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String name = getRequiredAttribute(attrs, "name", node);
+ String namespace = getAttribute(attrs, "namespace");
+ String uas = getAttribute(attrs, "use-attribute-sets");
+ TemplateNode n = parseAttributeValueTemplate(name, node);
+ TemplateNode ns = (namespace == null) ? null :
+ parseAttributeValueTemplate(namespace, node);
+ Node children = node.getFirstChild();
+ ElementNode ret = new ElementNode(n, ns, uas, node);
+ ret.children = parse(children);
+ return ret;
+ }
+
+ /**
+ * attribute
+ */
+ final TemplateNode parseAttribute(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String name = getRequiredAttribute(attrs, "name", node);
+ String namespace = getAttribute(attrs, "namespace");
+ TemplateNode n = parseAttributeValueTemplate(name, node);
+ TemplateNode ns = (namespace == null) ? null :
+ parseAttributeValueTemplate(namespace, node);
+ Node children = node.getFirstChild();
+ AttributeNode ret = new AttributeNode(n, ns, node);
+ ret.children = parse(children);
+ return ret;
+ }
+
+ /**
+ * text
+ */
+ final TemplateNode parseText(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String doe = getAttribute(attrs, "disable-output-escaping");
+ boolean d = "yes".equals(doe);
+ Node children = node.getFirstChild();
+ TextNode ret = new TextNode(d);
+ ret.children = parse(children);
+ return ret;
+ }
+
+ /**
+ * copy
+ */
+ final TemplateNode parseCopy(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String uas = getAttribute(attrs, "use-attribute-sets");
+ Node children = node.getFirstChild();
+ CopyNode ret = new CopyNode(uas);
+ ret.children = parse(children);
+ return ret;
+ }
+
+ /**
+ * processing-instruction
+ */
+ final TemplateNode parseProcessingInstruction(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String name = getRequiredAttribute(attrs, "name", node);
+ Node children = node.getFirstChild();
+ ProcessingInstructionNode ret = new ProcessingInstructionNode(name);
+ ret.children = parse(children);
+ return ret;
+ }
+
+ /**
+ * number
+ */
+ final TemplateNode parseNumber(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String v = getAttribute(attrs, "value");
+ String ff = getAttribute(attrs, "format");
+ if (ff == null)
+ {
+ ff = "1";
+ }
+ TemplateNode format = parseAttributeValueTemplate(ff, node);
+ String lang = getAttribute(attrs, "lang");
+ String lv = getAttribute(attrs, "letter-value");
+ int letterValue = "traditional".equals(lv) ?
+ AbstractNumberNode.TRADITIONAL :
+ AbstractNumberNode.ALPHABETIC;
+ String gs = getAttribute(attrs, "grouping-separator");
+ String gz = getAttribute(attrs, "grouping-size");
+ int gz2 = (gz != null && gz.length() > 0) ?
+ Integer.parseInt(gz) : 1;
+ Node children = node.getFirstChild();
+ TemplateNode ret;
+ if (v != null && v.length() > 0)
+ {
+ Expr value = (Expr) xpath.compile(v);
+ ret = new NumberNode(value, format, lang,
+ letterValue, gs, gz2);
+ }
+ else
+ {
+ String l = getAttribute(attrs, "level");
+ int level =
+ "multiple".equals(l) ? NodeNumberNode.MULTIPLE :
+ "any".equals(l) ? NodeNumberNode.ANY :
+ NodeNumberNode.SINGLE;
+ String c = getAttribute(attrs, "count");
+ String f = getAttribute(attrs, "from");
+ Pattern count = null;
+ Pattern from = null;
+ if (c != null)
+ {
+ try
+ {
+ count = (Pattern) xpath.compile(c);
+ }
+ catch (ClassCastException e)
+ {
+ String msg = "invalid pattern: " + c;
+ throw new TransformerConfigurationException(msg);
+ }
+ }
+ if (f != null)
+ {
+ try
+ {
+ from = (Pattern) xpath.compile(f);
+ }
+ catch (ClassCastException e)
+ {
+ String msg = "invalid pattern: " + f;
+ throw new TransformerConfigurationException(msg);
+ }
+ }
+ ret = new NodeNumberNode(level, count, from,
+ format, lang,
+ letterValue, gs, gz2);
+ }
+ ret.children = parse(children);
+ return ret;
+ }
+
+ /**
+ * copy-of
+ */
+ final TemplateNode parseCopyOf(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String s = getRequiredAttribute(attrs, "select", node);
+ Expr select = (Expr) xpath.compile(s);
+ Node children = node.getFirstChild();
+ CopyOfNode ret = new CopyOfNode(select);
+ ret.children = parse(children);
+ return ret;
+ }
+
+ /**
+ * message
+ */
+ final TemplateNode parseMessage(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String t = getAttribute(attrs, "terminate");
+ boolean terminate = "yes".equals(t);
+ Node children = node.getFirstChild();
+ MessageNode ret = new MessageNode(terminate);
+ ret.children = parse(children);
+ return ret;
+ }
+
+ /**
+ * Parse template-level elements.
+ */
+ final TemplateNode parse(Node node)
+ throws TransformerConfigurationException
+ {
+ TemplateNode first = null;
+ TemplateNode previous = null;
+ while (node != null)
+ {
+ Node next = node.getNextSibling();
+ TemplateNode tnode = doParse(node);
+ if (tnode != null)
+ {
+ if (first == null)
+ first = tnode;
+ if (previous != null)
+ previous.next = tnode;
+ previous = tnode;
+ }
+ node = next;
+ }
+ return first;
+ }
+
+ private final TemplateNode doParse(Node node)
+ throws TransformerConfigurationException
+ {
+ // Hack to associate the document function with its declaring node
+ current = node;
+ try
+ {
+ String namespaceUri = node.getNamespaceURI();
+ if (Stylesheet.XSL_NS.equals(namespaceUri) &&
+ Node.ELEMENT_NODE == node.getNodeType())
+ {
+ String name = node.getLocalName();
+ if ("apply-templates".equals(name))
+ return parseApplyTemplates(node);
+ else if ("call-template".equals(name))
+ return parseCallTemplate(node);
+ else if ("value-of".equals(name))
+ return parseValueOf(node);
+ else if ("for-each".equals(name))
+ return parseForEach(node);
+ else if ("if".equals(name))
+ return parseIf(node);
+ else if ("choose".equals(name))
+ {
+ Node children = node.getFirstChild();
+ ChooseNode ret = new ChooseNode();
+ ret.children = parse(children);
+ return ret;
+ }
+ else if ("when".equals(name))
+ return parseWhen(node);
+ else if ("otherwise".equals(name))
+ {
+ Node children = node.getFirstChild();
+ OtherwiseNode ret = new OtherwiseNode();
+ ret.children = parse(children);
+ return ret;
+ }
+ else if ("element".equals(name))
+ return parseElement(node);
+ else if ("attribute".equals(name))
+ return parseAttribute(node);
+ else if ("text".equals(name))
+ return parseText(node);
+ else if ("copy".equals(name))
+ return parseCopy(node);
+ else if ("processing-instruction".equals(name))
+ return parseProcessingInstruction(node);
+ else if ("comment".equals(name))
+ {
+ Node children = node.getFirstChild();
+ CommentNode ret = new CommentNode();
+ ret.children = parse(children);
+ return ret;
+ }
+ else if ("number".equals(name))
+ return parseNumber(node);
+ else if ("param".equals(name) ||
+ "variable".equals(name))
+ {
+ int type = "variable".equals(name) ?
+ Bindings.VARIABLE : Bindings.PARAM;
+ NamedNodeMap attrs = node.getAttributes();
+ Node children = node.getFirstChild();
+ TemplateNode content = parse(children);
+ QName paramName =
+ getQName(getRequiredAttribute(attrs, "name", node));
+ String select = getAttribute(attrs, "select");
+ ParameterNode ret;
+ if (select != null)
+ {
+ if (content != null)
+ {
+ String msg = "parameter '" + paramName +
+ "' has both select and content";
+ DOMSourceLocator l = new DOMSourceLocator(node);
+ throw new TransformerConfigurationException(msg, l);
+ }
+ Expr expr = (Expr) xpath.compile(select);
+ ret = new ParameterNode(paramName, expr, type);
+ }
+ else
+ {
+ ret = new ParameterNode(paramName, null, type);
+ ret.children = content;
+ }
+ return ret;
+ }
+ else if ("copy-of".equals(name))
+ return parseCopyOf(node);
+ else if ("message".equals(name))
+ return parseMessage(node);
+ else if ("apply-imports".equals(name))
+ {
+ Node children = node.getFirstChild();
+ ApplyImportsNode ret = new ApplyImportsNode();
+ ret.children = parse(children);
+ return ret;
+ }
+ else
+ {
+ // xsl:fallback
+ // Pass over any other XSLT nodes
+ return null;
+ }
+ }
+ String prefix = node.getPrefix();
+ if (extensionElementPrefixes.contains(prefix))
+ {
+ // Check for xsl:fallback
+ for (Node ctx = node.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ String ctxUri = ctx.getNamespaceURI();
+ if (XSL_NS.equals(ctxUri) &&
+ "fallback".equals(ctx.getLocalName()))
+ {
+ ctx = ctx.getFirstChild();
+ return (ctx == null) ? null : parse(ctx);
+ }
+ }
+ // Otherwise pass over extension element
+ return null;
+ }
+ switch (node.getNodeType())
+ {
+ case Node.TEXT_NODE:
+ case Node.CDATA_SECTION_NODE:
+ // Determine whether to strip whitespace
+ Text text = (Text) node;
+ if (!isPreserved(text, false))
+ {
+ // Strip
+ text.getParentNode().removeChild(text);
+ return null;
+ }
+ break;
+ case Node.COMMENT_NODE:
+ // Ignore comments
+ return null;
+ case Node.ELEMENT_NODE:
+ // Check for attribute value templates and use-attribute-sets
+ NamedNodeMap attrs = node.getAttributes();
+ boolean convert = false;
+ String useAttributeSets = null;
+ int len = attrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Node attr = attrs.item(i);
+ String value = attr.getNodeValue();
+ if (Stylesheet.XSL_NS.equals(attr.getNamespaceURI()) &&
+ "use-attribute-sets".equals(attr.getLocalName()))
+ {
+ useAttributeSets = value;
+ convert = true;
+ break;
+ }
+ int start = value.indexOf('{');
+ int end = value.indexOf('}');
+ if (start != -1 || end != -1)
+ {
+ convert = true;
+ break;
+ }
+ }
+ if (convert)
+ {
+ // Create an element-producing template node instead
+ // with appropriate attribute-producing child template nodes
+ Node children = node.getFirstChild();
+ TemplateNode child = parse(children);
+ for (int i = 0; i < len; i++)
+ {
+ Node attr = attrs.item(i);
+ String ans = attr.getNamespaceURI();
+ String aname = attr.getNodeName();
+ if (Stylesheet.XSL_NS.equals(ans) &&
+ "use-attribute-sets".equals(attr.getLocalName()))
+ continue;
+ String value = attr.getNodeValue();
+ TemplateNode grandchild =
+ parseAttributeValueTemplate(value, node);
+ TemplateNode n =
+ parseAttributeValueTemplate(aname, node);
+ TemplateNode ns = (ans == null) ? null :
+ parseAttributeValueTemplate(ans, node);
+ TemplateNode newChild = new AttributeNode(n, ns, attr);
+ newChild.children = grandchild;
+ newChild.next = child;
+ child = newChild;
+ }
+ String ename = node.getNodeName();
+ TemplateNode n = parseAttributeValueTemplate(ename, node);
+ //TemplateNode ns = (namespaceUri == null) ? null :
+ // parseAttributeValueTemplate(namespaceUri, node);
+ TemplateNode ns = null;
+ ElementNode ret = new ElementNode(n, ns, useAttributeSets,
+ node);
+ ret.children = child;
+ return ret;
+ }
+ // Otherwise fall through
+ break;
+ }
+ }
+ catch (XPathExpressionException e)
+ {
+ DOMSourceLocator l = new DOMSourceLocator(node);
+ throw new TransformerConfigurationException(e.getMessage(), l, e);
+ }
+ Node children = node.getFirstChild();
+ LiteralNode ret = new LiteralNode(node);
+ ret.children = parse(children);
+ return ret;
+ }
+
+ final List<SortKey> parseSortKeys(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ List<SortKey> ret = new LinkedList<SortKey>();
+ while (node != null)
+ {
+ String namespaceUri = node.getNamespaceURI();
+ if (Stylesheet.XSL_NS.equals(namespaceUri) &&
+ Node.ELEMENT_NODE == node.getNodeType() &&
+ "sort".equals(node.getLocalName()))
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String s = getAttribute(attrs, "select");
+ if (s == null)
+ s = ".";
+ Expr select = (Expr) xpath.compile(s);
+ String l = getAttribute(attrs, "lang");
+ TemplateNode lang = (l == null) ? null :
+ parseAttributeValueTemplate(l, node);
+ String dt = getAttribute(attrs, "data-type");
+ TemplateNode dataType = (dt == null) ? null :
+ parseAttributeValueTemplate(dt, node);
+ String o = getAttribute(attrs, "order");
+ TemplateNode order = (o == null) ? null :
+ parseAttributeValueTemplate(o, node);
+ String co = getAttribute(attrs, "case-order");
+ TemplateNode caseOrder = (co == null) ? null :
+ parseAttributeValueTemplate(co, node);
+ ret.add(new SortKey(select, lang, dataType, order, caseOrder));
+ }
+ node = node.getNextSibling();
+ }
+ return ret;
+ }
+
+ final List<WithParam> parseWithParams(Node node)
+ throws TransformerConfigurationException, XPathExpressionException
+ {
+ List<WithParam> ret = new LinkedList<WithParam>();
+ while (node != null)
+ {
+ String namespaceUri = node.getNamespaceURI();
+ if (Stylesheet.XSL_NS.equals(namespaceUri) &&
+ Node.ELEMENT_NODE == node.getNodeType() &&
+ "with-param".equals(node.getLocalName()))
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ TemplateNode content = parse(node.getFirstChild());
+ QName name =
+ getQName(getRequiredAttribute(attrs, "name", node));
+ String select = getAttribute(attrs, "select");
+ if (select != null)
+ {
+ if (content != null)
+ {
+ String msg = "parameter '" + name +
+ "' has both select and content";
+ DOMSourceLocator l = new DOMSourceLocator(node);
+ throw new TransformerConfigurationException(msg, l);
+ }
+ Expr expr = (Expr) xpath.compile(select);
+ ret.add(new WithParam(name, expr));
+ }
+ else
+ ret.add(new WithParam(name, content));
+ }
+ node = node.getNextSibling();
+ }
+ return ret;
+ }
+
+ /**
+ * Created element nodes have a copy of the namespace nodes in the
+ * stylesheet, except the XSLT namespace, extension namespaces, and
+ * exclude-result-prefixes.
+ */
+ final void addNamespaceNodes(Node source, Node target, Document doc,
+ Collection<String> elementExcludeResultPrefixes)
+ {
+ NamedNodeMap attrs = source.getAttributes();
+ if (attrs != null)
+ {
+ int len = attrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Node attr = attrs.item(i);
+ String uri = attr.getNamespaceURI();
+ if (uri == XMLConstants.XMLNS_ATTRIBUTE_NS_URI)
+ {
+ String prefix = attr.getLocalName();
+ if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
+ prefix = "#default";
+ String ns = attr.getNodeValue();
+ // Should the namespace be excluded?
+ if (XSL_NS.equals(ns) ||
+ extensionElementPrefixes.contains(prefix) ||
+ elementExcludeResultPrefixes.contains(prefix) ||
+ excludeResultPrefixes.contains(prefix))
+ continue;
+ // Is the namespace already defined on the target?
+ if (prefix == "#default")
+ prefix = null;
+ if (target.lookupNamespaceURI(prefix) != null)
+ continue;
+ attr = attr.cloneNode(true);
+ attr = doc.adoptNode(attr);
+ target.getAttributes().setNamedItemNS(attr);
+ }
+ }
+ }
+ Node parent = source.getParentNode();
+ if (parent != null)
+ addNamespaceNodes(parent, target, doc, elementExcludeResultPrefixes);
+ }
+
+ static final String getAttribute(NamedNodeMap attrs, String name)
+ {
+ Node attr = attrs.getNamedItem(name);
+ if (attr == null)
+ return null;
+ String ret = attr.getNodeValue();
+ if (ret.length() == 0)
+ return null;
+ return ret;
+ }
+
+ static final String getRequiredAttribute(NamedNodeMap attrs, String name,
+ Node source)
+ throws TransformerConfigurationException
+ {
+ String value = getAttribute(attrs, name);
+ if (value == null || value.length() == 0)
+ {
+ String msg =
+ name + " attribute is required on " + source.getNodeName();
+ DOMSourceLocator l = new DOMSourceLocator(source);
+ throw new TransformerConfigurationException(msg, l);
+ }
+ return value;
+ }
+
+ // Handle user data changes when nodes are cloned etc
+
+ public void handle(short op, String key, Object data, Node src, Node dst)
+ {
+ dst.setUserData(key, data, this);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder b = new CPStringBuilder(getClass().getName());
+ b.append("[templates=");
+ b.append(templates);
+ b.append("]");
+ return b.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/SystemPropertyFunction.java b/libjava/classpath/gnu/xml/transform/SystemPropertyFunction.java
new file mode 100644
index 000000000..f314227e6
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/SystemPropertyFunction.java
@@ -0,0 +1,140 @@
+/* SystemPropertyFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+import gnu.xml.xpath.Function;
+
+/**
+ * The XSLT <code>system-property()</code>function.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class SystemPropertyFunction
+ extends Expr
+ implements XPathFunction, Function
+{
+
+ List args;
+
+ public Object evaluate(List args)
+ throws XPathFunctionException
+ {
+ String name = (String) args.get(0);
+ return systemProperty(QName.valueOf(name));
+ }
+
+ public void setArguments(List args)
+ {
+ this.args = args;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ int arity = args.size();
+ List values = new ArrayList(arity);
+ for (int i = 0; i < arity; i++)
+ {
+ Expr arg = (Expr) args.get(i);
+ values.add(arg.evaluate(context, pos, len));
+ }
+ String name = _string(context, values.get(0));
+ return systemProperty(QName.valueOf(name));
+ }
+
+ Object systemProperty(QName name)
+ {
+ String localName = name.getLocalPart();
+ String prefix = name.getPrefix();
+ String uri = name.getNamespaceURI();
+ if (Stylesheet.XSL_NS.equals(uri) ||
+ "xsl".equals(prefix))
+ {
+ if ("version".equals(localName))
+ {
+ return new Double(1.0d);
+ }
+ else if ("vendor".equals(localName))
+ {
+ return "The Free Software Foundation";
+ }
+ else if ("vendor-url".equals(localName))
+ {
+ return "http://www.gnu.org/";
+ }
+ else
+ {
+ return "";
+ }
+ }
+ return System.getProperty(localName);
+ }
+
+ public Expr clone(Object context)
+ {
+ SystemPropertyFunction f = new SystemPropertyFunction();
+ int len = args.size();
+ List args2 = new ArrayList(len);
+ for (int i = 0; i < len; i++)
+ {
+ args2.add(((Expr) args.get(i)).clone(context));
+ }
+ f.setArguments(args2);
+ return f;
+ }
+
+ public boolean references(QName var)
+ {
+ for (Iterator i = args.iterator(); i.hasNext(); )
+ {
+ if (((Expr) i.next()).references(var))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/Template.java b/libjava/classpath/gnu/xml/transform/Template.java
new file mode 100644
index 000000000..09d1a997c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/Template.java
@@ -0,0 +1,263 @@
+/* Template.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.io.PrintStream;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+import gnu.xml.xpath.NameTest;
+import gnu.xml.xpath.NodeTypeTest;
+import gnu.xml.xpath.Pattern;
+import gnu.xml.xpath.Selector;
+import gnu.xml.xpath.Test;
+
+/**
+ * A template in an XSL stylesheet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class Template
+ implements Comparable
+{
+
+ static final double DEFAULT_PRIORITY = 0.5d;
+
+ final Stylesheet stylesheet;
+ final QName name;
+ final Pattern match;
+ final TemplateNode node;
+ final double priority;
+ final int precedence;
+ final QName mode;
+ final boolean isAnyNode; // is the match simply "node()"?
+
+ Template(Stylesheet stylesheet,
+ QName name, Pattern match, TemplateNode node,
+ int precedence, String priorityAttr, QName mode)
+ {
+ this.stylesheet = stylesheet;
+ this.name = name;
+ this.match = match;
+ this.node = node;
+ this.precedence = precedence;
+ this.mode = mode;
+
+ double p = DEFAULT_PRIORITY;
+ boolean a = false;
+ if (priorityAttr != null)
+ p = Double.parseDouble(priorityAttr);
+ else
+ {
+ // adjust priority if necessary
+ // see XSLT section 5.5
+ if (match instanceof Selector)
+ {
+ Selector selector = (Selector) match;
+ Test[] tests = selector.getTests();
+ if (tests.length > 0)
+ {
+ Test test = tests[0];
+ if (test instanceof NameTest)
+ {
+ NameTest nameTest = (NameTest) test;
+ if (nameTest.matchesAny())
+ p = -0.25d;
+ else if (nameTest.matchesAnyLocalName())
+ p = -0.20d;
+ else
+ p = 0.0d;
+ }
+ else
+ {
+ NodeTypeTest nodeTypeTest = (NodeTypeTest) test;
+ if (nodeTypeTest.getNodeType() ==
+ Node.PROCESSING_INSTRUCTION_NODE &&
+ nodeTypeTest.getData() != null)
+ p = 0.0d;
+ else
+ p = -0.5d;
+ a = (nodeTypeTest.getNodeType() == 0);
+ }
+ // Add a small difference for predicates
+ if (tests.length > 1)
+ p += ((double) tests.length - 1) * 0.001;
+ }
+ }
+ }
+ this.priority = p;
+ this.isAnyNode = a;
+ }
+
+ private Template(Stylesheet stylesheet,
+ QName name, Pattern match, TemplateNode node,
+ int precedence, double priority, QName mode, boolean isAnyNode)
+ {
+ this.stylesheet = stylesheet;
+ this.name = name;
+ this.match = match;
+ this.node = node;
+ this.precedence = precedence;
+ this.priority = priority;
+ this.mode = mode;
+ this.isAnyNode = isAnyNode;
+ }
+
+ Template clone(Stylesheet stylesheet)
+ {
+ // FIXME by cloning we lose the imports() functionality, so
+ // apply-imports will be broken.
+ return new Template(stylesheet,
+ name,
+ (match == null) ? null :
+ (Pattern) match.clone(stylesheet),
+ (node == null) ? null : node.clone(stylesheet),
+ precedence,
+ priority,
+ mode,
+ isAnyNode);
+ }
+
+ public int compareTo(Object other)
+ {
+ if (other instanceof Template)
+ {
+ Template t = (Template) other;
+ int d = t.precedence - precedence;
+ if (d != 0)
+ return d;
+ double d2 = t.priority - priority;
+ if (d2 != 0.0d)
+ return (int) Math.round(d2 * 1000.0d);
+ }
+ return 0;
+ }
+
+ Test getNodeTest(Expr expr)
+ {
+ return null;
+ }
+
+ boolean matches(QName mode, Node node)
+ {
+ if ((mode == null && this.mode != null) ||
+ (mode != null && !mode.equals(this.mode)))
+ return false;
+ if (match == null)
+ return false;
+ if (isAnyNode && node.getNodeType() == Node.DOCUMENT_NODE)
+ return false; // don't match document node
+ return match.matches(node);
+ }
+
+ boolean matches(QName name)
+ {
+ return name.equals(this.name);
+ }
+
+ boolean imports(Template other)
+ {
+ for (Stylesheet ctx = other.stylesheet.parent;
+ ctx != null;
+ ctx = ctx.parent)
+ {
+ if (ctx == stylesheet)
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * @param stylesheet the stylesheet
+ * @param parent the parent of result nodes
+ * @param context the context node in the source document
+ * @param pos the context position
+ * @param len the context size
+ * @param nextSibling if non-null, add result nodes before this node
+ */
+ void apply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ if (stylesheet.debug)
+ System.err.println("...applying " + toString() + " to " + context);
+ if (node != null)
+ node.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder(getClass().getName());
+ buf.append('[');
+ if (name != null)
+ {
+ buf.append("name=");
+ buf.append(name);
+ }
+ else if (match != null)
+ {
+ buf.append("match=");
+ buf.append(match);
+ }
+ if (mode != null)
+ {
+ buf.append(",mode=");
+ buf.append(mode);
+ }
+ buf.append(",node=");
+ buf.append(node);
+ buf.append(']');
+ return buf.toString();
+
+ //return (name != null) ? name.toString() : match.toString();
+ }
+
+ void list(PrintStream out)
+ {
+ out.println(toString());
+ if (node != null)
+ node.list(1, out, true);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/TemplateNode.java b/libjava/classpath/gnu/xml/transform/TemplateNode.java
new file mode 100644
index 000000000..d4c223e01
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/TemplateNode.java
@@ -0,0 +1,130 @@
+/* TemplateNode.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.io.PrintStream;
+import java.util.Comparator;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.DocumentOrderComparator;
+
+/**
+ * Wrapper for a source node in a template.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+abstract class TemplateNode
+{
+
+ static final Comparator<Node> documentOrderComparator =
+ new DocumentOrderComparator();
+
+ TemplateNode children;
+ TemplateNode next;
+
+ final void apply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ if (stylesheet.terminated)
+ return;
+ if (Thread.currentThread().isInterrupted())
+ {
+ // Try to head off any infinite loops at the pass
+ return;
+ }
+ if (stylesheet.debug)
+ {
+ System.err.println("Applying " + toString());
+ System.err.println("\twith context=" + context + ", pos=" + pos +
+ ", len=" + len);
+ }
+ doApply(stylesheet, mode, context, pos, len, parent, nextSibling);
+ }
+
+ abstract void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException;
+
+ abstract TemplateNode clone(Stylesheet stylesheet);
+
+ public boolean references(QName var)
+ {
+ if (children != null && children.references(var))
+ return true;
+ if (next != null && next.references(var))
+ return true;
+ return false;
+ }
+
+ /**
+ * Debugging
+ */
+ void list(int depth, PrintStream out, boolean listNext)
+ {
+ for (int i = 0; i < depth; i++)
+ out.print(" ");
+ out.println(toString());
+ if (children != null)
+ children.list(depth + 1, out, true);
+ if (listNext && next != null)
+ next.list(depth, out, listNext);
+ }
+
+ /**
+ * Indicates whether the template for which this template node is the
+ * first node specifies the given parameter.
+ */
+ boolean hasParam(QName name)
+ {
+ for (TemplateNode ctx = this; ctx != null; ctx = ctx.next)
+ {
+ if (ctx instanceof ParameterNode)
+ {
+ ParameterNode param = (ParameterNode) ctx;
+ if (param.type == Bindings.PARAM && param.name.equals(name))
+ return true;
+ }
+ }
+ return false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/TemplatesImpl.java b/libjava/classpath/gnu/xml/transform/TemplatesImpl.java
new file mode 100644
index 000000000..fc741bc81
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/TemplatesImpl.java
@@ -0,0 +1,95 @@
+/* TemplatesImpl.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.Properties;
+import javax.xml.transform.Templates;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+
+/**
+ * GNU precompiled stylesheet implementation.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class TemplatesImpl
+ implements Templates
+{
+
+ final TransformerFactoryImpl factory;
+ final Stylesheet stylesheet;
+ final Properties outputProperties;
+
+ TemplatesImpl(TransformerFactoryImpl factory, Stylesheet stylesheet)
+ {
+ this.factory = factory;
+ this.stylesheet = stylesheet;
+ outputProperties = new TransformerOutputProperties(stylesheet);
+ }
+
+ public Transformer newTransformer()
+ throws TransformerConfigurationException
+ {
+ Stylesheet stylesheet = (Stylesheet) this.stylesheet.clone();
+ TransformerImpl transformer =
+ new TransformerImpl(factory, stylesheet, outputProperties);
+ stylesheet.transformer = transformer;
+ return transformer;
+ }
+
+ public Properties getOutputProperties()
+ {
+ return (Properties) outputProperties.clone();
+ }
+
+ public String toString()
+ {
+ CPStringBuilder b = new CPStringBuilder(getClass().getName());
+ b.append("[factory=");
+ b.append(factory);
+ b.append(",stylesheet=");
+ b.append(stylesheet);
+ b.append(",outputProperties=");
+ b.append(outputProperties);
+ b.append("]");
+ return b.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/TextNode.java b/libjava/classpath/gnu/xml/transform/TextNode.java
new file mode 100644
index 000000000..8263ef5bc
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/TextNode.java
@@ -0,0 +1,121 @@
+/* TextNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing the XSL <code>text</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class TextNode
+ extends TemplateNode
+{
+
+ final boolean disableOutputEscaping;
+
+ TextNode(boolean disableOutputEscaping)
+ {
+ this.disableOutputEscaping = disableOutputEscaping;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new TextNode(disableOutputEscaping);
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ String value = "";
+ Document doc = (parent instanceof Document) ? (Document) parent :
+ parent.getOwnerDocument();
+ if (children != null)
+ {
+ // Create a document fragment to hold the text
+ DocumentFragment fragment = doc.createDocumentFragment();
+ // Apply children to the fragment
+ children.apply(stylesheet, mode,
+ context, pos, len,
+ fragment, null);
+ // Use XPath string-value of fragment
+ value = Expr.stringValue(fragment);
+ }
+ Text text = doc.createTextNode(value);
+ if (disableOutputEscaping)
+ text.setUserData("disable-output-escaping", "yes", stylesheet);
+ // Insert into result tree
+ if (nextSibling != null)
+ parent.insertBefore(text, nextSibling);
+ else
+ parent.appendChild(text);
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("text");
+ if (disableOutputEscaping)
+ {
+ buf.append('[');
+ buf.append("disable-output-escaping");
+ buf.append(']');
+ }
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/TransformerFactoryImpl.java b/libjava/classpath/gnu/xml/transform/TransformerFactoryImpl.java
new file mode 100644
index 000000000..972e8a282
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/TransformerFactoryImpl.java
@@ -0,0 +1,438 @@
+/* TransformerFactoryImpl.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Properties;
+import javax.xml.transform.ErrorListener;
+import javax.xml.transform.Source;
+import javax.xml.transform.Templates;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.sax.SAXTransformerFactory;
+import javax.xml.transform.sax.TemplatesHandler;
+import javax.xml.transform.sax.TransformerHandler;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.xpath.XPathFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.xml.sax.XMLFilter;
+import gnu.xml.dom.DomDocument;
+
+/**
+ * GNU transformer factory implementation.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class TransformerFactoryImpl
+ extends SAXTransformerFactory
+{
+
+ final XPathFactory xpathFactory;
+ final XSLURIResolver resolver;
+ ErrorListener userListener;
+ URIResolver userResolver;
+
+ public TransformerFactoryImpl()
+ {
+ xpathFactory = new gnu.xml.xpath.XPathFactoryImpl();
+ resolver = new XSLURIResolver();
+ }
+
+ public Transformer newTransformer(Source source)
+ throws TransformerConfigurationException
+ {
+ Stylesheet stylesheet = newStylesheet(source, 0, null);
+ Properties outputProperties =
+ new TransformerOutputProperties(stylesheet);
+ TransformerImpl transformer =
+ new TransformerImpl(this, stylesheet, outputProperties);
+ stylesheet.transformer = transformer;
+ return transformer;
+ }
+
+ public Transformer newTransformer()
+ throws TransformerConfigurationException
+ {
+ return new TransformerImpl(this, null, new Properties());
+ }
+
+ public Templates newTemplates(Source source)
+ throws TransformerConfigurationException
+ {
+ Stylesheet stylesheet = newStylesheet(source, 0, null);
+ return new TemplatesImpl(this, stylesheet);
+ }
+
+ Stylesheet newStylesheet(Source source, int precedence, Stylesheet parent)
+ throws TransformerConfigurationException
+ {
+ Document doc = null;
+ String systemId = null;
+ if (source != null)
+ {
+ try
+ {
+ DOMSource ds;
+ synchronized (resolver)
+ {
+ resolver.setUserResolver(userResolver);
+ resolver.setUserListener(userListener);
+ ds = resolver.resolveDOM(source, null, null);
+ }
+ Node node = ds.getNode();
+ if (node == null)
+ {
+ throw new TransformerConfigurationException("no source document");
+ }
+ doc = (node instanceof Document) ? (Document) node :
+ node.getOwnerDocument();
+ systemId = ds.getSystemId();
+ }
+ catch (TransformerException e)
+ {
+ throw new TransformerConfigurationException(e);
+ }
+ }
+ return new Stylesheet(this, parent, doc, systemId, precedence);
+ }
+
+ public Source getAssociatedStylesheet(Source source,
+ String media,
+ String title,
+ String charset)
+ throws TransformerConfigurationException
+ {
+ try
+ {
+ DOMSource ds;
+ synchronized (resolver)
+ {
+ resolver.setUserResolver(userResolver);
+ resolver.setUserListener(userListener);
+ ds = resolver.resolveDOM(source, null, null);
+ }
+ Node node = ds.getNode();
+ if (node == null)
+ {
+ throw new TransformerConfigurationException("no source document");
+ }
+ Document doc = (node instanceof Document) ? (Document) node :
+ node.getOwnerDocument();
+ LinkedList matches = new LinkedList();
+ for (node = doc.getFirstChild();
+ node != null;
+ node = node.getNextSibling())
+ {
+ if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE &&
+ "xml-stylesheet".equals(node.getNodeName()))
+ {
+ Map params = parseParameters(node.getNodeValue());
+ if (media != null && !media.equals(params.get("media")))
+ {
+ continue;
+ }
+ if (title != null && !title.equals(params.get("title")))
+ {
+ continue;
+ }
+ if (charset != null && !charset.equals(params.get("charset")))
+ {
+ continue;
+ }
+ String href = (String) params.get("href");
+ URL url = resolver.resolveURL(null, node.getBaseURI(), href);
+ matches.add(url);
+ }
+ }
+ switch (matches.size())
+ {
+ case 0:
+ return null;
+ case 1:
+ return new StreamSource(((URL) matches.getFirst()).toString());
+ default:
+ // Create a source representing a stylesheet with a list of
+ // imports
+ DomDocument ssDoc = new DomDocument();
+ ssDoc.setBuilding(true);
+ // Create document element
+ Node root =
+ ssDoc.createElementNS(Stylesheet.XSL_NS, "stylesheet");
+ Node version =
+ ssDoc.createAttributeNS(null, "version");
+ version.setNodeValue("1.0");
+ root.getAttributes().setNamedItemNS(version);
+ ssDoc.appendChild(root);
+ // Create xsl:import for each URL
+ for (Iterator i = matches.iterator(); i.hasNext(); )
+ {
+ URL url = (URL) i.next();
+ Node imp =
+ ssDoc.createElementNS(Stylesheet.XSL_NS, "import");
+ Node href =
+ ssDoc.createAttributeNS(null, "href");
+ href.setNodeValue(url.toString());
+ imp.getAttributes().setNamedItemNS(href);
+ root.appendChild(imp);
+ }
+ ssDoc.setBuilding(false);
+ return new DOMSource(ssDoc);
+ }
+ }
+ catch (IOException e)
+ {
+ throw new TransformerConfigurationException(e);
+ }
+ catch (TransformerException e)
+ {
+ throw new TransformerConfigurationException(e);
+ }
+ }
+
+ Map parseParameters(String data)
+ {
+ Map ret = new LinkedHashMap();
+ int len = data.length();
+ String key = null;
+ int start = 0;
+ char quoteChar = '\u0000';
+ for (int i = 0; i < len; i++)
+ {
+ char c = data.charAt(i);
+ if (quoteChar == '\u0000' && c == ' ')
+ {
+ if (key == null && start < i)
+ {
+ key = data.substring(start, i);
+ }
+ else
+ {
+ String val = unquote(data.substring(start, i).trim());
+ ret.put(key, val);
+ key = null;
+ }
+ start = i + 1;
+ }
+ else if (c == '"')
+ {
+ quoteChar = (quoteChar == c) ? '\u0000' : c;
+ }
+ else if (c == '\'')
+ {
+ quoteChar = (quoteChar == c) ? '\u0000' : c;
+ }
+ }
+ if (start < len && key != null)
+ {
+ String val = unquote(data.substring(start, len).trim());
+ ret.put(key, val);
+ }
+ return ret;
+ }
+
+ String unquote(String text)
+ {
+ int end = text.length() - 1;
+ if (text.charAt(0) == '\'' && text.charAt(end) == '\'')
+ {
+ return text.substring(1, end);
+ }
+ if (text.charAt(0) == '"' && text.charAt(end) == '"')
+ {
+ return text.substring(1, end);
+ }
+ return text;
+ }
+
+ public void setURIResolver(URIResolver resolver)
+ {
+ userResolver = resolver;
+ }
+
+ public URIResolver getURIResolver()
+ {
+ return userResolver;
+ }
+
+ public void setFeature(String name, boolean value)
+ throws TransformerConfigurationException
+ {
+ throw new TransformerConfigurationException("not supported");
+ }
+
+ public boolean getFeature(String name)
+ {
+ if (SAXSource.FEATURE.equals(name) ||
+ SAXResult.FEATURE.equals(name) ||
+ StreamSource.FEATURE.equals(name) ||
+ StreamResult.FEATURE.equals(name) ||
+ DOMSource.FEATURE.equals(name) ||
+ DOMResult.FEATURE.equals(name) ||
+ SAXTransformerFactory.FEATURE.equals(name))
+ {
+ return true;
+ }
+ return false;
+ }
+
+ public void setAttribute(String name, Object value)
+ throws IllegalArgumentException
+ {
+ throw new IllegalArgumentException("not supported");
+ }
+
+ public Object getAttribute(String name)
+ throws IllegalArgumentException
+ {
+ throw new IllegalArgumentException("not supported");
+ }
+
+ public void setErrorListener(ErrorListener listener)
+ throws IllegalArgumentException
+ {
+ userListener = listener;
+ }
+
+ public ErrorListener getErrorListener()
+ {
+ return userListener;
+ }
+
+ // -- SAXTransformerFactory --
+
+ public TemplatesHandler newTemplatesHandler()
+ throws TransformerConfigurationException
+ {
+ return new SAXTemplatesHandler(this);
+ }
+
+ public TransformerHandler newTransformerHandler()
+ throws TransformerConfigurationException
+ {
+ Transformer transformer = newTransformer();
+ return new SAXTransformerHandler(this, transformer);
+ }
+
+ public TransformerHandler newTransformerHandler(Source source)
+ throws TransformerConfigurationException
+ {
+ Transformer transformer = newTransformer(source);
+ return new SAXTransformerHandler(this, transformer);
+ }
+
+ public TransformerHandler newTransformerHandler(Templates templates)
+ throws TransformerConfigurationException
+ {
+ Transformer transformer = templates.newTransformer();
+ return new SAXTransformerHandler(this, transformer);
+ }
+
+ public XMLFilter newXMLFilter(Source source)
+ throws TransformerConfigurationException
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public XMLFilter newXMLFilter(Templates templates)
+ throws TransformerConfigurationException
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ // -- SAXTransformerFactory end --
+
+ /**
+ * Syntax: TransformerFactoryImpl [<stylesheet> [<input> [<output>]]]
+ */
+ public static void main(String[] args)
+ throws Exception
+ {
+ InputStream stylesheet = null, in = null;
+ OutputStream out = null;
+ try
+ {
+ if (args.length > 0)
+ {
+ stylesheet = new FileInputStream(args[0]);
+ if (args.length > 1)
+ {
+ in = new FileInputStream(args[1]);
+ if (args.length > 2)
+ out = new FileOutputStream(args[2]);
+ }
+ }
+ if (in == null)
+ in = System.in;
+ if (out == null)
+ out = System.out;
+ TransformerFactory f = new TransformerFactoryImpl();
+ Transformer t = (stylesheet != null) ?
+ f.newTransformer(new StreamSource(stylesheet)) :
+ f.newTransformer();
+ t.transform(new StreamSource(in), new StreamResult(out));
+ }
+ finally
+ {
+ if (stylesheet != null)
+ stylesheet.close();
+ if (in != null && in instanceof FileInputStream)
+ in.close();
+ if (out != null && out instanceof FileOutputStream)
+ out.close();
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/TransformerImpl.java b/libjava/classpath/gnu/xml/transform/TransformerImpl.java
new file mode 100644
index 000000000..eb6a5dea1
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/TransformerImpl.java
@@ -0,0 +1,805 @@
+/* TransformerImpl.java --
+ Copyright (C) 2004,2005,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.io.BufferedOutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+import java.io.Writer;
+import java.net.MalformedURLException;
+import java.net.UnknownServiceException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Properties;
+import java.util.StringTokenizer;
+import javax.xml.namespace.QName;
+import javax.xml.transform.ErrorListener;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.stream.StreamResult;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.ext.LexicalHandler;
+import gnu.xml.dom.DomDoctype;
+import gnu.xml.dom.DomDocument;
+import gnu.xml.dom.ls.WriterOutputStream;
+
+/**
+ * The transformation process for a given stylesheet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class TransformerImpl
+ extends Transformer
+{
+
+ final TransformerFactoryImpl factory;
+ final Stylesheet stylesheet;
+ URIResolver uriResolver;
+ ErrorListener errorListener;
+ Properties outputProperties;
+
+ TransformerImpl(TransformerFactoryImpl factory,
+ Stylesheet stylesheet,
+ Properties outputProperties)
+ throws TransformerConfigurationException
+ {
+ this.factory = factory;
+ uriResolver = factory.userResolver;
+ errorListener = factory.userListener;
+ this.stylesheet = stylesheet;
+ this.outputProperties = outputProperties;
+ if (stylesheet != null)
+ {
+ // Set up parameter context for this transformer
+ stylesheet.bindings.push(Bindings.PARAM);
+ }
+ }
+
+ public void transform(Source xmlSource, Result outputTarget)
+ throws TransformerException
+ {
+ // Get the source tree
+ DOMSource source;
+ synchronized (factory.resolver)
+ {
+ factory.resolver.setUserResolver(uriResolver);
+ factory.resolver.setUserListener(errorListener);
+ source = factory.resolver.resolveDOM(xmlSource, null, null);
+ }
+ Node context = source.getNode();
+ Document doc = (context instanceof Document) ? (Document) context :
+ context.getOwnerDocument();
+ if (doc instanceof DomDocument)
+ {
+ // Suppress mutation events
+ ((DomDocument) doc).setBuilding(true);
+ }
+ // Get the result tree
+ Node parent = null, nextSibling = null;
+ if (outputTarget instanceof DOMResult)
+ {
+ DOMResult dr = (DOMResult) outputTarget;
+ parent = dr.getNode();
+ nextSibling = dr.getNextSibling();
+
+ Document rdoc = (parent instanceof Document) ? (Document) parent :
+ parent.getOwnerDocument();
+ if (rdoc instanceof DomDocument)
+ {
+ // Suppress mutation events and allow multiple root elements
+ DomDocument drdoc = (DomDocument) rdoc;
+ drdoc.setBuilding(true);
+ drdoc.setCheckWellformedness(false);
+ }
+ }
+ boolean created = false;
+ // Transformation
+ if (stylesheet != null)
+ {
+ if (parent == null)
+ {
+ // Create a new document to hold the result
+ DomDocument resultDoc = new DomDocument();
+ resultDoc.setBuilding(true);
+ resultDoc.setCheckWellformedness(false);
+ parent = resultDoc;
+ created = true;
+ }
+ // Make a copy of the source node, and strip it
+ context = context.cloneNode(true);
+ strip(stylesheet, context);
+ // XSLT transformation
+ try
+ {
+ // Set output properties in the underlying stylesheet
+ ((TransformerOutputProperties) outputProperties).apply();
+ stylesheet.initTopLevelVariables(context);
+ TemplateNode t = stylesheet.getTemplate(null, context, false);
+ if (t != null)
+ {
+ stylesheet.current = context;
+ t.apply(stylesheet, null, context, 1, 1, parent, nextSibling);
+ }
+ }
+ catch (TransformerException e)
+ {
+ // Done transforming, reset document
+ if (doc instanceof DomDocument)
+ ((DomDocument) doc).setBuilding(false);
+ throw e;
+ }
+ }
+ else
+ {
+ // Identity transform
+ Node clone = context.cloneNode(true);
+ if (context.getNodeType() != Node.DOCUMENT_NODE)
+ {
+ Document resultDoc;
+ if (parent == null)
+ {
+ // Create a new document to hold the result
+ DomDocument rd = new DomDocument();
+ rd.setBuilding(true);
+ rd.setCheckWellformedness(false);
+ parent = resultDoc = rd;
+ created = true;
+ }
+ else
+ {
+ resultDoc = (parent instanceof Document) ?
+ (Document) parent :
+ parent.getOwnerDocument();
+ }
+ Document sourceDoc = context.getOwnerDocument();
+ if (sourceDoc != resultDoc)
+ clone = resultDoc.adoptNode(clone);
+ if (nextSibling != null)
+ parent.insertBefore(clone, nextSibling);
+ else
+ parent.appendChild(clone);
+ }
+ else
+ {
+ // Cannot append document to another tree
+ parent = clone;
+ created = true;
+ }
+ }
+ String method = outputProperties.getProperty(OutputKeys.METHOD);
+ int outputMethod = "html".equals(method) ? Stylesheet.OUTPUT_HTML :
+ "text".equals(method) ? Stylesheet.OUTPUT_TEXT :
+ Stylesheet.OUTPUT_XML;
+ String encoding = outputProperties.getProperty(OutputKeys.ENCODING);
+ String publicId = outputProperties.getProperty(OutputKeys.DOCTYPE_PUBLIC);
+ String systemId = outputProperties.getProperty(OutputKeys.DOCTYPE_SYSTEM);
+ String version = outputProperties.getProperty(OutputKeys.VERSION);
+ boolean omitXmlDeclaration =
+ "yes".equals(outputProperties.getProperty(OutputKeys.OMIT_XML_DECLARATION));
+ boolean standalone =
+ "yes".equals(outputProperties.getProperty(OutputKeys.STANDALONE));
+ String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE);
+ String cdataSectionElements =
+ outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS);
+ boolean indent =
+ "yes".equals(outputProperties.getProperty(OutputKeys.INDENT));
+ if (created && parent instanceof DomDocument)
+ {
+ // Discover document element
+ DomDocument resultDoc = (DomDocument) parent;
+ Node root = resultDoc.getDocumentElement();
+ // Add doctype if specified
+ if (publicId != null || systemId != null)
+ {
+ if (root != null)
+ {
+ // We must know the name of the root element to
+ // create the document type
+ DocumentType doctype = new DomDoctype(resultDoc,
+ root.getNodeName(),
+ publicId,
+ systemId);
+ resultDoc.insertBefore(doctype, root);
+ }
+ }
+ resultDoc.setBuilding(false);
+ resultDoc.setCheckWellformedness(true);
+ }
+ else if (publicId != null || systemId != null)
+ {
+ switch (parent.getNodeType())
+ {
+ case Node.DOCUMENT_NODE:
+ case Node.DOCUMENT_FRAGMENT_NODE:
+ Document resultDoc = (parent instanceof Document) ?
+ (Document) parent :
+ parent.getOwnerDocument();
+ DOMImplementation impl = resultDoc.getImplementation();
+ Node root = resultDoc.getDocumentElement();
+ if (root != null)
+ {
+ DocumentType doctype =
+ impl.createDocumentType(root.getNodeName(),
+ publicId,
+ systemId);
+ resultDoc.insertBefore(doctype, root);
+ }
+ }
+ }
+ if (version != null)
+ parent.setUserData("version", version, stylesheet);
+ if (omitXmlDeclaration)
+ parent.setUserData("omit-xml-declaration", "yes", stylesheet);
+ if (standalone)
+ parent.setUserData("standalone", "yes", stylesheet);
+ if (mediaType != null)
+ parent.setUserData("media-type", mediaType, stylesheet);
+ if (cdataSectionElements != null)
+ {
+ List list = new LinkedList();
+ StringTokenizer st = new StringTokenizer(cdataSectionElements);
+ while (st.hasMoreTokens())
+ {
+ String name = st.nextToken();
+ String localName = name;
+ String uri = null;
+ String prefix = null;
+ int ci = name.indexOf(':');
+ if (ci != -1)
+ {
+ // Use namespaces defined on xsl:output node to resolve
+ // namespaces for QName
+ prefix = name.substring(0, ci);
+ localName = name.substring(ci + 1);
+ uri = stylesheet.output.lookupNamespaceURI(prefix);
+ }
+ list.add(new QName(uri, localName, prefix));
+ }
+ if (!list.isEmpty())
+ {
+ Document resultDoc = (parent instanceof Document) ?
+ (Document) parent :
+ parent.getOwnerDocument();
+ convertCdataSectionElements(resultDoc, parent, list);
+ }
+ }
+ if (indent)
+ {
+ if (created && parent instanceof DomDocument)
+ {
+ DomDocument domDoc = (DomDocument) parent;
+ domDoc.setBuilding(true);
+ domDoc.setCheckWellformedness(false);
+ }
+ parent.normalize();
+ if (stylesheet != null)
+ strip(stylesheet, parent);
+ Document resultDoc = (parent instanceof Document) ?
+ (Document) parent :
+ parent.getOwnerDocument();
+ reindent(resultDoc, parent, 0);
+ if (created && parent instanceof DomDocument)
+ {
+ DomDocument domDoc = (DomDocument) parent;
+ domDoc.setBuilding(false);
+ domDoc.setCheckWellformedness(true);
+ }
+ }
+ // Render result to the target device
+ if (outputTarget instanceof DOMResult)
+ {
+ if (created)
+ {
+ DOMResult dr = (DOMResult) outputTarget;
+ dr.setNode(parent);
+ dr.setNextSibling(null);
+ }
+ }
+ else if (outputTarget instanceof StreamResult)
+ {
+ StreamResult sr = (StreamResult) outputTarget;
+ IOException ex = null;
+ try
+ {
+ writeStreamResult(parent, sr, outputMethod, encoding);
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ try
+ {
+ writeStreamResult(parent, sr, outputMethod, "UTF-8");
+ }
+ catch (IOException e2)
+ {
+ ex = e2;
+ }
+ }
+ catch (IOException e)
+ {
+ ex = e;
+ }
+ if (ex != null)
+ {
+ if (errorListener != null)
+ errorListener.error(new TransformerException(ex));
+ else
+ ex.printStackTrace(System.err);
+ }
+ }
+ else if (outputTarget instanceof SAXResult)
+ {
+ SAXResult sr = (SAXResult) outputTarget;
+ try
+ {
+ ContentHandler ch = sr.getHandler();
+ LexicalHandler lh = sr.getLexicalHandler();
+ if (lh == null && ch instanceof LexicalHandler)
+ lh = (LexicalHandler) ch;
+ SAXSerializer serializer = new SAXSerializer();
+ serializer.serialize(parent, ch, lh);
+ }
+ catch (SAXException e)
+ {
+ if (errorListener != null)
+ errorListener.error(new TransformerException(e));
+ else
+ e.printStackTrace(System.err);
+ }
+ }
+ }
+
+ /**
+ * Strip whitespace from the source tree.
+ */
+ static boolean strip(Stylesheet stylesheet, Node node)
+ throws TransformerConfigurationException
+ {
+ short nt = node.getNodeType();
+ if (nt == Node.ENTITY_REFERENCE_NODE)
+ {
+ // Replace entity reference with its content
+ Node parent = node.getParentNode();
+ Node nextSibling = node.getNextSibling();
+ Node child = node.getFirstChild();
+ while (child != null)
+ {
+ Node next = child.getNextSibling();
+ node.removeChild(child);
+ if (nextSibling != null)
+ parent.insertBefore(child, nextSibling);
+ else
+ parent.appendChild(child);
+ child = next;
+ }
+ return true;
+ }
+ if (nt == Node.TEXT_NODE || nt == Node.CDATA_SECTION_NODE)
+ {
+ // Denormalize text into whitespace and non-whitespace nodes
+ String text = node.getNodeValue();
+ String[] tokens = tokenizeWhitespace(text);
+ if (tokens.length > 1)
+ {
+ node.setNodeValue(tokens[0]);
+ Node parent = node.getParentNode();
+ Node nextSibling = node.getNextSibling();
+ Document doc = node.getOwnerDocument();
+ for (int i = 1; i < tokens.length; i++)
+ {
+ Node newChild = (nt == Node.CDATA_SECTION_NODE) ?
+ doc.createCDATASection(tokens[i]) :
+ doc.createTextNode(tokens[i]);
+ if (nextSibling != null)
+ parent.insertBefore(newChild, nextSibling);
+ else
+ parent.appendChild(newChild);
+ }
+ }
+ return !stylesheet.isPreserved((Text) node, true);
+ }
+ else
+ {
+ Node child = node.getFirstChild();
+ while (child != null)
+ {
+ boolean remove = strip(stylesheet, child);
+ Node next = child.getNextSibling();
+ if (remove)
+ node.removeChild(child);
+ child = next;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Tokenize the specified text into contiguous whitespace-only and
+ * non-whitespace chunks.
+ */
+ private static String[] tokenizeWhitespace(String text)
+ {
+ int len = text.length();
+ int start = 0, end = len - 1;
+ // Find index of text start
+ for (int i = 0; i < len; i++)
+ {
+ char c = text.charAt(i);
+ boolean whitespace = (c == ' ' || c == '\n' || c == '\t' || c == '\r');
+ if (whitespace)
+ start++;
+ else
+ break;
+ }
+ if (start == end) // all whitespace
+ return new String[] { text };
+ // Find index of text end
+ for (int i = end; i > start; i--)
+ {
+ char c = text.charAt(i);
+ boolean whitespace = (c == ' ' || c == '\n' || c == '\t' || c == '\r');
+ if (whitespace)
+ end--;
+ else
+ break;
+ }
+ if (start == 0 && end == len - 1) // all non-whitespace
+ return new String[] { text };
+ // whitespace, then text, then whitespace
+ String[] ret = (start > 0 && end < len - 1) ?
+ new String[3] : new String[2];
+ int i = 0;
+ if (start > 0)
+ ret[i++] = text.substring(0, start);
+ ret[i++] = text.substring(start, end + 1);
+ if (end < len - 1)
+ ret[i++] = text.substring(end + 1);
+ return ret;
+ }
+
+ /**
+ * Obtain a suitable output stream for writing the result to,
+ * and use the StreamSerializer to write the result tree to the stream.
+ */
+ void writeStreamResult(Node node, StreamResult sr, int outputMethod,
+ String encoding)
+ throws IOException
+ {
+ OutputStream out = null;
+ boolean created = false;
+ try
+ {
+ out = sr.getOutputStream();
+ if (out == null)
+ {
+ Writer writer = sr.getWriter();
+ if (writer != null)
+ out = new WriterOutputStream(writer);
+ }
+ if (out == null)
+ {
+ String systemId = sr.getSystemId();
+ try
+ {
+ URL url = new URL(systemId);
+ URLConnection connection = url.openConnection();
+ // We need to call setDoInput(false), because our
+ // implementation of the file protocol allows writing
+ // (unlike Sun), but it will fail with a FileNotFoundException
+ // if we also open the connection for input and the output
+ // file doesn't yet exist.
+ connection.setDoInput(false);
+ connection.setDoOutput(true);
+ out = connection.getOutputStream();
+ }
+ catch (MalformedURLException e)
+ {
+ out = new FileOutputStream(systemId);
+ }
+ catch (UnknownServiceException e)
+ {
+ URL url = new URL(systemId);
+ out = new FileOutputStream(url.getPath());
+ }
+ created = true;
+ }
+ out = new BufferedOutputStream(out);
+ StreamSerializer serializer =
+ new StreamSerializer(outputMethod, encoding, null);
+ if (stylesheet != null)
+ {
+ Collection celem = stylesheet.outputCdataSectionElements;
+ serializer.setCdataSectionElements(celem);
+ }
+ serializer.serialize(node, out);
+ out.flush();
+ }
+ finally
+ {
+ try
+ {
+ if (out != null && created)
+ out.close();
+ }
+ catch (IOException e)
+ {
+ if (errorListener != null)
+ {
+ try
+ {
+ errorListener.error(new TransformerException(e));
+ }
+ catch (TransformerException e2)
+ {
+ e2.printStackTrace(System.err);
+ }
+ }
+ else
+ e.printStackTrace(System.err);
+ }
+ }
+ }
+
+ void copyChildren(Document dstDoc, Node src, Node dst)
+ {
+ Node srcChild = src.getFirstChild();
+ while (srcChild != null)
+ {
+ Node dstChild = dstDoc.adoptNode(srcChild);
+ dst.appendChild(dstChild);
+ srcChild = srcChild.getNextSibling();
+ }
+ }
+
+ public void setParameter(String name, Object value)
+ {
+ if (stylesheet != null)
+ stylesheet.bindings.set(new QName(null, name), value, Bindings.PARAM);
+ }
+
+ public Object getParameter(String name)
+ {
+ if (stylesheet != null)
+ return stylesheet.bindings.get(new QName(null, name), null, 1, 1);
+ return null;
+ }
+
+ public void clearParameters()
+ {
+ if (stylesheet != null)
+ {
+ stylesheet.bindings.pop(Bindings.PARAM);
+ stylesheet.bindings.push(Bindings.PARAM);
+ }
+ }
+
+ public void setURIResolver(URIResolver resolver)
+ {
+ uriResolver = resolver;
+ }
+
+ public URIResolver getURIResolver()
+ {
+ return uriResolver;
+ }
+
+ public void setOutputProperties(Properties oformat)
+ throws IllegalArgumentException
+ {
+ if (oformat == null)
+ outputProperties.clear();
+ else
+ outputProperties.putAll(oformat);
+ }
+
+ public Properties getOutputProperties()
+ {
+ return (Properties) outputProperties.clone();
+ }
+
+ public void setOutputProperty(String name, String value)
+ throws IllegalArgumentException
+ {
+ outputProperties.put(name, value);
+ }
+
+ public String getOutputProperty(String name)
+ throws IllegalArgumentException
+ {
+ return outputProperties.getProperty(name);
+ }
+
+ public void setErrorListener(ErrorListener listener)
+ {
+ errorListener = listener;
+ }
+
+ public ErrorListener getErrorListener()
+ {
+ return errorListener;
+ }
+
+ static final String INDENT_WHITESPACE = " ";
+
+ /*
+ * Apply indent formatting to the given tree.
+ */
+ void reindent(Document doc, Node node, int offset)
+ {
+ if (node.hasChildNodes())
+ {
+ boolean markupContent = false;
+ boolean textContent = false;
+ List children = new LinkedList();
+ Node ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ switch (ctx.getNodeType())
+ {
+ case Node.ELEMENT_NODE:
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ case Node.DOCUMENT_TYPE_NODE:
+ markupContent = true;
+ break;
+ case Node.TEXT_NODE:
+ case Node.CDATA_SECTION_NODE:
+ case Node.ENTITY_REFERENCE_NODE:
+ case Node.COMMENT_NODE:
+ textContent = true;
+ break;
+ }
+ children.add(ctx);
+ ctx = ctx.getNextSibling();
+ }
+ if (markupContent)
+ {
+ if (textContent)
+ {
+ // XXX handle mixed content differently?
+ }
+ int nodeType = node.getNodeType();
+ if (nodeType == Node.DOCUMENT_NODE)
+ {
+ for (Iterator i = children.iterator(); i.hasNext(); )
+ {
+ ctx = (Node) i.next();
+ reindent(doc, ctx, offset);
+ }
+ }
+ else
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ buf.append('\n');
+ for (int i = 0; i < offset + 1; i++)
+ buf.append(INDENT_WHITESPACE);
+ String ws = buf.toString();
+ for (Iterator i = children.iterator(); i.hasNext(); )
+ {
+ ctx = (Node) i.next();
+ node.insertBefore(doc.createTextNode(ws), ctx);
+ reindent(doc, ctx, offset + 1);
+ }
+ buf = new CPStringBuilder();
+ buf.append('\n');
+ for (int i = 0; i < offset; i++)
+ buf.append(INDENT_WHITESPACE);
+ ws = buf.toString();
+ node.appendChild(doc.createTextNode(ws));
+ }
+ }
+ }
+ }
+
+ /**
+ * Converts the text node children of any cdata-section-elements in the
+ * tree to CDATA section nodes.
+ */
+ void convertCdataSectionElements(Document doc, Node node, List list)
+ {
+ if (node.getNodeType() == Node.ELEMENT_NODE)
+ {
+ boolean match = false;
+ for (Iterator i = list.iterator(); i.hasNext(); )
+ {
+ QName qname = (QName) i.next();
+ if (match(qname, node))
+ {
+ match = true;
+ break;
+ }
+ }
+ if (match)
+ {
+ Node ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ if (ctx.getNodeType() == Node.TEXT_NODE)
+ {
+ Node cdata = doc.createCDATASection(ctx.getNodeValue());
+ node.replaceChild(cdata, ctx);
+ ctx = cdata;
+ }
+ ctx = ctx.getNextSibling();
+ }
+ }
+ }
+ Node ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ if (ctx.hasChildNodes())
+ convertCdataSectionElements(doc, ctx, list);
+ ctx = ctx.getNextSibling();
+ }
+ }
+
+ boolean match(QName qname, Node node)
+ {
+ String ln1 = qname.getLocalPart();
+ String ln2 = node.getLocalName();
+ if (ln2 == null)
+ return ln1.equals(node.getNodeName());
+ else
+ {
+ String uri1 = qname.getNamespaceURI();
+ String uri2 = node.getNamespaceURI();
+ return (uri1.equals(uri2) && ln1.equals(ln2));
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/TransformerOutputProperties.java b/libjava/classpath/gnu/xml/transform/TransformerOutputProperties.java
new file mode 100644
index 000000000..ddaa260c4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/TransformerOutputProperties.java
@@ -0,0 +1,186 @@
+/* TransformerOutputProperties.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.Properties;
+import java.util.StringTokenizer;
+import javax.xml.transform.OutputKeys;
+
+/**
+ * Helper class to manage JAXP user setting of output properties.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class TransformerOutputProperties
+ extends Properties
+{
+
+ final Properties defaultProperties;
+ final Stylesheet stylesheet;
+ boolean dirty;
+
+ TransformerOutputProperties(Stylesheet stylesheet)
+ {
+ this.stylesheet = stylesheet;
+ defaultProperties = new Properties();
+ switch (stylesheet.outputMethod)
+ {
+ case Stylesheet.OUTPUT_XML:
+ defaultProperties.put(OutputKeys.METHOD, "xml");
+ break;
+ case Stylesheet.OUTPUT_HTML:
+ defaultProperties.put(OutputKeys.METHOD, "html");
+ break;
+ case Stylesheet.OUTPUT_TEXT:
+ defaultProperties.put(OutputKeys.METHOD, "text");
+ break;
+ }
+ if (stylesheet.outputVersion != null)
+ {
+ defaultProperties.put(OutputKeys.VERSION, stylesheet.outputVersion);
+ }
+ if (stylesheet.outputEncoding != null)
+ {
+ defaultProperties.put(OutputKeys.ENCODING, stylesheet.outputEncoding);
+ }
+ defaultProperties.put(OutputKeys.OMIT_XML_DECLARATION,
+ stylesheet.outputOmitXmlDeclaration ? "yes" : "no");
+ defaultProperties.put(OutputKeys.STANDALONE,
+ stylesheet.outputStandalone ? "yes" : "no");
+ if (stylesheet.outputPublicId != null)
+ {
+ defaultProperties.put(OutputKeys.DOCTYPE_PUBLIC,
+ stylesheet.outputPublicId);
+ }
+ if (stylesheet.outputSystemId != null)
+ {
+ defaultProperties.put(OutputKeys.DOCTYPE_SYSTEM,
+ stylesheet.outputSystemId);
+ }
+ CPStringBuilder buf = new CPStringBuilder();
+ for (Iterator i = stylesheet.outputCdataSectionElements.iterator();
+ i.hasNext(); )
+ {
+ if (buf.length() > 0)
+ {
+ buf.append(' ');
+ }
+ buf.append((String) i.next());
+ }
+ defaultProperties.put(OutputKeys.CDATA_SECTION_ELEMENTS, buf.toString());
+ defaultProperties.put(OutputKeys.INDENT,
+ stylesheet.outputIndent ? "yes" : "no");
+ if (stylesheet.outputMediaType != null)
+ {
+ defaultProperties.put(OutputKeys.MEDIA_TYPE,
+ stylesheet.outputMediaType);
+ }
+ }
+
+ public String getProperty(String key)
+ {
+ String val = super.getProperty(key);
+ if (val == null)
+ {
+ val = defaultProperties.getProperty(key);
+ }
+ return val;
+ }
+
+ public Object put(Object key, Object value)
+ {
+ Object ret = super.put(key, value);
+ dirty = true;
+ return ret;
+ }
+
+ public void clear()
+ {
+ super.clear();
+ dirty = true;
+ }
+
+ /**
+ * Applies the current set of properties to the underlying stylesheet.
+ */
+ void apply()
+ {
+ if (!dirty)
+ {
+ return;
+ }
+ String method = getProperty(OutputKeys.METHOD);
+ if ("xml".equals(method))
+ {
+ stylesheet.outputMethod = Stylesheet.OUTPUT_XML;
+ }
+ else if ("html".equals(method))
+ {
+ stylesheet.outputMethod = Stylesheet.OUTPUT_HTML;
+ }
+ else if ("text".equals(method))
+ {
+ stylesheet.outputMethod = Stylesheet.OUTPUT_TEXT;
+ }
+ stylesheet.outputVersion = getProperty(OutputKeys.VERSION);
+ stylesheet.outputEncoding = getProperty(OutputKeys.ENCODING);
+ stylesheet.outputOmitXmlDeclaration =
+ "yes".equals(getProperty(OutputKeys.OMIT_XML_DECLARATION));
+ stylesheet.outputStandalone =
+ "yes".equals(getProperty(OutputKeys.STANDALONE));
+ stylesheet.outputPublicId = getProperty(OutputKeys.DOCTYPE_PUBLIC);
+ stylesheet.outputSystemId = getProperty(OutputKeys.DOCTYPE_SYSTEM);
+ StringTokenizer st =
+ new StringTokenizer(getProperty(OutputKeys.CDATA_SECTION_ELEMENTS));
+ Collection acc = new LinkedHashSet();
+ while (st.hasMoreTokens())
+ {
+ acc.add(st.nextToken());
+ }
+ stylesheet.outputCdataSectionElements = acc;
+ stylesheet.outputIndent = "yes".equals(getProperty(OutputKeys.INDENT));
+ stylesheet.outputMediaType = getProperty(OutputKeys.MEDIA_TYPE);
+ dirty = false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/URIResolverEntityResolver.java b/libjava/classpath/gnu/xml/transform/URIResolverEntityResolver.java
new file mode 100644
index 000000000..49193a473
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/URIResolverEntityResolver.java
@@ -0,0 +1,83 @@
+/* URIResolverEntityResolver.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.io.IOException;
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.sax.SAXSource;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * EntityResolver that wraps a URIResolver.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class URIResolverEntityResolver
+ implements EntityResolver
+{
+
+ final URIResolver resolver;
+
+ URIResolverEntityResolver(URIResolver resolver)
+ {
+ this.resolver = resolver;
+ }
+
+ public InputSource resolveEntity(String publicId, String systemId)
+ throws SAXException, IOException
+ {
+ try
+ {
+ Source source = resolver.resolve(null, systemId);
+ if (source == null)
+ {
+ return null;
+ }
+ return SAXSource.sourceToInputSource(source);
+ }
+ catch (TransformerException e)
+ {
+ throw new SAXException(e);
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/UnparsedEntityUriFunction.java b/libjava/classpath/gnu/xml/transform/UnparsedEntityUriFunction.java
new file mode 100644
index 000000000..5eae78cb2
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/UnparsedEntityUriFunction.java
@@ -0,0 +1,132 @@
+/* UnparsedEntityUriFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionException;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.Notation;
+import gnu.xml.xpath.Expr;
+import gnu.xml.xpath.Function;
+
+/**
+ * The XSLT <code>unparsed-entity-uri()</code>function.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class UnparsedEntityUriFunction
+ extends Expr
+ implements XPathFunction, Function
+{
+
+ List<Expr> args;
+
+ public Object evaluate(List args)
+ throws XPathFunctionException
+ {
+ // Useless...
+ return Collections.emptySet();
+ }
+
+ public void setArguments(List<Expr> args)
+ {
+ this.args = args;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ int arity = args.size();
+ List<Object> values = new ArrayList<Object>(arity);
+ for (int i = 0; i < arity; i++)
+ {
+ Expr arg = args.get(i);
+ values.add(arg.evaluate(context, pos, len));
+ }
+ String name = _string(context, values.get(0));
+ DocumentType doctype = context.getOwnerDocument().getDoctype();
+ if (doctype != null)
+ {
+ NamedNodeMap notations = doctype.getNotations();
+ Notation notation = (Notation) notations.getNamedItem(name);
+ if (notation != null)
+ {
+ String systemId = notation.getSystemId();
+ // XXX absolutize?
+ if (systemId != null)
+ {
+ return systemId;
+ }
+ }
+ }
+ return "";
+ }
+
+ public Expr clone(Object context)
+ {
+ UnparsedEntityUriFunction f = new UnparsedEntityUriFunction();
+ int len = args.size();
+ List<Expr> args2 = new ArrayList<Expr>(len);
+ for (int i = 0; i < len; i++)
+ {
+ args2.add(args.get(i).clone(context));
+ }
+ f.setArguments(args2);
+ return f;
+ }
+
+ public boolean references(QName var)
+ {
+ for (Iterator<Expr> i = args.iterator(); i.hasNext(); )
+ {
+ if (i.next().references(var))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/ValueOfNode.java b/libjava/classpath/gnu/xml/transform/ValueOfNode.java
new file mode 100644
index 000000000..0d9a656e7
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/ValueOfNode.java
@@ -0,0 +1,141 @@
+/* ValueOfNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.Collection;
+import java.util.Iterator;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing an XSLT <code>value-of</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class ValueOfNode
+ extends TemplateNode
+{
+
+ final Expr select;
+ final boolean disableOutputEscaping;
+
+ ValueOfNode(Expr select, boolean disableOutputEscaping)
+ {
+ this.select = select;
+ this.disableOutputEscaping = disableOutputEscaping;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new ValueOfNode(select.clone(stylesheet),
+ disableOutputEscaping);
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ @Override
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ Object ret = select.evaluate(context, pos, len);
+ String value;
+ if (ret instanceof Collection)
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ for (Node node : ((Collection<Node>) ret))
+ {
+ buf.append(Expr.stringValue(node));
+ }
+ value = buf.toString();
+ }
+ else if (ret == null)
+ value = "";
+ else
+ value = Expr._string(context, ret);
+ if (stylesheet.debug)
+ System.err.println("value-of: "+context+" "+ select + " -> "+ value);
+ if (value != null && value.length() > 0)
+ {
+ Document doc = (parent instanceof Document) ?
+ (Document) parent : parent.getOwnerDocument();
+ Text textNode = doc.createTextNode(value);
+ if (disableOutputEscaping)
+ textNode.setUserData("disable-output-escaping", "yes", stylesheet);
+ if (nextSibling != null)
+ parent.insertBefore(textNode, nextSibling);
+ else
+ parent.appendChild(textNode);
+ }
+ // value-of doesn't process children
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ public boolean references(QName var)
+ {
+ if (select != null && select.references(var))
+ return true;
+ return super.references(var);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("value-of");
+ buf.append('[');
+ buf.append("select=");
+ buf.append(select);
+ if (disableOutputEscaping)
+ buf.append(",disableOutputEscaping");
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/WhenNode.java b/libjava/classpath/gnu/xml/transform/WhenNode.java
new file mode 100644
index 000000000..abfe796fd
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/WhenNode.java
@@ -0,0 +1,115 @@
+/* WhenNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing an XSL <code>when</code> instruction.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class WhenNode
+ extends TemplateNode
+{
+
+ final Expr test;
+
+ WhenNode(Expr test)
+ {
+ this.test = test;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new WhenNode(test.clone(stylesheet));
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ Object ret = test.evaluate(context, pos, len);
+ boolean success = (ret instanceof Boolean) ?
+ ((Boolean) ret).booleanValue() :
+ Expr._boolean(context, ret);
+ if (success)
+ {
+ if (children != null)
+ children.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+ else
+ {
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+ }
+
+ public boolean references(QName var)
+ {
+ if (test != null && test.references(var))
+ return true;
+ return super.references(var);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("when");
+ buf.append('[');
+ buf.append("test=");
+ buf.append(test);
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/WithParam.java b/libjava/classpath/gnu/xml/transform/WithParam.java
new file mode 100644
index 000000000..2e726fb82
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/WithParam.java
@@ -0,0 +1,127 @@
+/* WithParam.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.util.Collections;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A specification for setting a variable or parameter during template
+ * processing with <code>apply-templates</code> or
+ * <code>call-template</code>.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class WithParam
+{
+
+ final QName name;
+ final Expr select;
+ final TemplateNode content;
+
+ WithParam(QName name, Expr select)
+ {
+ this.name = name;
+ this.select = select;
+ content = null;
+ }
+
+ WithParam(QName name, TemplateNode content)
+ {
+ this.name = name;
+ this.content = content;
+ select = null;
+ }
+
+ Object getValue(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len)
+ throws TransformerException
+ {
+ if (select != null)
+ {
+ return select.evaluate(context, pos, len);
+ }
+ else if (content == null)
+ {
+ return "";
+ }
+ else
+ {
+ Document doc = (context instanceof Document) ? (Document) context :
+ context.getOwnerDocument();
+ DocumentFragment fragment = doc.createDocumentFragment();
+ content.apply(stylesheet, mode,
+ context, pos, len,
+ fragment, null);
+ return Collections.singleton(fragment);
+ }
+ }
+
+ WithParam clone(Stylesheet stylesheet)
+ {
+ if (content == null)
+ {
+ return new WithParam(name,
+ select.clone(stylesheet));
+ }
+ else
+ {
+ return new WithParam(name,
+ content.clone(stylesheet));
+ }
+ }
+
+ boolean references(QName var)
+ {
+ if (select != null && select.references(var))
+ {
+ return true;
+ }
+ if (content != null && content.references(var))
+ {
+ return true;
+ }
+ return false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/XSLComparator.java b/libjava/classpath/gnu/xml/transform/XSLComparator.java
new file mode 100644
index 000000000..1fd0b20e6
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/XSLComparator.java
@@ -0,0 +1,118 @@
+/* XSLComparator.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.text.Collator;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * Comparator for sorting lists of nodes according to a list of sort keys.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class XSLComparator
+ implements Comparator<Node>
+{
+
+ final List<SortKey> sortKeys;
+
+ XSLComparator(List<SortKey> sortKeys)
+ {
+ this.sortKeys = sortKeys;
+ }
+
+ public int compare(Node n1, Node n2)
+ {
+ for (SortKey sortKey : sortKeys)
+ {
+ String k1 = sortKey.key(n1);
+ String k2 = sortKey.key(n2);
+ if ("text".equals(sortKey.dataType))
+ {
+ Locale locale = (sortKey.lang == null) ? Locale.getDefault() :
+ new Locale(sortKey.lang);
+ Collator collator = Collator.getInstance(locale);
+ int d = collator.compare(k1, k2);
+ if (d != 0)
+ {
+ switch (sortKey.caseOrder)
+ {
+ case SortKey.UPPER_FIRST:
+ // TODO
+ break;
+ case SortKey.LOWER_FIRST:
+ // TODO
+ break;
+ }
+ if (sortKey.descending)
+ {
+ d = -d;
+ }
+ return d;
+ }
+ }
+ else if ("number".equals(sortKey.dataType))
+ {
+ double kn1 = Expr._number(n1, k1);
+ double kn2 = Expr._number(n2, k2);
+ int d;
+ if (Double.isNaN(kn1) || Double.isInfinite(kn2))
+ {
+ d = -1;
+ }
+ else if (Double.isNaN(kn2) || Double.isInfinite(kn1))
+ {
+ d = 1;
+ }
+ else
+ {
+ // conversion to int may give 0 for small numbers
+ d = (kn1 > kn2) ? 1 : (kn1 < kn2) ? -1 : 0;
+ }
+ return (sortKey.descending) ? -d : d;
+ }
+ }
+ return 0;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/XSLURIResolver.java b/libjava/classpath/gnu/xml/transform/XSLURIResolver.java
new file mode 100644
index 000000000..cf596c1e6
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/XSLURIResolver.java
@@ -0,0 +1,321 @@
+/* XSLURIResolver.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.Reader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.HashMap;
+import java.util.Map;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.ErrorListener;
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamSource;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import gnu.xml.dom.DomDocument;
+import gnu.xml.dom.ls.SAXEventSink;
+import gnu.xml.dom.ls.ReaderInputStream;
+
+/**
+ * URI resolver for XSLT.
+ * This resolver parses external entities into DOMSources. It
+ * maintains a cache of URIs to DOMSources to avoid expensive re-parsing.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class XSLURIResolver
+ implements URIResolver
+{
+
+ final Map<String,Long> lastModifiedCache = new HashMap<String,Long>();
+ final Map<String,Node> nodeCache = new HashMap<String,Node>();
+ DocumentBuilder builder;
+ URIResolver userResolver;
+ ErrorListener userListener;
+
+ void setUserResolver(URIResolver userResolver)
+ {
+ this.userResolver = userResolver;
+ }
+
+ void setUserListener(ErrorListener userListener)
+ {
+ this.userListener = userListener;
+ }
+
+ /**
+ * Clear the cache.
+ */
+ void flush()
+ {
+ lastModifiedCache.clear();
+ nodeCache.clear();
+ }
+
+ public Source resolve(String href, String base)
+ throws TransformerException
+ {
+ Source source = null;
+ if (userResolver != null)
+ {
+ source = userResolver.resolve(base, href);
+ }
+ return resolveDOM(source, href, base);
+ }
+
+ DOMSource resolveDOM(Source source, String base, String href)
+ throws TransformerException
+ {
+ if (source != null && source instanceof DOMSource)
+ {
+ return (DOMSource) source;
+ }
+ String systemId = (source == null) ? null : source.getSystemId();
+ long lastModified = 0L, lastLastModified = 0L;
+
+ try
+ {
+ Node node = null;
+ InputStream in = null;
+ if (source != null && source instanceof StreamSource)
+ {
+ StreamSource ss = (StreamSource) source;
+ in = ss.getInputStream();
+ if (in == null)
+ {
+ Reader reader = ss.getReader();
+ if (reader != null)
+ {
+ in = new ReaderInputStream(reader);
+ }
+ }
+ }
+ else if (source != null && source instanceof SAXSource)
+ {
+ SAXSource ss = (SAXSource) source;
+ InputSource input = ss.getInputSource();
+ if (input != null)
+ {
+ if (systemId == null)
+ systemId = input.getSystemId();
+ XMLReader reader = ss.getXMLReader();
+ if (reader != null)
+ return parse(input, reader);
+ }
+ }
+ if (in == null)
+ {
+ URL url = resolveURL(systemId, base, href);
+ if (url != null)
+ {
+ systemId = url.toString();
+ node = nodeCache.get(systemId);
+ // Is the resource up to date?
+ URLConnection conn = url.openConnection();
+ Long llm = lastModifiedCache.get(systemId);
+ if (llm != null)
+ {
+ lastLastModified = llm.longValue();
+ conn.setIfModifiedSince(lastLastModified);
+ }
+ conn.connect();
+ lastModified = conn.getLastModified();
+ if (node != null &&
+ lastModified > 0L &&
+ lastModified <= lastLastModified)
+ {
+ // Resource unchanged
+ return new DOMSource(node, systemId);
+ }
+ else
+ {
+ // Resource new or modified
+ in = conn.getInputStream();
+ nodeCache.put(systemId, node);
+ lastModifiedCache.put(systemId, new Long(lastModified));
+ }
+ }
+ else
+ {
+ throw new TransformerException("can't resolve URL: " +
+ systemId);
+ }
+ }
+ InputSource input = new InputSource(in);
+ input.setSystemId(systemId);
+ DocumentBuilder builder = getDocumentBuilder();
+ node = builder.parse(input);
+ return new DOMSource(node, systemId);
+ }
+ catch (IOException e)
+ {
+ throw new TransformerException(e);
+ }
+ catch (SAXException e)
+ {
+ throw new TransformerException(e);
+ }
+ }
+
+ URL resolveURL(String systemId, String base, String href)
+ throws IOException
+ {
+ URL url = null;
+ try
+ {
+ if (systemId != null)
+ {
+ try
+ {
+ url = new URL(systemId);
+ }
+ catch (MalformedURLException e)
+ {
+ // Try building from base + href
+ }
+ }
+ if (url == null)
+ {
+ if (base != null)
+ {
+ URL baseURL = new URL(base);
+ url = new URL(baseURL, href);
+ }
+ else if (href != null)
+ {
+ url = new URL(href);
+ }
+ else
+ {
+ // See below
+ throw new MalformedURLException(systemId);
+ }
+ }
+ return url;
+ }
+ catch (MalformedURLException e)
+ {
+ // Fall back to local filesystem
+ File file = null;
+ if (href == null)
+ {
+ href = systemId;
+ }
+ if (base != null)
+ {
+ int lsi = base.lastIndexOf(File.separatorChar);
+ if (lsi != -1 && lsi < base.length() - 1)
+ {
+ base = base.substring(0, lsi);
+ }
+ File baseFile = new File(base);
+ file = new File(baseFile, href);
+ }
+ else if (href != null)
+ {
+ file = new File(href);
+ }
+ return (file == null) ? null : file.toURL();
+ }
+ }
+
+ DocumentBuilder getDocumentBuilder()
+ throws TransformerException
+ {
+ try
+ {
+ if (builder == null)
+ {
+ DocumentBuilderFactory factory =
+ DocumentBuilderFactory.newInstance();
+ factory.setNamespaceAware(true);
+ factory.setExpandEntityReferences(true);
+ builder = factory.newDocumentBuilder();
+ }
+ if (userResolver != null)
+ {
+ builder.setEntityResolver(new URIResolverEntityResolver(userResolver));
+ }
+ if (userListener != null)
+ {
+ builder.setErrorHandler(new ErrorListenerErrorHandler(userListener));
+ }
+ return builder;
+ }
+ catch (Exception e)
+ {
+ throw new TransformerException(e);
+ }
+ }
+
+ DOMSource parse(InputSource source, XMLReader reader)
+ throws SAXException, IOException
+ {
+ SAXEventSink eventSink = new SAXEventSink();
+ eventSink.setReader(reader);
+ eventSink.setNamespaceAware(true);
+ reader.setContentHandler(eventSink);
+ reader.setDTDHandler(eventSink);
+ reader.setProperty("http://xml.org/sax/properties/lexical-handler",
+ eventSink);
+ reader.setProperty("http://xml.org/sax/properties/declaration-handler",
+ eventSink);
+ // XXX entityResolver
+ // XXX errorHandler
+ reader.parse(source);
+ Document doc = eventSink.getDocument();
+ String systemId = source.getSystemId();
+ if (systemId != null && doc instanceof DomDocument)
+ ((DomDocument) doc).setDocumentURI(systemId);
+ return new DOMSource(doc, systemId);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/package.html b/libjava/classpath/gnu/xml/transform/package.html
new file mode 100644
index 000000000..d4355966c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/package.html
@@ -0,0 +1,77 @@
+<html>
+<body>
+
+<h1>GNU JAXP XSL transformer</h1>
+
+<div>
+This package contains a Java XSL transformer compliant with the JAXP
+specification. It depends on the GNU DOM and XPath implementations, and
+will generate GNU DOM nodes unless a specific target from another
+implementation was given. It understands DOM, SAX, and stream sources
+and result sinks and supports these JAXP features.
+</div>
+
+<div>
+To use this transformer, set the system property
+<code>javax.xml.transform.TransformerFactory</code> to the value
+<code>gnu.xml.transform.TransformerFactoryImpl</code>. You can then
+instantiate <a href='TransformerFactory.html'>TransformerFactory</a>
+and transformers in the ordinary manner. Reuse of stylesheets is
+supported using the JAXP <a href='Templates.html'>Templates</a>
+mechanism.
+</div>
+
+<h3>Architecture</h3>
+
+<div>
+When given a stylesheet source, this implementation compiles it internally
+into a Stylesheet object, which is a container for templates and state.
+Each stylesheet instruction is represented by a subclass of TemplateNode,
+which is arranged in a directed graph: each TemplateNode has a reference
+to its first child and the next node.
+</div>
+
+<div>
+The transformation process consists of identifying the Template that matches
+the root of the source context, and calling <code>apply</code> on its
+corresponding TemplateNode. This in turn processes its children and next
+TemplateNode, depending on the semantics of each node type.
+</div>
+
+<div>
+Template nodes may reference XPath expressions or patterns. These are fully
+compiled to objects of type <a href='../xpath/Expr.html'>Expr</a> at the
+time the stylesheet is compiled.
+</div>
+
+<h3>Conformance</h3>
+
+<div>
+This implementation is feature complete, but the XSLT specification is
+large and there are still many bugs that need to be ironed out. It has
+been tested against the OASIS XSLT TC test suite, comprising unit tests
+from the Xalan project and Microsoft. Conformance to these unit tests
+is approximately 70% at the current time, although normal usage of the
+transformer should involve relatively few surprises (the test suite is
+designed to test very complex and obscure functionality).
+</div>
+
+<h3>Known bugs</h3>
+
+<ul>
+<li>When reusing stylesheets using the JAXP Templates mechanism, XSL
+<code>apply-imports</code> instructions will not work.</li>
+<li>XPath filter expressions do not always work as expected (this is a
+problem with the GNU XPath implementation rather than the transformer).
+This can result in problems with the <code>position()</code> function,
+as well as <code>select</code> expressions and numbering.</li>
+</ul>
+
+<div>
+Obviously we'd like to improve conformance and fix these bugs. If you're
+interested in working on any of these issues please
+<a href='mailto:classpathx-xml@gnu.org'>contact us</a>.
+</div>
+
+</body>
+</html>
diff --git a/libjava/classpath/gnu/xml/util/DoParse.java b/libjava/classpath/gnu/xml/util/DoParse.java
new file mode 100644
index 000000000..02c80d3a2
--- /dev/null
+++ b/libjava/classpath/gnu/xml/util/DoParse.java
@@ -0,0 +1,302 @@
+/* DoParse.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.util;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.io.IOException;
+
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
+
+import gnu.xml.pipeline.EventConsumer;
+import gnu.xml.pipeline.EventFilter;
+import gnu.xml.pipeline.NSFilter;
+import gnu.xml.pipeline.PipelineFactory;
+import gnu.xml.pipeline.TeeConsumer;
+import gnu.xml.pipeline.ValidationConsumer;
+import gnu.xml.pipeline.WellFormednessFilter;
+
+/**
+ * This class provides a driver which may be invoked from the command line
+ * to process a document using a SAX2 parser and a specified XML processing
+ * pipeline.
+ * This facilitates some common types of command line tools, such as parsing an
+ * XML document in order test it for well formedness or validity.
+ *
+ * <p>The SAX2 XMLReaderFactory should return a SAX2 XML parser which
+ * supports both of the standardized extension handlers (for declaration
+ * and lexical events). That parser will be used to produce events.
+ *
+ * <p>The first parameter to the command gives the name of the document that
+ * will be given to that processor. If it is a file name, it is converted
+ * to a URL first.
+ *
+ * <p>The second parameter describes a simple processing pipeline, and will
+ * be used as input to {@link gnu.xml.pipeline.PipelineFactory}
+ * methods which identify the processing to be done. Examples of such a
+ * pipeline include <pre>
+ *
+ * nsfix | validate <em>to validate the input document </em>
+ * nsfix | write ( stdout ) <em>to echo the file as XML text</em>
+ * dom | nsfix | write ( stdout ) <em>parse into DOM, print the result</em>
+ * </pre>
+ *
+ * <p> Relatively complex pipelines can be described on the command line, but
+ * not all interesting ones will require as little configuration as can be done
+ * in that way. Put filters like "nsfix", perhaps followed by "validate",
+ * at the front of the pipeline so they can be optimized out if a parser
+ * supports those modes natively.
+ *
+ * <p> If the parsing is aborted for any reason, the JVM will exit with a
+ * failure code. If a validating parse was done then both validation and
+ * well formedness errors will cause a failure. A non-validating parse
+ * will report failure on well formedness errors.
+ *
+ * @see gnu.xml.pipeline.PipelineFactory
+ *
+ * @author David Brownell
+ */
+final public class DoParse
+{
+ private DoParse () { /* no instances allowed */ }
+
+ // first reported nonrecoverable error
+ private static SAXParseException fatal;
+
+ // error categories
+ private static int errorCount;
+ private static int fatalCount;
+
+ /**
+ * Command line invoker for this class; pass a filename or URL
+ * as the first argument, and a pipeline description as the second.
+ * Make sure to use filters to condition the input to stages that
+ * require it; an <em>nsfix</em> filter will be a common requirement,
+ * to restore syntax that SAX2 parsers delete by default. Some
+ * conditioning filters may be eliminated by setting parser options.
+ * (For example, "nsfix" can set the "namespace-prefixes" feature to
+ * a non-default value of "true". In the same way, "validate" can set
+ * the "validation" feature to "true".)
+ */
+ public static void main (String argv [])
+ throws IOException
+ {
+ int exitStatus = 1;
+
+ if (argv.length != 2) {
+ System.err.println ("Usage: DoParse [filename|URL] pipeline-spec");
+ System.err.println ("Example pipeline specs:");
+ System.err.println (" 'nsfix | validate'");
+ System.err.println (
+ " ... restore namespace syntax, validate");
+ System.err.println (" 'nsfix | write ( stdout )'");
+ System.err.println (
+ " ... restore namespace syntax, write to stdout as XML"
+ );
+ System.exit (1);
+ }
+
+ try {
+ //
+ // Get input source for specified document (or try ;-)
+ //
+ argv [0] = Resolver.getURL (argv [0]);
+ InputSource input = new InputSource (argv [0]);
+
+ //
+ // Get the producer, using the system default parser (which
+ // can be overridden for this particular invocation).
+ //
+ // And the pipeline, using commandline options.
+ //
+ XMLReader producer;
+ EventConsumer consumer;
+
+ producer = XMLReaderFactory.createXMLReader ();
+
+ //
+ // XXX pipeline factory now has a pre-tokenized input
+ // method, use it ... that way at least some params
+ // can be written using quotes (have spaces, ...)
+ //
+ consumer = PipelineFactory.createPipeline (argv [1]);
+
+ //
+ // XXX want commandline option for tweaking error handler.
+ // Want to be able to present warnings.
+ //
+ producer.setErrorHandler (new MyErrorHandler ());
+
+ // XXX need facility enabling resolving to local DTDs
+
+ //
+ // Parse. The pipeline may get optimized a bit, so we
+ // can't always fail cleanly for validation without taking
+ // a look at the filter stages.
+ //
+ EventFilter.bind (producer, consumer);
+ producer.parse (input);
+
+ try {
+ if (producer.getFeature (
+ "http://org.xml/sax/features/validation"))
+ exitStatus = ((errorCount + fatalCount) > 0) ? 1 : 0;
+ else if (fatalCount == 0)
+ exitStatus = 0;
+ } catch (SAXException e) {
+ if (hasValidator (consumer))
+ exitStatus = ((errorCount + fatalCount) > 0) ? 1 : 0;
+ else if (fatalCount == 0)
+ exitStatus = 0;
+ }
+
+ } catch (java.net.MalformedURLException e) {
+ System.err.println ("** Malformed URL: " + e.getMessage ());
+ System.err.println ("Is '" + argv [0] + "' a non-existent file?");
+ e.printStackTrace ();
+ // e.g. FNF
+
+ } catch (SAXParseException e) {
+ if (e != fatal) {
+ System.err.print (printParseException ("Parsing Aborted", e));
+ e.printStackTrace ();
+ if (e.getException () != null) {
+ System.err.println ("++ Wrapped exception:");
+ e.getException ().printStackTrace ();
+ }
+ }
+
+ } catch (SAXException e) {
+ Exception x = e;
+ if (e.getException () != null)
+ x = e.getException ();
+ x.printStackTrace ();
+
+ } catch (Throwable t) {
+ t.printStackTrace ();
+ }
+
+ System.exit (exitStatus);
+ }
+
+ // returns true if saw a validator (before end or unrecognized node)
+ // false otherwise
+ private static boolean hasValidator (EventConsumer e)
+ {
+ if (e == null)
+ return false;
+ if (e instanceof ValidationConsumer)
+ return true;
+ if (e instanceof TeeConsumer) {
+ TeeConsumer t = (TeeConsumer) e;
+ return hasValidator (t.getFirst ())
+ || hasValidator (t.getRest ());
+ }
+ if (e instanceof WellFormednessFilter
+ || e instanceof NSFilter
+ )
+ return hasValidator (((EventFilter)e).getNext ());
+
+ // else ... gee, we can't know. Assume not.
+
+ return false;
+ }
+
+ static class MyErrorHandler implements ErrorHandler
+ {
+ // dump validation errors, but continue
+ public void error (SAXParseException e)
+ throws SAXParseException
+ {
+ errorCount++;
+ System.err.print (printParseException ("Error", e));
+ }
+
+ public void warning (SAXParseException e)
+ throws SAXParseException
+ {
+ // System.err.print (printParseException ("Warning", e));
+ }
+
+ // try to continue fatal errors, in case a parser reports more
+ public void fatalError (SAXParseException e)
+ throws SAXParseException
+ {
+ fatalCount++;
+ if (fatal == null)
+ fatal = e;
+ System.err.print (printParseException ("Nonrecoverable Error", e));
+ }
+ }
+
+ static private String printParseException (
+ String label,
+ SAXParseException e
+ ) {
+ CPStringBuilder buf = new CPStringBuilder ();
+ int temp;
+
+ buf.append ("** ");
+ buf.append (label);
+ buf.append (": ");
+ buf.append (e.getMessage ());
+ buf.append ('\n');
+ if (e.getSystemId () != null) {
+ buf.append (" URI: ");
+ buf.append (e.getSystemId ());
+ buf.append ('\n');
+ }
+ if ((temp = e.getLineNumber ()) != -1) {
+ buf.append (" line: ");
+ buf.append (temp);
+ buf.append ('\n');
+ }
+ if ((temp = e.getColumnNumber ()) != -1) {
+ buf.append (" char: ");
+ buf.append (temp);
+ buf.append ('\n');
+ }
+
+ return buf.toString ();
+ }
+}
diff --git a/libjava/classpath/gnu/xml/util/DomParser.java b/libjava/classpath/gnu/xml/util/DomParser.java
new file mode 100644
index 000000000..66e3f1d87
--- /dev/null
+++ b/libjava/classpath/gnu/xml/util/DomParser.java
@@ -0,0 +1,804 @@
+/* DomParser.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.util;
+
+import java.util.Enumeration;
+import java.util.Locale;
+
+import org.xml.sax.*;
+import org.xml.sax.helpers.AttributesImpl;
+import org.xml.sax.helpers.NamespaceSupport;
+import org.xml.sax.ext.DeclHandler;
+import org.xml.sax.ext.DefaultHandler2;
+import org.xml.sax.ext.LexicalHandler;
+
+import org.w3c.dom.*;
+
+
+/**
+ * This parser emits SAX2 parsing events as it traverses a DOM tree, using
+ * any conformant implementation of DOM. It exposes all SAX1 features,
+ * and the following SAX2 features and properties (as
+ * identified by standard URIs which are not fully provided here). Note
+ * that if a Level 1 DOM implementation is given, then this behaves as if
+ * namespaces were disabled, and namespace prefixes were enabled. </p>
+ *
+ * <table border="1" width='100%' cellpadding='3' cellspacing='0'>
+ * <tr bgcolor='#ccccff'>
+ * <th><font size='+1'>Name</font></th>
+ * <th><font size='+1'>Notes</font></th></tr>
+ *
+ * <tr><td colspan=2><center><em>Features ... URL prefix is
+ * <b>http://xml.org/sax/features/</b></em></center></td></tr>
+ *
+ * <tr><td>(URL)/external-general-entities</td>
+ * <td>false (does no parsing)</td></tr>
+ * <tr><td>(URL)/external-parameter-entities</td>
+ * <td>false (does no parsing)</td></tr>
+ * <tr><td>(URL)/namespaces</td>
+ * <td>Value is fixed at <em>true</em></td></tr>
+ * <tr><td>(URL)/namespace-prefixes</td>
+ * <td>Value is settable, defaulting to <em>false</em>
+ * (<code>xmlns</code> attributes hidden, and names aren't prefixed)
+ * </td></tr>
+ * <tr><td>(URL)/string-interning</td>
+ * <td>Value is fixed at <em>false</em> (DOM provides no
+ * guarantees as to interning)</td></tr>
+ * <tr><td>(URL)/validation</td>
+ * <td>false (does no parsing)</td></tr>
+ * <tr><td>(URL)/lexical-handler/parameter-entities</td>
+ * <td>false (DOM doesn't do parameter entities)</td></tr>
+ *
+ * <tr><td colspan=2><center><em>Properties ... URL prefix is
+ * <b>http://xml.org/sax/properties/</b></em></center></td></tr>
+ *
+ *
+ * <tr><td>(URL)/dom-node</td>
+ * <td>This property may be set before parsing to hold a DOM
+ * <em>Document</em> node; any arguments given to <em>parse</em>
+ * methods are ignored. When retrieved
+ * during a parse, this value contains the "current" DOM node.
+ * </td></tr>
+ * <tr><td>(URL)/declaration-handler</td>
+ * <td>A declaration handler may be provided. Declaration of external
+ * general entities is exposed, but not parameter entities; none of the
+ * entity names reported here will begin with "%". </td></tr>
+ * <tr><td>(URL)/lexical-handler</td>
+ * <td>A lexical handler may be provided. While the start and end of
+ * any external subset are reported, expansion of other parameter
+ * entities (e.g. inside attribute list declarations) is not exposed.
+ * Expansion of general entities within attributes is also not exposed
+ * (see below).</td></tr>
+ * </table>
+ *
+ * <P> The consequences of modifying a DOM document tree as it is being walked
+ * by this "parser" are unspecified; don't do it! </P>
+ *
+ * @author David Brownell
+ */
+final public class DomParser implements XMLReader
+{
+ // Stuff used internally to route events correctly
+ private DefaultHandler2 defaultHandler = new DefaultHandler2 ();
+
+ // per-parse SAX stuff
+ private ContentHandler contentHandler = defaultHandler;
+ private DTDHandler dtdHandler = defaultHandler;
+ private DeclHandler declHandler = defaultHandler;
+ private LexicalHandler lexicalHandler = defaultHandler;
+
+ // shared context
+ private ErrorHandler errHandler = defaultHandler;
+ private EntityResolver resolver = defaultHandler;
+ private Locale locale = Locale.getDefault ();
+
+ // parser state
+ private Node start;
+ private Node current;
+ private boolean isL2;
+ private boolean showNamespaces = true;
+ private boolean showXML1_0 = false;
+ private NamespaceSupport prefixStack = new NamespaceSupport ();
+ private boolean isDocument;
+
+
+ /**
+ * Constructs an unitialized <b>SAX2</b> parser.
+ */
+ public DomParser () {
+ }
+
+ /**
+ * Constructs an <b>SAX2</b> parser initialized to traverse the specified
+ * DOM tree. If the node is a document, the startDocument() and
+ * endDocument() calls bracket the calls exposing children.
+ */
+ public DomParser (Node node) {
+ setStart (node);
+ }
+
+
+ // stuff that most components in an application should be sharing:
+ // resolver and error locale.
+
+ /**
+ * <b>SAX2</b>: Returns the object used when resolving external
+ * entities during parsing (both general and parameter entities).
+ */
+ public EntityResolver getEntityResolver ()
+ {
+ return resolver;
+ }
+
+ /**
+ * <b>SAX1</b>: Provides an object which may be used when resolving external
+ * entities during parsing (both general and parameter entities).
+ */
+ public void setEntityResolver (EntityResolver resolver)
+ {
+ if (resolver == null)
+ resolver = defaultHandler;
+ this.resolver = resolver;
+ }
+
+ /**
+ * <b>SAX1</b>: Identifies the locale which the parser should use for the
+ * diagnostics it provides.
+ *
+ * @exception SAXException as defined in the specification for
+ * <em>org.xml.sax.Parser.setLocale()</em>
+ */
+ public void setLocale (Locale locale)
+ throws SAXException
+ {
+ if (locale == null)
+ locale = Locale.getDefault ();
+ this.locale = locale;
+ }
+
+
+ // different modules will tend to handle error handling the same,
+ // but it may not be the same through the whole app
+
+ /**
+ * <b>SAX2</b>: Returns the object used to receive callbacks for XML
+ * errors of all levels (fatal, nonfatal, warning).
+ */
+ public ErrorHandler getErrorHandler ()
+ {
+ return errHandler;
+ }
+
+ /**
+ * <b>SAX1</b>: Provides an object which receives callbacks for XML errors
+ * of all levels (fatal, nonfatal, warning).
+ */
+ public void setErrorHandler (ErrorHandler handler)
+ {
+ if (handler == null)
+ handler = defaultHandler;
+ errHandler = handler;
+ }
+
+
+ // stuff different parts of a module will handle differently
+
+ /**
+ * <b>SAX2</b>: Returns the object used to report the logical
+ * content of an XML document.
+ */
+ public ContentHandler getContentHandler ()
+ {
+ return contentHandler;
+ }
+
+ /**
+ * <b>SAX2</b>: Assigns the object used to report the logical
+ * content of an XML document.
+ */
+ public void setContentHandler (ContentHandler handler)
+ {
+ if (handler == null)
+ handler = defaultHandler;
+ contentHandler = handler;
+ }
+
+ /**
+ * <b>SAX2</b>: Returns the object used to process declarations related
+ * to notations and unparsed entities.
+ */
+ public DTDHandler getDTDHandler ()
+ {
+ return dtdHandler;
+ }
+
+ /**
+ * <b>SAX1</b>: Provides an object which may be used to intercept
+ * declarations related to notations and unparsed entities.
+ */
+ public void setDTDHandler (DTDHandler handler)
+ {
+ if (handler == null)
+ handler = defaultHandler;
+ dtdHandler = handler;
+ }
+
+
+ /**
+ * <b>SAX1</b>: Parses the previously provided DOM document (the
+ * input parameter is ignored). When this returns, that same
+ * document may be parsed again without needing a "reset".
+ *
+ * @param uri ignored (pass an empty string)
+ * @exception SAXException as defined in the specification for
+ * <em>org.xml.sax.Parser.parse()</em>
+ */
+ public void parse (String uri) throws SAXException
+ {
+ parse ();
+ }
+
+ /**
+ * <b>SAX1</b>: Parses the previously provided DOM document (the
+ * input parameter is ignored). When this returns, that same
+ * document may be parsed again without needing a "reset".
+ *
+ * @param input ignored
+ * @exception SAXException as defined in the specification for
+ * <em>org.xml.sax.Parser.parse()</em>
+ */
+ public void parse (InputSource input) throws SAXException
+ {
+ parse ();
+ }
+
+ private void parse () throws SAXException
+ {
+ try {
+ walk ();
+ } finally {
+ if (isDocument)
+ contentHandler.endDocument ();
+ current = null;
+ prefixStack.reset ();
+ }
+ }
+
+ private boolean getIsL2 (Node node)
+ {
+ DOMImplementation impl;
+ Document doc;
+
+ if (node instanceof Document)
+ doc = (Document) node;
+ else
+ doc = node.getOwnerDocument ();
+ if (doc == null)
+ throw new RuntimeException ("? unowned node - L2 DTD ?");
+ impl = doc.getImplementation ();
+ return impl.hasFeature ("XML", "2.0");
+ }
+
+
+ private static final String FEATURES = "http://xml.org/sax/features/";
+ private static final String HANDLERS = "http://xml.org/sax/properties/";
+
+ /**
+ * <b>SAX2</b>: Tells whether this parser supports the specified feature.
+ */
+ public boolean getFeature (String name)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ // basically, none are relevant -- they relate more to
+ // parsing than to walking a "parse tree".
+
+ // FIXME: DOM feature to expose interning?
+
+ if ((FEATURES + "validation").equals (name)
+ || (FEATURES + "external-general-entities")
+ .equals (name)
+ || (FEATURES + "external-parameter-entities")
+ .equals (name)
+ || (FEATURES + "string-interning").equals (name)
+ )
+ return false;
+
+ if ((FEATURES + "namespaces").equals (name))
+ return showNamespaces;
+ if ((FEATURES + "namespace-prefixes").equals (name))
+ return showXML1_0;
+
+ throw new SAXNotRecognizedException (name);
+ }
+
+ /**
+ * <b>SAX2</b>: Returns the specified property. At this time only
+ * the declaration and lexical handlers, and current the "DOM" node,
+ * are supported.
+ */
+ public Object getProperty (String name)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ if ((HANDLERS + "declaration-handler").equals (name))
+ return declHandler == defaultHandler ? null : declHandler;
+ if ((HANDLERS + "lexical-handler").equals (name))
+ return lexicalHandler == defaultHandler ? null : lexicalHandler;
+
+ if ((HANDLERS + "dom-node").equals (name))
+ return current;
+
+ // unknown properties
+ throw new SAXNotRecognizedException (name);
+ }
+
+ /**
+ * <b>SAX2</b>: Sets the state of features supported in this parser.
+ * Only the namespace support features are mutable.
+ */
+ public void setFeature (String name, boolean state)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ if (current != null)
+ throw new IllegalStateException ("feature change midparse");
+
+ boolean value = getFeature (name);
+
+ if (value == state)
+ return;
+
+ if ((FEATURES + "namespaces").equals (name)) {
+ if (!showXML1_0 && state == false)
+ throw new SAXNotSupportedException ("Illegal namespace "
+ + "processing configuration");
+ showNamespaces = state;
+ return;
+ }
+ if ((FEATURES + "namespace-prefixes").equals (name)) {
+ if (!showNamespaces && state == false)
+ throw new SAXNotSupportedException ("Illegal namespace "
+ + "processing configuration");
+ showXML1_0 = state;
+ return;
+ }
+
+ throw new SAXNotSupportedException (name);
+ }
+
+ /**
+ * <b>SAX2</b>: Assigns the specified property. At this time only
+ * declaration and lexical handlers, and the initial DOM document, are
+ * supported. These must not be changed to values of the wrong type.
+ * Like SAX1 handlers, these handlers may be changed at any time.
+ * Like SAX1 input source or document URI, the initial DOM document
+ * may not be changed during a parse.
+ */
+ public void setProperty (String name, Object state)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ if ((HANDLERS + "declaration-handler").equals (name)) {
+ if (!(state instanceof DeclHandler || state == null))
+ throw new SAXNotSupportedException (name);
+ declHandler = (DeclHandler) state;
+ return;
+ }
+
+ if ((HANDLERS + "lexical-handler").equals (name)) {
+ if (!(state instanceof LexicalHandler || state == null))
+ throw new SAXNotSupportedException (name);
+ lexicalHandler = (LexicalHandler) state;
+ return;
+ }
+
+ if ((HANDLERS + "dom-node").equals (name)) {
+ if (state == null || state instanceof Node) {
+ if (current != null)
+ throw new SAXNotSupportedException (
+ "property is readonly during parse: " + name);
+ setStart ((Node) state);
+ return;
+ }
+ throw new SAXNotSupportedException ("not a DOM Node");
+ }
+
+ // unknown properties
+ throw new SAXNotRecognizedException (name);
+ }
+
+ private void setStart (Node property)
+ {
+ start = property;
+ if (start != null) {
+ isL2 = getIsL2 (start);
+ isDocument = (start instanceof Document);
+ }
+ }
+
+ //
+ // Non-recursive walk, using DOM state when backtracking is needed
+ //
+ private void walk ()
+ throws SAXException
+ {
+ int type;
+ NamedNodeMap nodes;
+ int length;
+ AttributesImpl attrs = new AttributesImpl ();
+ char chars [];
+ String ns, local;
+
+ synchronized (this) {
+ if (current != null)
+ throw new IllegalStateException ("already walking tree");
+
+ // JVM guarantees assignments are atomic; so no other
+ // thread could get this far till this walk's done.
+ current = start;
+ }
+
+ for (;;) {
+ type = current.getNodeType ();
+
+ //
+ // First, visit the current node, including any "start" calls
+ //
+ switch (type) {
+
+ case Node.DOCUMENT_NODE:
+ contentHandler.startDocument ();
+ break;
+
+ case Node.ELEMENT_NODE:
+ nodes = current.getAttributes ();
+ length = nodes.getLength ();
+ prefixStack.pushContext ();
+ for (int i = 0; i < length; i++) {
+ Attr attr = (Attr) nodes.item (i);
+ String name = attr.getNodeName ();
+
+ if (showNamespaces && name.startsWith ("xmlns")) {
+ String prefix;
+ String uri;
+
+ // NOTE: DOM L2 (CR2+ and REC) violate the
+ // Namespaces REC, treat "xmlns" like a strange
+ // attribute instead of a magic token
+ if ("xmlns".equals (name))
+ prefix = "";
+ else
+ prefix = name.substring (6);
+ uri = attr.getNodeValue ();
+
+ prefixStack.declarePrefix (prefix, uri);
+ contentHandler.startPrefixMapping (prefix, uri);
+
+ if (!showXML1_0)
+ continue;
+ }
+
+ //
+ // NOTE: DOM doesn't record the attribute type info
+ // which SAX exposes; so this always reports CDATA.
+ //
+ // NOTE: SAX doesn't expose the isSpecified info which
+ // DOM exposes; that's discarded here. Similarly with
+ // the information DOM hides inside itself about what
+ // the default values for an attribute are.
+ //
+ if (showNamespaces) {
+ if (isL2) {
+ if ((ns = attr.getNamespaceURI ()) == null)
+ ns = "";
+ // Note: SAX2 and DOM handle "local" names
+ // differently
+ if ((local = attr.getLocalName ()) == null)
+ local = name;
+ } else {
+// XXX
+ throw new RuntimeException (
+ "NYI, ns lookup when parsing L1 DOM");
+ }
+ } else
+ ns = local = "";
+ attrs.addAttribute (ns, local, name,
+ "CDATA", attr.getNodeValue ());
+ }
+ if (showNamespaces) {
+ if (isL2) {
+ if ((ns = current.getNamespaceURI ()) == null)
+ ns = "";
+ // Note: SAX2 and DOM handle "local" names differently
+ if ((local = current.getLocalName ()) == null)
+ local = current.getNodeName ();
+ } else {
+// XXX
+ throw new RuntimeException (
+ "NYI, ns lookup when parsing L1 DOM");
+ }
+ } else
+ ns = local = "";
+ contentHandler.startElement (ns, local,
+ current.getNodeName (), attrs);
+ if (length != 0)
+ attrs.clear ();
+ break;
+
+ case Node.CDATA_SECTION_NODE:
+ lexicalHandler.startCDATA ();
+ chars = current.getNodeValue ().toCharArray ();
+ contentHandler.characters (chars, 0, chars.length);
+ lexicalHandler.endCDATA ();
+ break;
+
+ case Node.COMMENT_NODE:
+ chars = current.getNodeValue ().toCharArray ();
+ lexicalHandler.comment (chars, 0, chars.length);
+ break;
+
+ case Node.DOCUMENT_TYPE_NODE:
+ {
+ DocumentType doctype = (DocumentType) current;
+
+ //
+ // Only DOM L2 supports recreating even some DTDs in full.
+ //
+ if (isL2) {
+ lexicalHandler.startDTD (doctype.getName (),
+ doctype.getPublicId (),
+ doctype.getSystemId ());
+ } else
+ lexicalHandler.startDTD (doctype.getName (),
+ null, null);
+
+ //
+ // The only sure way to recreate is to provide both the
+ // internal and external subsets. Otherwise, only part
+ // of the job can be done ... because from the DTD, DOM
+ // discards both the critical data, like the attribute and
+ // element declarations, as well as the PIs and comments
+ // that are used to hold their documentation.
+ //
+ // Even the entity and notation declarations that it can
+ // expose can't be recorded without proprietary extensions.
+ //
+ // We construct a comment to tell what we know about how
+ // (in)complete this particular really DTD is.
+ //
+ {
+ String message;
+ char buf [];
+
+ //
+ // Though DOM L2 lets the whole doctype be recreated,
+ // SAX2 can't represent it (input or output).
+ // So this will be the typical case.
+ //
+ if (isL2 && doctype.getInternalSubset () != null)
+ message =
+ " Full DTD known; can't be shown using SAX2. ";
+
+ //
+ // Otherwise, we'll concoct a partial DTD. If there's
+ // any more data here at all, it was provided using a
+ // (proprietary) extension to DOM.
+ //
+ else
+ message =
+ " This DTD was was recreated using incomplete DOM L2 records. ";
+
+ buf = message.toCharArray ();
+ lexicalHandler.comment (buf, 0, buf.length);
+ }
+
+ // report notations first
+ nodes = doctype.getNotations ();
+ length = nodes.getLength ();
+ for (int i = 0; i < length; i++) {
+ Notation notation = (Notation) nodes.item (i);
+ dtdHandler.notationDecl (
+ notation.getNodeName (),
+ notation.getPublicId (),
+ notation.getSystemId ());
+ }
+
+ // then parsed and unparsed external general entities
+ nodes = doctype.getEntities ();
+ length = nodes.getLength ();
+ for (int i = 0; i < length; i++) {
+ Entity entity = (Entity) nodes.item (i);
+ String notation = entity.getNotationName ();
+
+ if (notation != null)
+ dtdHandler.unparsedEntityDecl (
+ entity.getNodeName (),
+ entity.getPublicId (),
+ entity.getSystemId (),
+ notation);
+ else if (entity.getSystemId () != null)
+ declHandler.externalEntityDecl (
+ entity.getNodeName (),
+ entity.getPublicId (),
+ entity.getSystemId ());
+
+ //
+ // NOTE: DOM doesn't clearly provide internal
+ // entity support; but in case someone tries to
+ // fudge such support, we defend ourselves above.
+ //
+ // NOTE: DOM doesn't expose parameter entities
+ // (thank you thank you thank you thank you)
+ //
+ }
+
+ //
+ // NOTE: DOM (levels 1 and 2) doesn't expose real
+ // typing information (element or attribute decls),
+ // as exposed by SAX2 declaration handlers.
+ //
+ lexicalHandler.endDTD ();
+ }
+ break;
+
+ case Node.ENTITY_REFERENCE_NODE:
+ // this isn't done except (a) in content, and
+ // (b) not within a start tag (att value)
+ lexicalHandler.startEntity (current.getNodeName ());
+ break;
+
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ contentHandler.processingInstruction (
+ current.getNodeName (), current.getNodeValue ());
+ break;
+
+ case Node.TEXT_NODE:
+ chars = current.getNodeValue ().toCharArray ();
+ contentHandler.characters (chars, 0, chars.length);
+ break;
+
+ default:
+ // e.g. fragments, entities, notations, attributes
+ throw new SAXException ("Illegal DOM Node type in Document: "
+ + current.getNodeType ());
+ }
+
+ //
+ // Then, pick the next node to visit. If the next node isn't
+ // a child, an "end" call may be needed before moving on.
+ // If there's no next node, we're done.
+ //
+ Node next;
+
+ switch (type) {
+ case Node.DOCUMENT_NODE:
+ case Node.ELEMENT_NODE:
+ case Node.ENTITY_REFERENCE_NODE:
+ //
+ // For elements that can have children, visit those
+ // children before any siblings (i.e. depth first)
+ // and after visiting this node (i.e. preorder)
+ //
+ next = current.getFirstChild ();
+ if (next != null) {
+ current = next;
+ break;
+ }
+ //
+ // Else treat this like other childless nodes, but
+ // handle this node's "end" immediately.
+ //
+ callEnd (current);
+
+ // FALLTHROUGH
+
+ case Node.CDATA_SECTION_NODE:
+ case Node.COMMENT_NODE:
+ case Node.DOCUMENT_TYPE_NODE:
+ case Node.ENTITY_NODE:
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ case Node.TEXT_NODE:
+ //
+ // Use next sibling, if there is one.
+ // Else, climb up a level (calling "end")
+ // until we find an ancestral sibling
+ // or until we we climb off the top (FINISH)
+ //
+ for (;;) {
+ if ((next = current.getNextSibling ()) != null)
+ break;
+ current = current.getParentNode ();
+ if (current == null || current == start)
+ return;
+ callEnd (current);
+ }
+ current = next;
+ break;
+
+ default:
+ throw new SAXException (
+ "Illegal DOM Node type found: " + current.getNodeType ());
+ }
+ }
+ }
+
+ private void callEnd (Node node) throws SAXException
+ {
+ switch (node.getNodeType ()) {
+ // only these three container types may ever be found
+ // directly inside a Document.
+ case Node.DOCUMENT_NODE:
+ // for SAX conformance, endDocument must always
+ // be called ... it's done in a "finally" clause)
+ return;
+
+ case Node.ELEMENT_NODE:
+ if (showNamespaces) {
+ if (isL2)
+ contentHandler.endElement (
+ node.getNamespaceURI (),
+ node.getLocalName (),
+ node.getNodeName ());
+ else
+// XXX
+ throw new RuntimeException (
+ "NYI, ns lookup when parsing L1 DOM");
+ for (Enumeration e = prefixStack.getDeclaredPrefixes ();
+ e.hasMoreElements ();
+ ) {
+ contentHandler.endPrefixMapping ((String) e.nextElement ());
+ }
+ } else
+ contentHandler.endElement ("", "", node.getNodeName ());
+ prefixStack.popContext ();
+ return;
+
+ case Node.ENTITY_REFERENCE_NODE:
+ // see above -- in content, outside start tags.
+ lexicalHandler.endEntity (node.getNodeName ());
+ return;
+
+ // these can be given at the top level
+ case Node.DOCUMENT_FRAGMENT_NODE:
+ case Node.ATTRIBUTE_NODE:
+ return;
+
+ default:
+ throw new SAXException (
+ "Illegal DOM container type found: "
+ + current.getNodeType ());
+ }
+ }
+}
diff --git a/libjava/classpath/gnu/xml/util/Resolver.java b/libjava/classpath/gnu/xml/util/Resolver.java
new file mode 100644
index 000000000..b16f9617f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/util/Resolver.java
@@ -0,0 +1,263 @@
+/* Resolver.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Dictionary;
+
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * Utility implementation of a SAX resolver, which can be used to improve
+ * network utilization of SAX based XML components. It does this by
+ * supporting local caches of external entities.
+ * SAX parsers <em>should</em> use such local caches when possible.
+ *
+ * @see XCat
+ */
+public class Resolver implements EntityResolver, Cloneable
+{
+ /**
+ * Updates a dictionary used to map PUBLIC identifiers to file names,
+ * so that it uses the mappings in a specified directory.
+ *
+ * @param mappings Array of string pairs, where the first member
+ * of each pair is a PUBLIC identifier and the second is the
+ * name of a file, relative to the specified directory.
+ * @param directory File holding the specified files.
+ */
+ public static void addDirectoryMapping (
+ Dictionary table,
+ String mappings [][],
+ File directory
+ ) throws IOException
+ {
+ for (int i = 0; i < mappings.length; i++) {
+ File file = new File (directory, mappings [i][1]);
+ String temp;
+
+ if (!file.exists ()) // ?? log a warning ??
+ continue;
+
+ temp = fileToURL (file);
+ table.put (mappings [i][0], temp);
+ }
+ }
+
+ // FIXME: these *URL routines don't quite belong here, except
+ // that they're all in the same spirit of making it easy to
+ // use local filesystem URIs with XML parsers.
+
+ /**
+ * Provides the URL for a named file, without relying on the JDK 1.2
+ * {@link java.io.File#toURL File.toURL}() utility method.
+ *
+ * @param filename the file name to convert. Relative file names
+ * are resolved the way the JVM resolves them (current to the
+ * process-global current working directory).
+ *
+ * @exception IOException if the file does not exist
+ */
+ public static String fileNameToURL (String filename)
+ throws IOException
+ {
+ return fileToURL (new File (filename));
+ }
+
+ /**
+ * Provides the URL for a file, without relying on the JDK 1.2
+ * {@link java.io.File#toURL File.toURL}() utility method.
+ *
+ * @param f the file to convert. Relative file names
+ * are resolved the way the JVM resolves them (current to the
+ * process-global current working directory).
+ *
+ * @exception IOException if the file does not exist
+ */
+ public static String fileToURL (File f)
+ throws IOException
+ {
+ String temp;
+
+ // NOTE: the javax.xml.parsers.DocumentBuilder and
+ // javax.xml.transform.stream.StreamSource versions
+ // of this don't have this test. Some JVM versions
+ // don't report this error sanely through URL code.
+ if (!f.exists ())
+ throw new IOException ("no such file: " + f.getName ());
+
+ // FIXME: getAbsolutePath() seems buggy; I'm seeing components
+ // like "/foo/../" which are clearly not "absolute"
+ // and should have been resolved with the filesystem.
+
+ // Substituting "/" would be wrong, "foo" may have been
+ // symlinked ... the URL code will make that change
+ // later, so that things can get _really_ broken!
+
+ temp = f.getAbsolutePath ();
+
+ if (File.separatorChar != '/')
+ temp = temp.replace (File.separatorChar, '/');
+ if (!temp.startsWith ("/"))
+ temp = "/" + temp;
+ if (!temp.endsWith ("/") && f.isDirectory ())
+ temp = temp + "/";
+ return "file:" + temp;
+ }
+
+
+ /**
+ * Returns a URL string. Note that if a malformed URL is provided, or
+ * the parameter names a nonexistent file, the resulting URL may be
+ * malformed.
+ *
+ * @param fileOrURL If this is the name of a file which exists,
+ * then its URL is returned. Otherwise the argument is returned.
+ */
+ public static String getURL (String fileOrURL)
+ {
+ try {
+ return fileNameToURL (fileOrURL);
+ } catch (Exception e) {
+ return fileOrURL;
+ }
+ }
+
+
+
+ // note: cloneable, this is just copied; unguarded against mods
+ private Dictionary pubidMapping;
+
+ /**
+ * Constructs a resolver which understands how to map PUBLIC identifiers
+ * to other URIs, typically for local copies of standard DTD components.
+ *
+ * @param dict maps PUBLIC identifiers to URIs. This is not
+ * copied; subsequent modifications will be reported through the
+ * resolution operations.
+ */
+ public Resolver (Dictionary dict)
+ { pubidMapping = dict; }
+
+
+ // FIXME: want notion of a "system default" resolver, presumably
+ // loaded with all sorts of useful stuff. At the same time need
+ // a notion of resolver chaining (failure --> next) so that subsystems
+ // can set up things that won't interfere with other ones.
+
+ /**
+ * This parses most MIME content type strings that have <em>charset=...</em>
+ * encoding declarations to and returns the specified encoding. This
+ * conforms to RFC 3023, and is useful when constructing InputSource
+ * objects from URLConnection objects or other objects using MIME
+ * content typing.
+ *
+ * @param contentType the MIME content type that will be parsed; must
+ * not be null.
+ * @return the appropriate encoding, or null if the content type is
+ * not text and there's no <code>charset=...</code> attribute
+ */
+ static public String getEncoding (String contentType)
+ {
+ // currently a dumb parsing algorithm that works "mostly" and handles
+ // ..anything...charset=ABC
+ // ..anything...charset=ABC;otherAttr=DEF
+ // ..anything...charset=ABC (comment);otherAttr=DEF
+ // ..anything...charset= "ABC" (comment);otherAttr=DEF
+
+ int temp;
+ String encoding;
+ String defValue = null;
+
+ if (contentType.startsWith ("text/"))
+ defValue = contentType.startsWith ("text/html")
+ ? "ISO-8859-1" : "US-ASCII";
+
+ // Assumes 'charset' is only an attribute name, not part
+ // of a value, comment, or other attribute name
+ // ALSO assumes no escaped values like "\;" or "\)"
+ if ((temp = contentType.indexOf ("charset")) != -1) {
+ // strip out everything up to '=' ...
+ temp = contentType.indexOf ('=', temp);
+ if (temp == -1)
+ return defValue;
+ encoding = contentType.substring (temp + 1);
+ // ... and any subsequent attributes
+ if ((temp = encoding.indexOf (';')) != -1)
+ encoding = encoding.substring (0, temp);
+ // ... and any comments after value
+ if ((temp = encoding.indexOf ('(')) != -1)
+ encoding = encoding.substring (0, temp);
+ // ... then whitespace, and any (double) quotes
+ encoding = encoding.trim ();
+ if (encoding.charAt (0) == '"')
+ encoding = encoding.substring (1, encoding.length () - 1);
+ } else
+ encoding = defValue;
+ return encoding;
+ }
+
+
+ /**
+ * Uses a local dictionary of public identifiers to resolve URIs,
+ * normally with the goal of minimizing network traffic or latencies.
+ */
+ public InputSource resolveEntity (String publicId, String systemId)
+ throws IOException, SAXException
+ {
+ InputSource retval = null;
+ String uri;
+
+ if (publicId != null
+ && ((uri = (String) pubidMapping.get (publicId)) != null)) {
+ retval = new InputSource (uri);
+ retval.setPublicId (publicId);
+ }
+
+ // Could do URN resolution here
+
+ // URL resolution always done by parser
+
+ // FIXME: chain to "next" resolver
+
+ return retval;
+ }
+}
diff --git a/libjava/classpath/gnu/xml/util/SAXNullTransformerFactory.java b/libjava/classpath/gnu/xml/util/SAXNullTransformerFactory.java
new file mode 100644
index 000000000..3a97678e0
--- /dev/null
+++ b/libjava/classpath/gnu/xml/util/SAXNullTransformerFactory.java
@@ -0,0 +1,676 @@
+/* SAXNullTransformerFactory.java --
+ Copyright (C) 2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.util;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Hashtable;
+import java.util.Properties;
+
+import gnu.xml.dom.Consumer;
+import gnu.xml.dom.DomDocument;
+import gnu.xml.pipeline.DomConsumer;
+import gnu.xml.pipeline.EventFilter;
+
+import javax.xml.transform.*;
+import javax.xml.transform.dom.*;
+import javax.xml.transform.sax.*;
+import javax.xml.transform.stream.*;
+
+import org.xml.sax.*;
+import org.xml.sax.helpers.XMLReaderFactory;
+import org.xml.sax.helpers.LocatorImpl;
+
+
+/**
+ * Implements null transforms. XSLT stylesheets are not supported.
+ * This class provides a way to translate three representations of
+ * XML data (SAX event stream, DOM tree, and XML text) into each other.
+ * In essence it's a thinnish wrapper around basic SAX event
+ * <a href="../pipeline/package-summary.html">pipeline</a> facilities, which
+ * exposes only limited functionality. The <em>javax.xml.transform</em>
+ * functionality is implemented as follows: <ul>
+ *
+ * <li>The {@link javax.xml.transform.sax.SAXSource SAXSource} class
+ * just wraps an {@link XMLReader} and {@link InputSource}, while the
+ * {@link javax.xml.transform.sax.SAXResult SAXResult} class is less
+ * functional than a {@link gnu.xml.pipeline.EventConsumer EventConsumer}.
+ * (Notably, it drops all but one declaration from any DTD.)</li>
+ *
+ * <li>The {@link javax.xml.transform.dom.DOMSource DOMSource} class
+ * corresponds to special SAX parsers like {@link DomParser}, and the
+ * {@link javax.xml.transform.dom.DOMResult DOMResult} class corresponds
+ * to a {@link gnu.xml.pipeline.DomConsumer DomConsumer}.</li>
+ *
+ * <li>The {@link javax.xml.transform.stream.StreamSource StreamSource}
+ * class corresponds to a SAX {@link InputSource}, and the
+ * {@link javax.xml.transform.stream.StreamResult StreamResult} class
+ * corresponds to a {@link gnu.xml.pipeline.TextConsumer TextConsumer}.</li>
+ *
+ * </ul>
+ *
+ * <p><em>This implementation is preliminary.</em>
+ *
+ * @see gnu.xml.pipeline.XsltFilter
+ *
+ * @author David Brownell
+ */
+public class SAXNullTransformerFactory extends SAXTransformerFactory
+{
+
+ private ErrorListener errListener;
+ private URIResolver uriResolver;
+
+ /** Default constructor */
+ public SAXNullTransformerFactory () { }
+
+ //
+ // only has stuff that makes sense with null transforms
+ //
+
+ /**
+ * Returns true if the requested feature is supported.
+ * All three kinds of input and output are accepted:
+ * XML text, SAX events, and DOM nodes.
+ */
+ public boolean getFeature (String feature)
+ {
+ return SAXTransformerFactory.FEATURE.equals (feature)
+ || SAXResult.FEATURE.equals (feature)
+ || SAXSource.FEATURE.equals (feature)
+ || DOMResult.FEATURE.equals (feature)
+ || DOMSource.FEATURE.equals (feature)
+ || StreamResult.FEATURE.equals (feature)
+ || StreamSource.FEATURE.equals (feature)
+ ;
+ }
+
+ public void setFeature(String name, boolean value)
+ throws TransformerConfigurationException
+ {
+ throw new TransformerConfigurationException(name);
+ }
+
+
+ /** Throws an exception (no implementation attributes are supported) */
+ public void setAttribute (String key, Object value)
+ {
+ throw new IllegalArgumentException ();
+ }
+
+ /** Throws an exception (no implementation attributes are supported) */
+ public Object getAttribute (String key)
+ {
+ throw new IllegalArgumentException ();
+ }
+
+ /** (not yet implemented) */
+ public Source getAssociatedStylesheet (Source source,
+ String media,
+ String title,
+ String charset)
+ throws TransformerConfigurationException
+ {
+ // parse, and find the appropriate xsl-stylesheet PI contents
+ throw new IllegalArgumentException ();
+ }
+
+ public Transformer newTransformer ()
+ throws TransformerConfigurationException
+ {
+ return new NullTransformer ();
+ }
+
+ /**
+ * Returns a TransformerHandler that knows how to generate output
+ * in all three standard formats. Output text is generated using
+ * {@link XMLWriter}, and the GNU implementation of
+ * {@link DomDocument DOM} is used.
+ *
+ * @see SAXResult
+ * @see StreamResult
+ * @see DOMResult
+ */
+ public TransformerHandler newTransformerHandler ()
+ throws TransformerConfigurationException
+ {
+ NullTransformer transformer = new NullTransformer ();
+ return transformer.handler;
+ }
+
+ //
+ // Stuff that depends on XSLT support, which we don't provide
+ //
+ private static final String noXSLT = "No XSLT support";
+
+ /** Throws an exception (XSLT is not supported). */
+ public Transformer newTransformer (Source stylesheet)
+ throws TransformerConfigurationException
+ {
+ throw new TransformerConfigurationException (noXSLT);
+ }
+
+ /** Throws an exception (XSLT is not supported). */
+ public Templates newTemplates (Source stylesheet)
+ throws TransformerConfigurationException
+ {
+ throw new TransformerConfigurationException (noXSLT);
+ }
+
+ /** Throws an exception (XSLT is not supported). */
+ public TemplatesHandler newTemplatesHandler ()
+ throws TransformerConfigurationException
+ {
+ throw new TransformerConfigurationException (noXSLT);
+ }
+
+ /** Throws an exception (XSLT is not supported). */
+ public TransformerHandler newTransformerHandler (Source stylesheet)
+ throws TransformerConfigurationException
+ {
+ throw new TransformerConfigurationException (noXSLT);
+ }
+
+ /** Throws an exception (XSLT is not supported). */
+ public TransformerHandler newTransformerHandler (Templates stylesheet)
+ throws TransformerConfigurationException
+ {
+ throw new TransformerConfigurationException (noXSLT);
+ }
+
+ /** Throws an exception (XSLT is not supported). */
+ public XMLFilter newXMLFilter (Source stylesheet)
+ throws TransformerConfigurationException
+ {
+ throw new TransformerConfigurationException (noXSLT);
+ }
+
+ /** Throws an exception (XSLT is not supported). */
+ public XMLFilter newXMLFilter (Templates stylesheet)
+ throws TransformerConfigurationException
+ {
+ throw new TransformerConfigurationException (noXSLT);
+ }
+
+ /** Returns the value assigned by {@link #setErrorListener}. */
+ public ErrorListener getErrorListener ()
+ {
+ return errListener;
+ }
+
+ /** Assigns a value that would be used when parsing stylesheets */
+ public void setErrorListener (ErrorListener e)
+ {
+ errListener = e;
+ }
+
+ /** Returns the value assigned by {@link #setURIResolver}. */
+ public URIResolver getURIResolver ()
+ {
+ return uriResolver;
+ }
+
+ /** Assigns a value that would be used when parsing stylesheets */
+ public void setURIResolver (URIResolver u)
+ {
+ uriResolver = u;
+ }
+
+
+ //
+ // Helper classes. These might in theory be subclassed
+ // by an XSLT implementation, if they were exported.
+ //
+
+ static class DomTerminus
+ extends DomConsumer
+ {
+
+ DomTerminus (DOMResult result)
+ throws SAXException
+ {
+ // won't really throw SAXException
+ super (DomDocument.class);
+ setHandler (new DomHandler (this, result));
+ }
+
+ }
+
+ static class DomHandler
+ extends Consumer.Backdoor
+ {
+
+ private DOMResult result;
+
+ DomHandler (DomConsumer c, DOMResult r)
+ throws SAXException
+ {
+ // won't really throw SAXException
+ super (c);
+ result = r;
+ }
+
+ public void endDocument ()
+ throws SAXException
+ {
+ super.endDocument ();
+ result.setNode (getDocument ());
+ }
+
+ }
+
+ private static OutputStream getOutputStream (String uri)
+ throws IOException
+ {
+ // JDK stupidity: file "protocol does not support output" ...
+ if (uri.startsWith ("file:"))
+ return new FileOutputStream (uri.substring (5));
+
+ // Otherwise ...
+ URL url = new URL (uri);
+ URLConnection conn = url.openConnection ();
+
+ conn.setDoOutput (true);
+ return conn.getOutputStream ();
+ }
+
+
+ static class NullHandler
+ extends EventFilter
+ implements TransformerHandler
+ {
+
+ private String systemId;
+ private Transformer transformer;
+
+ NullHandler (Transformer t)
+ {
+ transformer = t;
+ }
+
+ public Transformer getTransformer ()
+ {
+ return transformer;
+ }
+
+ public String getSystemId ()
+ {
+ return systemId;
+ }
+
+ public void setSystemId (String id)
+ {
+ systemId = id;
+ }
+
+ public void setResult (Result result)
+ {
+ if (result.getSystemId () != null)
+ systemId = result.getSystemId ();
+
+ try
+ {
+
+ // output to partial SAX event stream?
+ if (result instanceof SAXResult)
+ {
+ SAXResult r = (SAXResult) result;
+
+ setContentHandler (r.getHandler ());
+ setProperty (LEXICAL_HANDLER, r.getLexicalHandler ());
+ // DTD info is filtered out by javax.transform
+
+ // output to DOM tree?
+ }
+ else if (result instanceof DOMResult)
+ {
+ DomTerminus out = new DomTerminus ((DOMResult) result);
+
+ setContentHandler (out.getContentHandler ());
+ setProperty (LEXICAL_HANDLER,
+ out.getProperty (LEXICAL_HANDLER));
+ // save DTD-derived info, if any.
+ setDTDHandler (out.getDTDHandler ());
+ setProperty (DECL_HANDLER,
+ out.getProperty (DECL_HANDLER));
+
+ // node is saved into result on endDocument()
+
+ // output to (XML) text?
+ }
+ else if (result instanceof StreamResult)
+ {
+ StreamResult r = (StreamResult) result;
+ XMLWriter out;
+
+ // FIXME: when do output properties take effect?
+ // encoding, standalone decl, xml/xhtml/... ...
+
+ // FIXME: maybe put nsfix filter up front
+
+ try
+ {
+ if (r.getWriter () != null)
+ out = new XMLWriter (r.getWriter ());
+ else if (r.getOutputStream () != null)
+ out = new XMLWriter (r.getOutputStream ());
+ else if (r.getSystemId () != null)
+ out = new XMLWriter (
+ getOutputStream (r.getSystemId ()));
+ else
+ throw new IllegalArgumentException (
+ "bad StreamResult");
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace ();
+ // on jdk 1.4, pass the root cause ...
+ throw new IllegalArgumentException (e.getMessage ());
+ }
+
+ // out.setExpandingEntities (true);
+ // out.setPrettyPrinting (true);
+ // out.setXhtml (true);
+
+ setContentHandler (out);
+ setProperty (LEXICAL_HANDLER, out);
+ // save DTD info, if any; why not?
+ setDTDHandler (out);
+ setProperty (DECL_HANDLER, out);
+ }
+
+ }
+ catch (SAXException e)
+ {
+ // SAXNotSupportedException or SAXNotRecognizedException:
+ // "can't happen" ... but SAXException for DOM build probs
+ // could happen, so ...
+ // on jdk 1.4, pass the root cause ...
+ throw new IllegalArgumentException (e.getMessage ());
+ }
+ }
+ }
+
+ // an interface that adds no value
+ static class LocatorAdapter
+ extends LocatorImpl
+ implements SourceLocator
+ {
+
+ LocatorAdapter (SAXParseException e)
+ {
+ setSystemId (e.getSystemId ());
+ setPublicId (e.getPublicId ());
+ setLineNumber (e.getLineNumber ());
+ setColumnNumber (e.getColumnNumber ());
+ }
+
+ }
+
+ // another interface that adds no value
+ static class ListenerAdapter
+ implements ErrorHandler
+ {
+
+ NullTransformer transformer;
+
+ ListenerAdapter (NullTransformer t)
+ {
+ transformer = t;
+ }
+
+ private TransformerException map (SAXParseException e)
+ {
+ return new TransformerException (
+ e.getMessage (),
+ new LocatorAdapter (e),
+ e);
+ }
+
+ public void error (SAXParseException e)
+ throws SAXParseException
+ {
+ try
+ {
+ if (transformer.errListener != null)
+ transformer.errListener.error (map (e));
+ }
+ catch (TransformerException ex)
+ {
+ transformer.ex = ex;
+ throw e;
+ }
+ }
+
+ public void fatalError (SAXParseException e)
+ throws SAXParseException
+ {
+ try
+ {
+ if (transformer.errListener != null)
+ transformer.errListener.fatalError (map (e));
+ else
+ throw map (e);
+ } catch (TransformerException ex) {
+ transformer.ex = ex;
+ throw e;
+ }
+ }
+
+ public void warning (SAXParseException e)
+ throws SAXParseException
+ {
+ try
+ {
+ if (transformer.errListener != null)
+ transformer.errListener.warning (map (e));
+ }
+ catch (TransformerException ex)
+ {
+ transformer.ex = ex;
+ throw e;
+ }
+ }
+ }
+
+ static class NullTransformer
+ extends Transformer
+ {
+
+ private URIResolver uriResolver;
+ private Properties props = new Properties ();
+ private Hashtable params = new Hashtable (7);
+
+ ErrorListener errListener = null;
+ TransformerException ex = null;
+ NullHandler handler;
+
+ NullTransformer ()
+ {
+ super ();
+ handler = new NullHandler (this);
+ }
+
+ public ErrorListener getErrorListener ()
+ {
+ return errListener;
+ }
+
+ public void setErrorListener (ErrorListener e)
+ {
+ errListener = e;
+ }
+
+ public URIResolver getURIResolver ()
+ {
+ return uriResolver;
+ }
+
+ public void setURIResolver (URIResolver u)
+ {
+ uriResolver = u;
+ }
+
+ public void setOutputProperties (Properties p)
+ {
+ props = (Properties) p.clone ();
+ }
+
+ public Properties getOutputProperties ()
+ {
+ return (Properties) props.clone ();
+ }
+
+ public void setOutputProperty (String name, String value)
+ {
+ props.setProperty (name, value);
+ }
+
+ public String getOutputProperty (String name)
+ {
+ return props.getProperty (name);
+ }
+
+ public void clearParameters ()
+ {
+ params.clear ();
+ }
+
+ public void setParameter (String name, Object value)
+ {
+ props.put (name, value);
+ }
+
+ public Object getParameter (String name)
+ {
+ return props.get (name);
+ }
+
+ public void transform (Source in, Result out)
+ throws TransformerException
+ {
+ try
+ {
+ XMLReader producer;
+ InputSource input;
+
+ // Input from DOM?
+ if (in instanceof DOMSource)
+ {
+ DOMSource source = (DOMSource) in;
+
+ if (source.getNode () == null)
+ throw new IllegalArgumentException ("no DOM node");
+ producer = new DomParser (source.getNode ());
+ input = null;
+
+ // Input from SAX?
+ }
+ else if (in instanceof SAXSource)
+ {
+ SAXSource source = (SAXSource) in;
+
+ producer = source.getXMLReader ();
+ if (producer == null)
+ producer = XMLReaderFactory.createXMLReader ();
+
+ input = source.getInputSource ();
+ if (input == null)
+ {
+ if (source.getSystemId () != null)
+ input = new InputSource (source.getSystemId ());
+ else
+ throw new IllegalArgumentException (
+ "missing SAX input");
+ }
+
+ // Input from a stream or something?
+ }
+ else
+ {
+ producer = XMLReaderFactory.createXMLReader ();
+ input = SAXSource.sourceToInputSource (in);
+ if (input == null)
+ throw new IllegalArgumentException ("missing input");
+ }
+
+ // preserve original namespace prefixes
+ try
+ {
+ producer.setFeature(EventFilter.FEATURE_URI +
+ "namespace-prefixes",
+ true);
+ }
+ catch (Exception e)
+ {
+ /* ignore */
+ // FIXME if we couldn't, "NsFix" stage before the output ..
+ }
+
+ // arrange the output
+ handler.setResult (out);
+ EventFilter.bind (producer, handler);
+
+ // then parse ... single element pipeline
+ producer.parse (input);
+
+ }
+ catch (IOException e)
+ {
+ throw new TransformerException ("transform failed", e);
+
+ }
+ catch (SAXException e)
+ {
+ if (ex == null && ex.getCause () == e)
+ throw ex;
+ else
+ throw new TransformerException ("transform failed", e);
+
+ }
+ finally
+ {
+ ex = null;
+ }
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/util/XCat.java b/libjava/classpath/gnu/xml/util/XCat.java
new file mode 100644
index 000000000..ea23ad682
--- /dev/null
+++ b/libjava/classpath/gnu/xml/util/XCat.java
@@ -0,0 +1,1611 @@
+/* XCat.java --
+ Copyright (C) 2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.xml.util;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.StringTokenizer;
+import java.util.Stack;
+import java.util.Vector;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+
+import org.xml.sax.ext.DefaultHandler2;
+import org.xml.sax.ext.EntityResolver2;
+
+import org.xml.sax.helpers.XMLReaderFactory;
+
+/**
+ * Packages <a href=
+ "http://www.oasis-open.org/committees/entity/spec-2001-08-06.html"
+ >OASIS XML Catalogs</a>,
+ * primarily for entity resolution by parsers.
+ * That specification defines an XML syntax for mappings between
+ * identifiers declared in DTDs (particularly PUBLIC identifiers) and
+ * locations. SAX has always supported such mappings, but conventions for
+ * an XML file syntax to maintain them have previously been lacking.
+ *
+ * <p> This has three main operational modes. The primary intended mode is
+ * to create a resolver, then preloading it with one or more site-standard
+ * catalogs before using it with one or more SAX parsers: <pre>
+ * XCat catalog = new XCat ();
+ * catalog.setErrorHandler (diagnosticErrorHandler);
+ * catalog.loadCatalog ("file:/local/catalogs/catalog.cat");
+ * catalog.loadCatalog ("http://shared/catalog.cat");
+ * ...
+ * catalog.disableLoading ();
+ * parser1.setEntityResolver (catalog);
+ * parser2.setEntityResolver (catalog);
+ * ...</pre>
+ *
+ * <p>A second mode is to arrange that your application uses instances of
+ * this class as its entity resolver, and automatically loads catalogs
+ * referenced by <em>&lt;?oasis-xml-catalog...?&gt;</em> processing
+ * instructions found before the DTD in documents it parses.
+ * It would then discard the resolver after each parse.
+ *
+ * <p> A third mode applies catalogs in contexts other than entity
+ * resolution for parsers.
+ * The {@link #resolveURI resolveURI()} method supports resolving URIs
+ * stored in XML application data, rather than inside DTDs.
+ * Catalogs would be loaded as shown above, and the catalog could
+ * be used concurrently for parser entity resolution and for
+ * application URI resolution.
+ * </p>
+ *
+ * <center><hr width='70%'></center>
+ *
+ * <p>Errors in catalogs implicitly loaded (during resolution) are ignored
+ * beyond being reported through any <em>ErrorHandler</em> assigned using
+ * {@link #setErrorHandler setErrorHandler()}. SAX exceptions
+ * thrown from such a handler won't abort resolution, although throwing a
+ * <em>RuntimeException</em> or <em>Error</em> will normally abort both
+ * resolution and parsing. Useful diagnostic information is available to
+ * any <em>ErrorHandler</em> used to report problems, or from any exception
+ * thrown from an explicit {@link #loadCatalog loadCatalog()} invocation.
+ * Applications can use that information as troubleshooting aids.
+ *
+ * <p>While this class requires <em>SAX2 Extensions 1.1</em> classes in
+ * its class path, basic functionality does not require using a SAX2
+ * parser that supports the extended entity resolution functionality.
+ * See the original SAX1
+ * {@link #resolveEntity(java.lang.String,java.lang.String) resolveEntity()}
+ * method for a list of restrictions which apply when it is used with
+ * older SAX parsers.
+ *
+ * @see EntityResolver2
+ *
+ * @author David Brownell
+ */
+public class XCat implements EntityResolver2
+{
+ private Catalog catalogs [];
+ private boolean usingPublic = true;
+ private boolean loadingPermitted = true;
+ private boolean unified = true;
+ private String parserClass;
+ private ErrorHandler errorHandler;
+
+ // private EntityResolver next; // chain to next if we fail...
+
+ //
+ // NOTE: This is a straightforward implementation, and if
+ // there are lots of "nextCatalog" or "delegate*" entries
+ // in use, two tweaks would be worth considering:
+ //
+ // - Centralize some sort of cache (key by URI) for individual
+ // resolvers. That'd avoid multiple copies of a given catalog.
+ //
+ // - Have resolution track what catalogs (+modes) have been
+ // searched. This would support loop detection.
+ //
+
+
+ /**
+ * Initializes without preloading a catalog.
+ * This API is convenient when you may want to arrange that catalogs
+ * are automatically loaded when explicitly referenced in documents,
+ * using the <em>oasis-xml-catalog</em> processing instruction.
+ * In such cases you won't usually be able to preload catalogs.
+ */
+ public XCat () { }
+
+ /**
+ * Initializes, and preloads a catalog using the default SAX parser.
+ * This API is convenient when you operate with one or more standard
+ * catalogs.
+ *
+ * <p> This just delegates to {@link #loadCatalog loadCatalog()};
+ * see it for exception information.
+ *
+ * @param uri absolute URI for the catalog file.
+ */
+ public XCat (String uri)
+ throws SAXException, IOException
+ { loadCatalog (uri); }
+
+
+ /**
+ * Loads an OASIS XML Catalog.
+ * It is appended to the list of currently active catalogs, or
+ * reloaded if a catalog with the same URI was already loaded.
+ * Callers have control over what parser is used, how catalog parsing
+ * errors are reported, and whether URIs will be resolved consistently.
+ *
+ * <p> The OASIS specification says that errors detected when loading
+ * catalogs "must recover by ignoring the catalog entry file that
+ * failed, and proceeding." In this API, that action can be the
+ * responsibility of applications, when they explicitly load any
+ * catalog using this method.
+ *
+ * <p>Note that catalogs referenced by this one will not be loaded
+ * at this time. Catalogs referenced through <em>nextCatalog</em>
+ * or <em>delegate*</em> elements are normally loaded only if needed.
+ *
+ * @see #setErrorHandler
+ * @see #setParserClass
+ * @see #setUnified
+ *
+ * @param uri absolute URI for the catalog file.
+ *
+ * @exception IOException As thrown by the parser, typically to
+ * indicate problems reading data from that URI.
+ * @exception SAXException As thrown by the parser, typically to
+ * indicate problems parsing data from that URI. It may also
+ * be thrown if the parser doesn't support necessary handlers.
+ * @exception IllegalStateException When attempting to load a
+ * catalog after loading has been {@link #disableLoading disabled},
+ * such as after any entity or URI lookup has been performed.
+ */
+ public synchronized void loadCatalog (String uri)
+ throws SAXException, IOException
+ {
+ Catalog catalog;
+ int index = -1;
+
+ if (!loadingPermitted)
+ throw new IllegalStateException ();
+
+ uri = normalizeURI (uri);
+ if (catalogs != null) {
+ // maybe just reload
+ for (index = 0; index < catalogs.length; index++)
+ if (uri.equals (catalogs [index].catalogURI))
+ break;
+ }
+ catalog = loadCatalog (parserClass, errorHandler, uri, unified);
+
+ // add to list of catalogs
+ if (catalogs == null) {
+ index = 0;
+ catalogs = new Catalog [1];
+ } else if (index == catalogs.length) {
+ Catalog tmp [];
+
+ tmp = new Catalog [index + 1];
+ System.arraycopy (catalogs, 0, tmp, 0, index);
+ catalogs = tmp;
+ }
+ catalogs [index] = catalog;
+ }
+
+
+ /**
+ * "New Style" external entity resolution for parsers.
+ * Calls to this method prevent explicit loading of additional catalogs
+ * using {@link #loadCatalog loadCatalog()}.
+ *
+ * <p>This supports the full core catalog functionality for locating
+ * (and relocating) parsed entities that have been declared in a
+ * document's DTD.
+ *
+ * @param name Entity name, such as "dudley", "%nell", or "[dtd]".
+ * @param publicId Either a normalized public ID, or null.
+ * @param baseURI Absolute base URI associated with systemId.
+ * @param systemId URI found in entity declaration (may be
+ * relative to baseURI).
+ *
+ * @return Input source for accessing the external entity, or null
+ * if no mapping was found. The input source may have opened
+ * the stream, and will have a fully resolved URI.
+ *
+ * @see #getExternalSubset
+ */
+ public InputSource resolveEntity (
+ String name, // UNUSED ... systemId is always non-null
+ String publicId,
+ String baseURI, // UNUSED ... it just lets sysId be relative
+ String systemId
+ ) throws SAXException, IOException
+ {
+ if (loadingPermitted)
+ disableLoading ();
+
+ try {
+ // steps as found in OASIS XML catalog spec 7.1.2
+ // steps 1, 8 involve looping over the list of catalogs
+ for (int i = 0; i < catalogs.length; i++) {
+ InputSource retval;
+ retval = catalogs [i].resolve (usingPublic, publicId, systemId);
+ if (retval != null)
+ return retval;
+ }
+ } catch (DoneDelegation x) {
+ // done!
+ }
+ // step 9 involves returning "no match"
+ return null;
+ }
+
+
+ /**
+ * "New Style" parser callback to add an external subset.
+ * For documents that don't include an external subset, this may
+ * return one according to <em>doctype</em> catalog entries.
+ * (This functionality is not a core part of the OASIS XML Catalog
+ * specification, though it's presented in an appendix.)
+ * If no such entry is defined, this returns null to indicate that
+ * this document will not be modified to include such a subset.
+ * Calls to this method prevent explicit loading of additional catalogs
+ * using {@link #loadCatalog loadCatalog()}.
+ *
+ * <p><em>Warning:</em> That catalog functionality can be dangerous.
+ * It can provide definitions of general entities, and thereby mask
+ * certain well formedess errors.
+ *
+ * @param name Name of the document element, either as declared in
+ * a DOCTYPE declaration or as observed in the text.
+ * @param baseURI Document's base URI (absolute).
+ *
+ * @return Input source for accessing the external subset, or null
+ * if no mapping was found. The input source may have opened
+ * the stream, and will have a fully resolved URI.
+ */
+ public InputSource getExternalSubset (String name, String baseURI)
+ throws SAXException, IOException
+ {
+ if (loadingPermitted)
+ disableLoading ();
+ try {
+ for (int i = 0; i < catalogs.length; i++) {
+ InputSource retval = catalogs [i].getExternalSubset (name);
+ if (retval != null)
+ return retval;
+ }
+ } catch (DoneDelegation x) {
+ // done!
+ }
+ return null;
+ }
+
+
+ /**
+ * "Old Style" external entity resolution for parsers.
+ * This API provides only core functionality.
+ * Calls to this method prevent explicit loading of additional catalogs
+ * using {@link #loadCatalog loadCatalog()}.
+ *
+ * <p>The functional limitations of this interface include:</p><ul>
+ *
+ * <li>Since system IDs will be absolutized before the resolver
+ * sees them, matching against relative URIs won't work.
+ * This may affect <em>system</em>, <em>rewriteSystem</em>,
+ * and <em>delegateSystem</em> catalog entries.
+ *
+ * <li>Because of that absolutization, documents declaring entities
+ * with system IDs using URI schemes that the JVM does not recognize
+ * may be unparsable. URI schemes such as <em>file:/</em>,
+ * <em>http://</em>, <em>https://</em>, and <em>ftp://</em>
+ * will usually work reliably.
+ *
+ * <li>Because missing external subsets can't be provided, the
+ * <em>doctype</em> catalog entries will be ignored.
+ * (The {@link #getExternalSubset getExternalSubset()} method is
+ * a "New Style" resolution option.)
+ *
+ * </ul>
+ *
+ * <p>Applications can tell whether this limited functionality will be
+ * used: if the feature flag associated with the {@link EntityResolver2}
+ * interface is not <em>true</em>, the limitations apply. Applications
+ * can't usually know whether a given document and catalog will trigger
+ * those limitations. The issue can only be bypassed by operational
+ * procedures such as not using catalogs or documents which involve
+ * those features.
+ *
+ * @param publicId Either a normalized public ID, or null
+ * @param systemId Always an absolute URI.
+ *
+ * @return Input source for accessing the external entity, or null
+ * if no mapping was found. The input source may have opened
+ * the stream, and will have a fully resolved URI.
+ */
+ final public InputSource resolveEntity (String publicId, String systemId)
+ throws SAXException, IOException
+ {
+ return resolveEntity (null, publicId, null, systemId);
+ }
+
+
+ /**
+ * Resolves a URI reference that's not defined to the DTD.
+ * This is intended for use with URIs found in document text, such as
+ * <em>xml-stylesheet</em> processing instructions and in attribute
+ * values, where they are not recognized as URIs by XML parsers.
+ * Calls to this method prevent explicit loading of additional catalogs
+ * using {@link #loadCatalog loadCatalog()}.
+ *
+ * <p>This functionality is supported by the OASIS XML Catalog
+ * specification, but will never be invoked by an XML parser.
+ * It corresponds closely to functionality for mapping system
+ * identifiers for entities declared in DTDs; closely enough that
+ * this implementation's default behavior is that they be
+ * identical, to minimize potential confusion.
+ *
+ * <p>This method could be useful when implementing the
+ * {@link javax.xml.transform.URIResolver} interface, wrapping the
+ * input source in a {@link javax.xml.transform.sax.SAXSource}.
+ *
+ * @see #isUnified
+ * @see #setUnified
+ *
+ * @param baseURI The relevant base URI as specified by the XML Base
+ * specification. This recognizes <em>xml:base</em> attributes
+ * as overriding the actual (physical) base URI.
+ * @param uri Either an absolute URI, or one relative to baseURI
+ *
+ * @return Input source for accessing the mapped URI, or null
+ * if no mapping was found. The input source may have opened
+ * the stream, and will have a fully resolved URI.
+ */
+ public InputSource resolveURI (String baseURI, String uri)
+ throws SAXException, IOException
+ {
+ if (loadingPermitted)
+ disableLoading ();
+
+ // NOTE: baseURI isn't used here, but caller MUST have it,
+ // and heuristics _might_ use it in the future ... plus,
+ // it's symmetric with resolveEntity ().
+
+ // steps 1, 6 involve looping
+ try {
+ for (int i = 0; i < catalogs.length; i++) {
+ InputSource tmp = catalogs [i].resolveURI (uri);
+ if (tmp != null)
+ return tmp;
+ }
+ } catch (DoneDelegation x) {
+ // done
+ }
+ // step 7 reports no match
+ return null;
+ }
+
+
+ /**
+ * Records that catalog loading is no longer permitted.
+ * Loading is automatically disabled when lookups are performed,
+ * and should be manually disabled when <em>startDTD()</em> (or
+ * any other DTD declaration callback) is invoked, or at the latest
+ * when the document root element is seen.
+ */
+ public synchronized void disableLoading ()
+ {
+ // NOTE: this method and loadCatalog() are synchronized
+ // so that it's impossible to load (top level) catalogs
+ // after lookups start. Likewise, deferred loading is also
+ // synchronized (for "next" and delegated catalogs) to
+ // ensure that parsers can share resolvers.
+ loadingPermitted = false;
+ }
+
+
+ /**
+ * Returns the error handler used to report catalog errors.
+ * Null is returned if the parser's default error handling
+ * will be used.
+ *
+ * @see #setErrorHandler
+ */
+ public ErrorHandler getErrorHandler ()
+ { return errorHandler; }
+
+ /**
+ * Assigns the error handler used to report catalog errors.
+ * These errors may come either from the SAX2 parser or
+ * from the catalog parsing code driven by the parser.
+ *
+ * <p> If you're sharing the resolver between parsers, don't
+ * change this once lookups have begun.
+ *
+ * @see #getErrorHandler
+ *
+ * @param parser The error handler, or null saying to use the default
+ * (no diagnostics, and only fatal errors terminate loading).
+ */
+ public void setErrorHandler (ErrorHandler handler)
+ { errorHandler = handler; }
+
+
+ /**
+ * Returns the name of the SAX2 parser class used to parse catalogs.
+ * Null is returned if the system default is used.
+ * @see #setParserClass
+ */
+ public String getParserClass ()
+ { return parserClass; }
+
+ /**
+ * Names the SAX2 parser class used to parse catalogs.
+ *
+ * <p> If you're sharing the resolver between parsers, don't change
+ * this once lookups have begun.
+ *
+ * <p> Note that in order to properly support the <em>xml:base</em>
+ * attribute and relative URI resolution, the SAX parser used to parse
+ * the catalog must provide a {@link Locator} and support the optional
+ * declaration and lexical handlers.
+ *
+ * @see #getParserClass
+ *
+ * @param parser The parser class name, or null saying to use the
+ * system default SAX2 parser.
+ */
+ public void setParserClass (String parser)
+ { parserClass = parser; }
+
+
+ /**
+ * Returns true (the default) if all methods resolve
+ * a given URI in the same way.
+ * Returns false if calls resolving URIs as entities (such as
+ * {@link #resolveEntity resolveEntity()}) use different catalog entries
+ * than those resolving them as URIs ({@link #resolveURI resolveURI()}),
+ * which will generally produce different results.
+ *
+ * <p>The OASIS XML Catalog specification defines two related schemes
+ * to map URIs "as URIs" or "as system IDs".
+ * URIs use <em>uri</em>, <em>rewriteURI</em>, and <em>delegateURI</em>
+ * elements. System IDs do the same things with <em>systemId</em>,
+ * <em>rewriteSystemId</em>, and <em>delegateSystemId</em>.
+ * It's confusing and error prone to maintain two parallel copies of
+ * such data. Accordingly, this class makes that behavior optional.
+ * The <em>unified</em> interpretation of URI mappings is preferred,
+ * since it prevents surprises where one URI gets mapped to different
+ * contents depending on whether the reference happens to have come
+ * from a DTD (or not).
+ *
+ * @see #setUnified
+ */
+ public boolean isUnified ()
+ { return unified; }
+
+ /**
+ * Assigns the value of the flag returned by {@link #isUnified}.
+ * Set it to false to be strictly conformant with the OASIS XML Catalog
+ * specification. Set it to true to make all mappings for a given URI
+ * give the same result, regardless of the reason for the mapping.
+ *
+ * <p>Don't change this once you've loaded the first catalog.
+ *
+ * @param value new flag setting
+ */
+ public void setUnified (boolean value)
+ { unified = value; }
+
+
+ /**
+ * Returns true (the default) if a catalog's public identifier
+ * mappings will be used.
+ * When false is returned, such mappings are ignored except when
+ * system IDs are discarded, such as for
+ * entities using the <em>urn:publicid:</em> URI scheme in their
+ * system identifiers. (See RFC 3151 for information about that
+ * URI scheme. Using it in system identifiers may not work well
+ * with many SAX parsers unless the <em>resolve-dtd-uris</em>
+ * feature flag is set to false.)
+ * @see #setUsingPublic
+ */
+ public boolean isUsingPublic ()
+ { return usingPublic; }
+
+ /**
+ * Specifies which catalog search mode is used.
+ * By default, public identifier mappings are able to override system
+ * identifiers when both are available.
+ * Applications may choose to ignore public
+ * identifier mappings in such cases, so that system identifiers
+ * declared in DTDs will only be overridden by an explicit catalog
+ * match for that system ID.
+ *
+ * <p> If you're sharing the resolver between parsers, don't
+ * change this once lookups have begun.
+ * @see #isUsingPublic
+ *
+ * @param value true to always use public identifier mappings,
+ * false to only use them for system ids using the <em>urn:publicid:</em>
+ * URI scheme.
+ */
+ public void setUsingPublic (boolean value)
+ { usingPublic = value; }
+
+
+
+ // hmm, what's this do? :)
+ private static Catalog loadCatalog (
+ String parserClass,
+ ErrorHandler eh,
+ String uri,
+ boolean unified
+ ) throws SAXException, IOException
+ {
+ XMLReader parser;
+ Loader loader;
+ boolean doesIntern = false;
+
+ if (parserClass == null)
+ parser = XMLReaderFactory.createXMLReader ();
+ else
+ parser = XMLReaderFactory.createXMLReader (parserClass);
+ if (eh != null)
+ parser.setErrorHandler (eh);
+ // resolve-dtd-entities is at default value (unrecognized == true)
+
+ try {
+ doesIntern = parser.getFeature (
+ "http://xml.org/sax/features/string-interning");
+ } catch (SAXNotRecognizedException e) { }
+
+ loader = new Loader (doesIntern, eh, unified);
+ loader.cat.parserClass = parserClass;
+ loader.cat.catalogURI = uri;
+
+ parser.setContentHandler (loader);
+ parser.setProperty (
+ "http://xml.org/sax/properties/declaration-handler",
+ loader);
+ parser.setProperty (
+ "http://xml.org/sax/properties/lexical-handler",
+ loader);
+ parser.parse (uri);
+
+ return loader.cat;
+ }
+
+ // perform one or both the normalizations for public ids
+ private static String normalizePublicId (boolean full, String publicId)
+ {
+ if (publicId.startsWith ("urn:publicid:")) {
+ CPStringBuilder buf = new CPStringBuilder ();
+ char chars [] = publicId.toCharArray ();
+boolean hasbug = false;
+
+ for (int i = 13; i < chars.length; i++) {
+ switch (chars [i]) {
+ case '+': buf.append (' '); continue;
+ case ':': buf.append ("//"); continue;
+ case ';': buf.append ("::"); continue;
+ case '%':
+// FIXME unhex that char! meanwhile, warn and fallthrough ...
+ hasbug = true;
+ default: buf.append (chars [i]); continue;
+ }
+ }
+ publicId = buf.toString ();
+if (hasbug)
+System.err.println ("nyet unhexing public id: " + publicId);
+ full = true;
+ }
+
+ // SAX parsers do everything except that URN mapping, but
+ // we can't trust other sources to normalize correctly
+ if (full) {
+ StringTokenizer tokens;
+ String token;
+
+ tokens = new StringTokenizer (publicId, " \r\n");
+ publicId = null;
+ while (tokens.hasMoreTokens ()) {
+ if (publicId == null)
+ publicId = tokens.nextToken ();
+ else
+ publicId += " " + tokens.nextToken ();
+ }
+ }
+ return publicId;
+ }
+
+ private static boolean isUriExcluded (int c)
+ { return c <= 0x20 || c >= 0x7f || "\"<>^`{|}".indexOf (c) != -1; }
+
+ private static int hexNibble (int c)
+ {
+ if (c < 10)
+ return c + '0';
+ return ('a' - 10) + c;
+ }
+
+ // handles URIs with "excluded" characters
+ private static String normalizeURI (String systemId)
+ {
+ int length = systemId.length ();
+
+ for (int i = 0; i < length; i++) {
+ char c = systemId.charAt (i);
+
+ // escape non-ASCII plus "excluded" characters
+ if (isUriExcluded (c)) {
+ byte buf [];
+ ByteArrayOutputStream out;
+ int b;
+
+ // a JVM that doesn't know UTF8 and 8859_1 is unusable!
+ try {
+ buf = systemId.getBytes ("UTF8");
+ out = new ByteArrayOutputStream (buf.length + 10);
+
+ for (i = 0; i < buf.length; i++) {
+ b = buf [i] & 0x0ff;
+ if (isUriExcluded (b)) {
+ out.write ((int) '%');
+ out.write (hexNibble (b >> 4));
+ out.write (hexNibble (b & 0x0f));
+ } else
+ out.write (b);
+ }
+ return out.toString ("8859_1");
+ } catch (IOException e) {
+ throw new RuntimeException (
+ "can't normalize URI: " + e.getMessage ());
+ }
+ }
+ }
+ return systemId;
+ }
+
+ // thrown to mark authoritative end of a search
+ private static class DoneDelegation extends SAXException
+ {
+ DoneDelegation () { }
+ }
+
+
+ /**
+ * Represents a OASIS XML Catalog, and encapsulates much of
+ * the catalog functionality.
+ */
+ private static class Catalog
+ {
+ // loading infrastructure
+ String catalogURI;
+ ErrorHandler eh;
+ boolean unified;
+ String parserClass;
+
+ // catalog data
+ boolean hasPreference;
+ boolean usingPublic;
+
+ Hashtable publicIds;
+ Hashtable publicDelegations;
+
+ Hashtable systemIds;
+ Hashtable systemRewrites;
+ Hashtable systemDelegations;
+
+ Hashtable uris;
+ Hashtable uriRewrites;
+ Hashtable uriDelegations;
+
+ Hashtable doctypes;
+
+ Vector next;
+
+ // nonpublic!
+ Catalog () { }
+
+
+ // steps as found in OASIS XML catalog spec 7.1.2
+ private InputSource locatePublicId (String publicId)
+ throws SAXException, IOException
+ {
+ // 5. return (first) 'public' entry
+ if (publicIds != null) {
+ String retval = (String) publicIds.get (publicId);
+ if (retval != null) {
+ // IF the URI is accessible ...
+ return new InputSource (retval);
+ }
+ }
+
+ // 6. return delegatePublic catalog match [complex]
+ if (publicDelegations != null)
+ return checkDelegations (publicDelegations, publicId,
+ publicId, null);
+
+ return null;
+ }
+
+ // steps as found in OASIS XML catalog spec 7.1.2 or 7.2.2
+ private InputSource mapURI (
+ String uri,
+ Hashtable ids,
+ Hashtable rewrites,
+ Hashtable delegations
+ ) throws SAXException, IOException
+ {
+ // 7.1.2: 2. return (first) 'system' entry
+ // 7.2.2: 2. return (first) 'uri' entry
+ if (ids != null) {
+ String retval = (String) ids.get (uri);
+ if (retval != null) {
+ // IF the URI is accessible ...
+ return new InputSource (retval);
+ }
+ }
+
+ // 7.1.2: 3. return 'rewriteSystem' entries
+ // 7.2.2: 3. return 'rewriteURI' entries
+ if (rewrites != null) {
+ String prefix = null;
+ String replace = null;
+ int prefixLen = -1;
+
+ for (Enumeration e = rewrites.keys ();
+ e.hasMoreElements ();
+ /* NOP */) {
+ String temp = (String) e.nextElement ();
+ int len = -1;
+
+ if (!uri.startsWith (temp))
+ continue;
+ if (prefix != null
+ && (len = temp.length ()) < prefixLen)
+ continue;
+ prefix = temp;
+ prefixLen = len;
+ replace = (String) rewrites.get (temp);
+ }
+ if (prefix != null) {
+ CPStringBuilder buf = new CPStringBuilder (replace);
+ buf.append (uri.substring (prefixLen));
+ // IF the URI is accessible ...
+ return new InputSource (buf.toString ());
+ }
+ }
+
+ // 7.1.2: 4. return 'delegateSystem' catalog match [complex]
+ // 7.2.2: 4. return 'delegateURI' catalog match [complex]
+ if (delegations != null)
+ return checkDelegations (delegations, uri, null, uri);
+
+ return null;
+ }
+
+
+ /**
+ * Returns a URI for an external entity.
+ */
+ public InputSource resolve (
+ boolean usingPublic,
+ String publicId,
+ String systemId
+ ) throws SAXException, IOException
+ {
+ boolean preferSystem;
+ InputSource retval;
+
+ if (hasPreference)
+ preferSystem = !this.usingPublic;
+ else
+ preferSystem = !usingPublic;
+
+ if (publicId != null)
+ publicId = normalizePublicId (false, publicId);
+
+ // behavior here matches section 7.1.1 of the oasis spec
+ if (systemId != null) {
+ if (systemId.startsWith ("urn:publicid:")) {
+ String temp = normalizePublicId (true, systemId);
+ if (publicId == null) {
+ publicId = temp;
+ systemId = null;
+ } else if (!publicId.equals (temp)) {
+ // error; ok to recover by:
+ systemId = null;
+ }
+ } else
+ systemId = normalizeURI (systemId);
+ }
+
+ if (systemId == null && publicId == null)
+ return null;
+
+ if (systemId != null) {
+ retval = mapURI (systemId, systemIds, systemRewrites,
+ systemDelegations);
+ if (retval != null) {
+ retval.setPublicId (publicId);
+ return retval;
+ }
+ }
+
+ if (publicId != null
+ && !(systemId != null && preferSystem)) {
+ retval = locatePublicId (publicId);
+ if (retval != null) {
+ retval.setPublicId (publicId);
+ return retval;
+ }
+ }
+
+ // 7. apply nextCatalog entries
+ if (next != null) {
+ int length = next.size ();
+ for (int i = 0; i < length; i++) {
+ Catalog n = getNext (i);
+ retval = n.resolve (usingPublic, publicId, systemId);
+ if (retval != null)
+ return retval;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Maps one URI into another, for resources that are not defined
+ * using XML external entity or notation syntax.
+ */
+ public InputSource resolveURI (String uri)
+ throws SAXException, IOException
+ {
+ if (uri.startsWith ("urn:publicid:"))
+ return resolve (true, normalizePublicId (true, uri), null);
+
+ InputSource retval;
+
+ uri = normalizeURI (uri);
+
+ // 7.2.2 steps 2-4
+ retval = mapURI (uri, uris, uriRewrites, uriDelegations);
+ if (retval != null)
+ return retval;
+
+ // 7.2.2 step 5. apply nextCatalog entries
+ if (next != null) {
+ int length = next.size ();
+ for (int i = 0; i < length; i++) {
+ Catalog n = getNext (i);
+ retval = n.resolveURI (uri);
+ if (retval != null)
+ return retval;
+ }
+ }
+
+ return null;
+ }
+
+
+ /**
+ * Finds the external subset associated with a given root element.
+ */
+ public InputSource getExternalSubset (String name)
+ throws SAXException, IOException
+ {
+ if (doctypes != null) {
+ String value = (String) doctypes.get (name);
+ if (value != null) {
+ // IF the URI is accessible ...
+ return new InputSource (value);
+ }
+ }
+ if (next != null) {
+ int length = next.size ();
+ for (int i = 0; i < length; i++) {
+ Catalog n = getNext (i);
+ if (n == null)
+ continue;
+ InputSource retval = n.getExternalSubset (name);
+ if (retval != null)
+ return retval;
+ }
+ }
+ return null;
+ }
+
+ private synchronized Catalog getNext (int i)
+ throws SAXException, IOException
+ {
+ Object obj;
+
+ if (next == null || i < 0 || i >= next.size ())
+ return null;
+ obj = next.elementAt (i);
+ if (obj instanceof Catalog)
+ return (Catalog) obj;
+
+ // ok, we deferred reading that catalog till now.
+ // load and cache it.
+ Catalog cat = null;
+
+ try {
+ cat = loadCatalog (parserClass, eh, (String) obj, unified);
+ next.setElementAt (cat, i);
+ } catch (SAXException e) {
+ // must fail quietly, says the OASIS spec
+ } catch (IOException e) {
+ // same applies here
+ }
+ return cat;
+ }
+
+ private InputSource checkDelegations (
+ Hashtable delegations,
+ String id,
+ String publicId, // only one of public/system
+ String systemId // will be non-null...
+ ) throws SAXException, IOException
+ {
+ Vector matches = null;
+ int length = 0;
+
+ // first, see if any prefixes match.
+ for (Enumeration e = delegations.keys ();
+ e.hasMoreElements ();
+ /* NOP */) {
+ String prefix = (String) e.nextElement ();
+
+ if (!id.startsWith (prefix))
+ continue;
+ if (matches == null)
+ matches = new Vector ();
+
+ // maintain in longer->shorter sorted order
+ // NOTE: assumes not many matches will fire!
+ int index;
+
+ for (index = 0; index < length; index++) {
+ String temp = (String) matches.elementAt (index);
+ if (prefix.length () > temp.length ()) {
+ matches.insertElementAt (prefix, index);
+ break;
+ }
+ }
+ if (index == length)
+ matches.addElement (prefix);
+ length++;
+ }
+ if (matches == null)
+ return null;
+
+ // now we know the list of catalogs to replace our "top level"
+ // list ... we use it here, rather than somehow going back and
+ // restarting, since this helps avoid reading most catalogs.
+ // this assumes stackspace won't be a problem.
+ for (int i = 0; i < length; i++) {
+ Catalog catalog = null;
+ InputSource result;
+
+ // get this catalog. we may not have read it yet.
+ synchronized (delegations) {
+ Object prefix = matches.elementAt (i);
+ Object cat = delegations.get (prefix);
+
+ if (cat instanceof Catalog)
+ catalog = (Catalog) cat;
+ else {
+ try {
+ // load and cache that catalog
+ catalog = loadCatalog (parserClass, eh,
+ (String) cat, unified);
+ delegations.put (prefix, catalog);
+ } catch (SAXException e) {
+ // must ignore, says the OASIS spec
+ } catch (IOException e) {
+ // same applies here
+ }
+ }
+ }
+
+ // ignore failed loads, and proceed
+ if (catalog == null)
+ continue;
+
+ // we have a catalog ... resolve!
+ // usingPublic value can't matter, there's no choice
+ result = catalog.resolve (true, publicId, systemId);
+ if (result != null)
+ return result;
+ }
+
+ // if there were no successes, the entire
+ // lookup failed (all the way to top level)
+ throw new DoneDelegation ();
+ }
+ }
+
+
+ /** This is the namespace URI used for OASIS XML Catalogs. */
+ private static final String catalogNamespace =
+ "urn:oasis:names:tc:entity:xmlns:xml:catalog";
+
+
+ /**
+ * Loads/unmarshals one catalog.
+ */
+ private static class Loader extends DefaultHandler2
+ {
+ private boolean preInterned;
+ private ErrorHandler handler;
+ private boolean unified;
+ private int ignoreDepth;
+ private Locator locator;
+ private boolean started;
+ private Hashtable externals;
+ private Stack bases;
+
+ Catalog cat = new Catalog ();
+
+
+ /**
+ * Constructor.
+ * @param flag true iff the parser already interns strings.
+ * @param eh Errors and warnings are delegated to this.
+ * @param unified true keeps one table for URI mappings;
+ * false matches OASIS spec, storing mappings
+ * for URIs and SYSTEM ids in parallel tables.
+ */
+ Loader (boolean flag, ErrorHandler eh, boolean unified)
+ {
+ preInterned = flag;
+ handler = eh;
+ this.unified = unified;
+ cat.unified = unified;
+ cat.eh = eh;
+ }
+
+
+ // strips out fragments
+ private String nofrag (String uri)
+ throws SAXException
+ {
+ if (uri.indexOf ('#') != -1) {
+ warn ("URI with fragment: " + uri);
+ uri = uri.substring (0, uri.indexOf ('#'));
+ }
+ return uri;
+ }
+
+ // absolutizes relative URIs
+ private String absolutize (String uri)
+ throws SAXException
+ {
+ // avoid creating URLs if they're already absolutized,
+ // or if the URI is already using a known scheme
+ if (uri.startsWith ("file:/")
+ || uri.startsWith ("http:/")
+ || uri.startsWith ("https:/")
+ || uri.startsWith ("ftp:/")
+ || uri.startsWith ("urn:")
+ )
+ return uri;
+
+ // otherwise, let's hope the JDK handles this URI scheme.
+ try {
+ URL base = (URL) bases.peek ();
+ return new URL (base, uri).toString ();
+ } catch (Exception e) {
+ fatal ("can't absolutize URI: " + uri);
+ return null;
+ }
+ }
+
+ // recoverable error
+ private void error (String message)
+ throws SAXException
+ {
+ if (handler == null)
+ return;
+ handler.error (new SAXParseException (message, locator));
+ }
+
+ // nonrecoverable error
+ private void fatal (String message)
+ throws SAXException
+ {
+ SAXParseException spe;
+
+ spe = new SAXParseException (message, locator);
+ if (handler != null)
+ handler.fatalError (spe);
+ throw spe;
+ }
+
+ // low severity problem
+ private void warn (String message)
+ throws SAXException
+ {
+ if (handler == null)
+ return;
+ handler.warning (new SAXParseException (message, locator));
+ }
+
+ // callbacks:
+
+ public void setDocumentLocator (Locator l)
+ { locator = l; }
+
+ public void startDocument ()
+ throws SAXException
+ {
+ if (locator == null)
+ error ("no locator!");
+ bases = new Stack ();
+ String uri = locator.getSystemId ();
+ try {
+ bases.push (new URL (uri));
+ } catch (IOException e) {
+ fatal ("bad document base URI: " + uri);
+ }
+ }
+
+ public void endDocument ()
+ throws SAXException
+ {
+ try {
+ if (!started)
+ error ("not a catalog!");
+ } finally {
+ locator = null;
+ handler = null;
+ externals = null;
+ bases = null;
+ }
+ }
+
+ // XML Base support for external entities.
+
+ // NOTE: expects parser is in default "resolve-dtd-uris" mode.
+ public void externalEntityDecl (String name, String pub, String sys)
+ throws SAXException
+ {
+ if (externals == null)
+ externals = new Hashtable ();
+ if (externals.get (name) == null)
+ externals.put (name, pub);
+ }
+
+ public void startEntity (String name)
+ throws SAXException
+ {
+ if (externals == null)
+ return;
+ String uri = (String) externals.get (name);
+
+ // NOTE: breaks if an EntityResolver substitutes these URIs.
+ // If toplevel loader supports one, must intercept calls...
+ if (uri != null) {
+ try {
+ bases.push (new URL (uri));
+ } catch (IOException e) {
+ fatal ("entity '" + name + "', bad URI: " + uri);
+ }
+ }
+ }
+
+ public void endEntity (String name)
+ {
+ if (externals == null)
+ return;
+ String value = (String) externals.get (name);
+
+ if (value != null)
+ bases.pop ();
+ }
+
+ /**
+ * Processes catalog elements, saving their data.
+ */
+ public void startElement (String namespace, String local,
+ String qName, Attributes atts)
+ throws SAXException
+ {
+ // must ignore non-catalog elements, and their contents
+ if (ignoreDepth != 0 || !catalogNamespace.equals (namespace)) {
+ ignoreDepth++;
+ return;
+ }
+
+ // basic sanity checks
+ if (!preInterned)
+ local = local.intern ();
+ if (!started) {
+ started = true;
+ if ("catalog" != local)
+ fatal ("root element not 'catalog': " + local);
+ }
+
+ // Handle any xml:base attribute
+ String xmlbase = atts.getValue ("xml:base");
+
+ if (xmlbase != null) {
+ URL base = (URL) bases.peek ();
+ try {
+ base = new URL (base, xmlbase);
+ } catch (IOException e) {
+ fatal ("can't resolve xml:base attribute: " + xmlbase);
+ }
+ bases.push (base);
+ } else
+ bases.push (bases.peek ());
+
+ // fetch multi-element attributes, apply standard tweaks
+ // values (uri, catalog, rewritePrefix) get normalized too,
+ // as a precaution and since we may compare the values
+ String catalog = atts.getValue ("catalog");
+ if (catalog != null)
+ catalog = normalizeURI (absolutize (catalog));
+
+ String rewritePrefix = atts.getValue ("rewritePrefix");
+ if (rewritePrefix != null)
+ rewritePrefix = normalizeURI (absolutize (rewritePrefix));
+
+ String systemIdStartString;
+ systemIdStartString = atts.getValue ("systemIdStartString");
+ if (systemIdStartString != null) {
+ systemIdStartString = normalizeURI (systemIdStartString);
+ // unmatchable <rewriteSystemId>, <delegateSystemId> elements
+ if (systemIdStartString.startsWith ("urn:publicid:")) {
+ error ("systemIdStartString is really a publicId!!");
+ return;
+ }
+ }
+
+ String uri = atts.getValue ("uri");
+ if (uri != null)
+ uri = normalizeURI (absolutize (uri));
+
+ String uriStartString;
+ uriStartString = atts.getValue ("uriStartString");
+ if (uriStartString != null) {
+ uriStartString = normalizeURI (uriStartString);
+ // unmatchable <rewriteURI>, <delegateURI> elements
+ if (uriStartString.startsWith ("urn:publicid:")) {
+ error ("uriStartString is really a publicId!!");
+ return;
+ }
+ }
+
+ // strictly speaking "group" and "catalog" shouldn't nest
+ // ... arbitrary restriction, no evident motivation
+
+// FIXME stack "prefer" settings (two elements only!) and use
+// them to populate different public mapping/delegation tables
+
+ if ("catalog" == local || "group" == local) {
+ String prefer = atts.getValue ("prefer");
+
+ if (prefer != null && !"public".equals (prefer)) {
+ if (!"system".equals (prefer)) {
+ error ("in <" + local + " ... prefer='...'>, "
+ + "assuming 'public'");
+ prefer = "public";
+ }
+ }
+ if (prefer != null) {
+ if ("catalog" == local) {
+ cat.hasPreference = true;
+ cat.usingPublic = "public".equals (prefer);
+ } else {
+ if (!cat.hasPreference || cat.usingPublic
+ != "public".equals (prefer)) {
+fatal ("<group prefer=...> case not handled");
+ }
+ }
+ } else if ("group" == local && cat.hasPreference) {
+fatal ("<group prefer=...> case not handled");
+ }
+
+ //
+ // PUBLIC ids: cleanly set up for id substitution
+ //
+ } else if ("public" == local) {
+ String publicId = atts.getValue ("publicId");
+ String value = null;
+
+ if (publicId == null || uri == null) {
+ error ("expecting <public publicId=... uri=.../>");
+ return;
+ }
+ publicId = normalizePublicId (true, publicId);
+ uri = nofrag (uri);
+ if (cat.publicIds == null)
+ cat.publicIds = new Hashtable ();
+ else
+ value = (String) cat.publicIds.get (publicId);
+ if (value != null) {
+ if (!value.equals (uri))
+ warn ("ignoring <public...> entry for " + publicId);
+ } else
+ cat.publicIds.put (publicId, uri);
+
+ } else if ("delegatePublic" == local) {
+ String publicIdStartString;
+ Object value = null;
+
+ publicIdStartString = atts.getValue ("publicIdStartString");
+ if (publicIdStartString == null || catalog == null) {
+ error ("expecting <delegatePublic "
+ + "publicIdStartString=... catalog=.../>");
+ return;
+ }
+ publicIdStartString = normalizePublicId (true,
+ publicIdStartString);
+ if (cat.publicDelegations == null)
+ cat.publicDelegations = new Hashtable ();
+ else
+ value = cat.publicDelegations.get (publicIdStartString);
+ if (value != null) {
+ if (!value.equals (catalog))
+ warn ("ignoring <delegatePublic...> entry for "
+ + uriStartString);
+ } else
+ cat.publicDelegations.put (publicIdStartString, catalog);
+
+
+ //
+ // SYSTEM ids: need substitution due to operational issues
+ //
+ } else if ("system" == local) {
+ String systemId = atts.getValue ("systemId");
+ String value = null;
+
+ if (systemId == null || uri == null) {
+ error ("expecting <system systemId=... uri=.../>");
+ return;
+ }
+ systemId = normalizeURI (systemId);
+ uri = nofrag (uri);
+ if (systemId.startsWith ("urn:publicid:")) {
+ error ("systemId is really a publicId!!");
+ return;
+ }
+ if (cat.systemIds == null) {
+ cat.systemIds = new Hashtable ();
+ if (unified)
+ cat.uris = cat.systemIds;
+ } else
+ value = (String) cat.systemIds.get (systemId);
+ if (value != null) {
+ if (!value.equals (uri))
+ warn ("ignoring <system...> entry for " + systemId);
+ } else
+ cat.systemIds.put (systemId, uri);
+
+ } else if ("rewriteSystem" == local) {
+ String value = null;
+
+ if (systemIdStartString == null || rewritePrefix == null
+ || systemIdStartString.length () == 0
+ || rewritePrefix.length () == 0
+ ) {
+ error ("expecting <rewriteSystem "
+ + "systemIdStartString=... rewritePrefix=.../>");
+ return;
+ }
+ if (cat.systemRewrites == null) {
+ cat.systemRewrites = new Hashtable ();
+ if (unified)
+ cat.uriRewrites = cat.systemRewrites;
+ } else
+ value = (String) cat.systemRewrites.get (
+ systemIdStartString);
+ if (value != null) {
+ if (!value.equals (rewritePrefix))
+ warn ("ignoring <rewriteSystem...> entry for "
+ + systemIdStartString);
+ } else
+ cat.systemRewrites.put (systemIdStartString,
+ rewritePrefix);
+
+ } else if ("delegateSystem" == local) {
+ Object value = null;
+
+ if (systemIdStartString == null || catalog == null) {
+ error ("expecting <delegateSystem "
+ + "systemIdStartString=... catalog=.../>");
+ return;
+ }
+ if (cat.systemDelegations == null) {
+ cat.systemDelegations = new Hashtable ();
+ if (unified)
+ cat.uriDelegations = cat.systemDelegations;
+ } else
+ value = cat.systemDelegations.get (systemIdStartString);
+ if (value != null) {
+ if (!value.equals (catalog))
+ warn ("ignoring <delegateSystem...> entry for "
+ + uriStartString);
+ } else
+ cat.systemDelegations.put (systemIdStartString, catalog);
+
+
+ //
+ // URI: just like "system" ID support, except that
+ // fragment IDs are disallowed in "system" elements.
+ //
+ } else if ("uri" == local) {
+ String name = atts.getValue ("name");
+ String value = null;
+
+ if (name == null || uri == null) {
+ error ("expecting <uri name=... uri=.../>");
+ return;
+ }
+ if (name.startsWith ("urn:publicid:")) {
+ error ("name is really a publicId!!");
+ return;
+ }
+ name = normalizeURI (name);
+ if (cat.uris == null) {
+ cat.uris = new Hashtable ();
+ if (unified)
+ cat.systemIds = cat.uris;
+ } else
+ value = (String) cat.uris.get (name);
+ if (value != null) {
+ if (!value.equals (uri))
+ warn ("ignoring <uri...> entry for " + name);
+ } else
+ cat.uris.put (name, uri);
+
+ } else if ("rewriteURI" == local) {
+ String value = null;
+
+ if (uriStartString == null || rewritePrefix == null
+ || uriStartString.length () == 0
+ || rewritePrefix.length () == 0
+ ) {
+ error ("expecting <rewriteURI "
+ + "uriStartString=... rewritePrefix=.../>");
+ return;
+ }
+ if (cat.uriRewrites == null) {
+ cat.uriRewrites = new Hashtable ();
+ if (unified)
+ cat.systemRewrites = cat.uriRewrites;
+ } else
+ value = (String) cat.uriRewrites.get (uriStartString);
+ if (value != null) {
+ if (!value.equals (rewritePrefix))
+ warn ("ignoring <rewriteURI...> entry for "
+ + uriStartString);
+ } else
+ cat.uriRewrites.put (uriStartString, rewritePrefix);
+
+ } else if ("delegateURI" == local) {
+ Object value = null;
+
+ if (uriStartString == null || catalog == null) {
+ error ("expecting <delegateURI "
+ + "uriStartString=... catalog=.../>");
+ return;
+ }
+ if (cat.uriDelegations == null) {
+ cat.uriDelegations = new Hashtable ();
+ if (unified)
+ cat.systemDelegations = cat.uriDelegations;
+ } else
+ value = cat.uriDelegations.get (uriStartString);
+ if (value != null) {
+ if (!value.equals (catalog))
+ warn ("ignoring <delegateURI...> entry for "
+ + uriStartString);
+ } else
+ cat.uriDelegations.put (uriStartString, catalog);
+
+ //
+ // NON-DELEGATING approach to modularity
+ //
+ } else if ("nextCatalog" == local) {
+ if (catalog == null) {
+ error ("expecting <nextCatalog catalog=.../>");
+ return;
+ }
+ if (cat.next == null)
+ cat.next = new Vector ();
+ cat.next.addElement (catalog);
+
+ //
+ // EXTENSIONS from appendix E
+ //
+ } else if ("doctype" == local) {
+ String name = atts.getValue ("name");
+ String value = null;
+
+ if (name == null || uri == null) {
+ error ("expecting <doctype name=... uri=.../>");
+ return;
+ }
+ name = normalizeURI (name);
+ if (cat.doctypes == null)
+ cat.doctypes = new Hashtable ();
+ else
+ value = (String) cat.doctypes.get (name);
+ if (value != null) {
+ if (!value.equals (uri))
+ warn ("ignoring <doctype...> entry for "
+ + uriStartString);
+ } else
+ cat.doctypes.put (name, uri);
+
+
+ //
+ // RESERVED ... ignore (like reserved attributes) but warn
+ //
+ } else {
+ warn ("ignoring unknown catalog element: " + local);
+ ignoreDepth++;
+ }
+ }
+
+ public void endElement (String uri, String local, String qName)
+ throws SAXException
+ {
+ if (ignoreDepth != 0)
+ ignoreDepth--;
+ else
+ bases.pop ();
+ }
+ }
+}
diff --git a/libjava/classpath/gnu/xml/util/XHTMLWriter.java b/libjava/classpath/gnu/xml/util/XHTMLWriter.java
new file mode 100644
index 000000000..3a0c1adb7
--- /dev/null
+++ b/libjava/classpath/gnu/xml/util/XHTMLWriter.java
@@ -0,0 +1,114 @@
+/* XHTMLWriter.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.util;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+
+
+/**
+ * This extends XMLWriter to create a class which defaults to writing
+ * XHTML text, preferring the US-ASCII encoding. It adds no unique
+ * functionality, only changing the defaults slightly to simplify writing
+ * XHTML processing components by providing a bean class whose properties
+ * have more convenient defaults. An artifact of using the US-ASCII
+ * encoding is that no XML declaration is written, so that HTML tools
+ * that can't accept them will not become confused. Components can treat
+ * the output as UTF-8, ISO-8859-1, or US-ASCII without incurring any
+ * data loss.
+ *
+ * @author David Brownell
+ *
+ * @deprecated Please use the javax.xml.stream APIs instead
+ */
+public class XHTMLWriter extends XMLWriter
+{
+ /**
+ * Constructs this handler with System.out used to write
+ * SAX events using the US-ASCII encoding, as XHTML.
+ */
+ public XHTMLWriter ()
+ throws IOException
+ {
+ this (System.out);
+ }
+
+ /**
+ * Constructs this handler such that the specified output stream
+ * is used to write SAX events in the US-ASCII encoding, as XHTML.
+ *
+ * @param out Where US-ASCII encoding of the stream of SAX
+ * events will be sent.
+ */
+ public XHTMLWriter (OutputStream out)
+ throws IOException
+ {
+ // not all JVMs understand "ASCII" as an encoding name, so
+ // we use 8859_1 (they all seem to handle that one) and
+ // make the echo handler filter out non-ASCII characters
+ this (new OutputStreamWriter (out, "8859_1"), "US-ASCII");
+ }
+
+ /**
+ * Constructs this handler such that the specified output stream
+ * is used to write SAX events as XHTML.
+ *
+ * @param out Where the stream of SAX events will be written.
+ */
+ public XHTMLWriter (Writer out)
+ {
+ this (out, null);
+ }
+
+ /**
+ * Constructs this handler such that the specified output stream
+ * is used to write SAX events as XHTML, labeled with the specified
+ * encoding.
+ *
+ * @param out Where the stream of SAX events will be written.
+ * @param encoding If non-null, this names the encoding to be
+ * placed in the encoding declaration.
+ */
+ public XHTMLWriter (Writer out, String encoding)
+ {
+ super (out, encoding);
+ setXhtml (true);
+ }
+}
diff --git a/libjava/classpath/gnu/xml/util/XMLWriter.java b/libjava/classpath/gnu/xml/util/XMLWriter.java
new file mode 100644
index 000000000..a371debff
--- /dev/null
+++ b/libjava/classpath/gnu/xml/util/XMLWriter.java
@@ -0,0 +1,1931 @@
+/* XMLWriter.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.util;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.io.BufferedWriter;
+import java.io.CharConversionException;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.util.Stack;
+
+import org.xml.sax.*;
+import org.xml.sax.ext.*;
+import org.xml.sax.helpers.*;
+
+
+/**
+ * This class is a SAX handler which writes all its input as a well formed
+ * XML or XHTML document. If driven using SAX2 events, this output may
+ * include a recreated document type declaration, subject to limitations
+ * of SAX (no internal subset exposed) or DOM (the important declarations,
+ * with their documentation, are discarded).
+ *
+ * <p> By default, text is generated "as-is", but some optional modes
+ * are supported. Pretty-printing is supported, to make life easier
+ * for people reading the output. XHTML (1.0) output has can be made
+ * particularly pretty; all the built-in character entities are known.
+ * Canonical XML can also be generated, assuming the input is properly
+ * formed.
+ *
+ * <hr>
+ *
+ * <p> Some of the methods on this class are intended for applications to
+ * use directly, rather than as pure SAX2 event callbacks. Some of those
+ * methods access the JavaBeans properties (used to tweak output formats,
+ * for example canonicalization and pretty printing). Subclasses
+ * are expected to add new behaviors, not to modify current behavior, so
+ * many such methods are final.</p>
+ *
+ * <p> The <em>write*()</em> methods may be slightly simpler for some
+ * applications to use than direct callbacks. For example, they support
+ * a simple policy for encoding data items as the content of a single element.
+ *
+ * <p> To reuse an XMLWriter you must provide it with a new Writer, since
+ * this handler closes the writer it was given as part of its endDocument()
+ * handling. (XML documents have an end of input, and the way to encode
+ * that on a stream is to close it.) </p>
+ *
+ * <hr>
+ *
+ * <p> Note that any relative URIs in the source document, as found in
+ * entity and notation declarations, ought to have been fully resolved by
+ * the parser providing events to this handler. This means that the
+ * output text should only have fully resolved URIs, which may not be
+ * the desired behavior in cases where later binding is desired. </p>
+ *
+ * <p> <em>Note that due to SAX2 defaults, you may need to manually
+ * ensure that the input events are XML-conformant with respect to namespace
+ * prefixes and declarations. {@link gnu.xml.pipeline.NSFilter} is
+ * one solution to this problem, in the context of processing pipelines.</em>
+ * Something as simple as connecting this handler to a parser might not
+ * generate the correct output. Another workaround is to ensure that the
+ * <em>namespace-prefixes</em> feature is always set to true, if you're
+ * hooking this directly up to some XMLReader implementation.
+ *
+ * @see gnu.xml.pipeline.TextConsumer
+ *
+ * @author David Brownell
+ *
+ * @deprecated Please use the javax.xml.stream APIs instead
+ */
+public class XMLWriter
+ implements ContentHandler, LexicalHandler, DTDHandler, DeclHandler
+{
+ // text prints/escapes differently depending on context
+ // CTX_ENTITY ... entity literal value
+ // CTX_ATTRIBUTE ... attribute literal value
+ // CTX_CONTENT ... content of an element
+ // CTX_UNPARSED ... CDATA, comment, PI, names, etc
+ // CTX_NAME ... name or nmtoken, no escapes possible
+ private static final int CTX_ENTITY = 1;
+ private static final int CTX_ATTRIBUTE = 2;
+ private static final int CTX_CONTENT = 3;
+ private static final int CTX_UNPARSED = 4;
+ private static final int CTX_NAME = 5;
+
+// FIXME: names (element, attribute, PI, notation, etc) are not
+// currently written out with range checks (escapeChars).
+// In non-XHTML, some names can't be directly written; panic!
+
+ private static String sysEOL;
+
+ static {
+ try {
+ sysEOL = System.getProperty ("line.separator", "\n");
+
+ // don't use the system's EOL if it's illegal XML.
+ if (!isLineEnd (sysEOL))
+ sysEOL = "\n";
+
+ } catch (SecurityException e) {
+ sysEOL = "\n";
+ }
+ }
+
+ private static boolean isLineEnd (String eol)
+ {
+ return "\n".equals (eol)
+ || "\r".equals (eol)
+ || "\r\n".equals (eol);
+ }
+
+ private Writer out;
+ private boolean inCDATA;
+ private int elementNestLevel;
+ private String eol = sysEOL;
+
+ private short dangerMask;
+ private CPStringBuilder stringBuf;
+ private Locator locator;
+ private ErrorHandler errHandler;
+
+ private boolean expandingEntities = false;
+ private int entityNestLevel;
+ private boolean xhtml;
+ private boolean startedDoctype;
+ private String encoding;
+
+ private boolean canonical;
+ private boolean inDoctype;
+ private boolean inEpilogue;
+
+ // pretty printing controls
+ private boolean prettyPrinting;
+ private int column;
+ private boolean noWrap;
+ private Stack space = new Stack ();
+
+ // this is not a hard'n'fast rule -- longer lines are OK,
+ // but are to be avoided. Here, prettyprinting is more to
+ // show structure "cleanly" than to be precise about it.
+ // better to have ragged layout than one line 24Kb long.
+ private static final int lineLength = 75;
+
+
+ /**
+ * Constructs this handler with System.out used to write SAX events
+ * using the UTF-8 encoding. Avoid using this except when you know
+ * it's safe to close System.out at the end of the document.
+ */
+ public XMLWriter () throws IOException
+ { this (System.out); }
+
+ /**
+ * Constructs a handler which writes all input to the output stream
+ * in the UTF-8 encoding, and closes it when endDocument is called.
+ * (Yes it's annoying that this throws an exception -- but there's
+ * really no way around it, since it's barely possible a JDK may
+ * exist somewhere that doesn't know how to emit UTF-8.)
+ */
+ public XMLWriter (OutputStream out) throws IOException
+ {
+ this (new OutputStreamWriter (out, "UTF8"));
+ }
+
+ /**
+ * Constructs a handler which writes all input to the writer, and then
+ * closes the writer when the document ends. If an XML declaration is
+ * written onto the output, and this class can determine the name of
+ * the character encoding for this writer, that encoding name will be
+ * included in the XML declaration.
+ *
+ * <P> See the description of the constructor which takes an encoding
+ * name for imporant information about selection of encodings.
+ *
+ * @param writer XML text is written to this writer.
+ */
+ public XMLWriter (Writer writer)
+ {
+ this (writer, null);
+ }
+
+ /**
+ * Constructs a handler which writes all input to the writer, and then
+ * closes the writer when the document ends. If an XML declaration is
+ * written onto the output, this class will use the specified encoding
+ * name in that declaration. If no encoding name is specified, no
+ * encoding name will be declared unless this class can otherwise
+ * determine the name of the character encoding for this writer.
+ *
+ * <P> At this time, only the UTF-8 ("UTF8") and UTF-16 ("Unicode")
+ * output encodings are fully lossless with respect to XML data. If you
+ * use any other encoding you risk having your data be silently mangled
+ * on output, as the standard Java character encoding subsystem silently
+ * maps non-encodable characters to a question mark ("?") and will not
+ * report such errors to applications.
+ *
+ * <p> For a few other encodings the risk can be reduced. If the writer is
+ * a java.io.OutputStreamWriter, and uses either the ISO-8859-1 ("8859_1",
+ * "ISO8859_1", etc) or US-ASCII ("ASCII") encodings, content which
+ * can't be encoded in those encodings will be written safely. Where
+ * relevant, the XHTML entity names will be used; otherwise, numeric
+ * character references will be emitted.
+ *
+ * <P> However, there remain a number of cases where substituting such
+ * entity or character references is not an option. Such references are
+ * not usable within a DTD, comment, PI, or CDATA section. Neither may
+ * they be used when element, attribute, entity, or notation names have
+ * the problematic characters.
+ *
+ * @param writer XML text is written to this writer.
+ * @param encoding if non-null, and an XML declaration is written,
+ * this is the name that will be used for the character encoding.
+ */
+ public XMLWriter (Writer writer, String encoding)
+ {
+ setWriter (writer, encoding);
+ }
+
+ private void setEncoding (String encoding)
+ {
+ if (encoding == null && out instanceof OutputStreamWriter)
+ encoding = ((OutputStreamWriter)out).getEncoding ();
+
+ if (encoding != null) {
+ encoding = encoding.toUpperCase ();
+
+ // Use official encoding names where we know them,
+ // avoiding the Java-only names. When using common
+ // encodings where we can easily tell if characters
+ // are out of range, we'll escape out-of-range
+ // characters using character refs for safety.
+
+ // I _think_ these are all the main synonyms for these!
+ if ("UTF8".equals (encoding)) {
+ encoding = "UTF-8";
+ } else if ("US-ASCII".equals (encoding)
+ || "ASCII".equals (encoding)) {
+ dangerMask = (short) 0xff80;
+ encoding = "US-ASCII";
+ } else if ("ISO-8859-1".equals (encoding)
+ || "8859_1".equals (encoding)
+ || "ISO8859_1".equals (encoding)) {
+ dangerMask = (short) 0xff00;
+ encoding = "ISO-8859-1";
+ } else if ("UNICODE".equals (encoding)
+ || "UNICODE-BIG".equals (encoding)
+ || "UNICODE-LITTLE".equals (encoding)) {
+ encoding = "UTF-16";
+
+ // TODO: UTF-16BE, UTF-16LE ... no BOM; what
+ // release of JDK supports those Unicode names?
+ }
+
+ if (dangerMask != 0)
+ stringBuf = new CPStringBuilder ();
+ }
+
+ this.encoding = encoding;
+ }
+
+
+ /**
+ * Resets the handler to write a new text document.
+ *
+ * @param writer XML text is written to this writer.
+ * @param encoding if non-null, and an XML declaration is written,
+ * this is the name that will be used for the character encoding.
+ *
+ * @exception IllegalStateException if the current
+ * document hasn't yet ended (with {@link #endDocument})
+ */
+ final public void setWriter (Writer writer, String encoding)
+ {
+ if (out != null)
+ throw new IllegalStateException (
+ "can't change stream in mid course");
+ out = writer;
+ if (out != null)
+ setEncoding (encoding);
+ if (!(out instanceof BufferedWriter))
+ out = new BufferedWriter (out);
+ space.push ("default");
+ }
+
+ /**
+ * Assigns the line ending style to be used on output.
+ * @param eolString null to use the system default; else
+ * "\n", "\r", or "\r\n".
+ */
+ final public void setEOL (String eolString)
+ {
+ if (eolString == null)
+ eol = sysEOL;
+ else if (!isLineEnd (eolString))
+ eol = eolString;
+ else
+ throw new IllegalArgumentException (eolString);
+ }
+
+ /**
+ * Assigns the error handler to be used to present most fatal
+ * errors.
+ */
+ public void setErrorHandler (ErrorHandler handler)
+ {
+ errHandler = handler;
+ }
+
+ /**
+ * Used internally and by subclasses, this encapsulates the logic
+ * involved in reporting fatal errors. It uses locator information
+ * for good diagnostics, if available, and gives the application's
+ * ErrorHandler the opportunity to handle the error before throwing
+ * an exception.
+ */
+ protected void fatal (String message, Exception e)
+ throws SAXException
+ {
+ SAXParseException x;
+
+ if (locator == null)
+ x = new SAXParseException (message, null, null, -1, -1, e);
+ else
+ x = new SAXParseException (message, locator, e);
+ if (errHandler != null)
+ errHandler.fatalError (x);
+ throw x;
+ }
+
+
+ // JavaBeans properties
+
+ /**
+ * Controls whether the output should attempt to follow the "transitional"
+ * XHTML rules so that it meets the "HTML Compatibility Guidelines"
+ * appendix in the XHTML specification. A "transitional" Document Type
+ * Declaration (DTD) is placed near the beginning of the output document,
+ * instead of whatever DTD would otherwise have been placed there, and
+ * XHTML empty elements are printed specially. When writing text in
+ * US-ASCII or ISO-8859-1 encodings, the predefined XHTML internal
+ * entity names are used (in preference to character references) when
+ * writing content characters which can't be expressed in those encodings.
+ *
+ * <p> When this option is enabled, it is the caller's responsibility
+ * to ensure that the input is otherwise valid as XHTML. Things to
+ * be careful of in all cases, as described in the appendix referenced
+ * above, include: <ul>
+ *
+ * <li> Element and attribute names must be in lower case, both
+ * in the document and in any CSS style sheet.
+ * <li> All XML constructs must be valid as defined by the XHTML
+ * "transitional" DTD (including all familiar constructs,
+ * even deprecated ones).
+ * <li> The root element must be "html".
+ * <li> Elements that must be empty (such as <em>&lt;br&gt;</em>
+ * must have no content.
+ * <li> Use both <em>lang</em> and <em>xml:lang</em> attributes
+ * when specifying language.
+ * <li> Similarly, use both <em>id</em> and <em>name</em> attributes
+ * when defining elements that may be referred to through
+ * URI fragment identifiers ... and make sure that the
+ * value is a legal NMTOKEN, since not all such HTML 4.0
+ * identifiers are valid in XML.
+ * <li> Be careful with character encodings; make sure you provide
+ * a <em>&lt;meta http-equiv="Content-type"
+ * content="text/xml;charset=..." /&gt;</em> element in
+ * the HTML "head" element, naming the same encoding
+ * used to create this handler. Also, if that encoding
+ * is anything other than US-ASCII, make sure that if
+ * the document is given a MIME content type, it has
+ * a <em>charset=...</em> attribute with that encoding.
+ * </ul>
+ *
+ * <p> Additionally, some of the oldest browsers have additional
+ * quirks, to address with guidelines such as: <ul>
+ *
+ * <li> Processing instructions may be rendered, so avoid them.
+ * (Similarly for an XML declaration.)
+ * <li> Embedded style sheets and scripts should not contain XML
+ * markup delimiters: &amp;, &lt;, and ]]&gt; are trouble.
+ * <li> Attribute values should not have line breaks or multiple
+ * consecutive white space characters.
+ * <li> Use no more than one of the deprecated (transitional)
+ * <em>&lt;isindex&gt;</em> elements.
+ * <li> Some boolean attributes (such as <em>compact, checked,
+ * disabled, readonly, selected,</em> and more) confuse
+ * some browsers, since they only understand minimized
+ * versions which are illegal in XML.
+ * </ul>
+ *
+ * <p> Also, some characteristics of the resulting output may be
+ * a function of whether the document is later given a MIME
+ * content type of <em>text/html</em> rather than one indicating
+ * XML (<em>application/xml</em> or <em>text/xml</em>). Worse,
+ * some browsers ignore MIME content types and prefer to rely URI
+ * name suffixes -- so an "index.xml" could always be XML, never
+ * XHTML, no matter its MIME type.
+ */
+ final public void setXhtml (boolean value)
+ {
+ if (locator != null)
+ throw new IllegalStateException ("started parsing");
+ xhtml = value;
+ if (xhtml)
+ canonical = false;
+ }
+
+ /**
+ * Returns true if the output attempts to echo the input following
+ * "transitional" XHTML rules and matching the "HTML Compatibility
+ * Guidelines" so that an HTML version 3 browser can read the output
+ * as HTML; returns false (the default) othewise.
+ */
+ final public boolean isXhtml ()
+ {
+ return xhtml;
+ }
+
+ /**
+ * Controls whether the output text contains references to
+ * entities (the default), or instead contains the expanded
+ * values of those entities.
+ */
+ final public void setExpandingEntities (boolean value)
+ {
+ if (locator != null)
+ throw new IllegalStateException ("started parsing");
+ expandingEntities = value;
+ if (!expandingEntities)
+ canonical = false;
+ }
+
+ /**
+ * Returns true if the output will have no entity references;
+ * returns false (the default) otherwise.
+ */
+ final public boolean isExpandingEntities ()
+ {
+ return expandingEntities;
+ }
+
+ /**
+ * Controls pretty-printing, which by default is not enabled
+ * (and currently is most useful for XHTML output).
+ * Pretty printing enables structural indentation, sorting of attributes
+ * by name, line wrapping, and potentially other mechanisms for making
+ * output more or less readable.
+ *
+ * <p> At this writing, structural indentation and line wrapping are
+ * enabled when pretty printing is enabled and the <em>xml:space</em>
+ * attribute has the value <em>default</em> (its other legal value is
+ * <em>preserve</em>, as defined in the XML specification). The three
+ * XHTML element types which use another value are recognized by their
+ * names (namespaces are ignored).
+ *
+ * <p> Also, for the record, the "pretty" aspect of printing here
+ * is more to provide basic structure on outputs that would otherwise
+ * risk being a single long line of text. For now, expect the
+ * structure to be ragged ... unless you'd like to submit a patch
+ * to make this be more strictly formatted!
+ *
+ * @exception IllegalStateException thrown if this method is invoked
+ * after output has begun.
+ */
+ final public void setPrettyPrinting (boolean value)
+ {
+ if (locator != null)
+ throw new IllegalStateException ("started parsing");
+ prettyPrinting = value;
+ if (prettyPrinting)
+ canonical = false;
+ }
+
+ /**
+ * Returns value of flag controlling pretty printing.
+ */
+ final public boolean isPrettyPrinting ()
+ {
+ return prettyPrinting;
+ }
+
+
+ /**
+ * Sets the output style to be canonicalized. Input events must
+ * meet requirements that are slightly more stringent than the
+ * basic well-formedness ones, and include: <ul>
+ *
+ * <li> Namespace prefixes must not have been changed from those
+ * in the original document. (This may only be ensured by setting
+ * the SAX2 XMLReader <em>namespace-prefixes</em> feature flag;
+ * by default, it is cleared.)
+ *
+ * <li> Redundant namespace declaration attributes have been
+ * removed. (If an ancestor element defines a namespace prefix
+ * and that declaration hasn't been overriden, an element must
+ * not redeclare it.)
+ *
+ * <li> If comments are not to be included in the canonical output,
+ * they must first be removed from the input event stream; this
+ * <em>Canonical XML with comments</em> by default.
+ *
+ * <li> If the input character encoding was not UCS-based, the
+ * character data must have been normalized using Unicode
+ * Normalization Form C. (UTF-8 and UTF-16 are UCS-based.)
+ *
+ * <li> Attribute values must have been normalized, as is done
+ * by any conformant XML processor which processes all external
+ * parameter entities.
+ *
+ * <li> Similarly, attribute value defaulting has been performed.
+ *
+ * </ul>
+ *
+ * <p> Note that fragments of XML documents, as specified by an XPath
+ * node set, may be canonicalized. In such cases, elements may need
+ * some fixup (for <em>xml:*</em> attributes and application-specific
+ * context).
+ *
+ * @exception IllegalArgumentException if the output encoding
+ * is anything other than UTF-8.
+ */
+ final public void setCanonical (boolean value)
+ {
+ if (value && !"UTF-8".equals (encoding))
+ throw new IllegalArgumentException ("encoding != UTF-8");
+ canonical = value;
+ if (canonical) {
+ prettyPrinting = xhtml = false;
+ expandingEntities = true;
+ eol = "\n";
+ }
+ }
+
+
+ /**
+ * Returns value of flag controlling canonical output.
+ */
+ final public boolean isCanonical ()
+ {
+ return canonical;
+ }
+
+
+ /**
+ * Flushes the output stream. When this handler is used in long lived
+ * pipelines, it can be important to flush buffered state, for example
+ * so that it can reach the disk as part of a state checkpoint.
+ */
+ final public void flush ()
+ throws IOException
+ {
+ if (out != null)
+ out.flush ();
+ }
+
+
+ // convenience routines
+
+// FIXME: probably want a subclass that holds a lot of these...
+// and maybe more!
+
+ /**
+ * Writes the string as if characters() had been called on the contents
+ * of the string. This is particularly useful when applications act as
+ * producers and write data directly to event consumers.
+ */
+ final public void write (String data)
+ throws SAXException
+ {
+ char buf [] = data.toCharArray ();
+ characters (buf, 0, buf.length);
+ }
+
+
+ /**
+ * Writes an element that has content consisting of a single string.
+ * @see #writeEmptyElement
+ * @see #startElement
+ */
+ public void writeElement (
+ String uri,
+ String localName,
+ String qName,
+ Attributes atts,
+ String content
+ ) throws SAXException
+ {
+ if (content == null || content.length () == 0) {
+ writeEmptyElement (uri, localName, qName, atts);
+ return;
+ }
+ startElement (uri, localName, qName, atts);
+ char chars [] = content.toCharArray ();
+ characters (chars, 0, chars.length);
+ endElement (uri, localName, qName);
+ }
+
+
+ /**
+ * Writes an element that has content consisting of a single integer,
+ * encoded as a decimal string.
+ * @see #writeEmptyElement
+ * @see #startElement
+ */
+ public void writeElement (
+ String uri,
+ String localName,
+ String qName,
+ Attributes atts,
+ int content
+ ) throws SAXException
+ {
+ writeElement (uri, localName, qName, atts, Integer.toString (content));
+ }
+
+
+ // SAX1 ContentHandler
+ /** <b>SAX1</b>: provides parser status information */
+ final public void setDocumentLocator (Locator l)
+ {
+ locator = l;
+ }
+
+
+ // URL for dtd that validates against all normal HTML constructs
+ private static final String xhtmlFullDTD =
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";
+
+
+ /**
+ * <b>SAX1</b>: indicates the beginning of a document parse.
+ * If you're writing (well formed) fragments of XML, neither
+ * this nor endDocument should be called.
+ */
+ // NOT final
+ public void startDocument ()
+ throws SAXException
+ {
+ try {
+ if (out == null)
+ throw new IllegalStateException (
+ "null Writer given to XMLWriter");
+
+ // Not all parsers provide the locator we want; this also
+ // flags whether events are being sent to this object yet.
+ // We could only have this one call if we only printed whole
+ // documents ... but we also print fragments, so most of the
+ // callbacks here replicate this test.
+
+ if (locator == null)
+ locator = new LocatorImpl ();
+
+ // Unless the data is in US-ASCII or we're canonicalizing, write
+ // the XML declaration if we know the encoding. US-ASCII won't
+ // normally get mangled by web server confusion about the
+ // character encodings used. Plus, it's an easy way to
+ // ensure we can write ASCII that's unlikely to confuse
+ // elderly HTML parsers.
+
+ if (!canonical
+ && dangerMask != (short) 0xff80
+ && encoding != null) {
+ rawWrite ("<?xml version='1.0'");
+ rawWrite (" encoding='" + encoding + "'");
+ rawWrite ("?>");
+ newline ();
+ }
+
+ if (xhtml) {
+
+ rawWrite ("<!DOCTYPE html PUBLIC");
+ newline ();
+ rawWrite (" '-//W3C//DTD XHTML 1.0 Transitional//EN'");
+ newline ();
+ rawWrite (" '");
+ // NOTE: URL (above) matches the REC
+ rawWrite (xhtmlFullDTD);
+ rawWrite ("'>");
+ newline ();
+ newline ();
+
+ // fake the rest of the handler into ignoring
+ // everything until the root element, so any
+ // XHTML DTD comments, PIs, etc are ignored
+ startedDoctype = true;
+ }
+
+ entityNestLevel = 0;
+
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /**
+ * <b>SAX1</b>: indicates the completion of a parse.
+ * Note that all complete SAX event streams make this call, even
+ * if an error is reported during a parse.
+ */
+ // NOT final
+ public void endDocument ()
+ throws SAXException
+ {
+ try {
+ if (!canonical) {
+ newline ();
+ newline ();
+ }
+ out.close ();
+ out = null;
+ locator = null;
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ // XHTML elements declared as EMPTY print differently
+ final private static boolean isEmptyElementTag (String tag)
+ {
+ switch (tag.charAt (0)) {
+ case 'a': return "area".equals (tag);
+ case 'b': return "base".equals (tag)
+ || "basefont".equals (tag)
+ || "br".equals (tag);
+ case 'c': return "col".equals (tag);
+ case 'f': return "frame".equals (tag);
+ case 'h': return "hr".equals (tag);
+ case 'i': return "img".equals (tag)
+ || "input".equals (tag)
+ || "isindex".equals (tag);
+ case 'l': return "link".equals (tag);
+ case 'm': return "meta".equals (tag);
+ case 'p': return "param".equals (tag);
+ }
+ return false;
+ }
+
+ private static boolean indentBefore (String tag)
+ {
+ // basically indent before block content
+ // and within structure like tables, lists
+ switch (tag.charAt (0)) {
+ case 'a': return "applet".equals (tag);
+ case 'b': return "body".equals (tag)
+ || "blockquote".equals (tag);
+ case 'c': return "center".equals (tag);
+ case 'f': return "frame".equals (tag)
+ || "frameset".equals (tag);
+ case 'h': return "head".equals (tag);
+ case 'm': return "meta".equals (tag);
+ case 'o': return "object".equals (tag);
+ case 'p': return "param".equals (tag)
+ || "pre".equals (tag);
+ case 's': return "style".equals (tag);
+ case 't': return "title".equals (tag)
+ || "td".equals (tag)
+ || "th".equals (tag);
+ }
+ // ... but not inline elements like "em", "b", "font"
+ return false;
+ }
+
+ private static boolean spaceBefore (String tag)
+ {
+ // blank line AND INDENT before certain structural content
+ switch (tag.charAt (0)) {
+ case 'h': return "h1".equals (tag)
+ || "h2".equals (tag)
+ || "h3".equals (tag)
+ || "h4".equals (tag)
+ || "h5".equals (tag)
+ || "h6".equals (tag)
+ || "hr".equals (tag);
+ case 'l': return "li".equals (tag);
+ case 'o': return "ol".equals (tag);
+ case 'p': return "p".equals (tag);
+ case 't': return "table".equals (tag)
+ || "tr".equals (tag);
+ case 'u': return "ul".equals (tag);
+ }
+ return false;
+ }
+
+ // XHTML DTDs say these three have xml:space="preserve"
+ private static boolean spacePreserve (String tag)
+ {
+ return "pre".equals (tag)
+ || "style".equals (tag)
+ || "script".equals (tag);
+ }
+
+ /**
+ * <b>SAX2</b>: ignored.
+ */
+ final public void startPrefixMapping (String prefix, String uri)
+ {}
+
+ /**
+ * <b>SAX2</b>: ignored.
+ */
+ final public void endPrefixMapping (String prefix)
+ {}
+
+ private void writeStartTag (
+ String name,
+ Attributes atts,
+ boolean isEmpty
+ ) throws SAXException, IOException
+ {
+ rawWrite ('<');
+ rawWrite (name);
+
+ // write out attributes ... sorting is particularly useful
+ // with output that's been heavily defaulted.
+ if (atts != null && atts.getLength () != 0) {
+
+ // Set up to write, with optional sorting
+ int indices [] = new int [atts.getLength ()];
+
+ for (int i= 0; i < indices.length; i++)
+ indices [i] = i;
+
+ // optionally sort
+
+// FIXME: canon xml demands xmlns nodes go first,
+// and sorting by URI first (empty first) then localname
+// it should maybe use a different sort
+
+ if (canonical || prettyPrinting) {
+
+ // insertion sort by attribute name
+ for (int i = 1; i < indices.length; i++) {
+ int n = indices [i], j;
+ String s = atts.getQName (n);
+
+ for (j = i - 1; j >= 0; j--) {
+ if (s.compareTo (atts.getQName (indices [j]))
+ >= 0)
+ break;
+ indices [j + 1] = indices [j];
+ }
+ indices [j + 1] = n;
+ }
+ }
+
+ // write, sorted or no
+ for (int i= 0; i < indices.length; i++) {
+ String s = atts.getQName (indices [i]);
+
+ if (s == null || "".equals (s))
+ throw new IllegalArgumentException ("no XML name");
+ rawWrite (" ");
+ rawWrite (s);
+ rawWrite ("=");
+ writeQuotedValue (atts.getValue (indices [i]),
+ CTX_ATTRIBUTE);
+ }
+ }
+ if (isEmpty)
+ rawWrite (" /");
+ rawWrite ('>');
+ }
+
+ /**
+ * <b>SAX2</b>: indicates the start of an element.
+ * When XHTML is in use, avoid attribute values with
+ * line breaks or multiple whitespace characters, since
+ * not all user agents handle them correctly.
+ */
+ final public void startElement (
+ String uri,
+ String localName,
+ String qName,
+ Attributes atts
+ ) throws SAXException
+ {
+ startedDoctype = false;
+
+ if (locator == null)
+ locator = new LocatorImpl ();
+
+ if (qName == null || "".equals (qName))
+ throw new IllegalArgumentException ("no XML name");
+
+ try {
+ if (entityNestLevel != 0)
+ return;
+ if (prettyPrinting) {
+ String whitespace = null;
+
+ if (xhtml && spacePreserve (qName))
+ whitespace = "preserve";
+ else if (atts != null)
+ whitespace = atts.getValue ("xml:space");
+ if (whitespace == null)
+ whitespace = (String) space.peek ();
+ space.push (whitespace);
+
+ if ("default".equals (whitespace)) {
+ if (xhtml) {
+ if (spaceBefore (qName)) {
+ newline ();
+ doIndent ();
+ } else if (indentBefore (qName))
+ doIndent ();
+ // else it's inlined, modulo line length
+ // FIXME: incrementing element nest level
+ // for inlined elements causes ugliness
+ } else
+ doIndent ();
+ }
+ }
+ elementNestLevel++;
+ writeStartTag (qName, atts, xhtml && isEmptyElementTag (qName));
+
+ if (xhtml) {
+// FIXME: if this is an XHTML "pre" element, turn
+// off automatic wrapping.
+ }
+
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /**
+ * Writes an empty element.
+ * @see #startElement
+ */
+ public void writeEmptyElement (
+ String uri,
+ String localName,
+ String qName,
+ Attributes atts
+ ) throws SAXException
+ {
+ if (canonical) {
+ startElement (uri, localName, qName, atts);
+ endElement (uri, localName, qName);
+ } else {
+ try {
+ writeStartTag (qName, atts, true);
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+ }
+
+
+ /** <b>SAX2</b>: indicates the end of an element */
+ final public void endElement (String uri, String localName, String qName)
+ throws SAXException
+ {
+ if (qName == null || "".equals (qName))
+ throw new IllegalArgumentException ("no XML name");
+
+ try {
+ elementNestLevel--;
+ if (entityNestLevel != 0)
+ return;
+ if (xhtml && isEmptyElementTag (qName))
+ return;
+ rawWrite ("</");
+ rawWrite (qName);
+ rawWrite ('>');
+
+ if (prettyPrinting) {
+ if (!space.empty ())
+ space.pop ();
+ else
+ fatal ("stack discipline", null);
+ }
+ if (elementNestLevel == 0)
+ inEpilogue = true;
+
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /** <b>SAX1</b>: reports content characters */
+ final public void characters (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (locator == null)
+ locator = new LocatorImpl ();
+
+ try {
+ if (entityNestLevel != 0)
+ return;
+ if (inCDATA) {
+ escapeChars (ch, start, length, CTX_UNPARSED);
+ } else {
+ escapeChars (ch, start, length, CTX_CONTENT);
+ }
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /** <b>SAX1</b>: reports ignorable whitespace */
+ final public void ignorableWhitespace (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (locator == null)
+ locator = new LocatorImpl ();
+
+ try {
+ if (entityNestLevel != 0)
+ return;
+ // don't forget to map NL to CRLF, CR, etc
+ escapeChars (ch, start, length, CTX_CONTENT);
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /**
+ * <b>SAX1</b>: reports a PI.
+ * This doesn't check for illegal target names, such as "xml" or "XML",
+ * or namespace-incompatible ones like "big:dog"; the caller is
+ * responsible for ensuring those names are legal.
+ */
+ final public void processingInstruction (String target, String data)
+ throws SAXException
+ {
+ if (locator == null)
+ locator = new LocatorImpl ();
+
+ // don't print internal subset for XHTML
+ if (xhtml && startedDoctype)
+ return;
+
+ // ancient HTML browsers might render these ... their loss.
+ // to prevent: "if (xhtml) return;".
+
+ try {
+ if (entityNestLevel != 0)
+ return;
+ if (canonical && inEpilogue)
+ newline ();
+ rawWrite ("<?");
+ rawWrite (target);
+ rawWrite (' ');
+ escapeChars (data.toCharArray (), -1, -1, CTX_UNPARSED);
+ rawWrite ("?>");
+ if (elementNestLevel == 0 && !(canonical && inEpilogue))
+ newline ();
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /** <b>SAX1</b>: indicates a non-expanded entity reference */
+ public void skippedEntity (String name)
+ throws SAXException
+ {
+ try {
+ rawWrite ("&");
+ rawWrite (name);
+ rawWrite (";");
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ // SAX2 LexicalHandler
+
+ /** <b>SAX2</b>: called before parsing CDATA characters */
+ final public void startCDATA ()
+ throws SAXException
+ {
+ if (locator == null)
+ locator = new LocatorImpl ();
+
+ if (canonical)
+ return;
+
+ try {
+ inCDATA = true;
+ if (entityNestLevel == 0)
+ rawWrite ("<![CDATA[");
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /** <b>SAX2</b>: called after parsing CDATA characters */
+ final public void endCDATA ()
+ throws SAXException
+ {
+ if (canonical)
+ return;
+
+ try {
+ inCDATA = false;
+ if (entityNestLevel == 0)
+ rawWrite ("]]>");
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /**
+ * <b>SAX2</b>: called when the doctype is partially parsed
+ * Note that this, like other doctype related calls, is ignored
+ * when XHTML is in use.
+ */
+ final public void startDTD (String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (locator == null)
+ locator = new LocatorImpl ();
+ if (xhtml)
+ return;
+ try {
+ inDoctype = startedDoctype = true;
+ if (canonical)
+ return;
+ rawWrite ("<!DOCTYPE ");
+ rawWrite (name);
+ rawWrite (' ');
+
+ if (!expandingEntities) {
+ if (publicId != null)
+ rawWrite ("PUBLIC '" + publicId + "' '" + systemId + "' ");
+ else if (systemId != null)
+ rawWrite ("SYSTEM '" + systemId + "' ");
+ }
+
+ rawWrite ('[');
+ newline ();
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /** <b>SAX2</b>: called after the doctype is parsed */
+ final public void endDTD ()
+ throws SAXException
+ {
+ inDoctype = false;
+ if (canonical || xhtml)
+ return;
+ try {
+ rawWrite ("]>");
+ newline ();
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /**
+ * <b>SAX2</b>: called before parsing a general entity in content
+ */
+ final public void startEntity (String name)
+ throws SAXException
+ {
+ try {
+ boolean writeEOL = true;
+
+ // Predefined XHTML entities (for characters) will get
+ // mapped back later.
+ if (xhtml || expandingEntities)
+ return;
+
+ entityNestLevel++;
+ if (name.equals ("[dtd]"))
+ return;
+ if (entityNestLevel != 1)
+ return;
+ if (!name.startsWith ("%")) {
+ writeEOL = false;
+ rawWrite ('&');
+ }
+ rawWrite (name);
+ rawWrite (';');
+ if (writeEOL)
+ newline ();
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /**
+ * <b>SAX2</b>: called after parsing a general entity in content
+ */
+ final public void endEntity (String name)
+ throws SAXException
+ {
+ if (xhtml || expandingEntities)
+ return;
+ entityNestLevel--;
+ }
+
+ /**
+ * <b>SAX2</b>: called when comments are parsed.
+ * When XHTML is used, the old HTML tradition of using comments
+ * to for inline CSS, or for JavaScript code is discouraged.
+ * This is because XML processors are encouraged to discard, on
+ * the grounds that comments are for users (and perhaps text
+ * editors) not programs. Instead, use external scripts
+ */
+ final public void comment (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (locator == null)
+ locator = new LocatorImpl ();
+
+ // don't print internal subset for XHTML
+ if (xhtml && startedDoctype)
+ return;
+ // don't print comment in doctype for canon xml
+ if (canonical && inDoctype)
+ return;
+
+ try {
+ boolean indent;
+
+ if (prettyPrinting && space.empty ())
+ fatal ("stack discipline", null);
+ indent = prettyPrinting && "default".equals (space.peek ());
+ if (entityNestLevel != 0)
+ return;
+ if (indent)
+ doIndent ();
+ if (canonical && inEpilogue)
+ newline ();
+ rawWrite ("<!--");
+ escapeChars (ch, start, length, CTX_UNPARSED);
+ rawWrite ("-->");
+ if (indent)
+ doIndent ();
+ if (elementNestLevel == 0 && !(canonical && inEpilogue))
+ newline ();
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ // SAX1 DTDHandler
+
+ /** <b>SAX1</b>: called on notation declarations */
+ final public void notationDecl (String name,
+ String publicId, String systemId)
+ throws SAXException
+ {
+ if (xhtml)
+ return;
+ try {
+ // At this time, only SAX2 callbacks start these.
+ if (!startedDoctype)
+ return;
+
+ if (entityNestLevel != 0)
+ return;
+ rawWrite ("<!NOTATION " + name + " ");
+ if (publicId != null)
+ rawWrite ("PUBLIC \"" + publicId + '"');
+ else
+ rawWrite ("SYSTEM ");
+ if (systemId != null)
+ rawWrite ('"' + systemId + '"');
+ rawWrite (">");
+ newline ();
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /** <b>SAX1</b>: called on unparsed entity declarations */
+ final public void unparsedEntityDecl (String name,
+ String publicId, String systemId,
+ String notationName)
+ throws SAXException
+ {
+ if (xhtml)
+ return;
+ try {
+ // At this time, only SAX2 callbacks start these.
+ if (!startedDoctype) {
+ // FIXME: write to temporary buffer, and make the start
+ // of the root element write these declarations.
+ return;
+ }
+
+ if (entityNestLevel != 0)
+ return;
+ rawWrite ("<!ENTITY " + name + " ");
+ if (publicId != null)
+ rawWrite ("PUBLIC \"" + publicId + '"');
+ else
+ rawWrite ("SYSTEM ");
+ rawWrite ('"' + systemId + '"');
+ rawWrite (" NDATA " + notationName + ">");
+ newline ();
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ // SAX2 DeclHandler
+
+ /** <b>SAX2</b>: called on attribute declarations */
+ final public void attributeDecl (String eName, String aName,
+ String type, String mode, String value)
+ throws SAXException
+ {
+ if (xhtml)
+ return;
+ try {
+ // At this time, only SAX2 callbacks start these.
+ if (!startedDoctype)
+ return;
+ if (entityNestLevel != 0)
+ return;
+ rawWrite ("<!ATTLIST " + eName + ' ' + aName + ' ');
+ rawWrite (type);
+ rawWrite (' ');
+ if (mode != null)
+ rawWrite (mode + ' ');
+ if (value != null)
+ writeQuotedValue (value, CTX_ATTRIBUTE);
+ rawWrite ('>');
+ newline ();
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /** <b>SAX2</b>: called on element declarations */
+ final public void elementDecl (String name, String model)
+ throws SAXException
+ {
+ if (xhtml)
+ return;
+ try {
+ // At this time, only SAX2 callbacks start these.
+ if (!startedDoctype)
+ return;
+ if (entityNestLevel != 0)
+ return;
+ rawWrite ("<!ELEMENT " + name + ' ' + model + '>');
+ newline ();
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /** <b>SAX2</b>: called on external entity declarations */
+ final public void externalEntityDecl (
+ String name,
+ String publicId,
+ String systemId)
+ throws SAXException
+ {
+ if (xhtml)
+ return;
+ try {
+ // At this time, only SAX2 callbacks start these.
+ if (!startedDoctype)
+ return;
+ if (entityNestLevel != 0)
+ return;
+ rawWrite ("<!ENTITY ");
+ if (name.startsWith ("%")) {
+ rawWrite ("% ");
+ rawWrite (name.substring (1));
+ } else
+ rawWrite (name);
+ if (publicId != null)
+ rawWrite (" PUBLIC \"" + publicId + '"');
+ else
+ rawWrite (" SYSTEM ");
+ rawWrite ('"' + systemId + "\">");
+ newline ();
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ /** <b>SAX2</b>: called on internal entity declarations */
+ final public void internalEntityDecl (String name, String value)
+ throws SAXException
+ {
+ if (xhtml)
+ return;
+ try {
+ // At this time, only SAX2 callbacks start these.
+ if (!startedDoctype)
+ return;
+ if (entityNestLevel != 0)
+ return;
+ rawWrite ("<!ENTITY ");
+ if (name.startsWith ("%")) {
+ rawWrite ("% ");
+ rawWrite (name.substring (1));
+ } else
+ rawWrite (name);
+ rawWrite (' ');
+ writeQuotedValue (value, CTX_ENTITY);
+ rawWrite ('>');
+ newline ();
+ } catch (IOException e) {
+ fatal ("can't write", e);
+ }
+ }
+
+ private void writeQuotedValue (String value, int code)
+ throws SAXException, IOException
+ {
+ char buf [] = value.toCharArray ();
+ int off = 0, len = buf.length;
+
+ // we can't add line breaks to attribute/entity/... values
+ noWrap = true;
+ rawWrite ('"');
+ escapeChars (buf, off, len, code);
+ rawWrite ('"');
+ noWrap = false;
+ }
+
+ // From "HTMLlat1x.ent" ... names of entities for ISO-8859-1
+ // (Latin/1) characters, all codes: 160-255 (0xA0-0xFF).
+ // Codes 128-159 have no assigned values.
+ private static final String HTMLlat1x [] = {
+ // 160
+ "nbsp", "iexcl", "cent", "pound", "curren",
+ "yen", "brvbar", "sect", "uml", "copy",
+
+ // 170
+ "ordf", "laquo", "not", "shy", "reg",
+ "macr", "deg", "plusmn", "sup2", "sup3",
+
+ // 180
+ "acute", "micro", "para", "middot", "cedil",
+ "sup1", "ordm", "raquo", "frac14", "frac12",
+
+ // 190
+ "frac34", "iquest", "Agrave", "Aacute", "Acirc",
+ "Atilde", "Auml", "Aring", "AElig", "Ccedil",
+
+ // 200
+ "Egrave", "Eacute", "Ecirc", "Euml", "Igrave",
+ "Iacute", "Icirc", "Iuml", "ETH", "Ntilde",
+
+ // 210
+ "Ograve", "Oacute", "Ocirc", "Otilde", "Ouml",
+ "times", "Oslash", "Ugrave", "Uacute", "Ucirc",
+
+ // 220
+ "Uuml", "Yacute", "THORN", "szlig", "agrave",
+ "aacute", "acirc", "atilde", "auml", "aring",
+
+ // 230
+ "aelig", "ccedil", "egrave", "eacute", "ecirc",
+ "euml", "igrave", "iacute", "icirc", "iuml",
+
+ // 240
+ "eth", "ntilde", "ograve", "oacute", "ocirc",
+ "otilde", "ouml", "divide", "oslash", "ugrave",
+
+ // 250
+ "uacute", "ucirc", "uuml", "yacute", "thorn",
+ "yuml"
+ };
+
+ // From "HTMLsymbolx.ent" ... some of the symbols that
+ // we can conveniently handle. Entities for the Greek.
+ // alphabet (upper and lower cases) are compact.
+ private static final String HTMLsymbolx_GR [] = {
+ // 913
+ "Alpha", "Beta", "Gamma", "Delta", "Epsilon",
+ "Zeta", "Eta", "Theta", "Iota", "Kappa",
+
+ // 923
+ "Lambda", "Mu", "Nu", "Xi", "Omicron",
+ "Pi", "Rho", null, "Sigma", "Tau",
+
+ // 933
+ "Upsilon", "Phi", "Chi", "Psi", "Omega"
+ };
+
+ private static final String HTMLsymbolx_gr [] = {
+ // 945
+ "alpha", "beta", "gamma", "delta", "epsilon",
+ "zeta", "eta", "theta", "iota", "kappa",
+
+ // 955
+ "lambda", "mu", "nu", "xi", "omicron",
+ "pi", "rho", "sigmaf", "sigma", "tau",
+
+ // 965
+ "upsilon", "phi", "chi", "psi", "omega"
+ };
+
+
+ // General routine to write text and substitute predefined
+ // entities (XML, and a special case for XHTML) as needed.
+ private void escapeChars (char buf [], int off, int len, int code)
+ throws SAXException, IOException
+ {
+ int first = 0;
+
+ if (off < 0) {
+ off = 0;
+ len = buf.length;
+ }
+ for (int i = 0; i < len; i++) {
+ String esc;
+ char c = buf [off + i];
+
+ switch (c) {
+ // Note that CTX_ATTRIBUTE isn't explicitly tested here;
+ // all syntax delimiters are escaped in CTX_ATTRIBUTE,
+ // otherwise it's similar to CTX_CONTENT
+
+ // ampersand flags entity references; entity replacement
+ // text has unexpanded references, other text doesn't.
+ case '&':
+ if (code == CTX_ENTITY || code == CTX_UNPARSED)
+ continue;
+ esc = "amp";
+ break;
+
+ // attributes and text may NOT have literal '<', but
+ // entities may have markup constructs
+ case '<':
+ if (code == CTX_ENTITY || code == CTX_UNPARSED)
+ continue;
+ esc = "lt";
+ break;
+
+ // as above re markup constructs; but otherwise
+ // except when canonicalizing, this is for consistency
+ case '>':
+ if (code == CTX_ENTITY || code == CTX_UNPARSED)
+ continue;
+ esc = "gt";
+ break;
+ case '\'':
+ if (code == CTX_CONTENT || code == CTX_UNPARSED)
+ continue;
+ if (canonical)
+ continue;
+ esc = "apos";
+ break;
+
+ // needed when printing quoted attribute/entity values
+ case '"':
+ if (code == CTX_CONTENT || code == CTX_UNPARSED)
+ continue;
+ esc = "quot";
+ break;
+
+ // make line ends work per host OS convention
+ case '\n':
+ esc = eol;
+ break;
+
+ //
+ // No other characters NEED special treatment ... except
+ // for encoding-specific issues, like whether the character
+ // can really be represented in that encoding.
+ //
+ default:
+ //
+ // There are characters we can never write safely; getting
+ // them is an error.
+ //
+ // (a) They're never legal in XML ... detected by range
+ // checks, and (eventually) by remerging surrogate
+ // pairs on output. (Easy error for apps to prevent.)
+ //
+ // (b) This encoding can't represent them, and we
+ // can't make reference substitution (e.g. inside
+ // CDATA sections, names, PI data, etc). (Hard for
+ // apps to prevent, except by using UTF-8 or UTF-16
+ // as their output encoding.)
+ //
+ // We know a very little bit about what characters
+ // the US-ASCII and ISO-8859-1 encodings support. For
+ // other encodings we can't detect the second type of
+ // error at all. (Never an issue for UTF-8 or UTF-16.)
+ //
+
+// FIXME: CR in CDATA is an error; in text, turn to a char ref
+
+// FIXME: CR/LF/TAB in attributes should become char refs
+
+ if ((c > 0xfffd)
+ || ((c < 0x0020) && !((c == 0x0009)
+ || (c == 0x000A) || (c == 0x000D)))
+ || (((c & dangerMask) != 0)
+ && (code == CTX_UNPARSED))) {
+
+ // if case (b) in CDATA, we might end the section,
+ // write a reference, then restart ... possible
+ // in one DOM L3 draft.
+
+ throw new CharConversionException (
+ "Illegal or non-writable character: U+"
+ + Integer.toHexString (c));
+ }
+
+ //
+ // If the output encoding represents the character
+ // directly, let it do so! Else we'll escape it.
+ //
+ if ((c & dangerMask) == 0)
+ continue;
+ esc = null;
+
+ // Avoid numeric refs where symbolic ones exist, as
+ // symbolic ones make more sense to humans reading!
+ if (xhtml) {
+ // all the HTMLlat1x.ent entities
+ // (all the "ISO-8859-1" characters)
+ if (c >= 160 && c <= 255)
+ esc = HTMLlat1x [c - 160];
+
+ // not quite half the HTMLsymbolx.ent entities
+ else if (c >= 913 && c <= 937)
+ esc = HTMLsymbolx_GR [c - 913];
+ else if (c >= 945 && c <= 969)
+ esc = HTMLsymbolx_gr [c - 945];
+
+ else switch (c) {
+ // all of the HTMLspecialx.ent entities
+ case 338: esc = "OElig"; break;
+ case 339: esc = "oelig"; break;
+ case 352: esc = "Scaron"; break;
+ case 353: esc = "scaron"; break;
+ case 376: esc = "Yuml"; break;
+ case 710: esc = "circ"; break;
+ case 732: esc = "tilde"; break;
+ case 8194: esc = "ensp"; break;
+ case 8195: esc = "emsp"; break;
+ case 8201: esc = "thinsp"; break;
+ case 8204: esc = "zwnj"; break;
+ case 8205: esc = "zwj"; break;
+ case 8206: esc = "lrm"; break;
+ case 8207: esc = "rlm"; break;
+ case 8211: esc = "ndash"; break;
+ case 8212: esc = "mdash"; break;
+ case 8216: esc = "lsquo"; break;
+ case 8217: esc = "rsquo"; break;
+ case 8218: esc = "sbquo"; break;
+ case 8220: esc = "ldquo"; break;
+ case 8221: esc = "rdquo"; break;
+ case 8222: esc = "bdquo"; break;
+ case 8224: esc = "dagger"; break;
+ case 8225: esc = "Dagger"; break;
+ case 8240: esc = "permil"; break;
+ case 8249: esc = "lsaquo"; break;
+ case 8250: esc = "rsaquo"; break;
+ case 8364: esc = "euro"; break;
+
+ // the other HTMLsymbox.ent entities
+ case 402: esc = "fnof"; break;
+ case 977: esc = "thetasym"; break;
+ case 978: esc = "upsih"; break;
+ case 982: esc = "piv"; break;
+ case 8226: esc = "bull"; break;
+ case 8230: esc = "hellip"; break;
+ case 8242: esc = "prime"; break;
+ case 8243: esc = "Prime"; break;
+ case 8254: esc = "oline"; break;
+ case 8260: esc = "frasl"; break;
+ case 8472: esc = "weierp"; break;
+ case 8465: esc = "image"; break;
+ case 8476: esc = "real"; break;
+ case 8482: esc = "trade"; break;
+ case 8501: esc = "alefsym"; break;
+ case 8592: esc = "larr"; break;
+ case 8593: esc = "uarr"; break;
+ case 8594: esc = "rarr"; break;
+ case 8595: esc = "darr"; break;
+ case 8596: esc = "harr"; break;
+ case 8629: esc = "crarr"; break;
+ case 8656: esc = "lArr"; break;
+ case 8657: esc = "uArr"; break;
+ case 8658: esc = "rArr"; break;
+ case 8659: esc = "dArr"; break;
+ case 8660: esc = "hArr"; break;
+ case 8704: esc = "forall"; break;
+ case 8706: esc = "part"; break;
+ case 8707: esc = "exist"; break;
+ case 8709: esc = "empty"; break;
+ case 8711: esc = "nabla"; break;
+ case 8712: esc = "isin"; break;
+ case 8713: esc = "notin"; break;
+ case 8715: esc = "ni"; break;
+ case 8719: esc = "prod"; break;
+ case 8721: esc = "sum"; break;
+ case 8722: esc = "minus"; break;
+ case 8727: esc = "lowast"; break;
+ case 8730: esc = "radic"; break;
+ case 8733: esc = "prop"; break;
+ case 8734: esc = "infin"; break;
+ case 8736: esc = "ang"; break;
+ case 8743: esc = "and"; break;
+ case 8744: esc = "or"; break;
+ case 8745: esc = "cap"; break;
+ case 8746: esc = "cup"; break;
+ case 8747: esc = "int"; break;
+ case 8756: esc = "there4"; break;
+ case 8764: esc = "sim"; break;
+ case 8773: esc = "cong"; break;
+ case 8776: esc = "asymp"; break;
+ case 8800: esc = "ne"; break;
+ case 8801: esc = "equiv"; break;
+ case 8804: esc = "le"; break;
+ case 8805: esc = "ge"; break;
+ case 8834: esc = "sub"; break;
+ case 8835: esc = "sup"; break;
+ case 8836: esc = "nsub"; break;
+ case 8838: esc = "sube"; break;
+ case 8839: esc = "supe"; break;
+ case 8853: esc = "oplus"; break;
+ case 8855: esc = "otimes"; break;
+ case 8869: esc = "perp"; break;
+ case 8901: esc = "sdot"; break;
+ case 8968: esc = "lceil"; break;
+ case 8969: esc = "rceil"; break;
+ case 8970: esc = "lfloor"; break;
+ case 8971: esc = "rfloor"; break;
+ case 9001: esc = "lang"; break;
+ case 9002: esc = "rang"; break;
+ case 9674: esc = "loz"; break;
+ case 9824: esc = "spades"; break;
+ case 9827: esc = "clubs"; break;
+ case 9829: esc = "hearts"; break;
+ case 9830: esc = "diams"; break;
+ }
+ }
+
+ // else escape with numeric char refs
+ if (esc == null) {
+ stringBuf.setLength (0);
+ stringBuf.append ("#x");
+ stringBuf.append (Integer.toHexString (c).toUpperCase ());
+ esc = stringBuf.toString ();
+
+ // FIXME: We don't write surrogate pairs correctly.
+ // They should work as one ref per character, since
+ // each pair is one character. For reading back into
+ // Unicode, it matters beginning in Unicode 3.1 ...
+ }
+ break;
+ }
+ if (i != first)
+ rawWrite (buf, off + first, i - first);
+ first = i + 1;
+ if (esc == eol)
+ newline ();
+ else {
+ rawWrite ('&');
+ rawWrite (esc);
+ rawWrite (';');
+ }
+ }
+ if (first < len)
+ rawWrite (buf, off + first, len - first);
+ }
+
+
+
+ private void newline ()
+ throws SAXException, IOException
+ {
+ out.write (eol);
+ column = 0;
+ }
+
+ private void doIndent ()
+ throws SAXException, IOException
+ {
+ int space = elementNestLevel * 2;
+
+ newline ();
+ column = space;
+ // track tabs only at line starts
+ while (space > 8) {
+ out.write ("\t");
+ space -= 8;
+ }
+ while (space > 0) {
+ out.write (" ");
+ space -= 2;
+ }
+ }
+
+ private void rawWrite (char c)
+ throws IOException
+ {
+ out.write (c);
+ column++;
+ }
+
+ private void rawWrite (String s)
+ throws SAXException, IOException
+ {
+ if (prettyPrinting && "default".equals (space.peek ())) {
+ char data [] = s.toCharArray ();
+ rawWrite (data, 0, data.length);
+ } else {
+ out.write (s);
+ column += s.length ();
+ }
+ }
+
+ // NOTE: if xhtml, the REC gives some rules about whitespace
+ // which we could follow ... notably, many places where conformant
+ // agents "must" consolidate/normalize whitespace. Line ends can
+ // be removed there, etc. This may not be the right place to do
+ // such mappings though.
+
+ // Line buffering may help clarify algorithms and improve results.
+
+ // It's likely xml:space needs more attention.
+
+ private void rawWrite (char buf [], int offset, int length)
+ throws SAXException, IOException
+ {
+ boolean wrap;
+
+ if (prettyPrinting && space.empty ())
+ fatal ("stack discipline", null);
+
+ wrap = prettyPrinting && "default".equals (space.peek ());
+ if (!wrap) {
+ out.write (buf, offset, length);
+ column += length;
+ return;
+ }
+
+ // we're pretty printing and want to fill lines out only
+ // to the desired line length.
+ while (length > 0) {
+ int target = lineLength - column;
+ boolean wrote = false;
+
+ // Do we even have a problem?
+ if (target > length || noWrap) {
+ out.write (buf, offset, length);
+ column += length;
+ return;
+ }
+
+ // break the line at a space character, trying to fill
+ // as much of the line as possible.
+ char c;
+
+ for (int i = target - 1; i >= 0; i--) {
+ if ((c = buf [offset + i]) == ' ' || c == '\t') {
+ i++;
+ out.write (buf, offset, i);
+ doIndent ();
+ offset += i;
+ length -= i;
+ wrote = true;
+ break;
+ }
+ }
+ if (wrote)
+ continue;
+
+ // no space character permitting break before target
+ // line length is filled. So, take the next one.
+ if (target < 0)
+ target = 0;
+ for (int i = target; i < length; i++)
+ if ((c = buf [offset + i]) == ' ' || c == '\t') {
+ i++;
+ out.write (buf, offset, i);
+ doIndent ();
+ offset += i;
+ length -= i;
+ wrote = true;
+ break;
+ }
+ if (wrote)
+ continue;
+
+ // no such luck.
+ out.write (buf, offset, length);
+ column += length;
+ break;
+ }
+ }
+}
diff --git a/libjava/classpath/gnu/xml/util/package.html b/libjava/classpath/gnu/xml/util/package.html
new file mode 100644
index 000000000..6e6c0d77d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/util/package.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html PUBLIC
+ "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/1999/PR-xhtml1-19991210/DTD/xhtml1-transitional.dtd">
+
+<html><head><title> org.brownell.xml package </title> </head>
+<!--
+/*
+ * Copyright (C) 1999,2000 The Free Software Foundation, Inc.
+ */
+-->
+<body>
+ <p> This package contains XML utilities, including SAX2 XML writers
+ and a parser of DOM trees, plus a command line driver.
+ That <a href="DoParse.html">driver</a>
+ connects parsers simple processing pipelines.
+ It can be handy for command line validation or
+ transformation tasks, possibly in batch mode,
+ or within Makefiles. </p>
+
+</body></html>
diff --git a/libjava/classpath/gnu/xml/validation/datatype/Annotation.java b/libjava/classpath/gnu/xml/validation/datatype/Annotation.java
new file mode 100644
index 000000000..6438e2ebb
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/Annotation.java
@@ -0,0 +1,60 @@
+/* Annotation.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+/**
+ * A schema component annotation.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class Annotation
+{
+
+ public final String documentation;
+
+ public Annotation(String documentation)
+ {
+ this.documentation = documentation;
+ }
+
+ public String toString()
+ {
+ return documentation;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/AnySimpleType.java b/libjava/classpath/gnu/xml/validation/datatype/AnySimpleType.java
new file mode 100644
index 000000000..36fb98daa
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/AnySimpleType.java
@@ -0,0 +1,58 @@
+/* AnySimpleType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.Set;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+
+final class AnySimpleType
+ extends SimpleType
+{
+
+ AnySimpleType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "anySimpleType"),
+ ANY, /* variety */
+ (Set) null, /* facets */
+ 0, /* fundametalFacets */
+ (SimpleType) Type.ANY_TYPE, /* baseType */
+ null);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/AnyType.java b/libjava/classpath/gnu/xml/validation/datatype/AnyType.java
new file mode 100644
index 000000000..2a3f116dd
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/AnyType.java
@@ -0,0 +1,57 @@
+/* AnyType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+
+final class AnyType
+ extends SimpleType
+{
+
+ AnyType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "anyType"),
+ ANY, /* variety */
+ null, /* facets */
+ 0, /* fundamentalFacets */
+ null, /* baseType */
+ null);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/AnyURIType.java b/libjava/classpath/gnu/xml/validation/datatype/AnyURIType.java
new file mode 100644
index 000000000..ef409b99c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/AnyURIType.java
@@ -0,0 +1,93 @@
+/* AnyURIType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema anyURI type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class AnyURIType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ AnyURIType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "anyURI"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ try
+ {
+ new URI(value);
+ }
+ catch (URISyntaxException e)
+ {
+ DatatypeException e2 = new DatatypeException(e.getIndex(),
+ e.getReason());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/AtomicSimpleType.java b/libjava/classpath/gnu/xml/validation/datatype/AtomicSimpleType.java
new file mode 100644
index 000000000..38528eecf
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/AtomicSimpleType.java
@@ -0,0 +1,77 @@
+/* AtomicSimpleType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.Set;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * An XML Schema atomic simple type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class AtomicSimpleType
+ extends SimpleType
+{
+
+ public AtomicSimpleType(QName name,
+ Set facets,
+ int fundamentalFacets,
+ SimpleType baseType,
+ Annotation annotation)
+ {
+ super(name, ATOMIC, facets, fundamentalFacets, baseType, annotation);
+ }
+
+ // Only for use by built-in types
+ AtomicSimpleType(QName name, SimpleType baseType)
+ {
+ super(name, ATOMIC, null, 0, baseType, null);
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ if (baseType != null)
+ baseType.checkValid(value, context);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/Base64BinaryType.java b/libjava/classpath/gnu/xml/validation/datatype/Base64BinaryType.java
new file mode 100644
index 000000000..3f3442f8c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/Base64BinaryType.java
@@ -0,0 +1,128 @@
+/* Base64BinaryType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema base64Binary type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class Base64BinaryType
+ extends AtomicSimpleType
+{
+
+ static final String B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
+ "abcdefghijklmnopqrstuvwxyz0123456789+/";
+ static final String B16 = "AEIMQUYcgkosw048";
+ static final String B04 = "AQgw";
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ Base64BinaryType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "base64Binary"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ // TODO value = collapseWhitespace(value);
+ int len = value.length();
+ try
+ {
+ for (int i = len - 1; i >= 0; )
+ {
+ char c4 = value.charAt(i--);
+ if (c4 == ' ')
+ c4 = value.charAt(i--);
+ char c3 = value.charAt(i--);
+ if (c3 == ' ')
+ c3 = value.charAt(i--);
+ char c2 = value.charAt(i--);
+ if (c2 == ' ')
+ c2 = value.charAt(i--);
+ char c1 = value.charAt(i--);
+ if (c1 == ' ')
+ c1 = value.charAt(i--);
+
+ if (c4 == '=')
+ {
+ if (c3 == '=')
+ {
+ if (B04.indexOf(c2) != -1 &&
+ B64.indexOf(c1) != -1)
+ continue;
+ }
+ else if (B16.indexOf(c3) != -1)
+ {
+ if (B64.indexOf(c2) != -1 &&
+ B64.indexOf(c1) != -1)
+ continue;
+ }
+ }
+ else if (B64.indexOf(c4) != -1)
+ continue;
+ throw new DatatypeException(i, "illegal BASE64");
+ }
+ }
+ catch (IndexOutOfBoundsException e)
+ {
+ throw new DatatypeException("illegal BASE64");
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/BooleanType.java b/libjava/classpath/gnu/xml/validation/datatype/BooleanType.java
new file mode 100644
index 000000000..5d98d2412
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/BooleanType.java
@@ -0,0 +1,89 @@
+/* BooleanType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.Arrays;
+import java.util.Set;
+import java.util.TreeSet;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema boolean type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class BooleanType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.PATTERN,
+ Facet.WHITESPACE
+ };
+
+ static final Set VALUE_SPACE =
+ new TreeSet(Arrays.asList(new String[] {"true", "false", "1", "0"}));
+
+ BooleanType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "boolean"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ if (!VALUE_SPACE.contains(value))
+ throw new DatatypeException("invalid boolean value");
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ return ("1".equals(literal) || "true".equals(literal)) ? Boolean.TRUE :
+ Boolean.FALSE;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/ByteType.java b/libjava/classpath/gnu/xml/validation/datatype/ByteType.java
new file mode 100644
index 000000000..9bce45bf9
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/ByteType.java
@@ -0,0 +1,132 @@
+/* ByteType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema byte type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class ByteType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.TOTAL_DIGITS,
+ Facet.FRACTION_DIGITS,
+ Facet.PATTERN,
+ Facet.WHITESPACE,
+ Facet.ENUMERATION,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ static final String MAX_VALUE = "127";
+ static final String MIN_VALUE = "128";
+ static final int LENGTH = MAX_VALUE.length();
+
+ ByteType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "byte"),
+ TypeLibrary.SHORT);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValue(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException(0, "invalid byte value");
+ int i = 0, off = 0;
+ boolean compare = false;
+ String compareTo = MAX_VALUE;
+ char c = value.charAt(0);
+ if (c == '+')
+ i++;
+ else if (c == '-')
+ {
+ compareTo = MIN_VALUE;
+ i++;
+ }
+ if (len - i > LENGTH)
+ throw new DatatypeException(0, "invalid byte value");
+ else if (len - i == LENGTH)
+ compare = true;
+ for (; i < len; i++)
+ {
+ c = value.charAt(i);
+ if (c >= 0x30 && c <= 0x39)
+ {
+ if (compare)
+ {
+ char d = compareTo.charAt(off);
+ if (Character.digit(c, 10) > Character.digit(d, 10))
+ throw new DatatypeException(i, "invalid byte value");
+ }
+ off++;
+ continue;
+ }
+ throw new DatatypeException(i, "invalid byte value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new Byte(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/DateTimeType.java b/libjava/classpath/gnu/xml/validation/datatype/DateTimeType.java
new file mode 100644
index 000000000..81d2785bc
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/DateTimeType.java
@@ -0,0 +1,334 @@
+/* DateTimeType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.TimeZone;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema dateTime type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class DateTimeType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ DateTimeType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "dateTime"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValue(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ int state = 0;
+ int start = 0;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == '-' && i == 0)
+ {
+ start++;
+ continue;
+ }
+ if (c >= 0x30 && c <= 0x39)
+ continue;
+ switch (state)
+ {
+ case 0: // year
+ if (c == '-')
+ {
+ String year = value.substring(start, i);
+ if ("0000".equals(year) || year.length() < 4)
+ throw new DatatypeException(i, "invalid dateTime value");
+ state = 1;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 1: // month
+ if (c == '-')
+ {
+ if (i - start != 2)
+ throw new DatatypeException(i, "invalid dateTime value");
+ state = 2;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 2: // day
+ if (c == 'T')
+ {
+ if (i - start != 2)
+ throw new DatatypeException(i, "invalid dateTime value");
+ state = 3;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 3: // hour
+ if (c == ':')
+ {
+ if (i - start != 2)
+ throw new DatatypeException(i, "invalid dateTime value");
+ state = 4;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 4: // minute
+ if (c == ':')
+ {
+ if (i - start != 2)
+ throw new DatatypeException(i, "invalid dateTime value");
+ state = 5;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 5: // second
+ if (c == '.')
+ {
+ if (i - start != 2)
+ throw new DatatypeException(i, "invalid dateTime value");
+ state = 6;
+ start = i + 1;
+ continue;
+ }
+ else if (c == ' ')
+ {
+ if (i - start != 2)
+ throw new DatatypeException(i, "invalid dateTime value");
+ state = 7;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 6: // second fraction
+ if (c == ' ')
+ {
+ state = 7;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 7: // timezone 1
+ if (start == i)
+ {
+ if (c == '+' || c == '-')
+ continue;
+ else if (c == 'Z')
+ {
+ state = 9;
+ start = i + 1;
+ continue;
+ }
+ }
+ if (c == ':')
+ {
+ if (i - start != 2)
+ throw new DatatypeException(i, "invalid dateTime value");
+ state = 8;
+ start = i + 1;
+ continue;
+ }
+ break;
+ }
+ throw new DatatypeException(i, "invalid dateTime value");
+ }
+ switch (state)
+ {
+ case 5: // second
+ if (len - start != 2)
+ throw new DatatypeException(len, "invalid dateTime value");
+ break;
+ case 6: // second fraction
+ break;
+ case 8: // timezone 2
+ if (len - start != 2)
+ throw new DatatypeException(len, "invalid dateTime value");
+ break;
+ case 9: // post Z
+ break;
+ default:
+ throw new DatatypeException(len, "invalid dateTime value");
+ }
+ }
+
+ public Object createValue(String value, ValidationContext context) {
+ int len = value.length();
+ int state = 0;
+ int start = 0;
+ Calendar cal = new GregorianCalendar();
+ try
+ {
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == '-' && i == 0)
+ {
+ start++;
+ continue;
+ }
+ if (c >= 0x30 && c <= 0x39)
+ continue;
+ switch (state)
+ {
+ case 0: // year
+ if (c == '-')
+ {
+ cal.set(Calendar.YEAR,
+ Integer.parseInt(value.substring(0, i)));
+ state = 1;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 1: // month
+ if (c == '-')
+ {
+ cal.set(Calendar.MONTH,
+ Integer.parseInt(value.substring(start, i)));
+ state = 2;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 2: // day
+ if (c == 'T')
+ {
+ cal.set(Calendar.DATE,
+ Integer.parseInt(value.substring(start, i)));
+ state = 3;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 3: // hour
+ if (c == ':')
+ {
+ cal.set(Calendar.HOUR,
+ Integer.parseInt(value.substring(start, i)));
+ state = 4;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 4: // minute
+ if (c == ':')
+ {
+ cal.set(Calendar.MINUTE,
+ Integer.parseInt(value.substring(start, i)));
+ state = 5;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 5: // second
+ if (c == ' ')
+ {
+ float second = Float.parseFloat(value.substring(start, i));
+ // TODO adjust non-integer values
+ cal.set(Calendar.SECOND, (int) second);
+ state = 7;
+ start = i + 1;
+ continue;
+ }
+ break;
+ }
+ }
+ // end of input
+ if (len - start > 0 && state == 7)
+ {
+ // Timezone
+ String timezone = value.substring(len - start);
+ int i = timezone.indexOf(':');
+ if (i == -1)
+ {
+ if ("Z".equals(timezone))
+ timezone = "UTC";
+ TimeZone tz = TimeZone.getTimeZone(timezone);
+ if (tz == null)
+ return null;
+ cal.set(Calendar.ZONE_OFFSET, tz.getRawOffset());
+ }
+ else
+ {
+ String tzh = timezone.substring(0, i);
+ String tzm = timezone.substring(i + 1);
+ int offset = Integer.parseInt(tzh) * 360000;
+ if (offset < 0)
+ offset -= Integer.parseInt(tzm) * 60000;
+ else
+ offset += Integer.parseInt(tzm) * 60000;
+ cal.set(Calendar.ZONE_OFFSET, offset);
+ }
+ }
+ return cal.getTime();
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/DateType.java b/libjava/classpath/gnu/xml/validation/datatype/DateType.java
new file mode 100644
index 000000000..8e6e3c35a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/DateType.java
@@ -0,0 +1,221 @@
+/* DateType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.TimeZone;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema date type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class DateType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ DateType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "date"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ int state = 0;
+ int start = 0;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == '-' && i == 0)
+ {
+ start++;
+ continue;
+ }
+ if (c >= 0x30 && c <= 0x39)
+ continue;
+ switch (state)
+ {
+ case 0: // year
+ if (c == '-')
+ {
+ String year = value.substring(start, i);
+ if ("0000".equals(year) || year.length() < 4)
+ throw new DatatypeException(i, "invalid date value");
+ state = 1;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 1: // month
+ if (c == '-')
+ {
+ if (i - start != 2)
+ throw new DatatypeException(i, "invalid date value");
+ state = 2;
+ start = i + 1;
+ continue;
+ }
+ break;
+ }
+ throw new DatatypeException(i, "invalid date value");
+ }
+ switch (state)
+ {
+ case 2: // day
+ if (len - start != 2)
+ throw new DatatypeException("invalid date value");
+ break;
+ default:
+ throw new DatatypeException("invalid date value");
+ }
+ }
+
+ public Object createValue(String value, ValidationContext context) {
+ int len = value.length();
+ int state = 0;
+ int start = 0;
+ Calendar cal = new GregorianCalendar();
+ cal.set(Calendar.HOUR, 0);
+ cal.set(Calendar.MINUTE, 0);
+ cal.set(Calendar.SECOND, 0);
+ try
+ {
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == '-' && i == 0)
+ {
+ start++;
+ continue;
+ }
+ if (c >= 0x30 && c <= 0x39)
+ continue;
+ switch (state)
+ {
+ case 0: // year
+ if (c == '-')
+ {
+ cal.set(Calendar.YEAR,
+ Integer.parseInt(value.substring(0, i)));
+ state = 1;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 1: // month
+ if (c == '-')
+ {
+ cal.set(Calendar.MONTH,
+ Integer.parseInt(value.substring(start, i)));
+ state = 2;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 2: // day
+ if (c == 'T')
+ {
+ cal.set(Calendar.DATE,
+ Integer.parseInt(value.substring(start, i)));
+ state = 7;
+ start = i + 1;
+ continue;
+ }
+ break;
+ }
+ }
+ // end of input
+ if (len - start > 0 && state == 7)
+ {
+ // Timezone
+ String timezone = value.substring(len - start);
+ int i = timezone.indexOf(':');
+ if (i == -1)
+ {
+ if ("Z".equals(timezone))
+ timezone = "UTC";
+ TimeZone tz = TimeZone.getTimeZone(timezone);
+ if (tz == null)
+ return null;
+ cal.set(Calendar.ZONE_OFFSET, tz.getRawOffset());
+ }
+ else
+ {
+ String tzh = timezone.substring(0, i);
+ String tzm = timezone.substring(i + 1);
+ int offset = Integer.parseInt(tzh) * 360000;
+ if (offset < 0)
+ offset -= Integer.parseInt(tzm) * 60000;
+ else
+ offset += Integer.parseInt(tzm) * 60000;
+ cal.set(Calendar.ZONE_OFFSET, offset);
+ }
+ }
+ return cal.getTime();
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/DecimalType.java b/libjava/classpath/gnu/xml/validation/datatype/DecimalType.java
new file mode 100644
index 000000000..43a26a399
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/DecimalType.java
@@ -0,0 +1,118 @@
+/* DecimalType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.math.BigDecimal;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema decimal type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class DecimalType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.TOTAL_DIGITS,
+ Facet.FRACTION_DIGITS,
+ Facet.PATTERN,
+ Facet.WHITESPACE,
+ Facet.ENUMERATION,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ DecimalType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "decimal"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException("invalid decimal value");
+ boolean seenDot = false;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c >= 0x30 && c <= 0x39)
+ continue;
+ else if (c == '.')
+ {
+ if (seenDot)
+ throw new DatatypeException(i, "invalid decimal value");
+ seenDot = true;
+ continue;
+ }
+ else if (c == '+' && i == 0)
+ continue;
+ else if (c == '-' && i == 0)
+ continue;
+ else
+ throw new DatatypeException(i, "invalid decimal value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new BigDecimal(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/DoubleType.java b/libjava/classpath/gnu/xml/validation/datatype/DoubleType.java
new file mode 100644
index 000000000..dfaa7c8cb
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/DoubleType.java
@@ -0,0 +1,110 @@
+/* DoubleType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.Arrays;
+import java.util.Set;
+import java.util.TreeSet;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema double type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class DoubleType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ static final Set SPECIAL =
+ new TreeSet(Arrays.asList(new String[] {"INF", "-INF", "NaN"}));
+
+ DoubleType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "double"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ if (SPECIAL.contains(value))
+ return;
+ try
+ {
+ Double.parseDouble(value);
+ }
+ catch (NumberFormatException e)
+ {
+ DatatypeException e2 = new DatatypeException("invalid double value");
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new Double(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/DurationType.java b/libjava/classpath/gnu/xml/validation/datatype/DurationType.java
new file mode 100644
index 000000000..6c0f7b889
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/DurationType.java
@@ -0,0 +1,238 @@
+/* DurationType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema duration type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class DurationType
+ extends AtomicSimpleType
+{
+
+ static class Duration
+ implements Comparable
+ {
+ int years;
+ int months;
+ int days;
+ int minutes;
+ float seconds;
+
+ public int hashCode()
+ {
+ int hc = years;
+ hc = hc * 31 + months;
+ hc = hc * 31 + days;
+ hc = hc * 31 + minutes;
+ hc = hc * 31 + new Float(seconds).hashCode();
+ return hc;
+ }
+
+ public boolean equals(Object other)
+ {
+ if (other instanceof Duration)
+ {
+ Duration duration = (Duration) other;
+ return duration.years ==years &&
+ duration.months == months &&
+ duration.days == days &&
+ duration.minutes == minutes &&
+ duration.seconds == seconds;
+ }
+ return false;
+ }
+
+ public int compareTo(Object other)
+ {
+ if (other instanceof Duration)
+ {
+ Duration duration = (Duration) other;
+ if (duration.years != years)
+ return years - duration.years;
+ if (duration.months != months)
+ return months - duration.months;
+ if (duration.days != days)
+ return days - duration.days;
+ if (duration.minutes != minutes)
+ return minutes = duration.minutes;
+ if (duration.seconds == seconds)
+ return 0;
+ return (seconds < duration.seconds) ? -1 : 1;
+ }
+ return 0;
+ }
+
+ }
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ DurationType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "duration"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ char expect = 'P';
+ boolean seenT = false;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == '-' && expect == 'P')
+ continue;
+ if (c == expect)
+ {
+ if (c == 'P')
+ expect = 'Y';
+ else if (c == 'Y')
+ expect = 'M';
+ else if (c == 'M' && !seenT)
+ expect = 'D';
+ else if (c == 'D')
+ expect = 'T';
+ else if (c == 'T')
+ {
+ expect = 'H';
+ seenT = true;
+ }
+ else if (c == 'H')
+ expect = 'M';
+ else if (c == 'M' && seenT)
+ expect = 'S';
+ else if (c == 'S')
+ {
+ if (i + 1 != len)
+ throw new DatatypeException(i, "illegal duration value");
+ }
+ continue;
+ }
+ if (c >= 0x30 && c <= 0x39 && expect != 'P' && expect != 'T')
+ continue;
+ throw new DatatypeException(i, "illegal duration value");
+ }
+ }
+
+ public Object createValue(String value, ValidationContext context) {
+ boolean negative = false;
+ int days = 0, months = 0, years = 0;
+ int minutes = 0;
+ float seconds = 0.0f;
+ int len = value.length();
+ char expect = 'P';
+ boolean seenT = false;
+ int start = 0;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == '-' && expect == 'P')
+ {
+ negative = true;
+ continue;
+ }
+ if (c == expect)
+ {
+ if (c == 'P')
+ expect = 'Y';
+ else if (c == 'Y')
+ {
+ expect = 'M';
+ years = Integer.parseInt(value.substring(start, i));
+ }
+ else if (c == 'M' && !seenT)
+ expect = 'D';
+ else if (c == 'D')
+ expect = 'T';
+ else if (c == 'T')
+ {
+ expect = 'H';
+ seenT = true;
+ }
+ else if (c == 'H')
+ expect = 'M';
+ else if (c == 'M' && seenT)
+ expect = 'S';
+ else if (c == 'S')
+ {
+ if (i + 1 != len)
+ return null;
+ }
+ start = i + 1;
+ continue;
+ }
+ if (c >= 0x30 && c <= 0x39 && expect != 'P' && expect != 'T')
+ continue;
+ return null;
+ }
+ if (negative)
+ {
+ days = days * -1;
+ minutes = minutes * -1;
+ seconds = seconds * -1.0f;
+ }
+ Duration duration = new Duration();
+ duration.days = days;
+ duration.minutes = minutes;
+ duration.seconds = seconds;
+ return duration;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/EntitiesType.java b/libjava/classpath/gnu/xml/validation/datatype/EntitiesType.java
new file mode 100644
index 000000000..5415e88e2
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/EntitiesType.java
@@ -0,0 +1,108 @@
+/* EntitiesType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import gnu.java.lang.CPStringBuilder;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema ENTITIES type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class EntitiesType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ EntitiesType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "ENTITIES"),
+ TypeLibrary.ENTITY);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ CPStringBuilder buf = new CPStringBuilder();
+ int len = value.length();
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == ' ')
+ {
+ String token = buf.toString();
+ if (token.length() > 0)
+ {
+ if (!context.isUnparsedEntity(token))
+ throw new DatatypeException(i, "invalid ENTITIES value");
+ }
+ buf.setLength(0);
+ }
+ else
+ buf.append(c);
+ }
+ String token = buf.toString();
+ if (token.length() == 0 || !context.isUnparsedEntity(token))
+ throw new DatatypeException("invalid ENTITIES value");
+ }
+
+ public boolean isContextDependent()
+ {
+ return true;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/EntityType.java b/libjava/classpath/gnu/xml/validation/datatype/EntityType.java
new file mode 100644
index 000000000..f4a10bd50
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/EntityType.java
@@ -0,0 +1,87 @@
+/* EntityType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema ENTITY type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class EntityType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ EntityType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "ENTITY"),
+ TypeLibrary.NCNAME);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ if (value.length() == 0 || !context.isUnparsedEntity(value))
+ throw new DatatypeException("invalid ENTITY value");
+ }
+
+ public boolean isContextDependent()
+ {
+ return true;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/EnumerationFacet.java b/libjava/classpath/gnu/xml/validation/datatype/EnumerationFacet.java
new file mode 100644
index 000000000..45b9c2378
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/EnumerationFacet.java
@@ -0,0 +1,68 @@
+/* EnumerationFacet.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+/**
+ * The <code>enumeration</code> facet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class EnumerationFacet
+ extends Facet
+{
+
+ public final String value;
+
+ public EnumerationFacet(String value, Annotation annotation)
+ {
+ super(ENUMERATION, annotation);
+ this.value = value;
+ }
+
+ public int hashCode()
+ {
+ return value.hashCode();
+ }
+
+ public boolean equals(Object other)
+ {
+ return (other instanceof EnumerationFacet &&
+ ((EnumerationFacet) other).value.equals(value));
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/Facet.java b/libjava/classpath/gnu/xml/validation/datatype/Facet.java
new file mode 100644
index 000000000..9f18bedc8
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/Facet.java
@@ -0,0 +1,77 @@
+/* Facet.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+/**
+ * An XML Schema constraining facet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public abstract class Facet
+{
+
+ public static final int LENGTH = 1;
+ public static final int MIN_LENGTH = 2;
+ public static final int MAX_LENGTH = 3;
+ public static final int PATTERN = 4;
+ public static final int ENUMERATION = 5;
+ public static final int WHITESPACE = 6;
+ public static final int MAX_INCLUSIVE = 7;
+ public static final int MAX_EXCLUSIVE = 8;
+ public static final int MIN_EXCLUSIVE = 9;
+ public static final int MIN_INCLUSIVE = 10;
+ public static final int TOTAL_DIGITS = 11;
+ public static final int FRACTION_DIGITS = 12;
+
+ /**
+ * The type of this facet.
+ */
+ public final int type;
+
+ /**
+ * Optional annotation.
+ */
+ public Annotation annotation;
+
+ protected Facet(int type, Annotation annotation)
+ {
+ this.type = type;
+ this.annotation = annotation;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/FloatType.java b/libjava/classpath/gnu/xml/validation/datatype/FloatType.java
new file mode 100644
index 000000000..8dbb4aa56
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/FloatType.java
@@ -0,0 +1,110 @@
+/* FloatType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.Arrays;
+import java.util.Set;
+import java.util.TreeSet;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema float type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class FloatType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ static final Set SPECIAL =
+ new TreeSet(Arrays.asList(new String[] {"INF", "-INF", "NaN"}));
+
+ FloatType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "float"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ if (SPECIAL.contains(value))
+ return;
+ try
+ {
+ Float.parseFloat(value);
+ }
+ catch (NumberFormatException e)
+ {
+ DatatypeException e2 = new DatatypeException("invalid float value");
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new Float(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/FractionDigitsFacet.java b/libjava/classpath/gnu/xml/validation/datatype/FractionDigitsFacet.java
new file mode 100644
index 000000000..c0d714578
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/FractionDigitsFacet.java
@@ -0,0 +1,71 @@
+/* FractionDigitsFacet.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+/**
+ * The <code>fractionDigits</code> facet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class FractionDigitsFacet
+ extends Facet
+{
+
+ public final int value;
+
+ public final boolean fixed;
+
+ public FractionDigitsFacet(int value, boolean fixed, Annotation annotation)
+ {
+ super(FRACTION_DIGITS, annotation);
+ this.value = value;
+ this.fixed = fixed;
+ }
+
+ public int hashCode()
+ {
+ return value;
+ }
+
+ public boolean equals(Object other)
+ {
+ return (other instanceof FractionDigitsFacet &&
+ ((FractionDigitsFacet) other).value == value);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/GDayType.java b/libjava/classpath/gnu/xml/validation/datatype/GDayType.java
new file mode 100644
index 000000000..cc6b8e3d4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/GDayType.java
@@ -0,0 +1,174 @@
+/* GDayType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema gDay type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class GDayType
+ extends AtomicSimpleType
+{
+
+ static class GDay
+ implements Comparable
+ {
+
+ int day;
+
+ public int hashCode()
+ {
+ return day;
+ }
+
+ public boolean equals(Object other)
+ {
+ if (other instanceof GDay)
+ return ((GDay) other).day == day;
+ return false;
+ }
+
+ public int compareTo(Object other)
+ {
+ if (other instanceof GDay)
+ {
+ GDay gd = (GDay) other;
+ if (gd.day == day)
+ return 0;
+ return (day < gd.day) ? -1 : 1;
+ }
+ return 0;
+ }
+
+ }
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ GDayType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "gDay"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ int state = 0;
+ int start = 0;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c >= 0x30 && c <= 0x39)
+ continue;
+ switch (state)
+ {
+ case 0: // year
+ if (c == '-')
+ {
+ switch (i)
+ {
+ case 0:
+ continue;
+ case 1:
+ state = 1;
+ start = i + 1;
+ continue;
+ default:
+ throw new DatatypeException(i, "invalid GDay value");
+ }
+ }
+ break;
+ case 1: // month
+ if (c == '-')
+ {
+ if (i - start != 0)
+ throw new DatatypeException(i, "invalid GDay value");
+ state = 2;
+ start = i + 1;
+ continue;
+ }
+ break;
+ }
+ throw new DatatypeException(i, "invalid GDay value");
+ }
+ switch (state)
+ {
+ case 2: // day
+ if (len - start != 2)
+ throw new DatatypeException("invalid GDay value");
+ break;
+ default:
+ throw new DatatypeException("invalid GDay value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ GDay ret = new GDay();
+ ret.day = Integer.parseInt(literal.substring(3));
+ return ret;
+ }
+ catch (Exception e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/GMonthDayType.java b/libjava/classpath/gnu/xml/validation/datatype/GMonthDayType.java
new file mode 100644
index 000000000..d293d9b57
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/GMonthDayType.java
@@ -0,0 +1,183 @@
+/* GMonthDayType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema gMonthDay type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class GMonthDayType
+ extends AtomicSimpleType
+{
+
+ static class GMonthDay
+ implements Comparable
+ {
+
+ int month;
+ int day;
+
+ public int hashCode()
+ {
+ return month * 31 + day;
+ }
+
+ public boolean equals(Object other)
+ {
+ if (other instanceof GMonthDay)
+ {
+ GMonthDay gmd = (GMonthDay) other;
+ return gmd.month == month && gmd.day == day;
+ }
+ return false;
+ }
+
+ public int compareTo(Object other)
+ {
+ if (other instanceof GMonthDay)
+ {
+ GMonthDay gmd = (GMonthDay) other;
+ if (gmd.month == month)
+ {
+ if (gmd.day == day)
+ return 0;
+ return (day < gmd.day) ? -1 : 1;
+ }
+ return (month < gmd.month) ? -1 : 1;
+ }
+ return 0;
+ }
+
+ }
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ GMonthDayType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "gMonthDay"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ int state = 0;
+ int start = 0;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c >= 0x30 && c <= 0x39)
+ continue;
+ switch (state)
+ {
+ case 0: // year
+ if (c == '-')
+ {
+ switch (i)
+ {
+ case 0:
+ continue;
+ case 1:
+ state = 1;
+ start = i + 1;
+ continue;
+ default:
+ throw new DatatypeException(i, "illegal GMonthDay type");
+ }
+ }
+ break;
+ case 1: // month
+ if (c == '-')
+ {
+ if (i - start != 2)
+ throw new DatatypeException(i, "illegal GMonthDay type");
+ state = 2;
+ start = i + 1;
+ continue;
+ }
+ break;
+ }
+ throw new DatatypeException(i, "illegal GMonthDay type");
+ }
+ switch (state)
+ {
+ case 2: // day
+ if (len - start != 2)
+ throw new DatatypeException("illegal GMonthDay type");
+ break;
+ default:
+ throw new DatatypeException("illegal GMonthDay type");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ GMonthDay ret = new GMonthDay();
+ ret.month = Integer.parseInt(literal.substring(2, 5));
+ ret.day = Integer.parseInt(literal.substring(6));
+ return ret;
+ }
+ catch (Exception e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/GMonthType.java b/libjava/classpath/gnu/xml/validation/datatype/GMonthType.java
new file mode 100644
index 000000000..9450874b9
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/GMonthType.java
@@ -0,0 +1,163 @@
+/* GMonthType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema gMonth type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class GMonthType
+ extends AtomicSimpleType
+{
+
+ static class GMonth
+ implements Comparable
+ {
+
+ int month;
+
+ public int hashCode()
+ {
+ return month;
+ }
+
+ public boolean equals(Object other)
+ {
+ if (other instanceof GMonth)
+ return ((GMonth) other).month == month;
+ return false;
+ }
+
+ public int compareTo(Object other)
+ {
+ if (other instanceof GMonth)
+ {
+ GMonth gm = (GMonth) other;
+ if (gm.month == month)
+ return 0;
+ return (month < gm.month) ? -1 : 1;
+ }
+ return 0;
+ }
+
+ }
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ GMonthType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "gMonth"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ int len = value.length();
+ int state = 0;
+ int start = 0;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c >= 0x30 && c <= 0x39)
+ continue;
+ switch (state)
+ {
+ case 0: // year
+ if (c == '-')
+ {
+ switch (i)
+ {
+ case 0:
+ continue;
+ case 1:
+ state = 1;
+ start = i + 1;
+ continue;
+ default:
+ throw new DatatypeException(i, "illegal GMonth value");
+ }
+ }
+ break;
+ }
+ throw new DatatypeException(i, "illegal GMonth value");
+ }
+ switch (state)
+ {
+ case 1: // month
+ if (len - start != 2)
+ throw new DatatypeException("illegal GMonth value");
+ break;
+ default:
+ throw new DatatypeException("illegal GMonth value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ GMonth ret = new GMonth();
+ ret.month = Integer.parseInt(literal.substring(2));
+ return ret;
+ }
+ catch (Exception e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/GYearMonthType.java b/libjava/classpath/gnu/xml/validation/datatype/GYearMonthType.java
new file mode 100644
index 000000000..9aabfb61c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/GYearMonthType.java
@@ -0,0 +1,176 @@
+/* GYearMonthType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema gYearMonth type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class GYearMonthType
+ extends AtomicSimpleType
+{
+
+ static class GYearMonth
+ implements Comparable
+ {
+
+ int year;
+ int month;
+
+ public int hashCode()
+ {
+ return year * 31 + month;
+ }
+
+ public boolean equals(Object other)
+ {
+ if (other instanceof GYearMonth)
+ {
+ GYearMonth gmy = (GYearMonth) other;
+ return gmy.year == year && gmy.month == month;
+ }
+ return false;
+ }
+
+ public int compareTo(Object other)
+ {
+ if (other instanceof GYearMonth)
+ {
+ GYearMonth gmy = (GYearMonth) other;
+ if (gmy.year == year)
+ {
+ if (gmy.month == month)
+ return 0;
+ return (month < gmy.month) ? -1 : 1;
+ }
+ return (year < gmy.year) ? -1 : 1;
+ }
+ return 0;
+ }
+
+ }
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ GYearMonthType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "gYearMonth"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ int state = 0;
+ int start = 0;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == '-' && i == 0)
+ {
+ start++;
+ continue;
+ }
+ if (c >= 0x30 && c <= 0x39)
+ continue;
+ switch (state)
+ {
+ case 0: // year
+ if (c == '-')
+ {
+ String year = value.substring(start, i);
+ if (year.length() < 4 || Integer.parseInt(year) == 0)
+ throw new DatatypeException(i, "illegal GYear value");
+ state = 1;
+ start = i + 1;
+ continue;
+ }
+ break;
+ }
+ throw new DatatypeException(i, "illegal GYear value");
+ }
+ switch (state)
+ {
+ case 1: // month
+ if (len - start != 2)
+ throw new DatatypeException("illegal GYear value");
+ break;
+ default:
+ throw new DatatypeException("illegal GYear value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ int offset = 5;
+ if (literal.charAt(0) == '-')
+ offset++;
+ GYearMonth ret = new GYearMonth();
+ ret.year = Integer.parseInt(literal.substring(0, offset));
+ ret.month = Integer.parseInt(literal.substring(offset + 1));
+ return ret;
+ }
+ catch (Exception e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/GYearType.java b/libjava/classpath/gnu/xml/validation/datatype/GYearType.java
new file mode 100644
index 000000000..5b1e03232
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/GYearType.java
@@ -0,0 +1,151 @@
+/* GYearType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema gYear type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class GYearType
+ extends AtomicSimpleType
+{
+
+ static class GYear
+ implements Comparable
+ {
+
+ int year;
+
+ public int hashCode()
+ {
+ return year;
+ }
+
+ public boolean equals(Object other)
+ {
+ if (other instanceof GYear)
+ return ((GYear) other).year == year;
+ return false;
+ }
+
+ public int compareTo(Object other)
+ {
+ if (other instanceof GYear)
+ {
+ GYear gy = (GYear) other;
+ if (gy.year == year)
+ return 0;
+ return (year < gy.year) ? -1 : 1;
+ }
+ return 0;
+ }
+
+ }
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ GYearType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "gYear"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ int state = 0;
+ int start = 0;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == '-' && i == 0)
+ {
+ start++;
+ continue;
+ }
+ if (c >= 0x30 && c <= 0x39)
+ continue;
+ throw new DatatypeException(i, "invalid GYear value");
+ }
+ switch (state)
+ {
+ case 0: // year
+ String year = value.substring(start, len);
+ if (year.length() < 4 || Integer.parseInt(year) == 0)
+ throw new DatatypeException("invalid GYear value");
+ break;
+ default:
+ throw new DatatypeException("invalid GYear value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ GYear ret = new GYear();
+ ret.year = Integer.parseInt(literal);
+ return ret;
+ }
+ catch (Exception e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/HexBinaryType.java b/libjava/classpath/gnu/xml/validation/datatype/HexBinaryType.java
new file mode 100644
index 000000000..5f133e322
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/HexBinaryType.java
@@ -0,0 +1,89 @@
+/* HexBinaryType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema hexBinary type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class HexBinaryType
+ extends AtomicSimpleType
+{
+
+ static final String HEX = "0123456789ABCDEF";
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ HexBinaryType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "hexBinary"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (HEX.indexOf(c) == -1)
+ throw new DatatypeException(i, "invalid hexBinary value");
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/IDRefType.java b/libjava/classpath/gnu/xml/validation/datatype/IDRefType.java
new file mode 100644
index 000000000..932089549
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/IDRefType.java
@@ -0,0 +1,86 @@
+/* IDRefType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema IDREF type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class IDRefType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ IDRefType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "IDREF"),
+ TypeLibrary.NCNAME);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ // TODO
+ }
+
+ public int getIdType()
+ {
+ return ID_TYPE_IDREF;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/IDRefsType.java b/libjava/classpath/gnu/xml/validation/datatype/IDRefsType.java
new file mode 100644
index 000000000..6adaed13b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/IDRefsType.java
@@ -0,0 +1,86 @@
+/* IDRefsType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema IDREFS type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class IDRefsType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ IDRefsType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "IDREFS"),
+ TypeLibrary.IDREF);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ // TODO
+ }
+
+ public int getIdType()
+ {
+ return ID_TYPE_IDREFS;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/IDType.java b/libjava/classpath/gnu/xml/validation/datatype/IDType.java
new file mode 100644
index 000000000..792601d78
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/IDType.java
@@ -0,0 +1,86 @@
+/* IDType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema ID type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class IDType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ IDType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "ID"),
+ TypeLibrary.NCNAME);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ // TODO
+ }
+
+ public int getIdType()
+ {
+ return ID_TYPE_ID;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/IntType.java b/libjava/classpath/gnu/xml/validation/datatype/IntType.java
new file mode 100644
index 000000000..c8f756832
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/IntType.java
@@ -0,0 +1,132 @@
+/* IntType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema int type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class IntType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.TOTAL_DIGITS,
+ Facet.FRACTION_DIGITS,
+ Facet.PATTERN,
+ Facet.WHITESPACE,
+ Facet.ENUMERATION,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ static final String MAX_VALUE = "2147483647";
+ static final String MIN_VALUE = "2147483648";
+ static final int LENGTH = MAX_VALUE.length();
+
+ IntType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "int"),
+ TypeLibrary.LONG);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException(0, "invalid int value");
+ int i = 0, off = 0;
+ boolean compare = false;
+ String compareTo = MAX_VALUE;
+ char c = value.charAt(0);
+ if (c == '+')
+ i++;
+ else if (c == '-')
+ {
+ compareTo = MIN_VALUE;
+ i++;
+ }
+ if (len - i > LENGTH)
+ throw new DatatypeException("invalid int value");
+ else if (len - i == LENGTH)
+ compare = true;
+ for (; i < len; i++)
+ {
+ c = value.charAt(i);
+ if (c >= 0x30 && c <= 0x39)
+ {
+ if (compare)
+ {
+ char d = compareTo.charAt(off);
+ if (Character.digit(c, 10) > Character.digit(d, 10))
+ throw new DatatypeException(i, "invalid int value");
+ }
+ off++;
+ continue;
+ }
+ throw new DatatypeException(i, "invalid int value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new Integer(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/IntegerType.java b/libjava/classpath/gnu/xml/validation/datatype/IntegerType.java
new file mode 100644
index 000000000..b9a2a564a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/IntegerType.java
@@ -0,0 +1,109 @@
+/* IntegerType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.math.BigInteger;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema integer type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class IntegerType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.TOTAL_DIGITS,
+ Facet.FRACTION_DIGITS,
+ Facet.PATTERN,
+ Facet.WHITESPACE,
+ Facet.ENUMERATION,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ IntegerType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "integer"),
+ TypeLibrary.DECIMAL);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException(0, "invalid integer value");
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c >= 0x30 && c <= 0x39)
+ continue;
+ else if (c == '+' && i == 0)
+ continue;
+ else if (c == '-' && i == 0)
+ continue;
+ throw new DatatypeException(i, "invalid integer value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new BigInteger(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/LanguageType.java b/libjava/classpath/gnu/xml/validation/datatype/LanguageType.java
new file mode 100644
index 000000000..d30cc538f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/LanguageType.java
@@ -0,0 +1,86 @@
+/* LanguageType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.regex.Pattern;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema language type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class LanguageType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ static final Pattern PATTERN =
+ Pattern.compile("[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*");
+
+ LanguageType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "language"),
+ TypeLibrary.TOKEN);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ if (!PATTERN.matcher(value).matches())
+ throw new DatatypeException("invalid language value");
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/LengthFacet.java b/libjava/classpath/gnu/xml/validation/datatype/LengthFacet.java
new file mode 100644
index 000000000..d6eaacd71
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/LengthFacet.java
@@ -0,0 +1,71 @@
+/* LengthFacet.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+/**
+ * The <code>length</code> facet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class LengthFacet
+ extends Facet
+{
+
+ public final int value;
+
+ public final boolean fixed;
+
+ public LengthFacet(int value, boolean fixed, Annotation annotation)
+ {
+ super(LENGTH, annotation);
+ this.value = value;
+ this.fixed = fixed;
+ }
+
+ public int hashCode()
+ {
+ return value;
+ }
+
+ public boolean equals(Object other)
+ {
+ return (other instanceof LengthFacet &&
+ ((LengthFacet) other).value == value);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/ListSimpleType.java b/libjava/classpath/gnu/xml/validation/datatype/ListSimpleType.java
new file mode 100644
index 000000000..1e0d41d01
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/ListSimpleType.java
@@ -0,0 +1,82 @@
+/* ListSimpleType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.Set;
+import java.util.StringTokenizer;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * An XML Schema list simple type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class ListSimpleType
+ extends SimpleType
+{
+
+ /**
+ * The type of the items in this list (atomic or union).
+ */
+ public final SimpleType itemType;
+
+ public ListSimpleType(QName name, Set facets,
+ int fundamentalFacets, SimpleType baseType,
+ Annotation annotation, SimpleType itemType)
+ {
+ super(name, LIST, facets, fundamentalFacets, baseType, annotation);
+ this.itemType = itemType;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ StringTokenizer st = new StringTokenizer(value, " ");
+ if (!st.hasMoreTokens())
+ throw new DatatypeException("invalid list value");
+ while (st.hasMoreTokens())
+ {
+ String token = st.nextToken();
+ itemType.checkValid(token, context);
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/LongType.java b/libjava/classpath/gnu/xml/validation/datatype/LongType.java
new file mode 100644
index 000000000..781b17f62
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/LongType.java
@@ -0,0 +1,132 @@
+/* LongType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema long type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class LongType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.TOTAL_DIGITS,
+ Facet.FRACTION_DIGITS,
+ Facet.PATTERN,
+ Facet.WHITESPACE,
+ Facet.ENUMERATION,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ static final String MAX_VALUE = "9223372036854775807";
+ static final String MIN_VALUE = "9223372036854775808";
+ static final int LENGTH = MAX_VALUE.length();
+
+ LongType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "long"),
+ TypeLibrary.INTEGER);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException(0, "invalid long value");
+ int i = 0, off = 0;
+ boolean compare = false;
+ String compareTo = MAX_VALUE;
+ char c = value.charAt(0);
+ if (c == '+')
+ i++;
+ else if (c == '-')
+ {
+ compareTo = MIN_VALUE;
+ i++;
+ }
+ if (len - i > LENGTH)
+ throw new DatatypeException(i, "invalid long value");
+ else if (len - i == LENGTH)
+ compare = true;
+ for (; i < len; i++)
+ {
+ c = value.charAt(i);
+ if (c >= 0x30 && c <= 0x39)
+ {
+ if (compare)
+ {
+ char d = compareTo.charAt(off);
+ if (Character.digit(c, 10) > Character.digit(d, 10))
+ throw new DatatypeException(i, "invalid long value");
+ }
+ off++;
+ continue;
+ }
+ throw new DatatypeException(i, "invalid long value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new Long(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/MaxExclusiveFacet.java b/libjava/classpath/gnu/xml/validation/datatype/MaxExclusiveFacet.java
new file mode 100644
index 000000000..90e940371
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/MaxExclusiveFacet.java
@@ -0,0 +1,110 @@
+/* MaxExclusiveFacet.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Date;
+
+/**
+ * The <code>maxExclusive</code> facet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class MaxExclusiveFacet
+ extends Facet
+{
+
+ public final Object value; // date or number
+
+ public final boolean fixed;
+
+ public MaxExclusiveFacet(Object value, boolean fixed, Annotation annotation)
+ {
+ super(MAX_EXCLUSIVE, annotation);
+ this.value = value;
+ this.fixed = fixed;
+ }
+
+ public int hashCode()
+ {
+ return value.hashCode();
+ }
+
+ public boolean equals(Object other)
+ {
+ return (other instanceof MaxExclusiveFacet &&
+ ((MaxExclusiveFacet) other).value.equals(value));
+ }
+
+ boolean matches(Object test)
+ {
+ if (value instanceof Date)
+ {
+ Date dvalue = (Date) value;
+ if (!(test instanceof Date))
+ return false;
+ return ((Date) test).before(dvalue);
+ }
+ else if (value instanceof BigInteger)
+ {
+ BigInteger ivalue = (BigInteger) value;
+ if (!(test instanceof BigInteger))
+ return false;
+ return ((BigInteger) test).compareTo(ivalue) < 0;
+ }
+ else if (value instanceof BigDecimal)
+ {
+ BigDecimal dvalue = (BigDecimal) value;
+ if (!(test instanceof BigDecimal))
+ return false;
+ return ((BigDecimal) test).compareTo(dvalue) < 0;
+ }
+ else if (value instanceof Comparable)
+ {
+ if (!(test.getClass().equals(value.getClass())))
+ return false;
+ return ((Comparable) test).compareTo(value) < 0;
+ }
+ Number nvalue = (Number) value;
+ if (!(test instanceof Number))
+ return false;
+ return ((Number) test).doubleValue() < nvalue.doubleValue();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/MaxInclusiveFacet.java b/libjava/classpath/gnu/xml/validation/datatype/MaxInclusiveFacet.java
new file mode 100644
index 000000000..c260113da
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/MaxInclusiveFacet.java
@@ -0,0 +1,111 @@
+/* MaxInclusiveFacet.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Date;
+
+/**
+ * The <code>maxInclusive</code> facet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class MaxInclusiveFacet
+ extends Facet
+{
+
+ public final Object value;
+
+ public final boolean fixed;
+
+ public MaxInclusiveFacet(Object value, boolean fixed, Annotation annotation)
+ {
+ super(MAX_INCLUSIVE, annotation);
+ this.value = value;
+ this.fixed = fixed;
+ }
+
+ public int hashCode()
+ {
+ return value.hashCode();
+ }
+
+ public boolean equals(Object other)
+ {
+ return (other instanceof MaxInclusiveFacet &&
+ ((MaxInclusiveFacet) other).value.equals(value));
+ }
+
+ boolean matches(Object test)
+ {
+ if (value instanceof Date)
+ {
+ Date dvalue = (Date) value;
+ if (!(test instanceof Date))
+ return false;
+ Date dtest = (Date) test;
+ return dtest.equals(dvalue) || dtest.before(dvalue);
+ }
+ else if (value instanceof BigInteger)
+ {
+ BigInteger ivalue = (BigInteger) value;
+ if (!(test instanceof BigInteger))
+ return false;
+ return ((BigInteger) test).compareTo(ivalue) <= 0;
+ }
+ else if (value instanceof BigDecimal)
+ {
+ BigDecimal dvalue = (BigDecimal) value;
+ if (!(test instanceof BigDecimal))
+ return false;
+ return ((BigDecimal) test).compareTo(dvalue) <= 0;
+ }
+ else if (value instanceof Comparable)
+ {
+ if (!(test.getClass().equals(value.getClass())))
+ return false;
+ return ((Comparable) test).compareTo(value) <= 0;
+ }
+ Number nvalue = (Number) value;
+ if (!(test instanceof Number))
+ return false;
+ return ((Number) test).doubleValue() <= nvalue.doubleValue();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/MaxLengthFacet.java b/libjava/classpath/gnu/xml/validation/datatype/MaxLengthFacet.java
new file mode 100644
index 000000000..ba77ff2df
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/MaxLengthFacet.java
@@ -0,0 +1,71 @@
+/* MaxLengthFacet.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+/**
+ * The <code>maxLength</code> facet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class MaxLengthFacet
+ extends Facet
+{
+
+ public final int value;
+
+ public final boolean fixed;
+
+ public MaxLengthFacet(int value, boolean fixed, Annotation annotation)
+ {
+ super(MAX_LENGTH, annotation);
+ this.value = value;
+ this.fixed = fixed;
+ }
+
+ public int hashCode()
+ {
+ return value;
+ }
+
+ public boolean equals(Object other)
+ {
+ return (other instanceof MaxLengthFacet &&
+ ((MaxLengthFacet) other).value == value);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/MinExclusiveFacet.java b/libjava/classpath/gnu/xml/validation/datatype/MinExclusiveFacet.java
new file mode 100644
index 000000000..a779d09d8
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/MinExclusiveFacet.java
@@ -0,0 +1,110 @@
+/* MinExclusiveFacet.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Date;
+
+/**
+ * The <code>minExclusive</code> facet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class MinExclusiveFacet
+ extends Facet
+{
+
+ public final Object value;
+
+ public final boolean fixed;
+
+ public MinExclusiveFacet(Object value, boolean fixed, Annotation annotation)
+ {
+ super(MIN_EXCLUSIVE, annotation);
+ this.value = value;
+ this.fixed = fixed;
+ }
+
+ public int hashCode()
+ {
+ return value.hashCode();
+ }
+
+ public boolean equals(Object other)
+ {
+ return (other instanceof MinExclusiveFacet &&
+ ((MinExclusiveFacet) other).value.equals(value));
+ }
+
+ boolean matches(Object test)
+ {
+ if (value instanceof Date)
+ {
+ Date dvalue = (Date) value;
+ if (!(test instanceof Date))
+ return false;
+ return ((Date) test).after(dvalue);
+ }
+ else if (value instanceof BigInteger)
+ {
+ BigInteger ivalue = (BigInteger) value;
+ if (!(test instanceof BigInteger))
+ return false;
+ return ((BigInteger) test).compareTo(ivalue) > 0;
+ }
+ else if (value instanceof BigDecimal)
+ {
+ BigDecimal dvalue = (BigDecimal) value;
+ if (!(test instanceof BigDecimal))
+ return false;
+ return ((BigDecimal) test).compareTo(dvalue) > 0;
+ }
+ else if (value instanceof Comparable)
+ {
+ if (!(test.getClass().equals(value.getClass())))
+ return false;
+ return ((Comparable) test).compareTo(value) > 0;
+ }
+ Number nvalue = (Number) value;
+ if (!(test instanceof Number))
+ return false;
+ return ((Number) test).doubleValue() > nvalue.doubleValue();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/MinInclusiveFacet.java b/libjava/classpath/gnu/xml/validation/datatype/MinInclusiveFacet.java
new file mode 100644
index 000000000..6138c9af8
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/MinInclusiveFacet.java
@@ -0,0 +1,111 @@
+/* MinInclusiveFacet.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Date;
+
+/**
+ * The <code>minInclusive</code> facet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class MinInclusiveFacet
+ extends Facet
+{
+
+ public final Object value;
+
+ public final boolean fixed;
+
+ public MinInclusiveFacet(Object value, boolean fixed, Annotation annotation)
+ {
+ super(MIN_INCLUSIVE, annotation);
+ this.value = value;
+ this.fixed = fixed;
+ }
+
+ public int hashCode()
+ {
+ return value.hashCode();
+ }
+
+ public boolean equals(Object other)
+ {
+ return (other instanceof MinInclusiveFacet &&
+ ((MinInclusiveFacet) other).value.equals(value));
+ }
+
+ boolean matches(Object test)
+ {
+ if (value instanceof Date)
+ {
+ Date dvalue = (Date) value;
+ if (!(test instanceof Date))
+ return false;
+ Date dtest = (Date) test;
+ return dtest.equals(dvalue) || dtest.after(dvalue);
+ }
+ else if (value instanceof BigInteger)
+ {
+ BigInteger ivalue = (BigInteger) value;
+ if (!(test instanceof BigInteger))
+ return false;
+ return ((BigInteger) test).compareTo(ivalue) >= 0;
+ }
+ else if (value instanceof BigDecimal)
+ {
+ BigDecimal dvalue = (BigDecimal) value;
+ if (!(test instanceof BigDecimal))
+ return false;
+ return ((BigDecimal) test).compareTo(dvalue) >= 0;
+ }
+ else if (value instanceof Comparable)
+ {
+ if (!(test.getClass().equals(value.getClass())))
+ return false;
+ return ((Comparable) test).compareTo(value) >= 0;
+ }
+ Number nvalue = (Number) value;
+ if (!(test instanceof Number))
+ return false;
+ return ((Number) test).doubleValue() >= nvalue.doubleValue();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/MinLengthFacet.java b/libjava/classpath/gnu/xml/validation/datatype/MinLengthFacet.java
new file mode 100644
index 000000000..f13e80b62
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/MinLengthFacet.java
@@ -0,0 +1,71 @@
+/* MinLengthFacet.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+/**
+ * The <code>minLength</code> facet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class MinLengthFacet
+ extends Facet
+{
+
+ public final int value;
+
+ public final boolean fixed;
+
+ public MinLengthFacet(int value, boolean fixed, Annotation annotation)
+ {
+ super(MIN_LENGTH, annotation);
+ this.value = value;
+ this.fixed = fixed;
+ }
+
+ public int hashCode()
+ {
+ return value;
+ }
+
+ public boolean equals(Object other)
+ {
+ return (other instanceof MinLengthFacet &&
+ ((MinLengthFacet) other).value == value);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/NCNameType.java b/libjava/classpath/gnu/xml/validation/datatype/NCNameType.java
new file mode 100644
index 000000000..3f2088f9b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/NCNameType.java
@@ -0,0 +1,110 @@
+/* NCNameType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.io.IOException;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+import gnu.xml.stream.UnicodeReader;
+import gnu.xml.stream.XMLParser;
+
+/**
+ * The XML Schema NCName type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NCNameType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ NCNameType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "NCName"),
+ TypeLibrary.NAME);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ try
+ {
+ int[] cp = UnicodeReader.toCodePointArray(value);
+ if (cp.length == 0)
+ throw new DatatypeException("invalid NCName value");
+ // XXX XML 1.1 documents?
+ if (cp[0] == ':' || !XMLParser.isNameStartCharacter(cp[0], false))
+ throw new DatatypeException(0, "invalid NCName value");
+ boolean seenColon = false;
+ for (int i = 1; i < cp.length; i++)
+ {
+ if (cp[i] == ':')
+ {
+ if (seenColon || (i + 1 == cp.length))
+ throw new DatatypeException(i, "invalid NCName value");
+ seenColon = true;
+ }
+ else if (!XMLParser.isNameCharacter(cp[i], false))
+ throw new DatatypeException(i, "invalid NCName value");
+ }
+ }
+ catch (IOException e)
+ {
+ DatatypeException e2 = new DatatypeException("invalid NCName value");
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/NMTokenType.java b/libjava/classpath/gnu/xml/validation/datatype/NMTokenType.java
new file mode 100644
index 000000000..ca3271fef
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/NMTokenType.java
@@ -0,0 +1,101 @@
+/* NMTokenType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.io.IOException;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+import gnu.xml.stream.UnicodeReader;
+import gnu.xml.stream.XMLParser;
+
+/**
+ * The XML Schema NMTOKEN type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NMTokenType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ NMTokenType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "NMTOKEN"),
+ TypeLibrary.TOKEN);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ try
+ {
+ int[] cp = UnicodeReader.toCodePointArray(value);
+ if (cp.length == 0)
+ throw new DatatypeException("invalid NMTOKEN value");
+ for (int i = 0; i < cp.length; i++)
+ {
+ // XXX XML 1.1 documents?
+ if (!XMLParser.isNameCharacter(cp[i], false))
+ throw new DatatypeException(i, "invalid NMTOKEN value");
+ }
+ }
+ catch (IOException e)
+ {
+ DatatypeException e2 = new DatatypeException("invalid NMTOKEN value");
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/NMTokensType.java b/libjava/classpath/gnu/xml/validation/datatype/NMTokensType.java
new file mode 100644
index 000000000..388b451db
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/NMTokensType.java
@@ -0,0 +1,125 @@
+/* NMTokensType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.io.IOException;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+import gnu.xml.stream.UnicodeReader;
+import gnu.xml.stream.XMLParser;
+
+/**
+ * The XML Schema NMTOKENS type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NMTokensType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ NMTokensType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "NMTOKENS"),
+ TypeLibrary.NMTOKEN);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ CPStringBuilder buf = new CPStringBuilder();
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == ' ')
+ {
+ String token = buf.toString();
+ if (token.length() > 0)
+ checkNmtoken(token, i);
+ buf.setLength(0);
+ }
+ else
+ buf.append(c);
+ }
+ checkNmtoken(buf.toString(), len);
+ }
+
+ private void checkNmtoken(String text, int i)
+ throws DatatypeException
+ {
+ try
+ {
+ int[] cp = UnicodeReader.toCodePointArray(text);
+ if (cp.length == 0)
+ throw new DatatypeException("invalid NMTOKEN value");
+ for (int j = 0; j < cp.length; j++)
+ {
+ // XXX XML 1.1 documents?
+ if (!XMLParser.isNameCharacter(cp[j], false))
+ throw new DatatypeException(i, "invalid NMTOKEN value");
+ }
+ }
+ catch (IOException e)
+ {
+ DatatypeException e2 = new DatatypeException("invalid NMTOKEN value");
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/NameType.java b/libjava/classpath/gnu/xml/validation/datatype/NameType.java
new file mode 100644
index 000000000..70349aed8
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/NameType.java
@@ -0,0 +1,103 @@
+/* NameType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.io.IOException;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+import gnu.xml.stream.UnicodeReader;
+import gnu.xml.stream.XMLParser;
+
+/**
+ * The XML Schema Name type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NameType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ NameType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "Name"),
+ TypeLibrary.TOKEN);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ try
+ {
+ int[] cp = UnicodeReader.toCodePointArray(value);
+ if (cp.length == 0)
+ throw new DatatypeException("invalid Name value");
+ // XXX XML 1.1 documents?
+ if (!XMLParser.isNameStartCharacter(cp[0], false))
+ throw new DatatypeException(0, "invalid Name value");
+ for (int i = 1; i < cp.length; i++)
+ {
+ if (!XMLParser.isNameCharacter(cp[i], false))
+ throw new DatatypeException(i, "invalid Name value");
+ }
+ }
+ catch (IOException e)
+ {
+ DatatypeException e2 = new DatatypeException("invalid Name value");
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/NegativeIntegerType.java b/libjava/classpath/gnu/xml/validation/datatype/NegativeIntegerType.java
new file mode 100644
index 000000000..1e6439c99
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/NegativeIntegerType.java
@@ -0,0 +1,110 @@
+/* NegativeIntegerType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.math.BigInteger;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema negativeInteger type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NegativeIntegerType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.TOTAL_DIGITS,
+ Facet.FRACTION_DIGITS,
+ Facet.PATTERN,
+ Facet.WHITESPACE,
+ Facet.ENUMERATION,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ NegativeIntegerType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "negativeInteger"),
+ TypeLibrary.NON_POSITIVE_INTEGER);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValue(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException(0, "invalid negative integer value");
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == '-')
+ {
+ if (i == 0)
+ continue;
+ }
+ else if (c >= 0x30 && c <= 0x39)
+ continue;
+ throw new DatatypeException(i, "invalid negative integer value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new BigInteger(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/NonNegativeIntegerType.java b/libjava/classpath/gnu/xml/validation/datatype/NonNegativeIntegerType.java
new file mode 100644
index 000000000..c5192f346
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/NonNegativeIntegerType.java
@@ -0,0 +1,120 @@
+/* NonNegativeIntegerType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.math.BigInteger;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema nonNegativeInteger type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NonNegativeIntegerType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.TOTAL_DIGITS,
+ Facet.FRACTION_DIGITS,
+ Facet.PATTERN,
+ Facet.WHITESPACE,
+ Facet.ENUMERATION,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ NonNegativeIntegerType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "nonNegativeInteger"),
+ TypeLibrary.INTEGER);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException(0, "invalid non-negative integer value");
+ boolean negative = false;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == 0x30)
+ continue;
+ else if (c >= 0x31 && c <= 0x39)
+ {
+ if (negative)
+ throw new DatatypeException(i,
+ "invalid non-negative integer value");
+ continue;
+ }
+ else if (c == '+' && i == 0)
+ continue;
+ else if (c == '-' && i == 0)
+ {
+ negative = true;
+ continue;
+ }
+ throw new DatatypeException(i, "invalid non-negative integer value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new BigInteger(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/NonPositiveIntegerType.java b/libjava/classpath/gnu/xml/validation/datatype/NonPositiveIntegerType.java
new file mode 100644
index 000000000..e418c0615
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/NonPositiveIntegerType.java
@@ -0,0 +1,120 @@
+/* NonPositiveIntegerType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.math.BigInteger;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema nonPositiveInteger type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NonPositiveIntegerType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.TOTAL_DIGITS,
+ Facet.FRACTION_DIGITS,
+ Facet.PATTERN,
+ Facet.WHITESPACE,
+ Facet.ENUMERATION,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ NonPositiveIntegerType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "nonPositiveInteger"),
+ TypeLibrary.INTEGER);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException(0, "invalid non-positive integer value");
+ boolean positive = true;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == 0x30)
+ continue;
+ else if (c >= 0x31 && c <= 0x39)
+ {
+ if (positive)
+ throw new DatatypeException(i,
+ "invalid non-positive integer value");
+ continue;
+ }
+ else if (c == '+' && i == 0)
+ continue;
+ else if (c == '-' && i == 0)
+ {
+ positive = false;
+ continue;
+ }
+ throw new DatatypeException(i, "invalid non-positive integer value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new BigInteger(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/NormalizedStringType.java b/libjava/classpath/gnu/xml/validation/datatype/NormalizedStringType.java
new file mode 100644
index 000000000..9009f648a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/NormalizedStringType.java
@@ -0,0 +1,87 @@
+/* NormalizedStringType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema normalizedString type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NormalizedStringType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ NormalizedStringType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "normalizedString"),
+ TypeLibrary.STRING);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == 0x0a || c == 0x0d || c == 0x09)
+ throw new DatatypeException(i, "invalid normalized-string value");
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/NotationType.java b/libjava/classpath/gnu/xml/validation/datatype/NotationType.java
new file mode 100644
index 000000000..b661cb4e1
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/NotationType.java
@@ -0,0 +1,87 @@
+/* NotationType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema NOTATION type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NotationType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ NotationType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "NOTATION"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ if (!context.isNotation(value))
+ throw new DatatypeException("invalid NOTATION value");
+ }
+
+ public boolean isContextDependent()
+ {
+ return true;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/PatternFacet.java b/libjava/classpath/gnu/xml/validation/datatype/PatternFacet.java
new file mode 100644
index 000000000..9c1cb7e30
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/PatternFacet.java
@@ -0,0 +1,70 @@
+/* PatternFacet.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.regex.Pattern;
+
+/**
+ * The <code>pattern</code> facet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class PatternFacet
+ extends Facet
+{
+
+ public final Pattern value;
+
+ public PatternFacet(Pattern value, Annotation annotation)
+ {
+ super(PATTERN, annotation);
+ this.value = value;
+ }
+
+ public int hashCode()
+ {
+ return value.hashCode();
+ }
+
+ public boolean equals(Object other)
+ {
+ return (other instanceof PatternFacet &&
+ ((PatternFacet) other).value.equals(value));
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/PositiveIntegerType.java b/libjava/classpath/gnu/xml/validation/datatype/PositiveIntegerType.java
new file mode 100644
index 000000000..15d0febfc
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/PositiveIntegerType.java
@@ -0,0 +1,110 @@
+/* PositiveIntegerType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.math.BigInteger;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema positiveInteger type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class PositiveIntegerType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.TOTAL_DIGITS,
+ Facet.FRACTION_DIGITS,
+ Facet.PATTERN,
+ Facet.WHITESPACE,
+ Facet.ENUMERATION,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ PositiveIntegerType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "positiveInteger"),
+ TypeLibrary.NON_NEGATIVE_INTEGER);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException(0, "invalid positive integer value");
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == '+')
+ {
+ if (i == 0)
+ continue;
+ }
+ else if (c >= 0x30 && c <= 0x39)
+ continue;
+ throw new DatatypeException(i, "invalid positive integer value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new BigInteger(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/QNameType.java b/libjava/classpath/gnu/xml/validation/datatype/QNameType.java
new file mode 100644
index 000000000..bdc40c97f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/QNameType.java
@@ -0,0 +1,121 @@
+/* QNameType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.io.IOException;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+import gnu.xml.stream.UnicodeReader;
+import gnu.xml.stream.XMLParser;
+
+/**
+ * The XML Schema QName type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class QNameType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ QNameType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "QName"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int ci = -1;
+ try
+ {
+ int[] cp = UnicodeReader.toCodePointArray(value);
+ if (cp.length == 0)
+ throw new DatatypeException("invalid NCName value");
+ // XXX XML 1.1 documents?
+ if (cp[0] == ':' || !XMLParser.isNameStartCharacter(cp[0], false))
+ throw new DatatypeException(0, "invalid NCName value");
+ for (int i = 1; i < cp.length; i++)
+ {
+ if (cp[i] == ':')
+ {
+ if (ci != -1 || (i + 1 == cp.length))
+ throw new DatatypeException(i, "invalid NCName value");
+ ci = i;
+ }
+ else if (!XMLParser.isNameCharacter(cp[i], false))
+ throw new DatatypeException(i, "invalid NCName value");
+ }
+ }
+ catch (IOException e)
+ {
+ DatatypeException e2 = new DatatypeException("invalid NCName value");
+ e2.initCause(e);
+ throw e2;
+ }
+ if (ci != -1)
+ {
+ String prefix = value.substring(0, ci);
+ if (context.resolveNamespacePrefix(prefix) == null)
+ throw new DatatypeException("invalid namespace prefix");
+ }
+ }
+
+ public boolean isContextDependent()
+ {
+ return true;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/ShortType.java b/libjava/classpath/gnu/xml/validation/datatype/ShortType.java
new file mode 100644
index 000000000..43255571d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/ShortType.java
@@ -0,0 +1,132 @@
+/* ShortType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema short type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class ShortType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.TOTAL_DIGITS,
+ Facet.FRACTION_DIGITS,
+ Facet.PATTERN,
+ Facet.WHITESPACE,
+ Facet.ENUMERATION,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ static final String MAX_VALUE = "32767";
+ static final String MIN_VALUE = "32768";
+ static final int LENGTH = MAX_VALUE.length();
+
+ ShortType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "short"),
+ TypeLibrary.INT);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException(0, "invalid short value");
+ int i = 0, off = 0;
+ boolean compare = false;
+ String compareTo = MAX_VALUE;
+ char c = value.charAt(0);
+ if (c == '+')
+ i++;
+ else if (c == '-')
+ {
+ compareTo = MIN_VALUE;
+ i++;
+ }
+ if (len - i > LENGTH)
+ throw new DatatypeException(i, "invalid short value");
+ else if (len - i == LENGTH)
+ compare = true;
+ for (; i < len; i++)
+ {
+ c = value.charAt(i);
+ if (c >= 0x30 && c <= 0x39)
+ {
+ if (compare)
+ {
+ char d = compareTo.charAt(off);
+ if (Character.digit(c, 10) > Character.digit(d, 10))
+ throw new DatatypeException(i, "invalid short value");
+ }
+ off++;
+ continue;
+ }
+ throw new DatatypeException(i, "invalid short value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new Short(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/SimpleType.java b/libjava/classpath/gnu/xml/validation/datatype/SimpleType.java
new file mode 100644
index 000000000..1233723bd
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/SimpleType.java
@@ -0,0 +1,255 @@
+/* SimpleType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.Iterator;
+import java.util.Set;
+import java.util.regex.Matcher;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.Datatype;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.DatatypeStreamingValidator;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * An XML Schema simple type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class SimpleType
+ extends Type
+ implements Datatype
+{
+
+ /**
+ * The variety of the <code>anySimpleType</code> datatype.
+ */
+ public static final int ANY = 0;
+
+ /**
+ * The atomic variety.
+ */
+ public static final int ATOMIC = 1;
+
+ /**
+ * The list variety.
+ */
+ public static final int LIST = 2;
+
+ /**
+ * The union variety.
+ */
+ public static final int UNION = 3;
+
+ public static final int ID_TYPE_NULL = 0;
+ public static final int ID_TYPE_ID = 1;
+ public static final int ID_TYPE_IDREF = 2;
+ public static final int ID_TYPE_IDREFS = 3;
+
+ /**
+ * The variety of this simple type.
+ */
+ public final int variety;
+
+ /**
+ * The facets of this simple type.
+ */
+ public Set facets;
+
+ /**
+ * The fundamental facets of this simple type.
+ */
+ public int fundamentalFacets;
+
+ /**
+ * If this datatype has been derived by restriction, then the component
+ * from which it was derived.
+ */
+ public final SimpleType baseType;
+
+ /**
+ * Optional annotation.
+ */
+ public final Annotation annotation;
+
+ public SimpleType(QName name, int variety, Set facets,
+ int fundamentalFacets, SimpleType baseType,
+ Annotation annotation)
+ {
+ super(name);
+ this.variety = variety;
+ this.facets = facets;
+ this.fundamentalFacets = fundamentalFacets;
+ this.baseType = baseType;
+ this.annotation = annotation;
+ }
+
+ /**
+ * Indicates whether this type permits the specified value.
+ */
+ public boolean isValid(String value, ValidationContext context)
+ {
+ try
+ {
+ checkValid(value, context);
+ return true;
+ }
+ catch (DatatypeException e)
+ {
+ return false;
+ }
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ if (facets != null && !facets.isEmpty())
+ {
+ Object parsedValue = createValue(value, context);
+ for (Iterator i = facets.iterator(); i.hasNext(); )
+ {
+ Facet facet = (Facet) i.next();
+ switch (facet.type)
+ {
+ case Facet.LENGTH:
+ LengthFacet lf = (LengthFacet) facet;
+ if (value.length() != lf.value)
+ throw new DatatypeException("invalid length");
+ break;
+ case Facet.MIN_LENGTH:
+ MinLengthFacet nlf = (MinLengthFacet) facet;
+ if (value.length() < nlf.value)
+ throw new DatatypeException("invalid minimum length");
+ break;
+ case Facet.MAX_LENGTH:
+ MaxLengthFacet xlf = (MaxLengthFacet) facet;
+ if (value.length() > xlf.value)
+ throw new DatatypeException("invalid maximum length");
+ break;
+ case Facet.PATTERN:
+ PatternFacet pf = (PatternFacet) facet;
+ Matcher matcher = pf.value.matcher(value);
+ if (!matcher.find())
+ throw new DatatypeException("invalid match for pattern");
+ break;
+ case Facet.ENUMERATION:
+ // TODO
+ break;
+ case Facet.WHITESPACE:
+ // TODO
+ break;
+ case Facet.MAX_INCLUSIVE:
+ MaxInclusiveFacet xif = (MaxInclusiveFacet) facet;
+ if (!xif.matches(parsedValue))
+ throw new DatatypeException("beyond upper bound");
+ break;
+ case Facet.MAX_EXCLUSIVE:
+ MaxExclusiveFacet xef = (MaxExclusiveFacet) facet;
+ if (!xef.matches(parsedValue))
+ throw new DatatypeException("beyond upper bound");
+ break;
+ case Facet.MIN_EXCLUSIVE:
+ MinExclusiveFacet nef = (MinExclusiveFacet) facet;
+ if (!nef.matches(parsedValue))
+ throw new DatatypeException("beyond lower bound");
+ break;
+ case Facet.MIN_INCLUSIVE:
+ MinInclusiveFacet nif = (MinInclusiveFacet) facet;
+ if (!nif.matches(parsedValue))
+ throw new DatatypeException("beyond lower bound");
+ break;
+ case Facet.TOTAL_DIGITS:
+ TotalDigitsFacet tdf = (TotalDigitsFacet) facet;
+ if (countDigits(value, true) > tdf.value)
+ throw new DatatypeException("too many digits");
+ break;
+ case Facet.FRACTION_DIGITS:
+ FractionDigitsFacet fdf = (FractionDigitsFacet) facet;
+ if (countDigits(value, false) > fdf.value)
+ throw new DatatypeException("too many fraction digits");
+ break;
+ }
+ }
+ }
+ }
+
+ private static int countDigits(String value, boolean any)
+ {
+ int count = 0;
+ int len = value.length();
+ boolean seenDecimal = false;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == 0x2e)
+ seenDecimal = true;
+ else if (c >= 0x30 && c <= 0x39 && (any || seenDecimal))
+ count++;
+ }
+ return count;
+ }
+
+ // TODO createStreamingValidator
+ public DatatypeStreamingValidator createStreamingValidator(ValidationContext context)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ return literal;
+ }
+
+ public boolean sameValue(Object value1, Object value2) {
+ return value1.equals(value2);
+ }
+
+ public int valueHashCode(Object value) {
+ return value.hashCode();
+ }
+
+ public int getIdType()
+ {
+ return ID_TYPE_NULL;
+ }
+
+ public boolean isContextDependent()
+ {
+ return false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/StringType.java b/libjava/classpath/gnu/xml/validation/datatype/StringType.java
new file mode 100644
index 000000000..e20d1b87e
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/StringType.java
@@ -0,0 +1,72 @@
+/* StringType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+
+/**
+ * The XML Schema string type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class StringType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ StringType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "string"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/TimeType.java b/libjava/classpath/gnu/xml/validation/datatype/TimeType.java
new file mode 100644
index 000000000..83ada5bc6
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/TimeType.java
@@ -0,0 +1,302 @@
+/* TimeType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.TimeZone;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema time type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class TimeType
+ extends AtomicSimpleType
+{
+
+ static class Time
+ implements Comparable
+ {
+ int minutes;
+ float seconds;
+
+ public int hashCode()
+ {
+ return minutes * 31 + new Float(seconds).hashCode();
+ }
+
+ public boolean equals(Object other)
+ {
+ if (other instanceof Time)
+ {
+ Time time = (Time) other;
+ return time.minutes == minutes && time.seconds == seconds;
+ }
+ return false;
+ }
+
+ public int compareTo(Object other)
+ {
+ if (other instanceof Time)
+ {
+ Time time = (Time) other;
+ if (time.minutes != minutes)
+ return minutes - time.minutes;
+ if (time.seconds == seconds)
+ return 0;
+ return (seconds < time.seconds) ? -1 : 1;
+ }
+ return 0;
+ }
+
+ }
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ TimeType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "time"),
+ TypeLibrary.ANY_SIMPLE_TYPE);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ int state = 3;
+ int start = 0;
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == '-' && state == 0)
+ {
+ start++;
+ continue;
+ }
+ if (c >= 0x30 && c <= 0x39)
+ continue;
+ switch (state)
+ {
+ case 3: // hour
+ if (c == ':')
+ {
+ if (i - start != 2)
+ throw new DatatypeException(i, "invalid time value");
+ state = 4;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 4: // minute
+ if (c == ':')
+ {
+ if (i - start != 2)
+ throw new DatatypeException(i, "invalid time value");
+ state = 5;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 5: // second
+ if (c == '.')
+ {
+ if (i - start != 2)
+ throw new DatatypeException(i, "invalid time value");
+ state = 6;
+ start = i + 1;
+ continue;
+ }
+ else if (c == ' ')
+ {
+ if (i - start != 2)
+ throw new DatatypeException(i, "invalid time value");
+ state = 7;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 6: // second fraction
+ if (c == ' ')
+ {
+ state = 7;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 7: // timezone 1
+ if (start == i)
+ {
+ if (c == '+' || c == '-')
+ continue;
+ else if (c == 'Z')
+ {
+ state = 9;
+ start = i + 1;
+ continue;
+ }
+ }
+ if (c == ':')
+ {
+ if (i - start != 2)
+ throw new DatatypeException(i, "invalid time value");
+ state = 8;
+ start = i + 1;
+ continue;
+ }
+ break;
+ }
+ throw new DatatypeException(i, "invalid time value");
+ }
+ switch (state)
+ {
+ case 5: // second
+ if (len - start != 2)
+ throw new DatatypeException(len, "invalid time value");
+ break;
+ case 6: // second fraction
+ break;
+ case 8: // timezone 2
+ if (len - start != 2)
+ throw new DatatypeException(len, "invalid time value");
+ break;
+ case 9: // post Z
+ break;
+ default:
+ throw new DatatypeException(len, "invalid time value");
+ }
+ }
+
+ public Object createValue(String value, ValidationContext context) {
+ int len = value.length();
+ int state = 3;
+ int start = 0;
+ Time time = new Time();
+ try
+ {
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c >= 0x30 && c <= 0x39)
+ continue;
+ switch (state)
+ {
+ case 3: // hour
+ if (c == ':')
+ {
+ time.minutes =
+ Integer.parseInt(value.substring(start, i)) * 60;
+ state = 4;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 4: // minute
+ if (c == ':')
+ {
+ time.minutes +=
+ Integer.parseInt(value.substring(start, i));
+ state = 5;
+ start = i + 1;
+ continue;
+ }
+ break;
+ case 5: // second
+ if (c == ' ')
+ {
+ time.seconds =
+ Float.parseFloat(value.substring(start, i));
+ state = 7;
+ start = i + 1;
+ continue;
+ }
+ break;
+ }
+ }
+ // end of input
+ if (len - start > 0 && state == 7)
+ {
+ // Timezone
+ String timezone = value.substring(len - start);
+ int i = timezone.indexOf(':');
+ if (i == -1)
+ {
+ if ("Z".equals(timezone))
+ timezone = "UTC";
+ TimeZone tz = TimeZone.getTimeZone(timezone);
+ if (tz == null)
+ return null;
+ time.minutes += tz.getRawOffset();
+ }
+ else
+ {
+ String tzh = timezone.substring(0, i);
+ String tzm = timezone.substring(i + 1);
+ int offset = Integer.parseInt(tzh) * 60;
+ if (offset < 0)
+ offset -= Integer.parseInt(tzm);
+ else
+ offset += Integer.parseInt(tzm);
+ time.minutes += offset;
+ }
+ }
+ return time;
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/TokenType.java b/libjava/classpath/gnu/xml/validation/datatype/TokenType.java
new file mode 100644
index 000000000..dfe5f14ce
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/TokenType.java
@@ -0,0 +1,95 @@
+/* TokenType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema token type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class TokenType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.LENGTH,
+ Facet.MIN_LENGTH,
+ Facet.MAX_LENGTH,
+ Facet.PATTERN,
+ Facet.ENUMERATION,
+ Facet.WHITESPACE
+ };
+
+ TokenType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "token"),
+ TypeLibrary.NORMALIZED_STRING);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException(0, "invalid token value");
+ if (value.charAt(0) == ' ' || value.charAt(len - 1) == ' ')
+ throw new DatatypeException(0, "invalid token value");
+ char last = '\u0000';
+ for (int i = 0; i < len; i++)
+ {
+ char c = value.charAt(i);
+ if (c == 0x0a || c == 0x0d || c == 0x09)
+ throw new DatatypeException(i, "invalid token value");
+ if (c == ' ' && last == ' ')
+ throw new DatatypeException(i, "invalid token value");
+ last = c;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/TotalDigitsFacet.java b/libjava/classpath/gnu/xml/validation/datatype/TotalDigitsFacet.java
new file mode 100644
index 000000000..25b50cd6f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/TotalDigitsFacet.java
@@ -0,0 +1,71 @@
+/* TotalDigitsFacet.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+/**
+ * The <code>totalDigits</code> facet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class TotalDigitsFacet
+ extends Facet
+{
+
+ public final int value;
+
+ public final boolean fixed;
+
+ public TotalDigitsFacet(int value, boolean fixed, Annotation annotation)
+ {
+ super(TOTAL_DIGITS, annotation);
+ this.value = value;
+ this.fixed = fixed;
+ }
+
+ public int hashCode()
+ {
+ return value;
+ }
+
+ public boolean equals(Object other)
+ {
+ return (other instanceof TotalDigitsFacet &&
+ ((TotalDigitsFacet) other).value == value);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/Type.java b/libjava/classpath/gnu/xml/validation/datatype/Type.java
new file mode 100644
index 000000000..ebd650933
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/Type.java
@@ -0,0 +1,62 @@
+/* Type.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.namespace.QName;
+/**
+ * Abstract base class for XML Schema datatypes.
+ * @see http://www.w3.org/TR/xmlschema-2/
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public abstract class Type
+{
+
+ public static final Type ANY_TYPE = new AnyType();
+
+ /**
+ * The name of this type.
+ */
+ public final QName name;
+
+ public Type(QName name)
+ {
+ this.name = name;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/TypeBuilder.java b/libjava/classpath/gnu/xml/validation/datatype/TypeBuilder.java
new file mode 100644
index 000000000..087333153
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/TypeBuilder.java
@@ -0,0 +1,278 @@
+/* TypeBuilder.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.LinkedHashSet;
+import java.util.regex.Pattern;
+import org.relaxng.datatype.Datatype;
+import org.relaxng.datatype.DatatypeBuilder;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * Datatype builder.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class TypeBuilder
+ implements DatatypeBuilder
+{
+
+ final SimpleType type;
+
+ TypeBuilder(SimpleType type)
+ {
+ this.type = type;
+ // TODO fundamental facets
+ type.facets = new LinkedHashSet();
+ }
+
+ public void addParameter(String name, String value, ValidationContext context)
+ throws DatatypeException
+ {
+ // TODO fundamental facets
+ if ("length".equals(name))
+ type.facets.add(parseLengthFacet(value));
+ else if ("minLength".equals(name))
+ type.facets.add(parseMinLengthFacet(value));
+ else if ("maxLength".equals(name))
+ type.facets.add(parseMaxLengthFacet(value));
+ else if ("pattern".equals(name))
+ type.facets.add(parsePatternFacet(value));
+ else if ("enumeration".equals(name))
+ type.facets.add(parseEnumerationFacet(value));
+ else if ("whiteSpace".equals(name))
+ type.facets.add(parseWhiteSpaceFacet(value));
+ else if ("maxInclusive".equals(name))
+ type.facets.add(parseMaxInclusiveFacet(value, context));
+ else if ("maxExclusive".equals(name))
+ type.facets.add(parseMaxExclusiveFacet(value, context));
+ else if ("minExclusive".equals(name))
+ type.facets.add(parseMinExclusiveFacet(value, context));
+ else if ("minInclusive".equals(name))
+ type.facets.add(parseMinInclusiveFacet(value, context));
+ else if ("totalDigits".equals(name))
+ type.facets.add(parseTotalDigitsFacet(value));
+ else if ("fractionDigits".equals(name))
+ type.facets.add(parseFractionDigitsFacet(value));
+ }
+
+ LengthFacet parseLengthFacet(String value)
+ throws DatatypeException
+ {
+ int si = value.indexOf(' ');
+ boolean fixed = false;
+ if (si != -1)
+ {
+ if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
+ throw new DatatypeException("second argument must be FIXED if present");
+ fixed = true;
+ value = value.substring(0, si);
+ }
+ return new LengthFacet(Integer.parseInt(value), fixed, null);
+ }
+
+ MinLengthFacet parseMinLengthFacet(String value)
+ throws DatatypeException
+ {
+ int si = value.indexOf(' ');
+ boolean fixed = false;
+ if (si != -1)
+ {
+ if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
+ throw new DatatypeException("second argument must be FIXED if present");
+ fixed = true;
+ value = value.substring(0, si);
+ }
+ return new MinLengthFacet(Integer.parseInt(value), fixed, null);
+ }
+
+ MaxLengthFacet parseMaxLengthFacet(String value)
+ throws DatatypeException
+ {
+ int si = value.indexOf(' ');
+ boolean fixed = false;
+ if (si != -1)
+ {
+ if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
+ throw new DatatypeException("second argument must be FIXED if present");
+ fixed = true;
+ value = value.substring(0, si);
+ }
+ return new MaxLengthFacet(Integer.parseInt(value), fixed, null);
+ }
+
+ PatternFacet parsePatternFacet(String value)
+ throws DatatypeException
+ {
+ return new PatternFacet(Pattern.compile(value), null);
+ }
+
+ EnumerationFacet parseEnumerationFacet(String value)
+ throws DatatypeException
+ {
+ return new EnumerationFacet(value, null);
+ }
+
+ WhiteSpaceFacet parseWhiteSpaceFacet(String value)
+ throws DatatypeException
+ {
+ int si = value.indexOf(' ');
+ boolean fixed = false;
+ if (si != -1)
+ {
+ if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
+ throw new DatatypeException("second argument must be FIXED if present");
+ fixed = true;
+ value = value.substring(0, si);
+ }
+ if ("preserve".equals(value))
+ return new WhiteSpaceFacet(WhiteSpaceFacet.PRESERVE, fixed, null);
+ if ("replace".equals(value))
+ return new WhiteSpaceFacet(WhiteSpaceFacet.REPLACE, fixed, null);
+ if ("collapse".equals(value))
+ return new WhiteSpaceFacet(WhiteSpaceFacet.COLLAPSE, fixed, null);
+ throw new DatatypeException("argument must be preserve, replace, or collapse");
+ }
+
+ MaxInclusiveFacet parseMaxInclusiveFacet(String value,
+ ValidationContext context)
+ throws DatatypeException
+ {
+ int si = value.indexOf(' ');
+ boolean fixed = false;
+ if (si != -1)
+ {
+ if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
+ throw new DatatypeException("second argument must be FIXED if present");
+ fixed = true;
+ value = value.substring(0, si);
+ }
+ return new MaxInclusiveFacet(type.createValue(value, context), fixed, null);
+ }
+
+ MaxExclusiveFacet parseMaxExclusiveFacet(String value,
+ ValidationContext context)
+ throws DatatypeException
+ {
+ int si = value.indexOf(' ');
+ boolean fixed = false;
+ if (si != -1)
+ {
+ if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
+ throw new DatatypeException("second argument must be FIXED if present");
+ fixed = true;
+ value = value.substring(0, si);
+ }
+ return new MaxExclusiveFacet(type.createValue(value, context), fixed, null);
+ }
+
+ MinExclusiveFacet parseMinExclusiveFacet(String value,
+ ValidationContext context)
+ throws DatatypeException
+ {
+ int si = value.indexOf(' ');
+ boolean fixed = false;
+ if (si != -1)
+ {
+ if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
+ throw new DatatypeException("second argument must be FIXED if present");
+ fixed = true;
+ value = value.substring(0, si);
+ }
+ return new MinExclusiveFacet(type.createValue(value, context), fixed, null);
+ }
+
+ MinInclusiveFacet parseMinInclusiveFacet(String value,
+ ValidationContext context)
+ throws DatatypeException
+ {
+ int si = value.indexOf(' ');
+ boolean fixed = false;
+ if (si != -1)
+ {
+ if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
+ throw new DatatypeException("second argument must be FIXED if present");
+ fixed = true;
+ value = value.substring(0, si);
+ }
+ return new MinInclusiveFacet(type.createValue(value, context), fixed, null);
+ }
+
+ TotalDigitsFacet parseTotalDigitsFacet(String value)
+ throws DatatypeException
+ {
+ int si = value.indexOf(' ');
+ boolean fixed = false;
+ if (si != -1)
+ {
+ if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
+ throw new DatatypeException("second argument must be FIXED if present");
+ fixed = true;
+ value = value.substring(0, si);
+ }
+ int val = Integer.parseInt(value);
+ if (val < 0)
+ throw new DatatypeException("value must be a positiveInteger");
+ return new TotalDigitsFacet(val, fixed, null);
+ }
+
+ FractionDigitsFacet parseFractionDigitsFacet(String value)
+ throws DatatypeException
+ {
+ int si = value.indexOf(' ');
+ boolean fixed = false;
+ if (si != -1)
+ {
+ if (!"FIXED".equalsIgnoreCase(value.substring(si + 1)))
+ throw new DatatypeException("second argument must be FIXED if present");
+ fixed = true;
+ value = value.substring(0, si);
+ }
+ int val = Integer.parseInt(value);
+ if (val < 0)
+ throw new DatatypeException("value must be a positiveInteger");
+ return new FractionDigitsFacet(val, fixed, null);
+ }
+
+ public Datatype createDatatype()
+ {
+ return type;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/TypeLibrary.java b/libjava/classpath/gnu/xml/validation/datatype/TypeLibrary.java
new file mode 100644
index 000000000..4db48f799
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/TypeLibrary.java
@@ -0,0 +1,173 @@
+/* TypeLibrary.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.relaxng.datatype.Datatype;
+import org.relaxng.datatype.DatatypeBuilder;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.DatatypeLibrary;
+
+/**
+ * Datatype library for XML Schema datatypes.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class TypeLibrary
+ implements DatatypeLibrary
+{
+
+ public static final SimpleType ANY_SIMPLE_TYPE = new AnySimpleType();
+
+ public static final SimpleType STRING = new StringType();
+ public static final SimpleType BOOLEAN = new BooleanType();
+ public static final SimpleType DECIMAL = new DecimalType();
+ public static final SimpleType FLOAT = new FloatType();
+ public static final SimpleType DOUBLE = new DoubleType();
+ public static final SimpleType DURATION = new DurationType();
+ public static final SimpleType DATE_TIME = new DateTimeType();
+ public static final SimpleType TIME = new TimeType();
+ public static final SimpleType DATE = new DateType();
+ public static final SimpleType G_YEAR_MONTH = new GYearMonthType();
+ public static final SimpleType G_YEAR = new GYearType();
+ public static final SimpleType G_MONTH_DAY = new GMonthDayType();
+ public static final SimpleType G_DAY = new GDayType();
+ public static final SimpleType G_MONTH = new GMonthType();
+ public static final SimpleType HEX_BINARY = new HexBinaryType();
+ public static final SimpleType BASE64_BINARY = new Base64BinaryType();
+ public static final SimpleType ANY_URI = new AnyURIType();
+ public static final SimpleType QNAME = new QNameType();
+ public static final SimpleType NOTATION = new NotationType();
+
+ public static final SimpleType NORMALIZED_STRING = new NormalizedStringType();
+ public static final SimpleType TOKEN = new TokenType();
+ public static final SimpleType LANGUAGE = new LanguageType();
+ public static final SimpleType NMTOKEN = new NMTokenType();
+ public static final SimpleType NMTOKENS = new NMTokensType();
+ public static final SimpleType NAME = new NameType();
+ public static final SimpleType NCNAME = new NCNameType();
+ public static final SimpleType ID = new IDType();
+ public static final SimpleType IDREF = new IDRefType();
+ public static final SimpleType IDREFS = new IDRefsType();
+ public static final SimpleType ENTITY = new EntityType();
+ public static final SimpleType ENTITIES = new EntitiesType();
+ public static final SimpleType INTEGER = new IntegerType();
+ public static final SimpleType NON_POSITIVE_INTEGER = new NonPositiveIntegerType();
+ public static final SimpleType NEGATIVE_INTEGER = new NegativeIntegerType();
+ public static final SimpleType LONG = new LongType();
+ public static final SimpleType INT = new IntType();
+ public static final SimpleType SHORT = new ShortType();
+ public static final SimpleType BYTE = new ByteType();
+ public static final SimpleType NON_NEGATIVE_INTEGER = new NonNegativeIntegerType();
+ public static final SimpleType UNSIGNED_LONG = new UnsignedLongType();
+ public static final SimpleType UNSIGNED_INT = new UnsignedIntType();
+ public static final SimpleType UNSIGNED_SHORT = new UnsignedShortType();
+ public static final SimpleType UNSIGNED_BYTE = new UnsignedByteType();
+ public static final SimpleType POSITIVE_INTEGER = new PositiveIntegerType();
+
+ private static Map byName;
+ static
+ {
+ byName = new HashMap();
+ byName.put("anySimpleType", ANY_SIMPLE_TYPE);
+ byName.put("string", STRING);
+ byName.put("boolean", BOOLEAN);
+ byName.put("decimal", DECIMAL);
+ byName.put("float", FLOAT);
+ byName.put("double", DOUBLE);
+ byName.put("duration", DURATION);
+ byName.put("dateTime", DATE_TIME);
+ byName.put("time", TIME);
+ byName.put("date", DATE);
+ byName.put("gYearMonth", G_YEAR_MONTH);
+ byName.put("gYear", G_YEAR);
+ byName.put("gMonthDay", G_MONTH_DAY);
+ byName.put("gDay", G_DAY);
+ byName.put("gMonth",G_MONTH);
+ byName.put("hexBinary", HEX_BINARY);
+ byName.put("base64Binary", BASE64_BINARY);
+ byName.put("anyURI", ANY_URI);
+ byName.put("QName", QNAME);
+ byName.put("NOTATION", NOTATION);
+ byName.put("normalizedString", NORMALIZED_STRING);
+ byName.put("token", TOKEN);
+ byName.put("language", LANGUAGE);
+ byName.put("NMTOKEN", NMTOKEN);
+ byName.put("NMTOKENS", NMTOKENS);
+ byName.put("Name", NAME);
+ byName.put("NCName", NCNAME);
+ byName.put("ID", ID);
+ byName.put("IDREF", IDREF);
+ byName.put("IDREFS", IDREFS);
+ byName.put("ENTITY", ENTITY);
+ byName.put("ENTITIES", ENTITIES);
+ byName.put("integer", INTEGER);
+ byName.put("nonPositiveInteger", NON_POSITIVE_INTEGER);
+ byName.put("negativeInteger", NEGATIVE_INTEGER);
+ byName.put("long", LONG);
+ byName.put("int", INT);
+ byName.put("short", SHORT);
+ byName.put("byte", BYTE);
+ byName.put("nonNegativeInteger", NON_NEGATIVE_INTEGER);
+ byName.put("unsignedLong", UNSIGNED_LONG);
+ byName.put("unsignedInt", UNSIGNED_INT);
+ byName.put("unsignedShort", UNSIGNED_SHORT);
+ byName.put("unsignedByte", UNSIGNED_BYTE);
+ byName.put("positiveInteger", POSITIVE_INTEGER);
+ }
+
+ public DatatypeBuilder createDatatypeBuilder(String baseTypeLocalName)
+ throws DatatypeException
+ {
+ SimpleType type = (SimpleType) byName.get(baseTypeLocalName);
+ if (type == null)
+ throw new DatatypeException("Unknown type name: " + baseTypeLocalName);
+ return new TypeBuilder(type);
+ }
+
+ public Datatype createDatatype(String typeLocalName)
+ throws DatatypeException
+ {
+ SimpleType type = (SimpleType) byName.get(typeLocalName);
+ if (type == null)
+ throw new DatatypeException("Unknown type name: " + typeLocalName);
+ return type;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/TypeLibraryFactory.java b/libjava/classpath/gnu/xml/validation/datatype/TypeLibraryFactory.java
new file mode 100644
index 000000000..2f9bbd3c6
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/TypeLibraryFactory.java
@@ -0,0 +1,60 @@
+/* TypeLibraryFactory.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import org.relaxng.datatype.DatatypeLibrary;
+import org.relaxng.datatype.DatatypeLibraryFactory;
+
+/**
+ * Datatype library factory for XML Schema datatypes.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class TypeLibraryFactory
+ implements DatatypeLibraryFactory
+{
+
+ public DatatypeLibrary createDatatypeLibrary(String namespaceURI)
+ {
+ if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(namespaceURI))
+ return new TypeLibrary();
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/UnionSimpleType.java b/libjava/classpath/gnu/xml/validation/datatype/UnionSimpleType.java
new file mode 100644
index 000000000..253e1df7b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/UnionSimpleType.java
@@ -0,0 +1,82 @@
+/* UnionSimpleType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * An XML Schema union simple type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class UnionSimpleType
+ extends SimpleType
+{
+
+ /**
+ * The member types in this union.
+ */
+ public final List memberTypes;
+
+ public UnionSimpleType(QName name, Set facets,
+ int fundamentalFacets, SimpleType baseType,
+ Annotation annotation, List memberTypes)
+ {
+ super(name, UNION, facets, fundamentalFacets, baseType, annotation);
+ this.memberTypes = memberTypes;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ for (Iterator i = memberTypes.iterator(); i.hasNext(); )
+ {
+ SimpleType type = (SimpleType) i.next();
+ if (type.isValid(value, context))
+ return;
+ }
+ throw new DatatypeException("invalid union type value");
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/UnsignedByteType.java b/libjava/classpath/gnu/xml/validation/datatype/UnsignedByteType.java
new file mode 100644
index 000000000..cf12abe3a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/UnsignedByteType.java
@@ -0,0 +1,120 @@
+/* UnsignedByteType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema unsignedByte type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class UnsignedByteType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.TOTAL_DIGITS,
+ Facet.FRACTION_DIGITS,
+ Facet.PATTERN,
+ Facet.WHITESPACE,
+ Facet.ENUMERATION,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ static final String MAX_VALUE = "255";
+ static final int LENGTH = MAX_VALUE.length();
+
+ UnsignedByteType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "unsignedByte"),
+ TypeLibrary.UNSIGNED_SHORT);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException(0, "invalid unsigned byte value");
+ boolean compare = false;
+ for (int i = 0; i < len; i++)
+ {
+ if (len - i > LENGTH)
+ throw new DatatypeException(i, "invalid unsigned byte value");
+ else if (len - i == LENGTH)
+ compare = true;
+ char c = value.charAt(i);
+ if (c >= 0x30 && c <= 0x39)
+ {
+ if (compare)
+ {
+ char d = MAX_VALUE.charAt(i);
+ if (Character.digit(c, 10) > Character.digit(d, 10))
+ throw new DatatypeException(i, "invalid unsigned byte value");
+ }
+ continue;
+ }
+ throw new DatatypeException(i, "invalid unsigned byte value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new Byte(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/UnsignedIntType.java b/libjava/classpath/gnu/xml/validation/datatype/UnsignedIntType.java
new file mode 100644
index 000000000..b634442bb
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/UnsignedIntType.java
@@ -0,0 +1,120 @@
+/* UnsignedIntType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema unsignedInt type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class UnsignedIntType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.TOTAL_DIGITS,
+ Facet.FRACTION_DIGITS,
+ Facet.PATTERN,
+ Facet.WHITESPACE,
+ Facet.ENUMERATION,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ static final String MAX_VALUE = "4294967295";
+ static final int LENGTH = MAX_VALUE.length();
+
+ UnsignedIntType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "unsignedInt"),
+ TypeLibrary.UNSIGNED_LONG);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException(0, "invalid unsigned int value");
+ boolean compare = false;
+ for (int i = 0; i < len; i++)
+ {
+ if (len - i > LENGTH)
+ throw new DatatypeException(i, "invalid unsigned int value");
+ else if (len - i == LENGTH)
+ compare = true;
+ char c = value.charAt(i);
+ if (c >= 0x30 && c <= 0x39)
+ {
+ if (compare)
+ {
+ char d = MAX_VALUE.charAt(i);
+ if (Character.digit(c, 10) > Character.digit(d, 10))
+ throw new DatatypeException(i, "invalid unsigned int value");
+ }
+ continue;
+ }
+ throw new DatatypeException(i, "invalid unsigned int value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new Integer(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/UnsignedLongType.java b/libjava/classpath/gnu/xml/validation/datatype/UnsignedLongType.java
new file mode 100644
index 000000000..784b86663
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/UnsignedLongType.java
@@ -0,0 +1,120 @@
+/* UnsignedLongType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema unsignedLong type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class UnsignedLongType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.TOTAL_DIGITS,
+ Facet.FRACTION_DIGITS,
+ Facet.PATTERN,
+ Facet.WHITESPACE,
+ Facet.ENUMERATION,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ static final String MAX_VALUE = "18446744073709551615";
+ static final int LENGTH = MAX_VALUE.length();
+
+ UnsignedLongType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "unsignedLong"),
+ TypeLibrary.NON_NEGATIVE_INTEGER);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException(0, "invalid unsigned long value");
+ boolean compare = false;
+ for (int i = 0; i < len; i++)
+ {
+ if (len - i > LENGTH)
+ throw new DatatypeException(i, "invalid unsigned long value");
+ else if (len - i == LENGTH)
+ compare = true;
+ char c = value.charAt(i);
+ if (c >= 0x30 && c <= 0x39)
+ {
+ if (compare)
+ {
+ char d = MAX_VALUE.charAt(i);
+ if (Character.digit(c, 10) > Character.digit(d, 10))
+ throw new DatatypeException(i, "invalid unsigned long value");
+ }
+ continue;
+ }
+ throw new DatatypeException(i, "invalid unsigned long value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new Long(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/UnsignedShortType.java b/libjava/classpath/gnu/xml/validation/datatype/UnsignedShortType.java
new file mode 100644
index 000000000..ececb608c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/UnsignedShortType.java
@@ -0,0 +1,121 @@
+/* UnsignedShortType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.ValidationContext;
+
+/**
+ * The XML Schema unsignedShort type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class UnsignedShortType
+ extends AtomicSimpleType
+{
+
+ static final int[] CONSTRAINING_FACETS = {
+ Facet.TOTAL_DIGITS,
+ Facet.FRACTION_DIGITS,
+ Facet.PATTERN,
+ Facet.WHITESPACE,
+ Facet.ENUMERATION,
+ Facet.MAX_INCLUSIVE,
+ Facet.MAX_EXCLUSIVE,
+ Facet.MIN_INCLUSIVE,
+ Facet.MIN_EXCLUSIVE
+ };
+
+ static final String MAX_VALUE = "65535";
+ static final int LENGTH = MAX_VALUE.length();
+
+ UnsignedShortType()
+ {
+ super(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "unsignedShort"),
+ TypeLibrary.UNSIGNED_INT);
+ }
+
+ public int[] getConstrainingFacets()
+ {
+ return CONSTRAINING_FACETS;
+ }
+
+ public void checkValid(String value, ValidationContext context)
+ throws DatatypeException
+ {
+ super.checkValid(value, context);
+ int len = value.length();
+ if (len == 0)
+ throw new DatatypeException(0, "invalid unsigned short value");
+ boolean compare = false;
+ for (int i = 0; i < len; i++)
+ {
+ if (len - i > LENGTH)
+ throw new DatatypeException(i, "invalid unsigned short value");
+ else if (len - i == LENGTH)
+ compare = true;
+ char c = value.charAt(i);
+ if (c >= 0x30 && c <= 0x39)
+ {
+ if (compare)
+ {
+ char d = MAX_VALUE.charAt(i);
+ if (Character.digit(c, 10) > Character.digit(d, 10))
+ throw new DatatypeException(i,
+ "invalid unsigned short value");
+ }
+ continue;
+ }
+ throw new DatatypeException(i, "invalid unsigned short value");
+ }
+ }
+
+ public Object createValue(String literal, ValidationContext context) {
+ try
+ {
+ return new Short(literal);
+ }
+ catch (NumberFormatException e)
+ {
+ return null;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/datatype/WhiteSpaceFacet.java b/libjava/classpath/gnu/xml/validation/datatype/WhiteSpaceFacet.java
new file mode 100644
index 000000000..0f2109cfc
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/datatype/WhiteSpaceFacet.java
@@ -0,0 +1,74 @@
+/* WhiteSpaceFacet.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.datatype;
+
+/**
+ * The <code>whiteSpace</code> facet.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class WhiteSpaceFacet
+ extends Facet
+{
+
+ public static final int PRESERVE = 0;
+ public static final int REPLACE = 1;
+ public static final int COLLAPSE = 2;
+
+ public final int value;
+ public final boolean fixed;
+
+ public WhiteSpaceFacet(int value, boolean fixed, Annotation annotation)
+ {
+ super(WHITESPACE, annotation);
+ this.value = value;
+ this.fixed = fixed;
+ }
+
+ public int hashCode()
+ {
+ return value;
+ }
+
+ public boolean equals(Object other)
+ {
+ return (other instanceof WhiteSpaceFacet &&
+ ((WhiteSpaceFacet) other).value == value);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/AnyNameNameClass.java b/libjava/classpath/gnu/xml/validation/relaxng/AnyNameNameClass.java
new file mode 100644
index 000000000..7b27a3c98
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/AnyNameNameClass.java
@@ -0,0 +1,57 @@
+/* AnyNameNameClass.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG anyName element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class AnyNameNameClass
+ extends NameClass
+{
+
+ NameClass exceptNameClass;
+
+ boolean matchesName(String uri, String localName)
+ {
+ return (exceptNameClass == null) ? true :
+ !exceptNameClass.matchesName(uri, localName);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/AttributePattern.java b/libjava/classpath/gnu/xml/validation/relaxng/AttributePattern.java
new file mode 100644
index 000000000..54e414dc5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/AttributePattern.java
@@ -0,0 +1,52 @@
+/* AttributePattern.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG attribute element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class AttributePattern
+ extends Pattern
+{
+
+ NameClass nameClass;
+ Pattern pattern;
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/ChoiceNameClass.java b/libjava/classpath/gnu/xml/validation/relaxng/ChoiceNameClass.java
new file mode 100644
index 000000000..9c762b926
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/ChoiceNameClass.java
@@ -0,0 +1,58 @@
+/* ChoiceNameClass.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG choice element (in the context of a name class).
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class ChoiceNameClass
+ extends NameClass
+{
+
+ NameClass name1;
+ NameClass name2;
+
+ boolean matchesName(String uri, String localName)
+ {
+ return name1.matchesName(uri, localName) ||
+ name2.matchesName(uri, localName);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/ChoicePattern.java b/libjava/classpath/gnu/xml/validation/relaxng/ChoicePattern.java
new file mode 100644
index 000000000..48c96b2fb
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/ChoicePattern.java
@@ -0,0 +1,52 @@
+/* ChoicePattern.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG choice element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class ChoicePattern
+ extends Pattern
+{
+
+ Pattern pattern1;
+ Pattern pattern2;
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/DataPattern.java b/libjava/classpath/gnu/xml/validation/relaxng/DataPattern.java
new file mode 100644
index 000000000..f4e36c726
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/DataPattern.java
@@ -0,0 +1,59 @@
+/* DataPattern.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+import java.util.LinkedList;
+import java.util.List;
+import org.relaxng.datatype.Datatype;
+import org.relaxng.datatype.DatatypeLibrary;
+
+/**
+ * A RELAX NG data element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class DataPattern
+ extends Pattern
+{
+
+ Datatype type;
+ DatatypeLibrary datatypeLibrary;
+ List params = new LinkedList();
+ Pattern exceptPattern;
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/Define.java b/libjava/classpath/gnu/xml/validation/relaxng/Define.java
new file mode 100644
index 000000000..ffb1d41ee
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/Define.java
@@ -0,0 +1,51 @@
+/* Define.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG define.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class Define
+{
+
+ String name;
+ ElementPattern element;
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/ElementPattern.java b/libjava/classpath/gnu/xml/validation/relaxng/ElementPattern.java
new file mode 100644
index 000000000..e02731968
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/ElementPattern.java
@@ -0,0 +1,52 @@
+/* ElementPattern.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class ElementPattern
+ extends Pattern
+{
+
+ NameClass nameClass;
+ Pattern pattern;
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/EmptyPattern.java b/libjava/classpath/gnu/xml/validation/relaxng/EmptyPattern.java
new file mode 100644
index 000000000..13c2d3f61
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/EmptyPattern.java
@@ -0,0 +1,51 @@
+/* EmptyPattern.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG empty element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class EmptyPattern
+ extends Pattern
+{
+
+ static final EmptyPattern INSTANCE = new EmptyPattern();
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/FullSyntaxBuilder.java b/libjava/classpath/gnu/xml/validation/relaxng/FullSyntaxBuilder.java
new file mode 100644
index 000000000..bb73c4f38
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/FullSyntaxBuilder.java
@@ -0,0 +1,1650 @@
+/* FullSyntaxBuilder.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.xml.XMLConstants;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.DatatypeLibrary;
+import org.relaxng.datatype.helpers.DatatypeLibraryLoader;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+import gnu.xml.stream.XMLParser;
+
+/**
+ * Parses a RELAX NG XML DOM tree, constructing a compiled internal
+ * representation.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class FullSyntaxBuilder
+{
+
+ /**
+ * Complete vocabulary (elements and attributes) of the full syntax.
+ */
+ static final Map VOCABULARY = new HashMap();
+ static final Set STRIPPED_ATTRIBUTES = new HashSet();
+ static final Set PATTERN_ELEMENTS = new HashSet();
+ static
+ {
+ Set elementAttrs = Collections.singleton("name");
+ Set dataAttrs = new HashSet();
+ dataAttrs.add("type");
+ dataAttrs.add("datatypeLibrary");
+ Set valueAttrs = new HashSet();
+ valueAttrs.add("type");
+ valueAttrs.add("datatypeLibrary");
+ valueAttrs.add("ns");
+ Set externalAttrs = Collections.singleton("href");
+ Set startAttrs = Collections.singleton("combine");
+ Set defineAttrs = new HashSet();
+ defineAttrs.add("name");
+ defineAttrs.add("combine");
+ Set nsAttrs = Collections.singleton("ns");
+
+ VOCABULARY.put("element", elementAttrs);
+ VOCABULARY.put("attribute", elementAttrs);
+ VOCABULARY.put("group", Collections.EMPTY_SET);
+ VOCABULARY.put("interleave", Collections.EMPTY_SET);
+ VOCABULARY.put("choice", Collections.EMPTY_SET);
+ VOCABULARY.put("optional", Collections.EMPTY_SET);
+ VOCABULARY.put("zeroOrMore", Collections.EMPTY_SET);
+ VOCABULARY.put("oneOrMore", Collections.EMPTY_SET);
+ VOCABULARY.put("list", Collections.EMPTY_SET);
+ VOCABULARY.put("mixed", Collections.EMPTY_SET);
+ VOCABULARY.put("ref", elementAttrs);
+ VOCABULARY.put("parentRef", elementAttrs);
+ VOCABULARY.put("empty", Collections.EMPTY_SET);
+ VOCABULARY.put("text", Collections.EMPTY_SET);
+ VOCABULARY.put("value", valueAttrs);
+ VOCABULARY.put("data", dataAttrs);
+ VOCABULARY.put("notAllowed", Collections.EMPTY_SET);
+ VOCABULARY.put("externalRef", externalAttrs);
+ VOCABULARY.put("grammar", Collections.EMPTY_SET);
+ VOCABULARY.put("param", elementAttrs);
+ VOCABULARY.put("except", Collections.EMPTY_SET);
+ VOCABULARY.put("div", Collections.EMPTY_SET);
+ VOCABULARY.put("include", externalAttrs);
+ VOCABULARY.put("start", startAttrs);
+ VOCABULARY.put("define", defineAttrs);
+ VOCABULARY.put("name", nsAttrs);
+ VOCABULARY.put("anyName", Collections.EMPTY_SET);
+ VOCABULARY.put("nsName", nsAttrs);
+
+ STRIPPED_ATTRIBUTES.add("name");
+ STRIPPED_ATTRIBUTES.add("type");
+ STRIPPED_ATTRIBUTES.add("combine");
+
+ PATTERN_ELEMENTS.add("element");
+ PATTERN_ELEMENTS.add("attribute");
+ PATTERN_ELEMENTS.add("group");
+ PATTERN_ELEMENTS.add("interleave");
+ PATTERN_ELEMENTS.add("choice");
+ PATTERN_ELEMENTS.add("optional");
+ PATTERN_ELEMENTS.add("zeroOrMore");
+ PATTERN_ELEMENTS.add("oneOrMore");
+ PATTERN_ELEMENTS.add("list");
+ PATTERN_ELEMENTS.add("mixed");
+ PATTERN_ELEMENTS.add("ref");
+ PATTERN_ELEMENTS.add("parentRef");
+ PATTERN_ELEMENTS.add("empty");
+ PATTERN_ELEMENTS.add("text");
+ PATTERN_ELEMENTS.add("value");
+ PATTERN_ELEMENTS.add("data");
+ PATTERN_ELEMENTS.add("notAllowed");
+ PATTERN_ELEMENTS.add("externalRef");
+ PATTERN_ELEMENTS.add("grammar");
+ }
+
+ private Set urls; // recursion checking
+ private int refCount; // creation of ref names
+ private Map datatypeLibraries;
+
+ /**
+ * Parse the specified document into a grammar.
+ */
+ synchronized Grammar parse(Document doc)
+ throws IOException
+ {
+ urls = new HashSet();
+ refCount = 1;
+
+ doc.normalizeDocument(); // Normalize XML document
+ transform(doc); // Apply transformation rules to provide simple syntax
+
+ // 4.18. grammar element
+ Element p = doc.getDocumentElement();
+ Element grammar =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "grammar");
+ Element start =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "start");
+ doc.removeChild(p);
+ doc.appendChild(grammar);
+ grammar.appendChild(start);
+ start.appendChild(p);
+ transformGrammar(grammar, p);
+ Element define = getNextSiblingElement(start);
+ while (define != null)
+ {
+ Element next = getNextSiblingElement(define);
+ String name = define.getAttribute("new-name");
+ if (name != null)
+ {
+ define.setAttribute("name", name);
+ define.removeAttribute("new-name");
+ }
+ else
+ grammar.removeChild(define); // unreferenced
+ define = next;
+ }
+
+ // 4.19. define and ref elements
+ Set allDefines = new HashSet(), reachableDefines = new HashSet();
+ getDefines(allDefines, grammar, grammar, false);
+ getDefines(reachableDefines, grammar, start, true);
+ allDefines.removeAll(reachableDefines);
+ for (Iterator i = allDefines.iterator(); i.hasNext(); )
+ {
+ // remove unreachable defines
+ Element d = (Element) i.next();
+ Node parent = d.getParentNode();
+ parent.removeChild(d);
+ }
+ // replace all elements that are not children of defines by refs to new
+ // defines
+ Set elements = new HashSet();
+ getElements(elements, grammar, grammar);
+ for (Iterator i = elements.iterator(); i.hasNext(); )
+ {
+ Element element = (Element) i.next();
+ Node parent = element.getParentNode();
+ if (!reachableDefines.contains(parent))
+ {
+ define =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "define");
+ Element ref =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "ref");
+ String name = createRefName();
+ define.setAttribute("name", name);
+ ref.setAttribute("name", name);
+ parent.insertBefore(ref, element);
+ define.appendChild(element);
+ grammar.appendChild(define);
+ reachableDefines.add(define);
+ }
+ }
+ // Get defines that don't have element children
+ for (Iterator i = reachableDefines.iterator(); i.hasNext(); )
+ {
+ Element d = (Element) i.next();
+ Element child = getFirstChildElement(d);
+ if (child != null && "element".equals(child.getLocalName()))
+ i.remove();
+ }
+ // Expand refs that refer to these defines
+ expandRefs(reachableDefines, grammar);
+ // Remove any defines that don't have element children
+ for (Iterator i = reachableDefines.iterator(); i.hasNext(); )
+ {
+ Element d = (Element) i.next();
+ Node parent = d.getParentNode();
+ parent.removeChild(d);
+ }
+
+ transform2(p); // Apply second stage transformation rules
+
+ Grammar ret = parseGrammar(grammar);
+ datatypeLibraries = null; // free datatype libraries cache
+ return ret;
+ }
+
+ private void getDefines(Set defines, Element grammar, Element node,
+ boolean followRefs)
+ {
+ String elementName = node.getLocalName();
+ if ("define".equals(elementName))
+ defines.add(node);
+ else if ("ref".equals(elementName) && followRefs)
+ {
+ String rname = node.getAttribute("name");
+ Element define = getFirstChildElement(grammar);
+ define = getNextSiblingElement(define);
+ while (define != null)
+ {
+ String dname = define.getAttribute("name");
+ if (rname.equals(dname))
+ {
+ getDefines(defines, grammar, node, followRefs);
+ break;
+ }
+ define = getNextSiblingElement(define);
+ }
+ }
+ for (Element child = getFirstChildElement(node); child != null;
+ child = getNextSiblingElement(child))
+ getDefines(defines, grammar, child, followRefs);
+ }
+
+ private void getElements(Set elements, Element grammar, Element node)
+ {
+ String elementName = node.getLocalName();
+ if ("element".equals(elementName))
+ elements.add(node);
+ for (Element child = getFirstChildElement(node); child != null;
+ child = getNextSiblingElement(child))
+ getElements(elements, grammar, child);
+ }
+
+ private void expandRefs(Set defines, Element node)
+ throws GrammarException
+ {
+ String elementName = node.getLocalName();
+ if ("ref".equals(elementName))
+ {
+ String rname = node.getAttribute("name");
+ for (Iterator i = defines.iterator(); i.hasNext(); )
+ {
+ Element define = (Element) i.next();
+ String dname = define.getAttribute("name");
+ if (rname.equals(dname))
+ {
+ Element child = getFirstChildElement(define);
+ forbidRefs(child, rname);
+ Element refChild = (Element) child.cloneNode(true);
+ Node parent = node.getParentNode();
+ parent.insertBefore(refChild, node);
+ parent.removeChild(node);
+ node = refChild;
+ break;
+ }
+ }
+ }
+ for (Element child = getFirstChildElement(node); child != null;
+ child = getNextSiblingElement(child))
+ expandRefs(defines, child);
+ }
+
+ private void forbidRefs(Element node, String name)
+ throws GrammarException
+ {
+ String elementName = node.getLocalName();
+ if ("ref".equals(elementName))
+ {
+ String rname = node.getAttribute("name");
+ if (name.equals(rname))
+ throw new GrammarException("cannot expand ref with name '" + name +
+ "' due to circularity");
+ }
+ for (Element child = getFirstChildElement(node); child != null;
+ child = getNextSiblingElement(child))
+ forbidRefs(child, name);
+ }
+
+ private void transform(Node node)
+ throws IOException
+ {
+ Node parent = node.getParentNode();
+ switch (node.getNodeType())
+ {
+ case Node.ELEMENT_NODE:
+ // 4.1 Annotations
+ String elementNs = node.getNamespaceURI();
+ String elementName = node.getLocalName();
+ if (!XMLConstants.RELAXNG_NS_URI.equals(elementNs) ||
+ !VOCABULARY.containsKey(elementName))
+ parent.removeChild(node);
+ else
+ {
+ Set allowedAttrs = (Set) VOCABULARY.get(elementName);
+ NamedNodeMap attrs = node.getAttributes();
+ int len = attrs.getLength();
+ for (int i = len - 1; i >= 0; i--)
+ {
+ Node attr = attrs.item(i);
+ String attrNs = attr.getNamespaceURI();
+ String attrName = attr.getLocalName();
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attrNs))
+ continue; // ignore namespace nodes
+ if (!(XMLConstants.RELAXNG_NS_URI.equals(attrNs) ||
+ attrNs == null) ||
+ !allowedAttrs.contains(attrName))
+ attrs.removeNamedItemNS(attrNs, attrName);
+ else
+ {
+ // 4.2 Whitespace
+ if (STRIPPED_ATTRIBUTES.contains(attrName))
+ attr.setNodeValue(attr.getNodeValue().trim());
+ // 4.3 datatypeLibrary attribute
+ else if ("datatypeLibrary".equals(attrName))
+ {
+ String dl = attr.getNodeValue();
+ attr.setNodeValue(escapeURL(dl));
+ }
+ // 4.5. href attribute
+ else if ("href".equals(attrName))
+ {
+ String href = attr.getNodeValue();
+ href = XMLParser.absolutize(node.getBaseURI(),
+ escapeURL(href));
+ attr.setNodeValue(href);
+ }
+ }
+ }
+ // 4.3 datatypeLibrary attribute
+ if ("data".equals(elementName) || "value".equals(elementName))
+ {
+ Element element = (Element) node;
+ String dl = element.getAttribute("datatypeLibrary");
+ if (dl == null)
+ {
+ Node p = parent;
+ while (dl == null && p != null &&
+ p.getNodeType() == Node.ELEMENT_NODE)
+ {
+ dl = ((Element) p)
+ .getAttribute("datatypeLibrary");
+ p = p.getParentNode();
+ }
+ if (dl == null)
+ dl = "";
+ element.setAttribute("datatypeLibrary", dl);
+ }
+ // 4.4. type attribute of value element
+ if ("value".equals(elementName))
+ {
+ String type = element.getAttribute("type");
+ if (type == null)
+ {
+ element.setAttribute("type", "token");
+ element.setAttribute("datatypeLibrary", "");
+ }
+ }
+ // 4.16. Constraints
+ // TODO validate type
+ }
+ // 4.6. externalRef element
+ else if ("externalRef".equals(elementName))
+ {
+ Element externalRef = (Element) node;
+ String href = externalRef.getAttribute("href");
+ // check for recursion
+ if (urls.contains(href))
+ throw new GrammarException("recursive href");
+ urls.add(href);
+ Element element = resolve(href);
+ String eNs = element.getNamespaceURI();
+ String eName = element.getLocalName();
+ if (!(XMLConstants.RELAXNG_NS_URI.equals(eNs) ||
+ eNs == null) ||
+ !PATTERN_ELEMENTS.contains(eName))
+ throw new GrammarException("externally referenced element " +
+ "is not a pattern");
+ transform(element);
+ urls.remove(href);
+ String ns = element.getAttribute("ns");
+ if (ns != null)
+ element.setAttribute("ns",
+ externalRef.getAttribute("ns"));
+ element = (Element) externalRef.getOwnerDocument()
+ .importNode(element, true);
+ parent.replaceChild(element, externalRef);
+ return;
+ }
+ // 4.7 include element
+ else if ("include".equals(elementName))
+ {
+ Element include = (Element) node;
+ String href = include.getAttribute("href");
+ // check for recursion
+ if (urls.contains(href))
+ throw new GrammarException("recursive href");
+ urls.add(href);
+ Element element = resolve(href);
+ String eNs = element.getNamespaceURI();
+ String eName = element.getLocalName();
+ if (!(XMLConstants.RELAXNG_NS_URI.equals(eNs) ||
+ eNs == null) ||
+ !"grammar".equals(eName))
+ throw new GrammarException("included element is not " +
+ "a grammar");
+
+ transform(element);
+ urls.remove(href);
+ // handle components
+ List includeComponents = getComponents(include);
+ List grammarComponents = getComponents(element);
+ for (Iterator i = includeComponents.iterator(); i.hasNext(); )
+ {
+ Element comp = (Element) i.next();
+ String compName = comp.getLocalName();
+ if ("start".equals(compName))
+ {
+ boolean found = false;
+ for (Iterator j = grammarComponents.iterator();
+ j.hasNext(); )
+ {
+ Element c2 = (Element) j.next();
+ if ("start".equals(c2.getLocalName()))
+ {
+ c2.getParentNode().removeChild(c2);
+ found = true;
+ }
+ }
+ if (!found)
+ throw new GrammarException("no start component in " +
+ "included grammar");
+ }
+ else if ("define".equals(compName))
+ {
+ String name = comp.getAttribute("name");
+ boolean found = false;
+ for (Iterator j = grammarComponents.iterator();
+ j.hasNext(); )
+ {
+ Element c2 = (Element) j.next();
+ if ("define".equals(c2.getLocalName()) &&
+ name.equals(c2.getAttribute("name")))
+ {
+ c2.getParentNode().removeChild(c2);
+ found = true;
+ }
+ }
+ if (!found)
+ throw new GrammarException("no define component " +
+ "with name '" + name +
+ "' in included grammar");
+ }
+ }
+ // transform to div element
+ Document doc = include.getOwnerDocument();
+ Element includeDiv =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "div");
+ Element grammarDiv =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "div");
+ // XXX copy include non-href attributes (none defined?)
+ element = (Element) doc.importNode(element, true);
+ Node ctx = element.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ grammarDiv.appendChild(ctx);
+ ctx = next;
+ }
+ includeDiv.appendChild(grammarDiv);
+ ctx = include.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ includeDiv.appendChild(ctx);
+ ctx = next;
+ }
+ parent.replaceChild(includeDiv, include);
+ transform(includeDiv);
+ return;
+ }
+ // 4.8. name attribute of element and attribute elements
+ else if ("attribute".equals(elementName) ||
+ "element".equals(elementName))
+ {
+ Element element = (Element) node;
+ String name = element.getAttribute("name");
+ if (name != null)
+ {
+ Document doc = element.getOwnerDocument();
+ Element n =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI, "name");
+ n.appendChild(doc.createTextNode(name));
+ Node first = element.getFirstChild();
+ if (first != null)
+ element.insertBefore(n, first);
+ else
+ element.appendChild(n);
+ if ("attribute".equals(elementName))
+ {
+ String ns = element.getAttribute("ns");
+ if (ns != null)
+ {
+ n.setAttribute("ns", ns);
+ element.removeAttribute("ns");
+ }
+ }
+ element.removeAttribute("name");
+ }
+ // 4.12. Number of child elements
+ if ("attribute".equals(elementName))
+ {
+ if (getComponents(node).size() == 1)
+ {
+ Document doc = node.getOwnerDocument();
+ Element text =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI,
+ "text");
+ node.appendChild(text);
+ }
+ }
+ else // element
+ {
+ if (node.getChildNodes().getLength() > 2)
+ {
+ // transform to 2 child elements
+ Document doc = node.getOwnerDocument();
+ Element child =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI,
+ "group");
+ Node ctx = getFirstChildElement(node);
+ ctx = getNextSiblingElement(ctx); // skip 1
+ while (ctx != null)
+ {
+ Node next = getNextSiblingElement(ctx);
+ child.appendChild(ctx);
+ ctx = next;
+ }
+ node.appendChild(child);
+ }
+ }
+ }
+ // 4.11. div element
+ else if ("div".equals(elementName))
+ {
+ Node ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ parent.insertBefore(ctx, node);
+ transform(ctx);
+ ctx = next;
+ }
+ parent.removeChild(node);
+ return;
+ }
+ else if ("mixed".equals(elementName))
+ {
+ // 4.12. Number of child elements
+ transformToOneChildElement(node, "group");
+ // 4.13. mixed element
+ Document doc = node.getOwnerDocument();
+ Node interleave =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI,
+ "interleave");
+ Node ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ interleave.appendChild(ctx);
+ ctx = next;
+ }
+ Node text =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI,
+ "text");
+ interleave.appendChild(text);
+ parent.insertBefore(interleave, node);
+ parent.removeChild(node);
+ node = interleave;
+ }
+ else if ("optional".equals(elementName))
+ {
+ // 4.12. Number of child elements
+ transformToOneChildElement(node, "group");
+ // 4.14. optional element
+ Document doc = node.getOwnerDocument();
+ Node choice =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI,
+ "choice");
+ Node ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ choice.appendChild(ctx);
+ ctx = next;
+ }
+ Node empty =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI,
+ "empty");
+ choice.appendChild(empty);
+ parent.insertBefore(choice, node);
+ parent.removeChild(node);
+ node = choice;
+ }
+ else if ("zeroOrMore".equals(elementName))
+ {
+ // 4.12. Number of child elements
+ transformToOneChildElement(node, "group");
+ // 4.15. zeroOrMore element
+ Document doc = node.getOwnerDocument();
+ Node choice =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI,
+ "choice");
+ Node oneOrMore =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI,
+ "oneOrMore");
+ Node ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ oneOrMore.appendChild(ctx);
+ ctx = next;
+ }
+ Node empty =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI,
+ "empty");
+ choice.appendChild(oneOrMore);
+ choice.appendChild(empty);
+ parent.insertBefore(choice, node);
+ parent.removeChild(node);
+ node = choice;
+ }
+ else if ("list".equals(elementName) ||
+ "oneOrMore".equals(elementName) ||
+ "define".equals(elementName))
+ {
+ // 4.12. Number of child elements
+ transformToOneChildElement(node, "group");
+ }
+ else if ("except".equals(elementName))
+ {
+ // 4.12. Number of child elements
+ transformToOneChildElement(node, "choice");
+ // 4.16. Constraints
+ String parentName = parent.getLocalName();
+ if ("anyName".equals(parentName))
+ forbidDescendants(node, Collections.singleton("anyName"));
+ else if ("nsName".equals(parentName))
+ {
+ Set names = new HashSet();
+ names.add("nsName");
+ names.add("anyName");
+ forbidDescendants(node, names);
+ }
+ }
+ else if ("choice".equals(elementName) ||
+ "group".equals(elementName) ||
+ "interleave".equals(elementName))
+ {
+ // 4.12. Number of child elements
+ Node ctx = getFirstChildElement(node);
+ Node next = getNextSiblingElement(ctx);
+ if (next == null)
+ {
+ // replace
+ parent.insertBefore(ctx, node);
+ parent.removeChild(node);
+ transform(ctx);
+ return;
+ }
+ else
+ {
+ // transform to 2 child elements
+ Node next2 = getNextSiblingElement(next);
+ if (next2 != null)
+ {
+ Document doc = node.getOwnerDocument();
+ Node child =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI,
+ elementName);
+ child.appendChild(ctx);
+ child.appendChild(next);
+ node.insertBefore(next2, child);
+ transform(node); // recurse
+ }
+ }
+ }
+ // 4.17. combine attribute
+ else if ("grammar".equals(elementName))
+ {
+ String combine = null;
+ List nodes = new LinkedList();
+ Node ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ if ("start".equals(ctx.getLocalName()))
+ {
+ String c = ((Element) ctx).getAttribute("combine");
+ if (combine != null && !combine.equals(c))
+ throw new GrammarException("multiple start elements "+
+ "but no combine attribute");
+ combine = c;
+ nodes.add(ctx);
+ }
+ ctx = next;
+ }
+ if (!nodes.isEmpty())
+ combineNodes(node, combine, "start", nodes);
+ // defines
+ Map defines = new HashMap();
+ Map defineCombines = new HashMap();
+ ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ if ("define".equals(ctx.getLocalName()))
+ {
+ String name = ((Element) ctx).getAttribute("name");
+ combine = (String) defineCombines.get(name);
+ String c = ((Element) ctx).getAttribute("combine");
+ if (combine != null && !combine.equals(c))
+ throw new GrammarException("multiple define " +
+ "elements with name '"+
+ name + "' but no " +
+ "combine attribute");
+ defineCombines.put(name, c);
+ nodes = (List) defines.get(name);
+ if (nodes == null)
+ {
+ nodes = new LinkedList();
+ defines.put(name, nodes);
+ }
+ nodes.add(ctx);
+ }
+ ctx = next;
+ }
+ for (Iterator i = defines.keySet().iterator(); i.hasNext(); )
+ {
+ String name = (String) i.next();
+ combine = (String) defineCombines.get(name);
+ nodes = (List) defines.get(name);
+ if (!nodes.isEmpty())
+ combineNodes(node, combine, "define", nodes);
+ }
+ }
+ // 4.9. ns attribute
+ if ("name".equals(elementName) ||
+ "nsName".equals(elementName) ||
+ "value".equals(elementName))
+ {
+ Element element = (Element) node;
+ String ns = element.getAttribute("ns");
+ if (ns == null)
+ {
+ Node ctx = parent;
+ while (ns == null && ctx != null &&
+ ctx.getNodeType() == Node.ELEMENT_NODE)
+ {
+ ns = ((Element) ctx).getAttribute("ns");
+ ctx = ctx.getParentNode();
+ }
+ element.setAttribute("ns", (ns == null) ? "" : ns);
+ }
+ if ("name".equals(elementName))
+ {
+ // 4.10. QNames
+ String name = element.getTextContent();
+ int ci = name.indexOf(':');
+ if (ci != -1)
+ {
+ String prefix = name.substring(0, ci);
+ element.setTextContent(name.substring(ci + 1));
+ ns = element.lookupNamespaceURI(prefix);
+ element.setAttribute("ns", (ns == null) ? "" : ns);
+ }
+ // 4.16. Constraints
+ if (isDescendantOfFirstChildOfAttribute(element) &&
+ "".equals(element.getAttribute("ns")) &&
+ "xmlns".equals(element.getTextContent()))
+ throw new GrammarException("name cannot be xmlns");
+ }
+ else if ("nsName".equals(elementName))
+ {
+ // 4.16. Constraints
+ if (isDescendantOfFirstChildOfAttribute(element) &&
+ "http://www.w3.org/2000/xmlns"
+ .equals(element.getAttribute("ns")))
+ throw new GrammarException("nsName cannot be XMLNS URI");
+ }
+ }
+ }
+
+ break;
+ case Node.TEXT_NODE:
+ case Node.CDATA_SECTION_NODE:
+ // 4.2 Whitespace
+ String parentName = parent.getLocalName();
+ if ("name".equals(parentName))
+ node.setNodeValue(node.getNodeValue().trim());
+ if (!"param".equals(parentName) &&
+ !"value".equals(parentName) &&
+ isWhitespace(node.getNodeValue()))
+ parent.removeChild(node);
+ break;
+ case Node.DOCUMENT_NODE:
+ break;
+ default:
+ parent.removeChild(node);
+ }
+ // Transform children
+ Node ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ transform(ctx);
+ ctx = next;
+ }
+ }
+
+ /**
+ * Transforms the schema to place all defines under the top-level grammar
+ * element and replace all other grammar elements by their start child.
+ */
+ private void transformGrammar(Node grammar, Node node)
+ throws GrammarException
+ {
+ if (node.getNodeType() == Node.ELEMENT_NODE)
+ {
+ String elementName = node.getLocalName();
+ if ("grammar".equals(elementName))
+ {
+ handleRefs(grammar, node, node);
+ Node start = null;
+ Node ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ String childName = ctx.getLocalName();
+ if ("define".equals(childName))
+ grammar.appendChild(ctx);
+ else if ("start".equals(childName))
+ start = ctx;
+ ctx = next;
+ }
+ if (start == null)
+ throw new GrammarException("no start element for grammar");
+ Node p = getFirstChildElement(start);
+ Node parent = node.getParentNode();
+ parent.insertBefore(p, node);
+ parent.removeChild(node);
+ node = p;
+ }
+ Node ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ transformGrammar(grammar, ctx);
+ ctx = next;
+ }
+ }
+ }
+
+ /**
+ * Checks that all references in the specified grammar match a define in
+ * the grammar.
+ */
+ private void handleRefs(Node grammar1, Node grammar2, Node node)
+ throws GrammarException
+ {
+ if (node.getNodeType() == Node.ELEMENT_NODE)
+ {
+ String elementName = node.getLocalName();
+ if ("ref".equals(elementName) || "parentRef".equals(elementName))
+ {
+ Node grammar = grammar2;
+ if ("parentRef".equals(elementName))
+ grammar = grammar1;
+
+ String name = ((Element) node).getAttribute("name");
+ if (name != null)
+ throw new GrammarException("no name attribute on " +
+ elementName);
+ Node define = null;
+ for (Node ctx = grammar.getFirstChild();
+ define == null && ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if ("define".equals(ctx.getLocalName()))
+ {
+ String dname = ((Element) ctx).getAttribute("name");
+ if (name.equals(dname))
+ define = ctx;
+ }
+ }
+ if (define == null)
+ throw new GrammarException("no define for '" + name + "'");
+ name = ((Element) define).getAttribute("new-name");
+ if (name == null)
+ {
+ name = createRefName();
+ ((Element) define).setAttribute("new-name", name);
+ }
+ if ("parentRef".equals(elementName))
+ {
+ Document doc = node.getOwnerDocument();
+ Node ref = doc.createElementNS(XMLConstants.RELAXNG_NS_URI,
+ "ref");
+ Node ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ ref.appendChild(ctx);
+ ctx = next;
+ }
+ Node parent = node.getParentNode();
+ parent.insertBefore(ref, node);
+ parent.removeChild(node);
+ node = ref;
+ }
+ ((Element) node).setAttribute("name", name);
+ }
+ else if ("grammar".equals(elementName))
+ {
+ grammar1 = grammar2;
+ grammar2 = node;
+ }
+ Node ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ handleRefs(grammar1, grammar2, ctx);
+ ctx = next;
+ }
+ }
+ }
+
+ private String createRefName()
+ {
+ return "ref" + Integer.toString(refCount++);
+ }
+
+ private void transform2(Node node)
+ throws GrammarException
+ {
+ Node parent = node.getParentNode();
+ if (node.getNodeType() == Node.ELEMENT_NODE)
+ {
+ String elementName = node.getLocalName();
+ // 4.20. notAllowed element
+ if ("notAllowed".equals(elementName))
+ {
+ String parentName = parent.getLocalName();
+ if ("attribute".equals(parentName) ||
+ "list".equals(parentName) ||
+ "group".equals(parentName) ||
+ "interleave".equals(parentName) ||
+ "oneOrMore".equals(parentName))
+ {
+ Node pp = parent.getParentNode();
+ pp.insertBefore(node, parent);
+ pp.removeChild(parent);
+ transform2(node); // apply recursively
+ return;
+ }
+ else if ("choice".equals(parentName))
+ {
+ Node p1 = getFirstChildElement(parent);
+ Node p2 = getNextSiblingElement(p1);
+ if (p1 == null || p2 == null)
+ throw new GrammarException("choice does not have two " +
+ "children");
+ String p1Name = p1.getLocalName();
+ String p2Name = p2.getLocalName();
+ Node pp = parent.getParentNode();
+ if ("notAllowed".equals(p1Name) &&
+ "notAllowed".equals(p2Name))
+ {
+ pp.insertBefore(p1, parent);
+ pp.removeChild(parent);
+ transform2(p1); //apply recursively
+ return;
+ }
+ else if ("notAllowed".equals(p1Name))
+ {
+ pp.insertBefore(p2, parent);
+ pp.removeChild(parent);
+ transform2(p2);
+ return;
+ }
+ else
+ {
+ pp.insertBefore(p1, parent);
+ pp.removeChild(parent);
+ transform2(p1);
+ return;
+ }
+ }
+ else if ("except".equals(parentName))
+ {
+ Node pp = parent.getParentNode();
+ pp.removeChild(parent);
+ return;
+ }
+ }
+ // 4.21. empty element
+ else if ("empty".equals(elementName))
+ {
+ String parentName = parent.getLocalName();
+ if ("group".equals(parentName) ||
+ "interleave".equals(parentName))
+ {
+ Node p1 = getFirstChildElement(parent);
+ Node p2 = getNextSiblingElement(p1);
+ if (p1 == null || p2 == null)
+ throw new GrammarException(parentName + " does not have " +
+ "two children");
+ String p1Name = p1.getLocalName();
+ String p2Name = p2.getLocalName();
+ Node pp = parent.getParentNode();
+ if ("empty".equals(p1Name) &&
+ "empty".equals(p2Name))
+ {
+ pp.insertBefore(p1, parent);
+ pp.removeChild(parent);
+ transform2(p1);
+ return;
+ }
+ else if ("empty".equals(p1Name))
+ {
+ pp.insertBefore(p2, parent);
+ pp.removeChild(parent);
+ transform2(p2);
+ return;
+ }
+ else
+ {
+ pp.insertBefore(p1, parent);
+ pp.removeChild(parent);
+ transform2(p1);
+ return;
+ }
+ }
+ else if ("choice".equals(parentName))
+ {
+ Node p1 = getFirstChildElement(parent);
+ Node p2 = getNextSiblingElement(p1);
+ if (p1 == null || p2 == null)
+ throw new GrammarException(parentName + " does not have " +
+ "two children");
+ String p1Name = p1.getLocalName();
+ String p2Name = p2.getLocalName();
+ Node pp = parent.getParentNode();
+ if ("empty".equals(p1Name) &&
+ "empty".equals(p2Name))
+ {
+ pp.insertBefore(p1, parent);
+ pp.removeChild(parent);
+ transform2(p1);
+ return;
+ }
+ }
+ else if ("oneOrMore".equals(parentName))
+ {
+ Node pp = parent.getParentNode();
+ pp.insertBefore(node, parent);
+ pp.removeChild(parent);
+ transform2(node);
+ return;
+ }
+ }
+ Node ctx = node.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ transform2(ctx);
+ ctx = next;
+ }
+ }
+ }
+
+ private static boolean isWhitespace(String text)
+ {
+ int len = text.length();
+ for (int i = 0; i < len; i++)
+ {
+ char c = text.charAt(i);
+ if (c != ' ' && c != '\t' && c != '\n' && c != '\r')
+ return false;
+ }
+ return true;
+ }
+
+ private static String escapeURL(String url)
+ {
+ try
+ {
+ return URLEncoder.encode(url, "UTF-8");
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ RuntimeException e2 = new RuntimeException("UTF-8 is unsupported");
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ /**
+ * Resolve a URL to an element, as described in section 4.5.
+ */
+ private static Element resolve(String url)
+ throws IOException
+ {
+ try
+ {
+ URL u = new URL(url);
+ InputStream in = u.openStream();
+ DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
+ f.setNamespaceAware(true);
+ f.setCoalescing(true);
+ f.setExpandEntityReferences(true);
+ f.setIgnoringComments(true);
+ f.setIgnoringElementContentWhitespace(true);
+ DocumentBuilder b = f.newDocumentBuilder();
+ Document doc = b.parse(in, url);
+ in.close();
+ String fragment = u.getRef();
+ if (fragment != null)
+ return doc.getElementById(fragment);
+ return doc.getDocumentElement();
+ }
+ catch (SAXException e)
+ {
+ IOException e2 = new IOException("error parsing included element");
+ e2.initCause(e);
+ throw e2;
+ }
+ catch (ParserConfigurationException e)
+ {
+ IOException e2 = new IOException("error parsing included element");
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ /**
+ * Returns the "components" of an element, as described in section 4.7.
+ */
+ private List getComponents(Node node)
+ {
+ List ret = new LinkedList();
+ for (Node ctx = node.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if (ctx.getNodeType() != Node.ELEMENT_NODE)
+ continue;
+ String ns = ctx.getNamespaceURI();
+ if (ns != null && !ns.equals(XMLConstants.RELAXNG_NS_URI))
+ continue;
+ String name = ctx.getLocalName();
+ if ("div".equals(name))
+ ret.addAll(getComponents(ctx));
+ else if (VOCABULARY.containsKey(name))
+ ret.add(ctx);
+ }
+ return ret;
+ }
+
+ private static void transformToOneChildElement(Node node, String name)
+ {
+ if (node.getChildNodes().getLength() < 2)
+ return;
+ Document doc = node.getOwnerDocument();
+ Element child = doc.createElementNS(XMLConstants.RELAXNG_NS_URI, name);
+ Node ctx = getFirstChildElement(node);
+ while (ctx != null)
+ {
+ Node next = getNextSiblingElement(ctx);
+ child.appendChild(ctx);
+ ctx = next;
+ }
+ node.appendChild(child);
+ }
+
+ private static Element getFirstChildElement(Node node)
+ {
+ Node ctx = node.getFirstChild();
+ while (ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE)
+ ctx = ctx.getNextSibling();
+ return (Element) ctx;
+ }
+
+ private static Element getNextSiblingElement(Node node)
+ {
+ Node ctx = node.getNextSibling();
+ while (ctx != null && ctx.getNodeType() != Node.ELEMENT_NODE)
+ ctx = ctx.getNextSibling();
+ return (Element) ctx;
+ }
+
+ private static void forbidDescendants(Node node, Set names)
+ throws GrammarException
+ {
+ for (Node ctx = node.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ String ns = ctx.getNamespaceURI();
+ if (!XMLConstants.RELAXNG_NS_URI.equals(ns))
+ continue;
+ String name = ctx.getLocalName();
+ if (names.contains(name))
+ throw new GrammarException("name not allowed: " + name);
+ forbidDescendants(ctx, names);
+ }
+ }
+
+ private static boolean isDescendantOfFirstChildOfAttribute(Node node)
+ {
+ Node child = node;
+ Node parent = node.getParentNode();
+ while (parent != null && !"attribute".equals(parent.getLocalName()))
+ {
+ child = parent;
+ parent = child.getParentNode();
+ }
+ if (parent == null)
+ return false;
+ Node firstChild = getFirstChildElement(parent);
+ return firstChild == child;
+ }
+
+ private static void combineNodes(Node node, String combine, String name,
+ List nodes)
+ {
+ Document doc = node.getOwnerDocument();
+ Node child =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI, name);
+ Node combineNode =
+ doc.createElementNS(XMLConstants.RELAXNG_NS_URI, combine);
+ child.appendChild(combineNode);
+ boolean inserted = false;
+ for (Iterator i = nodes.iterator(); i.hasNext(); )
+ {
+ Node startNode = (Node) i.next();
+ if (!inserted)
+ {
+ node.insertBefore(child, startNode);
+ inserted = true;
+ }
+ Node ctx = startNode.getFirstChild();
+ while (ctx != null)
+ {
+ Node next = ctx.getNextSibling();
+ combineNode.appendChild(ctx);
+ ctx = next;
+ }
+ node.removeChild(startNode);
+ }
+ }
+
+ Grammar parseGrammar(Element node)
+ throws GrammarException
+ {
+ checkName(node, "grammar");
+ Grammar grammar = new Grammar();
+ Element start = getFirstChildElement(node);
+ grammar.start = parsePattern(getFirstChildElement(start));
+ for (Element define = getNextSiblingElement(start); define != null;
+ define = getNextSiblingElement(define))
+ grammar.defines.add(parseDefine(define));
+ return grammar;
+ }
+
+ Define parseDefine(Element node)
+ throws GrammarException
+ {
+ checkName(node, "define");
+ Define define = new Define();
+ define.name = node.getAttribute("name");
+ define.element = parseElement(getFirstChildElement(node));
+ return define;
+ }
+
+ Pattern parseTop(Element node)
+ throws GrammarException
+ {
+ String name = node.getLocalName();
+ if ("notAllowed".equals(name))
+ return parseNotAllowed(node);
+ return parsePattern(node);
+ }
+
+ Pattern parsePattern(Element node)
+ throws GrammarException
+ {
+ String name = node.getLocalName();
+ if ("empty".equals(name))
+ return parseEmpty(node);
+ return parseNonEmptyPattern(node);
+ }
+
+ Pattern parseNonEmptyPattern(Element node)
+ throws GrammarException
+ {
+ String name = node.getLocalName();
+ if ("text".equals(name))
+ return parseText(node);
+ else if ("data".equals(name))
+ return parseData(node);
+ else if ("value".equals(name))
+ return parseValue(node);
+ else if ("list".equals(name))
+ return parseList(node);
+ else if ("attribute".equals(name))
+ return parseAttribute(node);
+ else if ("ref".equals(name))
+ return parseRef(node);
+ else if ("oneOrMore".equals(name))
+ return parseOneOrMore(node);
+ else if ("choice".equals(name))
+ return parseChoice(node);
+ else if ("group".equals(name))
+ return parseGroup(node);
+ else if ("interleave".equals(name))
+ return parseInterleave(node);
+ throw new GrammarException("invalid pattern: " + name);
+ }
+
+ ElementPattern parseElement(Element node)
+ throws GrammarException
+ {
+ checkName(node, "element");
+ ElementPattern element = new ElementPattern();
+ Element nameClass = getFirstChildElement(node);
+ element.nameClass = parseNameClass(nameClass);
+ element.pattern = parseTop(getNextSiblingElement(nameClass));
+ return element;
+ }
+
+ NotAllowedPattern parseNotAllowed(Element node)
+ throws GrammarException
+ {
+ checkName(node, "notAllowed");
+ return NotAllowedPattern.INSTANCE;
+ }
+
+ EmptyPattern parseEmpty(Element node)
+ throws GrammarException
+ {
+ checkName(node, "empty");
+ return EmptyPattern.INSTANCE;
+ }
+
+ TextPattern parseText(Element node)
+ throws GrammarException
+ {
+ checkName(node, "text");
+ return TextPattern.INSTANCE;
+ }
+
+ DataPattern parseData(Element node)
+ throws GrammarException
+ {
+ checkName(node, "data");
+ DataPattern data = new DataPattern();
+ DatatypeLibrary dl =
+ getDatatypeLibrary(node.getAttribute("datatypeLibrary"));
+ String type = node.getAttribute("type");
+ try
+ {
+ data.type = dl.createDatatype(type);
+ data.datatypeLibrary = dl;
+ }
+ catch (DatatypeException e)
+ {
+ GrammarException e2 = new GrammarException(type);
+ e2.initCause(e);
+ throw e2;
+ }
+ Element ctx = getFirstChildElement(node);
+ while (ctx != null)
+ {
+ Element next = getNextSiblingElement(ctx);
+ String name = ctx.getLocalName();
+ if ("param".equals(name))
+ data.params.add(parseParam(ctx));
+ else if ("except".equals(name) && next == null)
+ data.exceptPattern = parsePattern(getFirstChildElement(ctx));
+ else
+ throw new GrammarException("invalid element: " + name);
+ ctx = next;
+ }
+ return data;
+ }
+
+ Param parseParam(Element node)
+ throws GrammarException
+ {
+ checkName(node, "param");
+ Param param = new Param();
+ param.name = node.getAttribute("name");
+ param.value = node.getTextContent();
+ return param;
+ }
+
+ ValuePattern parseValue(Element node)
+ throws GrammarException
+ {
+ checkName(node, "value");
+ ValuePattern value = new ValuePattern();
+ DatatypeLibrary dl =
+ getDatatypeLibrary(node.getAttribute("datatypeLibrary"));
+ String type = node.getAttribute("type");
+ try
+ {
+ value.type = dl.createDatatype(type);
+ value.datatypeLibrary = dl;
+ }
+ catch (DatatypeException e)
+ {
+ GrammarException e2 = new GrammarException(type);
+ e2.initCause(e);
+ throw e2;
+ }
+ value.ns = node.getAttribute("ns");
+ value.value = node.getTextContent();
+ return value;
+ }
+
+ ListPattern parseList(Element node)
+ throws GrammarException
+ {
+ checkName(node, "list");
+ ListPattern list = new ListPattern();
+ list.pattern = parsePattern(getFirstChildElement(node));
+ return list;
+ }
+
+ AttributePattern parseAttribute(Element node)
+ throws GrammarException
+ {
+ checkName(node, "attribute");
+ AttributePattern attribute = new AttributePattern();
+ Element nameClass = getFirstChildElement(node);
+ attribute.nameClass = parseNameClass(nameClass);
+ attribute.pattern = parsePattern(getNextSiblingElement(nameClass));
+ return attribute;
+ }
+
+ RefPattern parseRef(Element node)
+ throws GrammarException
+ {
+ checkName(node, "ref");
+ RefPattern ref = new RefPattern();
+ ref.name = node.getAttribute("name");
+ return ref;
+ }
+
+ OneOrMorePattern parseOneOrMore(Element node)
+ throws GrammarException
+ {
+ checkName(node, "oneOrMore");
+ OneOrMorePattern oneOrMore = new OneOrMorePattern();
+ oneOrMore.pattern = parseNonEmptyPattern(getFirstChildElement(node));
+ return oneOrMore;
+ }
+
+ ChoicePattern parseChoice(Element node)
+ throws GrammarException
+ {
+ checkName(node, "choice");
+ ChoicePattern choice = new ChoicePattern();
+ Element p1 = getFirstChildElement(node);
+ Element p2 = getNextSiblingElement(p1);
+ choice.pattern1 = parsePattern(p1);
+ choice.pattern2 = parseNonEmptyPattern(p2);
+ return choice;
+ }
+
+ GroupPattern parseGroup(Element node)
+ throws GrammarException
+ {
+ checkName(node, "group");
+ GroupPattern group = new GroupPattern();
+ Element p1 = getFirstChildElement(node);
+ Element p2 = getNextSiblingElement(p1);
+ group.pattern1 = parseNonEmptyPattern(p1);
+ group.pattern2 = parseNonEmptyPattern(p2);
+ return group;
+ }
+
+ InterleavePattern parseInterleave(Element node)
+ throws GrammarException
+ {
+ checkName(node, "interleave");
+ InterleavePattern interleave = new InterleavePattern();
+ Element p1 = getFirstChildElement(node);
+ Element p2 = getNextSiblingElement(p1);
+ interleave.pattern1 = parseNonEmptyPattern(p1);
+ interleave.pattern2 = parseNonEmptyPattern(p2);
+ return interleave;
+ }
+
+ NameClass parseNameClass(Element node)
+ throws GrammarException
+ {
+ String name = node.getLocalName();
+ if ("anyName".equals(name))
+ return parseAnyName(node);
+ else if ("name".equals(name))
+ return parseName(node);
+ else if ("nsName".equals(name))
+ return parseNsName(node);
+ else if ("choice".equals(name))
+ return parseChoiceNameClass(node);
+ throw new GrammarException("invalid name class: " + name);
+ }
+
+ AnyNameNameClass parseAnyName(Element node)
+ throws GrammarException
+ {
+ checkName(node, "anyName");
+ AnyNameNameClass anyName = new AnyNameNameClass();
+ Element except = getFirstChildElement(node);
+ if (except != null) {
+ checkName(except, "except");
+ anyName.exceptNameClass = parseNameClass(getFirstChildElement(except));
+ }
+ return anyName;
+ }
+
+ NameNameClass parseName(Element node)
+ throws GrammarException
+ {
+ checkName(node, "name");
+ NameNameClass name = new NameNameClass();
+ name.ns = node.getAttribute("ns");
+ name.name = node.getTextContent();
+ return name;
+ }
+
+ NSNameNameClass parseNsName(Element node)
+ throws GrammarException
+ {
+ checkName(node, "nsName");
+ NSNameNameClass nsName = new NSNameNameClass();
+ nsName.ns = node.getAttribute("ns");
+ Element except = getFirstChildElement(node);
+ if (except != null) {
+ checkName(except, "except");
+ nsName.exceptNameClass = parseNameClass(getFirstChildElement(except));
+ }
+ return nsName;
+ }
+
+ ChoiceNameClass parseChoiceNameClass(Element node)
+ throws GrammarException
+ {
+ checkName(node, "choice");
+ ChoiceNameClass choice = new ChoiceNameClass();
+ Element c1 = getFirstChildElement(node);
+ Element c2 = getNextSiblingElement(c1);
+ choice.name1 = parseNameClass(c1);
+ choice.name2 = parseNameClass(c2);
+ return choice;
+ }
+
+ void checkName(Element node, String name)
+ throws GrammarException
+ {
+ if (!name.equals(node.getLocalName()))
+ throw new GrammarException("expecting " + name);
+ }
+
+ DatatypeLibrary getDatatypeLibrary(String uri)
+ throws GrammarException
+ {
+ if (datatypeLibraries == null)
+ datatypeLibraries = new HashMap();
+ DatatypeLibrary library = (DatatypeLibrary) datatypeLibraries.get(uri);
+ if (library == null)
+ {
+ library = new DatatypeLibraryLoader().createDatatypeLibrary(uri);
+ if (library == null)
+ throw new GrammarException("Datatype library not supported: " + uri);
+ datatypeLibraries.put(uri, library);
+ }
+ return library;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/Grammar.java b/libjava/classpath/gnu/xml/validation/relaxng/Grammar.java
new file mode 100644
index 000000000..c2407da2b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/Grammar.java
@@ -0,0 +1,69 @@
+/* Grammar.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+import java.util.LinkedList;
+import java.util.List;
+import javax.xml.validation.Schema;
+import javax.xml.validation.Validator;
+import javax.xml.validation.ValidatorHandler;
+
+/**
+ * A RELAX NG grammar.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class Grammar
+ extends Schema
+{
+
+ Pattern start;
+ List defines = new LinkedList();
+
+ public Validator newValidator()
+ {
+ return new GrammarValidator(this);
+ }
+
+ public ValidatorHandler newValidatorHandler()
+ {
+ // TODO
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/GrammarException.java b/libjava/classpath/gnu/xml/validation/relaxng/GrammarException.java
new file mode 100644
index 000000000..167cf6482
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/GrammarException.java
@@ -0,0 +1,56 @@
+/* GrammarException.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+import java.io.IOException;
+
+/**
+ * Exception parsing a grammar.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GrammarException
+ extends IOException
+{
+
+ GrammarException(String message)
+ {
+ super(message);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/GrammarValidator.java b/libjava/classpath/gnu/xml/validation/relaxng/GrammarValidator.java
new file mode 100644
index 000000000..e4ff3dab4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/GrammarValidator.java
@@ -0,0 +1,97 @@
+/* GrammarValidator.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+import java.io.IOException;
+import javax.xml.transform.Source;
+import javax.xml.transform.Result;
+import javax.xml.validation.Validator;
+import org.w3c.dom.ls.LSResourceResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+
+/**
+ * RELAX NG validator.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GrammarValidator
+ extends Validator
+{
+
+ final Grammar grammar;
+ ErrorHandler errorHandler;
+ LSResourceResolver resourceResolver;
+
+ GrammarValidator(Grammar grammar)
+ {
+ this.grammar = grammar;
+ }
+
+ public ErrorHandler getErrorHandler()
+ {
+ return errorHandler;
+ }
+
+ public void setErrorHandler(ErrorHandler errorHandler)
+ {
+ this.errorHandler = errorHandler;
+ }
+
+ public LSResourceResolver getResourceResolver()
+ {
+ return resourceResolver;
+ }
+
+ public void setResourceResolver(LSResourceResolver resourceResolver)
+ {
+ this.resourceResolver = resourceResolver;
+ }
+
+ public void reset()
+ {
+ // TODO
+ }
+
+ public void validate(Source source, Result result)
+ throws SAXException, IOException
+ {
+ // TODO
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/GroupPattern.java b/libjava/classpath/gnu/xml/validation/relaxng/GroupPattern.java
new file mode 100644
index 000000000..b63c7aaa7
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/GroupPattern.java
@@ -0,0 +1,52 @@
+/* GroupPattern.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG group element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class GroupPattern
+ extends Pattern
+{
+
+ Pattern pattern1;
+ Pattern pattern2;
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/InterleavePattern.java b/libjava/classpath/gnu/xml/validation/relaxng/InterleavePattern.java
new file mode 100644
index 000000000..8a877d7af
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/InterleavePattern.java
@@ -0,0 +1,52 @@
+/* InterleavePattern.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG interleave element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class InterleavePattern
+ extends Pattern
+{
+
+ Pattern pattern1;
+ Pattern pattern2;
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/ListPattern.java b/libjava/classpath/gnu/xml/validation/relaxng/ListPattern.java
new file mode 100644
index 000000000..e16d5f6e5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/ListPattern.java
@@ -0,0 +1,51 @@
+/* ListPattern.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG list element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class ListPattern
+ extends Pattern
+{
+
+ Pattern pattern;
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/NSNameNameClass.java b/libjava/classpath/gnu/xml/validation/relaxng/NSNameNameClass.java
new file mode 100644
index 000000000..2f620c1d0
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/NSNameNameClass.java
@@ -0,0 +1,60 @@
+/* NSNameNameClass.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG nsName element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class NSNameNameClass
+ extends NameClass
+{
+
+ String ns;
+ NameClass exceptNameClass;
+
+ boolean matchesName(String uri, String localName)
+ {
+ if (!ns.equals(uri))
+ return false;
+ return (exceptNameClass == null) ? true :
+ !exceptNameClass.matchesName(uri, localName);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/NameClass.java b/libjava/classpath/gnu/xml/validation/relaxng/NameClass.java
new file mode 100644
index 000000000..0ef9ef131
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/NameClass.java
@@ -0,0 +1,50 @@
+/* NameClass.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG name class.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+abstract class NameClass
+{
+
+ abstract boolean matchesName(String uri, String localName);
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/NameNameClass.java b/libjava/classpath/gnu/xml/validation/relaxng/NameNameClass.java
new file mode 100644
index 000000000..b2d0e04ee
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/NameNameClass.java
@@ -0,0 +1,59 @@
+/* NameNameClass.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG name element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class NameNameClass
+ extends NameClass
+{
+
+ String ns;
+ String name;
+
+ boolean matchesName(String uri, String localName)
+ {
+ if (!ns.equals(uri))
+ return false;
+ return name.equals(localName);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/NotAllowedPattern.java b/libjava/classpath/gnu/xml/validation/relaxng/NotAllowedPattern.java
new file mode 100644
index 000000000..d56afb2d7
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/NotAllowedPattern.java
@@ -0,0 +1,51 @@
+/* NotAllowedPattern.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG notAllowed element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class NotAllowedPattern
+ extends Pattern
+{
+
+ static final NotAllowedPattern INSTANCE = new NotAllowedPattern();
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/OneOrMorePattern.java b/libjava/classpath/gnu/xml/validation/relaxng/OneOrMorePattern.java
new file mode 100644
index 000000000..0545130ad
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/OneOrMorePattern.java
@@ -0,0 +1,51 @@
+/* OneOrMorePattern.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG oneOrMore element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class OneOrMorePattern
+ extends Pattern
+{
+
+ Pattern pattern;
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/Param.java b/libjava/classpath/gnu/xml/validation/relaxng/Param.java
new file mode 100644
index 000000000..65a1b6689
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/Param.java
@@ -0,0 +1,51 @@
+/* Param.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG param element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class Param
+{
+
+ String name;
+ String value;
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/Pattern.java b/libjava/classpath/gnu/xml/validation/relaxng/Pattern.java
new file mode 100644
index 000000000..157f22316
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/Pattern.java
@@ -0,0 +1,47 @@
+/* Pattern.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG pattern.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+abstract class Pattern
+{
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/RELAXNGSchemaFactory.java b/libjava/classpath/gnu/xml/validation/relaxng/RELAXNGSchemaFactory.java
new file mode 100644
index 000000000..c0c9bd887
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/RELAXNGSchemaFactory.java
@@ -0,0 +1,163 @@
+/* RelaxNGSchemaFactory.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+import java.io.IOException;
+import java.net.URL;
+import javax.xml.XMLConstants;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.ls.LSResourceResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * Schema factory for RELAX NG grammars.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class RELAXNGSchemaFactory
+ extends SchemaFactory
+{
+
+ LSResourceResolver resourceResolver;
+ ErrorHandler errorHandler;
+
+ public LSResourceResolver getResourceResolver()
+ {
+ return resourceResolver;
+ }
+
+ public void setResourceResolver(LSResourceResolver resourceResolver)
+ {
+ this.resourceResolver = resourceResolver;
+ }
+
+ public ErrorHandler getErrorHandler()
+ {
+ return this.errorHandler;
+ }
+
+ public void setErrorHandler(ErrorHandler errorHandler)
+ {
+ this.errorHandler = errorHandler;
+ }
+
+ public boolean isSchemaLanguageSupported(String schemaLanguage)
+ {
+ return XMLConstants.RELAXNG_NS_URI.equals(schemaLanguage);
+ }
+
+ public Schema newSchema()
+ throws SAXException
+ {
+ // TODO
+ throw new UnsupportedOperationException();
+ }
+
+ public Schema newSchema(Source[] schemata)
+ throws SAXException
+ {
+ if (schemata == null || schemata.length != 1)
+ throw new IllegalArgumentException("must specify one source");
+ // TODO multiple schemata
+ // TODO compact syntax
+ try
+ {
+ Document doc = getDocument(schemata[0]);
+ FullSyntaxBuilder builder = new FullSyntaxBuilder();
+ return builder.parse(doc);
+ }
+ catch (IOException e)
+ {
+ SAXException e2 = new SAXException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ private static Document getDocument(Source source)
+ throws SAXException, IOException
+ {
+ if (source instanceof DOMSource)
+ {
+ Node node = ((DOMSource) source).getNode();
+ if (node != null && node instanceof Document)
+ return (Document) node;
+ }
+ String url = source.getSystemId();
+ try
+ {
+ InputSource input = new InputSource(url);
+ if (source instanceof StreamSource)
+ {
+ StreamSource streamSource = (StreamSource) source;
+ input.setByteStream(streamSource.getInputStream());
+ input.setCharacterStream(streamSource.getReader());
+ }
+ if (input.getByteStream() == null &&
+ input.getCharacterStream() == null &&
+ url != null)
+ input.setByteStream(new URL(url).openStream());
+ DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
+ f.setNamespaceAware(true);
+ f.setCoalescing(true);
+ f.setExpandEntityReferences(true);
+ f.setIgnoringComments(true);
+ f.setIgnoringElementContentWhitespace(true);
+ DocumentBuilder b = f.newDocumentBuilder();
+ return b.parse(input);
+ }
+ catch (ParserConfigurationException e)
+ {
+ SAXException e2 = new SAXException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/RefPattern.java b/libjava/classpath/gnu/xml/validation/relaxng/RefPattern.java
new file mode 100644
index 000000000..8c1ff8539
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/RefPattern.java
@@ -0,0 +1,51 @@
+/* RefPattern.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG ref element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class RefPattern
+ extends Pattern
+{
+
+ String name;
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/TextPattern.java b/libjava/classpath/gnu/xml/validation/relaxng/TextPattern.java
new file mode 100644
index 000000000..81c502820
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/TextPattern.java
@@ -0,0 +1,51 @@
+/* TextPattern.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+/**
+ * A RELAX NG text element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class TextPattern
+ extends Pattern
+{
+
+ static final TextPattern INSTANCE = new TextPattern();
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/relaxng/ValuePattern.java b/libjava/classpath/gnu/xml/validation/relaxng/ValuePattern.java
new file mode 100644
index 000000000..8b63d79ae
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/relaxng/ValuePattern.java
@@ -0,0 +1,57 @@
+/* ValuePattern.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.relaxng;
+
+import org.relaxng.datatype.Datatype;
+import org.relaxng.datatype.DatatypeLibrary;
+
+/**
+ * A RELAX NG value element.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class ValuePattern
+ extends Pattern
+{
+
+ DatatypeLibrary datatypeLibrary;
+ Datatype type;
+ String ns;
+ String value;
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/AnyAttribute.java b/libjava/classpath/gnu/xml/validation/xmlschema/AnyAttribute.java
new file mode 100644
index 000000000..6f94bfe28
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/AnyAttribute.java
@@ -0,0 +1,66 @@
+/* AnyAttribute.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+import gnu.xml.validation.datatype.Annotation;
+
+/**
+ * An attribute wildcard.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class AnyAttribute
+{
+
+ static final int STRICT = 0;
+ static final int LAX = 1;
+ static final int SKIP = 2;
+
+ final String namespace;
+
+ final int processContents;
+
+ Annotation annotation;
+
+ AnyAttribute(String namespace, int processContents)
+ {
+ this.namespace = namespace;
+ this.processContents = processContents;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/AttributeDeclaration.java b/libjava/classpath/gnu/xml/validation/xmlschema/AttributeDeclaration.java
new file mode 100644
index 000000000..c475b368d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/AttributeDeclaration.java
@@ -0,0 +1,98 @@
+/* AttributeDeclaration.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+import gnu.xml.validation.datatype.Annotation;
+import gnu.xml.validation.datatype.SimpleType;
+import javax.xml.namespace.QName;
+
+/**
+ * An XML Schema attribute declaration schema component.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class AttributeDeclaration
+{
+
+ static final int NONE = 0;
+ static final int DEFAULT = 1;
+ static final int FIXED = 2;
+
+ /**
+ * The scope of this attribute declaration (global or local).
+ */
+ final boolean scope;
+
+ /**
+ * The constraint type.
+ * One of NONE, DEFAULT, FIXED.
+ */
+ final int type;
+
+ /**
+ * The value constraint.
+ */
+ final String value;
+
+ /**
+ * The name of the attribute to which this declaration refers.
+ */
+ final QName name;
+
+ /**
+ * The type definition corresponding to this attribute.
+ */
+ final SimpleType datatype;
+
+ /**
+ * The annotation associated with this attribute declaration, if any.
+ */
+ final Annotation annotation;
+
+ AttributeDeclaration(boolean scope, int type, String value, QName name,
+ SimpleType datatype, Annotation annotation)
+ {
+ this.scope = scope;
+ this.type = type;
+ this.value = value;
+ this.name = name;
+ this.datatype = datatype;
+ this.annotation = annotation;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/AttributeUse.java b/libjava/classpath/gnu/xml/validation/xmlschema/AttributeUse.java
new file mode 100644
index 000000000..937076338
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/AttributeUse.java
@@ -0,0 +1,78 @@
+/* AttributeUse.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+/**
+ * An XML Schema attribute use schema component.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class AttributeUse
+{
+
+ /**
+ * Whether the attribute is required.
+ */
+ final boolean required;
+
+ /**
+ * The constraint type.
+ * One of NONE, DEFAULT, FIXED.
+ */
+ final int type;
+
+ /**
+ * The value constraint.
+ */
+ final String value;
+
+ /**
+ * The name of the attribute to which this declaration refers.
+ */
+ final AttributeDeclaration declaration;
+
+ AttributeUse(boolean required, int type, String value,
+ AttributeDeclaration declaration)
+ {
+ this.required = required;
+ this.type = type;
+ this.value = value;
+ this.declaration = declaration;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/ComplexType.java b/libjava/classpath/gnu/xml/validation/xmlschema/ComplexType.java
new file mode 100644
index 000000000..826ba32d9
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/ComplexType.java
@@ -0,0 +1,101 @@
+/* ComplexType.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+import javax.xml.namespace.QName;
+import gnu.xml.validation.datatype.Type;
+
+/**
+ * A complex type definition.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class ComplexType
+ extends Type
+{
+
+ /**
+ * Either a simple type definition or a complex type definition.
+ */
+ QName baseType;
+
+ /**
+ * Either EXTENSION or RESTRICTION.
+ */
+ int derivationMethod;
+
+ /**
+ * A subset of {EXTENSION, RESTRICTION}.
+ */
+ final int finality;
+
+ final boolean isAbstract;
+
+ Set attributeUses;
+
+ AnyAttribute attributeWildcard;
+
+ /**
+ * One of EMPTY, SIMPLE, MIXED, or ELEMENT_ONLY.
+ */
+ int contentType;
+
+ /**
+ * A simple type definition or a Particle.
+ */
+ Object contentModel;
+
+ final int prohibitedSubstitutions;
+
+ Set annotations;
+
+ ComplexType(QName name,
+ boolean isAbstract,
+ int prohibitedSubstitutions,
+ int finality)
+ {
+ super(name);
+ this.isAbstract = isAbstract;
+ this.prohibitedSubstitutions = prohibitedSubstitutions;
+ this.finality = finality;
+ attributeUses = new LinkedHashSet();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/ElementDeclaration.java b/libjava/classpath/gnu/xml/validation/xmlschema/ElementDeclaration.java
new file mode 100644
index 000000000..7392ca596
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/ElementDeclaration.java
@@ -0,0 +1,124 @@
+/* ElementDeclaration.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+import gnu.xml.validation.datatype.Annotation;
+import gnu.xml.validation.datatype.Type;
+import javax.xml.namespace.QName;
+
+/**
+ * An XML Schema element declaration schema component.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class ElementDeclaration
+{
+
+ /**
+ * The name of the element to which this declaration refers.
+ */
+ final QName name;
+
+ /**
+ * The type definition corresponding to this element.
+ */
+ Type datatype;
+
+ /**
+ * The scope of this schema component.
+ * One of GLOBAL, LOCAL, or ABSENT.
+ */
+ final int scope;
+
+ /**
+ * If scope is LOCAL, the parent element definition.
+ */
+ final ElementDeclaration parent;
+
+ /**
+ * The constraint type.
+ * One of NONE, DEFAULT, FIXED.
+ */
+ final int type;
+
+ /**
+ * The value constraint.
+ */
+ final String value;
+
+ final boolean nillable;
+
+ // TODO identity-constraint definitions
+
+ final QName substitutionGroup;
+
+ final int substitutionGroupExclusions;
+
+ final int disallowedSubstitutions;
+
+ final boolean isAbstract;
+
+ /**
+ * The annotation associated with this attribute declaration, if any.
+ */
+ Annotation annotation;
+
+ ElementDeclaration(QName name,
+ Type datatype,
+ int scope, ElementDeclaration parent,
+ int type, String value,
+ boolean nillable,
+ QName substitutionGroup,
+ int substitutionGroupExclusions,
+ int disallowedSubstitutions,
+ boolean isAbstract)
+ {
+ this.name = name;
+ this.datatype = datatype;
+ this.scope = scope;
+ this.parent = parent;
+ this.type = type;
+ this.value = value;
+ this.nillable = nillable;
+ this.substitutionGroup = substitutionGroup;
+ this.substitutionGroupExclusions = substitutionGroupExclusions;
+ this.disallowedSubstitutions = disallowedSubstitutions;
+ this.isAbstract = isAbstract;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/Particle.java b/libjava/classpath/gnu/xml/validation/xmlschema/Particle.java
new file mode 100644
index 000000000..5fe49203e
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/Particle.java
@@ -0,0 +1,60 @@
+/* Particle.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+/**
+ * Container for element content.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class Particle
+{
+
+ final Integer minOccurs;
+ final Integer maxOccurs;
+
+ final Object term;
+
+ Particle(Integer minOccurs, Integer maxOccurs, Object term)
+ {
+ this.minOccurs = minOccurs;
+ this.maxOccurs = maxOccurs;
+ this.term = term;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/ValidationException.java b/libjava/classpath/gnu/xml/validation/xmlschema/ValidationException.java
new file mode 100644
index 000000000..716ba8800
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/ValidationException.java
@@ -0,0 +1,57 @@
+/* ValidationException.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+import org.xml.sax.Locator;
+import org.xml.sax.SAXParseException;
+
+/**
+ * An XML Schema validation rule violation.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class ValidationException
+ extends SAXParseException
+{
+
+ ValidationException(String message, Locator locator)
+ {
+ super(message, locator);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchema.java b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchema.java
new file mode 100644
index 000000000..fb8eb88b1
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchema.java
@@ -0,0 +1,133 @@
+/* XMLSchema.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import javax.xml.validation.Schema;
+import javax.xml.validation.Validator;
+import javax.xml.validation.ValidatorHandler;
+
+/**
+ * An XML Schema schema.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class XMLSchema
+ extends Schema
+{
+
+ static final int FINAL_NONE = 0x00;
+ static final int FINAL_EXTENSION = 0x01;
+ static final int FINAL_RESTRICTION = 0x02;
+ static final int FINAL_LIST = 0x04;
+ static final int FINAL_UNION = 0x08;
+ static final int FINAL_ALL = 0x0f;
+
+ static final int BLOCK_NONE = 0x00;
+ static final int BLOCK_EXTENSION = 0x01;
+ static final int BLOCK_RESTRICTION = 0x02;
+ static final int BLOCK_SUBSTITUTION = 0x04;
+ static final int BLOCK_ALL = 0x07;
+
+ static final int GLOBAL = 0x00;
+ static final int LOCAL = 0x01;
+ static final int ABSENT = 0x02;
+
+ static final int CONSTRAINT_NONE = 0x00;
+ static final int CONSTRAINT_DEFAULT = 0x01;
+ static final int CONSTRAINT_FIXED = 0x02;
+
+ static final int CONTENT_EMPTY = 0x00;
+ static final int CONTENT_SIMPLE = 0x01;
+ static final int CONTENT_MIXED = 0x02;
+ static final int CONTENT_ELEMENT_ONLY = 0x03;
+
+ final String targetNamespace;
+ final String version;
+ final int finalDefault;
+ final int blockDefault;
+ final boolean attributeFormQualified;
+ final boolean elementFormQualified;
+
+ /**
+ * The element declarations in this schema.
+ */
+ final Map elementDeclarations;
+
+ /**
+ * The attribute declarations in this schema.
+ */
+ final Map attributeDeclarations;
+
+ /**
+ * The type declarations in this schema.
+ */
+ final Map types;
+
+ XMLSchema(String targetNamespace, String version,
+ int finalDefault, int blockDefault,
+ boolean attributeFormQualified,
+ boolean elementFormQualified)
+ {
+ this.targetNamespace = targetNamespace;
+ this.version = version;
+ this.finalDefault = finalDefault;
+ this.blockDefault = blockDefault;
+ this.attributeFormQualified = attributeFormQualified;
+ this.elementFormQualified = elementFormQualified;
+ elementDeclarations = new LinkedHashMap();
+ attributeDeclarations = new LinkedHashMap();
+ types = new LinkedHashMap();
+ }
+
+ public Validator newValidator()
+ {
+ // TODO
+ //return new XMLSchemaValidator(this);
+ return null;
+ }
+
+ public ValidatorHandler newValidatorHandler()
+ {
+ // TODO
+ //return new XMLSchemaValidatorHandler(this);
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaAttributeTypeInfo.java b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaAttributeTypeInfo.java
new file mode 100644
index 000000000..ba83973df
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaAttributeTypeInfo.java
@@ -0,0 +1,98 @@
+/* XMLSchemaAttributeTypeInfo.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+import gnu.xml.validation.datatype.SimpleType;
+
+/**
+ * Attribute type information provided by validation against an XML Schema.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class XMLSchemaAttributeTypeInfo
+ extends XMLSchemaTypeInfo
+{
+
+ final XMLSchema schema;
+ final AttributeDeclaration decl;
+ final SimpleType type;
+ boolean id;
+ final boolean specified;
+
+ XMLSchemaAttributeTypeInfo(XMLSchema schema, AttributeDeclaration decl,
+ boolean specified)
+ {
+ this.schema = schema;
+ this.decl = decl;
+ this.specified = specified;
+ type = (decl == null) ? null : decl.datatype;
+ }
+
+ public String getTypeName()
+ {
+ if (type == null)
+ {
+ return "CDATA";
+ }
+ return type.name.getLocalPart();
+ }
+
+ public String getTypeNamespace()
+ {
+ if (type == null)
+ {
+ return "";
+ }
+ return type.name.getNamespaceURI();
+ }
+
+ public boolean isDerivedFrom(String typeNamespace, String typeName,
+ int derivationMethod)
+ {
+ if (type == null)
+ {
+ return false;
+ }
+ else
+ {
+ return simpleTypeIsDerivedFrom(type, typeNamespace, typeName,
+ derivationMethod);
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaBuilder.java b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaBuilder.java
new file mode 100644
index 000000000..a7bad3129
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaBuilder.java
@@ -0,0 +1,839 @@
+/* XMLSchemaBuilder.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.StringTokenizer;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.DatatypeLibrary;
+import org.relaxng.datatype.helpers.DatatypeLibraryLoader;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import gnu.xml.validation.datatype.Annotation;
+import gnu.xml.validation.datatype.SimpleType;
+import gnu.xml.validation.datatype.Type;
+
+/**
+ * Parses an XML Schema DOM tree, constructing a compiled internal
+ * representation.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class XMLSchemaBuilder
+{
+
+ XMLSchema schema;
+ final DatatypeLibrary typeLibrary;
+
+ XMLSchemaBuilder()
+ {
+ final String ns = XMLConstants.W3C_XML_SCHEMA_NS_URI;
+ typeLibrary = new DatatypeLibraryLoader().createDatatypeLibrary(ns);
+ }
+
+ void parseSchema(Node node)
+ throws DatatypeException
+ {
+ String uri = node.getNamespaceURI();
+ String name = node.getLocalName();
+ if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) &&
+ node.getNodeType() == Node.ELEMENT_NODE)
+ {
+ if ("schema".equals(name))
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String targetNamespace = getAttribute(attrs, "targetNamespace");
+ String version = getAttribute(attrs, "version");
+ String fd = getAttribute(attrs, "finalDefault");
+ int finalDefault = parseFullDerivationSet(fd);
+ String bd = getAttribute(attrs, "blockDefault");
+ int blockDefault = parseBlockSet(bd);
+ String afd = getAttribute(attrs, "attributeFormDefault");
+ boolean attributeFormQualified = "qualified".equals(afd);
+ String efd = getAttribute(attrs, "elementFormDefault");
+ boolean elementFormQualified = "qualified".equals(efd);
+ schema = new XMLSchema(targetNamespace, version,
+ finalDefault, blockDefault,
+ attributeFormQualified,
+ elementFormQualified);
+ for (Node child = node.getFirstChild(); child != null;
+ child = child.getNextSibling())
+ {
+ parseTopLevelElement(child);
+ }
+ return;
+ }
+ }
+ // TODO throw schema exception
+ }
+
+ void parseTopLevelElement(Node node)
+ throws DatatypeException
+ {
+ String uri = node.getNamespaceURI();
+ String name = node.getLocalName();
+ if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) &&
+ node.getNodeType() == Node.ELEMENT_NODE)
+ {
+ if ("element".equals(name))
+ {
+ ElementDeclaration ed =
+ (ElementDeclaration) parseElement(node, null);
+ schema.elementDeclarations.put(ed.name, ed);
+ // TODO
+ }
+ else if ("attribute".equals(name))
+ {
+ AttributeDeclaration ad =
+ (AttributeDeclaration) parseAttribute(node, true);
+ schema.attributeDeclarations.put(ad.name, ad);
+ // TODO
+ }
+ else if ("type".equals(name))
+ {
+ // TODO
+ }
+ else if ("group".equals(name))
+ {
+ // TODO
+ }
+ else if ("attributeGroup".equals(name))
+ {
+ // TODO
+ }
+ else if ("notation".equals(name))
+ {
+ // TODO
+ }
+ else if ("identityConstraint".equals(name))
+ {
+ // TODO
+ }
+ }
+ // TODO throw schema exception
+ }
+
+ Object parseAttribute(Node node, boolean scope)
+ throws DatatypeException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String def = getAttribute(attrs, "default");
+ String fixed = getAttribute(attrs, "fixed");
+ int constraintType = AttributeDeclaration.NONE;
+ String constraintValue = null;
+ if (def != null)
+ {
+ constraintType = AttributeDeclaration.DEFAULT;
+ constraintValue = def;
+ }
+ else if (fixed != null)
+ {
+ constraintType = AttributeDeclaration.FIXED;
+ constraintValue = fixed;
+ }
+ // TODO form = (qualified | unqualified)
+ String attrName = getAttribute(attrs, "name");
+ String attrNamespace = getAttribute(attrs, "targetNamespace");
+ String ref = getAttribute(attrs, "ref");
+ String use = getAttribute(attrs, "use");
+ String type = getAttribute(attrs, "type");
+ SimpleType datatype = (type == null) ? null :
+ parseSimpleType(asQName(type, node));
+ Annotation annotation = null;
+ for (Node child = node.getFirstChild(); child != null;
+ child = child.getNextSibling())
+ {
+ String uri = child.getNamespaceURI();
+ String name = child.getLocalName();
+ if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) &&
+ child.getNodeType() == Node.ELEMENT_NODE)
+ {
+ if ("annotation".equals(name))
+ {
+ annotation = parseAnnotation(child);
+ }
+ else if ("simpleType".equals(name))
+ {
+ datatype = parseSimpleType(child);
+ }
+ else
+ {
+ // TODO throw schema exception
+ }
+ }
+ }
+ if (scope)
+ {
+ return new AttributeDeclaration(scope,
+ constraintType,
+ constraintValue,
+ new QName(attrNamespace, attrName),
+ datatype,
+ annotation);
+ }
+ else
+ {
+ boolean required = "required".equals(use);
+ // TODO ref
+ AttributeDeclaration decl = (ref == null) ?
+ new AttributeDeclaration(scope,
+ AttributeDeclaration.NONE,
+ null,
+ new QName(attrNamespace, attrName),
+ datatype,
+ annotation) :
+ /*schema.getAttributeDeclaration(ref)*/ null;
+ return new AttributeUse(required,
+ constraintType,
+ constraintValue,
+ decl);
+ }
+ }
+
+ int parseFullDerivationSet(String value)
+ {
+ int ret = XMLSchema.FINAL_NONE;
+ if ("#all".equals(value))
+ {
+ ret = XMLSchema.FINAL_ALL;
+ }
+ else
+ {
+ StringTokenizer st = new StringTokenizer(value, " ");
+ while (st.hasMoreTokens())
+ {
+ String token = st.nextToken();
+ if ("extension".equals(token))
+ {
+ ret |= XMLSchema.FINAL_EXTENSION;
+ }
+ else if ("restriction".equals(token))
+ {
+ ret |= XMLSchema.FINAL_RESTRICTION;
+ }
+ else if ("list".equals(token))
+ {
+ ret |= XMLSchema.FINAL_LIST;
+ }
+ else if ("union".equals(token))
+ {
+ ret |= XMLSchema.FINAL_UNION;
+ }
+ }
+ }
+ return ret;
+ }
+
+ int parseSimpleTypeDerivationSet(String value)
+ {
+ int ret = XMLSchema.FINAL_NONE;
+ if ("#all".equals(value))
+ {
+ ret = XMLSchema.FINAL_LIST |
+ XMLSchema.FINAL_UNION |
+ XMLSchema.FINAL_RESTRICTION;
+ }
+ else
+ {
+ StringTokenizer st = new StringTokenizer(value, " ");
+ while (st.hasMoreTokens())
+ {
+ String token = st.nextToken();
+ if ("list".equals(token))
+ {
+ ret |= XMLSchema.FINAL_LIST;
+ }
+ else if ("union".equals(token))
+ {
+ ret |= XMLSchema.FINAL_UNION;
+ }
+ else if ("restriction".equals(token))
+ {
+ ret |= XMLSchema.FINAL_RESTRICTION;
+ }
+ }
+ }
+ return ret;
+ }
+
+ int parseComplexTypeDerivationSet(String value)
+ {
+ int ret = XMLSchema.FINAL_NONE;
+ if ("#all".equals(value))
+ {
+ ret = XMLSchema.FINAL_EXTENSION | XMLSchema.FINAL_RESTRICTION;
+ }
+ else
+ {
+ StringTokenizer st = new StringTokenizer(value, " ");
+ while (st.hasMoreTokens())
+ {
+ String token = st.nextToken();
+ if ("extension".equals(token))
+ {
+ ret |= XMLSchema.FINAL_EXTENSION;
+ }
+ else if ("restriction".equals(token))
+ {
+ ret |= XMLSchema.FINAL_RESTRICTION;
+ }
+ }
+ }
+ return ret;
+ }
+
+ int parseBlockSet(String value)
+ {
+ int ret = XMLSchema.BLOCK_NONE;
+ if ("#all".equals(value))
+ {
+ ret = XMLSchema.BLOCK_ALL;
+ }
+ else
+ {
+ StringTokenizer st = new StringTokenizer(value, " ");
+ while (st.hasMoreTokens())
+ {
+ String token = st.nextToken();
+ if ("extension".equals(token))
+ {
+ ret |= XMLSchema.BLOCK_EXTENSION;
+ }
+ else if ("restriction".equals(token))
+ {
+ ret |= XMLSchema.BLOCK_RESTRICTION;
+ }
+ else if ("substitution".equals(token))
+ {
+ ret |= XMLSchema.BLOCK_SUBSTITUTION;
+ }
+ }
+ }
+ return ret;
+ }
+
+ int parseComplexTypeBlockSet(String value)
+ {
+ int ret = XMLSchema.BLOCK_NONE;
+ if ("#all".equals(value))
+ {
+ ret = XMLSchema.BLOCK_EXTENSION | XMLSchema.BLOCK_RESTRICTION;
+ }
+ else
+ {
+ StringTokenizer st = new StringTokenizer(value, " ");
+ while (st.hasMoreTokens())
+ {
+ String token = st.nextToken();
+ if ("extension".equals(token))
+ {
+ ret |= XMLSchema.BLOCK_EXTENSION;
+ }
+ else if ("restriction".equals(token))
+ {
+ ret |= XMLSchema.BLOCK_RESTRICTION;
+ }
+ }
+ }
+ return ret;
+ }
+
+ Object parseElement(Node node, ElementDeclaration parent)
+ throws DatatypeException
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ Integer minOccurs = null;
+ Integer maxOccurs = null;
+ Node parentNode = node.getParentNode();
+ boolean notTopLevel = !"schema".equals(parentNode.getLocalName());
+ if (notTopLevel)
+ {
+ String ref = getAttribute(attrs, "ref");
+ if (ref != null)
+ {
+ minOccurs = getOccurrence(getAttribute(attrs, "minOccurs"));
+ maxOccurs = getOccurrence(getAttribute(attrs, "maxOccurs"));
+ // TODO resolve top-level element declaration
+ ElementDeclaration ad = null;
+ return new Particle(minOccurs, maxOccurs, ad);
+ }
+ }
+ String elementName = getAttribute(attrs, "name");
+ String elementNamespace = getAttribute(attrs, "targetNamespace");
+ String type = getAttribute(attrs, "type");
+ Type datatype = (type != null) ?
+ parseSimpleType(asQName(type, node)) : null;
+ int scope = (parent == null) ?
+ XMLSchema.GLOBAL :
+ XMLSchema.LOCAL;
+ String def = getAttribute(attrs, "default");
+ String fixed = getAttribute(attrs, "fixed");
+ int constraintType = AttributeDeclaration.NONE;
+ String constraintValue = null;
+ if (def != null)
+ {
+ constraintType = AttributeDeclaration.DEFAULT;
+ constraintValue = def;
+ }
+ else if (fixed != null)
+ {
+ constraintType = AttributeDeclaration.FIXED;
+ constraintValue = fixed;
+ }
+ String sg = getAttribute(attrs, "substitutionGroup");
+ QName substitutionGroup = QName.valueOf(sg);
+ String sgPrefix = substitutionGroup.getPrefix();
+ if (sgPrefix != null && !"".equals(sgPrefix))
+ {
+ String sgName = substitutionGroup.getLocalPart();
+ String sgNamespace = node.lookupNamespaceURI(sgPrefix);
+ substitutionGroup = new QName(sgNamespace, sgName);
+ }
+
+ String block = getAttribute(attrs, "block");
+ int substitutionGroupExclusions = (block == null) ?
+ schema.blockDefault :
+ parseBlockSet(block);
+ String final_ = getAttribute(attrs, "final");
+ int disallowedSubstitutions = (final_ == null) ?
+ schema.finalDefault :
+ parseFullDerivationSet(final_);
+
+ boolean nillable = "true".equals(getAttribute(attrs, "nillable"));
+ boolean isAbstract = "true".equals(getAttribute(attrs, "abstract"));
+
+ if (notTopLevel)
+ {
+ minOccurs = getOccurrence(getAttribute(attrs, "minOccurs"));
+ maxOccurs = getOccurrence(getAttribute(attrs, "maxOccurs"));
+ String form = getAttribute(attrs, "form");
+ if (form != null)
+ {
+ if ("qualified".equals(form))
+ {
+ elementNamespace = schema.targetNamespace;
+ }
+ }
+ else if (schema.elementFormQualified)
+ {
+ elementNamespace = schema.targetNamespace;
+ }
+ }
+ ElementDeclaration ed =
+ new ElementDeclaration(new QName(elementNamespace, elementName),
+ datatype,
+ scope, parent,
+ constraintType, constraintValue,
+ nillable,
+ substitutionGroup,
+ substitutionGroupExclusions,
+ disallowedSubstitutions,
+ isAbstract);
+
+ for (Node child = node.getFirstChild(); child != null;
+ child = child.getNextSibling())
+ {
+ String uri = child.getNamespaceURI();
+ String name = child.getLocalName();
+ if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) &&
+ child.getNodeType() == Node.ELEMENT_NODE)
+ {
+ if ("annotation".equals(name))
+ {
+ ed.annotation = parseAnnotation(child);
+ }
+ else if ("simpleType".equals(name) && datatype == null)
+ {
+ ed.datatype = parseSimpleType(child);
+ }
+ else if ("complexType".equals(name) && datatype == null)
+ {
+ ed.datatype = parseComplexType(child, ed);
+ }
+ else
+ {
+ // throw schema exception
+ }
+ }
+ }
+
+ if (notTopLevel)
+ {
+ return new Particle(minOccurs, maxOccurs, ed);
+ }
+ else
+ {
+ return ed;
+ }
+ }
+
+ Integer getOccurrence(String value)
+ {
+ if (value == null)
+ {
+ return new Integer(1);
+ }
+ else if ("unbounded".equals(value))
+ {
+ return null;
+ }
+ else
+ {
+ return new Integer(value);
+ }
+ }
+
+ SimpleType parseSimpleType(QName typeName)
+ throws DatatypeException
+ {
+ SimpleType type = (SimpleType) schema.types.get(typeName);
+ if (!XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(typeName.getNamespaceURI()))
+ return null;
+ String localName = typeName.getLocalPart();
+ return (SimpleType) typeLibrary.createDatatype(localName);
+ }
+
+ SimpleType parseSimpleType(Node simpleType)
+ throws DatatypeException
+ {
+ NamedNodeMap attrs = simpleType.getAttributes();
+ String typeFinal = getAttribute(attrs, "final");
+ if (typeFinal == null)
+ {
+ Node schema = simpleType.getParentNode();
+ while (schema != null && !"schema".equals(schema.getLocalName()))
+ {
+ schema = schema.getParentNode();
+ }
+ if (schema != null)
+ {
+ NamedNodeMap schemaAttrs = schema.getAttributes();
+ typeFinal = getAttribute(schemaAttrs, "finalDefault");
+ }
+ }
+ int typeFinality = parseSimpleTypeDerivationSet(typeFinal);
+ QName typeName = asQName(getAttribute(attrs, "name"), simpleType);
+ int variety = 0;
+ Set facets = new LinkedHashSet();
+ int fundamentalFacets = 0; // TODO
+ SimpleType baseType = null; // TODO
+ Annotation annotation = null;
+ // TODO use DatatypeBuilder
+ for (Node child = simpleType.getFirstChild(); child != null;
+ child = child.getNextSibling())
+ {
+ String uri = child.getNamespaceURI();
+ String name = child.getLocalName();
+ if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) &&
+ child.getNodeType() == Node.ELEMENT_NODE)
+ {
+ if ("annotation".equals(name))
+ {
+ annotation = parseAnnotation(child);
+ }
+ else if ("restriction".equals(name))
+ {
+ // TODO
+ }
+ else if ("list".equals(name))
+ {
+ variety = SimpleType.LIST;
+ // TODO
+ }
+ else if ("union".equals(name))
+ {
+ variety = SimpleType.UNION;
+ // TODO
+ }
+ }
+ }
+ return new SimpleType(typeName, variety, facets, fundamentalFacets,
+ baseType, annotation);
+ }
+
+ Type parseComplexType(Node complexType, ElementDeclaration parent)
+ throws DatatypeException
+ {
+ NamedNodeMap attrs = complexType.getAttributes();
+ QName typeName = asQName(getAttribute(attrs, "name"), complexType);
+ boolean isAbstract = "true".equals(getAttribute(attrs, "abstract"));
+ String block = getAttribute(attrs, "block");
+ int prohibitedSubstitutions = (block == null) ?
+ schema.blockDefault :
+ parseComplexTypeBlockSet(block);
+ String final_ = getAttribute(attrs, "final");
+ int finality = (final_ == null) ?
+ schema.finalDefault :
+ parseComplexTypeDerivationSet(final_);
+ ComplexType type = new ComplexType(typeName, isAbstract,
+ prohibitedSubstitutions, finality);
+ boolean mixed = "true".equals(getAttribute(attrs, "mixed"));
+ for (Node child = complexType.getFirstChild(); child != null;
+ child = child.getNextSibling())
+ {
+ String uri = child.getNamespaceURI();
+ String name = child.getLocalName();
+ if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) &&
+ child.getNodeType() == Node.ELEMENT_NODE)
+ {
+ if ("simpleContent".equals(name))
+ {
+ parseSimpleContent(child, type);
+ }
+ }
+ }
+ if (mixed)
+ {
+ type.contentType = XMLSchema.CONTENT_MIXED;
+ }
+ return type;
+ }
+
+ void parseSimpleContent(Node simpleContent, ComplexType type)
+ throws DatatypeException
+ {
+ for (Node child = simpleContent.getFirstChild(); child != null;
+ child = child.getNextSibling())
+ {
+ String uri = child.getNamespaceURI();
+ String name = child.getLocalName();
+ if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) &&
+ child.getNodeType() == Node.ELEMENT_NODE)
+ {
+ if ("annotation".equals(name))
+ {
+ type.annotations.add(parseAnnotation(child));
+ }
+ else if ("restriction".equals(name))
+ {
+ type.derivationMethod = XMLSchema.FINAL_RESTRICTION;
+ parseRestriction(child, type);
+ }
+ else if ("extension".equals(name))
+ {
+ type.derivationMethod = XMLSchema.FINAL_EXTENSION;
+ parseExtension(child, type);
+ }
+ }
+ }
+ }
+
+ void parseRestriction(Node restriction, ComplexType type)
+ throws DatatypeException
+ {
+ NamedNodeMap attrs = restriction.getAttributes();
+ String base = getAttribute(attrs, "base");
+ QName baseType = asQName(base, restriction);
+ SimpleType simpleType = null;
+ for (Node child = restriction.getFirstChild(); child != null;
+ child = child.getNextSibling())
+ {
+ String uri = child.getNamespaceURI();
+ String name = child.getLocalName();
+ if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) &&
+ child.getNodeType() == Node.ELEMENT_NODE)
+ {
+ if ("annotation".equals(name))
+ {
+ type.annotations.add(parseAnnotation(child));
+ }
+ else if ("simpleType".equals(name))
+ {
+ type.contentType = XMLSchema.CONTENT_SIMPLE;
+ simpleType = parseSimpleType(child);
+ }
+ else if ("minExclusive".equals(name))
+ {
+ }
+ else if ("minInclusive".equals(name))
+ {
+ }
+ else if ("maxExclusive".equals(name))
+ {
+ }
+ else if ("maxInclusive".equals(name))
+ {
+ }
+ else if ("totalDigits".equals(name))
+ {
+ }
+ else if ("fractionDigits".equals(name))
+ {
+ }
+ else if ("length".equals(name))
+ {
+ }
+ else if ("minLength".equals(name))
+ {
+ }
+ else if ("maxLength".equals(name))
+ {
+ }
+ else if ("enumeration".equals(name))
+ {
+ }
+ else if ("whiteSpace".equals(name))
+ {
+ }
+ else if ("pattern".equals(name))
+ {
+ }
+ else if ("attribute".equals(name))
+ {
+ AttributeUse use =
+ (AttributeUse) parseAttribute(child, false);
+ schema.attributeDeclarations.put(use.declaration.name,
+ use.declaration);
+ type.attributeUses.add(use);
+ }
+ else if ("attributeGroup".equals(name))
+ {
+ NamedNodeMap agAttrs = child.getAttributes();
+ String ref = getAttribute(agAttrs, "ref");
+ QName ag = asQName(ref, child);
+ type.attributeUses.add(ag);
+ }
+ else if ("anyAttribute".equals(name))
+ {
+ type.attributeWildcard = parseAnyAttribute(child);
+ }
+ }
+ }
+ }
+
+ void parseExtension(Node extension, ComplexType type)
+ throws DatatypeException
+ {
+ NamedNodeMap attrs = extension.getAttributes();
+ String base = getAttribute(attrs, "base");
+ QName baseType = asQName(base, extension);
+ for (Node child = extension.getFirstChild(); child != null;
+ child = child.getNextSibling())
+ {
+ String uri = child.getNamespaceURI();
+ String name = child.getLocalName();
+ if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) &&
+ child.getNodeType() == Node.ELEMENT_NODE)
+ {
+ if ("annotation".equals(name))
+ {
+ type.annotations.add(parseAnnotation(child));
+ }
+ else if ("attribute".equals(name))
+ {
+ AttributeUse use =
+ (AttributeUse) parseAttribute(child, false);
+ schema.attributeDeclarations.put(use.declaration.name,
+ use.declaration);
+ type.attributeUses.add(use);
+ }
+ else if ("attributeGroup".equals(name))
+ {
+ NamedNodeMap agAttrs = child.getAttributes();
+ String ref = getAttribute(agAttrs, "ref");
+ QName ag = asQName(ref, child);
+ type.attributeUses.add(ag);
+ }
+ else if ("anyAttribute".equals(name))
+ {
+ type.attributeWildcard = parseAnyAttribute(child);
+ }
+ }
+ }
+ }
+
+ AnyAttribute parseAnyAttribute(Node node)
+ {
+ NamedNodeMap attrs = node.getAttributes();
+ String namespace = getAttribute(attrs, "namespace");
+ String pc = getAttribute(attrs, "processContents");
+ int processContents = AnyAttribute.STRICT;
+ if ("lax".equals(pc))
+ {
+ processContents = AnyAttribute.LAX;
+ }
+ else if ("skip".equals(pc))
+ {
+ processContents = AnyAttribute.SKIP;
+ }
+ AnyAttribute ret = new AnyAttribute(namespace, processContents);
+ for (Node child = node.getFirstChild(); child != null;
+ child = child.getNextSibling())
+ {
+ String uri = child.getNamespaceURI();
+ String name = child.getLocalName();
+ if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri) &&
+ child.getNodeType() == Node.ELEMENT_NODE)
+ {
+ if ("annotation".equals(name))
+ {
+ ret.annotation = parseAnnotation(child);
+ }
+ }
+ }
+ return ret;
+ }
+
+ Annotation parseAnnotation(Node node)
+ {
+ // TODO
+ return null;
+ }
+
+ private static String getAttribute(NamedNodeMap attrs, String name)
+ {
+ Node attr = attrs.getNamedItem(name);
+ return (attr == null) ? null : attr.getNodeValue();
+ }
+
+ private static QName asQName(String text, Node resolver)
+ {
+ QName name = QName.valueOf(text);
+ String prefix = name.getPrefix();
+ if (prefix != null && prefix.length() > 0)
+ {
+ String uri = resolver.lookupNamespaceURI(prefix);
+ name = new QName(uri, name.getLocalPart());
+ }
+ return name;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaElementTypeInfo.java b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaElementTypeInfo.java
new file mode 100644
index 000000000..99a59f39d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaElementTypeInfo.java
@@ -0,0 +1,91 @@
+/* XMLSchemaElementTypeInfo.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+import gnu.xml.validation.datatype.SimpleType;
+import gnu.xml.validation.datatype.Type;
+
+/**
+ * Element type information provided by validation against an XML Schema.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class XMLSchemaElementTypeInfo
+ extends XMLSchemaTypeInfo
+{
+
+ final XMLSchema schema;
+ final ElementDeclaration decl;
+ final Type type;
+ boolean nil;
+
+ XMLSchemaElementTypeInfo(XMLSchema schema, ElementDeclaration decl,
+ Type type)
+ {
+ this.schema = schema;
+ this.decl = decl;
+ this.type = type;
+ }
+
+ public String getTypeName()
+ {
+ return type.name.getLocalPart();
+ }
+
+ public String getTypeNamespace()
+ {
+ return type.name.getNamespaceURI();
+ }
+
+ public boolean isDerivedFrom(String typeNamespace, String typeName,
+ int derivationMethod)
+ {
+ if (type instanceof SimpleType)
+ {
+ SimpleType simpleType = (SimpleType) type;
+ return simpleTypeIsDerivedFrom(simpleType, typeNamespace, typeName,
+ derivationMethod);
+ }
+ else
+ {
+ // TODO
+ return false;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaSchemaFactory.java b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaSchemaFactory.java
new file mode 100644
index 000000000..18e91f28d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaSchemaFactory.java
@@ -0,0 +1,171 @@
+/* XMLSchemaSchemaFactory.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+import java.io.IOException;
+import java.net.URL;
+import javax.xml.XMLConstants;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+import org.relaxng.datatype.DatatypeException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.ls.LSResourceResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * Schema factory for W3C XML Schema schemata.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class XMLSchemaSchemaFactory
+ extends SchemaFactory
+{
+
+ LSResourceResolver resourceResolver;
+ ErrorHandler errorHandler;
+
+ public LSResourceResolver getResourceResolver()
+ {
+ return resourceResolver;
+ }
+
+ public void setResourceResolver(LSResourceResolver resourceResolver)
+ {
+ this.resourceResolver = resourceResolver;
+ }
+
+ public ErrorHandler getErrorHandler()
+ {
+ return this.errorHandler;
+ }
+
+ public void setErrorHandler(ErrorHandler errorHandler)
+ {
+ this.errorHandler = errorHandler;
+ }
+
+
+ public boolean isSchemaLanguageSupported(String schemaLanguage)
+ {
+ return XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schemaLanguage);
+ }
+
+ public Schema newSchema()
+ throws SAXException
+ {
+ // TODO
+ throw new UnsupportedOperationException();
+ }
+
+ public Schema newSchema(Source[] schemata)
+ throws SAXException
+ {
+ if (schemata == null || schemata.length != 1)
+ throw new IllegalArgumentException("must specify one source");
+ // TODO multiple sources
+ try
+ {
+ Document doc = getDocument(schemata[0]);
+ XMLSchemaBuilder builder = new XMLSchemaBuilder();
+ builder.parseSchema(doc);
+ return builder.schema;
+ }
+ catch (IOException e)
+ {
+ SAXException e2 = new SAXException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ catch (DatatypeException e)
+ {
+ SAXException e2 = new SAXException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ private static Document getDocument(Source source)
+ throws SAXException, IOException
+ {
+ if (source instanceof DOMSource)
+ {
+ Node node = ((DOMSource) source).getNode();
+ if (node != null && node instanceof Document)
+ return (Document) node;
+ }
+ String url = source.getSystemId();
+ try
+ {
+ InputSource input = new InputSource(url);
+ if (source instanceof StreamSource)
+ {
+ StreamSource streamSource = (StreamSource) source;
+ input.setByteStream(streamSource.getInputStream());
+ input.setCharacterStream(streamSource.getReader());
+ }
+ if (input.getByteStream() == null &&
+ input.getCharacterStream() == null &&
+ url != null)
+ input.setByteStream(new URL(url).openStream());
+ DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
+ f.setNamespaceAware(true);
+ f.setCoalescing(true);
+ f.setExpandEntityReferences(true);
+ f.setIgnoringComments(true);
+ f.setIgnoringElementContentWhitespace(true);
+ DocumentBuilder b = f.newDocumentBuilder();
+ return b.parse(input);
+ }
+ catch (ParserConfigurationException e)
+ {
+ SAXException e2 = new SAXException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaTypeInfo.java b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaTypeInfo.java
new file mode 100644
index 000000000..c1ca95114
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaTypeInfo.java
@@ -0,0 +1,76 @@
+/* XMLSchemaTypeInfo.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+import org.w3c.dom.TypeInfo;
+import gnu.xml.validation.datatype.SimpleType;
+
+/**
+ * Abstract superclass providing simple type derivation.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+abstract class XMLSchemaTypeInfo
+ implements TypeInfo
+{
+
+ protected boolean simpleTypeIsDerivedFrom(SimpleType simpleType,
+ String typeNamespace,
+ String typeName,
+ int derivationMethod)
+ {
+ switch (derivationMethod)
+ {
+ case TypeInfo.DERIVATION_RESTRICTION:
+ SimpleType baseType = simpleType.baseType;
+ while (baseType != null)
+ {
+ if (baseType.name.getNamespaceURI().equals(typeNamespace) &&
+ baseType.name.getLocalPart().equals(typeName))
+ {
+ return true;
+ }
+ baseType = baseType.baseType;
+ }
+ break;
+ // TODO other methods
+ }
+ return false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaTypeInfoProvider.java b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaTypeInfoProvider.java
new file mode 100644
index 000000000..4fc80d34b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaTypeInfoProvider.java
@@ -0,0 +1,81 @@
+/* XMLSchemaTypeInfoProvider.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+import javax.xml.validation.TypeInfoProvider;
+import org.w3c.dom.TypeInfo;
+
+/**
+ * TypeInfo provider for XML Schema validator handler.
+ * This simply delegates to the handler. It wouldn't be required if
+ * TypeInfoProvider were an interface instead of an abstract class.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class XMLSchemaTypeInfoProvider
+ extends TypeInfoProvider
+{
+
+ final XMLSchemaValidatorHandler handler;
+
+ XMLSchemaTypeInfoProvider(XMLSchemaValidatorHandler handler)
+ {
+ this.handler = handler;
+ }
+
+ public TypeInfo getElementTypeInfo()
+ {
+ return handler.getElementTypeInfo();
+ }
+
+ public TypeInfo getAttributeTypeInfo(int index)
+ {
+ return handler.getAttributeTypeInfo(index);
+ }
+
+ public boolean isIdAttribute(int index)
+ {
+ return handler.isIdAttribute(index);
+ }
+
+ public boolean isSpecified(int index)
+ {
+ return handler.isSpecified(index);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaValidator.java b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaValidator.java
new file mode 100644
index 000000000..6239ba6c6
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaValidator.java
@@ -0,0 +1,97 @@
+/* XMLSchemaValidator.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+import java.io.IOException;
+import javax.xml.validation.Validator;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import org.w3c.dom.ls.LSResourceResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+
+/**
+ * JAXP validator for an XML Schema.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class XMLSchemaValidator
+ extends Validator
+{
+
+ final XMLSchema schema;
+
+ ErrorHandler errorHandler;
+ LSResourceResolver resourceResolver;
+
+ XMLSchemaValidator(XMLSchema schema)
+ {
+ this.schema = schema;
+ }
+
+ public void reset()
+ {
+ }
+
+ public void validate(Source source, Result result)
+ throws SAXException, IOException
+ {
+ // TODO
+ }
+
+ public ErrorHandler getErrorHandler()
+ {
+ return errorHandler;
+ }
+
+ public void setErrorHandler(ErrorHandler errorHandler)
+ {
+ this.errorHandler = errorHandler;
+ }
+
+ public LSResourceResolver getResourceResolver()
+ {
+ return resourceResolver;
+ }
+
+ public void setResourceResolver(LSResourceResolver resourceResolver)
+ {
+ this.resourceResolver = resourceResolver;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaValidatorHandler.java b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaValidatorHandler.java
new file mode 100644
index 000000000..6c4f1f392
--- /dev/null
+++ b/libjava/classpath/gnu/xml/validation/xmlschema/XMLSchemaValidatorHandler.java
@@ -0,0 +1,393 @@
+/* XMLSchemaValidatorHandler.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.validation.xmlschema;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import javax.xml.validation.TypeInfoProvider;
+import javax.xml.validation.ValidatorHandler;
+import org.relaxng.datatype.DatatypeException;
+import org.relaxng.datatype.DatatypeLibrary;
+import org.relaxng.datatype.helpers.DatatypeLibraryLoader;
+import org.w3c.dom.TypeInfo;
+import org.w3c.dom.ls.LSResourceResolver;
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.ext.Attributes2Impl;
+import org.xml.sax.helpers.NamespaceSupport;
+import gnu.xml.validation.datatype.SimpleType;
+import gnu.xml.validation.datatype.Type;
+
+/**
+ * Streaming validator.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class XMLSchemaValidatorHandler
+ extends ValidatorHandler
+{
+
+ final XMLSchema schema;
+ final TypeInfoProvider typeInfoProvider;
+ final NamespaceSupport namespaceSupport;
+ final DatatypeLibrary typeLibrary;
+ Locator loc;
+ ContentHandler contentHandler;
+ ErrorHandler errorHandler;
+ LSResourceResolver resourceResolver;
+ final LinkedList context; // element context
+ final ArrayList attributes; // attribute context;
+
+ XMLSchemaValidatorHandler(XMLSchema schema)
+ {
+ this.schema = schema;
+ typeInfoProvider = new XMLSchemaTypeInfoProvider(this);
+ namespaceSupport = new NamespaceSupport();
+ context = new LinkedList();
+ attributes = new ArrayList();
+ final String ns = XMLConstants.W3C_XML_SCHEMA_NS_URI;
+ typeLibrary = new DatatypeLibraryLoader().createDatatypeLibrary(ns);
+ }
+
+ public ContentHandler getContentHandler()
+ {
+ return contentHandler;
+ }
+
+ public void setContentHandler(ContentHandler contentHandler)
+ {
+ this.contentHandler = contentHandler;
+ }
+
+ public ErrorHandler getErrorHandler()
+ {
+ return errorHandler;
+ }
+
+ public void setErrorHandler(ErrorHandler errorHandler)
+ {
+ this.errorHandler = errorHandler;
+ }
+
+ public LSResourceResolver getResourceResolver()
+ {
+ return resourceResolver;
+ }
+
+ public void setResourceResolver(LSResourceResolver resourceResolver)
+ {
+ this.resourceResolver = resourceResolver;
+ }
+
+ public TypeInfoProvider getTypeInfoProvider()
+ {
+ return typeInfoProvider;
+ }
+
+ TypeInfo getElementTypeInfo()
+ {
+ return (XMLSchemaElementTypeInfo) context.getFirst();
+ }
+
+ TypeInfo getAttributeTypeInfo(int index)
+ {
+ return (XMLSchemaAttributeTypeInfo) attributes.get(index);
+ }
+
+ boolean isIdAttribute(int index)
+ {
+ XMLSchemaAttributeTypeInfo typeInfo =
+ (XMLSchemaAttributeTypeInfo) attributes.get(index);
+ return typeInfo.id;
+ }
+
+ boolean isSpecified(int index)
+ {
+ XMLSchemaAttributeTypeInfo typeInfo =
+ (XMLSchemaAttributeTypeInfo) attributes.get(index);
+ return typeInfo.specified;
+ }
+
+ public void setDocumentLocator(Locator locator)
+ {
+ loc = locator;
+ if (contentHandler != null)
+ {
+ contentHandler.setDocumentLocator(locator);
+ }
+ }
+
+ public void startDocument()
+ throws SAXException
+ {
+ namespaceSupport.reset();
+ context.clear();
+ attributes.clear();
+ if (contentHandler != null)
+ {
+ contentHandler.startDocument();
+ }
+ }
+
+ public void endDocument()
+ throws SAXException
+ {
+ if (contentHandler != null)
+ {
+ contentHandler.endDocument();
+ }
+ }
+
+ public void startPrefixMapping(String prefix, String uri)
+ throws SAXException
+ {
+ namespaceSupport.declarePrefix(prefix, uri);
+ if (contentHandler != null)
+ {
+ contentHandler.startPrefixMapping(prefix, uri);
+ }
+ }
+
+ public void endPrefixMapping(String prefix)
+ throws SAXException
+ {
+ if (contentHandler != null)
+ {
+ contentHandler.endPrefixMapping(prefix);
+ }
+ }
+
+ public void startElement(String uri, String localName, String qName,
+ Attributes atts)
+ throws SAXException
+ {
+ namespaceSupport.pushContext();
+ QName name = new QName(uri, localName);
+ ElementDeclaration decl =
+ (ElementDeclaration) schema.elementDeclarations.get(name);
+ // Validation Rule: Element Locally Valid (Element)
+ String xsiType =
+ atts.getValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type");
+ xsiType = xsiType.trim(); // normalise
+ String xsiNil =
+ atts.getValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil");
+ Type type = decl.datatype;
+ if (xsiType.length() > 0)
+ {
+ try
+ {
+ Type specifiedType = resolveType(xsiType);
+ // TODO 4.3
+ type = specifiedType;
+ }
+ catch (DatatypeException e) // 4.1, 4.2
+ {
+ ValidationException e2 =
+ new ValidationException("Can't resolve type " + xsiType,
+ loc);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+ XMLSchemaElementTypeInfo typeInfo =
+ new XMLSchemaElementTypeInfo(schema, decl, type);
+ if (decl == null) // 1
+ {
+ throw new ValidationException("No declaration for " + name, loc);
+ }
+ if (decl.isAbstract) // 2
+ {
+ throw new ValidationException("Declaration for " + name +
+ " is abstract", loc);
+ }
+ if (xsiNil.length() > 0)
+ {
+ if (!decl.nillable) // 3.1
+ {
+ throw new ValidationException("Declaration for " + name +
+ " is nillable but xsi:nil present",
+ loc);
+ }
+ else if ("true".equals(xsiNil)) // 3.2
+ {
+ typeInfo.nil = true;
+ if (decl.type == XMLSchema.CONSTRAINT_FIXED) // 3.2.2
+ {
+ throw new ValidationException("Declaration for " + name +
+ " is fixed but xsi:nil is true",
+ loc);
+ }
+ }
+ }
+ // TODO 5, 6, 7
+
+ // parent
+ if (!context.isEmpty())
+ {
+ XMLSchemaElementTypeInfo parent =
+ (XMLSchemaElementTypeInfo) context.getFirst();
+ if (parent.nil) // Element Locally Valid (Element) 3.2.1
+ {
+ throw new ValidationException("Parent of " + qName +
+ " is declared xsi:nil", loc);
+ }
+ // TODO
+ }
+ context.addFirst(typeInfo);
+ // attributes
+ int len = atts.getLength();
+ Attributes2Impl atts2 = new Attributes2Impl();
+ int count = 0;
+ for (int i = 0; i < len; i++)
+ {
+ String attUri = atts.getURI(i);
+ String attLocalName = atts.getLocalName(i);
+ String attQName = atts.getQName(i);
+ String attValue = atts.getValue(i);
+
+ if (XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI.equals(attUri))
+ {
+ continue; // ?
+ }
+
+ QName attName = new QName(attUri, attLocalName);
+ AttributeDeclaration attDecl =
+ (AttributeDeclaration) schema.attributeDeclarations.get(attName);
+ boolean declared = (attDecl != null);
+
+ String attType = (attDecl != null) ?
+ attDecl.datatype.toString() : "CDATA";
+ XMLSchemaAttributeTypeInfo attTypeInfo =
+ new XMLSchemaAttributeTypeInfo(schema, attDecl, true);
+ attributes.add(attTypeInfo);
+
+ atts2.addAttribute(attUri, attLocalName, attQName, attType, attValue);
+ atts2.setDeclared(count, declared);
+ atts2.setSpecified(count, true);
+ count++;
+ }
+ // add defaulted attributes to atts2
+ // TODO
+ // atts2.setSpecified(count, false);
+ if (contentHandler != null)
+ {
+ contentHandler.startElement(uri, localName, qName, atts2);
+ }
+ }
+
+ public void endElement(String uri, String localName, String qName)
+ throws SAXException
+ {
+ // TODO check all required have been seen
+ context.removeFirst();
+ attributes.clear();
+ namespaceSupport.popContext();
+ if (contentHandler != null)
+ {
+ contentHandler.endElement(uri, localName, qName);
+ }
+ }
+
+ public void characters(char[] ch, int start, int length)
+ throws SAXException
+ {
+ XMLSchemaElementTypeInfo parent =
+ (XMLSchemaElementTypeInfo) context.getFirst();
+ if (parent.nil) // Element Locally Valid (Element) 3.2.1
+ {
+ throw new ValidationException(parent.decl.name.toString() +
+ " is declared xsi:nil",
+ loc);
+ }
+ // TODO
+ if (contentHandler != null)
+ {
+ contentHandler.characters(ch, start, length);
+ }
+ }
+
+ public void ignorableWhitespace(char[] ch, int start, int length)
+ throws SAXException
+ {
+ if (contentHandler != null)
+ {
+ contentHandler.ignorableWhitespace(ch, start, length);
+ }
+ }
+
+ public void processingInstruction(String target, String data)
+ throws SAXException
+ {
+ if (contentHandler != null)
+ {
+ contentHandler.processingInstruction(target, data);
+ }
+ }
+
+ public void skippedEntity(String name)
+ throws SAXException
+ {
+ if (contentHandler != null)
+ {
+ contentHandler.skippedEntity(name);
+ }
+ }
+
+ Type resolveType(String value)
+ throws DatatypeException
+ {
+ QName name = QName.valueOf(value);
+ String prefix = name.getPrefix();
+ String localName = name.getLocalPart();
+ if (prefix != null && prefix.length() > 0)
+ {
+ String uri = namespaceSupport.getURI(prefix);
+ if (!XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri))
+ return null;
+ }
+ if ("anyType".equals(localName))
+ return Type.ANY_TYPE;
+ return (SimpleType) typeLibrary.createDatatype(localName);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/AndExpr.java b/libjava/classpath/gnu/xml/xpath/AndExpr.java
new file mode 100644
index 000000000..9213a40cc
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/AndExpr.java
@@ -0,0 +1,87 @@
+/* AndExpr.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * Logical and.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class AndExpr
+ extends Expr
+{
+
+ final Expr lhs;
+ final Expr rhs;
+
+ public AndExpr(Expr lhs, Expr rhs)
+ {
+ this.lhs = lhs;
+ this.rhs = rhs;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object left = lhs.evaluate(context, pos, len);
+ if (!_boolean(context, left))
+ {
+ return Boolean.FALSE;
+ }
+ Object right = rhs.evaluate(context, pos, len);
+ return _boolean(context, right) ? Boolean.TRUE : Boolean.FALSE;
+ }
+
+ public Expr clone(Object context)
+ {
+ return new AndExpr(lhs.clone(context), rhs.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return (lhs.references(var) || rhs.references(var));
+ }
+
+ public String toString()
+ {
+ return lhs + " and " + rhs;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/ArithmeticExpr.java b/libjava/classpath/gnu/xml/xpath/ArithmeticExpr.java
new file mode 100644
index 000000000..a7bc62f2e
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/ArithmeticExpr.java
@@ -0,0 +1,170 @@
+/* ArithmeticExpr.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import gnu.java.lang.CPStringBuilder;
+
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * Binary arithmetic expression.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class ArithmeticExpr
+ extends Expr
+{
+
+ static final int ADD = 0;
+ static final int SUBTRACT = 1;
+ static final int MULTIPLY = 2;
+ static final int DIVIDE = 3;
+ static final int MODULO = 4;
+
+ final Expr lhs;
+ final Expr rhs;
+ final int op;
+
+ ArithmeticExpr(Expr lhs, Expr rhs, int op)
+ {
+ this.lhs = lhs;
+ this.rhs = rhs;
+ switch (op)
+ {
+ case ADD:
+ case SUBTRACT:
+ case MULTIPLY:
+ case DIVIDE:
+ case MODULO:
+ this.op = op;
+ break;
+ default:
+ throw new IllegalArgumentException();
+ }
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object left = lhs.evaluate(context, pos, len);
+ Object right = rhs.evaluate(context, pos, len);
+
+ double ln = _number(context, left);
+ double rn = _number(context, right);
+ switch (op)
+ {
+ case ADD:
+ return new Double(ln + rn);
+ case SUBTRACT:
+ return new Double(ln - rn);
+ case MULTIPLY:
+ return new Double(ln * rn);
+ case DIVIDE:
+ if (rn == 0.0d || rn == -0.0d)
+ {
+ if (ln == 0.0d || ln == -0.0d)
+ {
+ return new Double(Double.NaN);
+ }
+ else
+ {
+ return new Double(ln < 0.0d ?
+ Double.NEGATIVE_INFINITY :
+ Double.POSITIVE_INFINITY);
+ }
+ }
+ return new Double(ln / rn);
+ case MODULO:
+ if (rn == 0.0d || rn == 0.0d)
+ {
+ if (ln == 0.0d || ln == -0.0d)
+ {
+ return new Double(Double.NaN);
+ }
+ else
+ {
+ return new Double(ln < 0.0d ?
+ Double.NEGATIVE_INFINITY :
+ Double.POSITIVE_INFINITY);
+ }
+ }
+ return new Double(ln % rn);
+ default:
+ throw new IllegalStateException();
+ }
+ }
+
+ public Expr clone(Object context)
+ {
+ return new ArithmeticExpr(lhs.clone(context), rhs.clone(context), op);
+ }
+
+ public boolean references(QName var)
+ {
+ return (lhs.references(var) || rhs.references(var));
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ buf.append(lhs);
+ buf.append(' ');
+ switch (op)
+ {
+ case ADD:
+ buf.append('+');
+ break;
+ case SUBTRACT:
+ buf.append('-');
+ break;
+ case MULTIPLY:
+ buf.append('*');
+ break;
+ case DIVIDE:
+ buf.append("div");
+ break;
+ case MODULO:
+ buf.append("mod");
+ break;
+ }
+ buf.append(' ');
+ buf.append(rhs);
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/BooleanFunction.java b/libjava/classpath/gnu/xml/xpath/BooleanFunction.java
new file mode 100644
index 000000000..05e0bd5bc
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/BooleanFunction.java
@@ -0,0 +1,95 @@
+/* BooleanFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>boolean</code> function converts its argument to a boolean as
+ * follows:
+ * <ul>
+ * <li>a number is true if and only if it is neither positive or negative
+ * zero nor NaN</li>
+ * <li>a node-set is true if and only if it is non-empty</li>
+ * <li>a string is true if and only if its length is non-zero</li>
+ * <li>an object of a type other than the four basic types is converted to a
+ * boolean in a way that is dependent on that type</li>
+ * </ul>
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class BooleanFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ BooleanFunction(List<Expr> args)
+ {
+ this(args.get(0));
+ }
+
+ BooleanFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val = arg.evaluate(context, pos, len);
+ return _boolean(context, val) ? Boolean.TRUE : Boolean.FALSE;
+ }
+
+ public Expr clone(Object context)
+ {
+ return new BooleanFunction(arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return arg.references(var);
+ }
+
+ public String toString()
+ {
+ return "boolean(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/CeilingFunction.java b/libjava/classpath/gnu/xml/xpath/CeilingFunction.java
new file mode 100644
index 000000000..aa906e0dd
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/CeilingFunction.java
@@ -0,0 +1,89 @@
+/* CeilingFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>ceiling</code> function returns the smallest (closest to
+ * negative infinity) number that is not less than the argument and that
+ * is an integer.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class CeilingFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ CeilingFunction(List<Expr> args)
+ {
+ this(args.get(0));
+ }
+
+ CeilingFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val = arg.evaluate(context, pos, len);
+ double n = _number(context, val);
+ return new Double(Math.ceil(n));
+ }
+
+ public Expr clone(Object context)
+ {
+ return new CeilingFunction(arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return arg.references(var);
+ }
+
+ public String toString()
+ {
+ return "ceiling(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/ConcatFunction.java b/libjava/classpath/gnu/xml/xpath/ConcatFunction.java
new file mode 100644
index 000000000..58096e5ac
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/ConcatFunction.java
@@ -0,0 +1,115 @@
+/* ConcatFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>concat</code> function returns the concatenation of its arguments.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class ConcatFunction
+ extends Expr
+{
+
+ final List<Expr> args;
+
+ ConcatFunction(List<Expr> args)
+ {
+ this.args = args;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ for (Expr arg : args)
+ {
+ Object val = arg.evaluate(context, pos, len);
+ buf.append(_string(context, val));
+ }
+ return buf.toString();
+ }
+
+ public Expr clone(Object context)
+ {
+ int len = args.size();
+ List<Expr> args2 = new ArrayList<Expr>(len);
+ for (int i = 0; i < len; i++)
+ {
+ args2.add(args.get(i).clone(context));
+ }
+ return new ConcatFunction(args2);
+ }
+
+ public boolean references(QName var)
+ {
+ for (Iterator<Expr> i = args.iterator(); i.hasNext(); )
+ {
+ if (i.next().references(var))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("concat(");
+ int len = args.size();
+ for (int i = 0; i < len; i++)
+ {
+ if (i > 0)
+ {
+ buf.append(',');
+ }
+ buf.append(args.get(i));
+ }
+ buf.append(')');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/Constant.java b/libjava/classpath/gnu/xml/xpath/Constant.java
new file mode 100644
index 000000000..5ed39cc05
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/Constant.java
@@ -0,0 +1,98 @@
+/* Constant.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * Constant value (string literal or number).
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class Constant
+ extends Expr
+{
+
+ final Object value;
+
+ public Constant(Object value)
+ {
+ this.value = value;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ return value;
+ }
+
+ public Expr clone(Object context)
+ {
+ return new Constant(value);
+ }
+
+ public boolean references(QName var)
+ {
+ return false;
+ }
+
+ public String toString()
+ {
+ String ret = value.toString();
+ if (value instanceof String)
+ {
+ if (ret.indexOf('\'') == -1)
+ {
+ return '\'' + ret + '\'';
+ }
+ else
+ {
+ return '"' + ret + '"';
+ }
+ }
+ if (value instanceof Double)
+ {
+ if (ret.endsWith(".0"))
+ {
+ ret = ret.substring(0, ret.length() - 2);
+ }
+ }
+ return ret;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/ContainsFunction.java b/libjava/classpath/gnu/xml/xpath/ContainsFunction.java
new file mode 100644
index 000000000..473d2532f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/ContainsFunction.java
@@ -0,0 +1,93 @@
+/* ContainsFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>contains</code> function returns true if the first argument
+ * string contains the second argument string, and otherwise returns false.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class ContainsFunction
+ extends Expr
+{
+
+ final Expr arg1;
+ final Expr arg2;
+
+ ContainsFunction(List<Expr> args)
+ {
+ this(args.get(0), args.get(1));
+ }
+
+ ContainsFunction(Expr arg1, Expr arg2)
+ {
+ this.arg1 = arg1;
+ this.arg2 = arg2;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val1 = arg1.evaluate(context, pos, len);
+ Object val2 = arg2.evaluate(context, pos, len);
+ String s1 = _string(context, val1);
+ String s2 = _string(context, val2);
+ return (s1.indexOf(s2) != -1) ? Boolean.TRUE : Boolean.FALSE;
+ }
+
+ public Expr clone(Object context)
+ {
+ return new ContainsFunction(arg1.clone(context), arg2.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return (arg1.references(var) || arg2.references(var));
+ }
+
+ public String toString()
+ {
+ return "contains(" + arg1 + "," + arg2 + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/CountFunction.java b/libjava/classpath/gnu/xml/xpath/CountFunction.java
new file mode 100644
index 000000000..e1892a058
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/CountFunction.java
@@ -0,0 +1,89 @@
+/* CountFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.Collection;
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>count</code> function returns the number of nodes in the
+ * argument node-set.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class CountFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ CountFunction(List<Expr> args)
+ {
+ this(args.get(0));
+ }
+
+ CountFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val = arg.evaluate(context, pos, len);
+ return new Double((double) ((Collection<?>) val).size());
+ }
+
+ public Expr clone(Object context)
+ {
+ return new CountFunction(arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return arg.references(var);
+ }
+
+ public String toString()
+ {
+ return "count(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/DocumentOrderComparator.java b/libjava/classpath/gnu/xml/xpath/DocumentOrderComparator.java
new file mode 100644
index 000000000..c4471c120
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/DocumentOrderComparator.java
@@ -0,0 +1,57 @@
+/* DocumentOrderComparator.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.Comparator;
+import org.w3c.dom.Node;
+
+/**
+ * Sorts nodes into document order.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class DocumentOrderComparator
+ implements Comparator<Node>
+{
+
+ public int compare(Node n1, Node n2)
+ {
+ return (int) n1.compareDocumentPosition(n2);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/EqualityExpr.java b/libjava/classpath/gnu/xml/xpath/EqualityExpr.java
new file mode 100644
index 000000000..133e1ff20
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/EqualityExpr.java
@@ -0,0 +1,274 @@
+/* EqualityExpr.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.Collection;
+import java.util.Iterator;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * Boolean equality expression.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class EqualityExpr
+ extends Expr
+{
+
+ final Expr lhs;
+ final Expr rhs;
+ final boolean invert;
+
+ EqualityExpr(Expr lhs, Expr rhs, boolean invert)
+ {
+ this.lhs = lhs;
+ this.rhs = rhs;
+ this.invert = invert;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ boolean val = evaluateImpl(context, pos, len);
+ if (invert)
+ {
+ return val ? Boolean.FALSE : Boolean.TRUE;
+ }
+ else
+ {
+ return val ? Boolean.TRUE : Boolean.FALSE;
+ }
+ }
+
+ private boolean evaluateImpl(Node context, int pos, int len)
+ {
+ Object left = lhs.evaluate(context, pos, len);
+ Object right = rhs.evaluate(context, pos, len);
+
+ /*
+ * If both objects to be compared are node-sets, then the comparison
+ * will be true if and only if there is a node in the first node-set and
+ * a node in the second node-set such that the result of performing the
+ * comparison on the string-values of the two nodes is true.
+ */
+ boolean flns = left instanceof Collection;
+ boolean frns = right instanceof Collection;
+ if (flns && frns)
+ {
+ /* Suppression is safe, as we know context produces Collection<Node> */
+ @SuppressWarnings("unchecked")
+ Collection<Node> lns = (Collection<Node>) left;
+ @SuppressWarnings("unchecked")
+ Collection<Node> rns = (Collection<Node>) right;
+ if (lns.isEmpty())
+ {
+ return false;
+ }
+ boolean all = true;
+ for (Node ltest : lns)
+ {
+ for (Node rtest : rns)
+ {
+ if (ltest == rtest || ltest.equals(rtest))
+ {
+ // much shorter
+ if (!invert)
+ {
+ return true;
+ }
+ }
+ else if (stringValue(ltest).equals(stringValue(rtest)))
+ {
+ if (!invert)
+ {
+ return true;
+ }
+ }
+ else
+ {
+ all = false;
+ }
+ }
+ }
+ return all;
+ }
+ /*
+ * If one object to be compared is a node-set and the other is a number,
+ * then the comparison will be true if and only if there is a node in
+ * the node-set such that the result of performing the comparison on the
+ * number to be compared and on the result of converting the
+ * string-value of that node to a number using the number function is
+ * true.
+ */
+ boolean fln = left instanceof Double;
+ boolean frn = right instanceof Double;
+ if ((flns && frn) || (frns && fln))
+ {
+ /* Suppression is safe, as we know context produces Collection<Node> */
+ @SuppressWarnings("unchecked")
+ Collection<Node> ns = flns ? (Collection<Node>) left : (Collection<Node>) right;
+ double n = fln ? ((Double) left).doubleValue() :
+ ((Double) right).doubleValue();
+ boolean all = true;
+ for (Node test : ns)
+ {
+ double nn = _number(context, stringValue(test));
+ if (nn == n)
+ {
+ if (!invert)
+ {
+ return true;
+ }
+ }
+ else
+ {
+ all = false;
+ }
+ }
+ return invert ? all : false;
+ }
+ /*
+ * If one object to be compared is a node-set and the other is a
+ * string, then the comparison will be true if and only if there is a
+ * node in the node-set such that the result of performing the
+ * comparison on the string-value of the node and the other string is
+ * true.
+ */
+ boolean fls = left instanceof String;
+ boolean frs = right instanceof String;
+ if ((flns && frs) || (frns && fls))
+ {
+ /* Suppression is safe, as we know context produces Collection<Node> */
+ @SuppressWarnings("unchecked")
+ Collection<Node> ns = flns ? (Collection<Node>) left : (Collection<Node>) right;
+ String s = fls ? (String) left : (String) right;
+ boolean all = true;
+ for (Node test : ns)
+ {
+ if (stringValue(test).equals(s))
+ {
+ if (!invert)
+ {
+ return true;
+ }
+ }
+ else
+ {
+ all = false;
+ }
+ }
+ return invert ? all : false;
+ }
+ /*
+ * If one object to be compared is a node-set and the other is a
+ * boolean, then the comparison will be true if and only if the result
+ * of performing the comparison on the boolean and on the result of
+ * converting the node-set to a boolean using the boolean function is
+ * true.
+ */
+ boolean flb = left instanceof Boolean;
+ boolean frb = right instanceof Boolean;
+ if ((flns && frb) || (frns && flb))
+ {
+ /* Suppression is safe, as we know context produces Collection<Node> */
+ @SuppressWarnings("unchecked")
+ Collection<Node> ns = flns ? (Collection<Node>) left : (Collection<Node>) right;
+ boolean b = flb ? ((Boolean) left).booleanValue() :
+ ((Boolean) right).booleanValue();
+ return _boolean(context, ns) == b;
+ }
+ /*
+ * If at least one object to be compared is a boolean, then each object
+ * to be compared is converted to a boolean as if by applying the
+ * boolean function.
+ */
+ if (flb || frb)
+ {
+ boolean lb = flb ? ((Boolean) left).booleanValue() :
+ _boolean(context, left);
+ boolean rb = frb ? ((Boolean) right).booleanValue() :
+ _boolean(context, right);
+ return lb == rb;
+ }
+ /*
+ * Otherwise, if at least one object to be compared is
+ * a number, then each object to be compared is converted to a number as
+ * if by applying the number function.
+ */
+ if (fln || frn)
+ {
+ double ln = fln ? ((Double) left).doubleValue() :
+ _number(context, left);
+ double rn = frn ? ((Double) right).doubleValue() :
+ _number(context, right);
+ return ln == rn;
+ }
+ /*
+ * Otherwise, both objects to be
+ * compared are converted to strings as if by applying the string
+ * function.
+ */
+ String ls = fls ? (String) left : _string(context, left);
+ String rs = frs ? (String) right : _string(context, right);
+ return ls.equals(rs);
+ }
+
+ public Expr clone(Object context)
+ {
+ return new EqualityExpr(lhs.clone(context), rhs.clone(context), invert);
+ }
+
+ public boolean references(QName var)
+ {
+ return (lhs.references(var) || rhs.references(var));
+ }
+
+ public String toString()
+ {
+ if (invert)
+ {
+ return lhs + " != " + rhs;
+ }
+ else
+ {
+ return lhs + " = " + rhs;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/Expr.java b/libjava/classpath/gnu/xml/xpath/Expr.java
new file mode 100644
index 000000000..0e98f43f5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/Expr.java
@@ -0,0 +1,549 @@
+/* Expr.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.io.IOException;
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import java.util.StringTokenizer;
+import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * An XPath expression.
+ * This can be evaluated in the context of a node to produce a result.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public abstract class Expr
+ implements XPathExpression
+{
+
+ protected static final Comparator<Node> documentOrderComparator =
+ new DocumentOrderComparator();
+
+ protected static final DecimalFormat decimalFormat =
+ new DecimalFormat("####################################################" +
+ ".####################################################",
+ new DecimalFormatSymbols(Locale.US));
+
+ static class ExprNodeSet implements NodeList
+ {
+
+ private ArrayList<Node> list;
+
+ ExprNodeSet(Collection<Node> collection)
+ {
+ if (collection instanceof ArrayList)
+ list = (ArrayList<Node>) collection;
+ else
+ list = new ArrayList<Node>(collection);
+ }
+
+ public int getLength()
+ {
+ return list.size();
+ }
+
+ public Node item(int index)
+ {
+ try
+ {
+ return list.get(index);
+ }
+ catch (ArrayIndexOutOfBoundsException e)
+ {
+ return null;
+ }
+ }
+
+ }
+
+ public Object evaluate(Object item, QName returnType)
+ throws XPathExpressionException
+ {
+ Object ret = null;
+ Node context = null;
+ if (item instanceof Node)
+ {
+ context = (Node) item;
+ ret = evaluate(context, 1, 1);
+ if (XPathConstants.STRING == returnType &&
+ !(ret instanceof String))
+ {
+ ret = _string(context, ret);
+ }
+ else if (XPathConstants.NUMBER == returnType &&
+ !(ret instanceof Double))
+ {
+ ret = new Double(_number(context, ret));
+ }
+ else if (XPathConstants.BOOLEAN == returnType &&
+ !(ret instanceof Boolean))
+ {
+ ret = _boolean(context, ret) ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if (XPathConstants.NODE == returnType)
+ {
+ if (ret instanceof Collection)
+ {
+ /* Suppression is safe, as we know context
+ produces Collection<Node> */
+ @SuppressWarnings("unchecked")
+ Collection<Node> ns = (Collection<Node>) ret;
+ switch (ns.size())
+ {
+ case 0:
+ ret = null;
+ break;
+ case 1:
+ ret = ns.iterator().next();
+ break;
+ default:
+ throw new XPathExpressionException("multiple nodes in node-set");
+ }
+ }
+ else if (ret != null)
+ {
+ throw new XPathExpressionException("return value is not a node-set");
+ }
+ }
+ else if (XPathConstants.NODESET == returnType)
+ {
+ if (ret != null && !(ret instanceof Collection))
+ {
+ throw new XPathExpressionException("return value is not a node-set");
+ }
+ if (ret != null)
+ {
+ /* Suppression is safe, as we know context produces Collection<Node> */
+ @SuppressWarnings("unchecked")
+ Collection<Node> nodes = (Collection<Node>) ret;
+ ret = new ExprNodeSet(nodes);
+ }
+ }
+ }
+ return ret;
+ }
+
+ public String evaluate(Object item)
+ throws XPathExpressionException
+ {
+ return (String) evaluate(item, XPathConstants.STRING);
+ }
+
+ public Object evaluate(InputSource source, QName returnType)
+ throws XPathExpressionException
+ {
+ try
+ {
+ DocumentBuilderFactory factory =
+ new gnu.xml.dom.JAXPFactory();
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ Document doc = builder.parse(source);
+ return evaluate(doc, returnType);
+ }
+ catch (ParserConfigurationException e)
+ {
+ throw new XPathExpressionException(e);
+ }
+ catch (SAXException e)
+ {
+ throw new XPathExpressionException(e);
+ }
+ catch (IOException e)
+ {
+ throw new XPathExpressionException(e);
+ }
+ }
+
+ public String evaluate(InputSource source)
+ throws XPathExpressionException
+ {
+ return (String) evaluate(source, XPathConstants.STRING);
+ }
+
+ public abstract Object evaluate(Node context, int pos, int len);
+
+ public abstract Expr clone(Object context);
+
+ public abstract boolean references(QName var);
+
+ /* -- 4.1 Node Set Functions -- */
+
+ /**
+ * The id function selects elements by their unique ID.
+ * When the argument to id is of type node-set, then the result is
+ * the union of the result of applying id to the string-value of each of
+ * the nodes in the argument node-set. When the argument to id is of any
+ * other type, the argument is converted to a string as if by a call to
+ * the string function; the string is split into a whitespace-separated
+ * list of tokens (whitespace is any sequence of characters matching the
+ * production S); the result is a node-set containing the elements in the
+ * same document as the context node that have a unique ID equal to any of
+ * the tokens in the list.
+ */
+ public static Collection<Node> _id(Node context, Object object)
+ {
+ Set<Node> ret = new HashSet<Node>();
+ if (object instanceof Collection)
+ {
+ /* Suppression is safe, as the iteration will check each value is a Node */
+ @SuppressWarnings("unchecked")
+ Collection<Node> nodeSet = (Collection<Node>) object;
+ for (Iterator<Node> i = nodeSet.iterator(); i.hasNext(); )
+ {
+ String string = stringValue(i.next());
+ ret.addAll(_id (context, string));
+ }
+ }
+ else
+ {
+ Document doc = (context instanceof Document) ? (Document) context :
+ context.getOwnerDocument();
+ String string = _string(context, object);
+ StringTokenizer st = new StringTokenizer(string, " \t\r\n");
+ while (st.hasMoreTokens())
+ {
+ Node element = doc.getElementById(st.nextToken());
+ if (element != null)
+ {
+ ret.add(element);
+ }
+ }
+ }
+ return ret;
+ }
+
+ /**
+ * The local-name function returns the local part of the expanded-name of
+ * the node in the argument node-set that is first in document order. If
+ * the argument node-set is empty or the first node has no expanded-name,
+ * an empty string is returned. If the argument is omitted, it defaults to
+ * a node-set with the context node as its only member.
+ */
+ public static String _local_name(Node context, Collection<Node> nodeSet)
+ {
+ if (nodeSet == null || nodeSet.isEmpty())
+ return "";
+ Node node = firstNode(nodeSet);
+ String ret = node.getLocalName();
+ return (ret == null) ? "" : ret;
+ }
+
+ /**
+ * The namespace-uri function returns the namespace URI of the
+ * expanded-name of the node in the argument node-set that is first in
+ * document order. If the argument node-set is empty, the first node has
+ * no expanded-name, or the namespace URI of the expanded-name is null, an
+ * empty string is returned. If the argument is omitted, it defaults to a
+ * node-set with the context node as its only member.
+ */
+ public static String _namespace_uri(Node context, Collection<Node> nodeSet)
+ {
+ if (nodeSet == null || nodeSet.isEmpty())
+ return "";
+ Node node = firstNode(nodeSet);
+ String ret = node.getNamespaceURI();
+ return (ret == null) ? "" : ret;
+ }
+
+ /**
+ * The name function returns a string containing a QName representing the
+ * expanded-name of the node in the argument node-set that is first in
+ * document order. The QName must represent the expanded-name with respect
+ * to the namespace declarations in effect on the node whose expanded-name
+ * is being represented. Typically, this will be the QName that occurred
+ * in the XML source. This need not be the case if there are namespace
+ * declarations in effect on the node that associate multiple prefixes
+ * with the same namespace. However, an implementation may include
+ * information about the original prefix in its representation of nodes;
+ * in this case, an implementation can ensure that the returned string is
+ * always the same as the QName used in the XML source. If the argument
+ * node-set is empty or the first node has no expanded-name, an empty
+ * string is returned. If the argument it omitted, it defaults to a
+ * node-set with the context node as its only member.
+ */
+ public static String _name(Node context, Collection<Node> nodeSet)
+ {
+ if (nodeSet == null || nodeSet.isEmpty())
+ return "";
+ Node node = firstNode(nodeSet);
+ String ret = null;
+ switch (node.getNodeType())
+ {
+ case Node.ATTRIBUTE_NODE:
+ case Node.ELEMENT_NODE:
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ ret = node.getNodeName();
+ }
+ return (ret == null) ? "" : ret;
+ }
+
+ /**
+ * Returns the first node in the set in document order.
+ */
+ static Node firstNode(Collection<Node> nodeSet)
+ {
+ List<Node> list = new ArrayList<Node>(nodeSet);
+ Collections.sort(list, documentOrderComparator);
+ return list.get(0);
+ }
+
+ /* -- 4.2 String Functions -- */
+
+ /**
+ * Implementation of the XPath <code>string</code> function.
+ */
+ public static String _string(Node context, Object object)
+ {
+ if (object == null)
+ {
+ return stringValue(context);
+ }
+ if (object instanceof String)
+ {
+ return (String) object;
+ }
+ if (object instanceof Boolean)
+ {
+ return object.toString();
+ }
+ if (object instanceof Double)
+ {
+ double d = ((Double) object).doubleValue();
+ if (Double.isNaN(d))
+ {
+ return "NaN";
+ }
+ else if (d == 0.0d)
+ {
+ return "0";
+ }
+ else if (Double.isInfinite(d))
+ {
+ if (d < 0)
+ {
+ return "-Infinity";
+ }
+ else
+ {
+ return "Infinity";
+ }
+ }
+ else
+ {
+ String ret = decimalFormat.format(d);
+ if (ret.endsWith (".0"))
+ {
+ ret = ret.substring(0, ret.length() - 2);
+ }
+ return ret;
+ }
+ }
+ if (object instanceof Collection)
+ {
+ /* Suppression is safe, as we fail immediately if the
+ * first element is not a Node and don't use the rest */
+ @SuppressWarnings("unchecked")
+ Collection<Node> nodeSet = (Collection<Node>) object;
+ if (nodeSet.isEmpty())
+ {
+ return "";
+ }
+ Node node = firstNode(nodeSet);
+ return stringValue(node);
+ }
+ throw new IllegalArgumentException(object.toString());
+ }
+
+ /* -- 4.3 Boolean Functions -- */
+
+ /**
+ * Implementation of the XPath <code>boolean</code> function.
+ */
+ public static boolean _boolean(Node context, Object object)
+ {
+ if (object instanceof Boolean)
+ {
+ return ((Boolean) object).booleanValue();
+ }
+ if (object instanceof Double)
+ {
+ Double value = (Double) object;
+ if (value.isNaN())
+ return false;
+ return value.doubleValue() != 0.0;
+ }
+ if (object instanceof String)
+ {
+ return ((String) object).length() != 0;
+ }
+ if (object instanceof Collection)
+ {
+ return ((Collection<?>) object).size() != 0;
+ }
+ return false; // TODO user defined types
+ }
+
+ /* -- 4.4 Number Functions -- */
+
+ /**
+ * Implementation of the XPath <code>number</code> function.
+ */
+ public static double _number(Node context, Object object)
+ {
+ if (object == null)
+ {
+ object = Collections.singleton(context);
+ }
+ if (object instanceof Double)
+ {
+ return ((Double) object).doubleValue();
+ }
+ if (object instanceof Boolean)
+ {
+ return ((Boolean) object).booleanValue() ? 1.0 : 0.0;
+ }
+ if (object instanceof Collection)
+ {
+ /* Suppression is safe, as we fail immediately if one
+ * of the elements is not a Node */
+ @SuppressWarnings("unchecked")
+ Collection<Node> nodeSet = (Collection<Node>) object;
+ // Convert node-set to string
+ object = stringValue(nodeSet);
+ }
+ if (object instanceof String)
+ {
+ String string = ((String) object).trim();
+ try
+ {
+ return Double.parseDouble(string);
+ }
+ catch (NumberFormatException e)
+ {
+ return Double.NaN;
+ }
+ }
+ return Double.NaN; // TODO user-defined types
+ }
+
+ /**
+ * Computes the XPath string-value of the specified node-set.
+ */
+ public static String stringValue(Collection<Node> nodeSet)
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ for (Iterator<Node> i = nodeSet.iterator(); i.hasNext(); )
+ {
+ buf.append(stringValue(i.next()));
+ }
+ return buf.toString();
+ }
+
+ /**
+ * Computes the XPath string-value of the specified node.
+ */
+ public static String stringValue(Node node)
+ {
+ return stringValue(node, false);
+ }
+
+ static String stringValue(Node node, boolean elementMode)
+ {
+ switch (node.getNodeType())
+ {
+ case Node.DOCUMENT_NODE: // 5.1 Root Node
+ case Node.DOCUMENT_FRAGMENT_NODE:
+ case Node.ELEMENT_NODE: // 5.2 Element Nodes
+ CPStringBuilder buf = new CPStringBuilder();
+ for (Node ctx = node.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ buf.append(stringValue(ctx, true));
+ }
+ return buf.toString();
+ case Node.TEXT_NODE: // 5.7 Text Nodes
+ case Node.CDATA_SECTION_NODE:
+ return node.getNodeValue();
+ case Node.ATTRIBUTE_NODE: // 5.3 Attribute Nodes
+ case Node.PROCESSING_INSTRUCTION_NODE: // 5.5 Processing Instruction
+ case Node.COMMENT_NODE: // 5.6 Comment Nodes
+ if (!elementMode)
+ {
+ return node.getNodeValue();
+ }
+ default:
+ return "";
+ }
+ }
+
+ static int intValue(Object val)
+ {
+ if (val instanceof Double)
+ {
+ Double d = (Double) val;
+ return d.isNaN() ? 0 : d.intValue();
+ }
+ else
+ return (int) Math.ceil(_number(null, val));
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/FalseFunction.java b/libjava/classpath/gnu/xml/xpath/FalseFunction.java
new file mode 100644
index 000000000..9a20f9163
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/FalseFunction.java
@@ -0,0 +1,72 @@
+/* FalseFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>false</code> function returns false.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class FalseFunction
+ extends Expr
+{
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ return Boolean.FALSE;
+ }
+
+ public Expr clone(Object context)
+ {
+ return new FalseFunction();
+ }
+
+ public boolean references(QName var)
+ {
+ return false;
+ }
+
+ public String toString()
+ {
+ return "false()";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/FloorFunction.java b/libjava/classpath/gnu/xml/xpath/FloorFunction.java
new file mode 100644
index 000000000..ed61742a3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/FloorFunction.java
@@ -0,0 +1,90 @@
+/* FloorFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>floor</code> function returns the largest (closest to positive
+ * infinity) number that is not greater than the argument and that is an
+ * integer.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class FloorFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ FloorFunction(List<Expr> args)
+ {
+ this(args.get(0));
+ }
+
+ FloorFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val = arg.evaluate(context, pos, len);
+ double n = _number(context, val);
+ return new Double(Math.floor(n));
+ }
+
+ public Expr clone(Object context)
+ {
+ return new FloorFunction(arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return arg.references(var);
+ }
+
+ public String toString()
+ {
+ return "floor(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/Function.java b/libjava/classpath/gnu/xml/xpath/Function.java
new file mode 100644
index 000000000..f9eab2b73
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/Function.java
@@ -0,0 +1,56 @@
+/* Function.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+
+/**
+ * Interface to be implemented by external functions that need to receive
+ * parameter values.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public interface Function
+{
+
+ /**
+ * Sets the list of expressions to evaluate as parameter values.
+ */
+ void setArguments(List<Expr> args);
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/FunctionCall.java b/libjava/classpath/gnu/xml/xpath/FunctionCall.java
new file mode 100644
index 000000000..977482a8f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/FunctionCall.java
@@ -0,0 +1,169 @@
+/* FunctionCall.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionException;
+import javax.xml.xpath.XPathFunctionResolver;
+import org.w3c.dom.Node;
+
+/**
+ * Executes an XPath core or extension function.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class FunctionCall
+ extends Expr
+{
+
+ final XPathFunctionResolver resolver;
+ final String name;
+ final List<Expr> args;
+
+ public FunctionCall(XPathFunctionResolver resolver, String name)
+ {
+ this(resolver, name, null);
+ }
+
+ public FunctionCall(XPathFunctionResolver resolver, String name, List<Expr> args)
+ {
+ this.resolver = resolver;
+ this.name = name;
+ if (args == null)
+ this.args = Collections.emptyList();
+ else
+ this.args = args;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ if (resolver != null)
+ {
+ QName qname = QName.valueOf(name);
+ int arity = args.size();
+ XPathFunction function = resolver.resolveFunction(qname, arity);
+ if (function != null)
+ {
+ //System.err.println("Calling "+toString()+" with "+values);
+ if (function instanceof Expr)
+ {
+ if (function instanceof Function)
+ {
+ ((Function) function).setArguments(args);
+ }
+ return ((Expr) function).evaluate(context, pos, len);
+ }
+ else
+ {
+ List<Object> values = new ArrayList<Object>(arity);
+ for (int i = 0; i < arity; i++)
+ {
+ Expr arg = (Expr) args.get(i);
+ values.add(arg.evaluate(context, pos, len));
+ }
+ try
+ {
+ return function.evaluate(values);
+ }
+ catch (XPathFunctionException e)
+ {
+ e.printStackTrace(System.err); // FIXME
+ throw new RuntimeException(e.getMessage(), e);
+ }
+ }
+ }
+ }
+ throw new IllegalArgumentException("Invalid function call: " +
+ toString());
+ }
+
+ public Expr clone(Object context)
+ {
+ int len = args.size();
+ List<Expr> args2 = new ArrayList<Expr>(len);
+ for (int i = 0; i < len; i++)
+ {
+ args2.add(args.get(i).clone(context));
+ }
+ XPathFunctionResolver r = resolver;
+ if (context instanceof XPathFunctionResolver)
+ {
+ r = (XPathFunctionResolver) context;
+ }
+ return new FunctionCall(r, name, args2);
+ }
+
+ public boolean references(QName var)
+ {
+ for (Iterator<Expr> i = args.iterator(); i.hasNext(); )
+ {
+ if (i.next().references(var))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ buf.append(name);
+ buf.append('(');
+ int len = args.size();
+ for (int i = 0; i < len; i++)
+ {
+ if (i > 0)
+ {
+ buf.append(',');
+ }
+ buf.append(args.get(i));
+ }
+ buf.append(')');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/IdFunction.java b/libjava/classpath/gnu/xml/xpath/IdFunction.java
new file mode 100644
index 000000000..7b6c6ac25
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/IdFunction.java
@@ -0,0 +1,103 @@
+/* IdFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.Collection;
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>id</code> function selects elements by their unique ID.
+ * When the argument to id is of type node-set, then the result is
+ * the union of the result of applying id to the string-value of each of the
+ * nodes in the argument node-set. When the argument to id is of any other
+ * type, the argument is converted to a string as if by a call to the string
+ * function; the string is split into a whitespace-separated list of tokens
+ * (whitespace is any sequence of characters matching the production S); the
+ * result is a node-set containing the elements in the same document as the
+ * context node that have a unique ID equal to any of the tokens in the
+ * list.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class IdFunction
+ extends Pattern
+{
+
+ final Expr arg;
+
+ IdFunction(List<Expr> args)
+ {
+ this(args.get(0));
+ }
+
+ public IdFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ public boolean matches(Node context)
+ {
+ Object ret = evaluate(context, 1, 1);
+ return !((Collection<?>) ret).isEmpty();
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val = arg.evaluate(context, pos, len);
+ return _id(context, val);
+ }
+
+ public Expr clone(Object context)
+ {
+ return new IdFunction(arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return arg.references(var);
+ }
+
+ public String toString()
+ {
+ return "id(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/LangFunction.java b/libjava/classpath/gnu/xml/xpath/LangFunction.java
new file mode 100644
index 000000000..5a8d32fc0
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/LangFunction.java
@@ -0,0 +1,123 @@
+/* LangFunction.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>lang</code> function returns true or false depending on whether
+ * the language of the context node as specified by xml:lang attributes is
+ * the same as or is a sublanguage of the language specified by the argument
+ * string. The language of the context node is determined by the value of
+ * the xml:lang attribute on the context node, or, if the context node has
+ * no xml:lang attribute, by the value of the xml:lang attribute on the
+ * nearest ancestor of the context node that has an xml:lang attribute. If
+ * there is no such attribute, then lang returns false. If there is such an
+ * attribute, then lang returns true if the attribute value is equal to the
+ * argument ignoring case, or if there is some suffix starting with - such
+ * that the attribute value is equal to the argument ignoring that suffix of
+ * the attribute value and ignoring case.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class LangFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ LangFunction(List<Expr> args)
+ {
+ this(args.get(0));
+ }
+
+ LangFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val = arg.evaluate(context, pos, len);
+ String lang = _string(context, val);
+ String clang = getLang(context);
+ while (clang == null && context != null)
+ {
+ context = context.getParentNode();
+ clang = getLang(context);
+ }
+ boolean ret = (clang == null) ? false :
+ clang.toLowerCase().startsWith(lang.toLowerCase());
+ return ret ? Boolean.TRUE : Boolean.FALSE;
+ }
+
+ String getLang(Node node)
+ {
+ while (node != null)
+ {
+ if (node.getNodeType() == Node.ELEMENT_NODE)
+ {
+ String lang = ((Element) node).getAttribute("xml:lang");
+ if (lang != null)
+ return lang;
+ }
+ node = node.getParentNode();
+ }
+ return null;
+ }
+
+ public Expr clone(Object context)
+ {
+ return new IdFunction(arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return arg.references(var);
+ }
+
+ public String toString()
+ {
+ return "lang(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/LastFunction.java b/libjava/classpath/gnu/xml/xpath/LastFunction.java
new file mode 100644
index 000000000..51f988f3c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/LastFunction.java
@@ -0,0 +1,73 @@
+/* LastFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>last</code> function returns a number equal to the context
+ * size from the expression evaluation context.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class LastFunction
+ extends Expr
+{
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ return new Double((double) len);
+ }
+
+ public Expr clone(Object context)
+ {
+ return new LastFunction();
+ }
+
+ public boolean references(QName var)
+ {
+ return false;
+ }
+
+ public String toString()
+ {
+ return "last()";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/LocalNameFunction.java b/libjava/classpath/gnu/xml/xpath/LocalNameFunction.java
new file mode 100644
index 000000000..d829075c7
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/LocalNameFunction.java
@@ -0,0 +1,98 @@
+/* LocalNameFunction.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>local-name</code> function returns the local part of the
+ * expanded-name of the node in the argument node-set that is first in
+ * document order.
+ * If the argument node-set is empty or the first node has no expanded-name,
+ * an empty string is returned. If the argument is omitted, it defaults to a
+ * node-set with the context node as its only member.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class LocalNameFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ LocalNameFunction(List<Expr> args)
+ {
+ this(args.size() > 0 ? args.get(0) : null);
+ }
+
+ LocalNameFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ /* Suppression is safe, as we know context produces Collection<Node> */
+ @SuppressWarnings("unchecked")
+ Collection<Node> val = (arg == null) ? Collections.singleton(context) :
+ (Collection<Node>) arg.evaluate(context, pos, len);
+ return _local_name(context, val);
+ }
+
+ public Expr clone(Object context)
+ {
+ return new LocalNameFunction((arg == null) ? null :
+ arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return (arg == null) ? false : arg.references(var);
+ }
+
+ public String toString()
+ {
+ return (arg == null) ? "local-name()" : "local-name(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/NameFunction.java b/libjava/classpath/gnu/xml/xpath/NameFunction.java
new file mode 100644
index 000000000..7db0be531
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/NameFunction.java
@@ -0,0 +1,109 @@
+/* NameFunction.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>name</code> function returns a string containing a QName
+ * representing the expanded-name of the node in the argument node-set that
+ * is first in document order. The QName must represent the expanded-name
+ * with respect to the namespace declarations in effect on the node whose
+ * expanded-name is being represented. Typically, this will be the QName
+ * that occurred in the XML source. This need not be the case if there are
+ * namespace declarations in effect on the node that associate multiple
+ * prefixes with the same namespace. However, an implementation may include
+ * information about the original prefix in its representation of nodes; in
+ * this case, an implementation can ensure that the returned string is
+ * always the same as the QName used in the XML source. If the argument
+ * node-set is empty or the first node has no expanded-name, an empty string
+ * is returned. If the argument it omitted, it defaults to a node-set with
+ * the context node as its only member.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NameFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ NameFunction(List<Expr> args)
+ {
+ this(args.size() > 0 ? args.get(0) : null);
+ }
+
+ NameFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ /* Suppression is safe, as we know context produces Collection<Node> */
+ @SuppressWarnings("unchecked")
+ Collection<Node> val = (arg == null) ? Collections.singleton(context) :
+ (Collection<Node>) arg.evaluate(context, pos, len);
+ return _name(context, val);
+ }
+
+ @Override
+ public Expr clone(Object context)
+ {
+ return new NameFunction((arg == null) ? null :
+ arg.clone(context));
+ }
+
+ @Override
+ public boolean references(QName var)
+ {
+ return (arg == null) ? false : arg.references(var);
+ }
+
+ @Override
+ public String toString()
+ {
+ return (arg == null) ? "name()" : "name(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/NameTest.java b/libjava/classpath/gnu/xml/xpath/NameTest.java
new file mode 100644
index 000000000..8bd06bd3b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/NameTest.java
@@ -0,0 +1,143 @@
+/* NameTest.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * Tests whether a node has the specified name.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class NameTest
+ extends Test
+{
+
+ final QName qName;
+ final boolean anyLocalName;
+ final boolean any;
+
+ public NameTest(QName qName, boolean anyLocalName, boolean any)
+ {
+ this.anyLocalName = anyLocalName;
+ this.any = any;
+ this.qName = qName;
+ }
+
+ public boolean matchesAny()
+ {
+ return any;
+ }
+
+ public boolean matchesAnyLocalName()
+ {
+ return anyLocalName;
+ }
+
+ public boolean matches(Node node, int pos, int len)
+ {
+ switch (node.getNodeType())
+ {
+ case Node.ATTRIBUTE_NODE:
+ // Do not match namespace attributes
+ String uri = node.getNamespaceURI();
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri) ||
+ XMLConstants.XMLNS_ATTRIBUTE.equals(node.getPrefix()) ||
+ XMLConstants.XMLNS_ATTRIBUTE.equals(node.getNodeName()))
+ {
+ return false;
+ }
+ // Fall through
+ case Node.ELEMENT_NODE:
+ break;
+ default:
+ return false;
+ }
+ if (any)
+ return true;
+ String uri = qName.getNamespaceURI();
+ String nodeUri = node.getNamespaceURI();
+ if (!equal(uri, nodeUri))
+ return false;
+ if (anyLocalName)
+ return true;
+ String localName = qName.getLocalPart();
+ String nodeLocalName = getLocalName(node);
+ return (localName.equals(nodeLocalName));
+ }
+
+ static String getLocalName(Node node)
+ {
+ String localName = node.getLocalName();
+ if (localName == null)
+ {
+ localName = node.getNodeName();
+ int ci = localName.indexOf(':');
+ if (ci != -1)
+ localName = localName.substring(ci + 1);
+ }
+ return localName;
+ }
+
+ static boolean equal(String s1, String s2)
+ {
+ return (((s1 == null || s1.length() == 0) &&
+ (s2 == null || s2.length() == 0)) ||
+ s1 != null && s1.equals(s2));
+ }
+
+ public Test clone(Object context)
+ {
+ return new NameTest(qName, anyLocalName, any);
+ }
+
+ public boolean references(QName var)
+ {
+ return false;
+ }
+
+ public String toString ()
+ {
+ if (any)
+ return "*";
+ return qName.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/NamespaceTest.java b/libjava/classpath/gnu/xml/xpath/NamespaceTest.java
new file mode 100644
index 000000000..2f4fec179
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/NamespaceTest.java
@@ -0,0 +1,120 @@
+/* NamespaceTest.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * Tests whether a namespace attribute has the specified name.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class NamespaceTest
+ extends Test
+{
+
+ final QName qName;
+ final boolean anyLocalName;
+ final boolean any;
+
+ public NamespaceTest(QName qName, boolean anyLocalName, boolean any)
+ {
+ this.anyLocalName = anyLocalName;
+ this.any = any;
+ this.qName = qName;
+ }
+
+ public boolean matchesAny()
+ {
+ return any;
+ }
+
+ public boolean matchesAnyLocalName()
+ {
+ return anyLocalName;
+ }
+
+ public boolean matches(Node node, int pos, int len)
+ {
+ switch (node.getNodeType())
+ {
+ case Node.ATTRIBUTE_NODE:
+ // Only match namespace attributes
+ String uri = node.getNamespaceURI();
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri) ||
+ XMLConstants.XMLNS_ATTRIBUTE.equals(node.getPrefix()) ||
+ XMLConstants.XMLNS_ATTRIBUTE.equals(node.getNodeName()))
+ break;
+ // Fall through
+ default:
+ // Only process namespace attributes
+ return false;
+ }
+ if (any)
+ return true;
+ String uri = qName.getNamespaceURI();
+ String nodeUri = node.getNamespaceURI();
+ if (!NameTest.equal(uri, nodeUri))
+ return false;
+ if (anyLocalName)
+ return true;
+ String localName = qName.getLocalPart();
+ String nodeLocalName = NameTest.getLocalName(node);
+ return (localName.equals(nodeLocalName));
+ }
+
+ public Test clone(Object context)
+ {
+ return new NamespaceTest(qName, anyLocalName, any);
+ }
+
+ public boolean references(QName var)
+ {
+ return false;
+ }
+
+ public String toString ()
+ {
+ if (any)
+ return "*";
+ return qName.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/NamespaceUriFunction.java b/libjava/classpath/gnu/xml/xpath/NamespaceUriFunction.java
new file mode 100644
index 000000000..4972b0607
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/NamespaceUriFunction.java
@@ -0,0 +1,98 @@
+/* NamespaceUriFunction.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>namespace-uri</code> function returns the namespace URI of the
+ * expanded-name of the node in the argument node-set that is first in
+ * document order. If the argument node-set is empty, the first node has no
+ * expanded-name, or the namespace URI of the expanded-name is null, an
+ * empty string is returned. If the argument is omitted, it defaults to a
+ * node-set with the context node as its only member.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NamespaceUriFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ NamespaceUriFunction(List<Expr> args)
+ {
+ this(args.size() > 0 ? args.get(0) : null);
+ }
+
+ NamespaceUriFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ /* Suppression is safe, as we know context produces Collection<Node> */
+ @SuppressWarnings("unchecked")
+ Collection<Node> val = (arg == null) ? Collections.singleton(context) :
+ (Collection<Node>) arg.evaluate(context, pos, len);
+ return _namespace_uri(context, val);
+ }
+
+ public Expr clone(Object context)
+ {
+ return new NamespaceUriFunction((arg == null) ? null :
+ arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return (arg == null) ? false : arg.references(var);
+ }
+
+ public String toString()
+ {
+ return (arg == null) ? "namespace-uri()" : "namespace-uri(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/NegativeExpr.java b/libjava/classpath/gnu/xml/xpath/NegativeExpr.java
new file mode 100644
index 000000000..215ef3fa9
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/NegativeExpr.java
@@ -0,0 +1,81 @@
+/* NegativeExpr.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * Unary negative.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NegativeExpr
+ extends Expr
+{
+
+ final Expr expr;
+
+ NegativeExpr(Expr expr)
+ {
+ this.expr = expr;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val = expr.evaluate(context, pos, len);
+ double n = _number(context, val);
+ return new Double(-n);
+ }
+
+ public Expr clone(Object context)
+ {
+ return new NegativeExpr(expr.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return expr.references(var);
+ }
+
+ public String toString()
+ {
+ return "-" + expr;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/NodeTypeTest.java b/libjava/classpath/gnu/xml/xpath/NodeTypeTest.java
new file mode 100644
index 000000000..43e26994b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/NodeTypeTest.java
@@ -0,0 +1,141 @@
+/* NodeTypeTest.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+import org.w3c.dom.ProcessingInstruction;
+
+/**
+ * Tests whether a node is of a given type.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class NodeTypeTest
+ extends Test
+{
+
+ final short type;
+ final String data;
+
+ public NodeTypeTest(short type)
+ {
+ this(type, null);
+ }
+
+ public NodeTypeTest(short type, String data)
+ {
+ this.type = type;
+ this.data = data;
+ }
+
+ public short getNodeType()
+ {
+ return type;
+ }
+
+ public String getData()
+ {
+ return data;
+ }
+
+ public boolean matches(Node node, int pos, int len)
+ {
+ short nodeType = node.getNodeType();
+ switch (nodeType)
+ {
+ case Node.ELEMENT_NODE:
+ case Node.ATTRIBUTE_NODE:
+ case Node.TEXT_NODE:
+ case Node.CDATA_SECTION_NODE:
+ case Node.COMMENT_NODE:
+ case Node.DOCUMENT_NODE:
+ if (type > 0)
+ {
+ if (nodeType != type)
+ return false;
+ }
+ return true;
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ if (type > 0)
+ {
+ if (nodeType != type)
+ return false;
+ if (data != null &&
+ !data.equals(((ProcessingInstruction) node).getTarget()))
+ return false;
+ }
+ return true;
+ default:
+ // Not part of XPath data model
+ return false;
+ }
+ }
+
+ public Test clone(Object context)
+ {
+ return new NodeTypeTest(type, data);
+ }
+
+ public boolean references(QName var)
+ {
+ return false;
+ }
+
+ public String toString()
+ {
+ switch (type)
+ {
+ case 0:
+ return "node()";
+ case Node.TEXT_NODE:
+ return "text()";
+ case Node.COMMENT_NODE:
+ return "comment()";
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ if (data != null)
+ {
+ return "processing-instruction('" + data + "')";
+ }
+ return "processing-instruction()";
+ default:
+ throw new IllegalStateException();
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/NormalizeSpaceFunction.java b/libjava/classpath/gnu/xml/xpath/NormalizeSpaceFunction.java
new file mode 100644
index 000000000..6d728d9e7
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/NormalizeSpaceFunction.java
@@ -0,0 +1,108 @@
+/* NormalizeSpaceFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.List;
+import java.util.StringTokenizer;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>normalize-space</code> function returns the argument string
+ * with whitespace normalized by stripping leading and trailing whitespace
+ * and replacing sequences of whitespace characters by a single space.
+ * Whitespace characters are the same as those allowed by the S production
+ * in XML. If the argument is omitted, it defaults to the context node
+ * converted to a string, in other words the string-value of the context
+ * node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NormalizeSpaceFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ NormalizeSpaceFunction(List<Expr> args)
+ {
+ this(args.get(0));
+ }
+
+ NormalizeSpaceFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val = (arg == null) ? null : arg.evaluate(context, pos, len);
+ String s = _string(context, val);
+ StringTokenizer st = new StringTokenizer(s, " \t\r\n");
+ CPStringBuilder buf = new CPStringBuilder();
+ if (st.hasMoreTokens())
+ {
+ buf.append(st.nextToken());
+ while (st.hasMoreTokens())
+ {
+ buf.append(' ');
+ buf.append(st.nextToken());
+ }
+ }
+ return buf.toString();
+ }
+
+ public Expr clone(Object context)
+ {
+ return new NormalizeSpaceFunction(arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return (arg == null) ? false : arg.references(var);
+ }
+
+ public String toString()
+ {
+ return (arg == null) ? "normalize-space()" : "normalize-space(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/NotFunction.java b/libjava/classpath/gnu/xml/xpath/NotFunction.java
new file mode 100644
index 000000000..a7c20a0c1
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/NotFunction.java
@@ -0,0 +1,88 @@
+/* NotFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>not</code> function returns true if its argument is false,
+ * and false otherwise.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NotFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ NotFunction(List<Expr> args)
+ {
+ this(args.get(0));
+ }
+
+ NotFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val = arg.evaluate(context, pos, len);
+ return _boolean(context, val) ? Boolean.FALSE : Boolean.TRUE;
+ }
+
+ public Expr clone(Object context)
+ {
+ return new NotFunction(arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return arg.references(var);
+ }
+
+ public String toString()
+ {
+ return "not(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/NumberFunction.java b/libjava/classpath/gnu/xml/xpath/NumberFunction.java
new file mode 100644
index 000000000..08831020b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/NumberFunction.java
@@ -0,0 +1,103 @@
+/* NumberFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>number</code> function converts its argument to a number as
+ * follows:
+ * <ul>
+ * <li>a string that consists of optional whitespace followed by an optional
+ * minus sign followed by a Number followed by whitespace is converted to
+ * the IEEE 754 number that is nearest (according to the IEEE 754
+ * round-to-nearest rule) to the mathematical value represented by the
+ * string; any other string is converted to NaN</li>
+ * <li>boolean true is converted to 1; boolean false is converted to 0</li>
+ * <li>a node-set is first converted to a string as if by a call to the
+ * string function and then converted in the same way as a string
+ * argument</li>
+ * <li>an object of a type other than the four basic types is converted to a
+ * number in a way that is dependent on that type</li>
+ * </ul>
+ * If the argument is omitted, it defaults to a node-set with the context
+ * node as its only member.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class NumberFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ NumberFunction(List<Expr> args)
+ {
+ this(args.size() > 0 ? args.get(0) : null);
+ }
+
+ NumberFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val = (arg == null) ? null : arg.evaluate(context, pos, len);
+ return new Double(_number(context, val));
+ }
+
+ public Expr clone(Object context)
+ {
+ return new NumberFunction(arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return arg.references(var);
+ }
+
+ public String toString()
+ {
+ return "number(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/OrExpr.java b/libjava/classpath/gnu/xml/xpath/OrExpr.java
new file mode 100644
index 000000000..937dedf6a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/OrExpr.java
@@ -0,0 +1,87 @@
+/* OrExpr.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * Logical or.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class OrExpr
+ extends Expr
+{
+
+ final Expr lhs;
+ final Expr rhs;
+
+ public OrExpr(Expr lhs, Expr rhs)
+ {
+ this.lhs = lhs;
+ this.rhs = rhs;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object left = lhs.evaluate(context, pos, len);
+ if (_boolean(context, left))
+ {
+ return Boolean.TRUE;
+ }
+ Object right = rhs.evaluate(context, pos, len);
+ return _boolean(context, right) ? Boolean.TRUE : Boolean.FALSE;
+ }
+
+ public Expr clone(Object context)
+ {
+ return new OrExpr(lhs.clone(context), rhs.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return (lhs.references(var) || rhs.references(var));
+ }
+
+ public String toString()
+ {
+ return lhs + " or " + rhs;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/ParenthesizedExpr.java b/libjava/classpath/gnu/xml/xpath/ParenthesizedExpr.java
new file mode 100644
index 000000000..e8ceb5aa5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/ParenthesizedExpr.java
@@ -0,0 +1,95 @@
+/* ParenthesizedExpr.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * Simple subexpression.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class ParenthesizedExpr
+ extends Expr
+{
+
+ final Expr expr;
+
+ ParenthesizedExpr(Expr expr)
+ {
+ this.expr = expr;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object ret = expr.evaluate(context, pos, len);
+ if (ret instanceof Collection)
+ {
+ /* Suppression is safe, as we know context produces
+ Collection<Node> */
+ @SuppressWarnings("unchecked")
+ Collection<Node> nodes = (Collection<Node>) ret;
+ List<Node> list = new ArrayList<Node>(nodes);
+ Collections.sort(list, documentOrderComparator);
+ ret = list;
+ }
+ return ret;
+ }
+
+ public Expr clone(Object context)
+ {
+ return new ParenthesizedExpr(expr.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return expr.references(var);
+ }
+
+ public String toString()
+ {
+ return "(" + expr + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/Path.java b/libjava/classpath/gnu/xml/xpath/Path.java
new file mode 100644
index 000000000..f19759567
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/Path.java
@@ -0,0 +1,54 @@
+/* Path.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.Collection;
+import org.w3c.dom.Node;
+
+/**
+ * An XPath path component expression.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+abstract class Path
+ extends Pattern
+{
+
+ abstract Collection<Node> evaluate(Node context, Collection<Node> nodeSet);
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/Pattern.java b/libjava/classpath/gnu/xml/xpath/Pattern.java
new file mode 100644
index 000000000..ca03cda37
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/Pattern.java
@@ -0,0 +1,53 @@
+/* Pattern.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import org.w3c.dom.Node;
+
+/**
+ * Interface implemented by expressions that can form part of XSL patterns.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public abstract class Pattern
+ extends Expr
+{
+
+ public abstract boolean matches(Node context);
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/PositionFunction.java b/libjava/classpath/gnu/xml/xpath/PositionFunction.java
new file mode 100644
index 000000000..28ead3af0
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/PositionFunction.java
@@ -0,0 +1,73 @@
+/* PositionFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>position</code> function returns a number equal to the context
+ * position from the expression evaluation context.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class PositionFunction
+ extends Expr
+{
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ return new Double((double) pos);
+ }
+
+ public Expr clone(Object context)
+ {
+ return new PositionFunction();
+ }
+
+ public boolean references(QName var)
+ {
+ return false;
+ }
+
+ public String toString()
+ {
+ return "position()";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/Predicate.java b/libjava/classpath/gnu/xml/xpath/Predicate.java
new file mode 100644
index 000000000..a02e22b94
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/Predicate.java
@@ -0,0 +1,85 @@
+/* Predicate.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * Tests whether an expression matches against a given context node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+class Predicate
+ extends Test
+{
+
+ final Expr expr;
+
+ Predicate(Expr expr)
+ {
+ this.expr = expr;
+ }
+
+ public boolean matches(Node node, int pos, int len)
+ {
+ Object ret = expr.evaluate(node, pos, len);
+ if (ret instanceof Double)
+ {
+ // Same as [position() = x]
+ return ((Double) ret).intValue() == pos;
+ }
+ return Expr._boolean(node, expr.evaluate(node, pos, len));
+ }
+
+ public Test clone(Object context)
+ {
+ return new Predicate(expr.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return expr.references(var);
+ }
+
+ public String toString()
+ {
+ return "[" + expr + "]";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/RelationalExpr.java b/libjava/classpath/gnu/xml/xpath/RelationalExpr.java
new file mode 100644
index 000000000..ac79d5049
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/RelationalExpr.java
@@ -0,0 +1,107 @@
+/* RelationalExpr.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * Numerical comparison expression.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class RelationalExpr
+ extends Expr
+{
+
+ final Expr lhs;
+ final Expr rhs;
+ final boolean lt;
+ final boolean eq;
+
+ RelationalExpr(Expr lhs, Expr rhs, boolean lt, boolean eq)
+ {
+ this.lhs = lhs;
+ this.rhs = rhs;
+ this.lt = lt;
+ this.eq = eq;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object left = lhs.evaluate(context, pos, len);
+ Object right = rhs.evaluate(context, pos, len);
+ double ln = _number(context, left);
+ double rn = _number(context, right);
+ if (eq && ln == rn)
+ {
+ return Boolean.TRUE;
+ }
+ if (lt)
+ {
+ if (ln < rn || Double.isInfinite(rn))
+ {
+ return Boolean.TRUE;
+ }
+ }
+ else
+ {
+ if (ln > rn || Double.isInfinite(ln))
+ {
+ return Boolean.TRUE;
+ }
+ }
+ return Boolean.FALSE;
+ }
+
+ public Expr clone(Object context)
+ {
+ return new RelationalExpr(lhs.clone(context), rhs.clone(context), lt, eq);
+ }
+
+ public boolean references(QName var)
+ {
+ return (lhs.references(var) || rhs.references(var));
+ }
+
+ public String toString()
+ {
+ return lhs + " " + (lt ? "<" : ">") + (eq ? "=" : "") + " " + rhs;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/Root.java b/libjava/classpath/gnu/xml/xpath/Root.java
new file mode 100644
index 000000000..8eef3408f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/Root.java
@@ -0,0 +1,91 @@
+/* Root.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Set;
+
+import javax.xml.namespace.QName;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+
+/**
+ * Expression that evaluates to the document root.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class Root
+ extends Path
+{
+
+ public boolean matches(Node node)
+ {
+ return (node.getNodeType() == Node.DOCUMENT_NODE);
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Set<Node> emptySet = Collections.emptySet();
+ return evaluate(context, emptySet);
+ }
+
+ @Override
+ Collection<Node> evaluate(Node context, Collection<Node> ns)
+ {
+ Node doc = (context instanceof Document) ? context :
+ context.getOwnerDocument();
+ return Collections.singleton(doc);
+ }
+
+ public Expr clone(Object context)
+ {
+ return new Root();
+ }
+
+ public boolean references(QName var)
+ {
+ return false;
+ }
+
+ public String toString()
+ {
+ return "/";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/RoundFunction.java b/libjava/classpath/gnu/xml/xpath/RoundFunction.java
new file mode 100644
index 000000000..3b79e3e21
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/RoundFunction.java
@@ -0,0 +1,97 @@
+/* RoundFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>round</code> function returns the number that is closest to the
+ * argument and that is an integer. If there are two such numbers, then the
+ * one that is closest to positive infinity is returned. If the argument is
+ * NaN, then NaN is returned. If the argument is positive infinity, then
+ * positive infinity is returned. If the argument is negative infinity, then
+ * negative infinity is returned. If the argument is positive zero, then
+ * positive zero is returned. If the argument is negative zero, then
+ * negative zero is returned. If the argument is less than zero, but greater
+ * than or equal to -0.5, then negative zero is returned.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class RoundFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ RoundFunction(List<Expr> args)
+ {
+ this(args.get(0));
+ }
+
+ RoundFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val = arg.evaluate(context, pos, len);
+ double n = _number(context, val);
+ return (Double.isNaN(n) || Double.isInfinite(n)) ?
+ new Double(n) : new Double(Math.round(n));
+ }
+
+ public Expr clone(Object context)
+ {
+ return new RoundFunction(arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return arg.references(var);
+ }
+
+ public String toString()
+ {
+ return "round(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/Selector.java b/libjava/classpath/gnu/xml/xpath/Selector.java
new file mode 100644
index 000000000..2146f26f5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/Selector.java
@@ -0,0 +1,503 @@
+/* Selector.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Attr;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+/**
+ * A single component of a location path.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class Selector
+ extends Path
+{
+
+ public static final int ANCESTOR = 0;
+ public static final int ANCESTOR_OR_SELF = 1;
+ public static final int ATTRIBUTE = 2;
+ public static final int CHILD = 3;
+ public static final int DESCENDANT = 4;
+ public static final int DESCENDANT_OR_SELF = 5;
+ public static final int FOLLOWING = 6;
+ public static final int FOLLOWING_SIBLING = 7;
+ public static final int NAMESPACE = 8;
+ public static final int PARENT = 9;
+ public static final int PRECEDING = 10;
+ public static final int PRECEDING_SIBLING = 11;
+ public static final int SELF = 12;
+
+ /**
+ * Axis to select nodes in.
+ */
+ final int axis;
+
+ /**
+ * List of tests to perform on candidates.
+ */
+ final Test[] tests;
+
+ public Selector(int axis, List<? extends Test> tests)
+ {
+ this.axis = axis;
+ int len = tests.size();
+ this.tests = new Test[(len == 0) ? 1 : len];
+ if (len > 0)
+ tests.toArray(this.tests);
+ else
+ this.tests[0] = new NodeTypeTest((short) 0);
+ if (axis == NAMESPACE && this.tests[0] instanceof NameTest)
+ {
+ NameTest nt = (NameTest) this.tests[0];
+ this.tests[0] = new NamespaceTest(nt.qName, nt.anyLocalName, nt.any);
+ }
+ }
+
+ /**
+ * Returns the list of tests to perform on candidates.
+ */
+ public Test[] getTests()
+ {
+ return tests;
+ }
+
+ public boolean matches(Node context)
+ {
+ // If called directly, selector is the top level of the path
+ return matches(context,
+ getContextPosition(context),
+ getContextSize(context));
+ }
+
+ boolean matches(Node context, int pos, int len)
+ {
+ short nodeType = context.getNodeType();
+ switch (axis)
+ {
+ case CHILD:
+ if (nodeType == Node.ATTRIBUTE_NODE)
+ return false;
+ break;
+ case ATTRIBUTE:
+ case NAMESPACE:
+ if (nodeType != Node.ATTRIBUTE_NODE)
+ return false;
+ break;
+ case DESCENDANT_OR_SELF:
+ return true;
+ default:
+ return false;
+ }
+ for (int j = 0; j < tests.length && len > 0; j++)
+ {
+ Test test = tests[j];
+ if (!test.matches(context, pos, len))
+ return false;
+ }
+ return true;
+ }
+
+ private int getContextPosition(Node ctx)
+ {
+ int pos = 1;
+ for (ctx = ctx.getPreviousSibling(); ctx != null;
+ ctx = ctx.getPreviousSibling())
+ {
+ if (tests[0].matches(ctx, 1, 1))
+ pos++;
+ }
+ return pos;
+ }
+
+ private int getContextSize(Node ctx)
+ {
+ if (ctx.getNodeType() == Node.ATTRIBUTE_NODE)
+ {
+ Node owner = ((Attr) ctx).getOwnerElement();
+ return owner.getAttributes().getLength();
+ }
+ int count = 1;
+ Node sib = ctx.getPreviousSibling();
+ for (; sib != null; sib = sib.getPreviousSibling())
+ {
+ if (tests[0].matches(ctx, 1, 1))
+ count++;
+ }
+ sib = ctx.getNextSibling();
+ for (; sib != null; sib = sib.getNextSibling())
+ {
+ if (tests[0].matches(ctx, 1, 1))
+ count++;
+ }
+ return count;
+ }
+
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Set<Node> acc = new LinkedHashSet<Node>();
+ addCandidates(context, acc);
+ List<Node> candidates = new ArrayList<Node>(acc);
+ List<Node> ret = filterCandidates(candidates, false);
+ return ret;
+ }
+
+ Collection<Node> evaluate(Node context, Collection<Node> ns)
+ {
+ Set<Node> acc = new LinkedHashSet<Node>();
+ for (Iterator<Node> i = ns.iterator(); i.hasNext(); )
+ addCandidates(i.next(), acc);
+ List<Node> candidates = new ArrayList<Node>(acc);
+ List<Node> ret = filterCandidates(candidates, true);
+ return ret;
+ }
+
+ /**
+ * Filter the given list of candidates according to the node tests.
+ */
+ List<Node> filterCandidates(List<Node> candidates, boolean cascade)
+ {
+ int len = candidates.size();
+ int tlen = tests.length;
+ if (tlen > 0 && len > 0)
+ {
+ // Present the result of each successful generation to the next test
+ for (int j = 0; j < tlen && len > 0; j++)
+ {
+ Test test = tests[j];
+ List<Node> successful = new ArrayList<Node>(len);
+ for (int i = 0; i < len; i++)
+ {
+ Node node = candidates.get(i);
+ if (cascade)
+ {
+ // Documents and DocumentFragments should be considered
+ // if part of a location path where the axis involves
+ // the SELF concept
+ short nodeType = node.getNodeType();
+ if ((nodeType == Node.DOCUMENT_NODE ||
+ nodeType == Node.DOCUMENT_FRAGMENT_NODE) &&
+ (axis == DESCENDANT_OR_SELF ||
+ axis == ANCESTOR_OR_SELF ||
+ axis == SELF) &&
+ (tests.length == 1 &&
+ tests[0] instanceof NodeTypeTest &&
+ ((NodeTypeTest) tests[0]).type == (short) 0))
+ {
+ successful.add(node);
+ continue;
+ }
+ }
+ if (test.matches(node, i + 1, len))
+ successful.add(node);
+ }
+ candidates = successful;
+ len = candidates.size();
+ }
+ }
+ return candidates;
+ }
+
+ void addCandidates(Node context, Collection<Node> candidates)
+ {
+ // Build list of candidates
+ switch (axis)
+ {
+ case CHILD:
+ addChildNodes(context, candidates, false);
+ break;
+ case DESCENDANT:
+ addChildNodes(context, candidates, true);
+ break;
+ case DESCENDANT_OR_SELF:
+ candidates.add (context);
+ addChildNodes(context, candidates, true);
+ break;
+ case PARENT:
+ addParentNode(context, candidates, false);
+ break;
+ case ANCESTOR:
+ addParentNode(context, candidates, true);
+ break;
+ case ANCESTOR_OR_SELF:
+ candidates.add(context);
+ addParentNode(context, candidates, true);
+ break;
+ case FOLLOWING_SIBLING:
+ addFollowingNodes(context, candidates, false);
+ break;
+ case PRECEDING_SIBLING:
+ addPrecedingNodes(context, candidates, false);
+ break;
+ case FOLLOWING:
+ addFollowingNodes(context, candidates, true);
+ break;
+ case PRECEDING:
+ addPrecedingNodes(context, candidates, true);
+ break;
+ case ATTRIBUTE:
+ addAttributes(context, candidates);
+ break;
+ case NAMESPACE:
+ addNamespaceAttributes(context, candidates);
+ break;
+ case SELF:
+ candidates.add(context);
+ break;
+ }
+ }
+
+ void addChildNodes(Node context, Collection<Node> acc, boolean recurse)
+ {
+ Node child = context.getFirstChild();
+ while (child != null)
+ {
+ acc.add(child);
+ if (recurse)
+ addChildNodes(child, acc, recurse);
+ child = child.getNextSibling();
+ }
+ }
+
+ void addParentNode(Node context, Collection<Node> acc, boolean recurse)
+ {
+ Node parent = (context.getNodeType() == Node.ATTRIBUTE_NODE) ?
+ ((Attr) context).getOwnerElement() : context.getParentNode();
+ if (parent != null)
+ {
+ acc.add(parent);
+ if (recurse)
+ addParentNode(parent, acc, recurse);
+ }
+ }
+
+ void addFollowingNodes(Node context, Collection<Node> acc, boolean recurse)
+ {
+ if (context != null && recurse)
+ addChildNodes(context, acc, true);
+ Node cur = (context.getNodeType() == Node.ATTRIBUTE_NODE) ? null :
+ context.getNextSibling();
+ while (cur != null)
+ {
+ acc.add(cur);
+ if (recurse)
+ addChildNodes(cur, acc, true);
+ cur = cur.getNextSibling();
+ }
+ if (recurse)
+ {
+ while (context != null)
+ {
+ context = (context.getNodeType() == Node.ATTRIBUTE_NODE) ?
+ ((Attr) context).getOwnerElement() : context.getParentNode();
+ if (context != null)
+ {
+ cur = context.getNextSibling();
+ while (cur != null)
+ {
+ acc.add(cur);
+ if (recurse)
+ addChildNodes(cur, acc, true);
+ cur = cur.getNextSibling();
+ }
+ }
+ }
+ }
+ }
+
+ void addPrecedingNodes(Node context, Collection<Node> acc, boolean recurse)
+ {
+ Node cur = (context.getNodeType() == Node.ATTRIBUTE_NODE) ? null :
+ context.getPreviousSibling();
+ while (cur != null)
+ {
+ acc.add(cur);
+ if (recurse)
+ addChildNodes(cur, acc, true);
+ cur = cur.getPreviousSibling();
+ }
+ if (recurse)
+ {
+ cur = context;
+ cur = (cur.getNodeType() == Node.ATTRIBUTE_NODE) ?
+ ((Attr) cur).getOwnerElement() : cur.getParentNode();
+ if (cur != null)
+ addPrecedingNodes(cur, acc, recurse);
+ }
+ }
+
+ void addAttributes(Node context, Collection<Node> acc)
+ {
+ NamedNodeMap attrs = context.getAttributes();
+ if (attrs != null)
+ {
+ int attrLen = attrs.getLength();
+ for (int i = 0; i < attrLen; i++)
+ {
+ Node attr = attrs.item(i);
+ if (!isNamespaceAttribute(attr))
+ {
+ acc.add(attr);
+ }
+ }
+ }
+ }
+
+ void addNamespaceAttributes(Node context, Collection<Node> acc)
+ {
+ NamedNodeMap attrs = context.getAttributes();
+ if (attrs != null)
+ {
+ int attrLen = attrs.getLength();
+ for (int i = 0; i < attrLen; i++)
+ {
+ Node attr = attrs.item(i);
+ if (isNamespaceAttribute(attr))
+ acc.add(attr);
+ }
+ }
+ }
+
+ final boolean isNamespaceAttribute(Node node)
+ {
+ String uri = node.getNamespaceURI();
+ return (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri) ||
+ XMLConstants.XMLNS_ATTRIBUTE.equals(node.getPrefix()) ||
+ XMLConstants.XMLNS_ATTRIBUTE.equals(node.getNodeName()));
+ }
+
+ public Expr clone(Object context)
+ {
+ int len = tests.length;
+ List<Test> tests2 = new ArrayList<Test>(len);
+ for (int i = 0; i < len; i++)
+ tests2.add(tests[i].clone(context));
+ return new Selector(axis, tests2);
+ }
+
+ public boolean references(QName var)
+ {
+ for (int i = 0; i < tests.length; i++)
+ {
+ if (tests[i].references(var))
+ return true;
+ }
+ return false;
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ switch (axis)
+ {
+ case ANCESTOR:
+ buf.append("ancestor::");
+ break;
+ case ANCESTOR_OR_SELF:
+ buf.append("ancestor-or-self::");
+ break;
+ case ATTRIBUTE:
+ if (tests.length == 0 ||
+ (tests[0] instanceof NameTest))
+ buf.append('@');
+ else
+ buf.append("attribute::");
+ break;
+ case CHILD:
+ //buf.append("child::");
+ break;
+ case DESCENDANT:
+ buf.append("descendant::");
+ break;
+ case DESCENDANT_OR_SELF:
+ buf.append("descendant-or-self::");
+ break;
+ case FOLLOWING:
+ buf.append("following::");
+ break;
+ case FOLLOWING_SIBLING:
+ buf.append("following-sibling::");
+ break;
+ case NAMESPACE:
+ buf.append("namespace::");
+ break;
+ case PARENT:
+ if (tests.length == 0 ||
+ (tests[0] instanceof NodeTypeTest &&
+ ((NodeTypeTest) tests[0]).type == 0))
+ return "..";
+ buf.append("parent::");
+ break;
+ case PRECEDING:
+ buf.append("preceding::");
+ break;
+ case PRECEDING_SIBLING:
+ buf.append("preceding-sibling::");
+ break;
+ case SELF:
+ if (tests.length == 0 ||
+ (tests[0] instanceof NodeTypeTest &&
+ ((NodeTypeTest) tests[0]).type == 0))
+ return ".";
+ buf.append("self::");
+ break;
+ }
+ if (tests.length == 0)
+ buf.append("[error]");
+ else
+ {
+ for (int i = 0; i < tests.length; i++)
+ buf.append(tests[i]);
+ }
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/StartsWithFunction.java b/libjava/classpath/gnu/xml/xpath/StartsWithFunction.java
new file mode 100644
index 000000000..6f9716a2d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/StartsWithFunction.java
@@ -0,0 +1,92 @@
+/* StartsWithFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>starts-with</code> function returns true if the first argument
+ * string starts with the second argument string, and otherwise returns false.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class StartsWithFunction
+ extends Expr
+{
+
+ final Expr arg1;
+ final Expr arg2;
+
+ StartsWithFunction(List<Expr> args)
+ {
+ this(args.get(0), args.get(1));
+ }
+
+ StartsWithFunction(Expr arg1, Expr arg2)
+ {
+ this.arg1 = arg1;
+ this.arg2 = arg2;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val1 = arg1.evaluate(context, pos, len);
+ Object val2 = arg2.evaluate(context, pos, len);
+ String s1 = _string(context, val1);
+ String s2 = _string(context, val2);
+ return s1.startsWith(s2) ? Boolean.TRUE : Boolean.FALSE;
+ }
+
+ public Expr clone(Object context)
+ {
+ return new StartsWithFunction(arg1.clone(context), arg2.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return (arg1.references(var) || arg2.references(var));
+ }
+
+ public String toString()
+ {
+ return "starts-with(" + arg1 + "," + arg2 + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/Steps.java b/libjava/classpath/gnu/xml/xpath/Steps.java
new file mode 100644
index 000000000..75644541c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/Steps.java
@@ -0,0 +1,260 @@
+/* Steps.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.Set;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Node;
+
+/**
+ * A list of transitions between components in a location path.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class Steps
+ extends Path
+{
+
+ final LinkedList<Expr> path;
+
+ public Steps()
+ {
+ this(new LinkedList<Expr>());
+ }
+
+ Steps(LinkedList<Expr> path)
+ {
+ this.path = path;
+ }
+
+ public boolean matches(Node context)
+ {
+ // Right to left
+ return matches(context, path.size() - 1);
+ }
+
+ boolean matches(Node context, int pos)
+ {
+ Pattern right = (Pattern) path.get(pos);
+ if (!right.matches(context))
+ {
+ return false;
+ }
+ if (pos > 0)
+ {
+ Pattern left = (Pattern) path.get(pos - 1);
+ for (Node candidate : possibleContexts(right, context))
+ {
+ if (left.matches(candidate) &&
+ matches(candidate, pos - 1))
+ {
+ return true;
+ }
+ // keep going, there may be another candidate
+ }
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Essentially the reverse of Selector.addCandidates.
+ * The idea is to determine possible context nodes for a match.
+ */
+ Collection<Node> possibleContexts(Pattern pattern, Node context)
+ {
+ if (pattern instanceof Selector)
+ {
+ Selector s = (Selector) pattern;
+ Collection<Node> candidates = new LinkedHashSet<Node>();
+ switch (s.axis)
+ {
+ case Selector.PARENT:
+ s.addChildNodes(context, candidates, false);
+ break;
+ case Selector.ANCESTOR:
+ s.addChildNodes(context, candidates, true);
+ break;
+ case Selector.ANCESTOR_OR_SELF:
+ candidates.add (context);
+ s.addChildNodes(context, candidates, true);
+ break;
+ case Selector.CHILD:
+ s.addParentNode(context, candidates, false);
+ break;
+ case Selector.DESCENDANT:
+ s.addParentNode(context, candidates, true);
+ break;
+ case Selector.DESCENDANT_OR_SELF:
+ candidates.add(context);
+ s.addParentNode(context, candidates, true);
+ break;
+ case Selector.PRECEDING_SIBLING:
+ s.addFollowingNodes(context, candidates, false);
+ break;
+ case Selector.FOLLOWING_SIBLING:
+ s.addPrecedingNodes(context, candidates, false);
+ break;
+ case Selector.PRECEDING:
+ s.addFollowingNodes(context, candidates, true);
+ break;
+ case Selector.FOLLOWING:
+ s.addPrecedingNodes(context, candidates, true);
+ break;
+ case Selector.ATTRIBUTE:
+ case Selector.NAMESPACE:
+ if (context.getNodeType() == Node.ATTRIBUTE_NODE)
+ {
+ candidates.add(((Attr) context).getOwnerElement());
+ }
+ break;
+ case Selector.SELF:
+ candidates.add(context);
+ break;
+ }
+ return candidates;
+ }
+ return Collections.emptySet();
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ //System.err.println(toString()+" evaluate");
+ // Left to right
+ Iterator<Expr> i = path.iterator();
+ Expr lhs = i.next();
+ Object val = lhs.evaluate(context, pos, len);
+ //System.err.println("\tevaluate "+lhs+" = "+val);
+ while (val instanceof Collection && i.hasNext())
+ {
+ Path rhs = (Path) i.next();
+ /* Suppression is safe, as we know context produces Collection<Node> */
+ @SuppressWarnings("unchecked")
+ Collection<Node> nodes = (Collection<Node>) val;
+ val = rhs.evaluate(context, nodes);
+ //System.err.println("\tevaluate "+rhs+" = "+val);
+ }
+ return val;
+ }
+
+ @Override
+ Collection<Node> evaluate(Node context, Collection<Node> ns)
+ {
+ // Left to right
+ Iterator<Expr> i = path.iterator();
+ Expr lhs = i.next();
+ if (lhs instanceof Path)
+ {
+ ns = ((Path) lhs).evaluate(context, ns);
+ }
+ else
+ {
+ Set<Node> acc = new LinkedHashSet<Node>();
+ int pos = 1, len = ns.size();
+ for (Node node : ns)
+ {
+ Object ret = lhs.evaluate(node, pos++, len);
+ if (ret instanceof Collection)
+ {
+ /* Suppression is safe, as we know context produces Collection<Node> */
+ @SuppressWarnings("unchecked")
+ Collection<Node> nodes = (Collection<Node>) ret;
+ acc.addAll(nodes);
+ }
+ }
+ ns = acc;
+ }
+ while (i.hasNext())
+ {
+ Path rhs = (Path) i.next();
+ ns = rhs.evaluate(context, ns);
+ }
+ return ns;
+ }
+
+ public Expr clone(Object context)
+ {
+ int len = path.size();
+ LinkedList<Expr> path2 = new LinkedList<Expr>();
+ for (int i = 0; i < len; i++)
+ {
+ path2.add(path.get(i).clone(context));
+ }
+ return new Steps(path2);
+ }
+
+ public boolean references(QName var)
+ {
+ for (Iterator<Expr> i = path.iterator(); i.hasNext(); )
+ {
+ if (i.next().references(var))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ Iterator<Expr> i = path.iterator();
+ Expr expr = i.next();
+ if (!(expr instanceof Root))
+ {
+ buf.append(expr);
+ }
+ while (i.hasNext())
+ {
+ expr = i.next();
+ buf.append('/');
+ buf.append(expr);
+ }
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/StringFunction.java b/libjava/classpath/gnu/xml/xpath/StringFunction.java
new file mode 100644
index 000000000..b2b3cecf7
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/StringFunction.java
@@ -0,0 +1,119 @@
+/* StringFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>string function converts an object to a string as follows:
+ * <ul>
+ * <li>A node-set is converted to a string by returning the string-value of
+ * the node in the node-set that is first in document order. If the node-set
+ * is empty, an empty string is returned.</li>
+ * <li>A number is converted to a string as follows
+ * <ul>
+ * <li>NaN is converted to the string NaN</li>
+ * <li>positive zero is converted to the string 0</li>
+ * <li>negative zero is converted to the string 0</li>
+ * <li>positive infinity is converted to the string Infinity</li>
+ * <li>negative infinity is converted to the string -Infinity</li>
+ * <li>if the number is an integer, the number is represented in decimal
+ * form as a Number with no decimal point and no leading zeros, preceded by
+ * a minus sign (-) if the number is negative</li>
+ * <li>otherwise, the number is represented in decimal form as a Number
+ * including a decimal point with at least one digit before the decimal
+ * point and at least one digit after the decimal point, preceded by a minus
+ * sign (-) if the number is negative; there must be no leading zeros before
+ * the decimal point apart possibly from the one required digit immediately
+ * before the decimal point; beyond the one required digit after the decimal
+ * point there must be as many, but only as many, more digits as are needed
+ * to uniquely distinguish the number from all other IEEE 754 numeric
+ * values.</li>
+ * </ul>
+ * </li>
+ * <li>The boolean false value is converted to the string false. The boolean
+ * true value is converted to the string true.</li>
+ * <li>An object of a type other than the four basic types is converted to a
+ * string in a way that is dependent on that type.</li>
+ * </ul>
+ * If the argument is omitted, it defaults to a node-set with the context
+ * node as its only member.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class StringFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ StringFunction(List<Expr> args)
+ {
+ this(args.size() > 0 ? args.get(0) : null);
+ }
+
+ StringFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val = (arg == null) ? null : arg.evaluate(context, pos, len);
+ return _string(context, val);
+ }
+
+ public Expr clone(Object context)
+ {
+ return new StringFunction((arg == null) ? null :
+ arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return (arg == null) ? false : arg.references(var);
+ }
+
+ public String toString()
+ {
+ return (arg == null) ? "string()" : "string(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/StringLengthFunction.java b/libjava/classpath/gnu/xml/xpath/StringLengthFunction.java
new file mode 100644
index 000000000..c72b0d9bc
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/StringLengthFunction.java
@@ -0,0 +1,92 @@
+/* StringLengthFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>string-length</code> function returns the number of characters
+ * in the string.
+ * If the argument is omitted, it defaults to the context node converted to
+ * a string, in other words the string-value of the context node.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class StringLengthFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ StringLengthFunction(List<Expr> args)
+ {
+ this(args.isEmpty() ? null : (Expr) args.get(0));
+ }
+
+ StringLengthFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val = (arg == null) ? null : arg.evaluate(context, pos, len);
+ String s = _string(context, val);
+ return new Double((double) s.length());
+ }
+
+ public Expr clone(Object context)
+ {
+ return new StringLengthFunction((arg == null) ? null :
+ arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return (arg == null) ? false : arg.references(var);
+ }
+
+ public String toString()
+ {
+ return (arg == null) ? "string-length()" : "string-length(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/SubstringAfterFunction.java b/libjava/classpath/gnu/xml/xpath/SubstringAfterFunction.java
new file mode 100644
index 000000000..4f10723a7
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/SubstringAfterFunction.java
@@ -0,0 +1,98 @@
+/* SubstringAfterFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>substring-after</code> function returns the substring of the
+ * first argument string that follows the first occurrence of the second
+ * argument string in the first argument string, or the empty string if the
+ * first argument string does not contain the second argument string. For
+ * example, substring-after("1999/04/01","/") returns 04/01, and
+ * substring-after("1999/04/01","19") returns 99/04/01.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class SubstringAfterFunction
+ extends Expr
+{
+
+ final Expr arg1;
+ final Expr arg2;
+
+ SubstringAfterFunction(List<Expr> args)
+ {
+ this(args.get(0), args.get(1));
+ }
+
+ SubstringAfterFunction(Expr arg1, Expr arg2)
+ {
+ this.arg1 = arg1;
+ this.arg2 = arg2;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val1 = arg1.evaluate(context, pos, len);
+ Object val2 = arg2.evaluate(context, pos, len);
+ String s1 = _string(context, val1);
+ String s2 = _string(context, val2);
+ int index = s1.indexOf(s2);
+ return (index == -1) ? "" : s1.substring(index + s2.length());
+ }
+
+ public Expr clone(Object context)
+ {
+ return new SubstringAfterFunction(arg1.clone(context),
+ arg2.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return (arg1.references(var) || arg2.references(var));
+ }
+
+ public String toString()
+ {
+ return "substring-after(" + arg1 + "," + arg2 + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/SubstringBeforeFunction.java b/libjava/classpath/gnu/xml/xpath/SubstringBeforeFunction.java
new file mode 100644
index 000000000..1a5a04c4f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/SubstringBeforeFunction.java
@@ -0,0 +1,97 @@
+/* SubstringBeforeFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>substring-before</code> function returns the substring of the
+ * first argument string that precedes the first occurrence of the second
+ * argument string in the first argument string, or the empty string if the
+ * first argument string does not contain the second argument string. For
+ * example, substring-before("1999/04/01","/") returns 1999.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class SubstringBeforeFunction
+ extends Expr
+{
+
+ final Expr arg1;
+ final Expr arg2;
+
+ SubstringBeforeFunction(List<Expr> args)
+ {
+ this(args.get(0), args.get(1));
+ }
+
+ SubstringBeforeFunction(Expr arg1, Expr arg2)
+ {
+ this.arg1 = arg1;
+ this.arg2 = arg2;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val1 = arg1.evaluate(context, pos, len);
+ Object val2 = arg2.evaluate(context, pos, len);
+ String s1 = _string(context, val1);
+ String s2 = _string(context, val2);
+ int index = s1.indexOf(s2);
+ return (index == -1) ? "" : s1.substring(0, index);
+ }
+
+ public Expr clone(Object context)
+ {
+ return new SubstringBeforeFunction(arg1.clone(context),
+ arg2.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return (arg1.references(var) || arg2.references(var));
+ }
+
+ public String toString()
+ {
+ return "substring-before(" + arg1 + "," + arg2 + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/SubstringFunction.java b/libjava/classpath/gnu/xml/xpath/SubstringFunction.java
new file mode 100644
index 000000000..d17c3b662
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/SubstringFunction.java
@@ -0,0 +1,119 @@
+/* SubstringFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The substring function returns the substring of the first argument
+ * starting at the position specified in the second argument with length
+ * specified in the third argument. For example, substring("12345",2,3)
+ * returns "234". If the third argument is not specified, it returns the
+ * substring starting at the position specified in the second argument and
+ * continuing to the end of the string. For example, substring("12345",2)
+ * returns "2345".
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class SubstringFunction
+ extends Expr
+{
+
+ final Expr arg1;
+ final Expr arg2;
+ final Expr arg3;
+
+ SubstringFunction(List<Expr> args)
+ {
+ this(args.get(0), args.get(1),
+ (args.size() > 2) ? args.get(2) : null);
+ }
+
+ SubstringFunction(Expr arg1, Expr arg2, Expr arg3)
+ {
+ this.arg1 = arg1;
+ this.arg2 = arg2;
+ this.arg3 = arg3;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val1 = arg1.evaluate(context, pos, len);
+ Object val2 = arg2.evaluate(context, pos, len);
+ String s = _string(context, val1);
+ int p = Expr.intValue(val2) - 1;
+ if (p < 0)
+ p = 0;
+
+ int l = s.length() - p;
+ if (l <= 0)
+ return "";
+
+ if (arg3 != null)
+ {
+ Object val3 = arg3.evaluate(context, pos, len);
+ int v3 = Expr.intValue(val3);
+ if (v3 < l)
+ l = v3;
+ }
+
+ return s.substring(p, p + l);
+ }
+
+ public Expr clone(Object context)
+ {
+ return new SubstringFunction(arg1.clone(context), arg2.clone(context),
+ (arg3 == null) ? null : arg3.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return (arg1.references(var) || arg2.references(var) ||
+ (arg3 == null) ? false : arg3.references(var));
+ }
+
+ public String toString()
+ {
+ return (arg3 == null) ? "substring(" + arg1 + "," + arg2 + ")" :
+ "substring(" + arg1 + "," + arg2 + "," + arg3 + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/SumFunction.java b/libjava/classpath/gnu/xml/xpath/SumFunction.java
new file mode 100644
index 000000000..6c3323fb2
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/SumFunction.java
@@ -0,0 +1,104 @@
+/* SumFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>sum</code> function returns the sum, for each node in the
+ * argument node-set, of the result of converting the string-values of the
+ * node to a number.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class SumFunction
+ extends Expr
+{
+
+ final Expr arg;
+
+ SumFunction(List<Expr> args)
+ {
+ this(args.get(0));
+ }
+
+ SumFunction(Expr arg)
+ {
+ this.arg = arg;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val = arg.evaluate(context, pos, len);
+ double sum = 0.0d;
+ if (val instanceof Collection)
+ {
+ /* Suppression is safe, as we know context produces
+ Collection<Node> */
+ @SuppressWarnings("unchecked")
+ Collection<Node> nodes = (Collection<Node>) val;
+ for (Node node : nodes)
+ {
+ String s = stringValue(node);
+ sum += _number(context, s);
+ }
+ }
+ return new Double(sum);
+ }
+
+ public Expr clone(Object context)
+ {
+ return new SumFunction(arg.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return arg.references(var);
+ }
+
+ public String toString()
+ {
+ return "sum(" + arg + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/Test.java b/libjava/classpath/gnu/xml/xpath/Test.java
new file mode 100644
index 000000000..529c99bd8
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/Test.java
@@ -0,0 +1,58 @@
+/* Test.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * A test that can be performed on a node to determine whether to include it
+ * in a selection.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public abstract class Test
+{
+
+ public abstract boolean matches(Node node, int pos, int len);
+
+ public abstract Test clone(Object context);
+
+ public abstract boolean references(QName var);
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/TranslateFunction.java b/libjava/classpath/gnu/xml/xpath/TranslateFunction.java
new file mode 100644
index 000000000..4e06354c3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/TranslateFunction.java
@@ -0,0 +1,135 @@
+/* TranslateFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.List;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>translate</code> function returns the first argument string
+ * with occurrences of characters in the second argument string replaced by
+ * the character at the corresponding position in the third argument string.
+ * For example, translate("bar","abc","ABC") returns the string BAr. If
+ * there is a character in the second argument string with no character at a
+ * corresponding position in the third argument string (because the second
+ * argument string is longer than the third argument string), then
+ * occurrences of that character in the first argument string are removed.
+ * For example, translate("--aaa--","abc-","ABC") returns "AAA". If a
+ * character occurs more than once in the second argument string, then the
+ * first occurrence determines the replacement character. If the third
+ * argument string is longer than the second argument string, then excess
+ * characters are ignored.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class TranslateFunction
+ extends Expr
+{
+
+ final Expr arg1;
+ final Expr arg2;
+ final Expr arg3;
+
+ TranslateFunction(List<Expr> args)
+ {
+ this(args.get(0), args.get(1), args.get(2));
+ }
+
+ TranslateFunction(Expr arg1, Expr arg2, Expr arg3)
+ {
+ this.arg1 = arg1;
+ this.arg2 = arg2;
+ this.arg3 = arg3;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object val1 = arg1.evaluate(context, pos, len);
+ Object val2 = arg2.evaluate(context, pos, len);
+ Object val3 = arg3.evaluate(context, pos, len);
+ String string = _string(context, val1);
+ String search = _string(context, val2);
+ String replace = _string(context, val3);
+ CPStringBuilder buf = new CPStringBuilder();
+ int l1 = string.length();
+ int l2 = search.length();
+ int l3 = replace.length();
+ for (int i = 0; i < l1; i++)
+ {
+ char c = string.charAt(i);
+ boolean replaced = false;
+ for (int j = 0; j < l2; j++)
+ {
+ if (c == search.charAt(j))
+ {
+ if (j < l3)
+ {
+ buf.append(replace.charAt(j));
+ }
+ replaced = true;
+ }
+ }
+ if (!replaced)
+ {
+ buf.append(c);
+ }
+ }
+ return buf.toString();
+ }
+
+ public Expr clone(Object context)
+ {
+ return new TranslateFunction(arg1.clone(context), arg2.clone(context),
+ arg3.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return (arg1.references(var) || arg2.references(var) ||
+ arg3.references(var));
+ }
+
+ public String toString()
+ {
+ return "translate(" + arg1 + "," + arg2 + "," + arg3 + ")";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/TrueFunction.java b/libjava/classpath/gnu/xml/xpath/TrueFunction.java
new file mode 100644
index 000000000..41ab54b75
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/TrueFunction.java
@@ -0,0 +1,72 @@
+/* TrueFunction.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The <code>true</code> function returns true.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+final class TrueFunction
+ extends Expr
+{
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ return Boolean.TRUE;
+ }
+
+ public Expr clone(Object context)
+ {
+ return new TrueFunction();
+ }
+
+ public boolean references(QName var)
+ {
+ return false;
+ }
+
+ public String toString()
+ {
+ return "true()";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/UnionExpr.java b/libjava/classpath/gnu/xml/xpath/UnionExpr.java
new file mode 100644
index 000000000..bd5f5e990
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/UnionExpr.java
@@ -0,0 +1,117 @@
+/* UnionExpr.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import javax.xml.namespace.QName;
+import org.w3c.dom.Node;
+
+/**
+ * The union of two node-sets.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public final class UnionExpr
+ extends Pattern
+{
+
+ final Expr lhs;
+ final Expr rhs;
+
+ public UnionExpr(Expr lhs, Expr rhs)
+ {
+ this.lhs = lhs;
+ this.rhs = rhs;
+ }
+
+ public boolean matches(Node context)
+ {
+ if (lhs instanceof Pattern && rhs instanceof Pattern)
+ {
+ return ((Pattern) lhs).matches(context) ||
+ ((Pattern) rhs).matches(context);
+ }
+ return false;
+ }
+
+ @Override
+ public Object evaluate(Node context, int pos, int len)
+ {
+ Object left = lhs.evaluate(context, pos, len);
+ Object right = rhs.evaluate(context, pos, len);
+ List<Node> list;
+ if (left instanceof Collection && right instanceof Collection)
+ {
+ Set<Node> set = new HashSet<Node>();
+ /* Suppression is safe as addAll will check the types
+ of the elements and throw a ClassCastException as necessary */
+ @SuppressWarnings("unchecked")
+ Collection<Node> l = (Collection<Node>) left;
+ @SuppressWarnings("unchecked")
+ Collection<Node> r = (Collection<Node>) right;
+ set.addAll (l);
+ set.addAll (r);
+ list = new ArrayList<Node>(set);
+ Collections.sort(list, documentOrderComparator);
+ }
+ else
+ list = Collections.emptyList();
+ return list;
+ }
+
+ public Expr clone(Object context)
+ {
+ return new UnionExpr(lhs.clone(context), rhs.clone(context));
+ }
+
+ public boolean references(QName var)
+ {
+ return (lhs.references(var) || rhs.references(var));
+ }
+
+ public String toString()
+ {
+ return lhs + " | " + rhs;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/VariableReference.java b/libjava/classpath/gnu/xml/xpath/VariableReference.java
new file mode 100644
index 000000000..e1698c94c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/VariableReference.java
@@ -0,0 +1,102 @@
+/* VariableReference.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import gnu.java.lang.CPStringBuilder;
+
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathVariableResolver;
+import org.w3c.dom.Node;
+import gnu.xml.transform.Bindings;
+
+public class VariableReference
+ extends Expr
+{
+
+ final XPathVariableResolver resolver;
+ final QName name;
+
+ public VariableReference(XPathVariableResolver resolver, QName name)
+ {
+ this.resolver = resolver;
+ this.name = name;
+ }
+
+ public Object evaluate(Node context, int pos, int len)
+ {
+ if (resolver != null)
+ {
+ if (resolver instanceof Bindings)
+ {
+ // Needs context to operate properly
+ return ((Bindings) resolver).get(name, context, pos, len);
+ }
+ return resolver.resolveVariable(name);
+ }
+ throw new IllegalStateException("no variable resolver");
+ }
+
+ public Expr clone(Object context)
+ {
+ XPathVariableResolver r = resolver;
+ if (context instanceof XPathVariableResolver)
+ {
+ r = (XPathVariableResolver) context;
+ }
+ return new VariableReference(r, name);
+ }
+
+ public boolean references(QName var)
+ {
+ return name.equals(var);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("$");
+ String prefix = name.getPrefix();
+ if (prefix != null && !"".equals(prefix))
+ {
+ buf.append(prefix);
+ buf.append(':');
+ }
+ buf.append(name.getLocalPart());
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/XPathFactoryImpl.java b/libjava/classpath/gnu/xml/xpath/XPathFactoryImpl.java
new file mode 100644
index 000000000..970cf0e5d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/XPathFactoryImpl.java
@@ -0,0 +1,90 @@
+/* XPathFactoryImpl.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathFactory;
+import javax.xml.xpath.XPathFactoryConfigurationException;
+import javax.xml.xpath.XPathFunctionResolver;
+import javax.xml.xpath.XPathVariableResolver;
+
+/**
+ * GNU XPath factory implementation.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class XPathFactoryImpl
+ extends XPathFactory
+{
+
+ XPathVariableResolver variableResolver;
+ XPathFunctionResolver functionResolver;
+
+ public boolean isObjectModelSupported(String objectModel)
+ {
+ return XPathFactory.DEFAULT_OBJECT_MODEL_URI.equals(objectModel);
+ }
+
+ public void setFeature(String name, boolean value)
+ throws XPathFactoryConfigurationException
+ {
+ throw new XPathFactoryConfigurationException(name);
+ }
+
+ public boolean getFeature(String name)
+ throws XPathFactoryConfigurationException
+ {
+ throw new XPathFactoryConfigurationException(name);
+ }
+
+ public void setXPathVariableResolver(XPathVariableResolver resolver)
+ {
+ variableResolver = resolver;
+ }
+
+ public void setXPathFunctionResolver(XPathFunctionResolver resolver)
+ {
+ functionResolver = resolver;
+ }
+
+ public XPath newXPath()
+ {
+ return new XPathImpl(null, variableResolver, functionResolver);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/XPathImpl.java b/libjava/classpath/gnu/xml/xpath/XPathImpl.java
new file mode 100644
index 000000000..62f86171b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/XPathImpl.java
@@ -0,0 +1,164 @@
+/* XPathImpl.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import java.io.IOException;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFunctionResolver;
+import javax.xml.xpath.XPathVariableResolver;
+import org.xml.sax.InputSource;
+
+/**
+ * JAXP XPath implementation.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class XPathImpl
+ implements XPath
+{
+
+ XPathParser parser;
+ NamespaceContext namespaceContext;
+ XPathVariableResolver variableResolver;
+ XPathFunctionResolver functionResolver;
+
+ XPathImpl(NamespaceContext namespaceContext,
+ XPathVariableResolver variableResolver,
+ XPathFunctionResolver functionResolver)
+ {
+ parser = new XPathParser();
+ this.namespaceContext = namespaceContext;
+ this.variableResolver = variableResolver;
+ this.functionResolver = functionResolver;
+ reset();
+ }
+
+ public void reset()
+ {
+ parser.namespaceContext = namespaceContext;
+ parser.variableResolver = variableResolver;
+ parser.functionResolver = functionResolver;
+ }
+
+ public void setXPathVariableResolver(XPathVariableResolver resolver)
+ {
+ parser.variableResolver = resolver;
+ }
+
+ public XPathVariableResolver getXPathVariableResolver()
+ {
+ return parser.variableResolver;
+ }
+
+ public void setXPathFunctionResolver(XPathFunctionResolver resolver)
+ {
+ parser.functionResolver = resolver;
+ }
+
+ public XPathFunctionResolver getXPathFunctionResolver()
+ {
+ return parser.functionResolver;
+ }
+
+ public void setNamespaceContext(NamespaceContext nsContext)
+ {
+ parser.namespaceContext = nsContext;
+ }
+
+ public NamespaceContext getNamespaceContext()
+ {
+ return parser.namespaceContext;
+ }
+
+ public XPathExpression compile(String expression)
+ throws XPathExpressionException
+ {
+ XPathTokenizer tokenizer = new XPathTokenizer(expression);
+ try
+ {
+ return (Expr) parser.yyparse(tokenizer);
+ }
+ catch (IOException e)
+ {
+ throw new XPathExpressionException(e);
+ }
+ catch (XPathParser.yyException e)
+ {
+ throw new XPathExpressionException(expression);
+ }
+ }
+
+ public Object evaluate(String expression,
+ Object item,
+ QName returnType)
+ throws XPathExpressionException
+ {
+ XPathExpression expr = compile(expression);
+ return expr.evaluate(item, returnType);
+ }
+
+ public String evaluate(String expression,
+ Object item)
+ throws XPathExpressionException
+ {
+ XPathExpression expr = compile(expression);
+ return expr.evaluate(item);
+ }
+
+ public Object evaluate(String expression,
+ InputSource source,
+ QName returnType)
+ throws XPathExpressionException
+ {
+ XPathExpression expr = compile(expression);
+ return expr.evaluate(source, returnType);
+ }
+
+ public String evaluate(String expression,
+ InputSource source)
+ throws XPathExpressionException
+ {
+ XPathExpression expr = compile(expression);
+ return expr.evaluate(source);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/XPathParser.java b/libjava/classpath/gnu/xml/xpath/XPathParser.java
new file mode 100644
index 000000000..26e59091e
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/XPathParser.java
@@ -0,0 +1,1475 @@
+// created by jay 0.8 (c) 1998 Axel.Schreiner@informatik.uni-osnabrueck.de
+
+ // line 2 "XPathParser.y"
+/* XPathParser.y - An XPath 1.0 parser.
+ Copyright (C) 2004 The Free Software Foundation
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.xml.xpath;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathFunctionResolver;
+import javax.xml.xpath.XPathVariableResolver;
+import org.w3c.dom.Node;
+
+/**
+ * An XPath 1.0 parser.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class XPathParser
+{
+
+ NamespaceContext namespaceContext;
+ XPathVariableResolver variableResolver;
+ XPathFunctionResolver functionResolver;
+
+ QName getQName(String name)
+ {
+ QName qName = QName.valueOf(name);
+ if (namespaceContext != null)
+ {
+ String prefix = qName.getPrefix();
+ String uri = qName.getNamespaceURI();
+ if (prefix != null && (uri == null || uri.length() == 0))
+ {
+ uri = namespaceContext.getNamespaceURI(prefix);
+ String localName = qName.getLocalPart();
+ qName = new QName(uri, localName, prefix);
+ }
+ }
+ return qName;
+ }
+
+ Expr lookupFunction(String name, List<Expr> args)
+ {
+ int arity = args.size();
+ if ("position".equals(name) && arity == 0)
+ {
+ return new PositionFunction();
+ }
+ else if ("last".equals(name) && arity == 0)
+ {
+ return new LastFunction();
+ }
+ else if ("string".equals(name) && (arity == 1 || arity == 0))
+ {
+ return new StringFunction(args);
+ }
+ else if ("number".equals(name) && (arity == 1 || arity == 0))
+ {
+ return new NumberFunction(args);
+ }
+ else if ("boolean".equals(name) && arity == 1)
+ {
+ return new BooleanFunction(args);
+ }
+ else if ("count".equals(name) && arity == 1)
+ {
+ return new CountFunction(args);
+ }
+ else if ("not".equals(name) && arity == 1)
+ {
+ return new NotFunction(args);
+ }
+ else if ("id".equals(name) && arity == 1)
+ {
+ return new IdFunction(args);
+ }
+ else if ("concat".equals(name) && arity > 1)
+ {
+ return new ConcatFunction(args);
+ }
+ else if ("true".equals(name) && arity == 0)
+ {
+ return new TrueFunction();
+ }
+ else if ("false".equals(name) && arity == 0)
+ {
+ return new FalseFunction();
+ }
+ else if ("name".equals(name) && (arity == 1 || arity == 0))
+ {
+ return new NameFunction(args);
+ }
+ else if ("local-name".equals(name) && (arity == 1 || arity == 0))
+ {
+ return new LocalNameFunction(args);
+ }
+ else if ("namespace-uri".equals(name) && (arity == 1 || arity == 0))
+ {
+ return new NamespaceUriFunction(args);
+ }
+ else if ("starts-with".equals(name) && arity == 2)
+ {
+ return new StartsWithFunction(args);
+ }
+ else if ("contains".equals(name) && arity == 2)
+ {
+ return new ContainsFunction(args);
+ }
+ else if ("string-length".equals(name) && (arity == 1 || arity == 0))
+ {
+ return new StringLengthFunction(args);
+ }
+ else if ("translate".equals(name) && arity == 3)
+ {
+ return new TranslateFunction(args);
+ }
+ else if ("normalize-space".equals(name) && (arity == 1 || arity == 0))
+ {
+ return new NormalizeSpaceFunction(args);
+ }
+ else if ("substring".equals(name) && (arity == 2 || arity == 3))
+ {
+ return new SubstringFunction(args);
+ }
+ else if ("substring-before".equals(name) && arity == 2)
+ {
+ return new SubstringBeforeFunction(args);
+ }
+ else if ("substring-after".equals(name) && arity == 2)
+ {
+ return new SubstringAfterFunction(args);
+ }
+ else if ("lang".equals(name) && arity == 1)
+ {
+ return new LangFunction(args);
+ }
+ else if ("sum".equals(name) && arity == 1)
+ {
+ return new SumFunction(args);
+ }
+ else if ("floor".equals(name) && arity == 1)
+ {
+ return new FloorFunction(args);
+ }
+ else if ("ceiling".equals(name) && arity == 1)
+ {
+ return new CeilingFunction(args);
+ }
+ else if ("round".equals(name) && arity == 1)
+ {
+ return new RoundFunction(args);
+ }
+ else if (functionResolver != null)
+ {
+ QName qName = QName.valueOf(name);
+ Object function = functionResolver.resolveFunction(qName, arity);
+ if (function != null &&
+ function instanceof Function &&
+ function instanceof Expr)
+ {
+ Function f = (Function) function;
+ f.setArguments(args);
+ return (Expr) function;
+ }
+ }
+ return new FunctionCall(functionResolver, name, args);
+ }
+
+ // line 211 "-"
+// %token constants
+
+ public static final int LITERAL = 257;
+ public static final int DIGITS = 258;
+ public static final int NAME = 259;
+ public static final int LP = 260;
+ public static final int RP = 261;
+ public static final int LB = 262;
+ public static final int RB = 263;
+ public static final int COMMA = 264;
+ public static final int PIPE = 265;
+ public static final int SLASH = 266;
+ public static final int DOUBLE_SLASH = 267;
+ public static final int EQ = 268;
+ public static final int NE = 269;
+ public static final int GT = 270;
+ public static final int LT = 271;
+ public static final int GTE = 272;
+ public static final int LTE = 273;
+ public static final int PLUS = 274;
+ public static final int MINUS = 275;
+ public static final int AT = 276;
+ public static final int STAR = 277;
+ public static final int DOLLAR = 278;
+ public static final int COLON = 279;
+ public static final int DOUBLE_COLON = 280;
+ public static final int DOT = 281;
+ public static final int DOUBLE_DOT = 282;
+ public static final int ANCESTOR = 283;
+ public static final int ANCESTOR_OR_SELF = 284;
+ public static final int ATTRIBUTE = 285;
+ public static final int CHILD = 286;
+ public static final int DESCENDANT = 287;
+ public static final int DESCENDANT_OR_SELF = 288;
+ public static final int FOLLOWING = 289;
+ public static final int FOLLOWING_SIBLING = 290;
+ public static final int NAMESPACE = 291;
+ public static final int PARENT = 292;
+ public static final int PRECEDING = 293;
+ public static final int PRECEDING_SIBLING = 294;
+ public static final int SELF = 295;
+ public static final int DIV = 296;
+ public static final int MOD = 297;
+ public static final int OR = 298;
+ public static final int AND = 299;
+ public static final int COMMENT = 300;
+ public static final int PROCESSING_INSTRUCTION = 301;
+ public static final int TEXT = 302;
+ public static final int NODE = 303;
+ public static final int UNARY = 304;
+ public static final int yyErrorCode = 256;
+
+ /** thrown for irrecoverable syntax errors and stack overflow.
+ */
+ public static class yyException extends java.lang.Exception {
+ public yyException (String message) {
+ super(message);
+ }
+ }
+
+ /** must be implemented by a scanner object to supply input to the parser.
+ */
+ public interface yyInput {
+ /** move on to next token.
+ @return false if positioned beyond tokens.
+ @throws IOException on input error.
+ */
+ boolean advance () throws java.io.IOException;
+ /** classifies current token.
+ Should not be called if advance() returned false.
+ @return current %token or single character.
+ */
+ int token ();
+ /** associated with current token.
+ Should not be called if advance() returned false.
+ @return value for token().
+ */
+ Object value ();
+ }
+
+ /** simplified error message.
+ @see <a href="#yyerror(java.lang.String, java.lang.String[])">yyerror</a>
+ */
+ public void yyerror (String message) {
+ yyerror(message, null);
+ }
+
+ /** (syntax) error message.
+ Can be overwritten to control message format.
+ @param message text to be displayed.
+ @param expected vector of acceptable tokens, if available.
+ */
+ public void yyerror (String message, String[] expected) {
+ if (expected != null && expected.length > 0) {
+ System.err.print(message+", expecting");
+ for (int n = 0; n < expected.length; ++ n)
+ System.err.print(" "+expected[n]);
+ System.err.println();
+ } else
+ System.err.println(message);
+ }
+
+ /** debugging support, requires the package jay.yydebug.
+ Set to null to suppress debugging messages.
+ */
+//t protected jay.yydebug.yyDebug yydebug;
+
+ protected static final int yyFinal = 30;
+
+ /** index-checked interface to yyName[].
+ @param token single character or %token value.
+ @return token name or [illegal] or [unknown].
+ */
+//t public static final String yyname (int token) {
+//t if (token < 0 || token > YyNameClass.yyName.length) return "[illegal]";
+//t String name;
+//t if ((name = YyNameClass.yyName[token]) != null) return name;
+//t return "[unknown]";
+//t }
+
+ /** computes list of expected tokens on error by tracing the tables.
+ @param state for which to compute the list.
+ @return list of token names.
+ */
+ protected String[] yyExpecting (int state) {
+ int token, n, len = 0;
+ boolean[] ok = new boolean[YyNameClass.yyName.length];
+
+ if ((n = YySindexClass.yySindex[state]) != 0)
+ for (token = n < 0 ? -n : 0;
+ token < YyNameClass.yyName.length && n+token < YyTableClass.yyTable.length; ++ token)
+ if (YyCheckClass.yyCheck[n+token] == token && !ok[token] && YyNameClass.yyName[token] != null) {
+ ++ len;
+ ok[token] = true;
+ }
+ if ((n = YyRindexClass.yyRindex[state]) != 0)
+ for (token = n < 0 ? -n : 0;
+ token < YyNameClass.yyName.length && n+token < YyTableClass.yyTable.length; ++ token)
+ if (YyCheckClass.yyCheck[n+token] == token && !ok[token] && YyNameClass.yyName[token] != null) {
+ ++ len;
+ ok[token] = true;
+ }
+
+ String result[] = new String[len];
+ for (n = token = 0; n < len; ++ token)
+ if (ok[token]) result[n++] = YyNameClass.yyName[token];
+ return result;
+ }
+
+ /** the generated parser, with debugging messages.
+ Maintains a state and a value stack, currently with fixed maximum size.
+ @param yyLex scanner.
+ @param yydebug debug message writer implementing yyDebug, or null.
+ @return result of the last reduction, if any.
+ @throws yyException on irrecoverable parse error.
+ */
+ public Object yyparse (yyInput yyLex, Object yydebug)
+ throws java.io.IOException, yyException {
+//t this.yydebug = (jay.yydebug.yyDebug)yydebug;
+ return yyparse(yyLex);
+ }
+
+ /** initial size and increment of the state/value stack [default 256].
+ This is not final so that it can be overwritten outside of invocations
+ of yyparse().
+ */
+ protected int yyMax;
+
+ /** executed at the beginning of a reduce action.
+ Used as $$ = yyDefault($1), prior to the user-specified action, if any.
+ Can be overwritten to provide deep copy, etc.
+ @param first value for $1, or null.
+ @return first.
+ */
+ protected Object yyDefault (Object first) {
+ return first;
+ }
+
+ /** the generated parser.
+ Maintains a state and a value stack, currently with fixed maximum size.
+ @param yyLex scanner.
+ @return result of the last reduction, if any.
+ @throws yyException on irrecoverable parse error.
+ */
+ public Object yyparse (yyInput yyLex)
+ throws java.io.IOException, yyException {
+ if (yyMax <= 0) yyMax = 256; // initial size
+ int yyState = 0, yyStates[] = new int[yyMax]; // state stack
+ Object yyVal = null, yyVals[] = new Object[yyMax]; // value stack
+ int yyToken = -1; // current input
+ int yyErrorFlag = 0; // #tks to shift
+
+ yyLoop: for (int yyTop = 0;; ++ yyTop) {
+ if (yyTop >= yyStates.length) { // dynamically increase
+ int[] i = new int[yyStates.length+yyMax];
+ System.arraycopy(yyStates, 0, i, 0, yyStates.length);
+ yyStates = i;
+ Object[] o = new Object[yyVals.length+yyMax];
+ System.arraycopy(yyVals, 0, o, 0, yyVals.length);
+ yyVals = o;
+ }
+ yyStates[yyTop] = yyState;
+ yyVals[yyTop] = yyVal;
+//t if (yydebug != null) yydebug.push(yyState, yyVal);
+
+ yyDiscarded: for (;;) { // discarding a token does not change stack
+ int yyN;
+ if ((yyN = YyDefRedClass.yyDefRed[yyState]) == 0) { // else [default] reduce (yyN)
+ if (yyToken < 0) {
+ yyToken = yyLex.advance() ? yyLex.token() : 0;
+//t if (yydebug != null)
+//t yydebug.lex(yyState, yyToken, yyname(yyToken), yyLex.value());
+ }
+ if ((yyN = YySindexClass.yySindex[yyState]) != 0 && (yyN += yyToken) >= 0
+ && yyN < YyTableClass.yyTable.length && YyCheckClass.yyCheck[yyN] == yyToken) {
+//t if (yydebug != null)
+//t yydebug.shift(yyState, YyTableClass.yyTable[yyN], yyErrorFlag-1);
+ yyState = YyTableClass.yyTable[yyN]; // shift to yyN
+ yyVal = yyLex.value();
+ yyToken = -1;
+ if (yyErrorFlag > 0) -- yyErrorFlag;
+ continue yyLoop;
+ }
+ if ((yyN = YyRindexClass.yyRindex[yyState]) != 0 && (yyN += yyToken) >= 0
+ && yyN < YyTableClass.yyTable.length && YyCheckClass.yyCheck[yyN] == yyToken)
+ yyN = YyTableClass.yyTable[yyN]; // reduce (yyN)
+ else
+ switch (yyErrorFlag) {
+
+ case 0:
+ yyerror("syntax error", yyExpecting(yyState));
+//t if (yydebug != null) yydebug.error("syntax error");
+
+ case 1: case 2:
+ yyErrorFlag = 3;
+ do {
+ if ((yyN = YySindexClass.yySindex[yyStates[yyTop]]) != 0
+ && (yyN += yyErrorCode) >= 0 && yyN < YyTableClass.yyTable.length
+ && YyCheckClass.yyCheck[yyN] == yyErrorCode) {
+//t if (yydebug != null)
+//t yydebug.shift(yyStates[yyTop], YyTableClass.yyTable[yyN], 3);
+ yyState = YyTableClass.yyTable[yyN];
+ yyVal = yyLex.value();
+ continue yyLoop;
+ }
+//t if (yydebug != null) yydebug.pop(yyStates[yyTop]);
+ } while (-- yyTop >= 0);
+//t if (yydebug != null) yydebug.reject();
+ throw new yyException("irrecoverable syntax error");
+
+ case 3:
+ if (yyToken == 0) {
+//t if (yydebug != null) yydebug.reject();
+ throw new yyException("irrecoverable syntax error at end-of-file");
+ }
+//t if (yydebug != null)
+//t yydebug.discard(yyState, yyToken, yyname(yyToken),
+//t yyLex.value());
+ yyToken = -1;
+ continue yyDiscarded; // leave stack alone
+ }
+ }
+ int yyV = yyTop + 1-YyLenClass.yyLen[yyN];
+//t if (yydebug != null)
+//t yydebug.reduce(yyState, yyStates[yyV-1], yyN, YyRuleClass.yyRule[yyN], YyLenClass.yyLen[yyN]);
+ yyVal = yyDefault(yyV > yyTop ? null : yyVals[yyV]);
+ switch (yyN) {
+case 4:
+ // line 277 "XPathParser.y"
+ {
+ yyVal = new Root();
+ }
+ break;
+case 5:
+ // line 281 "XPathParser.y"
+ {
+ Steps steps;
+ if (yyVals[0+yyTop] instanceof Steps)
+ {
+ steps = (Steps) yyVals[0+yyTop];
+ }
+ else
+ {
+ steps = new Steps();
+ steps.path.addFirst((Expr) yyVals[0+yyTop]);
+ }
+ steps.path.addFirst(new Root());
+ yyVal = steps;
+ /*$$ = new Step(new Root(), (Path) $2);*/
+ }
+ break;
+case 6:
+ // line 297 "XPathParser.y"
+ {
+ Test nt = new NodeTypeTest((short) 0);
+ Selector s = new Selector(Selector.DESCENDANT_OR_SELF,
+ Collections.singletonList (nt));
+ Steps steps;
+ if (yyVals[0+yyTop] instanceof Steps)
+ {
+ steps = (Steps) yyVals[0+yyTop];
+ }
+ else
+ {
+ steps = new Steps();
+ steps.path.addFirst((Expr) yyVals[0+yyTop]);
+ }
+ steps.path.addFirst(s);
+ steps.path.addFirst(new Root());
+ yyVal = steps;
+ /*Step step = new Step(s, (Path) $2);*/
+ /*$$ = new Step(new Root(), step);*/
+ }
+ break;
+case 8:
+ // line 322 "XPathParser.y"
+ {
+ Steps steps;
+ if (yyVals[-2+yyTop] instanceof Steps)
+ {
+ steps = (Steps) yyVals[-2+yyTop];
+ }
+ else
+ {
+ steps = new Steps();
+ steps.path.addFirst((Expr) yyVals[-2+yyTop]);
+ }
+ steps.path.addLast((Expr) yyVals[0+yyTop]);
+ yyVal = steps;
+ /*$$ = new Step((Expr) $1, (Path) $3);*/
+ }
+ break;
+case 9:
+ // line 338 "XPathParser.y"
+ {
+ Test nt = new NodeTypeTest((short) 0);
+ Selector s = new Selector(Selector.DESCENDANT_OR_SELF,
+ Collections.singletonList (nt));
+ Steps steps;
+ if (yyVals[-2+yyTop] instanceof Steps)
+ {
+ steps = (Steps) yyVals[-2+yyTop];
+ }
+ else
+ {
+ steps = new Steps();
+ steps.path.addFirst((Expr) yyVals[-2+yyTop]);
+ }
+ steps.path.addLast(s);
+ steps.path.addLast((Expr) yyVals[0+yyTop]);
+ yyVal = steps;
+ /*Step step = new Step(s, (Path) $3);*/
+ /*$$ = new Step((Expr) $1, step);*/
+ }
+ break;
+case 10:
+ // line 362 "XPathParser.y"
+ {
+ @SuppressWarnings("unchecked") List<Test> tests = (List<Test>) yyVals[0+yyTop];
+ yyVal = new Selector (Selector.CHILD, tests);
+ }
+ break;
+case 11:
+ // line 366 "XPathParser.y"
+ {
+ /* This is safe as we create this in one of the other cases */
+ @SuppressWarnings("unchecked") List<Test> tests = (List<Test>) yyVals[0+yyTop];
+ yyVal = new Selector (Selector.ATTRIBUTE, tests);
+ }
+ break;
+case 12:
+ // line 370 "XPathParser.y"
+ {
+ /* This is safe as we create this in one of the other cases */
+ @SuppressWarnings("unchecked") List<Test> tests = (List<Test>) yyVals[0+yyTop];
+ yyVal = new Selector (((Integer) yyVals[-2+yyTop]).intValue (), tests);
+ }
+ break;
+case 13:
+ // line 374 "XPathParser.y"
+ {
+ List<Test> emptyList = Collections.emptyList();
+ yyVal = new Selector (Selector.SELF, emptyList);
+ }
+ break;
+case 14:
+ // line 378 "XPathParser.y"
+ {
+ List<Test> emptyList = Collections.emptyList();
+ yyVal = new Selector (Selector.PARENT, emptyList);
+ }
+ break;
+case 15:
+ // line 385 "XPathParser.y"
+ {
+ List<Test> list = new ArrayList<Test>();
+ list.add((Test) yyVals[0+yyTop]);
+ yyVal = list;
+ }
+ break;
+case 16:
+ // line 391 "XPathParser.y"
+ {
+ /* This is safe as we create this in one of the other cases */
+ @SuppressWarnings("unchecked") List<Test> tests = (List<Test>)yyVals[-1+yyTop];
+ tests.add((Test) yyVals[0+yyTop]);
+ yyVal = tests;
+ }
+ break;
+case 17:
+ // line 415 "XPathParser.y"
+ {
+ yyVal = new Integer(Selector.ANCESTOR);
+ }
+ break;
+case 18:
+ // line 419 "XPathParser.y"
+ {
+ yyVal = new Integer(Selector.ANCESTOR_OR_SELF);
+ }
+ break;
+case 19:
+ // line 423 "XPathParser.y"
+ {
+ yyVal = new Integer(Selector.ATTRIBUTE);
+ }
+ break;
+case 20:
+ // line 427 "XPathParser.y"
+ {
+ yyVal = new Integer(Selector.CHILD);
+ }
+ break;
+case 21:
+ // line 431 "XPathParser.y"
+ {
+ yyVal = new Integer(Selector.DESCENDANT);
+ }
+ break;
+case 22:
+ // line 435 "XPathParser.y"
+ {
+ yyVal = new Integer(Selector.DESCENDANT_OR_SELF);
+ }
+ break;
+case 23:
+ // line 439 "XPathParser.y"
+ {
+ yyVal = new Integer(Selector.FOLLOWING);
+ }
+ break;
+case 24:
+ // line 443 "XPathParser.y"
+ {
+ yyVal = new Integer(Selector.FOLLOWING_SIBLING);
+ }
+ break;
+case 25:
+ // line 447 "XPathParser.y"
+ {
+ yyVal = new Integer(Selector.NAMESPACE);
+ }
+ break;
+case 26:
+ // line 451 "XPathParser.y"
+ {
+ yyVal = new Integer(Selector.PARENT);
+ }
+ break;
+case 27:
+ // line 455 "XPathParser.y"
+ {
+ yyVal = new Integer(Selector.PRECEDING);
+ }
+ break;
+case 28:
+ // line 459 "XPathParser.y"
+ {
+ yyVal = new Integer(Selector.PRECEDING_SIBLING);
+ }
+ break;
+case 29:
+ // line 463 "XPathParser.y"
+ {
+ yyVal = new Integer(Selector.SELF);
+ }
+ break;
+case 31:
+ // line 472 "XPathParser.y"
+ {
+ yyVal = new NodeTypeTest(Node.PROCESSING_INSTRUCTION_NODE, (String) yyVals[-1+yyTop]);
+ }
+ break;
+case 32:
+ // line 477 "XPathParser.y"
+ {
+ yyVal = new NodeTypeTest(((Short) yyVals[-1+yyTop]).shortValue());
+ }
+ break;
+case 33:
+ // line 484 "XPathParser.y"
+ {
+ yyVal = new Predicate((Expr) yyVals[-1+yyTop]);
+ }
+ break;
+case 35:
+ // line 492 "XPathParser.y"
+ {
+ yyVal = new ParenthesizedExpr((Expr) yyVals[-1+yyTop]);
+ }
+ break;
+case 36:
+ // line 496 "XPathParser.y"
+ {
+ yyVal = new Constant(yyVals[0+yyTop]);
+ }
+ break;
+case 37:
+ // line 500 "XPathParser.y"
+ {
+ yyVal = new Constant(yyVals[0+yyTop]);
+ }
+ break;
+case 39:
+ // line 508 "XPathParser.y"
+ {
+ List<Expr> emptyList = Collections.emptyList();
+ yyVal = lookupFunction((String) yyVals[-2+yyTop], emptyList);
+ }
+ break;
+case 40:
+ // line 512 "XPathParser.y"
+ {
+ /* This is safe as we create this below */
+ @SuppressWarnings("unchecked") List<Expr> exprs = (List<Expr>) yyVals[-1+yyTop];
+ yyVal = lookupFunction((String) yyVals[-3+yyTop], exprs);
+ }
+ break;
+case 41:
+ // line 519 "XPathParser.y"
+ {
+ List<Expr> list = new ArrayList<Expr>();
+ list.add((Expr) yyVals[0+yyTop]);
+ yyVal = list;
+ }
+ break;
+case 42:
+ // line 525 "XPathParser.y"
+ {
+ /* This is safe as we create this above */
+ @SuppressWarnings("unchecked") List<Expr> list = (List<Expr>) yyVals[0+yyTop];
+ list.add(0, (Expr) yyVals[-2+yyTop]);
+ yyVal = list;
+ }
+ break;
+case 44:
+ // line 535 "XPathParser.y"
+ {
+ yyVal = new UnionExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop]);
+ }
+ break;
+case 47:
+ // line 544 "XPathParser.y"
+ {
+ Steps steps;
+ if (yyVals[0+yyTop] instanceof Steps)
+ {
+ steps = (Steps) yyVals[0+yyTop];
+ }
+ else
+ {
+ steps = new Steps();
+ steps.path.addFirst((Expr) yyVals[0+yyTop]);
+ }
+ steps.path.addFirst((Expr) yyVals[-2+yyTop]);
+ yyVal = steps;
+ /*$$ = new Step ((Expr) $1, (Path) $3);*/
+ }
+ break;
+case 48:
+ // line 560 "XPathParser.y"
+ {
+ Test nt = new NodeTypeTest((short) 0);
+ Selector s = new Selector(Selector.DESCENDANT_OR_SELF,
+ Collections.singletonList(nt));
+ Steps steps;
+ if (yyVals[0+yyTop] instanceof Steps)
+ {
+ steps = (Steps) yyVals[0+yyTop];
+ }
+ else
+ {
+ steps = new Steps();
+ steps.path.addFirst((Expr) yyVals[0+yyTop]);
+ }
+ steps.path.addFirst(s);
+ steps.path.addFirst((Expr) yyVals[-2+yyTop]);
+ yyVal = steps;
+ /*Step step = new Step (s, (Path) $3);*/
+ /*$$ = new Step ((Expr) $1, step);*/
+ }
+ break;
+case 50:
+ // line 585 "XPathParser.y"
+ {
+ Predicate filter = (Predicate) yyVals[0+yyTop];
+ Selector s = new Selector(Selector.SELF,
+ Collections.singletonList(filter));
+ Steps steps;
+ if (yyVals[-1+yyTop] instanceof Steps)
+ {
+ steps = (Steps) yyVals[-1+yyTop];
+ }
+ else
+ {
+ steps = new Steps();
+ steps.path.addFirst((Expr) yyVals[-1+yyTop]);
+ }
+ steps.path.addLast(s);
+ yyVal = steps;
+ /*$$ = new Step ((Expr) $1, s);*/
+ }
+ break;
+case 52:
+ // line 608 "XPathParser.y"
+ {
+ yyVal = new OrExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop]);
+ }
+ break;
+case 54:
+ // line 616 "XPathParser.y"
+ {
+ yyVal = new AndExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop]);
+ }
+ break;
+case 56:
+ // line 624 "XPathParser.y"
+ {
+ yyVal = new EqualityExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], false);
+ }
+ break;
+case 57:
+ // line 628 "XPathParser.y"
+ {
+ yyVal = new EqualityExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], true);
+ }
+ break;
+case 59:
+ // line 636 "XPathParser.y"
+ {
+ yyVal = new RelationalExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], true, false);
+ }
+ break;
+case 60:
+ // line 640 "XPathParser.y"
+ {
+ yyVal = new RelationalExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], false, false);
+ }
+ break;
+case 61:
+ // line 644 "XPathParser.y"
+ {
+ yyVal = new RelationalExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], true, true);
+ }
+ break;
+case 62:
+ // line 648 "XPathParser.y"
+ {
+ yyVal = new RelationalExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], false, true);
+ }
+ break;
+case 64:
+ // line 656 "XPathParser.y"
+ {
+ yyVal = new ArithmeticExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], ArithmeticExpr.ADD);
+ }
+ break;
+case 65:
+ // line 660 "XPathParser.y"
+ {
+ yyVal = new ArithmeticExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], ArithmeticExpr.SUBTRACT);
+ }
+ break;
+case 67:
+ // line 668 "XPathParser.y"
+ {
+ yyVal = new ArithmeticExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], ArithmeticExpr.MULTIPLY);
+ }
+ break;
+case 68:
+ // line 672 "XPathParser.y"
+ {
+ yyVal = new ArithmeticExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], ArithmeticExpr.DIVIDE);
+ }
+ break;
+case 69:
+ // line 676 "XPathParser.y"
+ {
+ yyVal = new ArithmeticExpr((Expr) yyVals[-2+yyTop], (Expr) yyVals[0+yyTop], ArithmeticExpr.MODULO);
+ }
+ break;
+case 71:
+ // line 684 "XPathParser.y"
+ {
+ yyVal = new NegativeExpr((Expr) yyVals[0+yyTop]);
+ }
+ break;
+case 72:
+ // line 691 "XPathParser.y"
+ {
+ yyVal = new Double((String) yyVals[0+yyTop] + ".0");
+ }
+ break;
+case 73:
+ // line 695 "XPathParser.y"
+ {
+ yyVal = new Double((String) yyVals[-1+yyTop] + ".0");
+ }
+ break;
+case 74:
+ // line 699 "XPathParser.y"
+ {
+ yyVal = new Double((String) yyVals[-2+yyTop] + "." + (String) yyVals[0+yyTop]);
+ }
+ break;
+case 75:
+ // line 703 "XPathParser.y"
+ {
+ yyVal = new Double("0." + (String) yyVals[0+yyTop]);
+ }
+ break;
+case 77:
+ // line 732 "XPathParser.y"
+ {
+ String name = (String) yyVals[0+yyTop];
+ yyVal = new VariableReference(variableResolver, getQName(name));
+ }
+ break;
+case 78:
+ // line 740 "XPathParser.y"
+ {
+ yyVal = new NameTest(null, true, true);
+ }
+ break;
+case 79:
+ // line 744 "XPathParser.y"
+ {
+ QName qName = getQName((String) yyVals[-2+yyTop]);
+ yyVal = new NameTest(qName, true, false);
+ }
+ break;
+case 80:
+ // line 749 "XPathParser.y"
+ {
+ QName qName = getQName((String) yyVals[0+yyTop]);
+ yyVal = new NameTest(qName, false, false);
+ }
+ break;
+case 82:
+ // line 758 "XPathParser.y"
+ {
+ yyVal = (String) yyVals[-2+yyTop] + ':' + (String) yyVals[0+yyTop];
+ }
+ break;
+case 83:
+ // line 765 "XPathParser.y"
+ {
+ yyVal = new Short(Node.COMMENT_NODE);
+ }
+ break;
+case 84:
+ // line 769 "XPathParser.y"
+ {
+ yyVal = new Short(Node.TEXT_NODE);
+ }
+ break;
+case 85:
+ // line 773 "XPathParser.y"
+ {
+ yyVal = new Short(Node.PROCESSING_INSTRUCTION_NODE);
+ }
+ break;
+case 86:
+ // line 777 "XPathParser.y"
+ {
+ yyVal = new Short((short) 0);
+ }
+ break;
+ // line 988 "-"
+ }
+ yyTop -= YyLenClass.yyLen[yyN];
+ yyState = yyStates[yyTop];
+ int yyM = YyLhsClass.yyLhs[yyN];
+ if (yyState == 0 && yyM == 0) {
+//t if (yydebug != null) yydebug.shift(0, yyFinal);
+ yyState = yyFinal;
+ if (yyToken < 0) {
+ yyToken = yyLex.advance() ? yyLex.token() : 0;
+//t if (yydebug != null)
+//t yydebug.lex(yyState, yyToken,yyname(yyToken), yyLex.value());
+ }
+ if (yyToken == 0) {
+//t if (yydebug != null) yydebug.accept(yyVal);
+ return yyVal;
+ }
+ continue yyLoop;
+ }
+ if ((yyN = YyGindexClass.yyGindex[yyM]) != 0 && (yyN += yyState) >= 0
+ && yyN < YyTableClass.yyTable.length && YyCheckClass.yyCheck[yyN] == yyState)
+ yyState = YyTableClass.yyTable[yyN];
+ else
+ yyState = YyDgotoClass.yyDgoto[yyM];
+//t if (yydebug != null) yydebug.shift(yyStates[yyTop], yyState);
+ continue yyLoop;
+ }
+ }
+ }
+
+ protected static final class YyLhsClass {
+
+ public static final short yyLhs [] = { -1,
+ 0, 2, 2, 4, 4, 4, 3, 3, 3, 5,
+ 5, 5, 5, 5, 6, 6, 7, 7, 7, 7,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 8,
+ 8, 8, 9, 12, 12, 12, 12, 12, 15, 15,
+ 17, 17, 18, 18, 19, 19, 19, 19, 20, 20,
+ 1, 1, 21, 21, 22, 22, 22, 23, 23, 23,
+ 23, 23, 24, 24, 24, 25, 25, 25, 25, 26,
+ 26, 14, 14, 14, 14, 16, 13, 10, 10, 10,
+ 27, 27, 11, 11, 11, 11,
+ };
+ } /* End of class YyLhsClass */
+
+ protected static final class YyLenClass {
+
+ public static final short yyLen [] = { 2,
+ 1, 1, 1, 1, 2, 2, 1, 3, 3, 1,
+ 2, 3, 1, 1, 1, 2, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 3, 2, 3, 1, 3, 1, 1, 1, 3, 4,
+ 1, 3, 1, 3, 1, 1, 3, 3, 1, 2,
+ 1, 3, 1, 3, 1, 3, 3, 1, 3, 3,
+ 3, 3, 1, 3, 3, 1, 3, 3, 3, 1,
+ 2, 1, 2, 3, 2, 1, 2, 1, 3, 1,
+ 1, 3, 1, 1, 1, 1,
+ };
+ } /* End class YyLenClass */
+
+ protected static final class YyDefRedClass {
+
+ public static final short yyDefRed [] = { 0,
+ 36, 0, 0, 0, 0, 0, 0, 0, 78, 0,
+ 0, 14, 17, 18, 19, 20, 21, 22, 23, 24,
+ 25, 26, 27, 28, 29, 83, 0, 84, 86, 0,
+ 0, 45, 0, 3, 7, 0, 0, 15, 30, 0,
+ 49, 34, 37, 38, 0, 0, 43, 0, 0, 0,
+ 0, 0, 0, 66, 0, 0, 0, 0, 13, 0,
+ 80, 0, 71, 0, 0, 77, 75, 0, 0, 0,
+ 0, 0, 16, 0, 32, 0, 0, 0, 0, 50,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 74, 82, 79, 35, 0, 31, 0, 8,
+ 9, 0, 0, 39, 0, 0, 44, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 67, 68,
+ 69, 33, 0, 40, 42,
+ };
+ } /* End of class YyDefRedClass */
+
+ protected static final class YyDgotoClass {
+
+ public static final short yyDgoto [] = { 105,
+ 31, 32, 33, 34, 35, 36, 37, 38, 73, 39,
+ 40, 41, 42, 43, 44, 45, 106, 46, 47, 48,
+ 49, 50, 51, 52, 53, 54, 55,
+ };
+ } /* End of class YyDgotoClass */
+
+ protected static final class YySindexClass {
+
+ public static final short yySindex [] = { -97,
+ 0, -271, -267, -97, -239, -239, -97, -199, 0, -236,
+ -222, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, -218, 0, 0, 0,
+ -257, 0, -241, 0, 0, -205, -221, 0, 0, -194,
+ 0, 0, 0, 0, -190, -185, 0, -238, -211, -234,
+ -255, -209, -275, 0, 0, -169, -250, -168, 0, -241,
+ 0, -241, 0, -205, -187, 0, 0, -167, -97, -239,
+ -239, -97, 0, -199, 0, -151, -43, -239, -239, 0,
+ -97, -97, -97, -97, -97, -97, -97, -97, -97, -97,
+ -97, -97, 0, 0, 0, 0, -164, 0, -211, 0,
+ 0, -166, -205, 0, -165, -163, 0, -241, -241, -234,
+ -255, -255, -209, -209, -209, -209, -275, -275, 0, 0,
+ 0, 0, -97, 0, 0,
+ };
+ } /* End of class YySindexClass */
+
+ protected static final class YyRindexClass {
+
+ public static final short yyRindex [] = { 0,
+ 0, 58, 1, 0, 420, 0, 0, 0, 0, 0,
+ 129, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, -161, 0, 0, 0,
+ 40, 0, 237, 0, 0, 168, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 459, 0, 277, 557, 544,
+ 656, 561, 474, 0, 19, 75, 0, 0, 0, 295,
+ 0, 334, 0, 183, 114, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 686, 0,
+ 0, 0, 222, 0, -156, 0, 0, 351, 405, 553,
+ 665, 697, 577, 600, 617, 639, 513, 528, 0, 0,
+ 0, 0, 0, 0, 0,
+ };
+ } /* End of class YyRindexClass */
+
+ protected static final class YyGindexClass {
+
+ public static final short yyGindex [] = { 7,
+ 0, 0, 8, 0, 3, -3, 0, 0, 48, 0,
+ 0, 0, 0, 0, 0, 0, -12, 0, 35, 0,
+ 44, 36, -1, -54, 2, -7, -2,
+ };
+ } /* End of class YyGindexClass */
+
+ protected static final class YyTableClass {
+
+ public static final short yyTable [] = { 63,
+ 81, 90, 61, 61, 64, 61, 30, 66, 94, 56,
+ 58, 57, 60, 62, 84, 85, 86, 87, 80, 3,
+ 91, 92, 65, 72, 70, 71, 95, 78, 79, 113,
+ 114, 115, 116, 82, 83, 67, 8, 9, 68, 1,
+ 69, 59, 12, 13, 14, 15, 16, 17, 18, 19,
+ 20, 21, 22, 23, 24, 25, 72, 72, 74, 3,
+ 26, 27, 28, 29, 88, 89, 75, 61, 61, 76,
+ 103, 61, 100, 101, 73, 61, 61, 9, 102, 77,
+ 111, 112, 119, 120, 121, 108, 109, 81, 93, 117,
+ 118, 97, 96, 98, 94, 80, 122, 124, 123, 85,
+ 26, 27, 28, 29, 41, 1, 2, 3, 4, 104,
+ 125, 107, 99, 81, 5, 6, 110, 0, 0, 0,
+ 0, 0, 0, 7, 8, 9, 10, 0, 13, 11,
+ 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
+ 22, 23, 24, 25, 0, 0, 0, 0, 26, 27,
+ 28, 29, 0, 0, 0, 0, 0, 0, 0, 1,
+ 2, 3, 4, 0, 0, 0, 0, 10, 5, 6,
+ 0, 0, 0, 0, 0, 0, 0, 7, 8, 9,
+ 10, 0, 11, 11, 12, 13, 14, 15, 16, 17,
+ 18, 19, 20, 21, 22, 23, 24, 25, 0, 0,
+ 0, 0, 26, 27, 28, 29, 0, 0, 0, 0,
+ 0, 0, 0, 1, 2, 3, 4, 0, 0, 0,
+ 0, 12, 5, 6, 0, 0, 0, 0, 0, 0,
+ 0, 0, 8, 9, 10, 0, 2, 11, 12, 13,
+ 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
+ 24, 25, 0, 0, 0, 0, 26, 27, 28, 29,
+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
+ 81, 81, 81, 81, 81, 81, 46, 81, 76, 80,
+ 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
+ 80, 80, 80, 80, 5, 80, 81, 81, 81, 81,
+ 1, 0, 1, 1, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 80, 80, 80, 80, 72, 72,
+ 72, 72, 72, 72, 72, 72, 72, 72, 72, 72,
+ 72, 72, 72, 6, 72, 73, 73, 73, 73, 73,
+ 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
+ 47, 73, 0, 72, 72, 72, 72, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 73, 73, 73, 73, 81, 81, 81, 81, 81, 81,
+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 13,
+ 81, 13, 13, 13, 13, 13, 13, 13, 13, 13,
+ 13, 13, 13, 13, 48, 13, 0, 0, 0, 81,
+ 81, 81, 81, 0, 0, 0, 0, 0, 0, 4,
+ 0, 0, 0, 0, 13, 13, 13, 13, 10, 0,
+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+ 10, 10, 10, 11, 10, 11, 11, 11, 11, 11,
+ 11, 11, 11, 11, 11, 11, 11, 11, 70, 11,
+ 0, 0, 0, 10, 10, 10, 10, 0, 0, 0,
+ 0, 0, 0, 63, 0, 0, 0, 0, 11, 11,
+ 11, 11, 12, 0, 12, 12, 12, 12, 12, 12,
+ 12, 12, 12, 12, 12, 12, 12, 2, 12, 2,
+ 2, 2, 0, 0, 2, 2, 2, 2, 2, 2,
+ 2, 2, 64, 2, 0, 0, 0, 12, 12, 12,
+ 12, 0, 0, 0, 0, 0, 0, 65, 0, 0,
+ 0, 0, 2, 2, 2, 2, 0, 46, 0, 46,
+ 46, 46, 0, 53, 46, 46, 46, 46, 46, 46,
+ 46, 46, 54, 46, 0, 5, 51, 5, 5, 5,
+ 58, 0, 5, 5, 5, 5, 5, 5, 5, 5,
+ 0, 5, 46, 46, 46, 46, 60, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 5, 5, 5, 5, 6, 0, 6, 6, 6, 59,
+ 0, 6, 6, 6, 6, 6, 6, 6, 6, 0,
+ 6, 47, 0, 47, 47, 47, 62, 0, 47, 47,
+ 47, 47, 47, 47, 47, 47, 0, 47, 0, 6,
+ 6, 6, 6, 0, 0, 0, 0, 0, 61, 0,
+ 0, 0, 0, 0, 0, 0, 47, 47, 47, 47,
+ 0, 0, 0, 0, 0, 55, 0, 0, 0, 0,
+ 0, 0, 0, 0, 56, 48, 0, 48, 48, 48,
+ 0, 0, 48, 48, 48, 48, 48, 48, 48, 48,
+ 4, 48, 4, 4, 4, 52, 0, 4, 4, 4,
+ 4, 4, 4, 4, 4, 0, 57, 0, 0, 0,
+ 48, 48, 48, 48, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 4, 4, 4, 4, 70,
+ 0, 70, 70, 0, 0, 0, 70, 70, 70, 70,
+ 70, 70, 70, 70, 63, 70, 63, 63, 0, 0,
+ 0, 63, 63, 63, 63, 63, 63, 63, 63, 0,
+ 0, 0, 0, 0, 70, 70, 70, 70, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 63, 63, 64, 0, 64, 64, 0, 0, 0,
+ 64, 64, 64, 64, 64, 64, 64, 64, 65, 0,
+ 65, 65, 0, 0, 0, 65, 65, 65, 65, 65,
+ 65, 65, 65, 0, 53, 0, 53, 53, 0, 0,
+ 64, 64, 0, 54, 0, 54, 54, 51, 0, 51,
+ 51, 58, 0, 58, 58, 65, 65, 0, 58, 58,
+ 58, 58, 58, 58, 0, 0, 0, 60, 0, 60,
+ 60, 53, 53, 0, 60, 60, 60, 60, 60, 60,
+ 54, 54, 0, 0, 51, 0, 0, 0, 58, 58,
+ 59, 0, 59, 59, 0, 0, 0, 59, 59, 59,
+ 59, 59, 59, 0, 60, 60, 0, 62, 0, 62,
+ 62, 0, 0, 0, 62, 62, 62, 62, 62, 62,
+ 0, 0, 0, 0, 0, 0, 0, 59, 59, 61,
+ 0, 61, 61, 0, 0, 0, 61, 61, 61, 61,
+ 61, 61, 0, 0, 62, 62, 55, 0, 55, 55,
+ 0, 0, 0, 55, 55, 56, 0, 56, 56, 0,
+ 0, 0, 56, 56, 0, 0, 61, 61, 0, 0,
+ 0, 0, 0, 0, 0, 0, 52, 0, 52, 52,
+ 0, 0, 0, 55, 55, 0, 0, 57, 0, 57,
+ 57, 0, 56, 56, 57, 57, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 52, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 57, 57,
+ };
+ } /* End of class YyTableClass */
+
+ protected static final class YyCheckClass {
+
+ public static final short yyCheck [] = { 7,
+ 0, 277, 5, 6, 8, 8, 0, 10, 259, 281,
+ 4, 279, 5, 6, 270, 271, 272, 273, 0, 259,
+ 296, 297, 259, 262, 266, 267, 277, 266, 267, 84,
+ 85, 86, 87, 268, 269, 258, 276, 277, 257, 0,
+ 298, 281, 282, 283, 284, 285, 286, 287, 288, 289,
+ 290, 291, 292, 293, 294, 295, 262, 0, 280, 259,
+ 300, 301, 302, 303, 274, 275, 261, 70, 71, 260,
+ 74, 74, 70, 71, 0, 78, 79, 277, 72, 265,
+ 82, 83, 90, 91, 92, 78, 79, 299, 258, 88,
+ 89, 279, 261, 261, 259, 48, 263, 261, 264, 261,
+ 300, 301, 302, 303, 261, 257, 258, 259, 260, 261,
+ 123, 77, 69, 0, 266, 267, 81, -1, -1, -1,
+ -1, -1, -1, 275, 276, 277, 278, -1, 0, 281,
+ 282, 283, 284, 285, 286, 287, 288, 289, 290, 291,
+ 292, 293, 294, 295, -1, -1, -1, -1, 300, 301,
+ 302, 303, -1, -1, -1, -1, -1, -1, -1, 257,
+ 258, 259, 260, -1, -1, -1, -1, 0, 266, 267,
+ -1, -1, -1, -1, -1, -1, -1, 275, 276, 277,
+ 278, -1, 0, 281, 282, 283, 284, 285, 286, 287,
+ 288, 289, 290, 291, 292, 293, 294, 295, -1, -1,
+ -1, -1, 300, 301, 302, 303, -1, -1, -1, -1,
+ -1, -1, -1, 257, 258, 259, 260, -1, -1, -1,
+ -1, 0, 266, 267, -1, -1, -1, -1, -1, -1,
+ -1, -1, 276, 277, 278, -1, 0, 281, 282, 283,
+ 284, 285, 286, 287, 288, 289, 290, 291, 292, 293,
+ 294, 295, -1, -1, -1, -1, 300, 301, 302, 303,
+ 260, 261, 262, 263, 264, 265, 266, 267, 268, 269,
+ 270, 271, 272, 273, 274, 275, 0, 277, 260, 261,
+ 262, 263, 264, 265, 266, 267, 268, 269, 270, 271,
+ 272, 273, 274, 275, 0, 277, 296, 297, 298, 299,
+ 261, -1, 263, 264, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, 296, 297, 298, 299, 261, 262,
+ 263, 264, 265, 266, 267, 268, 269, 270, 271, 272,
+ 273, 274, 275, 0, 277, 261, 262, 263, 264, 265,
+ 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
+ 0, 277, -1, 296, 297, 298, 299, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ 296, 297, 298, 299, 261, 262, 263, 264, 265, 266,
+ 267, 268, 269, 270, 271, 272, 273, 274, 275, 261,
+ 277, 263, 264, 265, 266, 267, 268, 269, 270, 271,
+ 272, 273, 274, 275, 0, 277, -1, -1, -1, 296,
+ 297, 298, 299, -1, -1, -1, -1, -1, -1, 0,
+ -1, -1, -1, -1, 296, 297, 298, 299, 261, -1,
+ 263, 264, 265, 266, 267, 268, 269, 270, 271, 272,
+ 273, 274, 275, 261, 277, 263, 264, 265, 266, 267,
+ 268, 269, 270, 271, 272, 273, 274, 275, 0, 277,
+ -1, -1, -1, 296, 297, 298, 299, -1, -1, -1,
+ -1, -1, -1, 0, -1, -1, -1, -1, 296, 297,
+ 298, 299, 261, -1, 263, 264, 265, 266, 267, 268,
+ 269, 270, 271, 272, 273, 274, 275, 261, 277, 263,
+ 264, 265, -1, -1, 268, 269, 270, 271, 272, 273,
+ 274, 275, 0, 277, -1, -1, -1, 296, 297, 298,
+ 299, -1, -1, -1, -1, -1, -1, 0, -1, -1,
+ -1, -1, 296, 297, 298, 299, -1, 261, -1, 263,
+ 264, 265, -1, 0, 268, 269, 270, 271, 272, 273,
+ 274, 275, 0, 277, -1, 261, 0, 263, 264, 265,
+ 0, -1, 268, 269, 270, 271, 272, 273, 274, 275,
+ -1, 277, 296, 297, 298, 299, 0, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ 296, 297, 298, 299, 261, -1, 263, 264, 265, 0,
+ -1, 268, 269, 270, 271, 272, 273, 274, 275, -1,
+ 277, 261, -1, 263, 264, 265, 0, -1, 268, 269,
+ 270, 271, 272, 273, 274, 275, -1, 277, -1, 296,
+ 297, 298, 299, -1, -1, -1, -1, -1, 0, -1,
+ -1, -1, -1, -1, -1, -1, 296, 297, 298, 299,
+ -1, -1, -1, -1, -1, 0, -1, -1, -1, -1,
+ -1, -1, -1, -1, 0, 261, -1, 263, 264, 265,
+ -1, -1, 268, 269, 270, 271, 272, 273, 274, 275,
+ 261, 277, 263, 264, 265, 0, -1, 268, 269, 270,
+ 271, 272, 273, 274, 275, -1, 0, -1, -1, -1,
+ 296, 297, 298, 299, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, 296, 297, 298, 299, 261,
+ -1, 263, 264, -1, -1, -1, 268, 269, 270, 271,
+ 272, 273, 274, 275, 261, 277, 263, 264, -1, -1,
+ -1, 268, 269, 270, 271, 272, 273, 274, 275, -1,
+ -1, -1, -1, -1, 296, 297, 298, 299, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, 298, 299, 261, -1, 263, 264, -1, -1, -1,
+ 268, 269, 270, 271, 272, 273, 274, 275, 261, -1,
+ 263, 264, -1, -1, -1, 268, 269, 270, 271, 272,
+ 273, 274, 275, -1, 261, -1, 263, 264, -1, -1,
+ 298, 299, -1, 261, -1, 263, 264, 261, -1, 263,
+ 264, 261, -1, 263, 264, 298, 299, -1, 268, 269,
+ 270, 271, 272, 273, -1, -1, -1, 261, -1, 263,
+ 264, 298, 299, -1, 268, 269, 270, 271, 272, 273,
+ 298, 299, -1, -1, 298, -1, -1, -1, 298, 299,
+ 261, -1, 263, 264, -1, -1, -1, 268, 269, 270,
+ 271, 272, 273, -1, 298, 299, -1, 261, -1, 263,
+ 264, -1, -1, -1, 268, 269, 270, 271, 272, 273,
+ -1, -1, -1, -1, -1, -1, -1, 298, 299, 261,
+ -1, 263, 264, -1, -1, -1, 268, 269, 270, 271,
+ 272, 273, -1, -1, 298, 299, 261, -1, 263, 264,
+ -1, -1, -1, 268, 269, 261, -1, 263, 264, -1,
+ -1, -1, 268, 269, -1, -1, 298, 299, -1, -1,
+ -1, -1, -1, -1, -1, -1, 261, -1, 263, 264,
+ -1, -1, -1, 298, 299, -1, -1, 261, -1, 263,
+ 264, -1, 298, 299, 268, 269, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, 298, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, 298, 299,
+ };
+ } /* End of class YyCheckClass */
+
+
+//t protected static final class YyRuleClass {
+
+//t public static final String yyRule [] = {
+//t "$accept : expr",
+//t "expr : or_expr",
+//t "location_path : relative_location_path",
+//t "location_path : absolute_location_path",
+//t "absolute_location_path : SLASH",
+//t "absolute_location_path : SLASH relative_location_path",
+//t "absolute_location_path : DOUBLE_SLASH relative_location_path",
+//t "relative_location_path : step",
+//t "relative_location_path : relative_location_path SLASH step",
+//t "relative_location_path : relative_location_path DOUBLE_SLASH step",
+//t "step : step_node_test",
+//t "step : AT step_node_test",
+//t "step : axis_name DOUBLE_COLON step_node_test",
+//t "step : DOT",
+//t "step : DOUBLE_DOT",
+//t "step_node_test : node_test",
+//t "step_node_test : step_node_test predicate",
+//t "axis_name : ANCESTOR",
+//t "axis_name : ANCESTOR_OR_SELF",
+//t "axis_name : ATTRIBUTE",
+//t "axis_name : CHILD",
+//t "axis_name : DESCENDANT",
+//t "axis_name : DESCENDANT_OR_SELF",
+//t "axis_name : FOLLOWING",
+//t "axis_name : FOLLOWING_SIBLING",
+//t "axis_name : NAMESPACE",
+//t "axis_name : PARENT",
+//t "axis_name : PRECEDING",
+//t "axis_name : PRECEDING_SIBLING",
+//t "axis_name : SELF",
+//t "node_test : name_test",
+//t "node_test : PROCESSING_INSTRUCTION LITERAL RP",
+//t "node_test : node_type RP",
+//t "predicate : LB expr RB",
+//t "primary_expr : variable_reference",
+//t "primary_expr : LP expr RP",
+//t "primary_expr : LITERAL",
+//t "primary_expr : number",
+//t "primary_expr : function_call",
+//t "function_call : function_name LP RP",
+//t "function_call : function_name LP argument_list RP",
+//t "argument_list : expr",
+//t "argument_list : expr COMMA argument_list",
+//t "union_expr : path_expr",
+//t "union_expr : union_expr PIPE path_expr",
+//t "path_expr : location_path",
+//t "path_expr : filter_expr",
+//t "path_expr : filter_expr SLASH relative_location_path",
+//t "path_expr : filter_expr DOUBLE_SLASH relative_location_path",
+//t "filter_expr : primary_expr",
+//t "filter_expr : filter_expr predicate",
+//t "or_expr : and_expr",
+//t "or_expr : or_expr OR and_expr",
+//t "and_expr : equality_expr",
+//t "and_expr : and_expr AND equality_expr",
+//t "equality_expr : relational_expr",
+//t "equality_expr : equality_expr EQ relational_expr",
+//t "equality_expr : equality_expr NE relational_expr",
+//t "relational_expr : additive_expr",
+//t "relational_expr : relational_expr LT additive_expr",
+//t "relational_expr : relational_expr GT additive_expr",
+//t "relational_expr : relational_expr LTE additive_expr",
+//t "relational_expr : relational_expr GTE additive_expr",
+//t "additive_expr : multiplicative_expr",
+//t "additive_expr : additive_expr PLUS multiplicative_expr",
+//t "additive_expr : additive_expr MINUS multiplicative_expr",
+//t "multiplicative_expr : unary_expr",
+//t "multiplicative_expr : multiplicative_expr STAR unary_expr",
+//t "multiplicative_expr : multiplicative_expr DIV unary_expr",
+//t "multiplicative_expr : multiplicative_expr MOD unary_expr",
+//t "unary_expr : union_expr",
+//t "unary_expr : MINUS unary_expr",
+//t "number : DIGITS",
+//t "number : DIGITS DOT",
+//t "number : DIGITS DOT DIGITS",
+//t "number : DOT DIGITS",
+//t "function_name : qname",
+//t "variable_reference : DOLLAR qname",
+//t "name_test : STAR",
+//t "name_test : NAME COLON STAR",
+//t "name_test : qname",
+//t "qname : NAME",
+//t "qname : NAME COLON NAME",
+//t "node_type : COMMENT",
+//t "node_type : TEXT",
+//t "node_type : PROCESSING_INSTRUCTION",
+//t "node_type : NODE",
+//t };
+//t } /* End of class YyRuleClass */
+
+ protected static final class YyNameClass {
+
+ public static final String yyName [] = {
+ "end-of-file",null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,null,null,null,null,null,null,null,
+ null,null,null,null,null,null,null,"LITERAL","DIGITS","NAME","LP",
+ "RP","LB","RB","COMMA","PIPE","SLASH","DOUBLE_SLASH","EQ","NE","GT",
+ "LT","GTE","LTE","PLUS","MINUS","AT","STAR","DOLLAR","COLON",
+ "DOUBLE_COLON","DOT","DOUBLE_DOT","ANCESTOR","ANCESTOR_OR_SELF",
+ "ATTRIBUTE","CHILD","DESCENDANT","DESCENDANT_OR_SELF","FOLLOWING",
+ "FOLLOWING_SIBLING","NAMESPACE","PARENT","PRECEDING",
+ "PRECEDING_SIBLING","SELF","DIV","MOD","OR","AND","COMMENT",
+ "PROCESSING_INSTRUCTION","TEXT","NODE","UNARY",
+ };
+ } /* End of class YyNameClass */
+
+
+ // line 783 "XPathParser.y"
+
+}
+ // line 1463 "-"
diff --git a/libjava/classpath/gnu/xml/xpath/XPathParser.y b/libjava/classpath/gnu/xml/xpath/XPathParser.y
new file mode 100644
index 000000000..a6d3fd130
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/XPathParser.y
@@ -0,0 +1,794 @@
+%{
+/* XPathParser.y - An XPath 1.0 parser.
+ Copyright (C) 2004 The Free Software Foundation
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.xml.xpath;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathFunctionResolver;
+import javax.xml.xpath.XPathVariableResolver;
+import org.w3c.dom.Node;
+
+/**
+ * An XPath 1.0 parser.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class XPathParser
+{
+
+ NamespaceContext namespaceContext;
+ XPathVariableResolver variableResolver;
+ XPathFunctionResolver functionResolver;
+
+ QName getQName(String name)
+ {
+ QName qName = QName.valueOf(name);
+ if (namespaceContext != null)
+ {
+ String prefix = qName.getPrefix();
+ String uri = qName.getNamespaceURI();
+ if (prefix != null && (uri == null || uri.length() == 0))
+ {
+ uri = namespaceContext.getNamespaceURI(prefix);
+ String localName = qName.getLocalPart();
+ qName = new QName(uri, localName, prefix);
+ }
+ }
+ return qName;
+ }
+
+ Expr lookupFunction(String name, List<Expr> args)
+ {
+ int arity = args.size();
+ if ("position".equals(name) && arity == 0)
+ {
+ return new PositionFunction();
+ }
+ else if ("last".equals(name) && arity == 0)
+ {
+ return new LastFunction();
+ }
+ else if ("string".equals(name) && (arity == 1 || arity == 0))
+ {
+ return new StringFunction(args);
+ }
+ else if ("number".equals(name) && (arity == 1 || arity == 0))
+ {
+ return new NumberFunction(args);
+ }
+ else if ("boolean".equals(name) && arity == 1)
+ {
+ return new BooleanFunction(args);
+ }
+ else if ("count".equals(name) && arity == 1)
+ {
+ return new CountFunction(args);
+ }
+ else if ("not".equals(name) && arity == 1)
+ {
+ return new NotFunction(args);
+ }
+ else if ("id".equals(name) && arity == 1)
+ {
+ return new IdFunction(args);
+ }
+ else if ("concat".equals(name) && arity > 1)
+ {
+ return new ConcatFunction(args);
+ }
+ else if ("true".equals(name) && arity == 0)
+ {
+ return new TrueFunction();
+ }
+ else if ("false".equals(name) && arity == 0)
+ {
+ return new FalseFunction();
+ }
+ else if ("name".equals(name) && (arity == 1 || arity == 0))
+ {
+ return new NameFunction(args);
+ }
+ else if ("local-name".equals(name) && (arity == 1 || arity == 0))
+ {
+ return new LocalNameFunction(args);
+ }
+ else if ("namespace-uri".equals(name) && (arity == 1 || arity == 0))
+ {
+ return new NamespaceUriFunction(args);
+ }
+ else if ("starts-with".equals(name) && arity == 2)
+ {
+ return new StartsWithFunction(args);
+ }
+ else if ("contains".equals(name) && arity == 2)
+ {
+ return new ContainsFunction(args);
+ }
+ else if ("string-length".equals(name) && (arity == 1 || arity == 0))
+ {
+ return new StringLengthFunction(args);
+ }
+ else if ("translate".equals(name) && arity == 3)
+ {
+ return new TranslateFunction(args);
+ }
+ else if ("normalize-space".equals(name) && (arity == 1 || arity == 0))
+ {
+ return new NormalizeSpaceFunction(args);
+ }
+ else if ("substring".equals(name) && (arity == 2 || arity == 3))
+ {
+ return new SubstringFunction(args);
+ }
+ else if ("substring-before".equals(name) && arity == 2)
+ {
+ return new SubstringBeforeFunction(args);
+ }
+ else if ("substring-after".equals(name) && arity == 2)
+ {
+ return new SubstringAfterFunction(args);
+ }
+ else if ("lang".equals(name) && arity == 1)
+ {
+ return new LangFunction(args);
+ }
+ else if ("sum".equals(name) && arity == 1)
+ {
+ return new SumFunction(args);
+ }
+ else if ("floor".equals(name) && arity == 1)
+ {
+ return new FloorFunction(args);
+ }
+ else if ("ceiling".equals(name) && arity == 1)
+ {
+ return new CeilingFunction(args);
+ }
+ else if ("round".equals(name) && arity == 1)
+ {
+ return new RoundFunction(args);
+ }
+ else if (functionResolver != null)
+ {
+ QName qName = QName.valueOf(name);
+ Object function = functionResolver.resolveFunction(qName, arity);
+ if (function != null &&
+ function instanceof Function &&
+ function instanceof Expr)
+ {
+ Function f = (Function) function;
+ f.setArguments(args);
+ return (Expr) function;
+ }
+ }
+ return new FunctionCall(functionResolver, name, args);
+ }
+
+%}
+
+%token LITERAL
+%token DIGITS
+%token NAME
+
+%token LP // '('
+%token RP // ')'
+%token LB // '['
+%token RB // ']'
+%token COMMA // ','
+%token PIPE // '|'
+%token SLASH // '/'
+%token DOUBLE_SLASH // '//'
+%token EQ // '='
+%token NE // '!='
+%token GT // '>'
+%token LT // '<'
+%token GTE // '>='
+%token LTE // '<='
+%token PLUS // '+'
+%token MINUS // '-'
+%token AT // '@'
+%token STAR // '*'
+%token DOLLAR // '$'
+%token COLON // ':'
+%token DOUBLE_COLON // '::'
+%token DOT // '.'
+%token DOUBLE_DOT // '..'
+
+%token ANCESTOR
+%token ANCESTOR_OR_SELF
+%token ATTRIBUTE
+%token CHILD
+%token DESCENDANT
+%token DESCENDANT_OR_SELF
+%token FOLLOWING
+%token FOLLOWING_SIBLING
+%token NAMESPACE
+%token PARENT
+%token PRECEDING
+%token PRECEDING_SIBLING
+%token SELF
+%token DIV
+%token MOD
+%token OR
+%token AND
+%token COMMENT
+%token PROCESSING_INSTRUCTION
+%token TEXT
+%token NODE
+
+%right UNARY
+
+%start expr
+
+%%
+
+expr:
+ or_expr
+ ;
+
+location_path:
+ relative_location_path
+ | absolute_location_path
+ ;
+
+absolute_location_path:
+ SLASH
+ {
+ $$ = new Root();
+ }
+ | SLASH relative_location_path
+ {
+ Steps steps;
+ if ($2 instanceof Steps)
+ {
+ steps = (Steps) $2;
+ }
+ else
+ {
+ steps = new Steps();
+ steps.path.addFirst((Expr) $2);
+ }
+ steps.path.addFirst(new Root());
+ $$ = steps;
+ //$$ = new Step(new Root(), (Path) $2);
+ }
+ | DOUBLE_SLASH relative_location_path
+ {
+ Test nt = new NodeTypeTest((short) 0);
+ Selector s = new Selector(Selector.DESCENDANT_OR_SELF,
+ Collections.singletonList (nt));
+ Steps steps;
+ if ($2 instanceof Steps)
+ {
+ steps = (Steps) $2;
+ }
+ else
+ {
+ steps = new Steps();
+ steps.path.addFirst((Expr) $2);
+ }
+ steps.path.addFirst(s);
+ steps.path.addFirst(new Root());
+ $$ = steps;
+ //Step step = new Step(s, (Path) $2);
+ //$$ = new Step(new Root(), step);
+ }
+ ;
+
+relative_location_path:
+ step
+ | relative_location_path SLASH step
+ {
+ Steps steps;
+ if ($1 instanceof Steps)
+ {
+ steps = (Steps) $1;
+ }
+ else
+ {
+ steps = new Steps();
+ steps.path.addFirst((Expr) $1);
+ }
+ steps.path.addLast((Expr) $3);
+ $$ = steps;
+ //$$ = new Step((Expr) $1, (Path) $3);
+ }
+ | relative_location_path DOUBLE_SLASH step
+ {
+ Test nt = new NodeTypeTest((short) 0);
+ Selector s = new Selector(Selector.DESCENDANT_OR_SELF,
+ Collections.singletonList (nt));
+ Steps steps;
+ if ($1 instanceof Steps)
+ {
+ steps = (Steps) $1;
+ }
+ else
+ {
+ steps = new Steps();
+ steps.path.addFirst((Expr) $1);
+ }
+ steps.path.addLast(s);
+ steps.path.addLast((Expr) $3);
+ $$ = steps;
+ //Step step = new Step(s, (Path) $3);
+ //$$ = new Step((Expr) $1, step);
+ }
+ ;
+
+step:
+ step_node_test
+ {
+ @SuppressWarnings("unchecked") List<Test> tests = (List<Test>) $1;
+ $$ = new Selector (Selector.CHILD, tests);
+ }
+ | AT step_node_test
+ {
+ @SuppressWarnings("unchecked") List<Test> tests = (List<Test>) $2;
+ $$ = new Selector (Selector.ATTRIBUTE, tests);
+ }
+ | axis_name DOUBLE_COLON step_node_test
+ {
+ @SuppressWarnings("unchecked") List<Test> tests = (List<Test>) $3;
+ $$ = new Selector (((Integer) $1).intValue (), tests);
+ }
+ | DOT
+ {
+ List<Test> emptyList = Collections.emptyList();
+ $$ = new Selector (Selector.SELF, emptyList);
+ }
+ | DOUBLE_DOT
+ {
+ List<Test> emptyList = Collections.emptyList();
+ $$ = new Selector (Selector.PARENT, emptyList);
+ }
+ ;
+
+step_node_test:
+ node_test
+ {
+ List<Test> list = new ArrayList<Test>();
+ list.add((Test) $1);
+ $$ = list;
+ }
+ | step_node_test predicate
+ {
+ /* This is safe as we create this in one of the other cases */
+ @SuppressWarnings("unchecked") List<Test> tests = (List<Test>)$1;
+ tests.add((Test) $2);
+ $$ = list;
+ }
+ ;
+
+/*predicate_list:
+ predicate
+ {
+ List list = new ArrayList ();
+ list.add ($1);
+ $$ = list;
+ }
+ | predicate predicate_list
+ {
+ List list = (List) $3;
+ list.add (0, $1);
+ $$ = list;
+ }
+ ;*/
+
+axis_name:
+ ANCESTOR
+ {
+ $$ = new Integer(Selector.ANCESTOR);
+ }
+ | ANCESTOR_OR_SELF
+ {
+ $$ = new Integer(Selector.ANCESTOR_OR_SELF);
+ }
+ | ATTRIBUTE
+ {
+ $$ = new Integer(Selector.ATTRIBUTE);
+ }
+ | CHILD
+ {
+ $$ = new Integer(Selector.CHILD);
+ }
+ | DESCENDANT
+ {
+ $$ = new Integer(Selector.DESCENDANT);
+ }
+ | DESCENDANT_OR_SELF
+ {
+ $$ = new Integer(Selector.DESCENDANT_OR_SELF);
+ }
+ | FOLLOWING
+ {
+ $$ = new Integer(Selector.FOLLOWING);
+ }
+ | FOLLOWING_SIBLING
+ {
+ $$ = new Integer(Selector.FOLLOWING_SIBLING);
+ }
+ | NAMESPACE
+ {
+ $$ = new Integer(Selector.NAMESPACE);
+ }
+ | PARENT
+ {
+ $$ = new Integer(Selector.PARENT);
+ }
+ | PRECEDING
+ {
+ $$ = new Integer(Selector.PRECEDING);
+ }
+ | PRECEDING_SIBLING
+ {
+ $$ = new Integer(Selector.PRECEDING_SIBLING);
+ }
+ | SELF
+ {
+ $$ = new Integer(Selector.SELF);
+ }
+ ;
+
+node_test:
+ name_test
+ /*| PROCESSING_INSTRUCTION LP LITERAL RP*/
+ | PROCESSING_INSTRUCTION LITERAL RP
+ {
+ $$ = new NodeTypeTest(Node.PROCESSING_INSTRUCTION_NODE, (String) $2);
+ }
+ /*| node_type LP RP*/
+ | node_type RP
+ {
+ $$ = new NodeTypeTest(((Short) $1).shortValue());
+ }
+ ;
+
+predicate:
+ LB expr RB
+ {
+ $$ = new Predicate((Expr) $2);
+ }
+ ;
+
+primary_expr:
+ variable_reference
+ | LP expr RP
+ {
+ $$ = new ParenthesizedExpr((Expr) $2);
+ }
+ | LITERAL
+ {
+ $$ = new Constant($1);
+ }
+ | number
+ {
+ $$ = new Constant($1);
+ }
+ | function_call
+ ;
+
+function_call:
+ function_name LP RP
+ {
+ List<Expr> emptyList = Collections.emptyList();
+ $$ = lookupFunction((String) $1, emptyList);
+ }
+ | function_name LP argument_list RP
+ {
+ /* This is safe as we create this below */
+ @SuppressWarnings("unchecked") List<Expr> exprs = (List<Expr>) $3;
+ $$ = lookupFunction((String) $1, (List) exprs);
+ }
+ ;
+
+argument_list:
+ expr
+ {
+ List<Expr> list = new ArrayList<Expr>();
+ list.add((Expr) $1);
+ $$ = list;
+ }
+ | expr COMMA argument_list
+ {
+ /* This is safe as we create this above */
+ @SuppressWarnings("unchecked") List<Expr> list = (List<Expr>) $3;
+ list.add(0, (Expr) $1);
+ $$ = list;
+ }
+ ;
+
+union_expr:
+ path_expr
+ | union_expr PIPE path_expr
+ {
+ $$ = new UnionExpr((Expr) $1, (Expr) $3);
+ }
+ ;
+
+path_expr:
+ location_path
+ | filter_expr
+ | filter_expr SLASH relative_location_path
+ {
+ Steps steps;
+ if ($3 instanceof Steps)
+ {
+ steps = (Steps) $3;
+ }
+ else
+ {
+ steps = new Steps();
+ steps.path.addFirst((Expr) $3);
+ }
+ steps.path.addFirst((Expr) $1);
+ $$ = steps;
+ //$$ = new Step ((Expr) $1, (Path) $3);
+ }
+ | filter_expr DOUBLE_SLASH relative_location_path
+ {
+ Test nt = new NodeTypeTest((short) 0);
+ Selector s = new Selector(Selector.DESCENDANT_OR_SELF,
+ Collections.singletonList(nt));
+ Steps steps;
+ if ($3 instanceof Steps)
+ {
+ steps = (Steps) $3;
+ }
+ else
+ {
+ steps = new Steps();
+ steps.path.addFirst($3);
+ }
+ steps.path.addFirst(s);
+ steps.path.addFirst((Expr) $1);
+ $$ = steps;
+ //Step step = new Step (s, (Path) $3);
+ //$$ = new Step ((Expr) $1, step);
+ }
+ ;
+
+filter_expr:
+ primary_expr
+ | filter_expr predicate
+ {
+ Predicate filter = (Predicate) $2;
+ Selector s = new Selector(Selector.SELF,
+ Collections.singletonList(filter));
+ Steps steps;
+ if ($1 instanceof Steps)
+ {
+ steps = (Steps) $1;
+ }
+ else
+ {
+ steps = new Steps();
+ steps.path.addFirst((Expr) $1);
+ }
+ steps.path.addLast(s);
+ $$ = steps;
+ //$$ = new Step ((Expr) $1, s);
+ }
+ ;
+
+or_expr:
+ and_expr
+ | or_expr OR and_expr
+ {
+ $$ = new OrExpr((Expr) $1, (Expr) $3);
+ }
+ ;
+
+and_expr:
+ equality_expr
+ | and_expr AND equality_expr
+ {
+ $$ = new AndExpr((Expr) $1, (Expr) $3);
+ }
+ ;
+
+equality_expr:
+ relational_expr
+ | equality_expr EQ relational_expr
+ {
+ $$ = new EqualityExpr((Expr) $1, (Expr) $3, false);
+ }
+ | equality_expr NE relational_expr
+ {
+ $$ = new EqualityExpr((Expr) $1, (Expr) $3, true);
+ }
+ ;
+
+relational_expr:
+ additive_expr
+ | relational_expr LT additive_expr
+ {
+ $$ = new RelationalExpr((Expr) $1, (Expr) $3, true, false);
+ }
+ | relational_expr GT additive_expr
+ {
+ $$ = new RelationalExpr((Expr) $1, (Expr) $3, false, false);
+ }
+ | relational_expr LTE additive_expr
+ {
+ $$ = new RelationalExpr((Expr) $1, (Expr) $3, true, true);
+ }
+ | relational_expr GTE additive_expr
+ {
+ $$ = new RelationalExpr((Expr) $1, (Expr) $3, false, true);
+ }
+ ;
+
+additive_expr:
+ multiplicative_expr
+ | additive_expr PLUS multiplicative_expr
+ {
+ $$ = new ArithmeticExpr((Expr) $1, (Expr) $3, ArithmeticExpr.ADD);
+ }
+ | additive_expr MINUS multiplicative_expr
+ {
+ $$ = new ArithmeticExpr((Expr) $1, (Expr) $3, ArithmeticExpr.SUBTRACT);
+ }
+ ;
+
+multiplicative_expr:
+ unary_expr
+ | multiplicative_expr STAR unary_expr
+ {
+ $$ = new ArithmeticExpr((Expr) $1, (Expr) $3, ArithmeticExpr.MULTIPLY);
+ }
+ | multiplicative_expr DIV unary_expr
+ {
+ $$ = new ArithmeticExpr((Expr) $1, (Expr) $3, ArithmeticExpr.DIVIDE);
+ }
+ | multiplicative_expr MOD unary_expr
+ {
+ $$ = new ArithmeticExpr((Expr) $1, (Expr) $3, ArithmeticExpr.MODULO);
+ }
+ ;
+
+unary_expr:
+ union_expr
+ | MINUS unary_expr %prec UNARY
+ {
+ $$ = new NegativeExpr((Expr) $2);
+ }
+ ;
+
+number:
+ DIGITS
+ {
+ $$ = new Double((String) $1 + ".0");
+ }
+ | DIGITS DOT
+ {
+ $$ = new Double((String) $1 + ".0");
+ }
+ | DIGITS DOT DIGITS
+ {
+ $$ = new Double((String) $1 + "." + (String) $3);
+ }
+ | DOT DIGITS
+ {
+ $$ = new Double("0." + (String) $2);
+ }
+ ;
+
+function_name:
+ qname
+/* | node_type
+ {
+ switch (((Short) $1).shortValue ())
+ {
+ case Node.COMMENT_NODE:
+ $$ = "comment";
+ break;
+ case Node.TEXT_NODE:
+ $$ = "text";
+ break;
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ $$ = "processing-instruction";
+ break;
+ default:
+ $$ = "node";
+ break;
+ }
+ }*/
+ ;
+
+variable_reference:
+ DOLLAR qname
+ {
+ String name = (String) $2;
+ $$ = new VariableReference(variableResolver, getQName(name));
+ }
+ ;
+
+name_test:
+ STAR
+ {
+ $$ = new NameTest(null, true, true);
+ }
+ | NAME COLON STAR
+ {
+ QName qName = getQName((String) $1);
+ $$ = new NameTest(qName, true, false);
+ }
+ | qname
+ {
+ QName qName = getQName((String) $1);
+ $$ = new NameTest(qName, false, false);
+ }
+ ;
+
+qname:
+ NAME
+ | NAME COLON NAME
+ {
+ $$ = (String) $1 + ':' + (String) $3;
+ }
+ ;
+
+node_type:
+ COMMENT
+ {
+ $$ = new Short(Node.COMMENT_NODE);
+ }
+ | TEXT
+ {
+ $$ = new Short(Node.TEXT_NODE);
+ }
+ | PROCESSING_INSTRUCTION
+ {
+ $$ = new Short(Node.PROCESSING_INSTRUCTION_NODE);
+ }
+ | NODE
+ {
+ $$ = new Short((short) 0);
+ }
+ ;
+
+%%
+
+}
diff --git a/libjava/classpath/gnu/xml/xpath/XPathTokenizer.java b/libjava/classpath/gnu/xml/xpath/XPathTokenizer.java
new file mode 100644
index 000000000..879931bd5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/xpath/XPathTokenizer.java
@@ -0,0 +1,594 @@
+/* XPathTokenizer.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.xpath;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.Map;
+import java.util.TreeMap;
+
+/*import antlr.Token;
+import antlr.TokenStream;
+import antlr.TokenStreamException;
+import antlr.TokenStreamIOException;*/
+
+/**
+ * XPath 1.0 expression tokenizer.
+ *
+ * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
+ */
+public class XPathTokenizer
+implements XPathParser.yyInput
+//implements TokenStream
+{
+
+ static class XPathToken
+ //extends Token
+ {
+
+ int type;
+ String val;
+
+ XPathToken (int type)
+ {
+ this (type, null);
+ }
+
+ XPathToken (int type, String val)
+ {
+ //super (type);
+ this.type = type;
+ this.val = val;
+ }
+
+ public String getText ()
+ {
+ return val;
+ }
+
+ public String toString ()
+ {
+ return val;
+ }
+
+ }
+
+ static final Map<String,Integer> keywords = new TreeMap<String,Integer> ();
+ static
+ {
+ keywords.put ("ancestor", new Integer (XPathParser.ANCESTOR));
+ keywords.put ("ancestor-or-self", new Integer (XPathParser.ANCESTOR_OR_SELF));
+ keywords.put ("attribute", new Integer (XPathParser.ATTRIBUTE));
+ keywords.put ("child", new Integer (XPathParser.CHILD));
+ keywords.put ("descendant", new Integer (XPathParser.DESCENDANT));
+ keywords.put ("descendant-or-self", new Integer (XPathParser.DESCENDANT_OR_SELF));
+ keywords.put ("following", new Integer (XPathParser.FOLLOWING));
+ keywords.put ("following-sibling", new Integer (XPathParser.FOLLOWING_SIBLING));
+ keywords.put ("namespace", new Integer (XPathParser.NAMESPACE));
+ keywords.put ("parent", new Integer (XPathParser.PARENT));
+ keywords.put ("preceding", new Integer (XPathParser.PRECEDING));
+ keywords.put ("preceding-sibling", new Integer (XPathParser.PRECEDING_SIBLING));
+ keywords.put ("self", new Integer (XPathParser.SELF));
+ keywords.put ("div", new Integer (XPathParser.DIV));
+ keywords.put ("mod", new Integer (XPathParser.MOD));
+ keywords.put ("or", new Integer (XPathParser.OR));
+ keywords.put ("and", new Integer (XPathParser.AND));
+ keywords.put ("comment", new Integer (XPathParser.COMMENT));
+ keywords.put ("processing-instruction", new Integer (XPathParser.PROCESSING_INSTRUCTION));
+ keywords.put ("text", new Integer (XPathParser.TEXT));
+ keywords.put ("node", new Integer (XPathParser.NODE));
+ }
+
+ Reader in;
+ XPathToken token;
+ XPathToken lastToken;
+
+ public XPathTokenizer (String expr)
+ {
+ this (new StringReader (expr));
+ }
+
+ XPathTokenizer (Reader in)
+ {
+ this.in = in.markSupported () ? in : new BufferedReader (in);
+ }
+
+ /* Begin ANTLR specific *
+
+ public Token nextToken ()
+ throws TokenStreamException
+ {
+ try
+ {
+ if (!advance ())
+ {
+ throw new TokenStreamException ("eof");
+ }
+ token ();
+ return token;
+ }
+ catch (IOException e)
+ {
+ throw new TokenStreamIOException (e);
+ }
+ }
+
+ * End ANTLR specific */
+
+ public boolean advance ()
+ throws IOException
+ {
+ lastToken = token;
+ int c = in.read ();
+ switch (c)
+ {
+ case -1: // eof
+ return false;
+ case 0x20:
+ case 0x09:
+ case 0x0d:
+ case 0x0a: // skip whitespace
+ return advance ();
+ case 0x22: // "
+ case 0x27: // '
+ token = consume_literal (c);
+ break;
+ case 0x28: // (
+ token = new XPathToken (XPathParser.LP);
+ break;
+ case 0x29: // )
+ token = new XPathToken (XPathParser.RP);
+ break;
+ case 0x5b: // [
+ token = new XPathToken (XPathParser.LB);
+ break;
+ case 0x5d: // ]
+ token = new XPathToken (XPathParser.RB);
+ break;
+ case 0x2c: // ,
+ token = new XPathToken (XPathParser.COMMA);
+ break;
+ case 0x7c: // |
+ token = new XPathToken (XPathParser.PIPE);
+ break;
+ case 0x2f: // /
+ in.mark (1);
+ int d1 = in.read ();
+ if (d1 == 0x2f)
+ {
+ token = new XPathToken (XPathParser.DOUBLE_SLASH);
+ }
+ else
+ {
+ in.reset ();
+ token = new XPathToken (XPathParser.SLASH);
+ }
+ break;
+ case 0x3d: // =
+ token = new XPathToken (XPathParser.EQ);
+ break;
+ case 0x21: // !
+ in.mark (1);
+ int d2 = in.read ();
+ if (d2 == 0x3d) // =
+ {
+ token = new XPathToken (XPathParser.NE);
+ }
+ else
+ {
+ in.reset ();
+ token = new XPathToken (XPathParser.yyErrorCode);
+ }
+ break;
+ case 0x3e: // >
+ in.mark (1);
+ int d3 = in.read ();
+ if (d3 == 0x3d) // =
+ {
+ token = new XPathToken (XPathParser.GTE);
+ }
+ else
+ {
+ in.reset ();
+ token = new XPathToken (XPathParser.GT);
+ }
+ break;
+ case 0x3c: // <
+ in.mark (1);
+ int d4 = in.read ();
+ if (d4 == 0x3d) // =
+ {
+ token = new XPathToken (XPathParser.LTE);
+ }
+ else
+ {
+ in.reset ();
+ token = new XPathToken (XPathParser.LT);
+ }
+ break;
+ case 0x2b: // +
+ token = new XPathToken (XPathParser.PLUS);
+ break;
+ case 0x2d: // -
+ token = new XPathToken (XPathParser.MINUS);
+ break;
+ case 0x40: // @
+ token = new XPathToken (XPathParser.AT);
+ break;
+ case 0x2a: // *
+ token = new XPathToken (XPathParser.STAR);
+ break;
+ case 0x24: // $
+ token = new XPathToken (XPathParser.DOLLAR);
+ break;
+ case 0x3a: // :
+ in.mark (1);
+ int d5 = in.read ();
+ if (d5 == 0x3a)
+ {
+ token = new XPathToken (XPathParser.DOUBLE_COLON);
+ }
+ else
+ {
+ in.reset ();
+ token = new XPathToken (XPathParser.COLON);
+ }
+ break;
+ case 0x2e: // .
+ in.mark (1);
+ int d6 = in.read ();
+ if (d6 == 0x2e)
+ {
+ token = new XPathToken (XPathParser.DOUBLE_DOT);
+ }
+ else
+ {
+ in.reset ();
+ token = new XPathToken (XPathParser.DOT);
+ }
+ break;
+ default:
+ if (c >= 0x30 && c <= 0x39)
+ {
+ token = consume_digits (c);
+ }
+ else if (c == 0x5f || Character.isLetter ((char) c))
+ {
+ token = consume_name (c);
+ }
+ else
+ {
+ token = new XPathToken (XPathParser.yyErrorCode);
+ }
+ }
+ return true;
+ }
+
+ public int token ()
+ {
+ return token.type;
+ }
+
+ public Object value ()
+ {
+ return token.val;
+ }
+
+ XPathToken consume_literal (int delimiter)
+ throws IOException
+ {
+ CPStringBuilder buf = new CPStringBuilder ();
+ while (true)
+ {
+ int c = in.read ();
+ if (c == -1)
+ {
+ return new XPathToken (XPathParser.yyErrorCode);
+ }
+ else if (c == delimiter)
+ {
+ return new XPathToken (XPathParser.LITERAL, buf.toString ());
+ }
+ else
+ {
+ buf.append ((char) c);
+ }
+ }
+ }
+
+ XPathToken consume_digits (int c)
+ throws IOException
+ {
+ CPStringBuilder buf = new CPStringBuilder ();
+ buf.append ((char) c);
+ while (true)
+ {
+ in.mark (1);
+ c = in.read ();
+ if (c >= 0x30 && c <= 0x39)
+ {
+ buf.append ((char) c);
+ }
+ else
+ {
+ in.reset ();
+ return new XPathToken (XPathParser.DIGITS, buf.toString ());
+ }
+ }
+ }
+
+ XPathToken consume_name (int c)
+ throws IOException
+ {
+ CPStringBuilder buf = new CPStringBuilder ();
+ buf.append ((char) c);
+ while (true)
+ {
+ in.mark (1);
+ c = in.read ();
+ if (isNameChar (c))
+ {
+ buf.append ((char) c);
+ }
+ else
+ {
+ in.reset ();
+ String name = buf.toString ();
+ Integer keyword = (Integer) keywords.get (name);
+ if (keyword == null)
+ {
+ return new XPathToken (XPathParser.NAME, name);
+ }
+ else
+ {
+ int val = keyword.intValue ();
+ switch (val)
+ {
+ case XPathParser.NODE:
+ case XPathParser.COMMENT:
+ case XPathParser.TEXT:
+ case XPathParser.PROCESSING_INSTRUCTION:
+ // Consume subsequent (
+ in.mark (1);
+ do
+ {
+ c = in.read ();
+ }
+ while (c == 0x20 || c == 0x09);
+ if (c != 0x28)
+ {
+ in.reset ();
+ return new XPathToken (XPathParser.NAME, name);
+ }
+ break;
+ case XPathParser.CHILD:
+ case XPathParser.PARENT:
+ case XPathParser.SELF:
+ case XPathParser.DESCENDANT:
+ case XPathParser.ANCESTOR:
+ case XPathParser.DESCENDANT_OR_SELF:
+ case XPathParser.ANCESTOR_OR_SELF:
+ case XPathParser.ATTRIBUTE:
+ case XPathParser.NAMESPACE:
+ case XPathParser.FOLLOWING:
+ case XPathParser.FOLLOWING_SIBLING:
+ case XPathParser.PRECEDING:
+ case XPathParser.PRECEDING_SIBLING:
+ // Check that this is an axis specifier
+ in.mark(1);
+ do
+ {
+ c = in.read();
+ }
+ while (c == 0x20 || c == 0x09);
+ if (c == 0x3a)
+ {
+ c = in.read();
+ if (c == 0x3a)
+ {
+ in.reset();
+ return new XPathToken(val);
+ }
+ }
+ in.reset();
+ return new XPathToken(XPathParser.NAME, name);
+ case XPathParser.DIV:
+ case XPathParser.MOD:
+ // May be a name
+ if (lastToken == null)
+ {
+ return new XPathToken(XPathParser.NAME, name);
+ }
+ switch (lastToken.type)
+ {
+ case XPathParser.LP:
+ case XPathParser.LB:
+ case XPathParser.COMMA:
+ case XPathParser.PIPE:
+ case XPathParser.EQ:
+ case XPathParser.NE:
+ case XPathParser.GT:
+ case XPathParser.LT:
+ case XPathParser.GTE:
+ case XPathParser.LTE:
+ case XPathParser.PLUS:
+ case XPathParser.MINUS:
+ case XPathParser.STAR:
+ case XPathParser.AT:
+ case XPathParser.DOLLAR:
+ case XPathParser.COLON:
+ case XPathParser.DOUBLE_COLON:
+ case XPathParser.DIV:
+ case XPathParser.MOD:
+ case XPathParser.OR:
+ case XPathParser.AND:
+ case XPathParser.SLASH:
+ return new XPathToken(XPathParser.NAME, name);
+ }
+ break;
+ }
+ return new XPathToken (val);
+ }
+ }
+ }
+ }
+
+ boolean isNameChar (int c)
+ {
+ /* Name */
+ return (c == 0x5f
+ || c == 0x2d
+ || c == 0x2e
+ || (c >= 0x30 && c <= 0x39)
+ /* CombiningChar */
+ || (c >= 0x0300 && c <= 0x0345)
+ || (c >= 0x0360 && c <= 0x0361)
+ || (c >= 0x0483 && c <= 0x0486)
+ || (c >= 0x0591 && c <= 0x05A1)
+ || (c >= 0x05A3 && c <= 0x05B9)
+ || (c >= 0x05BB && c <= 0x05BD)
+ || c == 0x05BF
+ || (c >= 0x05C1 && c <= 0x05C2)
+ || c == 0x05C4
+ || (c >= 0x064B && c <= 0x0652)
+ || c == 0x0670
+ || (c >= 0x06D6 && c <= 0x06DC)
+ || (c >= 0x06DD && c <= 0x06DF)
+ || (c >= 0x06E0 && c <= 0x06E4)
+ || (c >= 0x06E7 && c <= 0x06E8)
+ || (c >= 0x06EA && c <= 0x06ED)
+ || (c >= 0x0901 && c <= 0x0903)
+ || c == 0x093C
+ || (c >= 0x093E && c <= 0x094C)
+ || c == 0x094D
+ || (c >= 0x0951 && c <= 0x0954)
+ || (c >= 0x0962 && c <= 0x0963)
+ || (c >= 0x0981 && c <= 0x0983)
+ || c == 0x09BC
+ || c == 0x09BE
+ || c == 0x09BF
+ || (c >= 0x09C0 && c <= 0x09C4)
+ || (c >= 0x09C7 && c <= 0x09C8)
+ || (c >= 0x09CB && c <= 0x09CD)
+ || c == 0x09D7
+ || (c >= 0x09E2 && c <= 0x09E3)
+ || c == 0x0A02
+ || c == 0x0A3C
+ || c == 0x0A3E
+ || c == 0x0A3F
+ || (c >= 0x0A40 && c <= 0x0A42)
+ || (c >= 0x0A47 && c <= 0x0A48)
+ || (c >= 0x0A4B && c <= 0x0A4D)
+ || (c >= 0x0A70 && c <= 0x0A71)
+ || (c >= 0x0A81 && c <= 0x0A83)
+ || c == 0x0ABC
+ || (c >= 0x0ABE && c <= 0x0AC5)
+ || (c >= 0x0AC7 && c <= 0x0AC9)
+ || (c >= 0x0ACB && c <= 0x0ACD)
+ || (c >= 0x0B01 && c <= 0x0B03)
+ || c == 0x0B3C
+ || (c >= 0x0B3E && c <= 0x0B43)
+ || (c >= 0x0B47 && c <= 0x0B48)
+ || (c >= 0x0B4B && c <= 0x0B4D)
+ || (c >= 0x0B56 && c <= 0x0B57)
+ || (c >= 0x0B82 && c <= 0x0B83)
+ || (c >= 0x0BBE && c <= 0x0BC2)
+ || (c >= 0x0BC6 && c <= 0x0BC8)
+ || (c >= 0x0BCA && c <= 0x0BCD)
+ || c == 0x0BD7
+ || (c >= 0x0C01 && c <= 0x0C03)
+ || (c >= 0x0C3E && c <= 0x0C44)
+ || (c >= 0x0C46 && c <= 0x0C48)
+ || (c >= 0x0C4A && c <= 0x0C4D)
+ || (c >= 0x0C55 && c <= 0x0C56)
+ || (c >= 0x0C82 && c <= 0x0C83)
+ || (c >= 0x0CBE && c <= 0x0CC4)
+ || (c >= 0x0CC6 && c <= 0x0CC8)
+ || (c >= 0x0CCA && c <= 0x0CCD)
+ || (c >= 0x0CD5 && c <= 0x0CD6)
+ || (c >= 0x0D02 && c <= 0x0D03)
+ || (c >= 0x0D3E && c <= 0x0D43)
+ || (c >= 0x0D46 && c <= 0x0D48)
+ || (c >= 0x0D4A && c <= 0x0D4D)
+ || c == 0x0D57
+ || c == 0x0E31
+ || (c >= 0x0E34 && c <= 0x0E3A)
+ || (c >= 0x0E47 && c <= 0x0E4E)
+ || c == 0x0EB1
+ || (c >= 0x0EB4 && c <= 0x0EB9)
+ || (c >= 0x0EBB && c <= 0x0EBC)
+ || (c >= 0x0EC8 && c <= 0x0ECD)
+ || (c >= 0x0F18 && c <= 0x0F19)
+ || c == 0x0F35
+ || c == 0x0F37
+ || c == 0x0F39
+ || c == 0x0F3E
+ || c == 0x0F3F
+ || (c >= 0x0F71 && c <= 0x0F84)
+ || (c >= 0x0F86 && c <= 0x0F8B)
+ || (c >= 0x0F90 && c <= 0x0F95)
+ || c == 0x0F97
+ || (c >= 0x0F99 && c <= 0x0FAD)
+ || (c >= 0x0FB1 && c <= 0x0FB7)
+ || c == 0x0FB9
+ || (c >= 0x20D0 && c <= 0x20DC)
+ || c == 0x20E1
+ || (c >= 0x302A && c <= 0x302F)
+ || c == 0x3099
+ || c == 0x309A
+ /* Extender */
+ || c == 0x00B7
+ || c == 0x02D0
+ || c == 0x02D1
+ || c == 0x0387
+ || c == 0x0640
+ || c == 0x0E46
+ || c == 0x0EC6
+ || c == 0x3005
+ || (c >= 0x3031 && c <= 0x3035)
+ || (c >= 0x309D && c <= 0x309E)
+ || (c >= 0x30FC && c <= 0x30FE)
+ /* Name */
+ || Character.isLetter ((char) c));
+ }
+
+}