use of org.w3c.dom.DocumentType in project liferay-ide by liferay.
the class ServiceUtil method getDTDVersion.
public static String getDTDVersion(Document document) {
String dtdVersion = null;
DocumentType docType = document.getDoctype();
if (docType != null) {
String publicId = docType.getPublicId();
String systemId = docType.getSystemId();
if ((publicId != null) && (systemId != null)) {
if (publicId.contains("6.0.0") || systemId.contains("6.0.0")) {
dtdVersion = "6.0.0";
} else if (publicId.contains("6.1.0") || systemId.contains("6.1.0")) {
dtdVersion = "6.1.0";
}
}
}
return dtdVersion;
}
use of org.w3c.dom.DocumentType in project openmrs-core by openmrs.
the class OpenmrsUtil method saveDocument.
/**
* Save the given xml document to the given outfile
*
* @param doc Document to be saved
* @param outFile file pointer to the location the xml file is to be saved to
*/
public static void saveDocument(Document doc, File outFile) {
OutputStream outStream = null;
try {
outStream = new FileOutputStream(outFile);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DocumentType doctype = doc.getDoctype();
if (doctype != null) {
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, doctype.getPublicId());
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doctype.getSystemId());
}
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(outStream);
transformer.transform(source, result);
} catch (TransformerException e) {
throw new ModuleException("Error while saving dwrmodulexml back to dwr-modules.xml", e);
} catch (FileNotFoundException e) {
throw new ModuleException(outFile.getAbsolutePath() + " file doesn't exist.", e);
} finally {
try {
if (outStream != null) {
outStream.close();
}
} catch (Exception e) {
log.warn("Unable to close outstream", e);
}
}
}
use of org.w3c.dom.DocumentType in project jsoup by jhy.
the class W3CDom method fromJsoup.
/**
* Convert a jsoup DOM to a W3C Document. The created nodes will link back to the original
* jsoup nodes in the user property {@link #SourceProperty} (but after conversion, changes on one side will not
* flow to the other). The input Element is used as a context node, but the whole surrounding jsoup Document is
* converted. (If you just want a subtree converted, use {@link #convert(org.jsoup.nodes.Element, Document)}.)
*
* @param in jsoup element or doc
* @return a W3C DOM Document representing the jsoup Document or Element contents.
* @see #sourceNodes(NodeList, Class)
* @see #contextNode(Document)
*/
public Document fromJsoup(org.jsoup.nodes.Element in) {
Validate.notNull(in);
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
Document out = builder.newDocument();
org.jsoup.nodes.Document inDoc = in.ownerDocument();
org.jsoup.nodes.DocumentType doctype = inDoc != null ? inDoc.documentType() : null;
if (doctype != null) {
org.w3c.dom.DocumentType documentType = impl.createDocumentType(doctype.name(), doctype.publicId(), doctype.systemId());
out.appendChild(documentType);
}
out.setXmlStandalone(true);
// if in is Document, use the root element, not the wrapping document, as the context:
org.jsoup.nodes.Element context = (in instanceof org.jsoup.nodes.Document) ? in.child(0) : in;
out.setUserData(ContextProperty, context, null);
convert(inDoc != null ? inDoc : in, out);
return out;
} catch (ParserConfigurationException e) {
throw new IllegalStateException(e);
}
}
use of org.w3c.dom.DocumentType in project generator by mybatis.
the class ConfigurationParser method parseConfiguration.
private Configuration parseConfiguration(InputSource inputSource) throws IOException, XMLParserException {
parseErrors.clear();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
factory.setValidating(true);
try {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new ParserEntityResolver());
ParserErrorHandler handler = new ParserErrorHandler(warnings, parseErrors);
builder.setErrorHandler(handler);
Document document = null;
try {
document = builder.parse(inputSource);
} catch (SAXParseException e) {
throw new XMLParserException(parseErrors);
} catch (SAXException e) {
if (e.getException() == null) {
parseErrors.add(e.getMessage());
} else {
parseErrors.add(e.getException().getMessage());
}
}
if (document == null || !parseErrors.isEmpty()) {
throw new XMLParserException(parseErrors);
}
Configuration config;
Element rootNode = document.getDocumentElement();
DocumentType docType = document.getDoctype();
if (rootNode.getNodeType() == Node.ELEMENT_NODE && docType.getPublicId().equals(XmlConstants.MYBATIS_GENERATOR_CONFIG_PUBLIC_ID)) {
config = parseMyBatisGeneratorConfiguration(rootNode);
} else {
// $NON-NLS-1$
throw new XMLParserException(getString("RuntimeError.5"));
}
if (!parseErrors.isEmpty()) {
throw new XMLParserException(parseErrors);
}
return config;
} catch (ParserConfigurationException e) {
parseErrors.add(e.getMessage());
throw new XMLParserException(parseErrors);
}
}
use of org.w3c.dom.DocumentType in project j2objc by google.
the class DOM2DTM method getUnparsedEntityURI.
/**
* The getUnparsedEntityURI function returns the URI of the unparsed
* entity with the specified name in the same document as the context
* node (see [3.3 Unparsed Entities]). It returns the empty string if
* there is no such entity.
* <p>
* XML processors may choose to use the System Identifier (if one
* is provided) to resolve the entity, rather than the URI in the
* Public Identifier. The details are dependent on the processor, and
* we would have to support some form of plug-in resolver to handle
* this properly. Currently, we simply return the System Identifier if
* present, and hope that it a usable URI or that our caller can
* map it to one.
* TODO: Resolve Public Identifiers... or consider changing function name.
* <p>
* If we find a relative URI
* reference, XML expects it to be resolved in terms of the base URI
* of the document. The DOM doesn't do that for us, and it isn't
* entirely clear whether that should be done here; currently that's
* pushed up to a higher level of our application. (Note that DOM Level
* 1 didn't store the document's base URI.)
* TODO: Consider resolving Relative URIs.
* <p>
* (The DOM's statement that "An XML processor may choose to
* completely expand entities before the structure model is passed
* to the DOM" refers only to parsed entities, not unparsed, and hence
* doesn't affect this function.)
*
* @param name A string containing the Entity Name of the unparsed
* entity.
*
* @return String containing the URI of the Unparsed Entity, or an
* empty string if no such entity exists.
*/
public String getUnparsedEntityURI(String name) {
String url = "";
Document doc = (m_root.getNodeType() == Node.DOCUMENT_NODE) ? (Document) m_root : m_root.getOwnerDocument();
if (null != doc) {
DocumentType doctype = doc.getDoctype();
if (null != doctype) {
NamedNodeMap entities = doctype.getEntities();
if (null == entities)
return url;
Entity entity = (Entity) entities.getNamedItem(name);
if (null == entity)
return url;
String notationName = entity.getNotationName();
if (// then it's unparsed
null != notationName) {
// The draft says: "The XSLT processor may use the public
// identifier to generate a URI for the entity instead of the URI
// specified in the system identifier. If the XSLT processor does
// not use the public identifier to generate the URI, it must use
// the system identifier; if the system identifier is a relative
// URI, it must be resolved into an absolute URI using the URI of
// the resource containing the entity declaration as the base
// URI [RFC2396]."
// So I'm falling a bit short here.
url = entity.getSystemId();
if (null == url) {
url = entity.getPublicId();
} else {
// This should be resolved to an absolute URL, but that's hard
// to do from here.
}
}
}
}
return url;
}
Aggregations