use of javax.xml.parsers.DocumentBuilder 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;
}
use of javax.xml.parsers.DocumentBuilder in project che by eclipse.
the class DialogSettings method load.
/* (non-Javadoc)
* Method declared on IDialogSettings.
*/
@Override
public void load(Reader r) {
Document document = null;
try {
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
// parser.setProcessNamespace(true);
document = parser.parse(new InputSource(r));
//Strip out any comments first
Node root = document.getFirstChild();
while (root.getNodeType() == Node.COMMENT_NODE) {
document.removeChild(root);
root = document.getFirstChild();
}
load(document, (Element) root);
} catch (ParserConfigurationException e) {
// ignore
} catch (IOException e) {
// ignore
} catch (SAXException e) {
// ignore
}
}
use of javax.xml.parsers.DocumentBuilder 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);
}
}
}
use of javax.xml.parsers.DocumentBuilder 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);
}
}
}
use of javax.xml.parsers.DocumentBuilder in project che by eclipse.
the class Launching method getDocument.
/**
* Returns a Document that can be used to build a DOM tree
* @return the Document
* @throws ParserConfigurationException if an exception occurs creating the document builder
*/
public static Document getDocument() throws ParserConfigurationException {
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
return doc;
}
Aggregations