2022-01-31 09:25:13 +01:00
|
|
|
include::../description.adoc[]
|
|
|
|
|
|
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
2022-01-31 09:25:13 +01:00
|
|
|
----
|
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
|
|
|
String xml = "<node id=\""+req.getParameter("id")+\"></node>;
|
|
|
|
FileOutputStream fos = new FileOutputStream("output.xml");
|
|
|
|
fos.write(xml.getBytes(Charset.forName("UTF-8"))); // Noncompliant
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
javax.xml.parsers.DocumentBuilder
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
2022-01-31 09:25:13 +01:00
|
|
|
----
|
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
|
|
|
String xml = "<node id=\""+req.getParameter("id")+\"></node>;
|
|
|
|
|
|
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
|
|
|
Document doc = builder.parse(new InputSource(new StringReader(xml))); // Noncompliant
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
2022-01-31 09:25:13 +01:00
|
|
|
----
|
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
|
|
|
String id = req.getParameter("id");
|
|
|
|
if(id.matches("^[A-Za-z0-9_]+$")) {
|
|
|
|
String xml = "<node id=\""+id+\"></node>;
|
|
|
|
FileOutputStream fos = new FileOutputStream("output.xml");
|
|
|
|
fos.write(xml.getBytes(Charset.forName("UTF-8")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
javax.xml.parsers.DocumentBuilder
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
2022-01-31 09:25:13 +01:00
|
|
|
----
|
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
|
|
|
String xml = "<node></node>;
|
|
|
|
|
|
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
|
|
|
Document doc = builder.parse(new InputSource(new StringReader(xml))); // Noncompliant
|
|
|
|
Element element = (Element) doc.getElementsByTagName("something").item(0);
|
|
|
|
element.setAttribute("id", req.getParameter("id"));
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
include::../see.adoc[]
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../message.adoc[]
|
|
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|