use of processing.data.StringDict in project processing by processing.
the class Util method readSettings.
/**
* Parse a String array that contains attribute/value pairs separated
* by = (the equals sign). The # (hash) symbol is used to denote comments.
* Comments can be anywhere on a line. Blank lines are ignored.
* In 3.0a6, no longer taking a blank HashMap as param; no cases in the main
* PDE code of adding to a (Hash)Map. Also returning the Map instead of void.
* Both changes modify the method signature, but this was only used by the
* contrib classes.
*/
public static StringDict readSettings(String filename, String[] lines) {
StringDict settings = new StringDict();
for (String line : lines) {
// Remove comments
int commentMarker = line.indexOf('#');
if (commentMarker != -1) {
line = line.substring(0, commentMarker);
}
// Remove extra whitespace
line = line.trim();
if (line.length() != 0) {
int equals = line.indexOf('=');
if (equals == -1) {
if (filename != null) {
System.err.println("Ignoring illegal line in " + filename);
System.err.println(" " + line);
}
} else {
String attr = line.substring(0, equals).trim();
String valu = line.substring(equals + 1).trim();
settings.set(attr, valu);
}
}
}
return settings;
}
use of processing.data.StringDict in project processing by processing.
the class ContributionManager method updateFlagged.
/**
* Updates all the flagged modes/tools.
*/
private static void updateFlagged(Base base, File root) throws Exception {
File[] markedForUpdate = root.listFiles(new FileFilter() {
public boolean accept(File folder) {
return (folder.isDirectory() && LocalContribution.isUpdateFlagged(folder));
}
});
ArrayList<String> updateContribsNames = new ArrayList<String>();
LinkedList<AvailableContribution> updateContribsList = new LinkedList<AvailableContribution>();
String type = root.getName().substring(root.getName().lastIndexOf('/') + 1);
String propFileName = null;
if (type.equalsIgnoreCase("tools"))
propFileName = "tool.properties";
else if (type.equalsIgnoreCase("modes"))
propFileName = "mode.properties";
else if (//putting this here, just in case
type.equalsIgnoreCase("libraries"))
propFileName = "libraries.properties";
for (File folder : markedForUpdate) {
StringDict props = Util.readSettings(new File(folder, propFileName));
updateContribsNames.add(props.get("name"));
Util.removeDir(folder);
}
Iterator<AvailableContribution> iter = listing.advertisedContributions.iterator();
while (iter.hasNext()) {
AvailableContribution availableContribs = iter.next();
if (updateContribsNames.contains(availableContribs.getName())) {
updateContribsList.add(availableContribs);
}
}
Iterator<AvailableContribution> iter2 = updateContribsList.iterator();
while (iter2.hasNext()) {
AvailableContribution contribToUpdate = iter2.next();
installOnStartUp(base, contribToUpdate);
listing.replaceContribution(contribToUpdate, contribToUpdate);
}
}
use of processing.data.StringDict in project processing by processing.
the class ExamplesFrame method buildContribTree.
protected DefaultMutableTreeNode buildContribTree() {
DefaultMutableTreeNode contribExamplesNode = new DefaultMutableTreeNode(Language.text("examples.contributed"));
try {
File[] subfolders = ContributionType.EXAMPLES.listCandidates(examplesContribFolder);
if (subfolders != null) {
for (File sub : subfolders) {
StringDict props = Contribution.loadProperties(sub, ContributionType.EXAMPLES);
if (props != null) {
if (ExamplesContribution.isCompatible(base, props)) {
DefaultMutableTreeNode subNode = new DefaultMutableTreeNode(props.get("name"));
if (base.addSketches(subNode, sub, true)) {
contribExamplesNode.add(subNode);
// TODO there has to be a simpler way of handling this along
// with addSketches() as well [fry 150811]
int exampleNodeNumber = -1;
// The contrib may have other items besides the examples folder
for (int i = 0; i < subNode.getChildCount(); i++) {
if (subNode.getChildAt(i).toString().equals("examples")) {
exampleNodeNumber = i;
}
}
if (exampleNodeNumber != -1) {
TreeNode exampleNode = subNode.getChildAt(exampleNodeNumber);
subNode.remove(exampleNodeNumber);
int count = exampleNode.getChildCount();
for (int j = 0; j < count; j++) {
subNode.add((DefaultMutableTreeNode) exampleNode.getChildAt(0));
}
}
// if (subNode.getChildCount() != 1) {
// System.err.println("more children than expected when one is enough");
// }
// TreeNode exampleNode = subNode.getChildAt(0);
// subNode.add((DefaultMutableTreeNode) exampleNode.getChildAt(0));
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return contribExamplesNode;
}
use of processing.data.StringDict in project processing by processing.
the class AvailableContribution method writePropertiesFile.
/**
* We overwrite those fields that aren't proper in the properties file with
* the curated version from the Processing site. This ensures that things have
* been cleaned up (for instance, that the "sentence" is really a sentence)
* and that bad data from the contrib's .properties file doesn't break the
* manager. However, it also ensures that valid fields in the properties file
* aren't overwritten, since the properties file may be more recent than the
* contributions.txt file.
*/
public boolean writePropertiesFile(File propFile) {
try {
StringDict properties = Util.readSettings(propFile);
String name = properties.get("name");
if (name == null || name.isEmpty()) {
name = getName();
}
String category;
StringList categoryList = parseCategories(properties);
if (categoryList.size() == 1 && categoryList.get(0).equals(UNKNOWN_CATEGORY)) {
category = getCategoryStr();
} else {
category = categoryList.join(",");
}
StringList importsList = parseImports(properties);
String authors = properties.get(AUTHORS_PROPERTY);
// }
if (authors == null || authors.isEmpty()) {
authors = getAuthorList();
}
String url = properties.get("url");
if (url == null || url.isEmpty()) {
url = getUrl();
}
String sentence = properties.get("sentence");
if (sentence == null || sentence.isEmpty()) {
sentence = getSentence();
}
String paragraph = properties.get("paragraph");
if (paragraph == null || paragraph.isEmpty()) {
paragraph = getParagraph();
}
int version;
try {
version = Integer.parseInt(properties.get("version"));
} catch (NumberFormatException e) {
version = getVersion();
System.err.println("The version number for “" + name + "” is not a number.");
System.err.println("Please contact the author to fix it according to the guidelines.");
}
String prettyVersion = properties.get("prettyVersion");
if (prettyVersion != null && prettyVersion.isEmpty()) {
prettyVersion = null;
}
String compatibleContribsList = null;
if (getType() == ContributionType.EXAMPLES) {
compatibleContribsList = properties.get(MODES_PROPERTY);
}
long lastUpdated;
try {
lastUpdated = Long.parseLong(properties.get("lastUpdated"));
} catch (NumberFormatException nfe) {
lastUpdated = getLastUpdated();
// Better comment these out till all contribs have a lastUpdated
// System.err.println("The last updated date for the “" + name
// + "” contribution is not set properly.");
// System.err
// .println("Please contact the author to fix it according to the guidelines.");
}
int minRev;
try {
minRev = Integer.parseInt(properties.get("minRevision"));
} catch (NumberFormatException e) {
minRev = getMinRevision();
// System.err.println("The minimum compatible revision for the “" + name
// + "” contribution is not set properly. Assuming minimum revision 0.");
}
int maxRev;
try {
maxRev = Integer.parseInt(properties.get("maxRevision"));
} catch (NumberFormatException e) {
maxRev = getMaxRevision();
// System.err.println("The maximum compatible revision for the “" + name
// + "” contribution is not set properly. Assuming maximum revision INF.");
}
if (propFile.delete() && propFile.createNewFile() && propFile.setWritable(true)) {
PrintWriter writer = PApplet.createWriter(propFile);
writer.println("name=" + name);
writer.println("category=" + category);
writer.println(AUTHORS_PROPERTY + "=" + authors);
writer.println("url=" + url);
writer.println("sentence=" + sentence);
writer.println("paragraph=" + paragraph);
writer.println("version=" + version);
if (prettyVersion != null) {
writer.println("prettyVersion=" + prettyVersion);
}
writer.println("lastUpdated=" + lastUpdated);
writer.println("minRevision=" + minRev);
writer.println("maxRevision=" + maxRev);
if ((getType() == ContributionType.LIBRARY || getType() == ContributionType.MODE) && importsList != null) {
writer.println("imports=" + importsList.join(","));
}
if (getType() == ContributionType.EXAMPLES) {
if (compatibleContribsList != null) {
writer.println(MODES_PROPERTY + "=" + compatibleContribsList);
}
}
writer.flush();
writer.close();
}
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
use of processing.data.StringDict in project processing by processing.
the class ContributionListing method parseContribList.
private List<AvailableContribution> parseContribList(File file) {
List<AvailableContribution> outgoing = new ArrayList<AvailableContribution>();
if (file != null && file.exists()) {
String[] lines = PApplet.loadStrings(file);
int start = 0;
while (start < lines.length) {
String type = lines[start];
ContributionType contribType = ContributionType.fromName(type);
if (contribType == null) {
System.err.println("Error in contribution listing file on line " + (start + 1));
// Scan forward for the next blank line
int end = ++start;
while (end < lines.length && !lines[end].trim().isEmpty()) {
end++;
}
start = end + 1;
} else {
// Scan forward for the next blank line
int end = ++start;
while (end < lines.length && !lines[end].trim().isEmpty()) {
end++;
}
String[] contribLines = PApplet.subset(lines, start, end - start);
StringDict contribParams = Util.readSettings(file.getName(), contribLines);
outgoing.add(new AvailableContribution(contribType, contribParams));
start = end + 1;
}
}
}
return outgoing;
}
Aggregations