Search in sources :

Example 91 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project jersey by jersey.

the class MultiPartWebAppTest method testApplicationWadl.

@Test
public void testApplicationWadl() throws Exception {
    final WebTarget target = target().path("application.wadl");
    final Response response = target.request().get();
    assertEquals(200, response.getStatus());
    final File tmpFile = response.readEntity(File.class);
    final DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
    bf.setNamespaceAware(true);
    bf.setValidating(false);
    if (!SaxHelper.isXdkDocumentBuilderFactory(bf)) {
        bf.setXIncludeAware(false);
    }
    final DocumentBuilder b = bf.newDocumentBuilder();
    final Document d = b.parse(tmpFile);
    final XPath xp = XPathFactory.newInstance().newXPath();
    xp.setNamespaceContext(new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02"));
    String val = (String) xp.evaluate("//wadl:resource[@path='part']/wadl:method[@name='POST']/wadl:request/wadl:representation/@mediaType", d, XPathConstants.STRING);
    assertEquals("multipart/form-data", val);
}
Also used : Response(javax.ws.rs.core.Response) XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) WebTarget(javax.ws.rs.client.WebTarget) SimpleNamespaceResolver(org.glassfish.jersey.internal.util.SimpleNamespaceResolver) Document(org.w3c.dom.Document) File(java.io.File) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 92 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project NabAlive by jcheype.

the class MeteoApplication method httpCall.

public void httpCall(final String mac, final String city, final String country, final String unit, final String key, final String lang) throws UnsupportedEncodingException {
    StringBuilder url = new StringBuilder(BASE_URL);
    url.append("?weather=").append(URLEncoder.encode(city + "," + country, "UTF-8"));
    url.append("&hl=fr");
    logger.debug("making httpCall: {}", url);
    try {
        asyncHttpClient.prepareGet(url.toString()).execute(new AsyncCompletionHandler<Response>() {

            @Override
            public Response onCompleted(Response response) throws Exception {
                DocumentBuilder builder = builderLocal.get();
                String responseBody = response.getResponseBody("ISO-8859-1");
                StringReader reader = new StringReader(responseBody);
                InputSource is = new InputSource(reader);
                Document document = builder.parse(is);
                NodeList forecastConditions = document.getElementsByTagName("forecast_conditions");
                MeteoResult meteoResult = new MeteoResult(getNodeValue(forecastConditions.item(0).getChildNodes(), "low"), getNodeValue(forecastConditions.item(0).getChildNodes(), "high"), getNodeValue(forecastConditions.item(0).getChildNodes(), "icon"), getNodeValue(forecastConditions.item(1).getChildNodes(), "low"), getNodeValue(forecastConditions.item(1).getChildNodes(), "high"), getNodeValue(forecastConditions.item(1).getChildNodes(), "icon"), unit);
                meteoCache.asMap().putIfAbsent(key, meteoResult);
                sendMeteo(mac, meteoResult, lang);
                return response;
            }

            @Override
            public void onThrowable(Throwable t) {
                logger.error("error in meteo, http received", t);
            }
        });
    } catch (IOException e) {
        logger.error("error in meteo, http call", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) Document(org.w3c.dom.Document) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Response(com.ning.http.client.Response) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader)

Example 93 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project violations-plugin by jenkinsci.

the class ViolationsDOMParser method parse.

/*
     * Parse a violations file.
     * @param model the model to store the violations in.
     * @param projectPath the project path used for resolving paths.
     * @param fileName the name of the violations file to parse
     *                       (relative to the projectPath).
     * @param sourcePaths a list of source paths to resolve classes against
     * @throws IOException if there is an error.
     */
public void parse(FullBuildModel model, File projectPath, String fileName, String[] sourcePaths) throws IOException {
    boolean success = false;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        dom = db.parse(new File(projectPath, fileName));
        setProjectPath(projectPath);
        setModel(model);
        setSourcePaths(sourcePaths);
        execute();
        success = true;
    } catch (IOException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new IOException2("Cannot parse " + fileName, ex);
    } finally {
    // ? terminate the parser
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) IOException2(hudson.util.IOException2)

Example 94 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project violations-plugin by jenkinsci.

the class ReSharperParser method parse.

public void parse(final FullBuildModel model, final File projectPath, final String fileName, final String[] sourcePaths) throws IOException {
    absoluteFileFinder.addSourcePath(projectPath.getAbsolutePath());
    absoluteFileFinder.addSourcePaths(sourcePaths);
    final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder;
    try {
        docBuilder = docBuilderFactory.newDocumentBuilder();
        final Document docElement = docBuilder.parse(new FileInputStream(new File(projectPath, fileName)));
        NodeList nl = docElement.getElementsByTagName("IssueType");
        if (nl == null)
            return;
        for (int i = 0; i < nl.getLength(); i++) {
            final Element issueTypeElement = (Element) nl.item(i);
            final IssueType issueType = parseIssueType(issueTypeElement);
            issueTypes.put(issueType.getId(), issueType);
        }
        nl = docElement.getElementsByTagName("Issue");
        if (nl == null)
            return;
        for (int i = 0; i < nl.getLength(); i++) {
            final Element issueElement = (Element) nl.item(i);
            final Issue issue = parseIssue(issueElement);
            final IssueType issueType = issueTypes.get(issue.getTypeId());
            if (// couldn't find the issue type, skip it
            issueType == null)
                continue;
            final Violation violation = new Violation();
            violation.setType("resharper");
            violation.setMessage(issue.getMessage());
            violation.setPopupMessage(issueType.getDescription() + " - " + issue.getMessage());
            violation.setSource(issueType.getCategory());
            violation.setLine(issue.getLine());
            violation.setSeverity(SEVERITIES.get(issueType.getSeverity()));
            violation.setSeverityLevel(Severity.getSeverityLevel(violation.getSeverity()));
            final File file = absoluteFileFinder.getFileForName(issue.getFile());
            final FullFileModel fullFileModel = getFileModel(model, issue.getFile().replace('\\', '/'), file);
            fullFileModel.addViolation(violation);
        }
    } catch (final ParserConfigurationException pce) {
        throw new IOException2(pce);
    } catch (final SAXException se) {
        throw new IOException2(se);
    }
}
Also used : Violation(hudson.plugins.violations.model.Violation) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) FullFileModel(hudson.plugins.violations.model.FullFileModel) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) IOException2(hudson.util.IOException2)

Example 95 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project jersey by jersey.

the class WadlResourceTest method extractWadlAsDocument.

/**
     * Extracts WADL as {@link Document} from given {@link Response}.
     *
     * @param response The response to extract {@code Document} from.
     * @return The extracted {@code Document}.
     * @throws ParserConfigurationException In case of parser configuration issues.
     * @throws SAXException                 In case of parsing issues.
     * @throws IOException                  In case of IO error.
     */
static Document extractWadlAsDocument(final Response response) throws ParserConfigurationException, SAXException, IOException {
    assertEquals(200, response.getStatus());
    final File tmpFile = response.readEntity(File.class);
    final DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
    bf.setNamespaceAware(true);
    bf.setValidating(false);
    if (!SaxHelper.isXdkDocumentBuilderFactory(bf)) {
        bf.setXIncludeAware(false);
    }
    final DocumentBuilder b = bf.newDocumentBuilder();
    final Document d = b.parse(tmpFile);
    Wadl5Test.printSource(new DOMSource(d));
    return d;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document) File(java.io.File)

Aggregations

DocumentBuilder (javax.xml.parsers.DocumentBuilder)883 Document (org.w3c.dom.Document)694 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)622 Element (org.w3c.dom.Element)339 IOException (java.io.IOException)276 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)276 NodeList (org.w3c.dom.NodeList)267 InputSource (org.xml.sax.InputSource)238 SAXException (org.xml.sax.SAXException)235 Node (org.w3c.dom.Node)199 StringReader (java.io.StringReader)167 Test (org.junit.Test)127 DOMSource (javax.xml.transform.dom.DOMSource)102 File (java.io.File)99 ByteArrayInputStream (java.io.ByteArrayInputStream)86 InputStream (java.io.InputStream)73 ArrayList (java.util.ArrayList)72 StreamResult (javax.xml.transform.stream.StreamResult)65 Transformer (javax.xml.transform.Transformer)59 SAXParseException (org.xml.sax.SAXParseException)56