public void testSetNamedItemNS8() throws Throwable {
    Document doc;
    NamedNodeMap attributes;
    NodeList elementList;
    Element element;
    Attr attr;

    doc = (Document) load("staffNS", builder);
    elementList = doc.getElementsByTagNameNS("*", "address");
    element = (Element) elementList.item(0);
    attributes = element.getAttributes();
    attr = (Attr) attributes.getNamedItemNS("http://www.usa.com", "domestic");
    element = (Element) elementList.item(1);
    attributes = element.getAttributes();

    {
      boolean success = false;
      try {
        attributes.setNamedItemNS(attr);
      } catch (DOMException ex) {
        success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
      }
      assertTrue("namednodemapsetnameditemns08", success);
    }
  }
  public void testSetNamedItemNS4() throws Throwable {
    Document doc;
    DOMImplementation domImpl;
    Document docAlt;
    DocumentType docType = null;

    NamedNodeMap attributes;
    NodeList elementList;
    Element element;
    Attr attrAlt;

    String nullNS = null;

    doc = (Document) load("staffNS", builder);
    elementList = doc.getElementsByTagNameNS("*", "address");
    element = (Element) elementList.item(1);
    attributes = element.getAttributes();
    domImpl = doc.getImplementation();
    docAlt = domImpl.createDocument(nullNS, "newDoc", docType);
    attrAlt = docAlt.createAttributeNS(nullNS, "street");

    {
      boolean success = false;
      try {
        attributes.setNamedItemNS(attrAlt);
      } catch (DOMException ex) {
        success = (ex.code == DOMException.WRONG_DOCUMENT_ERR);
      }
      assertTrue("throw_WRONG_DOCUMENT_ERR", success);
    }
  }
  public void testSetNamedItemNS3() throws Throwable {

    Document doc;
    Document docAlt;
    NamedNodeMap attributes;
    NamedNodeMap attributesAlt;
    NodeList elementList;
    NodeList elementListAlt;
    Element element;
    Element elementAlt;
    Attr attr;

    String nullNS = null;

    doc = (Document) load("staffNS", builder);
    elementList = doc.getElementsByTagNameNS("*", "address");
    element = (Element) elementList.item(1);
    attributes = element.getAttributes();
    docAlt = (Document) load("staffNS", builder);
    elementListAlt = docAlt.getElementsByTagNameNS("*", "address");
    elementAlt = (Element) elementListAlt.item(1);
    attributesAlt = elementAlt.getAttributes();
    attr = (Attr) attributesAlt.getNamedItemNS(nullNS, "street");
    attributesAlt.removeNamedItemNS(nullNS, "street");

    {
      boolean success = false;
      try {
        attributes.setNamedItemNS(attr);
      } catch (DOMException ex) {
        success = (ex.code == DOMException.WRONG_DOCUMENT_ERR);
      }
      assertTrue("throw_WRONG_DOCUMENT_ERR", success);
    }
  }
  /**
   * Runs the test case.
   *
   * @throws Throwable Any uncaught exception causes test to fail
   */
  public void runTest() throws Throwable {
    Document doc;
    NamedNodeMap notations;
    DocumentType docType;
    Node retval;
    Element elem;
    doc = (Document) load("hc_staff", true);
    docType = doc.getDoctype();

    if (!(("text/html".equals(getContentType())))) {
      assertNotNull("docTypeNotNull", docType);
      notations = docType.getNotations();
      assertNotNull("notationsNotNull", notations);
      elem = doc.createElementNS("http://www.w3.org/1999/xhtml", "br");

      try {
        retval = notations.setNamedItemNS(elem);
        fail("throw_HIER_OR_NO_MOD_ERR");

      } catch (DOMException ex) {
        switch (ex.code) {
          case 3:
            break;
          case 7:
            break;
          default:
            throw ex;
        }
      }
    }
  }
  /**
   * Runs the test case.
   *
   * @throws Throwable Any uncaught exception causes test to fail
   */
  public void runTest() throws Throwable {
    Document doc;
    DocumentType docType;
    NamedNodeMap entities;
    NamedNodeMap notations;
    Entity entity;
    Notation notation;
    Node newNode;
    String nullNS = null;

    doc = (Document) load("staffNS", true);
    docType = doc.getDoctype();
    entities = docType.getEntities();
    assertNotNull("entitiesNotNull", entities);
    notations = docType.getNotations();
    assertNotNull("notationsNotNull", notations);
    entity = (Entity) entities.getNamedItem("ent1");
    notation = (Notation) notations.getNamedItem("notation1");

    {
      boolean success = false;
      try {
        newNode = entities.setNamedItemNS(entity);
      } catch (DOMException ex) {
        success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR);
      }
      assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR_entities", success);
    }

    {
      boolean success = false;
      try {
        newNode = notations.setNamedItemNS(notation);
      } catch (DOMException ex) {
        success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR);
      }
      assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR_notations", success);
    }
  }
  public void testSetNamedItemNS2() throws Throwable {
    Document doc;
    NamedNodeMap attributes;
    Element element;
    Attr attribute;
    Attr attribute1;

    String attrName;
    doc = (Document) load("staffNS", builder);
    element = doc.createElementNS("http://www.w3.org/DOM/Test", "root");
    attribute1 = doc.createAttributeNS("http://www.w3.org/DOM/L1", "L1:att");
    attributes = element.getAttributes();
    attributes.setNamedItemNS(attribute1);
    attribute = (Attr) attributes.getNamedItemNS("http://www.w3.org/DOM/L1", "att");
    attrName = attribute.getNodeName();
    assertEquals("namednodemapsetnameditemns02", "L1:att", attrName);
  }
Example #7
0
 void doApply(
     Stylesheet stylesheet,
     QName mode,
     Node context,
     int pos,
     int len,
     Node parent,
     Node nextSibling)
     throws TransformerException {
   Node result = null;
   Document doc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument();
   short nodeType = source.getNodeType();
   if (nodeType == Node.ATTRIBUTE_NODE && parent.getFirstChild() != null) {
     // Ignore attributes added after child elements
   } else {
     // Namespace aliasing
     if (nodeType == Node.ELEMENT_NODE) {
       String prefix = source.getPrefix();
       if (prefix == null) prefix = "#default";
       String resultPrefix = (String) stylesheet.namespaceAliases.get(prefix);
       if (resultPrefix != null) {
         if ("#default".equals(resultPrefix)) resultPrefix = null;
         String uri = source.lookupNamespaceURI(resultPrefix);
         String name = source.getNodeName();
         // Create a new element node in the result document
         result = doc.createElementNS(uri, name);
         // copy attributes
         NamedNodeMap srcAttrs = source.getAttributes();
         NamedNodeMap dstAttrs = result.getAttributes();
         int l = srcAttrs.getLength();
         for (int i = 0; i < l; i++) {
           Node attr = srcAttrs.item(i);
           if (!Stylesheet.XSL_NS.equals(attr.getNamespaceURI())) {
             attr = attr.cloneNode(true);
             attr = doc.adoptNode(attr);
             dstAttrs.setNamedItemNS(attr);
           }
         }
       }
     }
     if (result == null) {
       // Create result node
       result = source.cloneNode(false);
       // Remove any XSL attributes
       NamedNodeMap attrs = result.getAttributes();
       if (attrs != null) {
         int l = attrs.getLength();
         for (int i = 0; i < l; i++) {
           Node attr = attrs.item(i);
           if (Stylesheet.XSL_NS.equals(attr.getNamespaceURI())) {
             attrs.removeNamedItem(attr.getNodeName());
             i--;
             l--;
           }
         }
       }
       Node result2 = doc.adoptNode(result);
       if (result2 == null) {
         String msg =
             "Error adopting node to result tree: "
                 + result
                 + " ("
                 + result.getClass().getName()
                 + ")";
         DOMSourceLocator l = new DOMSourceLocator(context);
         throw new TransformerException(msg, l);
       }
       result = result2;
     }
     if (nextSibling != null) parent.insertBefore(result, nextSibling);
     else parent.appendChild(result);
     if (nodeType == Node.ELEMENT_NODE)
       stylesheet.addNamespaceNodes(source, result, doc, elementExcludeResultPrefixes);
     // children
     if (children != null) children.apply(stylesheet, mode, context, pos, len, result, null);
   }
   // next sibling
   if (next != null) next.apply(stylesheet, mode, context, pos, len, parent, nextSibling);
 }
Example #8
0
  private void createNewEpubDoc(Node section) throws AuthorOperationException {
    docNumber = docNumber + 1;

    StringBuilder xmlTemplate = new StringBuilder();

    xmlTemplate.append("<html");

    for (Map.Entry<String, String> entry : splitHandler.getHtmlAttributes().entrySet()) {
      xmlTemplate.append(" " + entry.getKey() + "='" + entry.getValue() + "'");
    }

    xmlTemplate.append(">");

    xmlTemplate.append("<head>");
    xmlTemplate.append("<meta charset='UTF-8'/>");

    if (packageHandler.getTitle() != null) {
      xmlTemplate.append("<title>" + packageHandler.getTitle() + "</title>");
    } else {
      xmlTemplate.append("<title>" + splitHandler.getTitle() + "</title>");
    }

    for (Map.Entry<String, String> entry : splitHandler.getMetaNodes().entrySet()) {
      xmlTemplate.append(
          "<meta content='" + entry.getValue() + "' name='" + entry.getKey() + "'/>");
    }

    for (String cssLink : splitHandler.getCssLinks()) {
      xmlTemplate.append("<link href='" + cssLink + "' rel='stylesheet' type='text/css'/>");
    }

    xmlTemplate.append("</head>");
    xmlTemplate.append("<body/>");
    xmlTemplate.append("</html>");

    Document template = Utils.deserializeDocument(xmlTemplate.toString(), null);

    Node templateBody = template.getElementsByTagName("body").item(0);

    // Read Section Node
    // Section Classname
    String className = getAttributeFromNode(section, "class");
    if (!className.equals("")) {
      // Add class to body
      Attr attrClass = template.createAttribute("class");
      attrClass.setValue(className);
      NamedNodeMap bodyAttrs = templateBody.getAttributes();
      bodyAttrs.setNamedItem(attrClass);
    }

    // Section Id
    String id = getAttributeFromNode(section, "id");
    if (!id.equals("")) {
      // Add id to body
      Attr attrID = template.createAttribute("id");
      attrID.setValue(id);
      NamedNodeMap bodyAttrs = templateBody.getAttributes();
      bodyAttrs.setNamedItem(attrID);
    }

    // Section epub:type
    String epubType = getAttributeFromNode(section, "epub:type");

    // Add epub:type to body
    Attr attrEpubType = template.createAttributeNS("http://www.idpf.org/2007/ops", "epub:type");
    attrEpubType.setValue(epubType);
    NamedNodeMap bodyAttrs = templateBody.getAttributes();
    bodyAttrs.setNamedItemNS(attrEpubType);

    // epub:type might be divided by spaces - only 1 value will be used

    String epubMainType = getEpubMainType(epubType);
    if (epubMainType.equals("")) {
      epubMainType = "unknown";
    }

    // Add Nodes to body
    NodeList sectionNodes = section.getChildNodes();
    for (int i = 0; i < sectionNodes.getLength(); i++) {
      Node sectionNode = sectionNodes.item(i);

      // Import the node
      Node importedNode = template.importNode(sectionNode, true);
      templateBody.appendChild(importedNode);
    }

    // Create FileName
    String strNum = "00" + Integer.toString(docNumber);
    strNum = strNum.substring(strNum.length() - 3);
    String newFileName = idPrefix + "-" + strNum + "-" + epubMainType + ".xhtml";

    docList.put(newFileName, template);
    stop = false;
    collectIds(newFileName, templateBody);
  }