Search in sources :

Example 1 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project OpenPanodroid by duerrfk.

the class RESTRequestorXML method parseResponse.

@Override
protected void parseResponse(InputStream is, int contentLength) {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        DefaultHandler saxHandler = createSAXHandler();
        if (saxHandler == null) {
            setSuccessState(false);
        } else {
            parser.parse(is, saxHandler);
        }
    } catch (Exception ex) {
        setErrorMsg(ex.getLocalizedMessage());
        setSuccessState(false);
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 2 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project che by eclipse.

the class RefactoringHistoryManager method getCachedDocument.

/**
	 * Returns the cached refactoring history document.
	 *
	 * @param path
	 *            the path of the document
	 * @param input
	 *            the input stream where to read the document
	 * @return the cached refactoring history document
	 * @throws SAXException
	 *             if an error occurs while parsing the history entry
	 * @throws IOException
	 *             if an input/output error occurs
	 * @throws ParserConfigurationException
	 *             if an error occurs in the parser configuration
	 */
private Document getCachedDocument(final IPath path, final InputStream input) throws SAXException, IOException, ParserConfigurationException {
    if (path.equals(fCachedPath) && fCachedDocument != null)
        return fCachedDocument;
    DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    parser.setErrorHandler(new DefaultHandler());
    final Document document = parser.parse(new InputSource(input));
    fCachedDocument = document;
    fCachedPath = path;
    return document;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 3 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project che by eclipse.

the class TemplateReaderWriter method read.

/**
	 * Reads templates from an <code>InputSource</code> and adds them to the templates.
	 *
	 * @param source the input source
	 * @param bundle a resource bundle to use for translating the read templates, or <code>null</code> if no translation should occur
	 * @param singleId the template id to extract, or <code>null</code> to read in all templates
	 * @return the read templates, encapsulated in instances of <code>TemplatePersistenceData</code>
	 * @throws IOException if reading from the stream fails
	 */
private TemplatePersistenceData[] read(InputSource source, ResourceBundle bundle, String singleId) throws IOException {
    try {
        Collection templates = new ArrayList();
        Set ids = new HashSet();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = factory.newDocumentBuilder();
        parser.setErrorHandler(new DefaultHandler());
        Document document = parser.parse(source);
        NodeList elements = document.getElementsByTagName(TEMPLATE_ELEMENT);
        int count = elements.getLength();
        for (int i = 0; i != count; i++) {
            Node node = elements.item(i);
            NamedNodeMap attributes = node.getAttributes();
            if (attributes == null)
                continue;
            String id = getStringValue(attributes, ID_ATTRIBUTE, null);
            if (id != null && ids.contains(id))
                //$NON-NLS-1$
                throw new IOException(TemplatePersistenceMessages.getString("TemplateReaderWriter.duplicate.id"));
            if (singleId != null && !singleId.equals(id))
                continue;
            boolean deleted = getBooleanValue(attributes, DELETED_ATTRIBUTE, false);
            String name = getStringValue(attributes, NAME_ATTRIBUTE);
            name = translateString(name, bundle);
            //$NON-NLS-1$
            String description = getStringValue(attributes, DESCRIPTION_ATTRIBUTE, "");
            description = translateString(description, bundle);
            String context = getStringValue(attributes, CONTEXT_ATTRIBUTE);
            if (name == null || context == null)
                //$NON-NLS-1$
                throw new IOException(TemplatePersistenceMessages.getString("TemplateReaderWriter.error.missing_attribute"));
            boolean enabled = getBooleanValue(attributes, ENABLED_ATTRIBUTE, true);
            boolean autoInsertable = getBooleanValue(attributes, AUTO_INSERTABLE_ATTRIBUTE, true);
            StringBuffer buffer = new StringBuffer();
            NodeList children = node.getChildNodes();
            for (int j = 0; j != children.getLength(); j++) {
                String value = children.item(j).getNodeValue();
                if (value != null)
                    buffer.append(value);
            }
            String pattern = buffer.toString();
            pattern = translateString(pattern, bundle);
            Template template = new Template(name, description, context, pattern, autoInsertable);
            TemplatePersistenceData data = new TemplatePersistenceData(template, enabled, id);
            data.setDeleted(deleted);
            templates.add(data);
            if (singleId != null && singleId.equals(id))
                break;
        }
        return (TemplatePersistenceData[]) templates.toArray(new TemplatePersistenceData[templates.size()]);
    } catch (ParserConfigurationException e) {
        Assert.isTrue(false);
    } catch (SAXException e) {
        //$NON-NLS-1$
        throw (IOException) new IOException("Could not read template file").initCause(e);
    }
    // dummy
    return null;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Template(org.eclipse.jface.text.templates.Template) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Collection(java.util.Collection) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashSet(java.util.HashSet)

Example 4 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project che by eclipse.

the class Launching method restoreLibraryInfo.

/**
     * Restores library information for VMs
     */
private static void restoreLibraryInfo() {
    fgLibraryInfoMap = new HashMap<String, LibraryInfo>(10);
    IPath libPath = getDefault().getStateLocation();
    //$NON-NLS-1$
    libPath = libPath.append("libraryInfos.xml");
    File file = libPath.toFile();
    if (file.exists()) {
        try {
            InputStream stream = new BufferedInputStream(new FileInputStream(file));
            DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            parser.setErrorHandler(new DefaultHandler());
            Element root = parser.parse(new InputSource(stream)).getDocumentElement();
            if (!root.getNodeName().equals("libraryInfos")) {
                //$NON-NLS-1$
                return;
            }
            NodeList list = root.getChildNodes();
            int length = list.getLength();
            for (int i = 0; i < length; ++i) {
                Node node = list.item(i);
                short type = node.getNodeType();
                if (type == Node.ELEMENT_NODE) {
                    Element element = (Element) node;
                    String nodeName = element.getNodeName();
                    if (nodeName.equalsIgnoreCase("libraryInfo")) {
                        //$NON-NLS-1$
                        //$NON-NLS-1$
                        String version = element.getAttribute("version");
                        //$NON-NLS-1$
                        String location = element.getAttribute("home");
                        //$NON-NLS-1$
                        String[] bootpath = getPathsFromXML(element, "bootpath");
                        //$NON-NLS-1$
                        String[] extDirs = getPathsFromXML(element, "extensionDirs");
                        //$NON-NLS-1$
                        String[] endDirs = getPathsFromXML(element, "endorsedDirs");
                        if (location != null) {
                            LibraryInfo info = new LibraryInfo(version, bootpath, extDirs, endDirs);
                            fgLibraryInfoMap.put(location, info);
                        }
                    }
                }
            }
        } catch (IOException | SAXException | ParserConfigurationException e) {
            log(e);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) IPath(org.eclipse.core.runtime.IPath) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) BufferedInputStream(java.io.BufferedInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File)

Example 5 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project che by eclipse.

the class Launching method readInstallInfo.

/**
     * Reads the file of saved time stamps and populates the {@link #fgInstallTimeMap}.
     * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=266651 for more information
     *
     * @since 3.7
     */
private static void readInstallInfo() {
    fgInstallTimeMap = new HashMap<String, Long>();
    IPath libPath = getDefault().getStateLocation();
    //$NON-NLS-1$
    libPath = libPath.append(".install.xml");
    File file = libPath.toFile();
    if (file.exists()) {
        try {
            InputStream stream = new BufferedInputStream(new FileInputStream(file));
            DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            parser.setErrorHandler(new DefaultHandler());
            Element root = parser.parse(new InputSource(stream)).getDocumentElement();
            if (root.getNodeName().equalsIgnoreCase("dirs")) {
                //$NON-NLS-1$
                NodeList nodes = root.getChildNodes();
                Node node = null;
                Element element = null;
                for (int i = 0; i < nodes.getLength(); i++) {
                    node = nodes.item(i);
                    if (node.getNodeType() == Node.ELEMENT_NODE) {
                        element = (Element) node;
                        if (element.getNodeName().equalsIgnoreCase("entry")) {
                            //$NON-NLS-1$
                            //$NON-NLS-1$
                            String loc = element.getAttribute("loc");
                            //$NON-NLS-1$
                            String stamp = element.getAttribute("stamp");
                            try {
                                Long l = new Long(stamp);
                                fgInstallTimeMap.put(loc, l);
                            } catch (NumberFormatException nfe) {
                            //do nothing
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            log(e);
        } catch (ParserConfigurationException e) {
            log(e);
        } catch (SAXException e) {
            log(e);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) IPath(org.eclipse.core.runtime.IPath) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) BufferedInputStream(java.io.BufferedInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File)

Aggregations

DefaultHandler (org.xml.sax.helpers.DefaultHandler)139 InputStream (java.io.InputStream)61 Metadata (org.apache.tika.metadata.Metadata)59 ParseContext (org.apache.tika.parser.ParseContext)52 Test (org.junit.Test)43 Attributes (org.xml.sax.Attributes)39 SAXParser (javax.xml.parsers.SAXParser)35 SAXException (org.xml.sax.SAXException)34 ByteArrayInputStream (java.io.ByteArrayInputStream)30 SAXParserFactory (javax.xml.parsers.SAXParserFactory)25 IOException (java.io.IOException)24 Parser (org.apache.tika.parser.Parser)22 InputSource (org.xml.sax.InputSource)21 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)20 TikaInputStream (org.apache.tika.io.TikaInputStream)20 ContentHandler (org.xml.sax.ContentHandler)20 File (java.io.File)17 AutoDetectParser (org.apache.tika.parser.AutoDetectParser)17 BodyContentHandler (org.apache.tika.sax.BodyContentHandler)16 FileInputStream (java.io.FileInputStream)14