下面列出了怎么用org.w3c.dom.DOMImplementation的API类实例代码及写法,或者点击链接到github查看源代码。
private void writeSVG(OutputStream stream) throws IOException {
DOMImplementation impl = GenericDOMImplementation
.getDOMImplementation();
String svgNS = "http://www.w3.org/2000/svg";
Document myFactory = impl.createDocument(svgNS, "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(myFactory);
ctx.setEmbeddedFontsOn(Options.getBoolean("EMBEDDED_SVG_FONTS", true));
SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, Options.getBoolean(
"EMBEDDED_SVG_FONTS", true));
svgGenerator.setSVGCanvasSize(size);
paint(svgGenerator, 0, 0);
boolean useCSS = true;
Writer out = new OutputStreamWriter(stream, "UTF-8");
svgGenerator.stream(out, useCSS);
}
/**
* Obtains DOMImpementaton interface providing a number of methods for performing
* operations that are independent of any particular DOM instance.
*
* @throw DOMException <code>NOT_SUPPORTED_ERR</code> if cannot get DOMImplementation
* @throw FactoryConfigurationError Application developers should never need to directly catch errors of this type.
*
* @return DOMImplementation implementation
*/
private static DOMImplementation getDOMImplementation()
throws DOMException { //can be made public
DocumentBuilderFactory factory = getFactory(false, false);
try {
return factory.newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException ex) {
throw new DOMException(
DOMException.NOT_SUPPORTED_ERR, "Cannot create parser satisfying configuration parameters"
); //NOI18N
} catch (RuntimeException e) {
// E.g. #36578, IllegalArgumentException. Try to recover gracefully.
throw (DOMException) new DOMException(DOMException.NOT_SUPPORTED_ERR, e.toString()).initCause(e);
}
}
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<>();
}
/**
* A method to request a DOM implementation.
* @param features A string that specifies which features are required.
* This is a space separated list in which each feature is specified
* by its name optionally followed by a space and a version number.
* This is something like: "XML 1.0 Traversal Events 2.0"
* @return An implementation that has the desired features, or
* <code>null</code> if this source has none.
*/
public DOMImplementation getDOMImplementation(String features) {
DOMImplementation impl = super.getDOMImplementation(features);
if (impl != null){
return impl;
}
// if not try the PSVIDOMImplementation
impl = PSVIDOMImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
return impl;
}
// if not try the XSImplementation
impl = XSImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
return impl;
}
return null;
}
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);
}
};
}
/**
* A method to request a DOM implementation.
* @param features A string that specifies which features are required.
* This is a space separated list in which each feature is specified
* by its name optionally followed by a space and a version number.
* This is something like: "XML 1.0 Traversal Events 2.0"
* @return An implementation that has the desired features, or
* <code>null</code> if this source has none.
*/
public DOMImplementation getDOMImplementation(String features) {
// first check whether the CoreDOMImplementation would do
DOMImplementation impl =
CoreDOMImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
return impl;
}
// if not try the DOMImplementation
impl = DOMImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
return impl;
}
return null;
}
/**
* A method to request a list of DOM implementations that support the
* specified features and versions, as specified in .
* @param features A string that specifies which features and versions
* are required. This is a space separated list in which each feature
* is specified by its name optionally followed by a space and a
* version number. This is something like: "XML 3.0 Traversal +Events
* 2.0"
* @return A list of DOM implementations that support the desired
* features.
*/
public DOMImplementationList getDOMImplementationList(String features) {
final Vector implementations = new Vector();
// first check whether the CoreDOMImplementation would do
DOMImplementationList list = super.getDOMImplementationList(features);
//Add core DOMImplementations
for (int i=0; i < list.getLength(); i++ ) {
implementations.addElement(list.item(i));
}
DOMImplementation impl = PSVIDOMImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
implementations.addElement(impl);
}
impl = XSImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
implementations.addElement(impl);
}
return new DOMImplementationListImpl(implementations);
}
/**
* Exports a JFreeChart to a SVG file.
*
* @param chart
* JFreeChart to export
* @param bounds
* the dimensions of the viewport
* @param svgFile
* the output file.
* @throws IOException
* if writing the svgFile fails.
*
* This method is taken from:
* http://dolf.trieschnigg.nl/jfreechart/
*/
void exportChartAsSVG(final JFreeChart chart, final Rectangle bounds, final File svgFile) throws IOException {
// Get a DOMImplementation and create an XML document
final DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
final Document document = domImpl.createDocument(null, "svg", null);
// Create an instance of the SVG Generator
final SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
// draw the chart in the SVG generator
chart.draw(svgGenerator, bounds);
// Write svg file
final OutputStream outputStream = new FileOutputStream(svgFile);
final Writer out = new OutputStreamWriter(outputStream, "UTF-8");
svgGenerator.stream(out, true /* use css */);
outputStream.flush();
outputStream.close();
}
@Override
public void fullExportToStream(ERDesignerGraph aGraph, OutputStream aStream) throws IOException {
Object[] cells = aGraph.getRoots();
Rectangle2D bounds = aGraph.toScreen(aGraph.getCellBounds(cells));
if (bounds != null) {
DOMImplementation theDomImpl = SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
Document theDocument = theDomImpl.createDocument(svgNS, "svg", null);
SVGGraphics2D theSvgGenerator = new SVGGraphics2D(theDocument);
theSvgGenerator.translate(-bounds.getX() + 10, -bounds.getY() + 0);
RepaintManager theRepaintManager = RepaintManager.currentManager(aGraph);
theRepaintManager.setDoubleBufferingEnabled(false);
boolean theDoubleBuffered = aGraph.isDoubleBuffered();
// Disable double buffering to allow Batik to render svg elements
// instead of images
aGraph.setDoubleBuffered(false);
aGraph.paint(theSvgGenerator);
aGraph.setDoubleBuffered(theDoubleBuffered);
Writer theWriter = new OutputStreamWriter(aStream, PlatformConfig.getXMLEncoding());
theSvgGenerator.stream(theWriter, false);
theRepaintManager.setDoubleBufferingEnabled(true);
theWriter.flush();
theWriter.close();
}
}
public static void main(String[] args) throws Exception {
String format = "javax_imageio_1.0";
BufferedImage img =
new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/png").next();
IIOMetadata meta =
iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), null);
DOMImplementationRegistry registry;
registry = DOMImplementationRegistry.newInstance();
DOMImplementation impl = registry.getDOMImplementation("XML 3.0");
Document doc = impl.createDocument(null, format, null);
Element root, text, entry;
root = doc.getDocumentElement();
root.appendChild(text = doc.createElement("Text"));
text.appendChild(entry = doc.createElement("TextEntry"));
// keyword isn't #REQUIRED by the standard metadata format.
// However, it is required by the PNG format, so we include it here.
entry.setAttribute("keyword", "Comment");
entry.setAttribute("value", "Some demo comment");
meta.mergeTree(format, root);
}
private String generateWorkflowSvgImage( WorkflowMeta workflowMeta ) throws Exception {
float magnification = ZOOM_FACTOR;
Point maximum = workflowMeta.getMaximum();
maximum.multiply( magnification );
DOMImplementation domImplementation = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
String svgNamespace = "http://www.w3.org/2000/svg";
Document document = domImplementation.createDocument(svgNamespace, "svg", null);
HopSvgGraphics2D graphics2D = new HopSvgGraphics2D( document );
SvgGc gc = new SvgGc( graphics2D, new Point(maximum.x,maximum.y), 32, 0, 0 );
WorkflowPainter workflowPainter = new WorkflowPainter( gc, workflowMeta, maximum, null, null, null, null, null, new ArrayList<AreaOwner>(), 32, 1, 0, "Arial", 10, 1.0d );
workflowPainter.setMagnification( magnification );
workflowPainter.drawWorkflow();
// convert to SVG
//
StringWriter stringWriter = new StringWriter();
graphics2D.stream( stringWriter, true );
return stringWriter.toString();
}
/**
* A method to request a DOM implementation.
* @param features A string that specifies which features are required.
* This is a space separated list in which each feature is specified
* by its name optionally followed by a space and a version number.
* This is something like: "XML 1.0 Traversal Events 2.0"
* @return An implementation that has the desired features, or
* <code>null</code> if this source has none.
*/
public DOMImplementation getDOMImplementation(String features) {
DOMImplementation impl = super.getDOMImplementation(features);
if (impl != null){
return impl;
}
// if not try the PSVIDOMImplementation
impl = PSVIDOMImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
return impl;
}
// if not try the XSImplementation
impl = XSImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
return impl;
}
return null;
}
/**
* A method to request a DOM implementation.
* @param features A string that specifies which features are required.
* This is a space separated list in which each feature is specified
* by its name optionally followed by a space and a version number.
* This is something like: "XML 1.0 Traversal Events 2.0"
* @return An implementation that has the desired features, or
* <code>null</code> if this source has none.
*/
public DOMImplementation getDOMImplementation(String features) {
DOMImplementation impl = super.getDOMImplementation(features);
if (impl != null){
return impl;
}
// if not try the PSVIDOMImplementation
impl = PSVIDOMImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
return impl;
}
// if not try the XSImplementation
impl = XSImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
return impl;
}
return null;
}
/**
* A method to request a list of DOM implementations that support the
* specified features and versions, as specified in .
* @param features A string that specifies which features and versions
* are required. This is a space separated list in which each feature
* is specified by its name optionally followed by a space and a
* version number. This is something like: "XML 3.0 Traversal +Events
* 2.0"
* @return A list of DOM implementations that support the desired
* features.
*/
public DOMImplementationList getDOMImplementationList(String features) {
final Vector implementations = new Vector();
// first check whether the CoreDOMImplementation would do
DOMImplementationList list = super.getDOMImplementationList(features);
//Add core DOMImplementations
for (int i=0; i < list.getLength(); i++ ) {
implementations.addElement(list.item(i));
}
DOMImplementation impl = PSVIDOMImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
implementations.addElement(impl);
}
impl = XSImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
implementations.addElement(impl);
}
return new DOMImplementationListImpl(implementations);
}
/**
* A method to request a DOM implementation.
* @param features A string that specifies which features are required.
* This is a space separated list in which each feature is specified
* by its name optionally followed by a space and a version number.
* This is something like: "XML 1.0 Traversal Events 2.0"
* @return An implementation that has the desired features, or
* <code>null</code> if this source has none.
*/
public DOMImplementation getDOMImplementation(String features) {
DOMImplementation impl = super.getDOMImplementation(features);
if (impl != null){
return impl;
}
// if not try the PSVIDOMImplementation
impl = PSVIDOMImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
return impl;
}
// if not try the XSImplementation
impl = XSImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
return impl;
}
return null;
}
/***
* Helper method which converts XML Document into pretty formatted string
*
* @param doc to convert
* @return converted XML as String
*/
public static String documentToString(Document doc) {
String strMsg = "";
try {
DOMImplementation domImpl = doc.getImplementation();
DOMImplementationLS domImplLS = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
LSSerializer lsSerializer = domImplLS.createLSSerializer();
lsSerializer.getDomConfig().setParameter("format-pretty-print", true);
Writer stringWriter = new StringWriter();
LSOutput lsOutput = domImplLS.createLSOutput();
lsOutput.setEncoding("UTF-8");
lsOutput.setCharacterStream(stringWriter);
lsSerializer.write(doc, lsOutput);
strMsg = stringWriter.toString();
} catch (Exception e) {
logger.warn("Error occurred when converting document to string", e);
}
return strMsg;
}
/**
* A method to request a list of DOM implementations that support the
* specified features and versions, as specified in .
* @param features A string that specifies which features and versions
* are required. This is a space separated list in which each feature
* is specified by its name optionally followed by a space and a
* version number. This is something like: "XML 3.0 Traversal +Events
* 2.0"
* @return A list of DOM implementations that support the desired
* features.
*/
public DOMImplementationList getDOMImplementationList(String features) {
final Vector implementations = new Vector();
// first check whether the CoreDOMImplementation would do
DOMImplementationList list = super.getDOMImplementationList(features);
//Add core DOMImplementations
for (int i=0; i < list.getLength(); i++ ) {
implementations.addElement(list.item(i));
}
DOMImplementation impl = PSVIDOMImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
implementations.addElement(impl);
}
impl = XSImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
implementations.addElement(impl);
}
return new DOMImplementationListImpl(implementations);
}
/**
* Return the first implementation that has the desired
* features, or <code>null</code> if none is found.
*
* @param features
* A string that specifies which features are required. This is
* a space separated list in which each feature is specified by
* its name optionally followed by a space and a version number.
* This is something like: "XML 1.0 Traversal +Events 2.0"
* @return An implementation that has the desired features,
* or <code>null</code> if none found.
*/
public DOMImplementation getDOMImplementation(final String features) {
int size = sources.size();
String name = null;
for (int i = 0; i < size; i++) {
DOMImplementationSource source =
(DOMImplementationSource) sources.elementAt(i);
DOMImplementation impl = source.getDOMImplementation(features);
if (impl != null) {
return impl;
}
}
return null;
}
/**
* Equivalence class partitioning with state and input values orientation
* for public void setParameter(String name, Object value) throws
* DOMException, <br>
* <b>pre-conditions</b>: The root contains a CDATASection with the
* termination marker ']]>', <br>
* <b>name</b>: split-cdata-sections <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: A warning is reported when the section is
* splitted
*/
@Test
public void testSplitCDATA001() {
DOMImplementation domImpl = null;
try {
domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
} catch (ParserConfigurationException pce) {
Assert.fail(pce.toString());
} catch (FactoryConfigurationError fce) {
Assert.fail(fce.toString());
}
Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);
DOMConfiguration config = doc.getDomConfig();
CDATASection cdata = doc.createCDATASection("text]" + "]>text");
doc.getDocumentElement().appendChild(cdata);
TestHandler testHandler = new TestHandler();
config.setParameter("error-handler", testHandler);
if (!config.canSetParameter("split-cdata-sections", Boolean.TRUE)) {
Assert.fail("cannot set the parameters 'split-cdata-sections' to true");
}
config.setParameter("split-cdata-sections", Boolean.TRUE);
doc.normalizeDocument();
if (null == testHandler.getWarning()) {
Assert.fail("no warning is reported");
}
return; // Status.passed("OK");
}
/**
* A method to request a list of DOM implementations that support the
* specified features and versions, as specified in .
* @param features A string that specifies which features and versions
* are required. This is a space separated list in which each feature
* is specified by its name optionally followed by a space and a
* version number. This is something like: "XML 3.0 Traversal +Events
* 2.0"
* @return A list of DOM implementations that support the desired
* features.
*/
public DOMImplementationList getDOMImplementationList(String features) {
// first check whether the CoreDOMImplementation would do
DOMImplementation impl = CoreDOMImplementationImpl.getDOMImplementation();
final Vector implementations = new Vector();
if (testImpl(impl, features)) {
implementations.addElement(impl);
}
impl = DOMImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
implementations.addElement(impl);
}
return new DOMImplementationListImpl(implementations);
}
/**
* Returns the indexth item in the collection.
*
* @param index The index of the DOMImplemetation from the list to return.
*/
public DOMImplementation item(int index) {
try {
return (DOMImplementation) fImplementations.elementAt(index);
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
/**
* Unimplemented. See org.w3c.dom.Document
*
* @return null
*/
public DOMImplementation getImplementation()
{
error(XMLErrorResources.ER_FUNCTION_NOT_SUPPORTED);
return null;
}
/**
* Return a list of implementations that support the
* desired features.
*
* @param features
* A string that specifies which features are required. This is
* a space separated list in which each feature is specified by
* its name optionally followed by a space and a version number.
* This is something like: "XML 1.0 Traversal +Events 2.0"
* @return A list of DOMImplementations that support the desired features.
*/
public DOMImplementationList getDOMImplementationList(final String features) {
final Vector implementations = new Vector();
int size = sources.size();
for (int i = 0; i < size; i++) {
DOMImplementationSource source =
(DOMImplementationSource) sources.elementAt(i);
DOMImplementationList impls =
source.getDOMImplementationList(features);
for (int j = 0; j < impls.getLength(); j++) {
DOMImplementation impl = impls.item(j);
implementations.addElement(impl);
}
}
return new DOMImplementationList() {
public DOMImplementation item(final int index) {
if (index >= 0 && index < implementations.size()) {
try {
return (DOMImplementation)
implementations.elementAt(index);
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
return null;
}
public int getLength() {
return implementations.size();
}
};
}
/**
* Returns the JAXP DOM implementation.
*
* @return the JAXP DOM implementation
*/
private static DOMImplementation jaxpDOMImplementation() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
}
return builder.getDOMImplementation();
}
/**
* Returns the indexth item in the collection.
*
* @param index The index of the DOMImplemetation from the list to return.
*/
public DOMImplementation item(int index) {
try {
return (DOMImplementation) fImplementations.elementAt(index);
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
/**
* Return a list of implementations that support the
* desired features.
*
* @param features
* A string that specifies which features are required. This is
* a space separated list in which each feature is specified by
* its name optionally followed by a space and a version number.
* This is something like: "XML 1.0 Traversal +Events 2.0"
* @return A list of DOMImplementations that support the desired features.
*/
public DOMImplementationList getDOMImplementationList(final String features) {
final Vector implementations = new Vector();
int size = sources.size();
for (int i = 0; i < size; i++) {
DOMImplementationSource source =
(DOMImplementationSource) sources.elementAt(i);
DOMImplementationList impls =
source.getDOMImplementationList(features);
for (int j = 0; j < impls.getLength(); j++) {
DOMImplementation impl = impls.item(j);
implementations.addElement(impl);
}
}
return new DOMImplementationList() {
public DOMImplementation item(final int index) {
if (index >= 0 && index < implementations.size()) {
try {
return (DOMImplementation)
implementations.elementAt(index);
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
return null;
}
public int getLength() {
return implementations.size();
}
};
}
/**
* Return the first implementation that has the desired
* features, or <code>null</code> if none is found.
*
* @param features
* A string that specifies which features are required. This is
* a space separated list in which each feature is specified by
* its name optionally followed by a space and a version number.
* This is something like: "XML 1.0 Traversal +Events 2.0"
* @return An implementation that has the desired features,
* or <code>null</code> if none found.
*/
public DOMImplementation getDOMImplementation(final String features) {
int size = sources.size();
String name = null;
for (int i = 0; i < size; i++) {
DOMImplementationSource source =
(DOMImplementationSource) sources.elementAt(i);
DOMImplementation impl = source.getDOMImplementation(features);
if (impl != null) {
return impl;
}
}
return null;
}
protected Document getXmlDocument(final String rootname) throws XmlException {
try {
DocumentBuilder builder = getFactory().newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
return impl.createDocument(null, rootname, null);
} catch (ParserConfigurationException ex) {
throw new XmlException(ex.getMessage(), ex);
}
}
/**
* A method to request a list of DOM implementations that support the
* specified features and versions, as specified in .
* @param features A string that specifies which features and versions
* are required. This is a space separated list in which each feature
* is specified by its name optionally followed by a space and a
* version number. This is something like: "XML 3.0 Traversal +Events
* 2.0"
* @return A list of DOM implementations that support the desired
* features.
*/
public DOMImplementationList getDOMImplementationList(String features) {
// first check whether the CoreDOMImplementation would do
DOMImplementation impl = CoreDOMImplementationImpl.getDOMImplementation();
final Vector implementations = new Vector();
if (testImpl(impl, features)) {
implementations.addElement(impl);
}
impl = DOMImplementationImpl.getDOMImplementation();
if (testImpl(impl, features)) {
implementations.addElement(impl);
}
return new DOMImplementationListImpl(implementations);
}
/**
* Unimplemented. See org.w3c.dom.Document
*
* @return null
*/
public DOMImplementation getImplementation()
{
error(XMLErrorResources.ER_FUNCTION_NOT_SUPPORTED);
return null;
}