Exemplo n.º 1
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);
    }
  }
Exemplo n.º 2
0
    public void addMessage(SAXParseException exception, String typeOfError) {
      if (errorCount == 0) xpaths = new Element("xsderrors", ns);
      errorCount++;

      Element elem = (Element) so.getLocator().getNode();
      Element x = new Element("xpath", ns);
      try {
        String xpath = jeeves.utils.XPath.getXPath(elem);
        // -- remove the first element to ensure XPath fits XML passed with
        // -- root element
        if (xpath.startsWith("/")) {
          int ind = xpath.indexOf('/', 1);
          if (ind != -1) {
            xpath = xpath.substring(ind + 1);
          } else {
            xpath = "."; // error to be placed on the root element
          }
        }
        x.setText(xpath);
      } catch (JDOMException e) {
        e.printStackTrace();
        x.setText("nopath");
      }
      String message = exception.getMessage() + " (Element: " + elem.getQualifiedName();
      String parentName;
      if (!elem.isRootElement()) {
        Element parent = (Element) elem.getParent();
        if (parent != null) parentName = parent.getQualifiedName();
        else parentName = "Unknown";
      } else {
        parentName = "/";
      }
      message += " with parent element: " + parentName + ")";

      Element m = new Element("message", ns).setText(message);
      Element errorType = new Element("typeOfError", ns).setText(typeOfError);
      Element errorNumber = new Element("errorNumber", ns).setText(String.valueOf(errorCount));
      Element e = new Element("error", ns);
      e.addContent(errorType);
      e.addContent(errorNumber);
      e.addContent(m);
      e.addContent(x);
      xpaths.addContent(e);
    }
Exemplo n.º 3
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);
    }
  }
Exemplo n.º 4
0
  /**
   * Displays information about all nodes in a result set.
   *
   * @param nodelist the result list of an XPath expression evaluation
   */
  public static void display_node_list(List nodelist) {
    int size = nodelist.size();

    for (Object obj : nodelist) {
      String value = null;

      if (obj instanceof Element) {
        Element elem = (Element) obj;
        String details =
            String.format(
                "%d children, %d attrs", elem.getChildren().size(), elem.getAttributes().size());
        value = String.format("%s (%s)", elem.getQualifiedName(), details);
      } else if (obj instanceof Attribute) {
        Attribute attr = (Attribute) obj;
        value = String.format("@%s=\'%s\'", attr.getQualifiedName(), attr.getValue());
      } else if (obj instanceof Text) {
        Text text = (Text) obj;
        value = String.format("\"%s\"", text.getText());
      } else if (obj instanceof String) {
        String s = (String) obj;
        value = String.format("\"%s\"", s);
      } else if (obj instanceof Boolean) {
        Boolean b = (Boolean) obj;
        value = String.format("%s", b.toString());
      } else if (obj instanceof Double) {
        Double d = (Double) obj;
        value = String.format("%s", d.toString());
      } else {
        value = "Unsupported dynamic type";
      } // if-else

      System.out.printf("   (%s) %s\n", obj.getClass().getName(), value);
    } // for

    System.out.printf("Result: %d nodes\n", size);
  } // display_node_list()
Exemplo n.º 5
0
  public Z3950ServerResults harvest() throws Exception {
    Set<String> newUuids = new HashSet<String>();

    int groupSize = 10;

    log.info("Retrieving remote metadata information:" + params.uuid);

    Z3950ServerResults serverResults = new Z3950ServerResults();

    // --- Clean all before harvest : Remove/Add mechanism
    localUuids = new UUIDMapper(dbms, params.uuid);

    // --- remove old metadata
    for (String uuid : localUuids.getUUIDs()) {
      String id = localUuids.getID(uuid);
      if (log.isDebugEnabled()) log.debug("  - Removing old metadata before update with id: " + id);
      dataMan.deleteMetadataGroup(context, dbms, id);
      serverResults.locallyRemoved++;
    }

    if (serverResults.locallyRemoved > 0) dbms.commit();

    // --- Search remote node
    MetaSearcher s = searchMan.newSearcher(SearchManager.Z3950, Geonet.File.SEARCH_Z3950_CLIENT);

    ServiceConfig config = new ServiceConfig();

    Element request = new Element("request");

    // --- Z39.50 servers from harvest params
    for (String id : params.getRepositories()) {
      request.addContent(new Element(Geonet.SearchResult.SERVERS).setText(id));
    }

    // --- Z39.50 query from harvest params
    request.addContent(new Element(Geonet.SearchResult.ZQUERY).setText(params.query));

    // --- don't get html presentations (get them later)
    request.addContent(new Element(Geonet.SearchResult.SERVERHTML).setText("off"));

    // --- set timeout to be 100 seconds
    request.addContent(new Element(Geonet.SearchResult.TIMEOUT).setText("100"));
    // --- set hitsPerPage
    request.addContent(new Element(Geonet.SearchResult.HITS_PER_PAGE).setText(groupSize + ""));

    // --- do the search
    s.search(context, request, config);

    if (s.getSize() == 0) {
      log.error("Search failed or returned 0 results, trying again");
      s.search(context, request, config);
      if (s.getSize() == 0) {
        throw new Exception("Bad luck, Search failed or returned 0 results");
      }
    }

    if (log.isDebugEnabled()) log.debug("Search returned " + s.getSize() + " hits");

    // -- process the hits in groups of groupSize
    int numberOfHits = Math.min(Integer.parseInt(params.maximumHits), s.getSize());
    // -- add from and to placeholders to request

    request.addContent(new Element("from"));
    request.addContent(new Element("to"));

    Element categories = Lib.local.retrieve(dbms, "Categories");
    if (log.isDebugEnabled()) log.debug("categories " + Xml.getString(categories));

    Element repositories = new Info().getZRepositories(context, settingMan);
    if (log.isDebugEnabled()) log.debug("repos " + Xml.getString(repositories));

    // -- build a map of collection code versus repository name for
    // -- assigning the categories
    Map<String, String> codes = new HashMap<String, String>();
    Map<String, String> catCodes = new HashMap<String, String>();

    // -- add new category for each repository
    boolean addcateg = false;
    for (String repo : params.getRepositories()) {
      Element repoElem = Xml.selectElement(repositories, "record[id='" + repo + "']");
      if (repoElem != null) {
        Element repoId = repoElem.getChild("id");
        String repoName = repoElem.getChildText("name");
        codes.put(
            repoId.getAttributeValue("serverCode") + ":" + repoId.getAttributeValue("code"),
            repoName);
        // create a result holder for this repository
        serverResults.getServerResult(repoName);

        // sanitize the name of the category
        String categName = repoName.replaceAll("[^\\w]", "");
        categName = categName.toLowerCase();
        catCodes.put(
            repoId.getAttributeValue("serverCode") + ":" + repoId.getAttributeValue("code"),
            categName);

        if (Xml.selectElement(categories, "record[name='" + categName + "']") == null) {
          int newId = context.getSerialFactory().getSerial(dbms, "Categories");
          dbms.execute("INSERT INTO Categories(id, name) VALUES (?, ?)", newId, categName);
          Lib.local.insert(dbms, "Categories", newId, repoName);
          addcateg = true;
        }
      }
    }

    if (addcateg) dbms.commit();

    // --- return only maximum hits as directed by the harvest params
    int nrGroups = (numberOfHits / groupSize) + 1;
    for (int i = 1; i <= nrGroups; i++) {
      int lower = ((i - 1) * groupSize) + 1;
      int upper = Math.min((i * groupSize), numberOfHits);
      request.getChild("from").setText("" + lower);
      request.getChild("to").setText("" + upper);

      // --- Loading results
      List<Document> list = s.presentDocuments(context, request, config);

      // --- Loading categories and groups
      localCateg = new CategoryMapper(dbms);
      localGroups = new GroupMapper(dbms);

      if (log.isDebugEnabled())
        log.debug(
            "There are "
                + (list.size() - 1)
                + " children in the results ("
                + lower
                + " to "
                + upper
                + ")");

      boolean transformIt = false;
      String thisXslt = context.getAppPath() + Geonet.Path.IMPORT_STYLESHEETS + "/";
      if (!params.importXslt.equals("none")) {
        thisXslt = thisXslt + params.importXslt;
        transformIt = true;
      }

      // --- For each record....
      for (Document doc : list) {
        Element md = doc.getRootElement();
        String eName = md.getQualifiedName();
        if (eName.equals("summary")) continue;

        // -- Remove existing geonet:info children as for example
        // -- GeoNetwork Z39.50 server return when full mode
        // -- an extra element with server info not needed
        // -- once harvested
        String colCode = "";
        Element info = md.getChild(Edit.RootChild.INFO, Edit.NAMESPACE);
        if (info != null) {
          String serverCode = info.getChildText("server");
          int colPos = serverCode.indexOf(':');
          if (colPos != -1) {
            colCode = serverCode.substring(0, colPos) + ":" + info.getChildText("collection");
          }
        }
        md.removeChildren(Edit.RootChild.INFO, Edit.NAMESPACE);
        String repoName = codes.get(colCode);
        if (log.isDebugEnabled()) log.debug("Processing record from server " + repoName);
        HarvestResult result = serverResults.getServerResult(repoName);
        result.totalMetadata++;

        if (eName.equals("error")) {
          log.error("JZKit could not retrieve record - returned " + Xml.getString(md));
          result.unretrievable++;
          continue;
        }

        // transform using importxslt if not none
        if (transformIt) {
          try {
            if (log.isDebugEnabled()) log.debug("Before transform: " + Xml.getString(md));
            md = Xml.transform(md, thisXslt);
            if (log.isDebugEnabled()) log.debug("After transform: " + Xml.getString(md));
          } catch (Exception e) {
            System.out.println("Cannot transform XML, ignoring. Error was: " + e.getMessage());
            result.badFormat++;
            continue; // skip this one
          }
        }

        // detect schema, extract uuid and add
        String schema = dataMan.autodetectSchema(md, null);
        if (schema == null) {
          log.warning("Skipping metadata with unknown schema.");
          result.unknownSchema++;
          continue;
        }

        String uuid = null;
        try {
          uuid = dataMan.extractUUID(schema, md);
        } catch (Exception e) {
          log.error("Unable to extract UUID: " + e.getMessage());
          e.printStackTrace();
        }

        if (uuid == null || uuid.equals("")) {
          log.warning("Skipping metadata due to failure extracting uuid (uuid null or empty).");
          result.unretrievable++;
          continue;
        }

        log.info("  - Adding metadata with " + uuid);

        // --- generate a new metadata id

        int id = context.getSerialFactory().getSerial(dbms, "Metadata");
        // TODO end confusion about datatypes
        String id$ = Integer.toString(id);

        String docType = "";
        if (!transformIt && (doc.getDocType() != null)) {
          docType = Xml.getString(doc.getDocType());
        }

        // --- check for duplicate uuid - violates constraints on metadata table
        // --- if we attempt insert
        boolean alreadyAdded = !newUuids.add(uuid);
        boolean alreadyInDb = (dataMan.getMetadataId(dbms, uuid) != null);
        if (alreadyAdded || alreadyInDb) {
          log.error("Uuid " + uuid + " already exists in this set/database - cannot insert");
          result.couldNotInsert++;
          continue;
        }

        //
        // insert metadata
        //
        try {
          String groupOwner = "1", isTemplate = "n", title = null;
          int owner = 1;
          String category = null, createDate = new ISODate().toString(), changeDate = createDate;
          boolean ufo = false, indexImmediate = false;
          dataMan.insertMetadata(
              context,
              dbms,
              schema,
              md,
              id,
              uuid,
              owner,
              groupOwner,
              params.uuid,
              isTemplate,
              docType,
              title,
              category,
              createDate,
              changeDate,
              ufo,
              indexImmediate);

        } catch (Exception e) {
          log.error("Unable to insert metadata " + e.getMessage());
          e.printStackTrace();
          result.couldNotInsert++;
          continue;
        }

        addPrivileges(id$, params.getPrivileges(), localGroups, dataMan, context, dbms, log);
        addCategories(
            id$,
            params.getCategories(),
            localCateg,
            dataMan,
            dbms,
            context,
            log,
            catCodes.get(colCode));

        dataMan.setTemplateExt(dbms, id, "n", null);
        dataMan.setHarvestedExt(dbms, id, params.uuid, params.name);

        // validate it here if requested
        if (params.validate) {
          Document docVal;
          if (!transformIt && (doc.getDocType() != null)) {
            docVal = new Document(md, (DocType) doc.getDocType().detach());
          } else {
            docVal = new Document(md);
          }

          if (!dataMan.doValidate(dbms, schema, id$, docVal, context.getLanguage())) {
            result.doesNotValidate++;
          }
        }

        dataMan.indexMetadata(dbms, id$);

        result.addedMetadata++;
      }
    }

    dbms.commit();
    return serverResults;
  }
  /**
   * This method is used to recursively create a java object based on the provided element.
   * Available elements : list, item, map, string and data. To create simple object see the
   * convertSimpleObject method.
   *
   * @param element the element to convert.
   * @return the created POJO, null if the element does not fit.
   */
  private void appendObject(Map<String, Object> container, Element element) {

    final String name = element.getQualifiedName();
    final List<Attribute> attributes = element.getAttributes();
    final List<Element> childElements = element.getChildren();

    /*
     * The simplest tag containing only a text value !
     */
    if ((childElements.size() == 0) && (attributes.size() == 0)) {
      container.put(name, element.getTextNormalize());
      return;
    }

    /*
     * Element is composed of attributes and child elements that we put in a map
     */
    final Map<String, Object> objectMap = new HashMap<String, Object>();

    // add attributes, using the predefined prefix to avoid collisions with
    // elements names
    for (final Attribute attr : attributes) {
      objectMap.put(this.mAttrPrefix + attr.getQualifiedName(), attr.getValue());
    }

    // add child elements under their names (recursive call)
    for (final Element elt : childElements) {
      appendObject(objectMap, elt);
    }

    // add all the text content concatenated in one
    final String textContent = element.getTextNormalize();

    if (textContent.length() > 0) {
      objectMap.put(this.mTextAttrName, textContent);
    }

    /*
     * now its time to add our object in the parent container but.. are we
     * the only one with the given name ?
     */
    if (container.containsKey(name)) {
      // we are not alone !
      final Object existingMappedElement = container.get(name);
      List<Object> fraternity;

      if (existingMappedElement instanceof List) {
        // the fraternity is allready organized
        fraternity = (List) existingMappedElement;
        fraternity.add(objectMap);

      } else {
        // we have one self-ignoring brother
        // > create the fraternity
        fraternity = new ArrayList<Object>();
        fraternity.add(existingMappedElement);
        fraternity.add(objectMap);
      }
      container.put(name, fraternity);

    } else {
      // we are truly unique.. (until now)
      container.put(name, objectMap);
    }
  }
Exemplo n.º 7
0
  /**
   * Constructor for switch Block
   *
   * <p>
   *
   * @param applyElement Reference to <code>org.jdom.Element</code> containing "apply" element
   * @param m The parent <code>Model</code>
   */
  @SuppressWarnings("unchecked")
  public BlockMathSwitch(Element applyElement, Model m) {
    // Initialize superblock elements
    super("pending", "switch", m);

    // Parse parts of the Apply element
    Namespace mathml = Namespace.getNamespace("", "http://www.w3.org/1998/Math/MathML");
    Element piecewise = applyElement.getChild("piecewise", mathml);

    // second chance if not qualified
    if (piecewise == null) {
      piecewise = applyElement.getChild("piecewise");
    }

    // take appropriate action based on type
    if (piecewise == null) {
      List<Element> elist = applyElement.getChildren();
      Iterator<Element> elisti = elist.iterator();
      Element el = elisti.next();
      System.err.println(
          "Error - BlockMathSwitch constructor called with"
              + " wrong type element. Expected <piecewise>, found <"
              + el.getQualifiedName()
              + ">.");
    } else {
      // set name of ourself - will be "uniquified" later
      this.setName("switch" + "_" + m.getNumBlocks());

      // look for two children: <piece> and <otherwise>
      List<Element> kids = piecewise.getChildren();
      if (kids.size() < 2) {
        System.err.println(
            "Error - expected at least two child elements (one <piece> and one <otherwise> element); found only "
                + kids.size()
                + " elements total.");
      } else {
        // Get the children that describe switch
        List<Element> pieces = piecewise.getChildren("piece", mathml);
        Element otherwise = piecewise.getChild("otherwise", mathml);

        // check they are not null
        if (pieces.isEmpty()) {
          pieces = piecewise.getChildren("piece"); // 2nd chance
        }
        if (pieces.isEmpty()) {
          System.err.println("Error - <piecewise> element contained no <piece> elements");
          return;
        }
        if (otherwise == null) {
          otherwise = piecewise.getChild("otherwise"); // 2nd chance
        }
        if (otherwise == null) {
          System.err.println("Error - <piecewise> element contained no <otherwise> element");
          return;
        }

        // look at first piece
        Element piece = pieces.get(0);
        List<Element> pieceChildren = piece.getChildren();
        if (pieceChildren.size() != 2) {
          System.err.println(
              "Error - <piece> element can only have two children - found "
                  + pieces.size()
                  + " instead.");
          return;
        }
        List<Element> other = otherwise.getChildren();
        if (other.size() != 1) {
          System.err.println(
              "Error - <otherwise> element needs one child - found " + other.size() + " instead.");
          return;
        }
        // process like:                  this.genInputsFromApply(ikid);
        // piece has two children: output 1 and condition it is used, and
        // otherwise has default condition.
        // Input 1 is passed if condition is true
        this.genInputsFromApply(pieceChildren.iterator(), 1); // input 1 & condition (input 2)

        if (pieces.size() <= 1) {
          // if last piece, then input 3 is the <otherwise> child
          this.genInputsFromApply(other.iterator(), 3); // input 3 (otherwise)
        } else {
          // if not, then recurse after removing the first <piece> element
          boolean childRemoved = this.removeFirstPiece(piecewise);
          if (!childRemoved)
            throw new AssertionError(
                "Unable to remove first child from <piecewise> "
                    + "while trying to build an upstream switch for "
                    + this.getName());
          BlockMathSwitch bms = new BlockMathSwitch(applyElement, m);
          this.addInput(bms, 3);
        }
      }
    }

    // System.out.println("    BlockMathSwitch constructor: " + myName + " created.");
  }