From 7a3d8ceb9a89abcf7f07724f47437032eaaf2f02 Mon Sep 17 00:00:00 2001 From: Ryan Gustafson Date: Tue, 8 Apr 2008 23:21:45 +0000 Subject: [PATCH] Update RuleSetWriter to handle non-Apache TRAX implementations, add an option to not use XML Namespaces git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@5992 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/etc/changelog.txt | 1 + .../net/sourceforge/pmd/RuleSetWriter.java | 26 ++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index a032b52938..0b2dbca9ba 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -4,6 +4,7 @@ Fixed bug 1928009 - Error using migration ruleset in PMD 4.2 Fixed bug 1932242 - EmptyMethodInAbstractClassShouldBeAbstract false + ruleset.dtd and ruleset_xml_schema.xsd added to jar file in rulesets directory +Update RuleSetWriter to handle non-Apache TRAX implementations, add an option to not use XML Namespaces March 25, 2008 - 4.2: diff --git a/pmd/src/net/sourceforge/pmd/RuleSetWriter.java b/pmd/src/net/sourceforge/pmd/RuleSetWriter.java index 7d5bf4e8c4..2707ff3c4f 100644 --- a/pmd/src/net/sourceforge/pmd/RuleSetWriter.java +++ b/pmd/src/net/sourceforge/pmd/RuleSetWriter.java @@ -30,11 +30,17 @@ import org.w3c.dom.Text; */ public class RuleSetWriter { private final OutputStream outputStream; + private final boolean outputNamespace; private Document document; private Set ruleSetFileNames; public RuleSetWriter(OutputStream outputStream) { + this(outputStream, true); + } + + public RuleSetWriter(OutputStream outputStream, boolean outputNamespace) { this.outputStream = outputStream; + this.outputNamespace = outputNamespace; } public void close() throws IOException { @@ -57,7 +63,11 @@ public class RuleSetWriter { transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // This is as close to pretty printing as we'll get using standard Java APIs. transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3"); + try { + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3"); + } catch (IllegalArgumentException e) { + // Not Apache + } transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(new DOMSource(document), new StreamResult(outputStream)); } catch (DOMException e) { @@ -73,11 +83,15 @@ public class RuleSetWriter { private Element createRuleSetElement(RuleSet ruleSet) { Element ruleSetElement = document.createElement("ruleset"); - ruleSetElement.setAttribute("xmlns", "http://pmd.sf.net/ruleset/1.0.0"); - ruleSetElement.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation", - "http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"); - ruleSetElement.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation", - "http://pmd.sf.net/ruleset_xml_schema.xsd"); + if (outputNamespace) { + ruleSetElement.setAttribute("language", ruleSet.getLanguage().getName()); + // TODO Should we keep track of schema versions? + ruleSetElement.setAttribute("xmlns", "http://pmd.sf.net/ruleset/1.0.0"); + ruleSetElement.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation", + "http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"); + ruleSetElement.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation", + "http://pmd.sf.net/ruleset_xml_schema.xsd"); + } ruleSetElement.setAttribute("name", ruleSet.getName()); if (ruleSet.getLanguage() != null) {