[Pixelle-commit] SF.net SVN: pixelle: [148] trunk/pixelle/src/com/mebigfatguy/pixelle/ AlgorithmArc
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2008-07-06 22:03:09
|
Revision: 148
http://pixelle.svn.sourceforge.net/pixelle/?rev=148&view=rev
Author: dbrosius
Date: 2008-07-06 15:02:11 -0700 (Sun, 06 Jul 2008)
Log Message:
-----------
parse system algorithms
Modified Paths:
--------------
trunk/pixelle/src/com/mebigfatguy/pixelle/AlgorithmArchiver.java
Modified: trunk/pixelle/src/com/mebigfatguy/pixelle/AlgorithmArchiver.java
===================================================================
--- trunk/pixelle/src/com/mebigfatguy/pixelle/AlgorithmArchiver.java 2008-07-06 21:29:32 UTC (rev 147)
+++ trunk/pixelle/src/com/mebigfatguy/pixelle/AlgorithmArchiver.java 2008-07-06 22:02:11 UTC (rev 148)
@@ -18,13 +18,37 @@
*/
package com.mebigfatguy.pixelle;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JPopupMenu;
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.DefaultHandler;
+import org.xml.sax.helpers.XMLReaderFactory;
+
+import com.mebigfatguy.pixelle.utils.Closer;
+
public class AlgorithmArchiver {
+ private static final String SYSTEM_ALGO_XML_PATH = "/com/mebigfatguy/pixelle/resources/algorithms.xml";
+ private static final String SYSTEM_ALGO_XSD_PATH = "/com/mebigfatguy/pixelle/resources/algorithms.xsd";
+ private static final String GROUP = "group";
+ private static final String ALGORITHM = "algorithm";
+ private static final String COMPONENT = "component";
+ private static final String NAME = "name";
+ private static final String RED = "red";
+ private static final String GREEN = "green";
+ private static final String BLUE = "blue";
+ private static final String TRANSPARENCY = "transparency";
+ private static final String SELECTION = "selection";
+
+
private static AlgorithmArchiver archiver = new AlgorithmArchiver();
private Map<String, Map<String, Map<PixelleComponent, String>>> systemAlgorithms;
@@ -33,7 +57,7 @@
private AlgorithmArchiver() {
systemAlgorithms = new HashMap<String, Map<String, Map<PixelleComponent, String>>>();
userAlgorithms = new HashMap<String, Map<String, Map<PixelleComponent, String>>>();
-
+ loadSystemAlgorithms();
}
public static AlgorithmArchiver getArchiver() {
@@ -51,4 +75,62 @@
public void removeAlgorithm(String group, String name) {
}
+
+ private void loadSystemAlgorithms() {
+ InputStream xmlIs = null;
+ try {
+ xmlIs = AlgorithmArchiver.class.getResourceAsStream(SYSTEM_ALGO_XML_PATH);
+ parseAlgorithms(xmlIs, systemAlgorithms);
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ Closer.close(xmlIs);
+ }
+ }
+
+ private void loadUserAlgorithms() {
+
+ }
+
+ private void parseAlgorithms(InputStream is, Map<String, Map<String, Map<PixelleComponent, String>>> algorithms) throws IOException, SAXException {
+ XMLReader r = XMLReaderFactory.createXMLReader();
+ r.setContentHandler(new DefaultHandler() {
+ Map<String, Map<PixelleComponent, String>> currentGroup = null;
+ Map<PixelleComponent, String> currentAlgorithm = null;
+ String currentComponentName = null;
+
+ @Override
+ public void startElement(String uri, String localName, String qName, Attributes atts) {
+ if (GROUP.equals(localName)) {
+ currentGroup = new HashMap<String, Map<PixelleComponent, String>>();
+ systemAlgorithms.put(atts.getValue(NAME), currentGroup);
+ } else if (ALGORITHM.equals(localName)) {
+ currentAlgorithm = new HashMap<PixelleComponent, String>();
+ currentGroup.put(atts.getValue(NAME), currentAlgorithm);
+ } else if (COMPONENT.equals(localName)) {
+ currentComponentName = atts.getValue(NAME);
+ }
+ }
+
+ @Override
+ public void characters(char[] c, int start, int offset) {
+ if (currentComponentName != null) {
+ PixelleComponent pc = PixelleComponent.valueOf(currentComponentName.toUpperCase());
+ currentAlgorithm.put(pc, new String(c, start, offset).trim());
+ }
+ }
+
+ @Override
+ public void endElement(String uri, String localName, String qName) {
+ if (COMPONENT.equals(localName)) {
+ currentComponentName = null;
+ } else if (ALGORITHM.equals(localName)) {
+ currentAlgorithm = null;
+ } else if (GROUP.equals(localName)) {
+ currentGroup = null;
+ }
+ }
+ });
+ r.parse(new InputSource(is));
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|