Search in sources :

Example 96 with Element

use of org.w3c.dom.Element in project che by eclipse.

the class Launching method writeInstallInfo.

/**
     * Writes out the mappings of SDK install time stamps to disk. See
     * https://bugs.eclipse.org/bugs/show_bug.cgi?id=266651 for more information.
     */
private static void writeInstallInfo() {
    if (fgInstallTimeMap != null) {
        OutputStream stream = null;
        try {
            Document doc = newDocument();
            //$NON-NLS-1$
            Element root = doc.createElement("dirs");
            doc.appendChild(root);
            Map.Entry<String, Long> entry = null;
            Element e = null;
            String key = null;
            for (Iterator<Map.Entry<String, Long>> i = fgInstallTimeMap.entrySet().iterator(); i.hasNext(); ) {
                entry = i.next();
                key = entry.getKey();
                if (fgLibraryInfoMap == null || fgLibraryInfoMap.containsKey(key)) {
                    //only persist the info if the library map also has info OR is null - prevent persisting deleted JRE information
                    //$NON-NLS-1$
                    e = doc.createElement("entry");
                    root.appendChild(e);
                    //$NON-NLS-1$
                    e.setAttribute("loc", key);
                    //$NON-NLS-1$
                    e.setAttribute("stamp", entry.getValue().toString());
                }
            }
            String xml = serializeDocument(doc);
            IPath libPath = getDefault().getStateLocation();
            //$NON-NLS-1$
            libPath = libPath.append(".install.xml");
            File file = libPath.toFile();
            if (!file.exists()) {
                file.createNewFile();
            }
            stream = new BufferedOutputStream(new FileOutputStream(file));
            //$NON-NLS-1$
            stream.write(xml.getBytes("UTF8"));
        } catch (IOException e) {
            log(e);
        } catch (CoreException e) {
            log(e);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                }
            }
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) CoreException(org.eclipse.core.runtime.CoreException) FileOutputStream(java.io.FileOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 97 with Element

use of org.w3c.dom.Element 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 98 with Element

use of org.w3c.dom.Element 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)

Example 99 with Element

use of org.w3c.dom.Element in project hackpad by dropbox.

the class XmlNode method addNamespaces.

private void addNamespaces(Namespaces rv, Element element) {
    if (element == null)
        throw new RuntimeException("element must not be null");
    String myDefaultNamespace = toUri(element.lookupNamespaceURI(null));
    String parentDefaultNamespace = "";
    if (element.getParentNode() != null) {
        parentDefaultNamespace = toUri(element.getParentNode().lookupNamespaceURI(null));
    }
    if (!myDefaultNamespace.equals(parentDefaultNamespace) || !(element.getParentNode() instanceof Element)) {
        rv.declare(Namespace.create("", myDefaultNamespace));
    }
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        if (attr.getPrefix() != null && attr.getPrefix().equals("xmlns")) {
            rv.declare(Namespace.create(attr.getLocalName(), attr.getValue()));
        }
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr)

Example 100 with Element

use of org.w3c.dom.Element in project hackpad by dropbox.

the class XmlNode method ecmaToXMLString.

String ecmaToXMLString(XmlProcessor processor) {
    if (this.isElementType()) {
        Element copy = (Element) this.dom.cloneNode(true);
        Namespace[] inScope = this.getInScopeNamespaces();
        for (int i = 0; i < inScope.length; i++) {
            declareNamespace(copy, inScope[i].getPrefix(), inScope[i].getUri());
        }
        return processor.ecmaToXmlString(copy);
    } else {
        return processor.ecmaToXmlString(dom);
    }
}
Also used : Element(org.w3c.dom.Element)

Aggregations

Element (org.w3c.dom.Element)9072 Document (org.w3c.dom.Document)2651 NodeList (org.w3c.dom.NodeList)2103 Node (org.w3c.dom.Node)1855 ArrayList (java.util.ArrayList)957 DocumentBuilder (javax.xml.parsers.DocumentBuilder)793 IOException (java.io.IOException)732 Test (org.junit.Test)693 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)591 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)489 HashMap (java.util.HashMap)434 SAXException (org.xml.sax.SAXException)406 File (java.io.File)358 Attr (org.w3c.dom.Attr)333 InputStream (java.io.InputStream)309 QName (javax.xml.namespace.QName)292 Map (java.util.Map)285 JAXBElement (javax.xml.bind.JAXBElement)285 NamedNodeMap (org.w3c.dom.NamedNodeMap)266 List (java.util.List)264