private void loadCustomTemplates() throws IOException { String pref= fPreferenceStore.get(fKey, null); if (pref != null && pref.trim().length() > 0) { Reader input= new StringReader(pref); TemplateReaderWriter reader= new TemplateReaderWriter(); TemplatePersistenceData[] datas= reader.read(input); for (TemplatePersistenceData data : datas) { add(data); } } }
public void save(TemplatePersistenceData[] templates, Writer writer) throws IOException { super.save(templates, writer); }
private String getStringValue(NamedNodeMap attributes, String name) throws SAXException { String val= getStringValue(attributes, name, null); if (val == null) throw new SAXException(TextTemplateMessages.getString("TemplateReaderWriter.error.missing_attribute")); //$NON-NLS-1$ return val; }
/** * Saves the templates to the preferences. * * @throws IOException if the templates cannot be written */ public void save() throws IOException { ArrayList<TemplatePersistenceData> custom= new ArrayList<>(); for (TemplatePersistenceData data : fTemplates) { if (data.isCustom() && !(data.isUserAdded() && data.isDeleted())) // don't save deleted user-added templates custom.add(data); } StringWriter output= new StringWriter(); TemplateReaderWriter writer= new TemplateReaderWriter(); writer.save(custom.toArray(new TemplatePersistenceData[custom.size()]), output); fIgnorePreferenceStoreChanges= true; try { fPreferenceStore.put(fKey, output.toString()); fPreferenceStore.flush(); } catch (BackingStoreException e) { } finally { fIgnorePreferenceStoreChanges= false; } }
/** * Reads templates from a reader and returns them. The reader must present * a serialized form as produced by the <code>save</code> method. * * @param reader the reader to read templates from * @return the read templates, encapsulated in instances of <code>TemplatePersistenceData</code> * @throws IOException if reading from the stream fails */ public TemplatePersistenceData[] read(Reader reader) throws IOException { return read(reader, null); }
if (isProjectSpecific(templateData[i].getId())) { StringWriter output= new StringWriter(); TemplateReaderWriter writer= new TemplateReaderWriter(); writer.save(getTemplateData(false), output);
continue; String id= getStringValue(attributes, ID_ATTRIBUTE, null); if (id != null && ids.contains(id)) { String PLUGIN_ID= "org.eclipse.jface.text"; //$NON-NLS-1$ continue; boolean deleted = getBooleanValue(attributes, DELETED_ATTRIBUTE, false); String name= getStringValue(attributes, NAME_ATTRIBUTE); name= translateString(name, bundle); String description= getStringValue(attributes, DESCRIPTION_ATTRIBUTE, ""); //$NON-NLS-1$ description= translateString(description, bundle); String context= getStringValue(attributes, CONTEXT_ATTRIBUTE); boolean enabled = getBooleanValue(attributes, ENABLED_ATTRIBUTE, true); boolean autoInsertable= getBooleanValue(attributes, AUTO_INSERTABLE_ATTRIBUTE, true); pattern= translateString(pattern, bundle);
private String translateString(String str, ResourceBundle bundle) { if (bundle == null) return str; int idx= str.indexOf('%'); if (idx == -1) { return str; } StringBuilder buf= new StringBuilder(); int k= 0; while (idx != -1) { buf.append(str.substring(k, idx)); for (k= idx + 1; k < str.length() && !Character.isWhitespace(str.charAt(k)); k++) { // loop } String key= str.substring(idx + 1, k); buf.append(getBundleString(key, bundle)); idx= str.indexOf('%', k); } buf.append(str.substring(k)); return buf.toString(); }
/** * Saves the templates to the preferences. * * @throws IOException if the templates cannot be written */ @Override public void save() throws IOException { ArrayList<TemplatePersistenceData> custom= new ArrayList<>(); for (TemplatePersistenceData data : internalGetTemplates()) { if (data.isCustom() && !(data.isUserAdded() && data.isDeleted())) // don't save deleted user-added templates custom.add(data); } StringWriter output= new StringWriter(); TemplateReaderWriter writer= new TemplateReaderWriter(); writer.save(custom.toArray(new TemplatePersistenceData[custom.size()]), output); fIgnorePreferenceStoreChanges= true; try { fPreferenceStore.setValue(getKey(), output.toString()); if (fPreferenceStore instanceof IPersistentPreferenceStore) ((IPersistentPreferenceStore)fPreferenceStore).save(); } finally { fIgnorePreferenceStoreChanges= false; } }
/** * Reads templates from a stream and adds them to the templates. * * @param reader the reader to read templates from * @param bundle a resource bundle to use for translating the read templates, or <code>null</code> if no translation should occur * @return the read templates, encapsulated in instances of <code>TemplatePersistenceData</code> * @throws IOException if reading from the stream fails */ public TemplatePersistenceData[] read(Reader reader, ResourceBundle bundle) throws IOException { return read(new InputSource(reader), bundle, null); }
private void export(TemplatePersistenceData[] templates) { FileDialog dialog= new FileDialog(getShell(), SWT.SAVE); dialog.setText(TemplatesMessages.TemplatePreferencePage_export_title); dialog.setFilterExtensions(new String[] {TemplatesMessages.TemplatePreferencePage_export_extension}); dialog.setFileName(TemplatesMessages.TemplatePreferencePage_export_filename); String path= dialog.open(); if (path == null) return; File file= new File(path); if (file.isHidden()) { String title= TemplatesMessages.TemplatePreferencePage_export_error_title; String message= NLSUtility.format(TemplatesMessages.TemplatePreferencePage_export_error_hidden, file.getAbsolutePath()); MessageDialog.openError(getShell(), title, message); return; } if (file.exists() && !file.canWrite()) { String title= TemplatesMessages.TemplatePreferencePage_export_error_title; String message= NLSUtility.format(TemplatesMessages.TemplatePreferencePage_export_error_canNotWrite, file.getAbsolutePath()); MessageDialog.openError(getShell(), title, message); return; } if (!file.exists() || confirmOverwrite(file)) { try (OutputStream output = new BufferedOutputStream(new FileOutputStream(file))) { TemplateReaderWriter writer= new TemplateReaderWriter(); writer.save(templates, output); } catch (IOException e) { openWriteErrorDialog(e); } } }
private void loadCustomTemplates() throws IOException { String pref= fPreferenceStore.getString(getKey()); if (pref != null && pref.trim().length() > 0) { Reader input= new StringReader(pref); TemplateReaderWriter reader= new TemplateReaderWriter(); TemplatePersistenceData[] datas= reader.read(input); for (TemplatePersistenceData data : datas) { add(data); } } }
/** * Reads templates from a stream and adds them to the templates. * * @param stream the byte stream to read templates from * @param bundle a resource bundle to use for translating the read templates, or <code>null</code> if no translation should occur * @return the read templates, encapsulated in instances of <code>TemplatePersistenceData</code> * @throws IOException if reading from the stream fails */ public TemplatePersistenceData[] read(InputStream stream, ResourceBundle bundle) throws IOException { return read(new InputSource(stream), bundle, null); }
public void save(TemplatePersistenceData[] templates, OutputStream stream) throws IOException { super.save(templates, stream); }
@Override public void save() throws IOException { TemplatePersistenceData[] templateData= ProjectTemplateStore.this.getTemplateData(); for (int i= 0; i < templateData.length; i++) { if (isProjectSpecific(templateData[i].getId())) { StringWriter output= new StringWriter(); TemplateReaderWriter writer= new TemplateReaderWriter(); writer.save(getTemplateData(false), output); projectSettings.put(KEY, output.toString()); try { projectSettings.flush(); } catch (BackingStoreException e) { } return; } } // See IPreferenceStore for default String value projectSettings.put(KEY, ""); //$NON-NLS-1$ try { projectSettings.flush(); } catch (BackingStoreException e) { } } };
public static boolean hasProjectSpecificTempates(IProject project) { String pref= new ProjectScope(project).getNode(JavaManipulation.getPreferenceNodeId()).get(KEY, null); if (pref != null && pref.trim().length() > 0) { Reader input= new StringReader(pref); TemplateReaderWriter reader= new TemplateReaderWriter(); TemplatePersistenceData[] datas; try { datas= reader.read(input); return datas.length > 0; } catch (IOException e) { // ignore } } return false; }
/** * Reads the template with identifier <code>id</code> from a reader and * returns it. The reader must present a serialized form as produced by the * <code>save</code> method. * * @param reader the reader to read templates from * @param id the id of the template to return * @return the read template, encapsulated in an instances of * <code>TemplatePersistenceData</code> * @throws IOException if reading from the stream fails * @since 3.1 */ public TemplatePersistenceData readSingle(Reader reader, String id) throws IOException { TemplatePersistenceData[] datas= read(new InputSource(reader), null, id); if (datas.length > 0) return datas[0]; return null; }
/** * Saves the templates as XML. * * @param templates the templates to save * @param writer the writer to write the templates to in XML * @throws IOException if writing the templates fails */ public void save(TemplatePersistenceData[] templates, Writer writer) throws IOException { save(templates, new StreamResult(writer)); }
try { output= new BufferedOutputStream(new FileOutputStream(file)); TemplateReaderWriter writer= new TemplateReaderWriter(); writer.save(templates, output); output.close(); } catch (IOException e) {
TemplateReaderWriter reader= new TemplateReaderWriter(); File file= new File(path); if (file.exists()) { InputStream input= new BufferedInputStream(new FileInputStream(file)); try { TemplatePersistenceData[] datas= reader.read(input, null); for (int i= 0; i < datas.length; i++) { updateTemplate(datas[i]);