1 11 12 package org.eclipse.jdt.internal.ui.preferences.formatter; 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.ByteArrayOutputStream ; 16 import java.io.File ; 17 import java.io.FileInputStream ; 18 import java.io.FileOutputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.io.UnsupportedEncodingException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import javax.xml.parsers.DocumentBuilder ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import javax.xml.parsers.ParserConfigurationException ; 33 import javax.xml.parsers.SAXParser ; 34 import javax.xml.parsers.SAXParserFactory ; 35 import javax.xml.transform.OutputKeys ; 36 import javax.xml.transform.Transformer ; 37 import javax.xml.transform.TransformerException ; 38 import javax.xml.transform.TransformerFactory ; 39 import javax.xml.transform.dom.DOMSource ; 40 import javax.xml.transform.stream.StreamResult ; 41 42 import org.eclipse.core.runtime.CoreException; 43 import org.eclipse.core.runtime.IStatus; 44 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 45 import org.eclipse.core.runtime.preferences.IScopeContext; 46 47 import org.eclipse.jdt.ui.JavaUI; 48 49 import org.eclipse.jdt.internal.ui.JavaPlugin; 50 import org.eclipse.jdt.internal.ui.JavaUIException; 51 import org.eclipse.jdt.internal.ui.JavaUIStatus; 52 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile; 53 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile; 54 55 import org.w3c.dom.Document ; 56 import org.w3c.dom.Element ; 57 import org.xml.sax.Attributes ; 58 import org.xml.sax.InputSource ; 59 import org.xml.sax.SAXException ; 60 import org.xml.sax.helpers.DefaultHandler ; 61 62 63 66 public class ProfileStore { 67 68 69 public static final String ENCODING= "UTF-8"; 71 protected static final String VERSION_KEY_SUFFIX= ".version"; 73 76 private final static class ProfileDefaultHandler extends DefaultHandler { 77 78 private List fProfiles; 79 private int fVersion; 80 81 private String fName; 82 private Map fSettings; 83 private String fKind; 84 85 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 86 87 if (qName.equals(XML_NODE_SETTING)) { 88 89 final String key= attributes.getValue(XML_ATTRIBUTE_ID); 90 final String value= attributes.getValue(XML_ATTRIBUTE_VALUE); 91 fSettings.put(key, value); 92 93 } else if (qName.equals(XML_NODE_PROFILE)) { 94 95 fName= attributes.getValue(XML_ATTRIBUTE_NAME); 96 fKind= attributes.getValue(XML_ATTRIBUTE_PROFILE_KIND); 97 if (fKind == null) fKind= ProfileVersioner.CODE_FORMATTER_PROFILE_KIND; 99 100 fSettings= new HashMap (200); 101 102 } 103 else if (qName.equals(XML_NODE_ROOT)) { 104 105 fProfiles= new ArrayList (); 106 try { 107 fVersion= Integer.parseInt(attributes.getValue(XML_ATTRIBUTE_VERSION)); 108 } catch (NumberFormatException ex) { 109 throw new SAXException (ex); 110 } 111 112 } 113 } 114 115 public void endElement(String uri, String localName, String qName) { 116 if (qName.equals(XML_NODE_PROFILE)) { 117 fProfiles.add(new CustomProfile(fName, fSettings, fVersion, fKind)); 118 fName= null; 119 fSettings= null; 120 fKind= null; 121 } 122 } 123 124 public List getProfiles() { 125 return fProfiles; 126 } 127 128 } 129 130 133 private final static String XML_NODE_ROOT= "profiles"; private final static String XML_NODE_PROFILE= "profile"; private final static String XML_NODE_SETTING= "setting"; 137 private final static String XML_ATTRIBUTE_VERSION= "version"; private final static String XML_ATTRIBUTE_ID= "id"; private final static String XML_ATTRIBUTE_NAME= "name"; private final static String XML_ATTRIBUTE_PROFILE_KIND= "kind"; private final static String XML_ATTRIBUTE_VALUE= "value"; 143 private final IProfileVersioner fProfileVersioner; 144 private final String fProfilesKey; 145 private final String fProfilesVersionKey; 146 147 148 public ProfileStore(String profilesKey, IProfileVersioner profileVersioner) { 149 fProfilesKey= profilesKey; 150 fProfileVersioner= profileVersioner; 151 fProfilesVersionKey= profilesKey + VERSION_KEY_SUFFIX; 152 } 153 154 160 public List readProfiles(IScopeContext scope) throws CoreException { 161 return readProfilesFromString(scope.getNode(JavaUI.ID_PLUGIN).get(fProfilesKey, null)); 162 } 163 164 public void writeProfiles(Collection profiles, IScopeContext instanceScope) throws CoreException { 165 ByteArrayOutputStream stream= new ByteArrayOutputStream (2000); 166 try { 167 writeProfilesToStream(profiles, stream, ENCODING, fProfileVersioner); 168 String val; 169 try { 170 val= stream.toString(ENCODING); 171 } catch (UnsupportedEncodingException e) { 172 val= stream.toString(); 173 } 174 IEclipsePreferences uiPreferences = instanceScope.getNode(JavaUI.ID_PLUGIN); 175 uiPreferences.put(fProfilesKey, val); 176 uiPreferences.putInt(fProfilesVersionKey, fProfileVersioner.getCurrentVersion()); 177 } finally { 178 try { stream.close(); } catch (IOException e) { } 179 } 180 } 181 182 public List readProfilesFromString(String profiles) throws CoreException { 183 if (profiles != null && profiles.length() > 0) { 184 byte[] bytes; 185 try { 186 bytes= profiles.getBytes(ENCODING); 187 } catch (UnsupportedEncodingException e) { 188 bytes= profiles.getBytes(); 189 } 190 InputStream is= new ByteArrayInputStream (bytes); 191 try { 192 List res= readProfilesFromStream(new InputSource (is)); 193 if (res != null) { 194 for (int i= 0; i < res.size(); i++) { 195 fProfileVersioner.update((CustomProfile) res.get(i)); 196 } 197 } 198 return res; 199 } finally { 200 try { is.close(); } catch (IOException e) { } 201 } 202 } 203 return null; 204 } 205 206 207 214 public List readProfilesFromFile(File file) throws CoreException { 215 try { 216 final FileInputStream reader= new FileInputStream (file); 217 try { 218 return readProfilesFromStream(new InputSource (reader)); 219 } finally { 220 try { reader.close(); } catch (IOException e) { } 221 } 222 } catch (IOException e) { 223 throw createException(e, FormatterMessages.CodingStyleConfigurationBlock_error_reading_xml_message); 224 } 225 } 226 227 233 public static List readProfilesFromStream(InputSource inputSource) throws CoreException { 234 235 final ProfileDefaultHandler handler= new ProfileDefaultHandler(); 236 try { 237 final SAXParserFactory factory= SAXParserFactory.newInstance(); 238 final SAXParser parser= factory.newSAXParser(); 239 parser.parse(inputSource, handler); 240 } catch (SAXException e) { 241 throw createException(e, FormatterMessages.CodingStyleConfigurationBlock_error_reading_xml_message); 242 } catch (IOException e) { 243 throw createException(e, FormatterMessages.CodingStyleConfigurationBlock_error_reading_xml_message); 244 } catch (ParserConfigurationException e) { 245 throw createException(e, FormatterMessages.CodingStyleConfigurationBlock_error_reading_xml_message); 246 } 247 return handler.getProfiles(); 248 } 249 250 257 public void writeProfilesToFile(Collection profiles, File file, String encoding) throws CoreException { 258 final OutputStream stream; 259 try { 260 stream= new FileOutputStream (file); 261 try { 262 writeProfilesToStream(profiles, stream, encoding, fProfileVersioner); 263 } finally { 264 try { stream.close(); } catch (IOException e) { } 265 } 266 } catch (IOException e) { 267 throw createException(e, FormatterMessages.CodingStyleConfigurationBlock_error_serializing_xml_message); 268 } 269 } 270 271 278 public static void writeProfilesToStream(Collection profiles, OutputStream stream, String encoding, IProfileVersioner profileVersioner) throws CoreException { 279 280 try { 281 final DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); 282 final DocumentBuilder builder= factory.newDocumentBuilder(); 283 final Document document= builder.newDocument(); 284 285 final Element rootElement = document.createElement(XML_NODE_ROOT); 286 rootElement.setAttribute(XML_ATTRIBUTE_VERSION, Integer.toString(profileVersioner.getCurrentVersion())); 287 288 document.appendChild(rootElement); 289 290 for(final Iterator iter= profiles.iterator(); iter.hasNext();) { 291 final Profile profile= (Profile)iter.next(); 292 if (profile.isProfileToSave()) { 293 final Element profileElement= createProfileElement(profile, document, profileVersioner); 294 rootElement.appendChild(profileElement); 295 } 296 } 297 298 Transformer transformer=TransformerFactory.newInstance().newTransformer(); 299 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); 301 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource (document), new StreamResult (stream)); 303 } catch (TransformerException e) { 304 throw createException(e, FormatterMessages.CodingStyleConfigurationBlock_error_serializing_xml_message); 305 } catch (ParserConfigurationException e) { 306 throw createException(e, FormatterMessages.CodingStyleConfigurationBlock_error_serializing_xml_message); 307 } 308 } 309 310 311 315 private static Element createProfileElement(Profile profile, Document document, IProfileVersioner profileVersioner) { 316 final Element element= document.createElement(XML_NODE_PROFILE); 317 element.setAttribute(XML_ATTRIBUTE_NAME, profile.getName()); 318 element.setAttribute(XML_ATTRIBUTE_VERSION, Integer.toString(profile.getVersion())); 319 element.setAttribute(XML_ATTRIBUTE_PROFILE_KIND, profileVersioner.getProfileKind()); 320 321 final Iterator keyIter= profile.getSettings().keySet().iterator(); 322 323 while (keyIter.hasNext()) { 324 final String key= (String )keyIter.next(); 325 final String value= (String )profile.getSettings().get(key); 326 if (value != null) { 327 final Element setting= document.createElement(XML_NODE_SETTING); 328 setting.setAttribute(XML_ATTRIBUTE_ID, key); 329 setting.setAttribute(XML_ATTRIBUTE_VALUE, value); 330 element.appendChild(setting); 331 } else { 332 JavaPlugin.logErrorMessage("ProfileStore: Profile does not contain value for key " + key); } 334 } 335 return element; 336 } 337 338 339 342 private static JavaUIException createException(Throwable t, String message) { 343 return new JavaUIException(JavaUIStatus.createError(IStatus.ERROR, message, t)); 344 } 345 } 346 | Popular Tags |