Exemplo n.º 1
0
  public BpmnModel convertToBpmnModel(XMLStreamReader xtr) {
    BpmnModel model = new BpmnModel();
    try {
      Process activeProcess = null;
      List<SubProcess> activeSubProcessList = new ArrayList<SubProcess>();
      while (xtr.hasNext()) {
        try {
          xtr.next();
        } catch (Exception e) {
          LOGGER.error("Error reading XML document", e);
          throw new XMLException("Error reading XML", e);
        }

        if (xtr.isEndElement() && ELEMENT_SUBPROCESS.equals(xtr.getLocalName())) {
          activeSubProcessList.remove(activeSubProcessList.size() - 1);
        }

        if (xtr.isEndElement() && ELEMENT_TRANSACTION.equals(xtr.getLocalName())) {
          activeSubProcessList.remove(activeSubProcessList.size() - 1);
        }

        if (xtr.isStartElement() == false) continue;

        if (ELEMENT_DEFINITIONS.equals(xtr.getLocalName())) {

          model.setTargetNamespace(xtr.getAttributeValue(null, TARGET_NAMESPACE_ATTRIBUTE));
          for (int i = 0; i < xtr.getNamespaceCount(); i++) {
            String prefix = xtr.getNamespacePrefix(i);
            if (prefix != null) {
              model.addNamespace(prefix, xtr.getNamespaceURI(i));
            }
          }

        } else if (ELEMENT_SIGNAL.equals(xtr.getLocalName())) {
          new SignalParser().parse(xtr, model);

        } else if (ELEMENT_MESSAGE.equals(xtr.getLocalName())) {
          new MessageParser().parse(xtr, model);

        } else if (ELEMENT_ERROR.equals(xtr.getLocalName())) {

          if (StringUtils.isNotEmpty(xtr.getAttributeValue(null, ATTRIBUTE_ID))) {
            model.addError(
                xtr.getAttributeValue(null, ATTRIBUTE_ID),
                xtr.getAttributeValue(null, ATTRIBUTE_ERROR_CODE));
          }

        } else if (ELEMENT_IMPORT.equals(xtr.getLocalName())) {
          new ImportParser().parse(xtr, model);

        } else if (ELEMENT_ITEM_DEFINITION.equals(xtr.getLocalName())) {
          new ItemDefinitionParser().parse(xtr, model);

        } else if (ELEMENT_INTERFACE.equals(xtr.getLocalName())) {
          new InterfaceParser().parse(xtr, model);

        } else if (ELEMENT_IOSPECIFICATION.equals(xtr.getLocalName())) {
          new IOSpecificationParser().parseChildElement(xtr, activeProcess, model);

        } else if (ELEMENT_PARTICIPANT.equals(xtr.getLocalName())) {

          if (StringUtils.isNotEmpty(xtr.getAttributeValue(null, ATTRIBUTE_ID))) {
            Pool pool = new Pool();
            pool.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID));
            pool.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME));
            pool.setProcessRef(xtr.getAttributeValue(null, ATTRIBUTE_PROCESS_REF));
            model.getPools().add(pool);
          }

        } else if (ELEMENT_PROCESS.equals(xtr.getLocalName())) {

          Process process = new ProcessParser().parse(xtr, model);
          if (process != null) {
            activeProcess = process;
          }

        } else if (ELEMENT_POTENTIAL_STARTER.equals(xtr.getLocalName())) {
          new PotentialStarterParser().parse(xtr, activeProcess);

        } else if (ELEMENT_LANE.equals(xtr.getLocalName())) {
          new LaneParser().parse(xtr, activeProcess);

        } else if (ELEMENT_DOCUMENTATION.equals(xtr.getLocalName())) {

          BaseElement parentElement = null;
          if (activeSubProcessList.size() > 0) {
            parentElement = activeSubProcessList.get(activeSubProcessList.size() - 1);
          } else if (activeProcess != null) {
            parentElement = activeProcess;
          }
          new DocumentationParser().parseChildElement(xtr, parentElement, model);

        } else if (ELEMENT_SUBPROCESS.equals(xtr.getLocalName())) {

          new SubProcessParser().parse(xtr, activeSubProcessList, activeProcess);

        } else if (ELEMENT_TRANSACTION.equals(xtr.getLocalName())) {

          new SubProcessParser().parse(xtr, activeSubProcessList, activeProcess);

        } else if (ELEMENT_DI_SHAPE.equals(xtr.getLocalName())) {

          new BpmnShapeParser().parse(xtr, model);

        } else if (ELEMENT_DI_EDGE.equals(xtr.getLocalName())) {

          new BpmnEdgeParser().parse(xtr, model);

        } else if (ELEMENT_EXECUTION_LISTENER.equals(xtr.getLocalName())) {

          new ExecutionListenerParser().parseChildElement(xtr, activeProcess, model);

        } else {

          if (activeSubProcessList.size() > 0
              && ELEMENT_EXTENSIONS.equalsIgnoreCase(xtr.getLocalName())) {
            new ExecutionListenerParser()
                .parseChildElement(
                    xtr, activeSubProcessList.get(activeSubProcessList.size() - 1), model);

          } else if (activeSubProcessList.size() > 0
              && ELEMENT_MULTIINSTANCE.equalsIgnoreCase(xtr.getLocalName())) {

            new MultiInstanceParser()
                .parseChildElement(
                    xtr, activeSubProcessList.get(activeSubProcessList.size() - 1), model);

          } else if (convertersToBpmnMap.containsKey(xtr.getLocalName())) {
            if (activeProcess.isExecutable()) {
              Class<? extends BaseBpmnXMLConverter> converterClass =
                  convertersToBpmnMap.get(xtr.getLocalName());
              BaseBpmnXMLConverter converter = converterClass.newInstance();
              if (userTaskFormTypes != null && ELEMENT_TASK_USER.equals(xtr.getLocalName())) {
                UserTaskXMLConverter userTaskConverter = (UserTaskXMLConverter) converter;
                for (String formType : userTaskFormTypes) {
                  userTaskConverter.addFormType(formType);
                }
              } else if (startEventFormTypes != null
                  && ELEMENT_EVENT_START.equals(xtr.getLocalName())) {
                StartEventXMLConverter startEventConverter = (StartEventXMLConverter) converter;
                for (String formType : startEventFormTypes) {
                  startEventConverter.addFormType(formType);
                }
              }
              converter.convertToBpmnModel(xtr, model, activeProcess, activeSubProcessList);
            }
          }
        }
      }

      for (Process process : model.getProcesses()) {
        processFlowElements(process.getFlowElements(), process);
      }

    } catch (Exception e) {
      LOGGER.error("Error processing BPMN document", e);
      throw new XMLException("Error processing BPMN document", e);
    }
    return model;
  }