protected void transformXML(File[] files) {
   int number = 0;
   try {
     SAXBuilder builder = new SAXBuilder();
     for (int f = 0; f < files.length; f++) {
       int fn = f + 1;
       // split by treatment
       Document doc = builder.build(files[f]);
       Element root = doc.getRootElement();
       List<Element> treatments =
           XPath.selectNodes(root, "/tax:taxonx/tax:taxonxBody/tax:treatment");
       // detach all but one treatments from doc
       ArrayList<Element> saved = new ArrayList<Element>();
       for (int t = 1; t < treatments.size(); t++) {
         Element e = treatments.get(t);
         doc.removeContent(e);
         e.detach();
         saved.add(e);
       }
       // now doc is a template to create other treatment files
       // root.detach();
       formatDescription(
           (Element) XPath.selectSingleNode(root, "/tax:taxonx/tax:taxonxBody/tax:treatment"),
           ".//tax:div[@type='description']",
           ".//tax:p",
           fn,
           0);
       root.detach();
       writeTreatment2Transformed(root, fn, 0);
       listener.info((number++) + "", fn + "_0.xml"); // list the file on GUI here
       getDescriptionFrom(root, fn, 0);
       // replace treatement in doc with a new treatment in saved
       Iterator<Element> it = saved.iterator();
       int count = 1;
       while (it.hasNext()) {
         Element e = it.next();
         Element body = root.getChild("taxonxBody", root.getNamespace());
         Element treatment =
             (Element) XPath.selectSingleNode(root, "/tax:taxonx/tax:taxonxBody/tax:treatment");
         // in treatment/div[@type="description"], replace <tax:p> tag with <description
         // pid="1.txtp436_1.txt">
         int index = body.indexOf(treatment);
         e = formatDescription(e, ".//tax:div[@type='description']", ".//tax:p", fn, count);
         body.setContent(index, e);
         // write each treatment as a file in the target/transfromed folder
         // write description text in the target/description folder
         root.detach();
         writeTreatment2Transformed(root, fn, count);
         listener.info((number++) + "", fn + "_" + count + ".xml"); // list the file on GUI here
         getDescriptionFrom(root, fn, count);
         count++;
       }
     }
   } catch (Exception e) {
     e.printStackTrace();
     LOGGER.error("Type4Transformer : error.", e);
   }
 }
Exemplo n.º 2
0
  /**
   * Used for editing : swaps 2 elements.
   *
   * @param el1
   * @param el2
   * @throws Exception
   */
  protected void swapElements(Element el1, Element el2) throws Exception {

    Element parent = el1.getParentElement();
    if (parent == null) {
      throw new IllegalArgumentException("No parent element for swapping");
    }

    int index1 = parent.indexOf(el1);
    if (index1 == -1) {
      throw new IllegalArgumentException("Element 1 not found for swapping");
    }
    int index2 = parent.indexOf(el2);
    if (index2 == -1) {
      throw new IllegalArgumentException("Element 2 not found for swapping");
    }

    Element el1Spare = (Element) el1.clone();

    parent.setContent(index1, (Element) el2.clone());
    parent.setContent(index2, el1Spare);
  }
Exemplo n.º 3
0
  private void doit()
      throws JDOMException, IOException, SAXException, ParserConfigurationException,
          TransformerConfigurationException, TransformerException {
    Document CATALOG_DOCUMENT = FileIO.readDocumentFromLocalFile(CATALOG_FILE);
    List l =
        XPath.newInstance(
                "//resource[contains(@type,'exmaralda') and descendant::exmaralda-coma[@type='local']]")
            .selectNodes(CATALOG_DOCUMENT);
    for (Object o : l) {
      Element resourceElement = (Element) o;
      Element comaCorpusElement =
          (Element)
              XPath.newInstance("descendant::exmaralda-coma[@type='local']")
                  .selectSingleNode(resourceElement);
      String url = comaCorpusElement.getAttributeValue("url").substring(7);
      System.out.println("Processing " + url);

      COMACorpus comaCorpus = new COMACorpus();
      comaCorpus.readCorpus(new File(url), true);

      // resourceElement.removeChildren("annotation");
      HashSet<String> annotationNames = comaCorpus.getAnnotationNames();
      for (String annotationName : annotationNames) {
        if (XPath.newInstance("descendant::annotation[@category='" + annotationName + "']")
                .selectSingleNode(resourceElement)
            == null) {
          Element annotationElement = new Element("annotation");
          annotationElement.setAttribute("category", annotationName);
          int index =
              resourceElement.indexOf(
                  (Element)
                      XPath.newInstance("descendant::short-description[last()]")
                          .selectSingleNode(resourceElement));
          resourceElement.addContent(index + 1, annotationElement);
        }
      }
      FileIO.writeDocumentToLocalFile(CATALOG_FILE, CATALOG_DOCUMENT);
    }
  }
Exemplo n.º 4
0
  /**
   * For Ajax Editing : removes an element from a metadata ([del] link).
   *
   * @param dbms
   * @param session
   * @param id
   * @param ref
   * @param parentRef
   * @return
   * @throws Exception
   */
  public synchronized Element deleteElementEmbedded(
      Dbms dbms, UserSession session, String id, String ref, String parentRef) throws Exception {

    String schema = dataManager.getMetadataSchema(dbms, id);

    // --- get metadata from session
    Element md = getMetadataFromSession(session, id);

    // --- locate the geonet:info element and clone for later re-use
    Element info = (Element) (md.getChild(Edit.RootChild.INFO, Edit.NAMESPACE)).clone();
    md.removeChild(Edit.RootChild.INFO, Edit.NAMESPACE);

    // --- get element to remove
    EditLib editLib = dataManager.getEditLib();
    Element el = editLib.findElement(md, ref);

    if (el == null) throw new IllegalStateException(MSG_ELEMENT_NOT_FOUND_AT_REF + ref);

    String uName = el.getName();
    Namespace ns = el.getNamespace();
    Element parent = el.getParentElement();
    Element result = null;
    if (parent != null) {
      int me = parent.indexOf(el);

      // --- check and see whether the element to be deleted is the last one of its kind
      Filter elFilter = new ElementFilter(uName, ns);
      if (parent.getContent(elFilter).size() == 1) {

        // --- get geonet child element with attribute name = unqualified name
        Filter chFilter = new ElementFilter(Edit.RootChild.CHILD, Edit.NAMESPACE);
        List children = parent.getContent(chFilter);

        for (int i = 0; i < children.size(); i++) {
          Element ch = (Element) children.get(i);
          String name = ch.getAttributeValue("name");
          if (name != null && name.equals(uName)) {
            result = (Element) ch.clone();
            break;
          }
        }

        // -- now delete the element as requested
        parent.removeContent(me);

        // --- existing geonet child element not present so create it and insert it
        // --- where the last element was deleted
        if (result == null) {
          result = editLib.createElement(schema, el, parent);
          parent.addContent(me, result);
        }

        result.setAttribute(Edit.ChildElem.Attr.PARENT, parentRef);
        result.addContent(info);
      }
      // --- if not the last one then just delete it
      else {
        parent.removeContent(me);
      }
    } else {
      throw new IllegalStateException("Element at ref = " + ref + " doesn't have a parent");
    }

    // if we don't need a child then create a geonet:null element
    if (result == null) {
      result = new Element(Edit.RootChild.NULL, Edit.NAMESPACE);
      result.addContent(info);
    }

    // --- reattach the info element to the metadata
    md.addContent((Element) info.clone());

    // --- store the metadata in the session again
    setMetadataIntoSession(session, (Element) md.clone(), id);

    return result;
  }
Exemplo n.º 5
0
 /**
  * Add content before or after an element.
  *
  * @param sibling an element with a parent.
  * @param toAdd the content to add alongside <code>sibling</code>.
  * @param after <code>true</code> to add it after <code>sibling</code>.
  */
 public static void insertSiblings(
     final Element sibling, final Collection<? extends Content> toAdd, final boolean after) {
   final Element parentElement = sibling.getParentElement();
   final int index = parentElement.indexOf(sibling);
   parentElement.addContent(after ? index + 1 : index, toAdd);
 }