use of javax.xml.stream.XMLInputFactory in project Activiti by Activiti.
the class ImportUploadReceiver method deployUploadedFile.
protected void deployUploadedFile() {
try {
try {
if (fileName.endsWith(".bpmn20.xml") || fileName.endsWith(".bpmn")) {
validFile = true;
XMLInputFactory xif = XmlUtil.createSafeXmlInputFactory();
InputStreamReader in = new InputStreamReader(new ByteArrayInputStream(outputStream.toByteArray()), "UTF-8");
XMLStreamReader xtr = xif.createXMLStreamReader(in);
BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
if (bpmnModel.getMainProcess() == null || bpmnModel.getMainProcess().getId() == null) {
notificationManager.showErrorNotification(Messages.MODEL_IMPORT_FAILED, i18nManager.getMessage(Messages.MODEL_IMPORT_INVALID_BPMN_EXPLANATION));
} else {
if (bpmnModel.getLocationMap().isEmpty()) {
notificationManager.showErrorNotification(Messages.MODEL_IMPORT_INVALID_BPMNDI, i18nManager.getMessage(Messages.MODEL_IMPORT_INVALID_BPMNDI_EXPLANATION));
} else {
String processName = null;
if (StringUtils.isNotEmpty(bpmnModel.getMainProcess().getName())) {
processName = bpmnModel.getMainProcess().getName();
} else {
processName = bpmnModel.getMainProcess().getId();
}
modelData = repositoryService.newModel();
ObjectNode modelObjectNode = new ObjectMapper().createObjectNode();
modelObjectNode.put(MODEL_NAME, processName);
modelObjectNode.put(MODEL_REVISION, 1);
modelData.setMetaInfo(modelObjectNode.toString());
modelData.setName(processName);
repositoryService.saveModel(modelData);
BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
ObjectNode editorNode = jsonConverter.convertToJson(bpmnModel);
repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));
}
}
} else {
notificationManager.showErrorNotification(Messages.MODEL_IMPORT_INVALID_FILE, i18nManager.getMessage(Messages.MODEL_IMPORT_INVALID_FILE_EXPLANATION));
}
} catch (Exception e) {
String errorMsg = e.getMessage().replace(System.getProperty("line.separator"), "<br/>");
notificationManager.showErrorNotification(Messages.MODEL_IMPORT_FAILED, errorMsg);
}
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
notificationManager.showErrorNotification("Server-side error", e.getMessage());
}
}
}
}
use of javax.xml.stream.XMLInputFactory in project Activiti by Activiti.
the class DefaultProcessValidatorTest method verifyValidation.
@Test
public void verifyValidation() throws Exception {
InputStream xmlStream = this.getClass().getClassLoader().getResourceAsStream("org/activiti/engine/test/validation/invalidProcess.bpmn20.xml");
XMLInputFactory xif = XMLInputFactory.newInstance();
InputStreamReader in = new InputStreamReader(xmlStream, "UTF-8");
XMLStreamReader xtr = xif.createXMLStreamReader(in);
BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
Assert.assertNotNull(bpmnModel);
List<ValidationError> allErrors = processValidator.validate(bpmnModel);
Assert.assertEquals(65, allErrors.size());
// shortening it a bit
String setName = ValidatorSetNames.ACTIVITI_EXECUTABLE_PROCESS;
// isExecutable should be true
List<ValidationError> problems = findErrors(allErrors, setName, Problems.ALL_PROCESS_DEFINITIONS_NOT_EXECUTABLE, 1);
Assert.assertNotNull(problems.get(0).getValidatorSetName());
Assert.assertNotNull(problems.get(0).getProblem());
Assert.assertNotNull(problems.get(0).getDefaultDescription());
// Event listeners
problems = findErrors(allErrors, setName, Problems.EVENT_LISTENER_IMPLEMENTATION_MISSING, 1);
assertProcessElementError(problems.get(0));
problems = findErrors(allErrors, setName, Problems.EVENT_LISTENER_INVALID_THROW_EVENT_TYPE, 1);
assertProcessElementError(problems.get(0));
// Execution listeners
problems = findErrors(allErrors, setName, Problems.EXECUTION_LISTENER_IMPLEMENTATION_MISSING, 2);
assertProcessElementError(problems.get(0));
assertCommonProblemFieldForActivity(problems.get(1));
// Association
problems = findErrors(allErrors, setName, Problems.ASSOCIATION_INVALID_SOURCE_REFERENCE, 1);
assertProcessElementError(problems.get(0));
problems = findErrors(allErrors, setName, Problems.ASSOCIATION_INVALID_TARGET_REFERENCE, 1);
assertProcessElementError(problems.get(0));
// Signals
problems = findErrors(allErrors, setName, Problems.SIGNAL_MISSING_ID, 1);
assertCommonErrorFields(problems.get(0));
problems = findErrors(allErrors, setName, Problems.SIGNAL_MISSING_NAME, 2);
assertCommonErrorFields(problems.get(0));
assertCommonErrorFields(problems.get(1));
problems = findErrors(allErrors, setName, Problems.SIGNAL_DUPLICATE_NAME, 2);
assertCommonErrorFields(problems.get(0));
assertCommonErrorFields(problems.get(1));
problems = findErrors(allErrors, setName, Problems.SIGNAL_INVALID_SCOPE, 1);
assertCommonErrorFields(problems.get(0));
// Start event
problems = findErrors(allErrors, setName, Problems.START_EVENT_MULTIPLE_FOUND, 2);
assertCommonProblemFieldForActivity(problems.get(0));
assertCommonProblemFieldForActivity(problems.get(1));
problems = findErrors(allErrors, setName, Problems.START_EVENT_INVALID_EVENT_DEFINITION, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Sequence flow
problems = findErrors(allErrors, setName, Problems.SEQ_FLOW_INVALID_SRC, 1);
assertCommonProblemFieldForActivity(problems.get(0));
problems = findErrors(allErrors, setName, Problems.SEQ_FLOW_INVALID_TARGET, 2);
assertCommonProblemFieldForActivity(problems.get(0));
// User task
problems = findErrors(allErrors, setName, Problems.USER_TASK_LISTENER_IMPLEMENTATION_MISSING, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Service task
problems = findErrors(allErrors, setName, Problems.SERVICE_TASK_RESULT_VAR_NAME_WITH_DELEGATE, 1);
assertCommonProblemFieldForActivity(problems.get(0));
problems = findErrors(allErrors, setName, Problems.SERVICE_TASK_INVALID_TYPE, 1);
assertCommonProblemFieldForActivity(problems.get(0));
problems = findErrors(allErrors, setName, Problems.SERVICE_TASK_MISSING_IMPLEMENTATION, 1);
assertCommonProblemFieldForActivity(problems.get(0));
problems = findErrors(allErrors, setName, Problems.SERVICE_TASK_WEBSERVICE_INVALID_OPERATION_REF, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Send task
problems = findErrors(allErrors, setName, Problems.SEND_TASK_INVALID_IMPLEMENTATION, 1);
assertCommonProblemFieldForActivity(problems.get(0));
problems = findErrors(allErrors, setName, Problems.SEND_TASK_INVALID_TYPE, 1);
assertCommonProblemFieldForActivity(problems.get(0));
problems = findErrors(allErrors, setName, Problems.SEND_TASK_WEBSERVICE_INVALID_OPERATION_REF, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Mail task
problems = findErrors(allErrors, setName, Problems.MAIL_TASK_NO_RECIPIENT, 2);
assertCommonProblemFieldForActivity(problems.get(0));
assertCommonProblemFieldForActivity(problems.get(1));
problems = findErrors(allErrors, setName, Problems.MAIL_TASK_NO_CONTENT, 4);
assertCommonProblemFieldForActivity(problems.get(0));
assertCommonProblemFieldForActivity(problems.get(1));
// Shell task
problems = findErrors(allErrors, setName, Problems.SHELL_TASK_NO_COMMAND, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Script task
problems = findErrors(allErrors, setName, Problems.SCRIPT_TASK_MISSING_SCRIPT, 2);
assertCommonProblemFieldForActivity(problems.get(0));
assertCommonProblemFieldForActivity(problems.get(1));
// Exclusive gateway
problems = findErrors(allErrors, setName, Problems.EXCLUSIVE_GATEWAY_CONDITION_NOT_ALLOWED_ON_SINGLE_SEQ_FLOW, 1);
assertCommonProblemFieldForActivity(problems.get(0));
problems = findErrors(allErrors, setName, Problems.EXCLUSIVE_GATEWAY_CONDITION_ON_DEFAULT_SEQ_FLOW, 1);
assertCommonProblemFieldForActivity(problems.get(0));
problems = findErrors(allErrors, setName, Problems.EXCLUSIVE_GATEWAY_NO_OUTGOING_SEQ_FLOW, 1);
assertCommonProblemFieldForActivity(problems.get(0));
problems = findErrors(allErrors, setName, Problems.EXCLUSIVE_GATEWAY_SEQ_FLOW_WITHOUT_CONDITIONS, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Event gateway
problems = findErrors(allErrors, setName, Problems.EVENT_GATEWAY_ONLY_CONNECTED_TO_INTERMEDIATE_EVENTS, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Subprocesses
problems = findErrors(allErrors, setName, Problems.SUBPROCESS_MULTIPLE_START_EVENTS, 1);
assertCommonProblemFieldForActivity(problems.get(0));
problems = findErrors(allErrors, setName, Problems.SUBPROCESS_START_EVENT_EVENT_DEFINITION_NOT_ALLOWED, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Event subprocesses
problems = findErrors(allErrors, setName, Problems.EVENT_SUBPROCESS_INVALID_START_EVENT_DEFINITION, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Boundary events
problems = findErrors(allErrors, setName, Problems.BOUNDARY_EVENT_NO_EVENT_DEFINITION, 1);
assertCommonProblemFieldForActivity(problems.get(0));
problems = findErrors(allErrors, setName, Problems.BOUNDARY_EVENT_CANCEL_ONLY_ON_TRANSACTION, 1);
assertCommonProblemFieldForActivity(problems.get(0));
problems = findErrors(allErrors, setName, Problems.BOUNDARY_EVENT_MULTIPLE_CANCEL_ON_TRANSACTION, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Intermediate catch event
problems = findErrors(allErrors, setName, Problems.INTERMEDIATE_CATCH_EVENT_NO_EVENTDEFINITION, 1);
assertCommonProblemFieldForActivity(problems.get(0));
problems = findErrors(allErrors, setName, Problems.INTERMEDIATE_CATCH_EVENT_INVALID_EVENTDEFINITION, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Intermediate throw event
problems = findErrors(allErrors, setName, Problems.THROW_EVENT_INVALID_EVENTDEFINITION, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Multi instance
problems = findErrors(allErrors, setName, Problems.MULTI_INSTANCE_MISSING_COLLECTION, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Message events
problems = findErrors(allErrors, setName, Problems.MESSAGE_EVENT_MISSING_MESSAGE_REF, 2);
assertCommonProblemFieldForActivity(problems.get(0));
assertCommonProblemFieldForActivity(problems.get(1));
// Signal events
problems = findErrors(allErrors, setName, Problems.SIGNAL_EVENT_MISSING_SIGNAL_REF, 1);
assertCommonProblemFieldForActivity(problems.get(0));
problems = findErrors(allErrors, setName, Problems.SIGNAL_EVENT_INVALID_SIGNAL_REF, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Compensate event
problems = findErrors(allErrors, setName, Problems.COMPENSATE_EVENT_INVALID_ACTIVITY_REF, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Timer event
problems = findErrors(allErrors, setName, Problems.EVENT_TIMER_MISSING_CONFIGURATION, 2);
assertCommonProblemFieldForActivity(problems.get(0));
// Data association
problems = findErrors(allErrors, setName, Problems.DATA_ASSOCIATION_MISSING_TARGETREF, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Data object
problems = findErrors(allErrors, setName, Problems.DATA_OBJECT_MISSING_NAME, 2);
assertCommonErrorFields(problems.get(0));
assertCommonErrorFields(problems.get(1));
// End event
problems = findErrors(allErrors, setName, Problems.END_EVENT_CANCEL_ONLY_INSIDE_TRANSACTION, 1);
assertCommonProblemFieldForActivity(problems.get(0));
// Messages
problems = findErrors(allErrors, setName, Problems.MESSAGE_INVALID_ITEM_REF, 1);
assertCommonErrorFields(problems.get(0));
}
use of javax.xml.stream.XMLInputFactory in project midpoint by Evolveum.
the class Validator method validate.
public void validate(InputStream inputStream, OperationResult validatorResult, String objectResultOperationName) {
XMLStreamReader stream = null;
try {
Map<String, String> rootNamespaceDeclarations = new HashMap<String, String>();
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
stream = xmlInputFactory.createXMLStreamReader(inputStream);
int eventType = stream.nextTag();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (!QNameUtil.match(stream.getName(), SchemaConstants.C_OBJECTS)) {
// This has to be an import file with a single objects. Try
// to process it.
OperationResult objectResult = validatorResult.createSubresult(objectResultOperationName);
progress++;
objectResult.addContext(OperationResult.CONTEXT_PROGRESS, progress);
EventResult cont = null;
try {
cont = readFromStreamAndValidate(stream, objectResult, rootNamespaceDeclarations, validatorResult);
} catch (RuntimeException e) {
// Make sure that unexpected error is recorded.
objectResult.recordFatalError(e);
throw e;
}
if (!cont.isCont()) {
String message = null;
if (cont.getReason() != null) {
message = cont.getReason();
} else {
message = "Object validation failed (no reason given)";
}
if (objectResult.isUnknown()) {
objectResult.recordFatalError(message);
}
validatorResult.recordFatalError(message);
return;
}
// return to avoid processing objects in loop
validatorResult.computeStatus("Validation failed", "Validation warnings");
return;
}
// Extract root namespace declarations
for (int i = 0; i < stream.getNamespaceCount(); i++) {
rootNamespaceDeclarations.put(stream.getNamespacePrefix(i), stream.getNamespaceURI(i));
}
} else {
throw new SystemException("StAX Malfunction?");
}
while (stream.hasNext()) {
eventType = stream.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
OperationResult objectResult = validatorResult.createSubresult(objectResultOperationName);
progress++;
objectResult.addContext(OperationResult.CONTEXT_PROGRESS, progress);
EventResult cont = null;
try {
// Read and validate individual object from the stream
cont = readFromStreamAndValidate(stream, objectResult, rootNamespaceDeclarations, validatorResult);
} catch (RuntimeException e) {
if (objectResult.isUnknown()) {
// Make sure that unexpected error is recorded.
objectResult.recordFatalError(e);
}
throw e;
}
if (objectResult.isError()) {
errors++;
}
objectResult.cleanupResult();
validatorResult.summarize();
if (cont.isStop()) {
if (cont.getReason() != null) {
validatorResult.recordFatalError("Processing has been stopped: " + cont.getReason());
} else {
validatorResult.recordFatalError("Processing has been stopped");
}
// processed
return;
}
if (!cont.isCont()) {
if (stopAfterErrors > 0 && errors >= stopAfterErrors) {
validatorResult.recordFatalError("Too many errors (" + errors + ")");
return;
}
}
}
}
} catch (XMLStreamException ex) {
// validatorResult.recordFatalError("XML parsing error: " +
// ex.getMessage()+" on line "+stream.getLocation().getLineNumber(),ex);
validatorResult.recordFatalError("XML parsing error: " + ex.getMessage(), ex);
if (handler != null) {
handler.handleGlobalError(validatorResult);
}
return;
}
// Error count is sufficient. Detailed messages are in subresults
validatorResult.computeStatus(errors + " errors, " + (progress - errors) + " passed");
}
use of javax.xml.stream.XMLInputFactory in project midpoint by Evolveum.
the class MidpointFunctionsImpl method parseXmlToMap.
@Override
public Map<String, String> parseXmlToMap(String xml) {
Map<String, String> resultingMap = new HashMap<String, String>();
if (xml != null && !xml.isEmpty()) {
XMLInputFactory factory = XMLInputFactory.newInstance();
String value = "";
String startName = "";
InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
boolean isRootElement = true;
try {
XMLEventReader eventReader = factory.createXMLEventReader(stream);
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
Integer code = event.getEventType();
if (code == XMLStreamConstants.START_ELEMENT) {
StartElement startElement = event.asStartElement();
startName = startElement.getName().getLocalPart();
if (!isRootElement) {
resultingMap.put(startName, null);
} else {
isRootElement = false;
}
} else if (code == XMLStreamConstants.CHARACTERS) {
Characters characters = event.asCharacters();
if (!characters.isWhiteSpace()) {
StringBuilder valueBuilder;
if (value != null) {
valueBuilder = new StringBuilder(value).append(" ").append(characters.getData().toString());
} else {
valueBuilder = new StringBuilder(characters.getData().toString());
}
value = valueBuilder.toString();
}
} else if (code == XMLStreamConstants.END_ELEMENT) {
EndElement endElement = event.asEndElement();
String endName = endElement.getName().getLocalPart();
if (endName.equals(startName)) {
if (value != null) {
resultingMap.put(endName, value);
value = null;
}
} else {
LOGGER.trace("No value between xml tags, tag name : {0}", endName);
}
} else if (code == XMLStreamConstants.END_DOCUMENT) {
isRootElement = true;
}
}
} catch (XMLStreamException e) {
StringBuilder error = new StringBuilder("Xml stream exception wile parsing xml string").append(e.getLocalizedMessage());
throw new SystemException(error.toString());
}
} else {
LOGGER.trace("Input xml string null or empty.");
}
return resultingMap;
}
use of javax.xml.stream.XMLInputFactory in project jdk8u_jdk by JetBrains.
the class SurrogatesTest method readXML.
// Reads generated XML data and check if it contains expected
// text in writeCharactersWithString and writeCharactersWithArray
// elements
private void readXML(byte[] xmlData, String expectedContent) throws Exception {
InputStream stream = new ByteArrayInputStream(xmlData);
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader xmlReader = factory.createXMLStreamReader(stream);
boolean inTestElement = false;
StringBuilder sb = new StringBuilder();
while (xmlReader.hasNext()) {
String ename;
switch(xmlReader.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
ename = xmlReader.getLocalName();
if (ename.equals("writeCharactersWithString") || ename.equals("writeCharactersWithArray")) {
inTestElement = true;
}
break;
case XMLStreamConstants.END_ELEMENT:
ename = xmlReader.getLocalName();
if (ename.equals("writeCharactersWithString") || ename.equals("writeCharactersWithArray")) {
inTestElement = false;
String content = sb.toString();
System.out.println(ename + " text:'" + content + "' expected:'" + expectedContent + "'");
Assert.assertEquals(content, expectedContent);
sb.setLength(0);
}
break;
case XMLStreamConstants.CHARACTERS:
if (inTestElement) {
sb.append(xmlReader.getText());
}
break;
}
xmlReader.next();
}
}
Aggregations