Ejemplo n.º 1
0
    /**
     * Get the DTD elements whose content spec is represented in the DTD DOM by the specified
     * runtime class.
     *
     * <p>Runtimes - DTDEmpty, DTDAny, DTDMixed, DTDPCData
     *
     * @param dtdItemRuntime - DTDEmpty, DTDAny, DTDMixed, DTDPCData
     * @param isInstance Desired results of the isinstance check.
     * @return Array of elements names.
     */
    private String[] getElements(Class dtdItemRuntime, boolean isInstance) {
      Iterator iterator;
      Vector elements = new Vector();
      String[] returnVal;

      iterator = wutkaDTDObject.elements.entrySet().iterator();
      while (iterator.hasNext()) {
        Map.Entry element = (Map.Entry) iterator.next();
        com.wutka.dtd.DTDElement dtdElement = (com.wutka.dtd.DTDElement) element.getValue();

        if (dtdItemRuntime.isInstance(dtdElement.getContent()) == isInstance) {
          elements.addElement(dtdElement.getName());
        }
      }

      returnVal = new String[elements.size()];
      elements.toArray(returnVal);

      return returnVal;
    }
Ejemplo n.º 2
0
  /** {@inheritDoc} */
  public void perform(Element originalElement, Element patchElement, Element outputParentElement)
      throws AbstractXmlMergeException {

    Element element;

    if (originalElement != null) {
      element = (Element) originalElement.clone();
    } else {
      element = (Element) patchElement.clone();
    }

    DTD dtd = getDTD(outputParentElement);

    List<DTDElement> dtdElements = dtd.getItemsByType(DTDElement.class);

    // Find the corresponding element
    DTDElement parentDtdElement = null;
    for (DTDElement dtdElement : dtdElements) {
      if (dtdElement.getName().equals(outputParentElement.getName())) {
        parentDtdElement = dtdElement;
      }
    }

    if (parentDtdElement == null) {
      throw new ElementException(
          element, "Element " + outputParentElement.getName() + " not defined in DTD");
    } else {

      DTDItem item = parentDtdElement.getContent();

      if (item instanceof DTDAny) {
        // the parent element accepts anything in any order
        outputParentElement.addContent(element);
      } else if (item instanceof DTDContainer) {

        // List existing elements in output parent element
        List<Element> existingChildren = outputParentElement.getChildren();

        if (existingChildren.size() == 0) {
          // This is the first child
          outputParentElement.addContent(element);
        } else {

          List<String> orderedDtdElements = getOrderedDtdElements((DTDContainer) item);

          int indexOfNewElementInDtd = orderedDtdElements.indexOf(element.getName());
          Debug.log("index of element " + element.getName() + ": " + indexOfNewElementInDtd);

          int pos = existingChildren.size();

          // Calculate the position in the parent where we insert the
          // element
          for (int i = 0; i < existingChildren.size(); i++) {
            String elementName = (existingChildren.get(i)).getName();
            Debug.log(
                "index of child " + elementName + ": " + orderedDtdElements.indexOf(elementName));
            if (orderedDtdElements.indexOf(elementName) > indexOfNewElementInDtd) {
              pos = i;
              break;
            }
          }

          Debug.log("adding element " + element.getName() + " add in pos " + pos);
          outputParentElement.addContent(pos, element);
        }
      }
    }
  }