Search in sources :

Example 51 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project titan.EclipsePlug-ins by eclipse.

the class GenerateBuilderInformation method generateInfoForProject.

private void generateInfoForProject(final IProject project) throws CoreException {
    final boolean win32 = Platform.OS_WIN32.equals(Platform.getOS());
    final boolean reportDebugInformation = Platform.getPreferencesService().getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.DISPLAYDEBUGINFORMATION, false, null);
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        ErrorReporter.logExceptionStackTrace(e);
        return;
    }
    final DOMImplementation impl = builder.getDOMImplementation();
    final Document document = impl.createDocument(null, "TITAN_External_Builder_Information", null);
    final Element root = document.getDocumentElement();
    root.setAttribute("version", "1.0");
    String temp;
    Node node;
    final Element makefileSettings = document.createElement("Makefile_settings");
    root.appendChild(makefileSettings);
    for (int i = 0; i < MakefileCreationData.MAKEFILE_PROPERTIES.length; i++) {
        try {
            temp = project.getPersistentProperty(new QualifiedName(ProjectBuildPropertyData.QUALIFIER, MakefileCreationData.MAKEFILE_PROPERTIES[i]));
            node = document.createElement(MakefileCreationData.MAKEFILE_TAGS[i]);
            node.appendChild(document.createTextNode(temp));
            makefileSettings.appendChild(node);
        } catch (CoreException ce) {
            ErrorReporter.logExceptionStackTrace(ce);
        }
    }
    final String projectLocationStr = project.getLocation().toOSString();
    node = document.createElement("projectName");
    node.appendChild(document.createTextNode(project.getName()));
    makefileSettings.appendChild(node);
    node = document.createElement("projectRoot");
    node.appendChild(document.createTextNode(project.getLocationURI().toString()));
    makefileSettings.appendChild(node);
    temp = project.getPersistentProperty(new QualifiedName(ProjectBuildPropertyData.QUALIFIER, "workingDir"));
    node = document.createElement("workingDirectory");
    node.appendChild(document.createTextNode(TITANPathUtilities.resolvePathURI(temp, projectLocationStr).toString()));
    makefileSettings.appendChild(node);
    temp = project.getPersistentProperty(new QualifiedName(ProjectBuildPropertyData.QUALIFIER, "targetExecutable"));
    node = document.createElement("targetExecutable");
    node.appendChild(document.createTextNode(TITANPathUtilities.resolvePathURI(temp, projectLocationStr).toString()));
    makefileSettings.appendChild(node);
    temp = project.getPersistentProperty(new QualifiedName(ProjectBuildPropertyData.QUALIFIER, "makefileUpdateScript"));
    node = document.createElement("MakefileScript");
    node.appendChild(document.createTextNode(TITANPathUtilities.resolvePathURI(temp, projectLocationStr).toString()));
    makefileSettings.appendChild(node);
    temp = project.getPersistentProperty(new QualifiedName(ProjectBuildPropertyData.QUALIFIER, "makefileFlags"));
    node = document.createElement("MakefileFlags");
    node.appendChild(document.createTextNode(temp));
    makefileSettings.appendChild(node);
    final Element projectsElement = document.createElement("ReferencedProjects");
    root.appendChild(projectsElement);
    final IProject[] referencedProjects = ProjectBasedBuilder.getProjectBasedBuilder(project).getReferencedProjects();
    for (IProject tempProject : referencedProjects) {
        final Element element = document.createElement("ReferencedProject");
        element.setAttribute("name", tempProject.getName());
        element.setAttribute("location", tempProject.getLocationURI().toString());
        if (win32 && tempProject.getLocation() != null) {
            final String converted = PathConverter.convert(tempProject.getLocation().toOSString(), reportDebugInformation, TITANDebugConsole.getConsole());
            final Path path = new Path(converted);
            element.setAttribute("cygwinPath", URIUtil.toURI(path).toString());
        }
        projectsElement.appendChild(element);
    }
    final Element filesElement = document.createElement("Files");
    root.appendChild(filesElement);
    final TITANBuilderResourceVisitor visitor = ProjectBasedBuilder.getProjectBasedBuilder(project).getResourceVisitor();
    final Map<String, IFile> files = visitor.getFiles();
    for (IFile file : files.values()) {
        final Element element = document.createElement("File");
        element.setAttribute("path", file.getLocationURI().toString());
        if (win32 && file.getLocation() != null) {
            final String fileLocation = file.getLocation().toOSString();
            final String converted = PathConverter.convert(fileLocation, reportDebugInformation, TITANDebugConsole.getConsole());
            if (!converted.equals(fileLocation)) {
                final Path path = new Path(converted);
                element.setAttribute("cygwinPath", URIUtil.toURI(path).toString());
            }
        }
        element.setAttribute("relativePath", org.eclipse.core.runtime.URIUtil.makeRelative(file.getLocationURI(), project.getLocationURI()).toString());
        filesElement.appendChild(element);
    }
    final Map<String, IFile> contralStorageFiles = visitor.getCentralStorageFiles();
    for (IFile file : contralStorageFiles.values()) {
        final Element element = document.createElement("File");
        final String fileLocation = file.getLocation().toString();
        element.setAttribute("path", fileLocation);
        if (win32 && file.getLocation() != null) {
            final String converted = PathConverter.convert(fileLocation, reportDebugInformation, TITANDebugConsole.getConsole());
            if (!converted.equals(fileLocation)) {
                final Path path = new Path(converted);
                element.setAttribute("cygwinPath", URIUtil.toURI(path).toString());
            }
        }
        element.setAttribute("relativePath", org.eclipse.core.runtime.URIUtil.makeRelative(file.getLocationURI(), project.getLocationURI()).toString());
        element.setAttribute("centralStorage", "true");
        filesElement.appendChild(element);
    }
    final Map<String, IFile> filesOfReferencedProjects = ProjectBasedBuilder.getProjectBasedBuilder(project).getFilesofReferencedProjects();
    for (IFile file : filesOfReferencedProjects.values()) {
        final Element element = document.createElement("File");
        element.setAttribute("path", file.getLocationURI().toString());
        if (win32 && file.getLocation() != null) {
            final String fileLocation = file.getLocation().toOSString();
            final String converted = PathConverter.convert(fileLocation, reportDebugInformation, TITANDebugConsole.getConsole());
            if (!converted.equals(fileLocation)) {
                final Path path = new Path(converted);
                element.setAttribute("cygwinPath", URIUtil.toURI(path).toString());
            }
        }
        element.setAttribute("relativePath", org.eclipse.core.runtime.URIUtil.makeRelative(file.getLocationURI(), project.getLocationURI()).toString());
        element.setAttribute("fromProject", file.getProject().getName());
        filesElement.appendChild(element);
    }
    ProjectFileHandler.indentNode(document, document.getDocumentElement(), 1);
    System.setProperty(DOMImplementationRegistry.PROPERTY, DOM_IMPLEMENTATION_SOURCE);
    DOMImplementationRegistry registry = null;
    try {
        registry = DOMImplementationRegistry.newInstance();
    } catch (ClassNotFoundException ce) {
        ErrorReporter.logExceptionStackTrace(ce);
        return;
    } catch (InstantiationException ie) {
        ErrorReporter.logExceptionStackTrace(ie);
        return;
    } catch (IllegalAccessException iae) {
        ErrorReporter.logExceptionStackTrace(iae);
        return;
    }
    // Specifying "LS 3.0" in the features list ensures that the
    // DOMImplementation
    // object implements the load and save features of the DOM 3.0
    // specification.
    final DOMImplementation domImpl = registry.getDOMImplementation(LOAD_SAVE_VERSION);
    final DOMImplementationLS domImplLS = (DOMImplementationLS) domImpl;
    // If the mode is MODE_SYNCHRONOUS, the parse and parseURI
    // methods of
    // the LSParser
    // object return the org.w3c.dom.Document object. If the mode is
    // MODE_ASYNCHRONOUS,
    // the parse and parseURI methods return null.
    final LSParser parser = domImplLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, XML_SCHEMA);
    final DOMConfiguration config = parser.getDomConfig();
    final DOMErrorHandlerImpl errorHandler = new DOMErrorHandlerImpl();
    config.setParameter("error-handler", errorHandler);
    config.setParameter("validate", Boolean.TRUE);
    config.setParameter("schema-type", XML_SCHEMA);
    config.setParameter("validate-if-schema", Boolean.TRUE);
    final LSSerializer dom3Writer = domImplLS.createLSSerializer();
    final LSOutput output = domImplLS.createLSOutput();
    final IFile propertiesFile = project.getFile('/' + "external_build_information.xml");
    final File file = propertiesFile.getLocation().toFile();
    StringWriter sw = null;
    try {
        propertiesFile.refreshLocal(IResource.DEPTH_ZERO, null);
        sw = new StringWriter();
        output.setCharacterStream(sw);
        output.setEncoding("UTF-8");
        dom3Writer.write(document, output);
        final String temporaloutput = sw.getBuffer().toString();
        // temporalStorage will hold the contents of the
        // existing .TITAN_properties file
        String temporalStorage = null;
        if (propertiesFile.isAccessible() && file.exists() && file.canRead()) {
            final InputStream is = propertiesFile.getContents(true);
            final BufferedReader br = new BufferedReader(new InputStreamReader(is));
            final StringBuilder sb = new StringBuilder();
            boolean firstLine = true;
            String line = br.readLine();
            while (line != null) {
                if (firstLine) {
                    firstLine = false;
                } else {
                    sb.append('\n');
                }
                sb.append(line);
                line = br.readLine();
            }
            temporalStorage = sb.toString();
            br.close();
        }
        // one will be overwritten by the new one.
        if (temporalStorage == null || !temporalStorage.equals(temporaloutput)) {
            if (file.exists()) {
                propertiesFile.setContents(new ByteArrayInputStream(temporaloutput.getBytes()), IResource.FORCE | IResource.KEEP_HISTORY, null);
            } else {
                propertiesFile.create(new ByteArrayInputStream(temporaloutput.getBytes()), IResource.FORCE, null);
            }
            try {
                propertiesFile.refreshLocal(IResource.DEPTH_ZERO, null);
            } catch (CoreException e) {
                ErrorReporter.logExceptionStackTrace(e);
            }
        }
    } catch (IOException e) {
        ErrorReporter.logExceptionStackTrace(e);
    } catch (CoreException e) {
        ErrorReporter.logExceptionStackTrace(e);
    } finally {
        if (sw != null) {
            try {
                sw.close();
            } catch (IOException e) {
                ErrorReporter.logExceptionStackTrace(e);
            }
        }
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) IFile(org.eclipse.core.resources.IFile) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSParser(org.w3c.dom.ls.LSParser) DOMImplementation(org.w3c.dom.DOMImplementation) DOMConfiguration(org.w3c.dom.DOMConfiguration) Document(org.w3c.dom.Document) StringWriter(java.io.StringWriter) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LSOutput(org.w3c.dom.ls.LSOutput) DOMErrorHandlerImpl(org.eclipse.titan.designer.properties.data.DOMErrorHandlerImpl) Path(org.eclipse.core.runtime.Path) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) QualifiedName(org.eclipse.core.runtime.QualifiedName) LSSerializer(org.w3c.dom.ls.LSSerializer) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) TITANBuilderResourceVisitor(org.eclipse.titan.designer.core.TITANBuilderResourceVisitor) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 52 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project titan.EclipsePlug-ins by eclipse.

the class GUIProjectImporter method getDocumentFromFile.

/**
 * Helper function to convert the file provided into an XML document.
 *
 * @param file
 *                the project file to be read.
 *
 * @return the XML document read from the file or null if there was an
 *         error.
 */
private Document getDocumentFromFile(final String file) {
    // DOMImplementationRegistry is a factory that enables
    // applications to obtain instances of a DOMImplementation.
    System.setProperty(DOMImplementationRegistry.PROPERTY, DOM_IMPLEMENTATION_SOURCE);
    DOMImplementationRegistry registry = null;
    try {
        registry = DOMImplementationRegistry.newInstance();
    } catch (ClassNotFoundException ce) {
        ErrorReporter.logExceptionStackTrace(ce);
        return null;
    } catch (InstantiationException ie) {
        ErrorReporter.logExceptionStackTrace(ie);
        return null;
    } catch (IllegalAccessException iae) {
        ErrorReporter.logExceptionStackTrace(iae);
        return null;
    }
    // Specifying "LS 3.0" in the features list ensures that the
    // DOMImplementation
    // object implements the load and save features of the DOM 3.0
    // specification.
    DOMImplementation domImpl = registry.getDOMImplementation(LOAD_SAVE_VERSION);
    DOMImplementationLS domImplLS = (DOMImplementationLS) domImpl;
    // If the mode is MODE_SYNCHRONOUS, the parse and parseURI
    // methods of the LSParser
    // object return the org.w3c.dom.Document object. If the mode is
    // MODE_ASYNCHRONOUS, the parse and parseURI methods return null.
    LSParser parser = domImplLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, XML_SCHEMA);
    DOMConfiguration config = parser.getDomConfig();
    DOMErrorHandlerImpl errorHandler = new DOMErrorHandlerImpl();
    config.setParameter("error-handler", errorHandler);
    config.setParameter("validate", Boolean.TRUE);
    config.setParameter("schema-type", XML_SCHEMA);
    config.setParameter("validate-if-schema", Boolean.TRUE);
    final LSInput lsInput = domImplLS.createLSInput();
    try {
        InputStream istream = new FileInputStream(file);
        lsInput.setByteStream(istream);
        Document document = parser.parse(lsInput);
        istream.close();
        return document;
    } catch (IOException e) {
        ErrorReporter.logExceptionStackTrace(e);
        return null;
    } catch (DOMException e) {
        ErrorReporter.logExceptionStackTrace(e);
        return null;
    } catch (LSException e) {
        ErrorReporter.logExceptionStackTrace(e);
        return null;
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSParser(org.w3c.dom.ls.LSParser) DOMImplementation(org.w3c.dom.DOMImplementation) DOMConfiguration(org.w3c.dom.DOMConfiguration) IOException(java.io.IOException) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) DOMException(org.w3c.dom.DOMException) LSInput(org.w3c.dom.ls.LSInput) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) LSException(org.w3c.dom.ls.LSException) DOMErrorHandlerImpl(org.eclipse.titan.designer.properties.data.DOMErrorHandlerImpl)

Example 53 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project memory-map-plugin by Praqma.

the class JsonParser method getRawXml.

public static String getRawXml(Node node) throws Exception {
    Document nodeDocument = node.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) nodeDocument.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    return serializer.writeToString(node);
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer) Document(org.w3c.dom.Document)

Aggregations

DOMImplementationLS (org.w3c.dom.ls.DOMImplementationLS)53 LSSerializer (org.w3c.dom.ls.LSSerializer)42 Document (org.w3c.dom.Document)22 DOMImplementationRegistry (org.w3c.dom.bootstrap.DOMImplementationRegistry)21 LSOutput (org.w3c.dom.ls.LSOutput)18 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)15 IOException (java.io.IOException)13 DocumentBuilder (javax.xml.parsers.DocumentBuilder)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)13 Node (org.w3c.dom.Node)12 DOMImplementation (org.w3c.dom.DOMImplementation)10 StringWriter (java.io.StringWriter)9 Element (org.w3c.dom.Element)9 LSParser (org.w3c.dom.ls.LSParser)8 InputSource (org.xml.sax.InputSource)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 StringReader (java.io.StringReader)5 TransformerException (javax.xml.transform.TransformerException)5 NodeList (org.w3c.dom.NodeList)5 LSInput (org.w3c.dom.ls.LSInput)5