Search in sources :

Example 96 with InputSource

use of org.xml.sax.InputSource in project OpenMEAP by OpenMEAP.

the class RESTAppMgmtClient method connectionOpen.

public ConnectionOpenResponse connectionOpen(ConnectionOpenRequest request) throws WebServiceException {
    ConnectionOpenResponse response = null;
    Hashtable postData = new Hashtable();
    postData.put(UrlParamConstants.ACTION, "connection-open-request");
    postData.put(UrlParamConstants.DEVICE_UUID, request.getApplication().getInstallation().getUuid());
    postData.put(UrlParamConstants.APP_NAME, request.getApplication().getName());
    postData.put(UrlParamConstants.APP_VERSION, request.getApplication().getVersionId());
    postData.put(UrlParamConstants.APPARCH_HASH, StringUtils.orEmpty(request.getApplication().getHashValue()));
    postData.put(UrlParamConstants.SLIC_VERSION, request.getSlic().getVersionId());
    HttpResponse httpResponse = null;
    InputSource responseInputSource = null;
    String responseText = null;
    try {
        httpResponse = requester.postData(serviceUrl, postData);
        if (httpResponse.getStatusCode() != 200) {
            throw new WebServiceException(WebServiceException.TypeEnum.CLIENT, "Posting to the service resulted in a " + httpResponse.getStatusCode() + " status code");
        }
        responseText = Utils.readInputStream(httpResponse.getResponseBody(), "UTF-8");
    } catch (Exception e) {
        throw new WebServiceException(WebServiceException.TypeEnum.CLIENT, StringUtils.isEmpty(e.getMessage()) ? e.getMessage() : "There's a problem connecting. Check your network or try again later", e);
    }
    // now we parse the response into a ConnectionOpenResponse object
    if (responseText != null) {
        Result result = new Result();
        JSONObjectBuilder builder = new JSONObjectBuilder();
        try {
            result = (Result) builder.fromJSON(new JSONObject(responseText), result);
            if (result.getError() != null) {
                throw new WebServiceException(WebServiceException.TypeEnum.fromValue(result.getError().getCode().value()), result.getError().getMessage());
            }
        } catch (JSONException e) {
            throw new WebServiceException(WebServiceException.TypeEnum.CLIENT, "Unable to parse service response content.");
        }
        response = result.getConnectionOpenResponse();
    }
    return response;
}
Also used : InputSource(org.xml.sax.InputSource) JSONObjectBuilder(com.openmeap.json.JSONObjectBuilder) WebServiceException(com.openmeap.protocol.WebServiceException) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) Hashtable(java.util.Hashtable) HttpResponse(com.openmeap.http.HttpResponse) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) ConnectionOpenResponse(com.openmeap.protocol.dto.ConnectionOpenResponse) WebServiceException(com.openmeap.protocol.WebServiceException) IOException(java.io.IOException) HttpRequestException(com.openmeap.http.HttpRequestException) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) Result(com.openmeap.protocol.dto.Result)

Example 97 with InputSource

use of org.xml.sax.InputSource in project atlas by alibaba.

the class Configuration method readXmlConfig.

/**
     * read args from xml
     **/
void readXmlConfig(File xmlConfigFile) throws IOException, ParserConfigurationException, SAXException {
    if (!xmlConfigFile.exists()) {
        return;
    }
    System.out.printf("reading config file, %s\n", xmlConfigFile.getAbsolutePath());
    BufferedInputStream input = null;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        input = new BufferedInputStream(new FileInputStream(xmlConfigFile));
        InputSource source = new InputSource(input);
        factory.setNamespaceAware(false);
        factory.setValidating(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(source);
        NodeList issues = document.getElementsByTagName(TAG_ISSUE);
        for (int i = 0, count = issues.getLength(); i < count; i++) {
            Node node = issues.item(i);
            Element element = (Element) node;
            String id = element.getAttribute(ATTR_ID);
            if (id.length() == 0) {
                System.err.println("Invalid config file: Missing required issue id attribute");
                continue;
            }
            if (id.equals(PROPERTY_ISSUE)) {
                readPropertyFromXml(node);
            } else if (id.equals(DEX_ISSUE)) {
                readDexPatternsFromXml(node);
            } else if (id.equals(SO_ISSUE)) {
                readLibPatternsFromXml(node);
            } else if (id.equals(RES_ISSUE)) {
                readResPatternsFromXml(node);
            } else if (id.equals(PACKAGE_CONFIG_ISSUE)) {
                readPackageConfigFromXml(node);
            } else if (id.equals(SIGN_ISSUE)) {
                if (mUseSignAPk) {
                    readSignFromXml(node);
                }
            } else {
                System.err.println("unknown issue " + id);
            }
        }
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                System.exit(-1);
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) BufferedInputStream(java.io.BufferedInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream)

Example 98 with InputSource

use of org.xml.sax.InputSource in project Openfire by igniterealtime.

the class UserSchemaValidator method validateAndParse.

/**
   * Perform a validate and a parse of the specified source document.
   * Validating is done with the specified schemafiles.
   * 
   * @return A Document when the validation s successful.
   */
Document validateAndParse() {
    ValidatorErrorHandler handler = new ValidatorErrorHandler();
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        documentBuilderFactory.setXIncludeAware(true);
        documentBuilderFactory.setValidating(false);
        // We don't want xml:base and xml:lang attributes in the output.
        documentBuilderFactory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
        documentBuilderFactory.setFeature("http://apache.org/xml/features/xinclude/fixup-language", false);
        if (schemaSources.length > 0) {
            Log.info("Checking Schema's");
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            schemaFactory.setErrorHandler(handler);
            Schema schema = schemaFactory.newSchema(schemaSources);
            documentBuilderFactory.setSchema(schema);
            Log.info("Start validating document");
        } else {
            Log.info("Loading document");
        }
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        handler.reset();
        // Communicate some info about the Xincludes in the imported file. These imports should be resolvable by the server. 
        documentBuilder.setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                Log.info(String.format("resolved Entity:%s %s", (publicId != null ? publicId : ""), systemId));
                return null;
            }
        });
        documentBuilder.setErrorHandler(handler);
        Document result = documentBuilder.parse(source);
        if (result != null && handler.isValid()) {
            return result;
        } else {
            Log.warn(String.format("document is invalid. %1$d errors found.", handler.getNrOfErrors()));
            return null;
        }
    } catch (Exception e) {
        Log.warn(String.format("document validation failed. %1$d errors found.", handler.getNrOfErrors()));
        Log.debug("", e);
        return null;
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Schema(javax.xml.validation.Schema) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 99 with InputSource

use of org.xml.sax.InputSource in project jmonkeyengine by jMonkeyEngine.

the class SceneMaterialLoader method load.

public MaterialList load(AssetManager assetManager, String folderName, InputStream in) throws IOException {
    try {
        this.assetManager = assetManager;
        this.folderName = folderName;
        reset();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XMLReader xr = factory.newSAXParser().getXMLReader();
        xr.setContentHandler(this);
        xr.setErrorHandler(this);
        InputStreamReader r = null;
        try {
            r = new InputStreamReader(in);
            xr.parse(new InputSource(r));
        } finally {
            if (r != null) {
                r.close();
            }
        }
        return materialList;
    } catch (SAXException ex) {
        IOException ioEx = new IOException("Error while parsing Ogre3D dotScene");
        ioEx.initCause(ex);
        throw ioEx;
    } catch (ParserConfigurationException ex) {
        IOException ioEx = new IOException("Error while parsing Ogre3D dotScene");
        ioEx.initCause(ex);
        throw ioEx;
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 100 with InputSource

use of org.xml.sax.InputSource in project OpenNotebook by jaltekruse.

the class OldReader method readFile.

public Document readFile(InputStreamReader file) throws SAXException, IOException {
    attributeNameInError = null;
    attributeValueInError = null;
    objectWithError = null;
    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setContentHandler(this);
    reader.setErrorHandler(this);
    reader.parse(new InputSource(file));
    if (doc == null) {
        if (DEBUG) {
            System.out.println("error 1");
        }
        throw new IOException("improper document format");
    }
    if (hadAttributeError) {
        if (DEBUG) {
            System.out.println("error 2");
        }
        throw new IOException("improper document format, error with attribute '" + attributeNameInError + "' with a value of '" + attributeValueInError + "'" + " in object '" + objectWithError + "'");
    }
    return doc;
}
Also used : InputSource(org.xml.sax.InputSource) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader)

Aggregations

InputSource (org.xml.sax.InputSource)966 StringReader (java.io.StringReader)365 IOException (java.io.IOException)249 Document (org.w3c.dom.Document)243 DocumentBuilder (javax.xml.parsers.DocumentBuilder)237 SAXException (org.xml.sax.SAXException)230 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)190 XMLReader (org.xml.sax.XMLReader)166 Test (org.junit.Test)149 NodeList (org.w3c.dom.NodeList)131 InputStream (java.io.InputStream)126 Element (org.w3c.dom.Element)124 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)117 SAXParser (javax.xml.parsers.SAXParser)94 ByteArrayInputStream (java.io.ByteArrayInputStream)80 SAXSource (javax.xml.transform.sax.SAXSource)78 Node (org.w3c.dom.Node)75 SAXParserFactory (javax.xml.parsers.SAXParserFactory)74 File (java.io.File)64 FileInputStream (java.io.FileInputStream)55