1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

bug 302720: [Error Parser] Build preferences should store only customized error parsers

This commit is contained in:
Andrew Gvozdev 2011-01-01 06:12:00 +00:00
parent 0593978730
commit 9454cc4d22
4 changed files with 288 additions and 136 deletions

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2009, 2010 Andrew Gvozdev (Quoin Inc.) and others. * Copyright (c) 2009, 2011 Andrew Gvozdev (Quoin Inc.) and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -11,11 +11,7 @@
package org.eclipse.cdt.internal.errorparsers; package org.eclipse.cdt.internal.errorparsers;
import java.io.FileInputStream; import java.net.URI;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
@ -25,16 +21,6 @@ import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ErrorParserManager; import org.eclipse.cdt.core.ErrorParserManager;
import org.eclipse.cdt.core.IErrorParser; import org.eclipse.cdt.core.IErrorParser;
@ -43,7 +29,6 @@ import org.eclipse.cdt.core.IMarkerGenerator;
import org.eclipse.cdt.core.errorparsers.ErrorParserNamedWrapper; import org.eclipse.cdt.core.errorparsers.ErrorParserNamedWrapper;
import org.eclipse.cdt.core.errorparsers.RegexErrorParser; import org.eclipse.cdt.core.errorparsers.RegexErrorParser;
import org.eclipse.cdt.core.errorparsers.RegexErrorPattern; import org.eclipse.cdt.core.errorparsers.RegexErrorPattern;
import org.eclipse.cdt.core.resources.ResourcesUtil;
import org.eclipse.cdt.internal.core.XmlUtil; import org.eclipse.cdt.internal.core.XmlUtil;
import org.eclipse.core.filesystem.URIUtil; import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
@ -52,18 +37,14 @@ import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint; import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry; import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.core.runtime.preferences.InstanceScope;
import org.osgi.service.prefs.BackingStoreException; import org.osgi.service.prefs.BackingStoreException;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/** /**
* ErrorParserExtensionManager manages error parser extensions, serialization and preferences * ErrorParserExtensionManager manages error parser extensions, serialization and preferences
@ -145,9 +126,9 @@ public class ErrorParserExtensionManager {
fUserDefinedErrorParsers = null; fUserDefinedErrorParsers = null;
Document doc = null; Document doc = null;
try { try {
doc = loadXml(getStoreLocation(STORAGE_ERRORPARSER_EXTENSIONS)); doc = XmlUtil.loadXml(getStoreURI(STORAGE_ERRORPARSER_EXTENSIONS));
} catch (Exception e) { } catch (Exception e) {
CCorePlugin.log("Can't load preferences from file "+STORAGE_ERRORPARSER_EXTENSIONS, e); //$NON-NLS-1$ CCorePlugin.log("Can't load preferences from "+STORAGE_ERRORPARSER_EXTENSIONS, e); //$NON-NLS-1$
} }
if (doc!=null) { if (doc!=null) {
@ -164,25 +145,6 @@ public class ErrorParserExtensionManager {
recalculateAvailableErrorParsers(); recalculateAvailableErrorParsers();
} }
/**
* Load XML from file to DOM Document.
*
* @param location - location of XML file
* @return new loaded XML Document or {@code null} if file does not exist
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
private static Document loadXml(IPath location) throws ParserConfigurationException, SAXException, IOException {
java.io.File storeFile = location.toFile();
if (storeFile.exists()) {
InputStream xmlStream = new FileInputStream(storeFile);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
return builder.parse(xmlStream);
}
return null;
}
/** /**
* Parse error parser contributed extensions from XML document. * Parse error parser contributed extensions from XML document.
* *
@ -191,21 +153,19 @@ public class ErrorParserExtensionManager {
*/ */
private static void loadErrorParserExtensions(Document doc, Set<IErrorParserNamed> errorParsers) { private static void loadErrorParserExtensions(Document doc, Set<IErrorParserNamed> errorParsers) {
errorParsers.clear(); errorParsers.clear();
NodeList extentionNodes = doc.getElementsByTagName(ELEM_EXTENSION); NodeList extensionNodes = doc.getElementsByTagName(ELEM_EXTENSION);
for (int iext=0;iext<extentionNodes.getLength();iext++) { for (int iext=0;iext<extensionNodes.getLength();iext++) {
Node extentionNode = extentionNodes.item(iext); Node extensionNode = extensionNodes.item(iext);
if(extentionNode.getNodeType() != Node.ELEMENT_NODE) if(extensionNode.getNodeType() != Node.ELEMENT_NODE)
continue; continue;
NodeList errorparserNodes = extentionNode.getChildNodes(); NodeList errorparserNodes = extensionNode.getChildNodes();
for (int ierp=0;ierp<errorparserNodes.getLength();ierp++) { for (int ierp=0;ierp<errorparserNodes.getLength();ierp++) {
Node errorparserNode = errorparserNodes.item(ierp); Node errorparserNode = errorparserNodes.item(ierp);
if(errorparserNode.getNodeType() != Node.ELEMENT_NODE || ! ELEM_ERRORPARSER.equals(errorparserNode.getNodeName())) if(errorparserNode.getNodeType() != Node.ELEMENT_NODE || ! ELEM_ERRORPARSER.equals(errorparserNode.getNodeName()))
continue; continue;
NamedNodeMap errorParserAttributes = errorparserNode.getAttributes(); String className = XmlUtil.determineAttributeValue(errorparserNode, ATTR_CLASS);
String className = determineNodeValue(errorParserAttributes.getNamedItem(ATTR_CLASS));
try { try {
IErrorParserNamed errorParser = createErrorParserCarcass(className, Platform.getExtensionRegistry()); IErrorParserNamed errorParser = createErrorParserCarcass(className, Platform.getExtensionRegistry());
if (errorParser!=null) { if (errorParser!=null) {
@ -336,10 +296,8 @@ public class ErrorParserExtensionManager {
*/ */
public static void serializeUserDefinedErrorParsers() throws CoreException { public static void serializeUserDefinedErrorParsers() throws CoreException {
try { try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = XmlUtil.newDocument();
Document doc = builder.newDocument(); Element elementPlugin = XmlUtil.appendElement(doc, ELEM_PLUGIN);
Element elementPlugin = doc.createElement(ELEM_PLUGIN);
doc.appendChild(elementPlugin);
if (fUserDefinedErrorParsers!=null) { if (fUserDefinedErrorParsers!=null) {
for (Entry<String, IErrorParserNamed> entry: fUserDefinedErrorParsers.entrySet()) { for (Entry<String, IErrorParserNamed> entry: fUserDefinedErrorParsers.entrySet()) {
@ -348,10 +306,10 @@ public class ErrorParserExtensionManager {
} }
} }
serializeXml(doc, getStoreLocation(STORAGE_ERRORPARSER_EXTENSIONS)); XmlUtil.serializeXml(doc, getStoreURI(STORAGE_ERRORPARSER_EXTENSIONS));
} catch (Exception e) { } catch (Exception e) {
throw new CoreException(new Status(IStatus.ERROR, "Failed serializing to file " + STORAGE_ERRORPARSER_EXTENSIONS, CCorePlugin.PLUGIN_ID, e)); //$NON-NLS-1$ throw new CoreException(CCorePlugin.createStatus("Failed serializing to file " + STORAGE_ERRORPARSER_EXTENSIONS, e)); //$NON-NLS-1$
} }
} }
@ -406,23 +364,19 @@ public class ErrorParserExtensionManager {
if (errorParser instanceof ErrorParserNamedWrapper) if (errorParser instanceof ErrorParserNamedWrapper)
errorParser = ((ErrorParserNamedWrapper)errorParser).getErrorParser(); errorParser = ((ErrorParserNamedWrapper)errorParser).getErrorParser();
Document doc = elementPlugin.getOwnerDocument();
// <extension/> // <extension/>
Element elementExtension = doc.createElement(ELEM_EXTENSION); Element elementExtension = XmlUtil.appendElement(elementPlugin, ELEM_EXTENSION, new String[] {
elementExtension.setAttribute(ATTR_ID, simpleId); ATTR_ID, simpleId,
elementExtension.setAttribute(ATTR_NAME, name); ATTR_NAME, name,
elementExtension.setAttribute(ATTR_POINT, EXTENSION_POINT_ERROR_PARSER); ATTR_POINT, EXTENSION_POINT_ERROR_PARSER,
});
elementPlugin.appendChild(elementExtension);
// <errorparser/> // <errorparser/>
Element elementErrorParser = doc.createElement(ELEM_ERRORPARSER); Element elementErrorParser = XmlUtil.appendElement(elementExtension, ELEM_ERRORPARSER, new String[] {
elementErrorParser.setAttribute(ATTR_ID, id); ATTR_ID, id,
elementErrorParser.setAttribute(ATTR_NAME, name); ATTR_NAME, name,
elementErrorParser.setAttribute(ATTR_CLASS, errorParser.getClass().getCanonicalName()); ATTR_CLASS, errorParser.getClass().getCanonicalName(),
});
elementExtension.appendChild(elementErrorParser);
if (errorParserNamed instanceof RegexErrorParser) { if (errorParserNamed instanceof RegexErrorParser) {
RegexErrorParser regexErrorParser = (RegexErrorParser)errorParserNamed; RegexErrorParser regexErrorParser = (RegexErrorParser)errorParserNamed;
@ -430,15 +384,15 @@ public class ErrorParserExtensionManager {
for (RegexErrorPattern pattern : patterns) { for (RegexErrorPattern pattern : patterns) {
// <pattern/> // <pattern/>
Element elementPattern = doc.createElement(ELEM_PATTERN); @SuppressWarnings("unused")
elementPattern.setAttribute(ATTR_SEVERITY, severityToString(pattern.getSeverity())); Element elementPattern = XmlUtil.appendElement(elementErrorParser, ELEM_PATTERN, new String[] {
elementPattern.setAttribute(ATTR_REGEX, pattern.getPattern()); ATTR_SEVERITY, severityToString(pattern.getSeverity()),
elementPattern.setAttribute(ATTR_FILE, pattern.getFileExpression()); ATTR_REGEX, pattern.getPattern(),
elementPattern.setAttribute(ATTR_LINE, pattern.getLineExpression()); ATTR_FILE, pattern.getFileExpression(),
elementPattern.setAttribute(ATTR_DESCRIPTION, pattern.getDescriptionExpression()); ATTR_LINE, pattern.getLineExpression(),
elementPattern.setAttribute(ATTR_EAT_LINE, String.valueOf(pattern.isEatProcessedLine())); ATTR_DESCRIPTION, pattern.getDescriptionExpression(),
ATTR_EAT_LINE, String.valueOf(pattern.isEatProcessedLine()),
elementErrorParser.appendChild(elementPattern); });
} }
} }
@ -459,37 +413,6 @@ public class ErrorParserExtensionManager {
return simpleId; return simpleId;
} }
/**
* Serialize XML Document in a file.
*
* @param doc - XML to serialize
* @param location - location of the file
* @throws IOException in case of problems with file I/O
* @throws TransformerException in case of problems with XML output
*/
synchronized private static void serializeXml(Document doc, IPath location) throws IOException, TransformerException {
java.io.File storeFile = location.toFile();
if (!storeFile.exists()) {
storeFile.createNewFile();
}
OutputStream fileStream = new FileOutputStream(storeFile);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
XmlUtil.prettyFormat(doc);
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new FileOutputStream(storeFile));
transformer.transform(source, result);
fileStream.close();
ResourcesUtil.refreshWorkspaceFiles(URIUtil.toURI(location));
}
/** /**
* Save the list of default error parsers in preferences. * Save the list of default error parsers in preferences.
* *
@ -508,10 +431,12 @@ public class ErrorParserExtensionManager {
/** /**
* @param store - name of the store * @param store - name of the store
* @return location of the store in the plug-in state area * @return URI of the store in the plug-in state area to keep plug-in specific data.
*/ */
private static IPath getStoreLocation(String store) { private static URI getStoreURI(String store) {
return CCorePlugin.getDefault().getStateLocation().append(store); IPath location = CCorePlugin.getDefault().getStateLocation().append(store);
URI uri = URIUtil.toURI(location);
return uri;
} }
/** /**
@ -582,9 +507,8 @@ public class ErrorParserExtensionManager {
* @param errorparserNode - XML error parser node * @param errorparserNode - XML error parser node
*/ */
private static void configureErrorParser(IErrorParserNamed errorParser, Node errorparserNode) { private static void configureErrorParser(IErrorParserNamed errorParser, Node errorparserNode) {
NamedNodeMap errorParserAttributes = errorparserNode.getAttributes(); String id = XmlUtil.determineAttributeValue(errorparserNode, ATTR_ID);
String id = determineNodeValue(errorParserAttributes.getNamedItem(ATTR_ID)); String name = XmlUtil.determineAttributeValue(errorparserNode, ATTR_NAME);
String name = determineNodeValue(errorParserAttributes.getNamedItem(ATTR_NAME));
errorParser.setId(id); errorParser.setId(id);
errorParser.setName(name); errorParser.setName(name);
if (errorParser instanceof RegexErrorParser) { if (errorParser instanceof RegexErrorParser) {
@ -596,13 +520,12 @@ public class ErrorParserExtensionManager {
if(patternNode.getNodeType() != Node.ELEMENT_NODE || ! ELEM_PATTERN.equals(patternNode.getNodeName())) if(patternNode.getNodeType() != Node.ELEMENT_NODE || ! ELEM_PATTERN.equals(patternNode.getNodeName()))
continue; continue;
NamedNodeMap patternAttributes = patternNode.getAttributes(); String attrSeverity = XmlUtil.determineAttributeValue(patternNode, ATTR_SEVERITY);
String attrSeverity = determineNodeValue(patternAttributes.getNamedItem(ATTR_SEVERITY)); String regex = XmlUtil.determineAttributeValue(patternNode, ATTR_REGEX);
String regex = determineNodeValue(patternAttributes.getNamedItem(ATTR_REGEX)); String fileExpr = XmlUtil.determineAttributeValue(patternNode, ATTR_FILE);
String fileExpr = determineNodeValue(patternAttributes.getNamedItem(ATTR_FILE)); String lineExpr = XmlUtil.determineAttributeValue(patternNode, ATTR_LINE);
String lineExpr = determineNodeValue(patternAttributes.getNamedItem(ATTR_LINE)); String DescExpr = XmlUtil.determineAttributeValue(patternNode, ATTR_DESCRIPTION);
String DescExpr = determineNodeValue(patternAttributes.getNamedItem(ATTR_DESCRIPTION)); String attrEatLine = XmlUtil.determineAttributeValue(patternNode, ATTR_EAT_LINE);
String attrEatLine = determineNodeValue(patternAttributes.getNamedItem(ATTR_EAT_LINE));
int severity = stringToSeverity(attrSeverity); int severity = stringToSeverity(attrSeverity);
@ -613,14 +536,6 @@ public class ErrorParserExtensionManager {
} }
} }
/**
* @param node
* @return node value or {@code null}
*/
private static String determineNodeValue(Node node) {
return node!=null ? node.getNodeValue() : null;
}
/** /**
* Configure error parser from extension configuration element. * Configure error parser from extension configuration element.
* *

View file

@ -49,6 +49,8 @@ public class Messages extends NLS {
public static String convention_enum_leadingUnderscore; public static String convention_enum_leadingUnderscore;
public static String convention_enum_lowercaseName; public static String convention_enum_lowercaseName;
public static String convention_enum_invalidName; public static String convention_enum_invalidName;
public static String XmlUtil_InternalErrorLoading;
public static String XmlUtil_InternalErrorSerializing;
static { static {
// initialize resource bundle // initialize resource bundle

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2009, 2009 Andrew Gvozdev (Quoin Inc.). * Copyright (c) 2009, 2011 Andrew Gvozdev (Quoin Inc.).
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -10,8 +10,33 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core; package org.eclipse.cdt.internal.core;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.resources.ResourcesUtil;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
@ -23,6 +48,78 @@ public class XmlUtil {
private static final String EOL_XML = "\n"; //$NON-NLS-1$ private static final String EOL_XML = "\n"; //$NON-NLS-1$
private static final String DEFAULT_IDENT = "\t"; //$NON-NLS-1$ private static final String DEFAULT_IDENT = "\t"; //$NON-NLS-1$
/**
* Convenience method to create new XML DOM Document.
*
* @return a new instance of a DOM {@link Document}.
* @throws ParserConfigurationException in case of a problem retrieving {@link DocumentBuilder}.
*/
public static Document newDocument() throws ParserConfigurationException {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
return builder.newDocument();
}
/**
* Convenience method to retrieve value of a node.
* @return node value or {@code null}
*/
public static String determineNodeValue(Node node) {
return node!=null ? node.getNodeValue() : null;
}
/**
* Convenience method to retrieve an attribute of an element.
* Note that calling element.getAttributes() once may be more efficient when pulling several attributes.
*
* @param element - element to retrieve the attribute from.
* @param attr - attribute to get value.
* @return attribute value or {@code null}
*/
public static String determineAttributeValue(Node element, String attr) {
NamedNodeMap attributes = element.getAttributes();
return attributes!=null ? determineNodeValue(attributes.getNamedItem(attr)) : null;
}
/**
* The method creates an element with specified name and attributes and appends it to the parent element.
* This is a convenience method for often used sequence of calls.
*
* @param parent - the node where to append the new element.
* @param name - the name of the element type being created.
* @param attributes - string array of pairs attributes and their values.
* Each attribute must have a value, so the array must have even number of elements.
* @return the newly created element.
*
* @throws ArrayIndexOutOfBoundsException in case of odd number of elements of the attribute array
* (i.e. the last attribute is missing a value).
*/
public static Element appendElement(Node parent, String name, String[] attributes) {
Document doc = parent instanceof Document ? (Document)parent : parent.getOwnerDocument();
Element element = doc.createElement(name);
if (attributes!=null) {
int attrLen = attributes.length;
for (int i=0;i<attrLen;i+=2) {
String attrName = attributes[i];
String attrValue = attributes[i+1];
element.setAttribute(attrName, attrValue);
}
}
parent.appendChild(element);
return element;
}
/**
* The method creates an element with specified name and appends it to the parent element.
* This is a shortcut for {@link #appendElement(Node, String, String[])} with no attributes specified.
*
* @param parent - the node where to append the new element.
* @param name - the name of the element type being created.
* @return the newly created element.
*/
public static Element appendElement(Node parent, String name) {
return appendElement(parent, name, null);
}
/** /**
* As a workaround for {@code javax.xml.transform.Transformer} not being able * As a workaround for {@code javax.xml.transform.Transformer} not being able
* to pretty print XML. This method prepares DOM {@code Document} for the transformer * to pretty print XML. This method prepares DOM {@code Document} for the transformer
@ -52,7 +149,7 @@ public class XmlUtil {
/** /**
* The method inserts end-of-line+indentation Text nodes where indentation is necessary. * The method inserts end-of-line+indentation Text nodes where indentation is necessary.
* *
* @param node - node to be pretty formatted * @param node - node to be pretty formatted
* @param identLevel - initial indentation level of the node * @param identLevel - initial indentation level of the node
* @param ident - additional indentation inside the node * @param ident - additional indentation inside the node
@ -101,8 +198,143 @@ public class XmlUtil {
} }
} }
} }
}
/**
* Load XML from input stream to DOM Document.
*
* @param xmlStream - XML stream.
* @return new loaded DOM Document.
* @throws CoreException if something goes wrong.
*/
private static Document loadXml(InputStream xmlStream) throws CoreException {
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
return builder.parse(xmlStream);
} catch (Exception e) {
throw new CoreException(CCorePlugin.createStatus(Messages.XmlUtil_InternalErrorLoading, e));
}
}
/**
* Load XML from file to DOM Document.
*
* @param uriLocation - location of XML file.
* @return new loaded XML Document or {@code null} if file does not exist.
* @throws CoreException if something goes wrong.
*/
public static Document loadXml(URI uriLocation) throws CoreException {
java.io.File xmlFile = new java.io.File(uriLocation);
if (!xmlFile.exists()) {
return null;
}
InputStream xmlStream;
try {
xmlStream = new FileInputStream(xmlFile);
} catch (Exception e) {
throw new CoreException(CCorePlugin.createStatus(Messages.XmlUtil_InternalErrorLoading, e));
}
return loadXml(xmlStream);
}
/**
* Load XML from file to DOM Document.
*
* @param xmlFile - XML file
* @return new loaded XML Document.
* @throws CoreException if something goes wrong.
*/
public static Document loadXml(IFile xmlFile) throws CoreException {
InputStream xmlStream = xmlFile.getContents();
return loadXml(xmlStream);
}
/**
* Serialize XML Document into a file.<br/>
* Note: clients should synchronize access to this method.
*
* @param doc - DOM Document to serialize.
* @param uriLocation - URI of the file.
* @throws IOException in case of problems with file I/O
* @throws TransformerException in case of problems with XML output
*/
public static void serializeXml(Document doc, URI uriLocation) throws IOException, TransformerException {
XmlUtil.prettyFormat(doc);
java.io.File storeFile = new java.io.File(uriLocation);
if (!storeFile.exists()) {
storeFile.createNewFile();
}
OutputStream fileStream = new FileOutputStream(storeFile);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
XmlUtil.prettyFormat(doc);
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new FileOutputStream(storeFile));
transformer.transform(source, result);
fileStream.close();
ResourcesUtil.refreshWorkspaceFiles(uriLocation);
}
/**
* Serialize XML Document into a byte array.
* @param doc - DOM Document to serialize.
* @return XML as a byte array.
* @throws CoreException if something goes wrong.
*/
private static byte[] toByteArray(Document doc) throws CoreException {
XmlUtil.prettyFormat(doc);
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(stream);
transformer.transform(source, result);
return stream.toByteArray();
} catch (Exception e) {
throw new CoreException(CCorePlugin.createStatus(Messages.XmlUtil_InternalErrorSerializing, e));
}
}
/**
* Serialize XML Document into a workspace file.<br/>
* Note: clients should synchronize access to this method.
*
* @param doc - DOM Document to serialize.
* @param file - file where to write the XML.
* @throws CoreException if something goes wrong.
*/
public static void serializeXml(Document doc, IFile file) throws CoreException {
XmlUtil.prettyFormat(doc);
InputStream input = new ByteArrayInputStream(toByteArray(doc));
if (file.exists()) {
file.setContents(input, IResource.FORCE, null);
} else {
file.create(input, IResource.FORCE, null);
}
}
/**
* Serialize XML Document into a string.
*
* @param doc - DOM Document to serialize.
* @return XML as a String.
* @throws CoreException if something goes wrong.
*/
public static String toString(Document doc) throws CoreException {
return new String(toByteArray(doc));
} }
} }

View file

@ -46,4 +46,7 @@ convention_enum_lowercaseName= Enum name starts with lower case
convention_enum_invalidName= Enum name is invalid convention_enum_invalidName= Enum name is invalid
Util_unexpectedError=Unexpected error Util_unexpectedError=Unexpected error
Addr_valueOutOfRange=Address is outside valid range. Addr_valueOutOfRange=Address is outside valid range.
XmlUtil_InternalErrorLoading=Internal error while trying to load XML document
XmlUtil_InternalErrorSerializing=Internal error while trying to serialize XML Document.