Java Code Examples for nu.xom.Document#getRootElement()

The following examples show how to use nu.xom.Document#getRootElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SettingsManager.java    From ciscorouter with MIT License 6 votes vote down vote up
/**
 * Constructs the class. Requires settings.xml to be present to run
 */
public SettingsManager() {
    String currentDir = System.getProperty("user.dir");
    String settingDir = currentDir + "/settings/";
    File f = new File(settingDir + "settings.xml");
    if (f.exists() && !f.isDirectory()) {
        try {
            Builder parser = new Builder();
            Document doc   = parser.build(f);
            Element root   = doc.getRootElement();

            Element eUsername = root.getFirstChildElement("Username");
            username = eUsername.getValue();

            Element ePassword = root.getFirstChildElement("Password");
            password = ePassword.getValue();

            requiresAuth = true;
        } catch (ParsingException|IOException e) {
            e.printStackTrace();
        }
    }
    else {
        requiresAuth = false;
    }
}
 
Example 2
Source File: XmlPersistenceRead.java    From rest-client with Apache License 2.0 5 votes vote down vote up
protected Request xml2Request(final Document doc)
        throws MalformedURLException, XMLException {
    // get the rootNode
    Element rootNode = doc.getRootElement();

    if (!"rest-client".equals(rootNode.getQualifiedName())) {
        throw new XMLException("Root node is not <rest-client>");
    }

    // checking correct rest version
    final String rcVersion = rootNode.getAttributeValue("version");
    try {
        Versions.versionValidCheck(rcVersion);
    }
    catch(Versions.VersionValidationException ex) {
        throw new XMLException(ex);
    }
    
    readVersion = rcVersion;

    // if more than two request element is present then throw the exception 
    if (rootNode.getChildElements().size() != 1) {
        throw new XMLException("There can be only one child node for root node: <request>");
    }
    // minimum one request element is present in xml 
    if (rootNode.getFirstChildElement("request") == null) {
        throw new XMLException("The child node of <rest-client> should be <request>");
    }
    Element requestNode = rootNode.getFirstChildElement("request");
    
    return getRequestBean(requestNode);
}
 
Example 3
Source File: XMLCollectionUtil.java    From rest-client with Apache License 2.0 5 votes vote down vote up
public static List<Request> getRequestCollectionFromXMLFile(final File f)
        throws IOException, XMLException {
    XmlPersistenceRead xUtlRead = new XmlPersistenceRead();
    
    List<Request> out = new ArrayList<>();
    Document doc = xUtlRead.getDocumentFromFile(f);
    Element eRoot = doc.getRootElement();
    if(!"request-collection".equals(eRoot.getLocalName())) {
        throw new XMLException("Expecting root element <request-collection>, but found: "
                + eRoot.getLocalName());
    }
    final String version = eRoot.getAttributeValue("version");
    try {
        Versions.versionValidCheck(version);
    }
    catch(Versions.VersionValidationException ex) {
        throw new XMLException(ex);
    }
    xUtlRead.setReadVersion(version);
    
    Elements eRequests = doc.getRootElement().getChildElements();
    for(int i=0; i<eRequests.size(); i++) {
        Element eRequest = eRequests.get(i);
        Request req = xUtlRead.getRequestBean(eRequest);
        out.add(req);
    }
    return out;
}
 
Example 4
Source File: ResponseCommand.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void verify(CommandCall commandCall, Evaluator evaluator, ResultRecorder resultRecorder) {
  ValidationResult validationResult;
  if (!StringUtils.isBlank(verificationMethod)) {
    evaluator.setVariable("#nl_knaw_huygens_httpcommand_result", requestCommand.getActualResult());
    evaluator.setVariable("#nl_knaw_huygens_httpcommand_expectation", expectation);
    validationResult = (ValidationResult) evaluator.evaluate(
      verificationMethod + "(#nl_knaw_huygens_httpcommand_expectation, #nl_knaw_huygens_httpcommand_result)"
    );
    evaluator.setVariable("#nl_knaw_huygens_httpcommand_result", null);
    evaluator.setVariable("#nl_knaw_huygens_httpcommand_expectation", null);
  } else {
    validationResult = defaultValidator.validate(expectation, requestCommand.getActualResult());
  }

  Element caption = null;
  if (addCaptions) {
    caption = new Element("div").addAttribute("class", "responseCaption").appendText("Response:");
  }

  Element resultElement = replaceWithEmptyElement(commandCall.getElement(), name, namespace, caption);
  addClass(resultElement, "responseContent");

  try {
    Builder builder = new Builder();
    Document document = builder.build(new StringReader(validationResult.getMessage()));
    //new Element() creates a deepcopy not attached to the doc
    nu.xom.Element rootElement = new nu.xom.Element(document.getRootElement());
    resultElement.appendChild(new Element(rootElement));
    resultRecorder.record(validationResult.isSucceeded() ? Result.SUCCESS : Result.FAILURE);
  } catch (ParsingException | IOException e) {
    resultRecorder.record(Result.FAILURE);
    if (e instanceof ParsingException) {
      resultElement.appendText(
        "Error at line " + ((ParsingException) e).getLineNumber() +
          ", column: " + ((ParsingException) e).getColumnNumber());
      resultElement.appendText(validationResult.getMessage());
    }
  }
}
 
Example 5
Source File: XomReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public XomReader(Document document) {
    super(document.getRootElement());
}
 
Example 6
Source File: XomReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @since 1.4
 */
public XomReader(Document document, NameCoder nameCoder) {
    super(document.getRootElement(), nameCoder);
}
 
Example 7
Source File: XmlPersistenceRead.java    From rest-client with Apache License 2.0 4 votes vote down vote up
protected Response xml2Response(final Document doc)
        throws XMLException {
    ResponseBean responseBean = new ResponseBean();

    // get the rootNode
    Element rootNode = doc.getRootElement();

    if (!"rest-client".equals(rootNode.getQualifiedName())) {
        throw new XMLException("Root node is not <rest-client>");
    }

    // checking correct rest version
    try {
        Versions.versionValidCheck(rootNode.getAttributeValue("version"));
    }
    catch(Versions.VersionValidationException ex) {
        throw new XMLException(ex);
    }

    // assign rootnode to current node and also finding 'response' node
    Element tNode = null;
    Element responseNode = null;

    // if more than two request element is present then throw the exception
    if (rootNode.getChildElements().size() != 1) {
        throw new XMLException("There can be only one child node for root node: <response>");
    }
    // minimum one response element is present in xml
    if (rootNode.getFirstChildElement("response") == null) {
        throw new XMLException("The child node of <rest-client> should be <response>");
    }
    responseNode = rootNode.getFirstChildElement("response");
    for (int i = 0; i < responseNode.getChildElements().size(); i++) {
        tNode = responseNode.getChildElements().get(i);
        String nodeName = tNode.getQualifiedName();

        if ("execution-time".equals(nodeName)) {
            responseBean.setExecutionTime(Long.parseLong(tNode.getValue()));
        } else if ("status".equals(nodeName)) {
            responseBean.setStatusLine(tNode.getValue());
            responseBean.setStatusCode(Integer.parseInt(tNode.getAttributeValue("code")));
        } else if ("headers".equals(nodeName)) {
            Map<String, String> m = getHeadersFromHeaderNode(tNode);
            for (String key : m.keySet()) {
                responseBean.addHeader(key, m.get(key));
            }
        } else if ("body".equals(nodeName)) {
            final String base64body = tNode.getValue();
            responseBean.setResponseBody(Util.base64decodeByteArray(base64body));
        } else if ("test-result".equals(nodeName)) {
            TestResultBean testResultBean = new TestResultBean();

            for (int j = 0; j < tNode.getChildCount(); j++) {
                String nn = tNode.getQualifiedName();
                if ("run-count".equals(nn)) {
                    throw new XMLException("<headers> element should contain only <header> elements");
                } else if ("failure-count".equals(nn)) {
                    throw new XMLException("<headers> element should contain only <header> elements");
                } else if ("error-count".equals(nn)) {
                    throw new XMLException("<headers> element should contain only <header> elements");
                } else if ("failures".equals(nn)) {
                    throw new XMLException("<headers> element should contain only <header> elements");
                } else if ("errors".equals(nn)) {
                    throw new XMLException("<headers> element should contain only <header> elements");
                }
            }
            responseBean.setTestResult(testResultBean);
        } else {
            throw new XMLException("Unrecognized element found: <" + nodeName + ">");
        }
    }
    return responseBean;
}
 
Example 8
Source File: XomReader.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * @since 1.2
 * @deprecated As of 1.4 use {@link XomReader#XomReader(Element, NameCoder)} instead.
 */
public XomReader(Document document, XmlFriendlyReplacer replacer) {
   this(document.getRootElement(), (NameCoder)replacer);
}