コード例 #1
0
 private String getConfigValue(String setting, String name, Document doc)
     throws XPathExpressionException {
   XPathFactory xPathfactory = XPathFactory.newInstance();
   XPath xpath = xPathfactory.newXPath();
   XPathExpression userExpr =
       xpath.compile(
           "wikipediaminer/setting[@name=\""
               + setting
               + "\"]/param[@name=\""
               + name
               + "\"]/@value");
   return userExpr.evaluate(doc);
 }
コード例 #2
0
 private PageManifestDef loadRootPageDefinition(String manifestPathOrPath)
     throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
   PageManifestDef pmd = new PageManifestDef();
   Document document = readPageDefinitionFile(manifestPathOrPath, pmd);
   pmd.setSelectorStrategy(selectorExpr.evaluate(document));
   return pmd;
 }
コード例 #3
0
ファイル: ConfigEditor.java プロジェクト: kaine-shaman/jcfg
 private NodeList findNodes(Document doc, Node operation) {
   List<Node> xpaths = getChildNodes(operation, "xpath");
   if (xpaths.isEmpty()) {
     return null;
   }
   String xpathExpression = xpaths.get(0).getTextContent();
   if (xpathExpression == null) {
     return null;
   }
   XPathFactory xPathfactory = XPathFactory.newInstance();
   XPath xpath = xPathfactory.newXPath();
   NodeList nl = null;
   try {
     XPathExpression expr = xpath.compile(xpathExpression);
     nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
   } catch (XPathExpressionException ex) {
     Utils.onError(new Error.WrongXpathExpression(xpathExpression));
   }
   return nl;
 }
  private SignatureData createSignatureDataFromXmlDigSig(String signature)
      throws SignatureException {

    // Start with setting some fields that are known directly
    SignatureData signatureData = new SignatureData();
    signatureData.setClientType(new ELegType("test", "test", PkiClient.NEXUS_PERSONAL_4X));
    signatureData.setSignature(new String(Base64.encode(signature.getBytes())));

    // For the rest we need to parse the XML
    try {
      Document document = createDocument(new ByteArrayInputStream(signature.getBytes()), false);

      // nonce and tbs
      XPath xpath = XPathFactory.newInstance().newXPath();
      XPathExpression expression =
          xpath.compile("/Signature/Object/bankIdSignedData/srvInfo/nonce/text()");
      String encodedNonce = (String) expression.evaluate(document, XPathConstants.STRING);
      String decodedNonce =
          new String(Base64.decode(encodedNonce)); // will be re-encoded again before it is sent
      signatureData.setNonce(decodedNonce);

      expression = xpath.compile("/Signature/Object/bankIdSignedData/usrVisibleData/text()");
      String encodedTbs = (String) expression.evaluate(document, XPathConstants.STRING);
      String decodedTbs =
          new String(Base64.decode(encodedTbs)); // this will be re-encoded again before it is sent

      signatureData.setEncodedTbs(encodedTbs);
      signatureData.setTbs(decodedTbs);

    } catch (XPathExpressionException e) {
      throw new SignatureException(e);
    } catch (ParserConfigurationException e) {
      throw new SignatureException(e);
    } catch (SAXException e) {
      throw new SignatureException(e);
    } catch (IOException e) {
      throw new SignatureException(e);
    }

    return signatureData;
  }
コード例 #5
0
ファイル: Tools.java プロジェクト: tanayuk/RestFixture
 /**
  * extract the XPath from the content. the return value type is passed in input using one of the
  * {@link XPathConstants}. See also {@link XPathExpression#evaluate(Object item, QName
  * returnType)} ;
  *
  * @param ns
  * @param xpathExpression
  * @param content
  * @param returnType
  * @param charset
  * @return the result
  */
 public static Object extractXPath(
     Map<String, String> ns,
     String xpathExpression,
     String content,
     QName returnType,
     String charset) {
   if (null == ns) {
     ns = new HashMap<String, String>();
   }
   String ch = charset;
   if (ch == null) {
     ch = Charset.defaultCharset().name();
   }
   Document doc = toDocument(content, charset);
   XPathExpression expr = toExpression(ns, xpathExpression);
   try {
     Object o = expr.evaluate(doc, returnType);
     return o;
   } catch (XPathExpressionException e) {
     throw new IllegalArgumentException("xPath expression cannot be executed: " + xpathExpression);
   }
 }
コード例 #6
0
ファイル: SWIFT_PING.java プロジェクト: belaran/JGroups
    public Credentials authenticate() throws Exception {
      HttpURLConnection urlConnection =
          new ConnBuilder(authUrl)
              .addHeader(HttpHeaders.CONTENT_TYPE_HEADER, "application/json")
              .addHeader(HttpHeaders.ACCEPT_HEADER, "application/xml")
              .getConnection();

      StringBuilder jsonBuilder = new StringBuilder();
      jsonBuilder
          .append("{\"auth\": {\"tenantName\": \"")
          .append(tenant)
          .append("\", \"passwordCredentials\": {\"username\": \"")
          .append(username)
          .append("\", \"password\": \"")
          .append(password)
          .append("\"}}}");

      HttpResponse response =
          Utils.doOperation(urlConnection, jsonBuilder.toString().getBytes(), true);

      if (response.isSuccessCode()) {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(response.payload));

        String authToken = (String) tokenIdExpression.evaluate(doc, XPathConstants.STRING);
        String storageUrl = (String) publicUrlExpression.evaluate(doc, XPathConstants.STRING);

        log.trace("Authentication successful");

        return new Credentials(authToken, storageUrl);
      } else {
        throw new IllegalStateException(
            "Error authenticating to the service. Please check your credentials. Code = "
                + response.code);
      }
    }
コード例 #7
0
  /**
   * Gets Xpath results
   *
   * @param contextNode
   * @param xpathValue
   * @return
   * @throws XPathExpressionException
   */
  private NodeList getXPathResults(Node contextNode, String xpathValue)
      throws XPathExpressionException {

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    // see if the request root is in a namespace
    String namespace = contextNode.getNamespaceURI();
    // name spaces are used, so we need to lookup the correct
    // prefix to use in the search string
    NamedNodeMap namedNodeMap = contextNode.getAttributes();

    Map<String, String> nsMap = new HashMap<String, String>();

    for (int i = 0; i < namedNodeMap.getLength(); i++) {
      Node n = namedNodeMap.item(i);
      // we found the matching namespace, so get the prefix
      // and then break out
      String prefix = DOMHelper.getLocalName(n);
      String nodeValue = n.getNodeValue();
      nsMap.put(prefix, nodeValue);
    }

    // if there is not any namespace is defined for content element, default XACML request
    //  name space would be there.
    if (XACMLConstants.REQUEST_CONTEXT_3_0_IDENTIFIER.equals(namespace)
        || XACMLConstants.REQUEST_CONTEXT_2_0_IDENTIFIER.equals(namespace)
        || XACMLConstants.REQUEST_CONTEXT_1_0_IDENTIFIER.equals(namespace)) {
      nsMap.put("xacml", namespace);
    }

    NamespaceContext namespaceContext = new DefaultNamespaceContext(nsMap);
    xpath.setNamespaceContext(namespaceContext);

    XPathExpression expression = xpath.compile(xpathValue);
    return (NodeList) expression.evaluate(contextNode, XPathConstants.NODESET);
  }
コード例 #8
0
  public void parseStartup(File file) {
    Logger.getLogger(com.bombdiggity.amazon.ec2.install.Installer.class)
        .info((new StringBuilder("InstallParser.parseStartup: ")).append(file).toString());
    try {
      Document doc = loadFile(file);
      if (doc != null) {
        XPathFactory factory = XMLUtils.newXPathFactory();
        XPath xpath = factory.newXPath();
        String rootXPath = "/Startup/Commands/*";
        Element root = doc.getDocumentElement();
        XPathExpression rootExp = xpath.compile(rootXPath);
        NodeList streamList = (NodeList) rootExp.evaluate(root, XPathConstants.NODESET);
        if (streamList != null) {
          for (int i = 0; i < streamList.getLength(); i++) {
            Node streamNode = streamList.item(i);
            Element streamElem = (Element) streamNode;
            if (streamElem.getNodeName().toLowerCase().equals("install")) {
              String packageName = null;
              String folderPath = null;
              for (Node child = streamNode.getFirstChild();
                  child != null;
                  child = child.getNextSibling())
                if (child.getNodeName().toLowerCase().equals("package"))
                  packageName = XMLUtils.getNodeValue(child).trim();
                else if (child.getNodeName().toLowerCase().equals("folder"))
                  folderPath = XMLUtils.getNodeValue(child).trim();

              if (packageName != null) InstallCommands.installPackage(session, packageName);
              else if (folderPath != null) InstallCommands.installFolder(session, folderPath);
              else
                Logger.getLogger(com.bombdiggity.amazon.ec2.install.InstallParser.class)
                    .error("StartupParser.loadFile: <Install>: <Package> or <Folder> required");
            } else if (streamElem.getNodeName().toLowerCase().equals("download")) {
              String url = null;
              String method = "get";
              String data = null;
              String destination = "/opt";
              String action = null;
              List headers = new ArrayList();
              for (Node child = streamNode.getFirstChild();
                  child != null;
                  child = child.getNextSibling())
                if (child.getNodeName().toLowerCase().equals("url"))
                  url = XMLUtils.getNodeValue(child).trim();
                else if (child.getNodeName().toLowerCase().equals("method"))
                  method = XMLUtils.getNodeValue(child).toLowerCase().trim();
                else if (child.getNodeName().toLowerCase().equals("data"))
                  data = XMLUtils.getNodeValue(child).trim();
                else if (child.getNodeName().toLowerCase().equals("destination"))
                  destination = XMLUtils.getNodeValue(child).trim();
                else if (child.getNodeName().toLowerCase().equals("action"))
                  action = XMLUtils.getNodeValue(child).toLowerCase().trim();
                else if (child.getNodeName().toLowerCase().equals("header")) {
                  Node nameNode = XMLUtils.getNodeByTagName((Element) child, "Name");
                  Node valueNode = XMLUtils.getNodeByTagName((Element) child, "Value");
                  if (nameNode != null && valueNode != null) {
                    Map namePair = new HashMap();
                    namePair.put(
                        XMLUtils.getNodeValue(nameNode).trim(),
                        XMLUtils.getNodeValue(valueNode).trim());
                    headers.add(namePair);
                  } else {
                    Logger.getLogger(com.bombdiggity.amazon.ec2.install.InstallParser.class)
                        .error(
                            "StartupParser.loadFile: <Download/Header>: <Name> and <Value> required");
                  }
                }

              if (url != null && destination != null)
                InstallCommands.downloadFile(
                    session, url, method, data, headers, destination, action);
              else
                Logger.getLogger(com.bombdiggity.amazon.ec2.install.InstallParser.class)
                    .error("StartupParser.loadFile: <Download>: <URL> and <Destination> required");
            } else if (streamElem.getNodeName().toLowerCase().equals("s3fetch")) {
              String awsAccessKeyId = null;
              String awsSecretAccessKey = null;
              String bucket = null;
              String key = null;
              String destination = null;
              String action = null;
              for (Node child = streamNode.getFirstChild();
                  child != null;
                  child = child.getNextSibling())
                if (child.getNodeName().toLowerCase().equals("awsaccesskeyid"))
                  awsAccessKeyId = XMLUtils.getNodeValue(child).trim();
                else if (child.getNodeName().toLowerCase().equals("awssecretaccesskey"))
                  awsSecretAccessKey = XMLUtils.getNodeValue(child).trim();
                else if (child.getNodeName().toLowerCase().equals("bucket"))
                  bucket = XMLUtils.getNodeValue(child).trim();
                else if (child.getNodeName().toLowerCase().equals("key"))
                  key = XMLUtils.getNodeValue(child).trim();
                else if (child.getNodeName().toLowerCase().equals("destination"))
                  destination = XMLUtils.getNodeValue(child).trim();
                else if (child.getNodeName().toLowerCase().equals("action"))
                  action = XMLUtils.getNodeValue(child).toLowerCase().trim();

              if (awsAccessKeyId != null
                  && awsSecretAccessKey != null
                  && bucket != null
                  && key != null
                  && destination != null)
                InstallCommands.fetchFile(
                    session, awsAccessKeyId, awsSecretAccessKey, bucket, key, destination, action);
              else
                Logger.getLogger(com.bombdiggity.amazon.ec2.install.InstallParser.class)
                    .error(
                        "StartupParser.loadFile: <Fetch>: <AWSAccessKeyId>, <AWSSecretAccessKey>, <Bucket>, <Key> and <Destination> required");
            } else if (streamElem.getNodeName().toLowerCase().equals("runscript")) {
              String script = null;
              List params = new ArrayList();
              for (Node child = streamNode.getFirstChild();
                  child != null;
                  child = child.getNextSibling())
                if (child.getNodeName().toLowerCase().equals("script"))
                  script = XMLUtils.getNodeValue(child).trim();
                else if (child.getNodeName().toLowerCase().equals("param")) {
                  String param = XMLUtils.getNodeValue(child).trim();
                  params.add(param);
                }

              if (script != null) InstallCommands.runScript(session, script, params);
              else
                Logger.getLogger(com.bombdiggity.amazon.ec2.install.InstallParser.class)
                    .error("StartupParser.loadFile: <RunScript>: <Script> required");
            }
          }
        }
      }
    } catch (Exception e) {
      Logger.getLogger(com.bombdiggity.amazon.ec2.install.InstallParser.class)
          .error(
              (new StringBuilder("InstallParser.parseStartup: ")).append(e.toString()).toString());
      e.printStackTrace();
    }
  }
コード例 #9
0
  public static void main(String[] args) {
    try {
      DocumentBuilderFactory db = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = db.newDocumentBuilder();
      Document dom = builder.parse("data/geographic-area-data.html");
      XPathFactory xpath = XPathFactory.newInstance();
      XPath path = xpath.newXPath();
      XPathExpression table =
          path.compile("//div[@id='mw-content-text']/table[contains(@class,'wikitable')]/tr");
      NodeList wikiData = (NodeList) table.evaluate(dom, XPathConstants.NODESET);

      NodeList children;
      String currentData, cleanData;

      /* Open output stream */
      FileWriter fstream = new FileWriter("data/parsed.yaml");
      BufferedWriter out = new BufferedWriter(fstream);

      for (int i = 0; i < wikiData.getLength(); i++) {
        if (i == 0) {
          continue;
        }
        out.write(new Integer(i).toString() + ":\n");
        children = wikiData.item(i).getChildNodes();
        for (int j = 0; j < children.getLength(); j++) {
          currentData = (String) children.item(j).getTextContent();
          switch (j) {
            case 0:
              /* Current Data is empty */
              break;
            case 1:
              cleanData = decompose(currentData).trim().replaceAll("[^a-zA-Z\\s]+", "");
              out.write("\t\"Geographic entity\": \"" + cleanData + "\"\n");
              break;
            case 2:
              /* Current Data is empty */
              break;
            case 3:
              cleanData = decompose(currentData).trim().replaceAll(",", "");
              out.write("\t\"Area\": \"" + cleanData + "\"\n");
              break;
            case 4:
              /* Current Data is empty */
              break;
            case 5:
              cleanData = decompose(currentData).trim();
              out.write("\t\"Notes\": \"" + cleanData + "\"\n");
              break;
            case 6:
              /* Current Data is empty */
              break;
            default:
              /* System.out.println("[" + j + "] Hit default case statement. Current Data is: " + currentData); */
              break;
          }
        }
      }
      /* Close output stream */
      out.close();
    } catch (Exception e) {
      System.out.println(e);
    }
  }
コード例 #10
0
ファイル: Library.java プロジェクト: pwansch/iTradeTunes
  protected Document parse(InputStream inputStream) throws LibraryException {
    Map<Integer, SongBean> songMap = new HashMap<Integer, SongBean>();
    Map<Integer, AlbumBean> albumMap = new HashMap<Integer, AlbumBean>();
    Map<Integer, AlbumBean> secondaryAlbumMap = new HashMap<Integer, AlbumBean>();
    warningList.clear();

    try {
      // Create a builder factory
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      factory.setValidating(false);
      factory.setNamespaceAware(true);
      factory.setIgnoringElementContentWhitespace(true);
      factory.setIgnoringComments(true);

      // Create the builder and parse the file
      DocumentBuilder builder = factory.newDocumentBuilder();

      // Set an error listener and parse the document
      builder.setErrorHandler(new iTradeTunesLibraryErrorHandler());
      builder.setEntityResolver(new iTradeTunesLibraryResolver());
      Document document = builder.parse(inputStream);

      synchronized (libraryList) {
        // Query the library document and build the library list
        XPath xPath = XPathFactory.newInstance().newXPath();
        XPathExpression xPathExpression = xPath.compile(XPATH_LIBRARY_LIST);
        NodeList nodelist = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET);

        // Process the elements in the nodelist
        SongBean song = null;

        for (int i = 0; i < nodelist.getLength(); i++) {
          boolean isTrackID = false;

          // Get element and child nodes
          Element elem = (Element) nodelist.item(i);
          NodeList list = elem.getChildNodes();

          // Get node value
          Node childKey = list.item(0);
          String key = childKey.getNodeValue();

          // Check if we have to create a new bean
          if (SongBean.NAME_TRACK_ID.equals(key)) {
            isTrackID = true;
            SongBean previousSong = song;
            song = new SongBean();
            if (previousSong != null
                && !("AAC audio file".equals(previousSong.getKind())
                    || "MPEG audio file".equals(previousSong.getKind()))) {
              songMap.remove(previousSong.getTrack_ID());
            } else {
              // Add an album bean
              addOrUpdateAlbum(albumMap, previousSong, false);
            }
          }

          // The first parameter is the key
          String prop = childKey.getNodeValue().replace(' ', '_');

          // The second parameter is the value
          i++;

          // Get element and child nodes
          elem = (Element) nodelist.item(i);

          // Check for boolean properties
          Object value = null;
          // Get node value
          list = elem.getChildNodes();
          childKey = list.item(0);
          value = (childKey == null) ? elem.getNodeName() : childKey.getNodeValue();

          if (isTrackID) {
            isTrackID = false;
          }

          // Set the property of the song bean
          Statement stmt = new Statement(song, "set" + prop, new Object[] {value});
          try {
            stmt.execute();

          } catch (Exception e) {
            // Ignore that field, we do not have it in our bean
          }

          // If the property is the track ID, add the song to the hash
          // map
          if (SongBean.NAME_TRACK_ID.equals(key)) {
            int trackID = Integer.valueOf((String) value);
            songMap.put(trackID, song);
          }
        }

        // Update album for last song
        addOrUpdateAlbum(albumMap, song, false);

        // Check the album map for inconsistencies
        Iterator<AlbumBean> albums = albumMap.values().iterator();
        while (albums.hasNext()) {
          AlbumBean album = albums.next();
          if (album.checkConsistency()) {
            libraryList.add(album);
            album.setHashCode();
          } else {
            // Add an inconsistent album only using the album title
            SongBean[] songs = album.getSongs();
            for (int i = 0; i < songs.length; i++) {
              addOrUpdateAlbum(secondaryAlbumMap, songs[i], true);
            }
          }
        }

        // Check secondary album map for consistency
        albums = secondaryAlbumMap.values().iterator();
        while (albums.hasNext()) {
          AlbumBean album = albums.next();
          if (album.checkConsistency()) {
            libraryList.add(album);
            album.setHashCode();
          } else {
            // This album cannot be matched
            // TODO: Add to warning message
          }
        }

        setChanged();
      }

      return document;
    } catch (IOException ioe) {
      // Log an expected connect exception
      throw new LibraryException(ioe);
    } catch (SAXException se) {
      // Catch all other exceptions
      throw new LibraryException(se);
    } catch (ParserConfigurationException pce) {
      // Catch all other exceptions
      Utils.logSevere(pce);
      throw new LibraryException(pce);
    } catch (XPathExpressionException xpe) {
      // Catch all other exceptions
      Utils.logSevere(xpe);
      throw new LibraryException(xpe);
    } catch (NumberFormatException nfe) {
      // Catch all other exceptions
      throw new LibraryException(nfe);
    }
  }
コード例 #11
0
  @Override
  public HashSet<ScoredAnnotation> solveSa2W(String text) throws AnnotationException {
    HashSet<ScoredAnnotation> res;
    try {
      res = new HashSet<ScoredAnnotation>();
      lastTime = Calendar.getInstance().getTimeInMillis();

      URL wikiApi = new URL(url);
      String parameters =
          "references=true&repeatMode=all&minProbability=0.0&source="
              + URLEncoder.encode(text, "UTF-8");
      HttpURLConnection slConnection = (HttpURLConnection) wikiApi.openConnection();
      slConnection.setRequestProperty("accept", "text/xml");
      slConnection.setDoOutput(true);
      slConnection.setDoInput(true);
      slConnection.setRequestMethod("POST");
      slConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      slConnection.setRequestProperty("charset", "utf-8");
      slConnection.setRequestProperty(
          "Content-Length", "" + Integer.toString(parameters.getBytes().length));
      slConnection.setUseCaches(false);

      DataOutputStream wr = new DataOutputStream(slConnection.getOutputStream());
      wr.writeBytes(parameters);
      wr.flush();
      wr.close();
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document doc = builder.parse(slConnection.getInputStream());

      /*			URL wikiApi = new URL(url+"?references=true&repeatMode=all&minProbability=0.0&source="+URLEncoder.encode(text, "UTF-8"));
      			URLConnection wikiConnection = wikiApi.openConnection();
      			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      			DocumentBuilder builder = factory.newDocumentBuilder();
      			Document doc = builder.parse(wikiConnection.getInputStream());
      */

      lastTime = Calendar.getInstance().getTimeInMillis() - lastTime;

      XPathFactory xPathfactory = XPathFactory.newInstance();
      XPath xpath = xPathfactory.newXPath();
      XPathExpression idExpr = xpath.compile("//detectedTopic/@id");
      XPathExpression weightExpr = xpath.compile("//detectedTopic/@weight");
      XPathExpression referenceExpr = xpath.compile("//detectedTopic/references");

      NodeList ids = (NodeList) idExpr.evaluate(doc, XPathConstants.NODESET);
      NodeList weights = (NodeList) weightExpr.evaluate(doc, XPathConstants.NODESET);
      NodeList references = (NodeList) referenceExpr.evaluate(doc, XPathConstants.NODESET);

      for (int i = 0; i < weights.getLength(); i++) {
        if (weights.item(i).getNodeType() != Node.TEXT_NODE) {
          int id = Integer.parseInt(ids.item(i).getNodeValue());
          float weight = Float.parseFloat(weights.item(i).getNodeValue());
          //					System.out.println("ID="+ids.item(i).getNodeValue()+" weight="+weight);
          XPathExpression startExpr =
              xpath.compile("//detectedTopic[@id=" + id + "]/references/reference/@start");
          XPathExpression endExpr =
              xpath.compile("//detectedTopic[@id=" + id + "]/references/reference/@end");
          NodeList starts =
              (NodeList) startExpr.evaluate(references.item(i), XPathConstants.NODESET);
          NodeList ends = (NodeList) endExpr.evaluate(references.item(i), XPathConstants.NODESET);
          for (int j = 0; j < starts.getLength(); j++) {
            int start = Integer.parseInt(starts.item(j).getNodeValue());
            int end = Integer.parseInt(ends.item(j).getNodeValue());
            int len = end - start;
            res.add(new ScoredAnnotation(start, len, id, weight));
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      throw new AnnotationException(
          "An error occurred while querying Wikipedia Miner API. Message: " + e.getMessage());
    }
    return res;
  }