示例#1
0
 void lookupPrefix(org.w3c.dom.Node node) {
   if (node == null) throw new IllegalArgumentException("node must not be null");
   String prefix = node.lookupPrefix(namespace.getUri());
   if (prefix == null) {
     //    check to see if we match the default namespace
     String defaultNamespace = node.lookupNamespaceURI(null);
     if (defaultNamespace == null) defaultNamespace = "";
     String nodeNamespace = namespace.getUri();
     if (nodeNamespace.equals(defaultNamespace)) {
       prefix = "";
     }
   }
   int i = 0;
   while (prefix == null) {
     String generatedPrefix = "e4x_" + i++;
     String generatedUri = node.lookupNamespaceURI(generatedPrefix);
     if (generatedUri == null) {
       prefix = generatedPrefix;
       org.w3c.dom.Node top = node;
       while (top.getParentNode() != null
           && top.getParentNode() instanceof org.w3c.dom.Element) {
         top = top.getParentNode();
       }
       ((org.w3c.dom.Element) top)
           .setAttributeNS(
               "http://www.w3.org/2000/xmlns/", "xmlns:" + prefix, namespace.getUri());
     }
   }
   namespace.setPrefix(prefix);
 }
示例#2
0
  public String getNamespacePrefix(String uri) {

    NamespaceContextIterator eachNamespace = getNamespaceContextNodes();
    while (eachNamespace.hasNext()) {
      org.w3c.dom.Attr namespaceDecl = eachNamespace.nextNamespaceAttr();
      if (namespaceDecl.getNodeValue().equals(uri)) {
        String candidatePrefix = namespaceDecl.getLocalName();
        if ("xmlns".equals(candidatePrefix)) return "";
        else return candidatePrefix;
      }
    }

    // Find if any of the ancestors' name has this uri
    org.w3c.dom.Node currentAncestor = this;
    while (currentAncestor != null && !(currentAncestor instanceof Document)) {

      if (uri.equals(currentAncestor.getNamespaceURI())) return currentAncestor.getPrefix();
      currentAncestor = currentAncestor.getParentNode();
    }

    return null;
  }
  protected void initFromNode(org.w3c.dom.Node doc, int options) throws Schema2BeansException {
    if (doc == null) {
      doc = GraphManager.createRootElementNode("deployment-plan"); // NOI18N
      if (doc == null)
        throw new Schema2BeansException(
            Common.getMessage("CantCreateDOMRoot_msg", "deployment-plan"));
    }
    Node n = GraphManager.getElementNode("deployment-plan", doc); // NOI18N
    if (n == null)
      throw new Schema2BeansException(
          Common.getMessage(
              "DocRootNotInDOMGraph_msg", "deployment-plan", doc.getFirstChild().getNodeName()));

    this.graphManager.setXmlDocument(doc);

    // Entry point of the createBeans() recursive calls
    this.createBean(n, this.graphManager());
    this.initialize(options);
  }
  /** updates the XMl with hashcode for the files */
  protected BudgetSubAwards updateXML(
      byte xmlContents[], Map fileMap, BudgetSubAwards budgetSubAwardBean, Budget budget)
      throws Exception {

    javax.xml.parsers.DocumentBuilderFactory domParserFactory =
        javax.xml.parsers.DocumentBuilderFactory.newInstance();
    javax.xml.parsers.DocumentBuilder domParser = domParserFactory.newDocumentBuilder();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlContents);

    org.w3c.dom.Document document = domParser.parse(byteArrayInputStream);
    byteArrayInputStream.close();
    String namespace = null;
    String formName = null;
    if (document != null) {
      Node node;
      Element element = document.getDocumentElement();
      NamedNodeMap map = element.getAttributes();
      String namespaceHolder =
          element.getNodeName().substring(0, element.getNodeName().indexOf(':'));
      node = map.getNamedItem("xmlns:" + namespaceHolder);
      namespace = node.getNodeValue();
      FormMappingInfo formMappingInfo = new FormMappingLoader().getFormInfo(namespace);
      formName = formMappingInfo.getFormName();
      budgetSubAwardBean.setNamespace(namespace);
      budgetSubAwardBean.setFormName(formName);
    }

    String xpathEmptyNodes =
        "//*[not(node()) and local-name(.) != 'FileLocation' and local-name(.) != 'HashValue']";
    String xpathOtherPers =
        "//*[local-name(.)='ProjectRole' and local-name(../../.)='OtherPersonnel' and count(../NumberOfPersonnel)=0]";
    removeAllEmptyNodes(document, xpathEmptyNodes, 0);
    removeAllEmptyNodes(document, xpathOtherPers, 1);
    removeAllEmptyNodes(document, xpathEmptyNodes, 0);
    changeDataTypeForNumberOfOtherPersons(document);

    List<String> fedNonFedSubAwardForms = getFedNonFedSubawardForms();
    NodeList budgetYearList =
        XPathAPI.selectNodeList(document, "//*[local-name(.) = 'BudgetYear']");
    for (int i = 0; i < budgetYearList.getLength(); i++) {
      Node bgtYearNode = budgetYearList.item(i);
      String period = getValue(XPathAPI.selectSingleNode(bgtYearNode, "BudgetPeriod"));
      if (fedNonFedSubAwardForms.contains(namespace)) {
        Element newBudgetYearElement =
            copyElementToName((Element) bgtYearNode, bgtYearNode.getNodeName());
        bgtYearNode.getParentNode().replaceChild(newBudgetYearElement, bgtYearNode);
      } else {
        Element newBudgetYearElement =
            copyElementToName((Element) bgtYearNode, bgtYearNode.getNodeName() + period);
        bgtYearNode.getParentNode().replaceChild(newBudgetYearElement, bgtYearNode);
      }
    }

    Node oldroot = document.removeChild(document.getDocumentElement());
    Node newroot = document.appendChild(document.createElement("Forms"));
    newroot.appendChild(oldroot);

    org.w3c.dom.NodeList lstFileName = document.getElementsByTagName("att:FileName");
    org.w3c.dom.NodeList lstFileLocation = document.getElementsByTagName("att:FileLocation");
    org.w3c.dom.NodeList lstMimeType = document.getElementsByTagName("att:MimeType");
    org.w3c.dom.NodeList lstHashValue = document.getElementsByTagName("glob:HashValue");

    if ((lstFileName.getLength() != lstFileLocation.getLength())
        || (lstFileLocation.getLength() != lstHashValue.getLength())) {
      //            throw new RuntimeException("Tag occurances mismatch in XML File");
    }

    org.w3c.dom.Node fileNode, hashNode, mimeTypeNode;
    org.w3c.dom.NamedNodeMap fileNodeMap, hashNodeMap;
    String fileName;
    byte fileBytes[];
    String contentId;
    List attachmentList = new ArrayList();

    for (int index = 0; index < lstFileName.getLength(); index++) {
      fileNode = lstFileName.item(index);

      Node fileNameNode = fileNode.getFirstChild();
      fileName = fileNameNode.getNodeValue();

      fileBytes = (byte[]) fileMap.get(fileName);

      if (fileBytes == null) {
        throw new RuntimeException("FileName mismatch in XML and PDF extracted file");
      }
      String hashVal = GrantApplicationHash.computeAttachmentHash(fileBytes);

      hashNode = lstHashValue.item(index);
      hashNodeMap = hashNode.getAttributes();

      Node temp = document.createTextNode(hashVal);
      hashNode.appendChild(temp);

      hashNode = hashNodeMap.getNamedItem("glob:hashAlgorithm");

      hashNode.setNodeValue(S2SConstants.HASH_ALGORITHM);

      fileNode = lstFileLocation.item(index);
      fileNodeMap = fileNode.getAttributes();
      fileNode = fileNodeMap.getNamedItem("att:href");

      contentId = fileNode.getNodeValue();
      String encodedContentId = cleanContentId(contentId);
      fileNode.setNodeValue(encodedContentId);

      mimeTypeNode = lstMimeType.item(0);
      String contentType = mimeTypeNode.getFirstChild().getNodeValue();

      BudgetSubAwardAttachment budgetSubAwardAttachmentBean = new BudgetSubAwardAttachment();
      budgetSubAwardAttachmentBean.setAttachment(fileBytes);
      budgetSubAwardAttachmentBean.setContentId(encodedContentId);

      budgetSubAwardAttachmentBean.setContentType(contentType);
      budgetSubAwardAttachmentBean.setBudgetId(budgetSubAwardBean.getBudgetId());
      budgetSubAwardAttachmentBean.setSubAwardNumber(budgetSubAwardBean.getSubAwardNumber());

      attachmentList.add(budgetSubAwardAttachmentBean);
    }

    budgetSubAwardBean.setBudgetSubAwardAttachments(attachmentList);

    javax.xml.transform.Transformer transformer =
        javax.xml.transform.TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    javax.xml.transform.stream.StreamResult result =
        new javax.xml.transform.stream.StreamResult(bos);
    javax.xml.transform.dom.DOMSource source = new javax.xml.transform.dom.DOMSource(document);

    transformer.transform(source, result);

    budgetSubAwardBean.setSubAwardXmlFileData(new String(bos.toByteArray()));

    bos.close();

    return budgetSubAwardBean;
  }
示例#5
0
  public String getNamespaceURI(String prefix) {

    if ("xmlns".equals(prefix)) {
      return XMLNS_URI;
    }

    if ("xml".equals(prefix)) {
      return XML_URI;
    }

    if ("".equals(prefix)) {

      org.w3c.dom.Node currentAncestor = this;
      while (currentAncestor != null && !(currentAncestor instanceof Document)) {

        if (currentAncestor instanceof ElementImpl) {
          QName name = ((ElementImpl) currentAncestor).getElementQName();
          /*
          if (prefix.equals(name.getPrefix())) {
              String uri = name.getNamespaceURI();
              if ("".equals(uri)) {
                  return null;
              }
              else {
                  return uri;
              }
          }*/
          if (((Element) currentAncestor).hasAttributeNS(XMLNS_URI, "xmlns")) {

            String uri = ((Element) currentAncestor).getAttributeNS(XMLNS_URI, "xmlns");
            if ("".equals(uri)) return null;
            else {
              return uri;
            }
          }
        }
        currentAncestor = currentAncestor.getParentNode();
      }

    } else if (prefix != null) {
      // Find if there's an ancester whose name contains this prefix
      org.w3c.dom.Node currentAncestor = this;

      //            String uri = currentAncestor.lookupNamespaceURI(prefix);
      //            return uri;
      while (currentAncestor != null && !(currentAncestor instanceof Document)) {

        /* if (prefix.equals(currentAncestor.getPrefix())) {
            String uri = currentAncestor.getNamespaceURI();
            // this is because the javadoc says getNamespaceURI() is not a computed value
            // and URI for a non-empty prefix cannot be null
            if (uri != null)
                return uri;
        }*/
        // String uri = currentAncestor.lookupNamespaceURI(prefix);
        // if (uri != null) {
        //    return uri;
        // }

        if (((Element) currentAncestor).hasAttributeNS(XMLNS_URI, prefix)) {
          return ((Element) currentAncestor).getAttributeNS(XMLNS_URI, prefix);
        }

        currentAncestor = currentAncestor.getParentNode();
      }
    }

    return null;
  }