Index: editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/intro/SampleDescriptionHelper.java |
diff --git a/editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/intro/SampleDescriptionHelper.java b/editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/intro/SampleDescriptionHelper.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4f6cde6184ca2dd66595f28245210babc5f07a6c |
--- /dev/null |
+++ b/editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/intro/SampleDescriptionHelper.java |
@@ -0,0 +1,159 @@ |
+/* |
+ * Copyright 2012 Google Inc. |
+ * |
+ * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except |
+ * in compliance with the License. You may obtain a copy of the License at |
+ * |
+ * http://www.eclipse.org/legal/epl-v10.html |
+ * |
+ * Unless required by applicable law or agreed to in writing, software distributed under the License |
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
+ * or implied. See the License for the specific language governing permissions and limitations under |
+ * the License. |
+ */ |
+package com.google.dart.tools.ui.internal.intro; |
+ |
+import com.google.common.collect.Lists; |
+import com.google.dart.tools.core.DartCore; |
+ |
+import org.xml.sax.Attributes; |
+import org.xml.sax.SAXException; |
+import org.xml.sax.helpers.DefaultHandler; |
+ |
+import java.io.File; |
+import java.io.FileInputStream; |
+import java.util.Collections; |
+import java.util.List; |
+ |
+import javax.xml.parsers.SAXParser; |
+import javax.xml.parsers.SAXParserFactory; |
+ |
+/** |
+ * Helper for providing {@link SampleDescription}s. |
+ */ |
+public final class SampleDescriptionHelper { |
+ /** |
+ * @return all {@link SampleDescription} from the "samples" directory. |
+ */ |
+ public static List<SampleDescription> getDescriptions() throws Exception { |
+ List<SampleDescription> descriptions = Lists.newArrayList(); |
+ File samplesDirectory = getSamplesDirectory(); |
+ scanFolder(descriptions, samplesDirectory); |
+ Collections.sort(descriptions); |
+ return descriptions; |
+ } |
+ |
+ /** |
+ * Attempts to add {@link SampleDescription} for given directory. |
+ * |
+ * @return <code>true</code> if {@link SampleDescription} was added. |
+ */ |
+ private static boolean addDescription(final List<SampleDescription> descriptions, |
+ final File directory) { |
+ // attempt to find logo |
+ final File logoFile = new File(directory, "logo.png"); |
+ if (!logoFile.exists() || !logoFile.isFile()) { |
+ return false; |
+ } |
+ // attempt to find description |
+ final boolean descriptionAdded[] = {false}; |
+ File descriptionFile = new File(directory, "sample.xml"); |
+ if (descriptionFile.exists() && descriptionFile.isFile()) { |
+ try { |
+ SAXParserFactory factory = SAXParserFactory.newInstance(); |
+ SAXParser saxParser = factory.newSAXParser(); |
+ DefaultHandler handler = new DefaultHandler() { |
+ private StringBuilder sb = new StringBuilder(); |
+ private String name; |
+ private String filePath; |
+ private String descriptionText; |
+ private int order; |
+ |
+ @Override |
+ public void characters(char[] ch, int start, int length) throws SAXException { |
+ sb.append(ch, start, length); |
+ } |
+ |
+ @Override |
+ public void endElement(String uri, String localName, String qName) throws SAXException { |
+ if (qName.equals("description")) { |
+ descriptionText = sb.toString(); |
+ } |
+ if (qName.equals("sample") && filePath != null && name != null |
+ && descriptionText != null) { |
+ descriptions.add(new SampleDescription(directory, filePath, name, descriptionText, |
+ logoFile, order)); |
+ descriptionAdded[0] = true; |
+ } |
+ } |
+ |
+ @Override |
+ public void startElement(String uri, String localName, String qName, Attributes attributes) |
+ throws SAXException { |
+ sb.setLength(0); |
+ if (qName.equals("sample")) { |
+ name = attributes.getValue("name"); |
+ filePath = attributes.getValue("file"); |
+ { |
+ order = Integer.MIN_VALUE; |
+ String orderString = attributes.getValue("order"); |
+ if (orderString != null) { |
+ try { |
+ order = Integer.parseInt(orderString); |
+ } catch (NumberFormatException e) { |
+ } |
+ } |
+ } |
+ } |
+ } |
+ }; |
+ FileInputStream is = new FileInputStream(descriptionFile); |
+ try { |
+ saxParser.parse(is, handler); |
+ } finally { |
+ is.close(); |
+ } |
+ } catch (Throwable e) { |
+ DartCore.logError(e); |
+ } |
+ } |
+ // may be added |
+ return descriptionAdded[0]; |
+ } |
+ |
+ /** |
+ * @return the {@link File} of the "samples" directory. |
+ */ |
+ private static File getSamplesDirectory() throws Exception { |
+ // try to get "development" path |
+ { |
+ String devSamplesPath = System.getProperty("dartEditor.development.samplesPath"); |
danrubel
2012/02/07 06:00:46
File installDir = new File(Platform.getInstallLoca
scheglov
2012/02/08 16:07:22
OK for "user" branch.
But for development branch
|
+ if (devSamplesPath != null) { |
+ return new File(devSamplesPath); |
+ } |
+ } |
+ // OK, get use path |
+ File eclipsePath = new File(".").getCanonicalFile(); |
+ return new File(eclipsePath, "samples"); |
+ } |
+ |
+ /** |
+ * Attempts to find sample description in the given directory, otherwise scans recursively. |
+ */ |
+ private static void scanFolder(List<SampleDescription> descriptions, File directory) |
+ throws Exception { |
+ // ignore not directories |
+ if (!directory.exists() || !directory.isDirectory()) { |
+ return; |
+ } |
+ // attempt to add description |
+ boolean added = addDescription(descriptions, directory); |
+ if (added) { |
+ return; |
+ } |
+ // scan children |
+ for (File child : directory.listFiles()) { |
+ scanFolder(descriptions, child); |
+ } |
+ } |
+} |