/**
  * Checks if there is a servlet element with specified Servlet name.
  *
  * @param servletName Servlet name to check.
  * @return <code>true</code> if servle with specified name is present, <code>false</code>
  *     otherwise.
  */
 public boolean isAppletDefined(String appletAID) {
   try {
     XPathExpression xPression =
         xPath.compile("/applet-app/applet/applet-AID[text()='" + appletAID + "']"); // NOI18N
     return (Boolean) xPression.evaluate(doc, XPathConstants.BOOLEAN);
   } catch (XPathExpressionException ex) {
     return false;
   }
 }
 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);
 }
 public NodeList getAppletClassElements() {
   try {
     return (NodeList) appletClassXPression.evaluate(doc, XPathConstants.NODESET);
   } catch (XPathExpressionException ex) {
     return null;
   }
 }
Example #4
0
  /**
   * Removes empty #text nodes from a document. From James Murty on this StackOverflow post:
   * http://stackoverflow.com/questions/978810/how-to-strip-whitespace-only-text-nodes-from-a-dom-before-serialization
   *
   * @param doc The document to remove empty text nodes from.
   */
  private static void removeEmptyTextNodes(Document doc) {
    try {
      XPathFactory xpathFactory = XPathFactory.newInstance();
      // XPath to find empty text nodes.
      XPathExpression xpathExp =
          xpathFactory.newXPath().compile("//text()[normalize-space(.) = '']");
      NodeList emptyTextNodes = (NodeList) xpathExp.evaluate(doc, XPathConstants.NODESET);

      // Remove each empty text node from document.
      for (int i = 0; i < emptyTextNodes.getLength(); i++) {
        Node emptyTextNode = emptyTextNodes.item(i);
        emptyTextNode.getParentNode().removeChild(emptyTextNode);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Example #5
0
 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;
 }
  public Collection<String> getAllAppletAIDs() {
    Set<String> names = new HashSet<String>();

    try {
      NodeList nodes = (NodeList) appletAIDXPression.evaluate(doc, XPathConstants.NODESET);
      for (int i = 0; i < nodes.getLength(); i++) {
        names.add(nodes.item(i).getNodeValue());
      }
    } catch (XPathExpressionException ex) {
      Exceptions.printStackTrace(ex);
    }
    return names;
  }
  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();
    }
  }
  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);
    }
  }
  @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;
  }