Beispiel #1
0
  /**
   * Gets the string of an XML document containing multiple words for saving purposes.
   *
   * @param words The words to save.
   * @return String containing XML containing all words.
   */
  public static String getMultipleWordXML(Word[] words) {
    try {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.newDocument();
      Element root = doc.createElement("Root");
      for (int i = 0; i < words.length; i++) {
        root.appendChild(words[i].getWordXMLNode(doc));
      }
      doc.appendChild(root);
      return getStringFromDocument(doc);

    } catch (Exception ex) {
      ex.printStackTrace();
      return null;
    }
  }
Beispiel #2
0
 /**
  * Gets the definitions of a word, sets the main one if necessary, and returns it.
  *
  * @param overwrite if true, then function will attempt to retrieve from online even if the
  *     definition is already set.
  * @return String containing the main definition of the word.
  */
 public static Word[] getWordsFromFile(ByteArrayInputStream inputStream) {
   try {
     // now to parse the XML
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     DocumentBuilder db = dbf.newDocumentBuilder();
     Document doc = db.parse(inputStream);
     removeEmptyTextNodes(doc);
     NodeList wordList = doc.getElementsByTagName("WordDefinitions");
     int wordCount = wordList.getLength();
     Word[] returnWords = new Word[wordCount];
     for (int wordi = 0; wordi < wordCount; wordi++) {
       NodeList list = ((Element) wordList.item(wordi)).getElementsByTagName("Definition");
       Element wordElement = (Element) wordList.item(wordi);
       String word = wordElement.getAttribute("word");
       String mainDefinition = wordElement.getAttribute("mainDefinition");
       int numDefinitions = list.getLength();
       Definition[] wordOtherDefinitions = new Definition[numDefinitions];
       for (int i = 0; i < numDefinitions; i++) {
         Node node = list.item(i);
         NodeList children = node.getChildNodes(); // gets each child node of the definition
         // get all data
         String definition = children.item(XML_DEFINITION_LOC).getTextContent();
         String theWord = children.item(XML_WORD_LOC).getTextContent();
         NodeList dictionaryNode = children.item(XML_DICTIONARY_BRANCH_LOC).getChildNodes();
         String dictionaryId = dictionaryNode.item(XML_DICTIONARY_ID_LOC).getTextContent();
         String dictionaryName = dictionaryNode.item(XML_DICTIONARY_NAME_LOC).getTextContent();
         // now we've got all the data lets add it to the array
         wordOtherDefinitions[i] =
             new Definition(dictionaryName, dictionaryId, definition, theWord);
       }
       if (wordOtherDefinitions.length == 0) {
         wordOtherDefinitions =
             new Definition[] {new Definition("nil", "nil", "NO DEFINITION FOUND", word)};
       }
       Word currentWord = new Word(word, mainDefinition);
       currentWord.setOtherDefinitions(wordOtherDefinitions);
       returnWords[wordi] = currentWord;
     }
     return returnWords;
   } catch (Exception ex) {
     ex.printStackTrace();
     return null;
   }
 }
 public void addAppletElement(String appletAID, String className) {
   Element servlet = doc.createElement(XmlTagNames.XML_APPLET_TAG);
   Element sName = doc.createElement(XmlTagNames.XML_APPLET_AID_TAG);
   sName.setTextContent(appletAID);
   Element sClass = doc.createElement(XmlTagNames.XML_APPLET_CLASS_TAG);
   sClass.setTextContent(className);
   servlet.appendChild(sClass);
   servlet.appendChild(sName);
   getAppletAppNode().appendChild(servlet);
 }
  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();
    }
  }
Beispiel #5
0
  /**
   * Gets XML string for this definition so that it may be printed.
   *
   * @return XML node that can be saved of this word.
   */
  public Element getWordXMLNode(Document doc) {
    try {
      Element rootWordElement = doc.createElement("WordDefinitions");
      rootWordElement.setAttribute("word", this.word);
      rootWordElement.setAttribute("mainDefinition", this.getMainDefinition());
      if (this.otherDefinitions == null) {
        this.getDefinitions();
      }
      for (int i = 0; i < this.otherDefinitions.length; i++) {
        // main definition node
        Element definition = doc.createElement("Definition");

        // word
        Element word = doc.createElement("Word");
        word.setTextContent(otherDefinitions[i].getWord());

        // dictionary
        Element dictionary = doc.createElement("Dictionary");
        Element id = doc.createElement("Id");
        id.setTextContent(otherDefinitions[i].getId());
        Element source = doc.createElement("Name");
        source.setTextContent(otherDefinitions[i].getSource());
        dictionary.appendChild(id);
        dictionary.appendChild(source);

        // definition
        Element wordDefinition = doc.createElement("WordDefinition");
        wordDefinition.setTextContent(otherDefinitions[i].getDefinition());

        // append all the children
        definition.appendChild(word);
        definition.appendChild(dictionary);
        definition.appendChild(wordDefinition);
        rootWordElement.appendChild(definition);
      }
      return rootWordElement;
    } catch (Exception ex) {
      ex.printStackTrace();
      return null;
    }
  }