001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.support.processing;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import org.xml.sax.Attributes;
024: import org.xml.sax.SAXException;
025: import org.xml.sax.helpers.DefaultHandler;
026:
027: import spoon.AbstractLauncher;
028: import spoon.support.builder.CtResource;
029:
030: /**
031: * This class defines the SAX handler to parse a Spoonlet deployment descriptor
032: * file.
033: */
034: public class SpoonletXmlHandler extends DefaultHandler {
035:
036: private AbstractLauncher launcher;
037:
038: private List<CtResource> spoonletIndex;
039:
040: XmlProcessorProperties prop;
041:
042: String propName;
043:
044: List<Object> values;
045:
046: String buffer;
047:
048: /**
049: * Creates a new handler.
050: *
051: * @param launcher
052: * the launcher
053: * @param spoonletIndex ?
054: */
055: public SpoonletXmlHandler(AbstractLauncher launcher,
056: List<CtResource> spoonletIndex) {
057: super ();
058: this .launcher = launcher;
059: this .spoonletIndex = spoonletIndex;
060: }
061:
062: /**
063: * Handles XML element ends.
064: */
065: @Override
066: public void endElement(String uri, String localName, String qName)
067: throws SAXException {
068: if (localName.equals("processor")) {
069: launcher.getFactory().getEnvironment()
070: .setProcessorProperties(prop.getProcessorName(),
071: prop);
072: prop = null;
073: } else if (localName.equals("property")) {
074: if (values != null) {
075: prop.addProperty(propName, values);
076: }
077: values = null;
078: propName = null;
079: } else if (localName.equals("value")) {
080: values.add(buffer);
081: }
082: buffer = null;
083: super .endElement(uri, localName, qName);
084: }
085:
086: /**
087: * Handles characters.
088: */
089: @Override
090: public void characters(char[] ch, int start, int end)
091: throws SAXException {
092: buffer = new String(ch, start, end);
093: }
094:
095: /**
096: * Handles XML element starts.
097: */
098: @Override
099: public void startElement(String uri, String localName,
100: String qName, Attributes attributes) throws SAXException {
101: if (localName.equals("processor")) {
102: launcher.addProcessor(attributes.getValue("class"));
103: prop = new XmlProcessorProperties(launcher.getFactory(),
104: attributes.getValue("class"));
105: } else if (localName.equals("template")) {
106: if (attributes.getValue("path") != null) {
107: String foldername = attributes.getValue("path");
108: for (CtResource r : spoonletIndex) {
109: if (r.getName().startsWith(foldername)) {
110: launcher.addTemplateResource(r);
111: }
112: }
113: }
114: if (attributes.getValue("folder") != null) {
115: String foldername = attributes.getValue("folder");
116: for (CtResource r : spoonletIndex) {
117: if (r.getName().startsWith(foldername)) {
118: launcher.addTemplateResource(r);
119: }
120: }
121: }
122: if (attributes.getValue("file") != null) {
123: String filename = attributes.getValue("file");
124: for (CtResource r : spoonletIndex) {
125: if (r.getName().startsWith(filename)) {
126: launcher.addTemplateResource(r);
127: }
128: }
129: }
130:
131: } else if (localName.equals("property")) {
132: propName = attributes.getValue("name");
133: if (attributes.getValue("value") != null) {
134: prop
135: .addProperty(propName, attributes
136: .getValue("value"));
137: } else {
138: values = new ArrayList<Object>();
139: }
140: }
141: }
142: }
|