@MinDoc(copyright="Copyright 2001 - 2005, 2009 A. Weinert", author="Albrecht Weinert", version="V.46", lastModified="11.05.2021", usage="use in XML / HTML applications", purpose="represents a XML document") public class XMLdoc extends Object implements Cloneable, Serializable
(DOM-) Document
as well as properties and
methods for input, manipulation and output. XMLdoc
thereby
facilitates the (partly laborious and restricted) handling of
DOM (org.w3c.dom) documents.(DOM-) Document
(but by JDom) is supported
here — and is still for those historical reasons. In between this
feature seems a bit questionable in the light of XML itself being a (the)
serialisation format.Document
encapsulated in
an object of this class is of the more common (parent) type
Node
, as a matter of fact. But it is settable
or available just as Document
,
DocumentFragment
or
Element
.Modifier and Type | Field and Description |
---|---|
protected PrintWriter |
log
Writer (output) for error and warning reports.
|
static String |
NO_CLOSE
Output coding "UTF-8 without closing the output stream".
|
XMLconf |
xmlConf
Parser properties — plus some others.
|
Constructor and Description |
---|
XMLdoc(App app)
Make for an application (App inheritor).
|
XMLdoc(PrintWriter log)
Make with standard settings.
|
Modifier and Type | Method and Description |
---|---|
boolean |
applyTransforms()
Apply the transformation.
|
XMLdoc |
clone()
A copy of this object.
|
static boolean |
doc2xml(Node doc,
OutputStream os,
String encoding)
Output (facsimile) a DOM tree as XML.
|
Node |
getDocument()
The document (contained).
|
ParseErrorHandler |
getErrorHandler()
rrorHandler for parsing..
|
static SAXException |
getLastXml2docError()
Fetch last XML error of
xml2doc() call (if any). |
String |
getPublicId()
The document's name (just for report and documentation).
|
static DocumentBuilder |
getXMLInputer()
Make a simple XML -> DOM input.
|
static Transformer |
getXMLOutputer()
Supply a DOM -> XML - output with no transformation.
|
boolean |
isDocNode()
The node (contained) is a document or a part.
|
static DocumentBuilder |
makeBuilder(boolean valid,
boolean nameSpace,
PrintWriter log)
Make a DOM parser.
|
void |
parse(InputSource is)
Parse from an input source.
|
void |
parse(InputStream is)
Parse from an input stream.
|
void |
parse(String fileOrURL,
String encoding)
Parse from a file or URL.
|
void |
readFrom(InputStream is)
Get (in facsimile) from an input stream.
|
void |
set(PropMap prop)
Set the properties by a PropMap object.
|
void |
setDocument(Node document)
Set the document.
|
void |
setErrorHanlder(ParseErrorHandler errorHandler)
ErrorHandler for parsing.
|
void |
setPublicId(String publicId)
The document's name (just for report and documentation).
|
boolean |
write(OutputStream os)
Write (with transformations) to an output stream..
|
boolean |
writeTo(OutputStream os)
Write (in facsimile) to an output stream.
|
static Document |
xml2doc(InputStream is)
Input (facsimile) from XML to DOM tree.
|
public final XMLconf xmlConf
XMLdoc
object lie in this associated XMLconf
object. It will be set on construction by XMLdoc(App)
according
to the App
lication's properties
(app.prop
).set(PropMap)
protected final PrintWriter log
public static final String NO_CLOSE
doc2xml(Node, OutputStream, String)
or
writeTo(OutputStream)
to use UTF-8 and not close the output.ObjectOutputStream
.XMLdoc
is serializable
.
public XMLdoc(PrintWriter log)
XMLdoc
object without a document
with log
output and all properties in default setting will be made.log
the parameter log
is taken or, if
null, the normal output (decorated System.out).log
- destination of reportspublic XMLdoc(App app) throws NullPointerException
XMLdoc
object without a document
will be
made.App
inheritor, supplied.app
- the application get output streams and properties fromNullPointerException
- if app is nullset(PropMap)
public void set(PropMap prop) throws IllegalArgumentException
prop
- source of the properties to be usedIllegalArgumentException
- parameter value problemsXMLconf.set(PropMap)
public Node getDocument()
public final boolean isDocNode()
document
is not null and of type
Document
, DocumentFragment
or
Element
(that is it may have children nodes).public final void setDocument(Node document) throws ClassCastException
document
- the new document (DOM tree or null).ClassCastException
- on wrong type; document
is null thenpublic final String getPublicId()
public void setPublicId(String publicId)
publicId
is not empty it is taken as new
publicId
. Otherwise that is set null.public XMLdoc clone()
document
.public void setErrorHanlder(ParseErrorHandler errorHandler)
public ParseErrorHandler getErrorHandler()
public void parse(InputStream is) throws IllegalArgumentException, IOException, SAXException, FactoryConfigurationError, ParserConfigurationException
is
- the XML input stream to be parsedIllegalArgumentException
- (forwarded)IOException
- stream problemSAXException
- (forwarded from parsing)FactoryConfigurationError
- XML problemParserConfigurationException
- XML prolemparse(InputSource)
public void parse(String fileOrURL, String encoding) throws IllegalArgumentException, IOException, SAXException, FactoryConfigurationError, ParserConfigurationException
fileOrURL
- names a file or URL as XML inputencoding
- the input's encoding (character set)IllegalArgumentException
- (forwarded)IOException
- stream problemSAXException
- (forwarded from parsing)FactoryConfigurationError
- XML problemParserConfigurationException
- XML problemparse(InputSource)
,
XMLconf.setProxyPort(CharSequence)
,
XMLconf.setProxyHost(CharSequence)
public void parse(InputSource is) throws IOException, SAXException, IllegalArgumentException, FactoryConfigurationError, ParserConfigurationException
XMLconf.makeDocumentBuilder(ParseErrorHandler)
.is
- the XML inputIllegalArgumentException
- (forwarded)IOException
- stream problemSAXException
- (forwarded from parsing)FactoryConfigurationError
- XML problemParserConfigurationException
- XML problempublic void readFrom(InputStream is) throws IOException, SAXException, ClassNotFoundException
is
.is
- the input stream to read the DOM fromIOException
- stream or file problemsSAXException
- XML problemsClassNotFoundException
- no DOM could be madexml2doc()
public boolean writeTo(OutputStream os) throws IOException
os
.encoding
.os
- the output stream to write the DOM as XML toIOException
- stream or file problemsdoc2xml()
,
write()
public boolean applyTransforms()
XMLconf.clearDefAtts
,
XMLconf.textNormalize
,
MLHelper.clearHTMLdefAtts(Element)
,
Node.normalize()
public boolean write(OutputStream os) throws IOException, TransformerException
os
using all
formating etc. specified by the output properties.applyTransforms()
must be called before this method.os
- the stream to write toIOException
- file or stream problemsTransformerException
- errors while applying the transformationswriteTo()
public static Transformer getXMLOutputer()
setOutputProperty()
, etc. until
transform()
respectively doc2xml()
.
Other methods using this singleton object do this also.public static boolean doc2xml(Node doc, OutputStream os, String encoding) throws IOException
encoding
is equal to
"NO_CLOSE"
; in that case the encoding is
UTF-8.doc
- the DOM tree to output; it's the starting node and it has to
be of type Document, DocumentFragment or Elementos
- the destination, the output; it will be closed afterwards
(except when ... see above)encoding
- the output encoding (default UTF-8); ISO-8859-1 and some
others are possible tooIOException
- on output problems with ospublic static DocumentBuilder makeBuilder(boolean valid, boolean nameSpace, PrintWriter log)
valid
and namespace
are false this will be the most simple
or most direct (1 to 1) translation from XML input to DOM tree (compare
doc2xml()
and getXMLOutputer()
).valid
- true: validatingnameSpace
- true: namespace awarelog
- Writer for error reports (if any); may be nullpublic static DocumentBuilder getXMLInputer()
public static SAXException getLastXml2docError()
xml2doc()
call (if any). xmlInputer
the method will give the right answer
within the synchronized block.public static Document xml2doc(InputStream is) throws IOException
is
will be parsed without any validating,
namespace awareness and the like and the DOM object made in the process
is returned. This method is the complement to
doc2xml()
.is
- the inputIOException
- on input problems with is