private static Project suiteProject(Project prj) { try { FileObject suiteProperties = prj.getProjectDirectory().getFileObject("nbproject/suite.properties"); if (suiteProperties == null) return null; //not a suite component EditableProperties p = new EditableProperties(false); try (InputStream is = suiteProperties.getInputStream()) { p.load(is); } String suiteDir = p.get("suite.dir").replace("${basedir}/", ""); FileObject suiteProjectLoc = suiteDir != null ? prj.getProjectDirectory().getFileObject(suiteDir) : null; return suiteProjectLoc != null ? ProjectManager.getDefault().findProject(suiteProjectLoc) : null; } catch (IOException ex) { Exceptions.printStackTrace(ex); return null; } }
public EditableProperties load() throws IOException { Statistics.StopWatch sw = Statistics.getStopWatch(Statistics.LOAD, true); try { EditableProperties retval = new EditableProperties(true); FileObject file = toPropertiesFile(false); if (file != null) { try { InputStream is = file.getInputStream(); try { retval.load(is); } finally { is.close(); } } catch (IllegalArgumentException x) { // #167745 Logger.getLogger(PropertiesStorage.class.getName()).log(Level.INFO, "While loading " + file, x); file.delete(); } } return retval; } finally { sw.stop(); } }
/** * Add **\/*.mirah to build.classes.excludes. */ protected final boolean addExcludes() { try { EditableProperties props = getEditableProperties(project, PROJECT_PROPERTIES_PATH); String exclude = props.getProperty(EXCLUDE_PROPERTY); //props.setProperty("mirahc.ant.classpath", "foobarfoo"); if (!exclude.contains(EXCLUSION_PATTERN)) { //System.out.println(EXCLUSION_PATTERN+" is not found"); props.setProperty(EXCLUDE_PROPERTY, exclude + "," + EXCLUSION_PATTERN); // NOI18N storeEditableProperties(project, PROJECT_PROPERTIES_PATH, props); } else { //System.out.println("Exclusion pattern "+EXCLUSION_PATTERN+" was found"); } return true; } catch (IOException ex) { Exceptions.printStackTrace(ex); } return false; }
/** * Inverse operation to {@link #addExcludes()}. * Remove **\/*.mirah from build.classes.excludes. */ protected final boolean removeExcludes() { try { EditableProperties props = getEditableProperties(project, PROJECT_PROPERTIES_PATH); String exclude = props.getProperty(EXCLUDE_PROPERTY); if (exclude.contains("," + EXCLUSION_PATTERN)) { exclude = exclude.replace("," + EXCLUSION_PATTERN, ""); props.setProperty(EXCLUDE_PROPERTY, exclude); storeEditableProperties(project, PROJECT_PROPERTIES_PATH, props); } return true; } catch (IOException ex) { Exceptions.printStackTrace(ex); } return false; }
private static void storeEditableProperties(final Project prj, final String propertiesPath, final EditableProperties ep) throws IOException { try { ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() { @Override public Void run() throws IOException { FileObject propertiesFo = prj.getProjectDirectory().getFileObject(propertiesPath); if (propertiesFo!=null) { OutputStream os = null; try { os = propertiesFo.getOutputStream(); ep.store(os); } finally { if (os != null) { os.close(); } } } return null; } }); } catch (MutexException ex) { } }
@Override public boolean isCurrent(){ try { EditableProperties props = getEditableProperties(project, PROJECT_PROPERTIES_PATH); int version = 0; if ( props.containsKey(VERSION_PROPERTY) ){ try { version = Integer.parseInt(props.getProperty(VERSION_PROPERTY)); } catch ( Throwable t){} } System.out.println("The current version is "+version); return isActive() && version >= PLUGIN_VERSION; } catch (IOException ex) { throw new RuntimeException(ex); } }
public void addModuleToSuiteProperties(String propertiesFilePath, String pluginBasePackage, String pluginName) throws IOException { FileObject propFileObject = FileUtil.toFileObject(new File(propertiesFilePath)); EditableProperties ep = loadProperties(propFileObject); String prop = ep.getProperty("modules"); if (prop.trim().isEmpty()) { prop = "${project." + pluginBasePackage + "}"; } else { prop += ":${project." + pluginBasePackage + "}"; } ep.setProperty("modules", prop); ep.setProperty("project." + pluginBasePackage, pluginName); storeProperties(propFileObject, ep); }
private void storeProperties(FileObject propsFO, EditableProperties props) throws IOException { FileLock lock = propsFO.lock(); try { OutputStream os = propsFO.getOutputStream(lock); try { props.store(os); } finally { os.close(); } } finally { lock.releaseLock(); } }
public void save(final EditableProperties properties) throws IOException { if (isModified) { Statistics.StopWatch sw = Statistics.getStopWatch(Statistics.FLUSH, true); try { isModified = false; if (!properties.isEmpty()) { OutputStream os = null; try { os = outputStream(); properties.store(os); } finally { if (os != null) { LOGGER.log(Level.FINE, "Closing output-stream for file {0} in {1}.", new Object[]{filePath, folderPath}); os.close(); } } } else { FileObject file = toPropertiesFile(); if (file != null) { file.delete(); } FileObject folder = toFolder(); while (folder != null && folder != preferencesRoot() && folder.getChildren().length == 0) { folder.delete(); folder = folder.getParent(); } } } finally { sw.stop(); } } }
private void putAllProperties(EditableProperties props, boolean clear) { synchronized (lock) { if (clear) { properties().clear(); } properties().putAll(props); } }
public void testBasic() throws IOException { //preconditions noFileRepresentationAssertion(); //load doesn't change file layout EditableProperties p = instance.load(); p.put("key", "value");//NOI18N noFileRepresentationAssertion(); //save doesn't change file layout if not marked modified instance.save(p); noFileRepresentationAssertion(); //marked modified but not saved instance.markModified(); noFileRepresentationAssertion(); if (!instance.isReadOnly()) { //saved after marked modified instance.save(p); fileRepresentationAssertion(); } else { try { //saved after marked modified instance.save(p); fail(); } catch (IOException ex) {} noFileRepresentationAssertion(); } }
/** Returns properties from relative path in zip or userdir. * @param relativePath relative path * @return properties from relative path in zip or userdir. * @throws IOException if cannot open stream */ private EditableProperties getProperties(String relativePath) throws IOException { EditableProperties properties = new EditableProperties(false); InputStream in = null; try { in = getInputStream(relativePath); properties.load(in); } finally { if (in != null) { in.close(); } } return properties; }
protected final boolean addMacrosClasspath(){ try { EditableProperties props = getEditableProperties(project, PROJECT_PROPERTIES_PATH); props.setProperty(MIRAH_MACROS_JARDIR_PROPERTY, "lib/mirah/macros"); // NOI18N storeEditableProperties(project, PROJECT_PROPERTIES_PATH, props); FileObject projectFO = project.getProjectDirectory(); FileUtil.createFolder(new File(projectFO.getPath(), "lib/mirah/macros")); return true; } catch (IOException ex) { Exceptions.printStackTrace(ex); } return false; }
/** * Disables compile on save for the project. * * @return true if CoS were disabled, false otherwise */ protected final boolean addDisableCompileOnSaveProperty() { try { EditableProperties props = getEditableProperties(project, PROJECT_PROPERTIES_PATH); props.put(DISABLE_COMPILE_ON_SAVE, "true"); storeEditableProperties(project, PROJECT_PROPERTIES_PATH, props); return true; } catch (IOException ex) { Exceptions.printStackTrace(ex); } return false; }
private final boolean addCN1LibProperty(){ try { EditableProperties props = getEditableProperties(project, CN1_LIBRARY_PROPERTIES_PATH); props.put("codename1.is_library", "true"); storeEditableProperties(project, CN1_LIBRARY_PROPERTIES_PATH, props); return true; } catch (IOException ex) { Exceptions.printStackTrace(ex); } return false; }
/** * Inverse operation to {@link #addDisableCompileOnSaveProperty()}. * Enabled compile on save for the project. * * @return true if CoS were enabled, false otherwise */ protected final boolean removeDisableCompileOnSaveProperty() { try { EditableProperties props = getEditableProperties(project, PROJECT_PROPERTIES_PATH); props.remove(DISABLE_COMPILE_ON_SAVE); storeEditableProperties(project, PROJECT_PROPERTIES_PATH, props); return true; } catch (IOException ex) { Exceptions.printStackTrace(ex); } return false; }
@Override public boolean update(){ try { EditableProperties props = getEditableProperties(project, PROJECT_PROPERTIES_PATH); int version = 0; if ( props.containsKey(VERSION_PROPERTY) ){ try { version = Integer.parseInt(props.getProperty(VERSION_PROPERTY)); } catch ( Throwable t){} } if ( !removeExcludes() || !removeBuildScript() || !addExcludes() || !addBuildScript()){ System.out.println("Failed to update"); return false; } if ( version < PLUGIN_VERSION){ props = getEditableProperties(project, PROJECT_PROPERTIES_PATH); props.setProperty(VERSION_PROPERTY, String.valueOf(PLUGIN_VERSION)); storeEditableProperties(project, PROJECT_PROPERTIES_PATH, props); } return true; } catch (IOException ex) { Exceptions.printStackTrace(ex); return false; } }
public boolean basePackageAlreadyExistsInModuleSuite(FileObject fo, String basePackage) { try { EditableProperties ep = loadProperties(fo); return (!(ep.getProperty("project." + basePackage) == null)); } catch (IOException ex) { Exceptions.printStackTrace(ex); return false; } }
public void setLicenseInProjectProperties(String projPropPath, String licenseLoc) throws IOException { File projPropFile = new File(projPropPath); FileObject fo = FileUtil.toFileObject(projPropFile); String licensePath = "src/" + licenseLoc.replace("\\", "/"); EditableProperties ep = loadProperties(fo); ep.setProperty("license.file", licensePath); storeProperties(fo, ep); }
public void createBundlePropertiesFile(FileObject fo, String pluginName) throws IOException { EditableProperties ep = loadProperties(fo); ep.setProperty("OpenIDE-Module-Name", pluginName); ep.setProperty("OpenIDE-Module-Short-Description", "A short description..."); ep.setProperty("OpenIDE-Module-Long-Description", "A long description..."); ep.setProperty("OpenIDE-Module-Display-Category", "jMonkeyEngine"); storeProperties(fo, ep); }
private EditableProperties loadProperties(FileObject propsFO) throws IOException { InputStream propsIS = propsFO.getInputStream(); EditableProperties props = new EditableProperties(true); try { props.load(propsIS); } finally { propsIS.close(); } return props; }
PropertyHandler(Project project, String propertyFilePath) { this.project = project; this.propsFO = project.getProjectDirectory() .getFileObject(propertyFilePath); if (this.propsFO != null) { this.props = new EditableProperties(false); } }
@Override protected void performRewrite(JavaFix.TransformationContext ctx) throws Exception { WorkingCopy wc = ctx.getWorkingCopy(); TreePath treePath = ctx.getPath(); TreeMaker make = wc.getTreeMaker(); if (treePath.getLeaf().getKind() == Kind.METHOD_INVOCATION) { MethodInvocationTree mit = (MethodInvocationTree) treePath.getLeaf(); CompilationUnitTree cut = wc.getCompilationUnit(); boolean imported = false; String importBundleStar = cut.getPackageName() + ".Bundle.*"; for (ImportTree it : cut.getImports()) { if (it.isStatic() && it.getQualifiedIdentifier().toString().equals(importBundleStar)) { imported = true; break; } } if (!imported) { wc.rewrite(cut, make.addCompUnitImport(cut, make.Import(make.Identifier(importBundleStar), true))); } List<? extends ExpressionTree> args = mit.getArguments(); List<? extends ExpressionTree> params; if (args.size() == 3 && args.get(2).getKind() == Kind.NEW_ARRAY) { params = ((NewArrayTree) args.get(2)).getInitializers(); } else { params = args.subList(2, args.size()); } wc.rewrite(mit, make.MethodInvocation(Collections.<ExpressionTree>emptyList(), make.Identifier(toIdentifier(key)), params)); } // else annotation value, nothing to change if (!isAlreadyRegistered) { EditableProperties ep = new EditableProperties(true); InputStream is = ctx.getResourceContent(bundleProperties); try { ep.load(is); } finally { is.close(); } List<ExpressionTree> lines = new ArrayList<ExpressionTree>(); for (String comment : ep.getComment(key)) { lines.add(make.Literal(comment)); } lines.add(make.Literal(key + '=' + ep.remove(key))); TypeElement nbBundleMessages = wc.getElements().getTypeElement("org.openide.util.NbBundle.Messages"); if (nbBundleMessages == null) { throw new IllegalArgumentException("cannot resolve org.openide.util.NbBundle.Messages"); } GeneratorUtilities gu = GeneratorUtilities.get(wc); Tree enclosing = findEnclosingElement(wc, treePath); Tree modifiers; Tree nueModifiers; ExpressionTree[] linesA = lines.toArray(new ExpressionTree[lines.size()]); switch (enclosing.getKind()) { case METHOD: modifiers = wc.resolveRewriteTarget(((MethodTree) enclosing).getModifiers()); nueModifiers = gu.appendToAnnotationValue((ModifiersTree) modifiers, nbBundleMessages, "value", linesA); break; case VARIABLE: modifiers = wc.resolveRewriteTarget(((VariableTree) enclosing).getModifiers()); nueModifiers = gu.appendToAnnotationValue((ModifiersTree) modifiers, nbBundleMessages, "value", linesA); break; case COMPILATION_UNIT: modifiers = wc.resolveRewriteTarget(enclosing); nueModifiers = gu.appendToAnnotationValue((CompilationUnitTree) modifiers, nbBundleMessages, "value", linesA); break; default: modifiers = wc.resolveRewriteTarget(((ClassTree) enclosing).getModifiers()); nueModifiers = gu.appendToAnnotationValue((ModifiersTree) modifiers, nbBundleMessages, "value", linesA); } wc.rewrite(modifiers, nueModifiers); // XXX remove NbBundle import if now unused OutputStream os = ctx.getResourceOutput(bundleProperties); try { ep.store(os); } finally { os.close(); } } // XXX after JavaFix rewrite, Savable.save (on DataObject.find(src)) no longer works (JG13 again) }
public Store() { lock = new Object(); properties = new EditableProperties(true); }
EditableProperties load() throws IOException;
void save(final EditableProperties properties) throws IOException;