Search in sources :

Example 26 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project AndEngine by nicolasgramlich.

the class AnimationPackLoader method load.

public AnimationPack load(final InputStream pInputStream, final String pAssetBasePath) throws AnimationPackParseException {
    try {
        final SAXParserFactory spf = SAXParserFactory.newInstance();
        final SAXParser sp = spf.newSAXParser();
        final XMLReader xr = sp.getXMLReader();
        final AnimationPackParser animationPackParser = new AnimationPackParser(this.mAssetManager, pAssetBasePath, this.mTextureManager);
        xr.setContentHandler(animationPackParser);
        xr.parse(new InputSource(new BufferedInputStream(pInputStream)));
        return animationPackParser.getAnimationPack();
    } catch (final SAXException e) {
        throw new AnimationPackParseException(e);
    } catch (final ParserConfigurationException pe) {
        /* Doesn't happen. */
        return null;
    } catch (final IOException e) {
        throw new AnimationPackParseException(e);
    } finally {
        StreamUtils.close(pInputStream);
    }
}
Also used : InputSource(org.xml.sax.InputSource) BufferedInputStream(java.io.BufferedInputStream) AnimationPackParseException(org.andengine.util.animationpack.exception.AnimationPackParseException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 27 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project che by eclipse.

the class JavaProject method decodeClasspathEntry.

public IClasspathEntry decodeClasspathEntry(String encodedEntry) {
    try {
        if (encodedEntry == null)
            return null;
        StringReader reader = new StringReader(encodedEntry);
        Element node;
        try {
            DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            node = parser.parse(new InputSource(reader)).getDocumentElement();
        } catch (SAXException e) {
            return null;
        } catch (ParserConfigurationException e) {
            return null;
        } finally {
            reader.close();
        }
        if (!node.getNodeName().equalsIgnoreCase(ClasspathEntry.TAG_CLASSPATHENTRY) || node.getNodeType() != Node.ELEMENT_NODE) {
            return null;
        }
        return ClasspathEntry.elementDecode(node, this, null);
    } catch (IOException e) {
        // bad format
        return null;
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) IJavaElement(org.eclipse.jdt.core.IJavaElement) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 28 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project che by eclipse.

the class RefactoringSessionReader method readSession.

/**
	 * Reads a refactoring history descriptor from the specified input object.
	 *
	 * @param source
	 *            the input source
	 * @return a corresponding refactoring history descriptor, or
	 *         <code>null</code>
	 * @throws CoreException
	 *             if an error occurs while reading form the input source
	 */
public RefactoringSessionDescriptor readSession(final InputSource source) throws CoreException {
    fSessionFound = false;
    try {
        //$NON-NLS-1$
        source.setSystemId("/");
        createParser(SAXParserFactory.newInstance()).parse(source, this);
        if (!fSessionFound)
            throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.REFACTORING_HISTORY_FORMAT_ERROR, RefactoringCoreMessages.RefactoringSessionReader_no_session, null));
        if (fRefactoringDescriptors != null) {
            if (//$NON-NLS-1$
            fVersion == null || "".equals(fVersion))
                throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.MISSING_REFACTORING_HISTORY_VERSION, RefactoringCoreMessages.RefactoringSessionReader_missing_version_information, null));
            if (!IRefactoringSerializationConstants.CURRENT_VERSION.equals(fVersion))
                throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IRefactoringCoreStatusCodes.UNSUPPORTED_REFACTORING_HISTORY_VERSION, RefactoringCoreMessages.RefactoringSessionReader_unsupported_version_information, null));
            return new RefactoringSessionDescriptor((RefactoringDescriptor[]) fRefactoringDescriptors.toArray(new RefactoringDescriptor[fRefactoringDescriptors.size()]), fVersion, fComment);
        }
    } catch (IOException exception) {
        throwCoreException(exception, exception.getLocalizedMessage());
    } catch (ParserConfigurationException exception) {
        throwCoreException(exception, exception.getLocalizedMessage());
    } catch (SAXParseException exception) {
        String message = Messages.format(RefactoringCoreMessages.RefactoringSessionReader_invalid_contents_at, new Object[] { Integer.toString(exception.getLineNumber()), Integer.toString(exception.getColumnNumber()) });
        throwCoreException(exception, message);
    } catch (SAXException exception) {
        throwCoreException(exception, exception.getLocalizedMessage());
    } finally {
        fRefactoringDescriptors = null;
        fVersion = null;
        fComment = null;
        fLocator = null;
    }
    return null;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) DefaultRefactoringDescriptor(org.eclipse.ltk.internal.core.refactoring.history.DefaultRefactoringDescriptor) RefactoringDescriptor(org.eclipse.ltk.core.refactoring.RefactoringDescriptor) CoreException(org.eclipse.core.runtime.CoreException) SAXParseException(org.xml.sax.SAXParseException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RefactoringSessionDescriptor(org.eclipse.ltk.core.refactoring.RefactoringSessionDescriptor) SAXException(org.xml.sax.SAXException)

Example 29 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project cogtool by cogtool.

the class ObjectPersister method load.

// registerForPersistence
/**
     * Load an Object from a file.  This method assumes that all error checking
     * and prompting/correction for the validity of the file has already
     * been completed.
     *
     * @param src the File location of the saved Object
     * @return a new instantiated Object
     * @throws java.io.IOException if any file operation fails or the SAX XML
     *         parser fails
     */
public Object load(File src) throws IOException {
    // See if an object is already loaded for the given file
    String canonicalFileName = src.getCanonicalPath();
    PersistInfo info = getInfoByName(canonicalFileName);
    if (info != null) {
        return info.obj;
    }
    // Attempt to create a file of 3x size in the temp directory
    // to hold the expanded XML serialization.
    File sizeCheckFile = File.createTempFile(tmpFilePrefix, ".size", tmpDir);
    try {
        checkDiskSpace(sizeCheckFile, 3 * src.length(), "Not enough temp space on disk to open file: " + src);
    } finally {
        sizeCheckFile.delete();
    }
    // If we're here, no exception was thrown; create checkpoint directory
    File chkptFile = createTempDirectory();
    // Open file as ZipFile and decompress
    ZipFile zip = null;
    try {
        zip = new ZipFile(src);
        // Unzip the file to the checkpoint directory
        ZipUtil.unzip(zip, chkptFile);
    } catch (ZipException ex) {
        IOException newE = new IOException("load encountered zip compression error");
        newE.initCause(ex);
        throw newE;
    } finally {
        if (zip != null) {
            zip.close();
        }
    }
    // Load object from the expanded XML serialization
    ObjectLoader l = new ObjectLoader();
    Object obj = null;
    Reader reader = null;
    try {
        reader = new InputStreamReader(new FileInputStream(new File(chkptFile, PERSIST_FILE)), "UTF-8");
        Collection<?> objSet = l.load(new InputSource(reader), null);
        // There should be only one top-level object
        Iterator<?> objs = objSet.iterator();
        if (objs.hasNext()) {
            obj = objs.next();
        }
        // Register this file for future lookup, both by object
        // and by file name
        info = new PersistInfo(obj, chkptFile, src);
        objInfos.put(obj, info);
        fileInfos.put(canonicalFileName, info);
    } catch (ParserConfigurationException e) {
        IOException newE = new IOException("load encountered parser error");
        newE.initCause(e);
        throw newE;
    } catch (SAXException e) {
        IOException newE = new IOException("load encountered SAX error");
        newE.initCause(e);
        throw newE;
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return obj;
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) FileReader(java.io.FileReader) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) ZipFile(java.util.zip.ZipFile) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 30 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project openhab1-addons by openhab.

the class OWServerBinding method getVariable.

String getVariable(String response, String romId, String name) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // Get the DOM Builder
    DocumentBuilder builder = null;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        logger.error("Error parsing OWServer XML response " + e.getMessage());
    }
    // Load and Parse the XML document
    // document contains the complete XML as a Tree.
    Document document = null;
    try {
        InputSource is = new InputSource(new StringReader(response));
        document = builder.parse(is);
    } catch (SAXException e) {
        logger.error("Error reading OWServer XML response " + e.getMessage());
    } catch (IOException e) {
        logger.error("Error reading OWServer XML response " + e.getMessage());
    }
    // Iterating through the nodes and extracting the data.
    NodeList nodeList = document.getDocumentElement().getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeName().startsWith("owd_")) {
            boolean romMatch = false;
            NodeList childNodes = node.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node cNode = childNodes.item(j);
                // Identifying the child tag of employee encountered.
                if (cNode instanceof Element) {
                    String content = cNode.getLastChild().getTextContent().trim();
                    if (cNode.getNodeName().equals("ROMId") & content.equals(romId)) {
                        romMatch = true;
                    }
                    String nname = cNode.getNodeName();
                    if (nname.equals(name) & romMatch == true) {
                        return content;
                    }
                }
            }
        }
    }
    return null;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1435 SAXException (org.xml.sax.SAXException)1039 IOException (java.io.IOException)951 Document (org.w3c.dom.Document)751 DocumentBuilder (javax.xml.parsers.DocumentBuilder)687 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)622 Element (org.w3c.dom.Element)389 InputSource (org.xml.sax.InputSource)260 NodeList (org.w3c.dom.NodeList)248 Node (org.w3c.dom.Node)225 SAXParser (javax.xml.parsers.SAXParser)185 File (java.io.File)171 InputStream (java.io.InputStream)170 TransformerException (javax.xml.transform.TransformerException)167 SAXParserFactory (javax.xml.parsers.SAXParserFactory)144 ByteArrayInputStream (java.io.ByteArrayInputStream)141 StringReader (java.io.StringReader)127 ArrayList (java.util.ArrayList)122 DOMSource (javax.xml.transform.dom.DOMSource)114 StreamResult (javax.xml.transform.stream.StreamResult)98