private QueryResult gatherResultInfoForSelectQuery(
      String queryString, int queryNr, boolean sorted, Document doc, String[] rows) {
    Element root = doc.getRootElement();

    // Get head information
    Element child =
        root.getChild("head", Namespace.getNamespace("http://www.w3.org/2005/sparql-results#"));

    // Get result rows (<head>)
    List headChildren =
        child.getChildren(
            "variable", Namespace.getNamespace("http://www.w3.org/2005/sparql-results#"));

    Iterator it = headChildren.iterator();
    ArrayList<String> headList = new ArrayList<String>();
    while (it.hasNext()) {
      headList.add(((Element) it.next()).getAttributeValue("name"));
    }

    List resultChildren =
        root.getChild("results", Namespace.getNamespace("http://www.w3.org/2005/sparql-results#"))
            .getChildren(
                "result", Namespace.getNamespace("http://www.w3.org/2005/sparql-results#"));
    int nrResults = resultChildren.size();

    QueryResult queryResult = new QueryResult(queryNr, queryString, nrResults, sorted, headList);

    it = resultChildren.iterator();
    while (it.hasNext()) {
      Element resultElement = (Element) it.next();
      String result = "";

      // get the row values and paste it together to one String
      for (int i = 0; i < rows.length; i++) {
        List bindings =
            resultElement.getChildren(
                "binding", Namespace.getNamespace("http://www.w3.org/2005/sparql-results#"));
        String rowName = rows[i];
        for (int j = 0; j < bindings.size(); j++) {
          Element binding = (Element) bindings.get(j);
          if (binding.getAttributeValue("name").equals(rowName))
            if (result.equals(""))
              result +=
                  rowName + ": " + ((Element) binding.getChildren().get(0)).getTextNormalize();
            else
              result +=
                  "\n"
                      + rowName
                      + ": "
                      + ((Element) binding.getChildren().get(0)).getTextNormalize();
        }
      }

      queryResult.addResult(result);
    }
    return queryResult;
  }
예제 #2
0
  /**
   * This will add the prefix mapping to the JDOM <code>Document</code> object.
   *
   * @param prefix <code>String</code> namespace prefix.
   * @param uri <code>String</code> namespace URI.
   */
  public void startPrefixMapping(String prefix, String uri) throws SAXException {

    if (suppress) return;

    Namespace ns = Namespace.getNamespace(prefix, uri);
    declaredNamespaces.add(ns);
  }
예제 #3
0
  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {

    in.defaultReadObject();

    Object prefix = in.readObject();
    Object uri = in.readObject();

    if (prefix != null) { // else leave namespace null here
      namespace = Namespace.getNamespace((String) prefix, (String) uri);
    }
  }
예제 #4
0
 @Test
 public void testGetNamespace() throws FOMException, FluidException, JSONException, IOException {
   // Lets create a new namespace underneath the user's default root namespace
   Namespace testNamespace = new Namespace(this.fdb, "", this.fdb.getUsername());
   String newName = UUID.randomUUID().toString();
   Namespace newNamespace = testNamespace.createNamespace(newName, "This is a test namespace");
   // if we successfully created a new namespace we'll be able to get it from FluidDB
   Namespace gotNamespace = testNamespace.getNamespace(newName);
   assertEquals(newNamespace.getId(), gotNamespace.getId());
   gotNamespace.delete();
 }
예제 #5
0
  /**
   * This reports the occurrence of an actual element. It will include the element's attributes,
   * with the exception of XML vocabulary specific attributes, such as <code>
   * xmlns:[namespace prefix]</code> and <code>xsi:schemaLocation</code>.
   *
   * @param namespaceURI <code>String</code> namespace URI this element is associated with, or an
   *     empty <code>String</code>
   * @param localName <code>String</code> name of element (with no namespace prefix, if one is
   *     present)
   * @param qName <code>String</code> XML 1.0 version of element name: [namespace
   *     prefix]:[localName]
   * @param atts <code>Attributes</code> list for this element
   * @throws SAXException when things go wrong
   */
  public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
      throws SAXException {
    if (suppress) return;

    Element element = null;

    if ((namespaceURI != null) && (!namespaceURI.equals(""))) {
      String prefix = "";

      // Determine any prefix on the Element
      if (!qName.equals(localName)) {
        int split = qName.indexOf(":");
        prefix = qName.substring(0, split);
      }
      Namespace elementNamespace = Namespace.getNamespace(prefix, namespaceURI);
      element = factory.element(localName, elementNamespace);
    } else {
      element = factory.element(localName);
    }

    // Take leftover declared namespaces and add them to this element's
    // map of namespaces
    if (declaredNamespaces.size() > 0) {
      transferNamespaces(element);
    }

    // Handle attributes
    for (int i = 0, len = atts.getLength(); i < len; i++) {
      Attribute attribute = null;

      String attLocalName = atts.getLocalName(i);
      String attQName = atts.getQName(i);
      int attType = getAttributeType(atts.getType(i));

      // Bypass any xmlns attributes which might appear, as we got
      // them already in startPrefixMapping().
      // This is sometimes necessary when SAXHandler is used with
      // another source than SAXBuilder, as with JDOMResult.
      if (attQName.startsWith("xmlns:") || attQName.equals("xmlns")) {
        continue;
      }

      // First clause per http://markmail.org/message/2p245ggcjst27xe6
      // patch from Mattias Jiderhamn
      if ("".equals(attLocalName) && attQName.indexOf(":") == -1) {
        attribute = factory.attribute(attQName, atts.getValue(i), attType);
      } else if (!attQName.equals(attLocalName)) {
        String attPrefix = attQName.substring(0, attQName.indexOf(":"));
        Namespace attNs = Namespace.getNamespace(attPrefix, atts.getURI(i));

        attribute = factory.attribute(attLocalName, atts.getValue(i), attType, attNs);
      } else {
        attribute = factory.attribute(attLocalName, atts.getValue(i), attType);
      }
      factory.setAttribute(element, attribute);
    }

    flushCharacters();

    if (atRoot) {
      document.setRootElement(element); // XXX should we use a factory call?
      atRoot = false;
    } else {
      factory.addContent(getCurrentElement(), element);
    }
    currentElement = element;
  }
예제 #6
0
  void transform() throws JDOMException, IOException {

    // EXMARaLDAs "transformer class" - true für 2.0
    StylesheetFactory ssf = new StylesheetFactory(true);
    try {
      // the first transformation, resulting in a String...
      String firstCMDI = ssf.applyExternalStylesheetToExternalXMLFile(xsltFile, comaFile);
      // ...from which we build a Document
      SAXBuilder builder = new SAXBuilder();
      StringReader in = new StringReader(firstCMDI);
      cmdi = builder.build(in);
      // set the namespace
      Namespace ns = Namespace.getNamespace("clarin", "http://www.clarin.eu/cmd/");

      // Replace variable CORPUS_NAME
      // in Header (MdSelfLink, ResourceRef), HZSKCorpusMetadata (Name, ID))
      XPath xURLs = XPath.newInstance("//clarin:MdSelfLink | //clarin:ResourceRef");
      xURLs.addNamespace(ns);

      List allURLs = xURLs.selectNodes(cmdi);

      String[] splitFile = comaFile.split("\\\\");
      String comaFileName = splitFile[splitFile.length - 1].replace(".coma", "");

      for (Object o : allURLs) {

        Element url = (Element) o;
        String selfLink = url.getText();
        url.setText(selfLink.replace("CORPUS_NAME", comaFileName));
      }

      // Replace language codes with names
      // in
      XPath xLanguageNames = XPath.newInstance("");
      xLanguageNames.addNamespace(ns);
      List allLanguageNames = xLanguageNames.selectNodes(cmdi);

      for (Object o : allLanguageNames) {

        Element languageName = (Element) o;
        languageName.setText(ISOLanguageCodeHelper.getLanguageName(languageName.getText()));
      }

      // Replace language names with codes
      // in DocumentationLanguages
      XPath xLanguageCodes =
          XPath.newInstance("//clarin:DocumentationLanguages/descendant::clarin:iso-639-3-code");
      xLanguageCodes.addNamespace(ns);
      List allLanguageCodes = xLanguageCodes.selectNodes(cmdi);

      for (Object o : allLanguageCodes) {

        Element languageCode = (Element) o;
        languageCode.setText(ISOLanguageCodeHelper.getLanguageCode(languageCode.getText()));
      }

      // Resources
      // add value of mimetype attribute to resourceType elements
      XPath xResourceTypes = XPath.newInstance("//clarin:ResourceType");
      xResourceTypes.addNamespace(ns);

      List allResourceTypes = xResourceTypes.selectNodes(cmdi);

      for (Object o : allResourceTypes) {

        Element resourceType = (Element) o;
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String filename = resourceType.getAttributeValue("mimetype").toLowerCase();
        String mimetype = fileNameMap.getContentTypeFor(filename);
        if (mimetype != null) {
          resourceType.setAttribute("mimetype", mimetype);
        } else if (filename.endsWith("exb") || filename.endsWith("exs")) {
          resourceType.setAttribute("mimetype", "text/xml");
        } else {
          resourceType.setAttribute("mimetype", "unknown");
        }
      }

      // Language

      // hzsk-corpus-materials
      // file size for the Size-elements
      XPath xMediaFiles =
          XPath.newInstance("//clarin:hzsk-corpus-materials/descendant::clarin:MediaFile");
      xMediaFiles.addNamespace(ns);
      List allFiles = xMediaFiles.selectNodes(cmdi);

      for (Object o : allFiles) {

        Element mediafile = (Element) o;

        File asocFile = new File(mediafile.getChild("ResourceLink", ns).getText());
        // es wird jetzt nicht sehr genau, aber das taugt...
        Element size = mediafile.getChild("Size", ns);
        size.addContent(String.valueOf(asocFile.length()));
      }

      // write CMDI-file
      Writer fileWriter = new FileWriter(cmdiFile);

      XMLOutputter serializer = new XMLOutputter();
      serializer.output(cmdi, fileWriter);

    } catch (SAXException ex) {
      Logger.getLogger(Coma2CMDI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParserConfigurationException ex) {
      Logger.getLogger(Coma2CMDI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (TransformerConfigurationException ex) {
      Logger.getLogger(Coma2CMDI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (TransformerException ex) {
      Logger.getLogger(Coma2CMDI.class.getName()).log(Level.SEVERE, null, ex);
    }
  }