Skip to content

Commit

Permalink
Use newDefaultInstance for the different Java XML Factories
Browse files Browse the repository at this point in the history
Sometimes there are other XML Factories available and some of those that are older versions cause issues in the parsing of BPMN / CMMN files.
Sometimes they cutoff some strings and we miss some data using it.
When using the default instance we can be sure that what is used by Flowable in testing is what it will be used for our users as well
  • Loading branch information
filiphr committed Feb 14, 2024
1 parent f43c3c6 commit d823e36
Show file tree
Hide file tree
Showing 24 changed files with 34 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected Set<String> getAllMappedEntityResources() {

protected Set<String> getResources(BiConsumer<NodeList, Set<String>> consumer) {
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newDefaultInstance();
docBuilderFactory.setValidating(false);
docBuilderFactory.setNamespaceAware(false);
docBuilderFactory.setExpandEntityReferences(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.List;
import java.util.Map;

import javax.xml.XMLConstants;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
Expand Down Expand Up @@ -231,7 +230,7 @@ public void validateModel(XMLStreamReader xmlStreamReader) throws Exception {
}

protected Schema createSchema() throws SAXException {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
SchemaFactory factory = SchemaFactory.newDefaultInstance();
Schema schema = null;
if (classloader != null) {
schema = factory.newSchema(classloader.getResource(BPMN_XSD));
Expand All @@ -252,7 +251,7 @@ public BpmnModel convertToBpmnModel(InputStreamProvider inputStreamProvider, boo
}

public BpmnModel convertToBpmnModel(InputStreamProvider inputStreamProvider, boolean validateSchema, boolean enableSafeBpmnXml, String encoding) {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLInputFactory xif = XMLInputFactory.newDefaultFactory();

if (xif.isPropertySupported(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES)) {
xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected Map<String, Map> parseBPMNDI(byte[] xml) throws Exception {
Map<String, List<GraphicInfo>> edgesMap = null;
Map<String, GraphicInfo> shapesMap = null;

XMLInputFactory xif = XMLInputFactory.newInstance();
XMLInputFactory xif = XMLInputFactory.newDefaultFactory();
InputStreamReader in = new InputStreamReader(new ByteArrayInputStream(xml), StandardCharsets.UTF_8);
XMLStreamReader xtr = xif.createXMLStreamReader(in);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Set<String> getResourceNames() {
return result;
}
try {
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(processFileUrl.openStream());
Document document = DocumentBuilderFactory.newDefaultInstance().newDocumentBuilder().parse(processFileUrl.openStream());
NodeList nodeList = document.getElementsByTagName(PROCESS_ELEMENT_NAME);
for (int i = 0; i < nodeList.getLength(); i++) {
Node cn = nodeList.item(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Set;
import java.util.stream.Collectors;

import javax.xml.XMLConstants;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
Expand Down Expand Up @@ -154,7 +153,7 @@ public CmmnModel convertToCmmnModel(InputStreamProvider inputStreamProvider, boo
}

public CmmnModel convertToCmmnModel(InputStreamProvider inputStreamProvider, boolean validateSchema, boolean enableSafeBpmnXml, String encoding) {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLInputFactory xif = XMLInputFactory.newDefaultFactory();

if (xif.isPropertySupported(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES)) {
xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
Expand Down Expand Up @@ -256,7 +255,7 @@ public void validateModel(XMLStreamReader xmlStreamReader) throws Exception {
}

protected Schema createSchema() throws SAXException {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
SchemaFactory factory = SchemaFactory.newDefaultInstance();
Schema schema = null;
if (classloader != null) {
schema = factory.newSchema(classloader.getResource(XSD_LOCATION));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected Set<String> getAllMappedEntityResources() {

protected Set<String> getResources(BiConsumer<NodeList, Set<String>> consumer) {
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newDefaultInstance();
docBuilderFactory.setValidating(false);
docBuilderFactory.setNamespaceAware(false);
docBuilderFactory.setExpandEntityReferences(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ protected void createDefaultStructures(Element rootTypes) {

protected Element getRootTypes() {
try {
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
DocumentBuilder docBuilder = DocumentBuilderFactory.newDefaultInstance().newDocumentBuilder();
Document doc = docBuilder.parse(this.wsdlLocation);
Element root = (Element) doc.getFirstChild();
Element typesElement = (Element) root.getElementsByTagName("wsdl:types").item(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected Set<String> getAllMappedEntityResources() {

protected Set<String> getResources(BiConsumer<NodeList, Set<String>> consumer) {
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newDefaultInstance();
docBuilderFactory.setValidating(false);
docBuilderFactory.setNamespaceAware(false);
docBuilderFactory.setExpandEntityReferences(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Map;
import java.util.Set;

import javax.xml.XMLConstants;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
Expand Down Expand Up @@ -163,7 +162,7 @@ public void validateModel(XMLStreamReader xmlStreamReader) throws Exception {

protected String getTargetNameSpace(InputStream is) {
try {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLInputFactory xif = XMLInputFactory.newDefaultFactory();
XMLStreamReader xtr = xif.createXMLStreamReader(is);

return getTargetNameSpace(xtr);
Expand Down Expand Up @@ -195,7 +194,7 @@ protected String getTargetNameSpace(XMLStreamReader xmlStreamReader) {
}

protected Schema createSchema(String xsd) throws SAXException {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
SchemaFactory factory = SchemaFactory.newDefaultInstance();
Schema schema = null;
if (classloader != null) {
schema = factory.newSchema(classloader.getResource(xsd));
Expand All @@ -216,7 +215,7 @@ public DmnDefinition convertToDmnModel(InputStreamProvider inputStreamProvider,
}

public DmnDefinition convertToDmnModel(InputStreamProvider inputStreamProvider, boolean validateSchema, boolean enableSafeDmnXml, String encoding) {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLInputFactory xif = XMLInputFactory.newDefaultFactory();

if (xif.isPropertySupported(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES)) {
xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public abstract class AbstractConverterTest {

protected DmnDefinition readXMLFile() throws Exception {
InputStream xmlStream = this.getClass().getClassLoader().getResourceAsStream(getResource());
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLInputFactory xif = XMLInputFactory.newDefaultFactory();
InputStreamReader in = new InputStreamReader(xmlStream, StandardCharsets.UTF_8);
XMLStreamReader xtr = xif.createXMLStreamReader(in);
return new DmnXMLConverter().convertToDmnModel(xtr);
}

protected DmnDefinition exportAndReadXMLFile(DmnDefinition definition) throws Exception {
byte[] xml = new DmnXMLConverter().convertToXML(definition);
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLInputFactory xif = XMLInputFactory.newDefaultFactory();
InputStreamReader in = new InputStreamReader(new ByteArrayInputStream(xml), StandardCharsets.UTF_8);
XMLStreamReader xtr = xif.createXMLStreamReader(in);
return new DmnXMLConverter().convertToDmnModel(xtr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void configure(TypeHandlerRegistry typeHandlerRegistry) {
}

protected DocumentBuilderFactory createDocumentBuilderFactory() throws ParserConfigurationException {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newDefaultInstance();
if (!enableMybatisXmlMappingValidation) {
docBuilderFactory.setValidating(false);
docBuilderFactory.setNamespaceAware(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public DurationHelper(String expressionS, int maxIterations, ClockReader clockRe
this.clockReader = clockReader;
this.maxIterations = maxIterations;
List<String> expression = Arrays.asList(expressionS.split("/"));
datatypeFactory = DatatypeFactory.newInstance();
datatypeFactory = DatatypeFactory.newDefaultInstance();

if (expression.size() > 3 || expression.isEmpty()) {
throw new FlowableIllegalArgumentException("Cannot parse duration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public DiagramLayout getBpmnProcessDiagramLayout(Document bpmnModel, InputStream

protected Document parseXml(InputStream bpmnXmlStream) {
// Initiate DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory factory = DocumentBuilderFactory.newDefaultInstance();
// Get one that understands namespaces
factory.setNamespaceAware(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected Set<String> getAllMappedEntityResources() {

protected Set<String> getResources(BiConsumer<NodeList, Set<String>> consumer) {
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newDefaultInstance();
docBuilderFactory.setValidating(false);
docBuilderFactory.setNamespaceAware(false);
docBuilderFactory.setExpandEntityReferences(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void verifyValidationWithCustomServiceTask() throws Exception {
ProcessValidator processValidator = processValidatorFactory.createDefaultProcessValidator();

InputStream xmlStream = this.getClass().getClassLoader().getResourceAsStream("org/flowable/engine/test/validation/customServiceTaskProcess.bpmn20.xml");
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLInputFactory xif = XMLInputFactory.newDefaultFactory();
InputStreamReader in = new InputStreamReader(xmlStream, StandardCharsets.UTF_8);
XMLStreamReader xtr = xif.createXMLStreamReader(in);
BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
Expand All @@ -73,7 +73,7 @@ public void verifyValidationWithCustomServiceTaskValidator() throws Exception {
ProcessValidator processValidator = processValidatorFactory.createDefaultProcessValidator();

InputStream xmlStream = this.getClass().getClassLoader().getResourceAsStream("org/flowable/engine/test/validation/customServiceTaskProcess.bpmn20.xml");
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLInputFactory xif = XMLInputFactory.newDefaultFactory();
InputStreamReader in = new InputStreamReader(xmlStream, StandardCharsets.UTF_8);
XMLStreamReader xtr = xif.createXMLStreamReader(in);
BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setupProcessValidator() {
public void verifyValidation() throws Exception {

InputStream xmlStream = this.getClass().getClassLoader().getResourceAsStream("org/flowable/engine/test/validation/invalidProcess.bpmn20.xml");
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLInputFactory xif = XMLInputFactory.newDefaultFactory();
InputStreamReader in = new InputStreamReader(xmlStream, StandardCharsets.UTF_8);
XMLStreamReader xtr = xif.createXMLStreamReader(in);
BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
Expand Down Expand Up @@ -275,7 +275,7 @@ public void testWarningError() throws XMLStreamException {
+ " <userTask id='theTask1' name='Input is one' /> " + " <userTask id='theTask2' name='Default input' /> " + " </process>"
+ "</definitions>";

XMLInputFactory xif = XMLInputFactory.newInstance();
XMLInputFactory xif = XMLInputFactory.newDefaultFactory();
InputStreamReader in = new InputStreamReader(new ByteArrayInputStream(flowWithoutConditionNoDefaultFlow.getBytes()), StandardCharsets.UTF_8);
XMLStreamReader xtr = xif.createXMLStreamReader(in);
BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void setupProcessValidator() {
public void verifyValidation() throws Exception {

InputStream xmlStream = this.getClass().getClassLoader().getResourceAsStream("org/flowable/engine/test/validation/multipleStartEvent.bpmn20.xml");
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLInputFactory xif = XMLInputFactory.newDefaultFactory();
InputStreamReader in = new InputStreamReader(xmlStream, StandardCharsets.UTF_8);
XMLStreamReader xtr = xif.createXMLStreamReader(in);
BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class EventPayloadToXmlStringSerializer implements OutboundEventSerialize
public String serialize(EventInstance eventInstance) {
try {

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newDefaultInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

Document doc = docBuilder.newDocument();
Expand All @@ -57,7 +57,7 @@ public String serialize(EventInstance eventInstance) {
}
}

TransformerFactory tf = TransformerFactory.newInstance();
TransformerFactory tf = TransformerFactory.newDefaultInstance();
Transformer transformer = tf.newTransformer();
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class StringToXmlDocumentDeserializer implements InboundEventDeserializer
@Override
public Document deserialize(Object rawEvent) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newDefaultInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
try (InputStream inputStream = new ByteArrayInputStream(convertEventToBytes(rawEvent))) {
Document document = documentBuilder.parse(inputStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected Set<String> getAllMappedEntityResources() {

protected Set<String> getResources(BiConsumer<NodeList, Set<String>> consumer) {
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newDefaultInstance();
docBuilderFactory.setValidating(false);
docBuilderFactory.setNamespaceAware(false);
docBuilderFactory.setExpandEntityReferences(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected Set<String> getAllMappedEntityResources() {

protected Set<String> getResources(BiConsumer<NodeList, Set<String>> consumer) {
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newDefaultInstance();
docBuilderFactory.setValidating(false);
docBuilderFactory.setNamespaceAware(false);
docBuilderFactory.setExpandEntityReferences(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public URL transform(URL artifact) {

protected Document parse(File artifact) throws Exception {
if (dbf == null) {
dbf = DocumentBuilderFactory.newInstance();
dbf = DocumentBuilderFactory.newDefaultInstance();
dbf.setNamespaceAware(true);
}
DocumentBuilder db = dbf.newDocumentBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static void transform(URL url, OutputStream os) throws Exception {
out.putNextEntry(e);
// Copy the new DOM
if (tf == null) {
tf = TransformerFactory.newInstance();
tf = TransformerFactory.newDefaultInstance();
}
tf.newTransformer().transform(new DOMSource(doc), new StreamResult(out));
out.closeEntry();
Expand Down Expand Up @@ -169,7 +169,7 @@ private static void cleanupModifier(StringBuffer result, String modifier) {

protected static Document parse(URL url) throws Exception {
if (dbf == null) {
dbf = DocumentBuilderFactory.newInstance();
dbf = DocumentBuilderFactory.newDefaultInstance();
dbf.setNamespaceAware(true);
}
DocumentBuilder db = dbf.newDocumentBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private Map<String, Document> readMappingFile(EntityMappingPackageInformation pa
}

private Document readXmlDocument(String resourceLocation) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newDefaultInstance();
docBuilderFactory.setValidating(false);
docBuilderFactory.setNamespaceAware(false);
docBuilderFactory.setExpandEntityReferences(false);
Expand Down Expand Up @@ -359,7 +359,7 @@ private List<Node> findChildNodes(Node node, String elementName) {
private String toString(Node node) {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(node), new StreamResult(byteArrayOutputStream));
TransformerFactory.newDefaultInstance().newTransformer().transform(new DOMSource(node), new StreamResult(byteArrayOutputStream));
return byteArrayOutputStream.toString();
} catch (TransformerException te) {
fail();
Expand Down

0 comments on commit d823e36

Please sign in to comment.