Example #1
0
  /**
   * Creates a "mini-template" with a given tag and an optional child tag, then evaluates
   * it recursively.
   *
   * <p>This method is used to map certain tags as combinations of other tags (as in <a
   * href="http://aitools.org/aiml/TR/2001/WD-aiml/#section-short-cut-elements">short-cut elements
   * </a>).
   *
   * @param element the element to modify
   * @param newElementName the new name to give the element
   * @param childContent the name or content for the child to add
   * @param childType the type of the child
   * @return the result of processing this structure
   * @throws ProcessorException if there is an error in processing
   */
  public String shortcutTag(
      Element element,
      String newElementName,
      String childContent,
      Class<? extends Content> childType)
      throws ProcessorException {
    String response = "";

    // If the node is empty, we need not continue.
    if (element == null) {
      return "";
    }

    /*
     * Process children (if any). Clearly, the root tag cannot have an empty type, and the children must exist.
     */
    if ((!"".equals(childContent)) && ((childType == Element.class) || (childType == Text.class))) {
      Element newElement = new Element(newElementName, element.getNamespaceURI());
      /*
       * Create an XML node for the child tag. Note that we assume that the child is an empty tag with no
       * attributes. This is reasonable for AIML 1.0.1, but might not always be.
       */
      if (childType == Element.class) {
        newElement.addContent(new Element(childContent, element.getNamespaceURI()));
      } else if (childType == Text.class) {
        newElement.setText(childContent);
      }

      // Now evaluate the node, just as if it came from the original AIML.
      response = response + evaluate(newElement);
    }

    return response;
  }
Example #2
0
  /**
   * Recursively evaluates an element.
   *
   * @param element the element
   * @return the result of processing the element
   * @throws ProcessorException if there is an error in processing
   */
  @SuppressWarnings("unchecked")
  public String evaluate(Element element) throws ProcessorException {
    // Is it a valid element?
    if (element == null) {
      return "";
    }

    // Search for the tag in the processor registry.
    Class<? extends P> processorClass = null;

    String elementNamespaceURI = element.getNamespaceURI();
    Document elementDocument = element.getDocument();
    boolean emitXMLNS =
        elementDocument != null
            && (element.equals(element.getDocument().getRootElement())
                || (elementNamespaceURI != null
                    && !elementNamespaceURI.equals(
                        element.getDocument().getRootElement().getNamespaceURI())));
    if (elementNamespaceURI == null
        || this._registry.getNamespaceURI().equals(elementNamespaceURI)) {
      processorClass = this._registry.get(element.getName());

      // Process the element with a new instance of the processor.
      return Classes.getNewInstance(processorClass, "Processor", this._core).process(element, this);
    }
    // otherwise (if this element is from a different namespace)
    if (element.getContent().size() == 0) {
      return JDOM.renderEmptyElement(element, emitXMLNS);
    }
    // otherwise...
    return JDOM.renderStartTag(element, emitXMLNS)
        + evaluate(element.getContent())
        + JDOM.renderEndTag(element);
  }
Example #3
0
  /**
   * This will invoke the <code>startElement</code> callback in the <code>ContentHandler</code>.
   *
   * @param element <code>Element</code> used in callbacks.
   * @param nsAtts <code>List</code> of namespaces to declare with the element or <code>null</code>.
   */
  private void startElement(Element element, Attributes nsAtts) throws JDOMException {
    String namespaceURI = element.getNamespaceURI();
    String localName = element.getName();
    String rawName = element.getQualifiedName();

    // Allocate attribute list.
    AttributesImpl atts = (nsAtts != null) ? new AttributesImpl(nsAtts) : new AttributesImpl();

    List attributes = element.getAttributes();
    Iterator i = attributes.iterator();
    while (i.hasNext()) {
      Attribute a = (Attribute) i.next();
      atts.addAttribute(
          a.getNamespaceURI(),
          a.getName(),
          a.getQualifiedName(),
          getAttributeTypeName(a.getAttributeType()),
          a.getValue());
    }

    try {
      contentHandler.startElement(namespaceURI, localName, rawName, atts);
    } catch (SAXException se) {
      throw new JDOMException("Exception in startElement", se);
    }
  }
Example #4
0
  public ProjectBuildConfigurator createProjectConfigurator(
      String url, File buildSpecFile, Document xmlDocument) throws ConfigException {
    if (xmlDocument == null) {
      return null;
    }

    final Element root = xmlDocument.getRootElement();

    if (!"".equals(root.getNamespaceURI())) {
      return null;
    }

    if (url.endsWith(".build")) {
      // probably a NAnt project.
      return null;
    }

    if (!"project".equals(root.getName())) {
      return null;
    }

    final String projectName = root.getAttributeValue("name");
    final String basedir = root.getAttributeValue("basedir");

    if (projectName == null) {
      return null;
    }

    return new AntProjectBuildConfigurator(applicationContext, projectName, basedir);
  }
Example #5
0
  @Override
  public void perform(Element originalElement, Element patchElement, Element outputParentElement)
      throws AbstractXmlMergeException {

    logger.fine("Merging: " + originalElement + " (original) and " + patchElement + " (patch)");

    Mapper mapper = (Mapper) m_mapperFactory.getOperation(originalElement, patchElement);

    if (originalElement == null) {
      outputParentElement.addContent(mapper.map(patchElement));
    } else if (patchElement == null) {
      outputParentElement.addContent((Content) originalElement.clone());
    } else {

      Element workingElement =
          new Element(
              originalElement.getName(),
              originalElement.getNamespacePrefix(),
              originalElement.getNamespaceURI());
      addAttributes(workingElement, originalElement);

      logger.fine("Adding " + workingElement);
      outputParentElement.addContent(workingElement);

      doIt(workingElement, originalElement, patchElement);
    }
  }
Example #6
0
  /**
   * This will invoke the <code>endElement</code> callback in the <code>ContentHandler</code>.
   *
   * @param element <code>Element</code> used in callbacks.
   */
  private void endElement(Element element) throws JDOMException {
    String namespaceURI = element.getNamespaceURI();
    String localName = element.getName();
    String rawName = element.getQualifiedName();

    try {
      contentHandler.endElement(namespaceURI, localName, rawName);
    } catch (SAXException se) {
      throw new JDOMException("Exception in endElement", se);
    }
  }
 void getChildren(Object node, String localName, String namespaceUri, List result) {
   if (node instanceof Element) {
     Element e = (Element) node;
     if (localName == null) {
       result.addAll(e.getChildren());
     } else {
       result.addAll(e.getChildren(localName, Namespace.getNamespace("", namespaceUri)));
     }
   } else if (node instanceof Document) {
     Element root = ((Document) node).getRootElement();
     if (localName == null
         || (equal(root.getName(), localName) && equal(root.getNamespaceURI(), namespaceUri))) {
       result.add(root);
     }
   }
 }
  private Set<QName> getProps(Document doc) {
    Element elProp = doc.getRootElement().getChild("prop", NS_DAV);
    if (elProp == null) {
      throw new RuntimeException("No prop element");
    }

    Set<QName> set = new HashSet<QName>();
    for (Object o : elProp.getChildren()) {
      if (o instanceof Element) {
        Element el = (Element) o;
        String local = el.getName();
        String ns = el.getNamespaceURI();
        set.add(new QName(ns, local, el.getNamespacePrefix()));
      }
    }
    return set;
  }
  static void rawGetPrefixes(Element element, String namespaceURI, List<String> prefixes) {
    if (element.getNamespaceURI().equals(namespaceURI)) {
      prefixes.add(element.getNamespacePrefix());
    }

    List<?> namespaces = element.getAdditionalNamespaces();

    for (Iterator<?> itr = namespaces.iterator(); itr.hasNext(); ) {
      Namespace ns = (Namespace) itr.next();

      if (ns.getURI().equals(namespaceURI)) {
        prefixes.add(ns.getPrefix());
      }
    }

    if (element.getParentElement() != null) {
      rawGetPrefixes(element.getParentElement(), namespaceURI, prefixes);
    }
  }
  public static String rawGetPrefix(Element element, String namespaceURI) {
    if (element.getNamespaceURI().equals(namespaceURI)) {
      return element.getNamespacePrefix();
    }

    List<?> namespaces = element.getAdditionalNamespaces();

    for (Iterator<?> itr = namespaces.iterator(); itr.hasNext(); ) {
      Namespace ns = (Namespace) itr.next();

      if (ns.getURI().equals(namespaceURI)) {
        return ns.getPrefix();
      }
    }

    if (element.getParentElement() != null) {
      return rawGetPrefix(element.getParentElement(), namespaceURI);
    } else {
      return null;
    }
  }
  protected List extractForeignMarkup(Element e, Extendable ext, Namespace basens) {
    ArrayList foreignMarkup = new ArrayList();
    Iterator children = e.getChildren().iterator();
    while (children.hasNext()) {
      Element elem = (Element) children.next();
      if (
      // if elemet not in the RSS namespace
      !basens.equals(elem.getNamespace())
          // and elem was not handled by a module
          && null == ext.getModule(elem.getNamespaceURI())) {

        // save it as foreign markup,
        // but we can't detach it while we're iterating
        foreignMarkup.add(elem.clone());
      }
    }
    // Now we can detach the foreign markup elements
    Iterator fm = foreignMarkup.iterator();
    while (fm.hasNext()) {
      Element elem = (Element) fm.next();
      elem.detach();
    }
    return foreignMarkup;
  }