下面列出了org.w3c.dom.DOMImplementation#createDocumentType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Document createDOM() throws ParserConfigurationException
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
DOMImplementation domImpl = documentBuilder.getDOMImplementation();
// We needed to reference using the full class name here because we already have
// a class named "DocumentType"
org.w3c.dom.DocumentType doctype = domImpl.createDocumentType(
"plist",
"-//Apple Computer//DTD PLIST 1.0//EN",
"http://www.apple.com/DTDs/PropertyList-1.0.dtd");
return domImpl.createDocument(null, "plist", doctype);
}
/**
* Creates an empty XML document.
*
* @throws ParserConfigurationException
* Error creating the document builder.
*/
public XmlDocument()
throws ParserConfigurationException {
// Check if there is a document type specified.
final DocumentType prototype = getDoctype();
final DocumentBuilder builder = LOCAL_BUILDER.get();
if (prototype == null) {
// If there is none, simply create a new document as usual.
document = builder.newDocument();
if (document != null) {
final Node root = createRootNode();
appendChild(root);
}
} else {
// otherwise create a document with the given doctype as a
// prototype
final DOMImplementation impl = builder.getDOMImplementation();
final DocumentType type =
impl.createDocumentType(prototype.getName(),
prototype.getPublicId(), prototype.getSystemId());
document = impl.createDocument(getDefaultNamespaceURI(),
prototype.getName(), type);
}
}
public static Function<DOMImplementation, DocumentType> parse(String text) {
// We recognize a subset of the XML DOCTYPE grammar. Specifically, we
// do not recognize embedded entity declarations to avoid XXE, or
// annotations.
// As noted above, we do not recognize the intSubset portion.
Matcher m = DOCTYPE_PATTERN.matcher(text);
if (!m.matches()) { return null; }
String name = m.group(1), system2 = dequote(m.group(2)),
pubid = dequote(m.group(3)), system4 = dequote(m.group(4));
final String system = system2 == null ? system4 : system2;
boolean isHtml = isHtml(name, pubid, system);
if (isHtml && name.indexOf(':') < 0) {
name = Strings.lower(name);
}
final String qname = name;
final String publicId = pubid;
final String systemId = system;
return new Function<DOMImplementation, DocumentType>() {
public DocumentType apply(DOMImplementation impl) {
return impl.createDocumentType(qname, publicId, systemId);
}
};
}
@Override
public void setUp() throws Exception {
super.setUp();
DOMImplementationRegistry registry =
DOMImplementationRegistry.newInstance();
DOMImplementation domImpl = registry.getDOMImplementation(
"XML 1.0 Traversal 2.0");
String qname = "html";
String systemId = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";
String publicId = "-//W3C//DTD XHTML 1.0 Transitional//EN";
DocumentType documentType = domImpl.createDocumentType(
qname, publicId, systemId);
Document doc = domImpl.createDocument(null, null, documentType);
mq = new SimpleMessageQueue();
stack = new Html5ElementStack(doc, false, mq);
stack.open(false);
}
/**
* Create a new document with a document type using a custom document builder.
*
* @param aDocBuilder
* the document builder to be used. May not be <code>null</code>.
* @param eVersion
* The XML version to use. If <code>null</code> is passed,
* {@link EXMLVersion#XML_10} will be used.
* @param sQualifiedName
* The qualified name to use.
* @param sPublicId
* The public ID of the document type.
* @param sSystemId
* The system ID of the document type.
* @return The created document. Never <code>null</code>.
*/
@Nonnull
public static Document newDocument (@Nonnull final DocumentBuilder aDocBuilder,
@Nullable final EXMLVersion eVersion,
@Nonnull final String sQualifiedName,
@Nullable final String sPublicId,
@Nullable final String sSystemId)
{
ValueEnforcer.notNull (aDocBuilder, "DocBuilder");
final DOMImplementation aDomImpl = aDocBuilder.getDOMImplementation ();
final DocumentType aDocType = aDomImpl.createDocumentType (sQualifiedName, sPublicId, sSystemId);
final Document aDoc = aDomImpl.createDocument (sSystemId, sQualifiedName, aDocType);
aDoc.setXmlVersion ((eVersion != null ? eVersion : EXMLVersion.XML_10).getVersion ());
return aDoc;
}
public SVGExporter(ExportRectangle bounds, double zoom) {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
DOMImplementation impl = docBuilder.getDOMImplementation();
DocumentType svgDocType = impl.createDocumentType("svg", "-//W3C//DTD SVG 1.0//EN",
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd");
_svg = impl.createDocument(sNamespace, "svg", svgDocType);
Element svgRoot = _svg.getDocumentElement();
svgRoot.setAttribute("xmlns:xlink", xlinkNamespace);
if (bounds != null) {
svgRoot.setAttribute("width", (bounds.getWidth() / SWF.unitDivisor) + "px");
svgRoot.setAttribute("height", (bounds.getHeight() / SWF.unitDivisor) + "px");
createDefGroup(bounds, null, zoom);
}
} catch (ParserConfigurationException ex) {
Logger.getLogger(SVGExporter.class.getName()).log(Level.SEVERE, null, ex);
}
gradients = new ArrayList<>();
}
public PListBuilder() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
builder = factory.newDocumentBuilder();
DOMImplementation di = builder.getDOMImplementation();
dt = di.createDocumentType("plist",
"-//Apple//DTD PLIST 1.0//EN",
"http://www.apple.com/DTDs/PropertyList-1.0.dtd");
doc = di.createDocument("", "plist", dt);
doc.setXmlStandalone(true);
root = doc.getDocumentElement();
root.setAttribute("version", "1.0");
rootDict = doc.createElement("dict");
root.appendChild(rootDict);
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
/**
* Create a new prefs XML document.
*/
private static Document createPrefsDoc(String qname) {
try {
DOMImplementation di = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
DocumentType dt = di.createDocumentType(qname, null, PREFS_DTD_URI);
return di.createDocument(null, qname, dt);
} catch (ParserConfigurationException e) {
throw new AssertionError(e);
}
}
@Test
public void testCreateDocumentType01() throws ParserConfigurationException {
final String name = "document:localName";
final String publicId = "pubid";
final String systemId = "sysid";
DOMImplementation domImpl = getDOMImplementation();
DocumentType documentType = domImpl.createDocumentType(name, publicId, systemId);
verifyDocumentType(documentType, name, publicId, systemId);
}
@Test
public void testCreateDocumentType02() throws ParserConfigurationException {
final String name = "document:localName";
final String publicId = "-//W3C//DTD HTML 4.0 Transitional//EN";
final String systemId = "http://www.w3.org/TR/REC-html40/loose.dtd";
DOMImplementation domImpl = getDOMImplementation();
DocumentType documentType = domImpl.createDocumentType(name, publicId, systemId);
Document document = domImpl.createDocument("http://www.document.com", "document:localName", documentType);
verifyDocumentType(document.getDoctype(), name, publicId, systemId);
}
private static Document createPrefsDoc( String qname ) {
try {
DOMImplementation di = DocumentBuilderFactory.newInstance().
newDocumentBuilder().getDOMImplementation();
DocumentType dt = di.createDocumentType(qname, null, PREFS_DTD_URI);
return di.createDocument(null, qname, dt);
} catch(ParserConfigurationException e) {
throw new AssertionError(e);
}
}
private static Document createPrefsDoc( String qname ) {
try {
DOMImplementation di = DocumentBuilderFactory.newInstance().
newDocumentBuilder().getDOMImplementation();
DocumentType dt = di.createDocumentType(qname, null, PREFS_DTD_URI);
return di.createDocument(null, qname, dt);
} catch(ParserConfigurationException e) {
throw new AssertionError(e);
}
}
/**
* Creates an SVG document and assigns width and height to the root "svg"
* element.
*
* @return Document the SVG document
* @throws Exception
*/
protected Document createSvgDocument( ) throws Exception
{
DocumentBuilderFactory factory = SecurityUtil.newDocumentBuilderFactory( );
DocumentBuilder builder;
builder = factory.newDocumentBuilder( );
DOMImplementation domImpl = builder.getDOMImplementation( );
DocumentType dType = domImpl.createDocumentType( "svg", //$NON-NLS-1$
SVG_VERSION,
SVG_DTD );
Document svgDocument = domImpl.createDocument( XMLNS, "svg", dType ); //$NON-NLS-1$
svgDocument.getDocumentElement( ).setAttribute( "xmlns", XMLNS ); //$NON-NLS-1$
svgDocument.getDocumentElement( )
.setAttribute( "xmlns:xlink", XMLNSXINK ); //$NON-NLS-1$
if ( _resizeSVG )
{
svgDocument.getDocumentElement( )
.setAttribute( "onload", "resizeSVG(evt)" ); //$NON-NLS-1$ //$NON-NLS-2$
// the onload() effect could be inaccurate, call onreisze again to
// ensure, Note onload() is still needed, because onresize may never
// be called.
svgDocument.getDocumentElement( )
.setAttribute( "onresize", "resizeSVG(evt)" ); //$NON-NLS-1$ //$NON-NLS-2$
}
return svgDocument;
}
/**
* Creates an empty DOM document. E.g.:
* <p><pre>
* Document doc = createDocument("book", null, null, null);
* </pre><p>
* creates new DOM of a well-formed document with root element named book.
*
* @param rootQName qualified name of root element. e.g. <code>myroot</code> or <code>ns:myroot</code>
* @param namespaceURI URI of root element namespace or <code>null</code>
* @param doctypePublicID public ID of DOCTYPE or <code>null</code>
* @param doctypeSystemID system ID of DOCTYPE or <code>null</code> if no DOCTYPE
* required and doctypePublicID is also <code>null</code>
*
* @throws DOMException if new DOM with passed parameters can not be created
* @throws FactoryConfigurationError Application developers should never need to directly catch errors of this type.
*
* @return new DOM Document
*/
public static Document createDocument(
String rootQName, String namespaceURI, String doctypePublicID, String doctypeSystemID
) throws DOMException {
DOMImplementation impl = getDOMImplementation();
if ((doctypePublicID != null) && (doctypeSystemID == null)) {
throw new IllegalArgumentException("System ID cannot be null if public ID specified. "); //NOI18N
}
DocumentType dtd = null;
if (doctypeSystemID != null) {
dtd = impl.createDocumentType(rootQName, doctypePublicID, doctypeSystemID);
}
return impl.createDocument(namespaceURI, rootQName, dtd);
}