public WorkspaceLanguageConfiguration decodeWorkspaceMappings() throws CoreException { IEclipsePreferences node = InstanceScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID); IEclipsePreferences defaultNode = DefaultScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID); String encodedMappings = node.get(CCorePreferenceConstants.WORKSPACE_LANGUAGE_MAPPINGS, null); if (encodedMappings == null) { encodedMappings = defaultNode.get(CCorePreferenceConstants.WORKSPACE_LANGUAGE_MAPPINGS, null); } WorkspaceLanguageConfiguration config = new WorkspaceLanguageConfiguration(); if (encodedMappings == null || encodedMappings.length() == 0) { return config; } // The mappings are encoded as XML in a String so we need to parse it. InputSource input = new InputSource(new StringReader(encodedMappings)); try { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input); config.setWorkspaceMappings(decodeContentTypeMappings(document.getDocumentElement())); return config; } catch (SAXException e) { throw new CoreException(Util.createStatus(e)); } catch (IOException e) { throw new CoreException(Util.createStatus(e)); } catch (ParserConfigurationException e) { throw new CoreException(Util.createStatus(e)); } }
public void storeMappings(WorkspaceLanguageConfiguration config) throws CoreException { try { // Encode mappings as XML and serialize as a String. Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element rootElement = doc.createElement(WORKSPACE_MAPPINGS); doc.appendChild(rootElement); addContentTypeMappings(config.getWorkspaceMappings(), rootElement); Transformer serializer = createSerializer(); DOMSource source = new DOMSource(doc); StringWriter buffer = new StringWriter(); StreamResult result = new StreamResult(buffer); serializer.transform(source, result); String encodedMappings = buffer.getBuffer().toString(); IEclipsePreferences node = InstanceScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID); node.put(CCorePreferenceConstants.WORKSPACE_LANGUAGE_MAPPINGS, encodedMappings); node.flush(); } catch (ParserConfigurationException e) { throw new CoreException(Util.createStatus(e)); } catch (TransformerException e) { throw new CoreException(Util.createStatus(e)); } catch (BackingStoreException e) { throw new CoreException(Util.createStatus(e)); } }
private Transformer createSerializer() throws CoreException { try { return TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException e) { throw new CoreException(Util.createStatus(e)); } catch (TransformerFactoryConfigurationError e) { throw new CoreException(Util.createStatus(e)); } }