示例#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;
  }
  public static void writeBPMNDI(BpmnModel model, XMLStreamWriter xtw) throws Exception {
    // BPMN DI information
    xtw.writeStartElement(BPMNDI_PREFIX, ELEMENT_DI_DIAGRAM, BPMNDI_NAMESPACE);

    String processId = null;
    if (model.getPools().size() > 0) {
      processId = "Collaboration";
    } else {
      processId = model.getMainProcess().getId();
    }

    xtw.writeAttribute(ATTRIBUTE_ID, "BPMNDiagram_" + processId);

    xtw.writeStartElement(BPMNDI_PREFIX, ELEMENT_DI_PLANE, BPMNDI_NAMESPACE);
    xtw.writeAttribute(ATTRIBUTE_DI_BPMNELEMENT, processId);
    xtw.writeAttribute(ATTRIBUTE_ID, "BPMNPlane_" + processId);

    // System.out.println("DI EXPORT");
    for (String elementId : model.getLocationMap().keySet()) {

      if (model.getFlowElement(elementId) != null
          || model.getArtifact(elementId) != null
          || model.getPool(elementId) != null
          || model.getLane(elementId) != null) {

        xtw.writeStartElement(BPMNDI_PREFIX, ELEMENT_DI_SHAPE, BPMNDI_NAMESPACE);
        xtw.writeAttribute(ATTRIBUTE_DI_BPMNELEMENT, elementId);
        xtw.writeAttribute(ATTRIBUTE_ID, "BPMNShape_" + elementId);

        GraphicInfo graphicInfo = model.getGraphicInfo(elementId);
        FlowElement flowElement = model.getFlowElement(elementId);
        if (flowElement != null
            && flowElement instanceof SubProcess
            && graphicInfo.getExpanded() != null) {
          xtw.writeAttribute(ATTRIBUTE_DI_IS_EXPANDED, String.valueOf(graphicInfo.getExpanded()));
        }

        if (flowElement == null) {
          boolean foundLane = false;
          for (Pool pool : model.getPools()) {
            if (foundLane) {
              break;
            }
            if (elementId.equals(pool.getId())) {
              if (graphicInfo.getHorizontal() != null) {
                xtw.writeAttribute(
                    ATTRIBUTE_DI_IS_HORIZONTAL, String.valueOf(graphicInfo.getHorizontal()));
              }
            } else {
              Process process = model.getProcess(pool.getId());
              if (process != null) {
                for (Lane lane : process.getLanes()) {
                  if (elementId.equals(lane.getId())) {
                    foundLane = true;
                    if (graphicInfo.getHorizontal() != null) {
                      xtw.writeAttribute(
                          ATTRIBUTE_DI_IS_HORIZONTAL, String.valueOf(graphicInfo.getHorizontal()));
                    }
                    break;
                  }
                }
              }
            }
          }
        }

        xtw.writeStartElement(OMGDC_PREFIX, ELEMENT_DI_BOUNDS, OMGDC_NAMESPACE);
        xtw.writeAttribute(ATTRIBUTE_DI_HEIGHT, "" + graphicInfo.getHeight());
        xtw.writeAttribute(ATTRIBUTE_DI_WIDTH, "" + graphicInfo.getWidth());
        xtw.writeAttribute(ATTRIBUTE_DI_X, "" + graphicInfo.getX());
        xtw.writeAttribute(ATTRIBUTE_DI_Y, "" + graphicInfo.getY());
        xtw.writeEndElement();

        xtw.writeEndElement();
      }
    }

    for (String elementId : model.getFlowLocationMap().keySet()) {

      if (model.getFlowElement(elementId) != null || model.getArtifact(elementId) != null) {

        xtw.writeStartElement(BPMNDI_PREFIX, ELEMENT_DI_EDGE, BPMNDI_NAMESPACE);
        xtw.writeAttribute(ATTRIBUTE_DI_BPMNELEMENT, elementId);
        xtw.writeAttribute(ATTRIBUTE_ID, "BPMNEdge_" + elementId);

        List<GraphicInfo> graphicInfoList = model.getFlowLocationGraphicInfo(elementId);
        for (GraphicInfo graphicInfo : graphicInfoList) {
          xtw.writeStartElement(OMGDI_PREFIX, ELEMENT_DI_WAYPOINT, OMGDI_NAMESPACE);
          xtw.writeAttribute(ATTRIBUTE_DI_X, "" + graphicInfo.getX());
          xtw.writeAttribute(ATTRIBUTE_DI_Y, "" + graphicInfo.getY());
          xtw.writeEndElement();
        }

        GraphicInfo labelGraphicInfo = model.getLabelGraphicInfo(elementId);
        FlowElement flowElement = model.getFlowElement(elementId);
        if (labelGraphicInfo != null
            && flowElement != null
            && StringUtils.isNotEmpty(flowElement.getName())) {
          xtw.writeStartElement(BPMNDI_PREFIX, ELEMENT_DI_LABEL, BPMNDI_NAMESPACE);
          xtw.writeStartElement(OMGDC_PREFIX, ELEMENT_DI_BOUNDS, OMGDC_NAMESPACE);
          xtw.writeAttribute(ATTRIBUTE_DI_HEIGHT, "" + labelGraphicInfo.getHeight());
          xtw.writeAttribute(ATTRIBUTE_DI_WIDTH, "" + labelGraphicInfo.getWidth());
          xtw.writeAttribute(ATTRIBUTE_DI_X, "" + labelGraphicInfo.getX());
          xtw.writeAttribute(ATTRIBUTE_DI_Y, "" + labelGraphicInfo.getY());
          xtw.writeEndElement();
          xtw.writeEndElement();
        }

        xtw.writeEndElement();
      }
    }

    // end BPMN DI elements
    xtw.writeEndElement();
    xtw.writeEndElement();
  }
  protected Point getMinXAndMinY(BpmnModel bpmnModel) {
    // We need to calculate maximum values to know how big the image will be in its entirety
    double theMinX = java.lang.Double.MAX_VALUE;
    double theMaxX = 0;
    double theMinY = java.lang.Double.MAX_VALUE;
    double theMaxY = 0;

    for (Pool pool : bpmnModel.getPools()) {
      GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(pool.getId());
      theMinX = graphicInfo.getX();
      theMaxX = graphicInfo.getX() + graphicInfo.getWidth();
      theMinY = graphicInfo.getY();
      theMaxY = graphicInfo.getY() + graphicInfo.getHeight();
    }

    List<FlowNode> flowNodes = gatherAllFlowNodes(bpmnModel);

    for (FlowNode flowNode : flowNodes) {
      GraphicInfo flowNodeGraphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());

      // width
      if ((flowNodeGraphicInfo.getX() + flowNodeGraphicInfo.getWidth()) > theMaxX) {
        theMaxX = flowNodeGraphicInfo.getX() + flowNodeGraphicInfo.getWidth();
      }

      if (flowNodeGraphicInfo.getX() < theMinX) {
        theMinX = flowNodeGraphicInfo.getX();
      }

      // height
      if ((flowNodeGraphicInfo.getY() + flowNodeGraphicInfo.getHeight()) > theMaxY) {
        theMaxY = flowNodeGraphicInfo.getY() + flowNodeGraphicInfo.getHeight();
      }

      if (flowNodeGraphicInfo.getY() < theMinY) {
        theMinY = flowNodeGraphicInfo.getY();
      }

      for (SequenceFlow sequenceFlow : flowNode.getOutgoingFlows()) {
        List<GraphicInfo> graphicInfoList =
            bpmnModel.getFlowLocationGraphicInfo(sequenceFlow.getId());

        for (GraphicInfo graphicInfo : graphicInfoList) {
          // width
          if (graphicInfo.getX() > theMaxX) {
            theMaxX = graphicInfo.getX();
          }

          if (graphicInfo.getX() < theMinX) {
            theMinX = graphicInfo.getX();
          }

          // height
          if (graphicInfo.getY() > theMaxY) {
            theMaxY = graphicInfo.getY();
          }

          if (graphicInfo.getY() < theMinY) {
            theMinY = graphicInfo.getY();
          }
        }
      }
    }

    List<Artifact> artifacts = gatherAllArtifacts(bpmnModel);

    for (Artifact artifact : artifacts) {
      GraphicInfo artifactGraphicInfo = bpmnModel.getGraphicInfo(artifact.getId());

      if (artifactGraphicInfo != null) {
        // width
        if ((artifactGraphicInfo.getX() + artifactGraphicInfo.getWidth()) > theMaxX) {
          theMaxX = artifactGraphicInfo.getX() + artifactGraphicInfo.getWidth();
        }

        if (artifactGraphicInfo.getX() < theMinX) {
          theMinX = artifactGraphicInfo.getX();
        }

        // height
        if ((artifactGraphicInfo.getY() + artifactGraphicInfo.getHeight()) > theMaxY) {
          theMaxY = artifactGraphicInfo.getY() + artifactGraphicInfo.getHeight();
        }

        if (artifactGraphicInfo.getY() < theMinY) {
          theMinY = artifactGraphicInfo.getY();
        }
      }

      List<GraphicInfo> graphicInfoList = bpmnModel.getFlowLocationGraphicInfo(artifact.getId());

      if (graphicInfoList != null) {
        for (GraphicInfo graphicInfo : graphicInfoList) {
          // width
          if (graphicInfo.getX() > theMaxX) {
            theMaxX = graphicInfo.getX();
          }

          if (graphicInfo.getX() < theMinX) {
            theMinX = graphicInfo.getX();
          }

          // height
          if (graphicInfo.getY() > theMaxY) {
            theMaxY = graphicInfo.getY();
          }

          if (graphicInfo.getY() < theMinY) {
            theMinY = graphicInfo.getY();
          }
        }
      }
    }

    int nrOfLanes = 0;

    for (org.activiti.bpmn.model.Process process : bpmnModel.getProcesses()) {
      for (Lane l : process.getLanes()) {
        nrOfLanes++;

        GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(l.getId());

        // // width
        if ((graphicInfo.getX() + graphicInfo.getWidth()) > theMaxX) {
          theMaxX = graphicInfo.getX() + graphicInfo.getWidth();
        }

        if (graphicInfo.getX() < theMinX) {
          theMinX = graphicInfo.getX();
        }

        // height
        if ((graphicInfo.getY() + graphicInfo.getHeight()) > theMaxY) {
          theMaxY = graphicInfo.getY() + graphicInfo.getHeight();
        }

        if (graphicInfo.getY() < theMinY) {
          theMinY = graphicInfo.getY();
        }
      }
    }

    // Special case, see http://jira.codehaus.org/browse/ACT-1431
    if ((flowNodes.size() == 0) && (bpmnModel.getPools().size() == 0) && (nrOfLanes == 0)) {
      // Nothing to show
      theMinX = 0;
      theMinY = 0;
    }

    return new Point((int) theMinX, (int) theMinY);
  }