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
This commit is contained in:
Ryan Gustafson 2008-04-08 23:21:45 +00:00
parent 159cbfa9e9
commit 7a3d8ceb9a
2 changed files with 21 additions and 6 deletions

View File

@ -4,6 +4,7 @@
Fixed bug 1928009 - Error using migration ruleset in PMD 4.2 Fixed bug 1928009 - Error using migration ruleset in PMD 4.2
Fixed bug 1932242 - EmptyMethodInAbstractClassShouldBeAbstract false + Fixed bug 1932242 - EmptyMethodInAbstractClassShouldBeAbstract false +
ruleset.dtd and ruleset_xml_schema.xsd added to jar file in rulesets directory 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: March 25, 2008 - 4.2:

View File

@ -30,11 +30,17 @@ import org.w3c.dom.Text;
*/ */
public class RuleSetWriter { public class RuleSetWriter {
private final OutputStream outputStream; private final OutputStream outputStream;
private final boolean outputNamespace;
private Document document; private Document document;
private Set<String> ruleSetFileNames; private Set<String> ruleSetFileNames;
public RuleSetWriter(OutputStream outputStream) { public RuleSetWriter(OutputStream outputStream) {
this(outputStream, true);
}
public RuleSetWriter(OutputStream outputStream, boolean outputNamespace) {
this.outputStream = outputStream; this.outputStream = outputStream;
this.outputNamespace = outputNamespace;
} }
public void close() throws IOException { public void close() throws IOException {
@ -57,7 +63,11 @@ public class RuleSetWriter {
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.METHOD, "xml");
// This is as close to pretty printing as we'll get using standard Java APIs. // This is as close to pretty printing as we'll get using standard Java APIs.
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 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.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(document), new StreamResult(outputStream)); transformer.transform(new DOMSource(document), new StreamResult(outputStream));
} catch (DOMException e) { } catch (DOMException e) {
@ -73,11 +83,15 @@ public class RuleSetWriter {
private Element createRuleSetElement(RuleSet ruleSet) { private Element createRuleSetElement(RuleSet ruleSet) {
Element ruleSetElement = document.createElement("ruleset"); Element ruleSetElement = document.createElement("ruleset");
ruleSetElement.setAttribute("xmlns", "http://pmd.sf.net/ruleset/1.0.0"); if (outputNamespace) {
ruleSetElement.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation", ruleSetElement.setAttribute("language", ruleSet.getLanguage().getName());
"http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"); // TODO Should we keep track of schema versions?
ruleSetElement.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation", ruleSetElement.setAttribute("xmlns", "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: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()); ruleSetElement.setAttribute("name", ruleSet.getName());
if (ruleSet.getLanguage() != null) { if (ruleSet.getLanguage() != null) {