/
XmlProcessReader.java
138 lines (119 loc) · 5.09 KB
/
XmlProcessReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jbpm.compiler.xml;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.List;
import java.util.LinkedList;
import java.text.MessageFormat;
import javax.xml.parsers.SAXParser;
import org.kie.api.definition.process.Process;
import org.drools.core.xml.ExtensibleXmlParser;
import org.drools.core.xml.SemanticModules;
import org.jbpm.ruleflow.core.RuleFlowProcess;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
//processId, processPkg, processName, processVersion
public class XmlProcessReader {
private ExtensibleXmlParser parser;
private final MessageFormat message = new java.text.MessageFormat("Node Info: id:{0} name:{1} \n" +
"Parser message: {2}");
private final MessageFormat messageWithProcessInfo = new java.text.MessageFormat("Process Info: id:{0}, pkg:{1}, name:{2}, version:{3} \n" +
"Node Info: id:{4} name:{5} \n" +
"Parser message: {6}");
private List<Process> processes;
public XmlProcessReader(final SemanticModules modules, ClassLoader classLoader) {
this( modules, classLoader, null );
}
public XmlProcessReader(final SemanticModules modules, ClassLoader classLoader, final SAXParser parser) {
this.parser = new ExtensibleXmlParser() {
@Override
protected String buildPrintMessage(final SAXParseException x) {
return processParserMessage(super.getParents(), super.getAttrs(), super.buildPrintMessage(x));
}
};
if(parser != null) {
this.parser.setParser(parser);
}
this.parser.setSemanticModules( modules );
this.parser.setData( new ProcessBuildData() );
this.parser.setClassLoader( classLoader );
}
/**
* Read a <code>Process</code> from a <code>Reader</code>.
*
* @param reader
* The reader containing the rule-set.
*
* @return The rule-set.
*/
public List<Process> read(final Reader reader) throws SAXException,
IOException {
this.processes = ((ProcessBuildData) this.parser.read( reader )).getProcesses();
return this.processes;
}
/**
* Read a <code>Process</code> from an <code>InputStream</code>.
*
* @param inputStream
* The input-stream containing the rule-set.
*
* @return The rule-set.
*/
public List<Process> read(final InputStream inputStream) throws SAXException,
IOException {
this.processes = ((ProcessBuildData) this.parser.read( inputStream )).getProcesses();
return this.processes;
}
/**
* Read a <code>Process</code> from an <code>InputSource</code>.
*
* @param in
* The rule-set input-source.
*
* @return The rule-set.
*/
public List<Process> read(final InputSource in) throws SAXException,
IOException {
this.processes = ((ProcessBuildData)this.parser.read( in )).getProcesses();
return this.processes;
}
void setProcesses(final List<Process> processes) {
this.processes = processes;
}
public List<Process> getProcess() {
return this.processes;
}
public ProcessBuildData getProcessBuildData() {
return (ProcessBuildData) this.parser.getData();
}
protected String processParserMessage(LinkedList<Object> parents, Attributes attr, String errorMessage) {
String nodeId = (attr == null || attr.getValue("id") == null) ? "" : attr.getValue("id");
String nodeName = (attr == null || attr.getValue("name") == null) ? "" : attr.getValue("name");
for(Object parent : parents) {
if(parent != null && parent instanceof RuleFlowProcess) {
RuleFlowProcess process = ((RuleFlowProcess) parent);
return messageWithProcessInfo.format(new Object[] {process.getId(),
process.getPackageName(),
process.getName(),
process.getVersion(), nodeId, nodeName, errorMessage});
}
}
return message.format(new Object[] {nodeId, nodeName, errorMessage});
}
}