From: zapata Date: Mon, 10 Feb 2003 16:50:17 +0000 (+0000) Subject: split the XML parser from the producer-config reader and improved XML parser error... X-Git-Tag: BEFORE_MERGE_1_1~265 X-Git-Url: http://erislabs.net/gitweb/?a=commitdiff_plain;h=0af4c00cb9990c3d6f365413f00654b2851f6071;p=mir.git split the XML parser from the producer-config reader and improved XML parser error reporting --- diff --git a/source/mir/producer/reader/DefaultProducerNodeBuilders.java b/source/mir/producer/reader/DefaultProducerNodeBuilders.java index 320d0dd2..a551e28e 100755 --- a/source/mir/producer/reader/DefaultProducerNodeBuilders.java +++ b/source/mir/producer/reader/DefaultProducerNodeBuilders.java @@ -57,6 +57,7 @@ import mir.producer.LoopProducerNode; import mir.producer.ProducerNode; import mir.producer.ResourceBundleProducerNode; import mir.producer.ScriptCallingProducerNode; +import mir.util.*; public class DefaultProducerNodeBuilders { @@ -137,8 +138,8 @@ public class DefaultProducerNodeBuilders { super(ASSIGNMENT_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, ASSIGNMENT_REQUIRED_ATTRIBUTES, ASSIGNMENT_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, ASSIGNMENT_REQUIRED_ATTRIBUTES, ASSIGNMENT_OPTIONAL_ATTRIBUTES); key = (String) anAttributes.get(ASSIGNMENT_KEY_ATTRIBUTE); value = (String) anAttributes.get(ASSIGNMENT_VALUE_ATTRIBUTE); @@ -160,8 +161,8 @@ public class DefaultProducerNodeBuilders { super(ASSIGNMENT_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, ASSIGNMENT_REQUIRED_ATTRIBUTES, ASSIGNMENT_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, ASSIGNMENT_REQUIRED_ATTRIBUTES, ASSIGNMENT_OPTIONAL_ATTRIBUTES); key = (String) anAttributes.get(ASSIGNMENT_KEY_ATTRIBUTE); value = (String) anAttributes.get(ASSIGNMENT_VALUE_ATTRIBUTE); @@ -200,13 +201,13 @@ public class DefaultProducerNodeBuilders { model = aModel; } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, ENUMERATION_REQUIRED_ATTRIBUTES, ENUMERATION_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, ENUMERATION_REQUIRED_ATTRIBUTES, ENUMERATION_OPTIONAL_ATTRIBUTES); key = (String) anAttributes.get(ENUMERATION_KEY_ATTRIBUTE); definition = (String) anAttributes.get(ENUMERATION_DEFINITION_ATTRIBUTE); - selection = (String) ReaderTool.getStringAttributeWithDefault(anAttributes, ENUMERATION_SELECTION_ATTRIBUTE, ""); - order = (String) ReaderTool.getStringAttributeWithDefault(anAttributes, ENUMERATION_ORDER_ATTRIBUTE, ""); + selection = (String) XMLReaderTool.getStringAttributeWithDefault(anAttributes, ENUMERATION_SELECTION_ATTRIBUTE, ""); + order = (String) XMLReaderTool.getStringAttributeWithDefault(anAttributes, ENUMERATION_ORDER_ATTRIBUTE, ""); limit = (String) anAttributes.get(ENUMERATION_LIMIT_ATTRIBUTE); skip = (String) anAttributes.get(ENUMERATION_SKIP_ATTRIBUTE); }; @@ -245,11 +246,11 @@ public class DefaultProducerNodeBuilders { super(LOOP_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, LOOP_REQUIRED_ATTRIBUTES, LOOP_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, LOOP_REQUIRED_ATTRIBUTES, LOOP_OPTIONAL_ATTRIBUTES); condition = (String) anAttributes.get(LOOP_CONDITION_ATTRIBUTE); - limit = (String) ReaderTool.getStringAttributeWithDefault(anAttributes, LOOP_LIMIT_ATTRIBUTE, ""); + limit = (String) XMLReaderTool.getStringAttributeWithDefault(anAttributes, LOOP_LIMIT_ATTRIBUTE, ""); }; public ProducerNode constructNode() { @@ -285,13 +286,13 @@ public class DefaultProducerNodeBuilders { model = aModel; } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, LIST_REQUIRED_ATTRIBUTES, LIST_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, LIST_REQUIRED_ATTRIBUTES, LIST_OPTIONAL_ATTRIBUTES); key = (String) anAttributes.get(LIST_KEY_ATTRIBUTE); definition = (String) anAttributes.get(LIST_DEFINITION_ATTRIBUTE); - selection = (String) ReaderTool.getStringAttributeWithDefault(anAttributes, LIST_SELECTION_ATTRIBUTE, ""); - order = (String) ReaderTool.getStringAttributeWithDefault(anAttributes, LIST_ORDER_ATTRIBUTE, ""); + selection = (String) XMLReaderTool.getStringAttributeWithDefault(anAttributes, LIST_SELECTION_ATTRIBUTE, ""); + order = (String) XMLReaderTool.getStringAttributeWithDefault(anAttributes, LIST_ORDER_ATTRIBUTE, ""); limit = (String) anAttributes.get(LIST_LIMIT_ATTRIBUTE); skip = (String) anAttributes.get(LIST_SKIP_ATTRIBUTE); }; @@ -329,10 +330,10 @@ public class DefaultProducerNodeBuilders { super(LOG_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { String typeString; - ReaderTool.checkAttributes(anAttributes, LOG_REQUIRED_ATTRIBUTES, LOG_OPTIONAL_ATTRIBUTES); + XMLReaderTool.checkAttributes(anAttributes, LOG_REQUIRED_ATTRIBUTES, LOG_OPTIONAL_ATTRIBUTES); message = (String) anAttributes.get(LOG_MESSAGE_ATTRIBUTE); if (anAttributes.containsKey(LOG_TYPE_ATTRIBUTE)) { @@ -379,8 +380,8 @@ public class DefaultProducerNodeBuilders { super(RESOURCEBUNDLE_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, RESOURCEBUNDLE_REQUIRED_ATTRIBUTES, RESOURCEBUNDLE_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, RESOURCEBUNDLE_REQUIRED_ATTRIBUTES, RESOURCEBUNDLE_OPTIONAL_ATTRIBUTES); key = (String) anAttributes.get(RESOURCEBUNDLE_KEY_ATTRIBUTE); bundle = (String) anAttributes.get(RESOURCEBUNDLE_BUNDLE_ATTRIBUTE); @@ -408,8 +409,8 @@ public class DefaultProducerNodeBuilders { super(FILEDATESETTING_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, FILEDATESETTING_REQUIRED_ATTRIBUTES, FILEDATESETTING_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, FILEDATESETTING_REQUIRED_ATTRIBUTES, FILEDATESETTING_OPTIONAL_ATTRIBUTES); fileNameKey = (String) anAttributes.get(FILEDATESETTING_FILE_ATTRIBUTE); dateKey = (String) anAttributes.get(FILEDATESETTING_DATE_ATTRIBUTE); @@ -434,8 +435,8 @@ public class DefaultProducerNodeBuilders { super(FILEDELETING_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, FILEDELETING_REQUIRED_ATTRIBUTES, FILEDELETING_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, FILEDELETING_REQUIRED_ATTRIBUTES, FILEDELETING_OPTIONAL_ATTRIBUTES); fileNameKey = (String) anAttributes.get(FILEDELETING_FILE_ATTRIBUTE); }; @@ -459,8 +460,8 @@ public class DefaultProducerNodeBuilders { super(SCRIPT_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, SCRIPT_REQUIRED_ATTRIBUTES, SCRIPT_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, SCRIPT_REQUIRED_ATTRIBUTES, SCRIPT_OPTIONAL_ATTRIBUTES); command = (String) anAttributes.get(SCRIPT_COMMAND_ATTRIBUTE); }; @@ -491,8 +492,8 @@ public class DefaultProducerNodeBuilders { destinationBasePath = aDestinationBasePath; } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, DIRCOPY_REQUIRED_ATTRIBUTES, DIRCOPY_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, DIRCOPY_REQUIRED_ATTRIBUTES, DIRCOPY_OPTIONAL_ATTRIBUTES); source = (String) anAttributes.get(DIRCOPY_SOURCE_ATTRIBUTE); destination = (String) anAttributes.get(DIRCOPY_DESTINATION_ATTRIBUTE); @@ -540,12 +541,12 @@ public class DefaultProducerNodeBuilders { generatorLibrary = aGeneratorLibrary; } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, GENERATION_REQUIRED_ATTRIBUTES, GENERATION_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, GENERATION_REQUIRED_ATTRIBUTES, GENERATION_OPTIONAL_ATTRIBUTES); generator = (String) anAttributes.get(GENERATION_GENERATOR_ATTRIBUTE); destination = (String) anAttributes.get(GENERATION_DESTINATION_ATTRIBUTE); - parameters = ReaderTool.getStringAttributeWithDefault(anAttributes, GENERATION_PARAMETERS_ATTRIBUTE, "" ); + parameters = XMLReaderTool.getStringAttributeWithDefault(anAttributes, GENERATION_PARAMETERS_ATTRIBUTE, "" ); }; public ProducerNode constructNode() { @@ -608,19 +609,19 @@ public class DefaultProducerNodeBuilders { model = aModel; } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, BATCHER_REQUIRED_ATTRIBUTES, BATCHER_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, BATCHER_REQUIRED_ATTRIBUTES, BATCHER_OPTIONAL_ATTRIBUTES); - batchDataKey = ReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_DATAKEY_ATTRIBUTE, "data" ); - batchInfoKey = ReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_INFOKEY_ATTRIBUTE, "info" ); - definition = ReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_DEFINITION_ATTRIBUTE, "" ); - selection = ReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_SELECTION_ATTRIBUTE, "" ); - order = ReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_ORDER_ATTRIBUTE, "" ); + batchDataKey = XMLReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_DATAKEY_ATTRIBUTE, "data" ); + batchInfoKey = XMLReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_INFOKEY_ATTRIBUTE, "info" ); + definition = XMLReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_DEFINITION_ATTRIBUTE, "" ); + selection = XMLReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_SELECTION_ATTRIBUTE, "" ); + order = XMLReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_ORDER_ATTRIBUTE, "" ); - batchSize = ReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_BATCHSIZE_ATTRIBUTE, "20" ); - minBatchSize = ReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_MINBATCHSIZE_ATTRIBUTE, "0" ); - skip = ReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_SKIP_ATTRIBUTE, "0" ); - process = ReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_PROCESS_ATTRIBUTE, "-1" ); + batchSize = XMLReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_BATCHSIZE_ATTRIBUTE, "20" ); + minBatchSize = XMLReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_MINBATCHSIZE_ATTRIBUTE, "0" ); + skip = XMLReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_SKIP_ATTRIBUTE, "0" ); + process = XMLReaderTool.getStringAttributeWithDefault(anAttributes, BATCHER_PROCESS_ATTRIBUTE, "-1" ); }; public ProducerNode constructNode() { @@ -670,8 +671,8 @@ public class DefaultProducerNodeBuilders { super(IF_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, IF_REQUIRED_ATTRIBUTES, IF_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, IF_REQUIRED_ATTRIBUTES, IF_OPTIONAL_ATTRIBUTES); condition = (String) anAttributes.get( IF_CONDITION_ATTRIBUTE ); }; @@ -742,8 +743,8 @@ public class DefaultProducerNodeBuilders { return definition.getNodeParameters(); }; - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributeSet(anAttributes.keySet(), definition.getRequiredAttributes(), definition.getOptionalAttributes()); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributeSet(anAttributes.keySet(), definition.getRequiredAttributes(), definition.getOptionalAttributes()); Iterator i = anAttributes.entrySet().iterator(); while (i.hasNext()) { diff --git a/source/mir/producer/reader/ProducerConfigReader.java b/source/mir/producer/reader/ProducerConfigReader.java index 8ad2acf6..f3683d43 100755 --- a/source/mir/producer/reader/ProducerConfigReader.java +++ b/source/mir/producer/reader/ProducerConfigReader.java @@ -1,780 +1,643 @@ -/* - * Copyright (C) 2001, 2002 The Mir-coders group - * - * This file is part of Mir. - * - * Mir is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Mir is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Mir; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with the com.oreilly.servlet library, any library - * licensed under the Apache Software License, The Sun (tm) Java Advanced - * Imaging library (JAI), The Sun JIMI library (or with modified versions of - * the above that use the same license as the above), and distribute linked - * combinations including the two. You must obey the GNU General Public - * License in all respects for all of the code used other than the above - * mentioned libraries. If you modify this file, you may extend this exception - * to your version of the file, but you are not obligated to do so. If you do - * not wish to do so, delete this exception statement from your version. - */ - -package mir.producer.reader; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.Stack; -import java.util.Vector; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; - -import mir.producer.CompositeProducerNode; -import mir.producer.ProducerFactory; -import mir.producer.ProducerNode; -import mir.producer.SimpleProducerVerb; - -import org.xml.sax.Attributes; -import org.xml.sax.InputSource; -import org.xml.sax.Locator; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; -import org.xml.sax.helpers.DefaultHandler; - -public class ProducerConfigReader { - private ProducerNodeBuilderLibrary builderLibrary; - private ProducerNodeBuilderLibrary scriptedNodeBuilderLibrary; - - public ProducerConfigReader() { - super(); - }; - - public void parseFile(String aFileName, ProducerNodeBuilderLibrary aBuilderLibrary, List aProducerFactories) throws ProducerConfigFailure { - parseFile(aFileName, aBuilderLibrary, aProducerFactories, new Vector()); - } - - public void parseFile(String aFileName, ProducerNodeBuilderLibrary aBuilderLibrary, List aProducerFactories, List aUsedFiles) throws ProducerConfigFailure { - try { - builderLibrary = aBuilderLibrary; - scriptedNodeBuilderLibrary = new ProducerNodeBuilderLibrary(); - - SAXParserFactory parserFactory = SAXParserFactory.newInstance(); - - parserFactory.setNamespaceAware(false); - parserFactory.setValidating(true); - - ProducerConfigHandler handler = new ProducerConfigHandler(parserFactory, aProducerFactories, aUsedFiles); - - handler.includeFile(aFileName); - } - catch (Throwable e) { - if (e instanceof SAXParseException && ((SAXParseException) e).getException() instanceof ProducerConfigFailure) { - throw (ProducerConfigFailure) ((SAXParseException) e).getException(); - } - else { - throw new ProducerConfigFailure( e ); - } - } - } - - private class ProducerConfigHandler extends DefaultHandler { - private Locator locator; - private Stack includeFileStack; - private SAXParserFactory parserFactory; - private SectionsManager manager; - private List usedFiles; - private InputSource inputSource; - - public ProducerConfigHandler(SAXParserFactory aParserFactory, List aProducers, List aUsedFiles) { - super(); - - includeFileStack=new Stack(); - parserFactory=aParserFactory; - includeFileStack = new Stack(); - manager = new SectionsManager(); - usedFiles = aUsedFiles; - - manager.pushHandler(new RootSectionHandler(aProducers)); - } - - public String getLocatorDescription(Locator aLocator) { - return aLocator.getPublicId()+" ("+aLocator.getLineNumber()+")"; - } - - public void setDocumentLocator(Locator aLocator) { - locator=aLocator; - } - - private void includeFile(String aFileName) throws ProducerConfigExc, ProducerConfigFailure, SAXParseException, SAXException { - File file; - SAXParser parser; - - try { - if (!includeFileStack.empty()) - file = new File(new File((String) includeFileStack.peek()).getParent(), aFileName); - else - file = new File(aFileName); - - System.err.println("about to include "+file.getCanonicalPath()); - - if (includeFileStack.contains(file.getCanonicalPath())) { - throw new ProducerConfigExc("recursive inclusion of file "+file.getCanonicalPath()); - } - - usedFiles.add(file); - - parser=parserFactory.newSAXParser(); - - inputSource = new InputSource(new FileInputStream(file)); - inputSource.setPublicId(file.getCanonicalPath()); - - includeFileStack.push(file.getCanonicalPath()); - try { - parser.parse(inputSource, this); - } - finally { - includeFileStack.pop(); - } - } - catch (ParserConfigurationException e) { - throw new ProducerConfigExc("Internal exception while including \""+aFileName+"\": "+e.getMessage()); - } - catch (SAXParseException e) { - throw e; - } - catch (ProducerConfigFailure e) { - throw e; - } - catch (FileNotFoundException e) { - throw new ProducerConfigExc("Include file \""+aFileName+"\" not found: "+e.getMessage()); - } - catch (IOException e) { - throw new ProducerConfigExc("unable to open include file \""+aFileName+"\": "+e.getMessage()); - } - } - - public void startElement(String aUri, String aTag, String aQualifiedName, Attributes anAttributes) throws SAXException { - Map attributesMap; - int i; - - try { - if (aQualifiedName.equals("include")) { - String fileName=anAttributes.getValue("file"); - - if (fileName==null) { - throw new ProducerConfigExc("include has no file attribute"); - } - - includeFile(fileName); - } - else { - attributesMap = new HashMap(); - for (i=0; i 0) { - throw new SAXParseException("Text not allowed", locator, new ProducerConfigExc("Config error at ["+getLocatorDescription(locator)+"]: Text not allowed")); - } - } - - } - public class SectionsManager { - Stack handlerStack; - - public SectionsManager() { - handlerStack = new Stack(); - } - - public void pushHandler(SectionHandler aSectionHandler) { - handlerStack.push(aSectionHandler); - } - - public SectionHandler popHandler() { - return (SectionHandler) handlerStack.pop(); - } - - public SectionHandler currentHandler() { - return (SectionHandler) handlerStack.peek(); - } - - public boolean isEmpty() { - return handlerStack.isEmpty(); - } - } - - public abstract class SectionHandler { - public abstract SectionHandler startElement(String aTag, Map anAttributes) throws ProducerConfigExc; - - public abstract void endElement(SectionHandler aHandler) throws ProducerConfigExc; -// { -// } - - public void finishSection() throws ProducerConfigExc { - } - } - - public class RootSectionHandler extends SectionHandler { - private List producers; - - public RootSectionHandler(List aProducers) { - producers = aProducers; - } - - public SectionHandler startElement(String aTag, Map anAttributes) throws ProducerConfigExc { - if (aTag.equals("producers")) { - return new ProducersSectionHandler(producers); - } - else - throw new ProducerConfigExc ("Tag 'producers' expected, tag '"+aTag+"' found"); - } - - public void endElement(SectionHandler aHandler) { - } - - public void finishSection() throws ProducerConfigExc { - } - } - - - private final static String PRODUCER_NAME_ATTRIBUTE = "name"; - private final static String[] PRODUCER_REQUIRED_ATTRIBUTES = { PRODUCER_NAME_ATTRIBUTE }; - private final static String[] PRODUCER_OPTIONAL_ATTRIBUTES = { }; - - private final static String NODE_DEFINITION_NAME_ATTRIBUTE = "name"; - private final static String[] NODE_DEFINITION_REQUIRED_ATTRIBUTES = { NODE_DEFINITION_NAME_ATTRIBUTE }; - private final static String[] NODE_DEFINITION_OPTIONAL_ATTRIBUTES = { }; - - public class ProducersSectionHandler extends SectionHandler { - private List producers; - private Set producerNames; - private String name; - - public ProducersSectionHandler(List aProducers) { - producers = aProducers; - producerNames = new HashSet(); - } - - public SectionHandler startElement(String aTag, Map anAttributes) throws ProducerConfigExc { - - if (aTag.equals("producer")) { - ReaderTool.checkAttributes(anAttributes, PRODUCER_REQUIRED_ATTRIBUTES, PRODUCER_OPTIONAL_ATTRIBUTES); - - name = (String) anAttributes.get(PRODUCER_NAME_ATTRIBUTE); - ReaderTool.checkValidIdentifier( name ); - - if (producerNames.contains(name)) - throw new ProducerConfigExc("Duplicate producer name: '" + name + "'"); - - name = (String) anAttributes.get(PRODUCER_NAME_ATTRIBUTE); - - return new ProducerSectionHandler(name); - } - else if (aTag.equals("nodedefinition")) { - ReaderTool.checkAttributes(anAttributes, NODE_DEFINITION_REQUIRED_ATTRIBUTES, NODE_DEFINITION_OPTIONAL_ATTRIBUTES); - - name = (String) anAttributes.get(NODE_DEFINITION_NAME_ATTRIBUTE); - ReaderTool.checkValidIdentifier( name ); - -// if (producers.containsKey(name)) -// throw new ProducerConfigExc("Duplicate producer name: '" + name + "'"); - - name = (String) anAttributes.get(NODE_DEFINITION_NAME_ATTRIBUTE); - - return new NodeDefinitionSectionHandler(name); - } - - throw new ProducerConfigExc("Unexpected tag: "+aTag ); - } - - public void endElement(SectionHandler aHandler) throws ProducerConfigExc { - if (aHandler instanceof ProducerSectionHandler) { - producers.add(((ProducerSectionHandler) aHandler).getProducerFactory()); - producerNames.add(((ProducerSectionHandler) aHandler).getProducerFactory().getName()); - } - else if (aHandler instanceof NodeDefinitionSectionHandler) { - scriptedNodeBuilderLibrary.registerFactory(name, - new DefaultProducerNodeBuilders.ScriptedProducerNodeBuilder.factory( - ((NodeDefinitionSectionHandler) aHandler).getDefinition())); - } - else throw new ProducerConfigExc("ProducersSectionHandler.endElement Internal error: Unexpected handler: " + aHandler.getClass().getName()); - } - - public void finishSection() throws ProducerConfigExc { - } - } - - public class ProducerSectionHandler extends SectionHandler { - private ProducerFactory producerFactory; - private String factoryName; - - private ProducerNode body; - private Map verbNodes; - private List verbs; - private String defaultVerb; - - public ProducerSectionHandler(String aName) { - factoryName = aName; - } - - public SectionHandler startElement(String aTag, Map anAttributes) throws ProducerConfigExc { - if (aTag.equals("verbs")) { - if (verbs!=null) - throw new ProducerConfigExc("Verbs already processed"); - if (body!=null) - throw new ProducerConfigExc("Verbs should come before body"); - else - return new ProducerVerbsSectionHandler(); - } - else if (aTag.equals("body")) { - if (body==null) - return new ProducerNodeSectionHandler(); - else - throw new ProducerConfigExc("Body already processed"); - } - throw new ProducerConfigExc("Unexpected tag: '"+aTag+"'"); - } - - public void endElement(SectionHandler aHandler) throws ProducerConfigExc { - if (aHandler instanceof ProducerNodeSectionHandler) { - body = ((ProducerNodeSectionHandler) aHandler).getProducerNode(); - } - else if (aHandler instanceof ProducerVerbsSectionHandler) - { - verbs = ((ProducerVerbsSectionHandler) aHandler).getVerbs(); - verbNodes = ((ProducerVerbsSectionHandler) aHandler).getVerbNodes(); - defaultVerb = ((ProducerVerbsSectionHandler) aHandler).getDefaultVerb(); - } - else throw new ProducerConfigExc("ProducerSectionHandler.endElement Internal error: Unexpected handler: " + aHandler.getClass().getName()); - } - - public void finishSection() throws ProducerConfigExc { - if (verbs==null) - throw new ProducerConfigExc("No verbs defined"); - - if (body==null) - throw new ProducerConfigExc("No body defined"); - - producerFactory = new ScriptedProducerFactory(factoryName, verbs, verbNodes, body, defaultVerb); - } - - public ProducerFactory getProducerFactory() { - return producerFactory; - } - } - - private final static String PRODUCER_VERB_NAME_ATTRIBUTE = "name"; - private final static String PRODUCER_VERB_DESCRIPTION_ATTRIBUTE = "description"; - private final static String PRODUCER_VERB_DEFAULT_ATTRIBUTE = "default"; - private final static String[] PRODUCER_VERB_REQUIRED_ATTRIBUTES = { PRODUCER_VERB_NAME_ATTRIBUTE }; - private final static String[] PRODUCER_VERB_OPTIONAL_ATTRIBUTES = { PRODUCER_VERB_DEFAULT_ATTRIBUTE, PRODUCER_VERB_DESCRIPTION_ATTRIBUTE }; - - public class ProducerVerbsSectionHandler extends SectionHandler { - private Map verbNodes; - private List verbs; - private String defaultVerb; - private String currentVerb; - private String currentVerbDescription; - - public ProducerVerbsSectionHandler() { - verbNodes = new HashMap(); - verbs = new Vector(); - defaultVerb = null; - } - - public SectionHandler startElement(String aTag, Map anAttributes) throws ProducerConfigExc { - if (aTag.equals("verb")) { - ReaderTool.checkAttributes(anAttributes, PRODUCER_VERB_REQUIRED_ATTRIBUTES, PRODUCER_VERB_OPTIONAL_ATTRIBUTES); - currentVerb = (String) anAttributes.get( PRODUCER_VERB_NAME_ATTRIBUTE ); - - ReaderTool.checkValidIdentifier( currentVerb ); - - if (verbNodes.containsKey(currentVerb)) - throw new ProducerConfigExc( "Duplicate definition of verb '" + currentVerb + "'" ); - - if (anAttributes.containsKey(PRODUCER_VERB_DEFAULT_ATTRIBUTE)) { - if (defaultVerb!=null) - throw new ProducerConfigExc( "Default verb already declared" ); - - defaultVerb = currentVerb; - } - - if (anAttributes.containsKey( PRODUCER_VERB_DESCRIPTION_ATTRIBUTE )) - currentVerbDescription = (String) anAttributes.get( PRODUCER_VERB_DESCRIPTION_ATTRIBUTE ); - else - currentVerbDescription = ""; - - return new ProducerNodeSectionHandler(); - } - else throw new ProducerConfigExc("Only 'verb' tags allowed here, '" + aTag + "' encountered."); - } - - public void endElement(SectionHandler aHandler) { - verbNodes.put(currentVerb, ((ProducerNodeSectionHandler) aHandler).getProducerNode()); - verbs.add(new SimpleProducerVerb(currentVerb, currentVerbDescription)); - } - - public void finishSection() { - } - - public String getDefaultVerb() { - return defaultVerb; - } - - public List getVerbs() { - return verbs; - } - - public Map getVerbNodes() { - return verbNodes; - } - } - - public class EmptySectionHandler extends SectionHandler { - public SectionHandler startElement(String aTag, Map anAttributes) throws ProducerConfigExc { - throw new ProducerConfigExc("No tags are allowed here"); - } - - public void endElement(SectionHandler aHandler) { - } - - } - - public class MultiProducerNodeSectionHandler extends SectionHandler { - private Map nodeParameters; - private Set validNodeParameters; - private String currentNodeParameter; - private String scriptedNodeName; - private Set allowedNodeParameterReferences; - - public MultiProducerNodeSectionHandler(String aScriptedNodeName, Set anAllowedNodeParameterReferences, Set aValidNodeParameters) { - allowedNodeParameterReferences = anAllowedNodeParameterReferences; - scriptedNodeName = aScriptedNodeName; - validNodeParameters = aValidNodeParameters; - nodeParameters = new HashMap(); - } - public MultiProducerNodeSectionHandler(Set aValidNodeParameters) { - this("", new HashSet(), aValidNodeParameters); - } - - public SectionHandler startElement(String aTag, Map anAttributes) throws ProducerConfigExc { - if (!validNodeParameters.contains(aTag)) - throw new ProducerConfigExc("Invalid node parameter: '" + aTag + "'"); - else if (nodeParameters.containsKey(aTag)) - throw new ProducerConfigExc("Node parameter: '" + aTag + "' already specified"); - else if (anAttributes.size()>0) - throw new ProducerConfigExc("No parameters are allowed here"); - - currentNodeParameter = aTag; - - return new ProducerNodeSectionHandler(scriptedNodeName, validNodeParameters); - } - - public void endElement(SectionHandler aHandler) throws ProducerConfigExc { - if (aHandler instanceof ProducerNodeSectionHandler) { - nodeParameters.put(currentNodeParameter, ((ProducerNodeSectionHandler) aHandler).getProducerNode()); - } - else { - throw new ProducerConfigExc("Internal error: unknown section handler '" + aHandler.getClass().getName() + "'" ); - } - } - - public Map getNodeParameters() { - return nodeParameters; - } - } - - public class ProducerNodeSectionHandler extends SectionHandler { - private CompositeProducerNode producerNode; - private ProducerNodeBuilder currentBuilder; - private String scriptedNodeName; - private Set allowedNodeParameterReferences; - - public ProducerNodeSectionHandler(String aScriptedNodeName, Set anAllowedNodeParameterReferences) { - producerNode = new CompositeProducerNode(); - scriptedNodeName = aScriptedNodeName; - allowedNodeParameterReferences = anAllowedNodeParameterReferences; - } - - public ProducerNodeSectionHandler() { - this("", new HashSet()); - } - - public SectionHandler startElement(String aTag, Map anAttributes) throws ProducerConfigExc { - if (allowedNodeParameterReferences.contains((aTag))) { - if (!anAttributes.isEmpty()) { - throw new ProducerConfigExc( "No attributes allowed" ); - } - - currentBuilder = new DefaultProducerNodeBuilders.ScriptedProducerParameterNodeBuilder(scriptedNodeName, aTag); -// producerNode.addSubNode( -// new ScriptedProducerNodeDefinition.NodeParameterProducerNode(scriptedNodeName, aTag)); - return new EmptySectionHandler(); - } - else if (scriptedNodeBuilderLibrary.hasBuilderForName(aTag) || builderLibrary.hasBuilderForName((aTag))) { - - if (scriptedNodeBuilderLibrary.hasBuilderForName(aTag)) - currentBuilder = scriptedNodeBuilderLibrary.constructBuilder(aTag); - else - currentBuilder = builderLibrary.constructBuilder(aTag); - - currentBuilder.setAttributes(anAttributes); - if (currentBuilder.getAvailableSubNodes().isEmpty()) { - return new EmptySectionHandler(); - } - if (currentBuilder.getAvailableSubNodes().size()>1) - return new MultiProducerNodeSectionHandler(scriptedNodeName, allowedNodeParameterReferences, currentBuilder.getAvailableSubNodes()); - else if (currentBuilder.getAvailableSubNodes().size()<1) - return new EmptySectionHandler(); - else { - return new ProducerNodeSectionHandler(scriptedNodeName, allowedNodeParameterReferences); - } - } - else - throw new ProducerConfigExc("Unknown producer node tag: '" + aTag + "'"); - } - - public void endElement(SectionHandler aHandler) throws ProducerConfigExc { - if (aHandler instanceof ProducerNodeSectionHandler) { - currentBuilder.setSubNode((String) (currentBuilder.getAvailableSubNodes().iterator().next()), - ((ProducerNodeSectionHandler) aHandler).getProducerNode()); - } - else if (aHandler instanceof MultiProducerNodeSectionHandler) { - Iterator i; - Map nodeParameters; - Map.Entry entry; - - nodeParameters = ((MultiProducerNodeSectionHandler) aHandler).getNodeParameters(); - i = nodeParameters.entrySet().iterator(); - while (i.hasNext()) { - entry = (Map.Entry) i.next(); - currentBuilder.setSubNode((String) entry.getKey(), (ProducerNode) entry.getValue()); - } - } - else if (aHandler instanceof EmptySectionHandler) { - // deliberately empty: nothing expected, so nothing to process - } - else { - throw new ProducerConfigExc("Internal error: unknown section handler '" + aHandler.getClass().getName() + "'" ); - } - - producerNode.addSubNode(currentBuilder.constructNode()); - currentBuilder = null; - } - - public ProducerNode getProducerNode() { - if (producerNode.getNrSubNodes()==1) { - return producerNode.getSubNode(0); - } - else { - return producerNode; - } - } - } - - public class NodeDefinitionSectionHandler extends SectionHandler { - private ScriptedProducerNodeDefinition nodeDefinition; - private ProducerNode body; - private Map stringParameters; - private Map integerParameters; - private Map nodeParameters; - private String name; - - public NodeDefinitionSectionHandler(String aName) { - body = null; - nodeParameters = null; - stringParameters = null; - integerParameters = null; - name = aName; - } - - public SectionHandler startElement(String aTag, Map anAttributes) throws ProducerConfigExc { - if (aTag.equals("parameters")) { - if (!anAttributes.isEmpty()) { - throw new ProducerConfigExc( "No attributes allowed for tag 'parameters'" ); - } - if (nodeParameters!=null) { - throw new ProducerConfigExc( "Parameters have already been declared" ); - } - if (body!=null) { - throw new ProducerConfigExc( "Parameters should come before definition" ); - } - - return new NodeDefinitionParametersSectionHandler(); - } - else if (aTag.equals("definition")) { - return new ProducerNodeSectionHandler(name, nodeParameters.keySet()); - } - else throw new ProducerConfigExc("Only 'definition' or 'parameters' tags allowed here, '" + aTag + "' encountered."); - } - - public void endElement(SectionHandler aHandler) { - if (aHandler instanceof NodeDefinitionParametersSectionHandler) { - stringParameters = ((NodeDefinitionParametersSectionHandler) aHandler).getStringParameters(); - integerParameters = ((NodeDefinitionParametersSectionHandler) aHandler).getIntegerParameters(); - nodeParameters = ((NodeDefinitionParametersSectionHandler) aHandler).getNodeParameters(); - } - else if (aHandler instanceof ProducerNodeSectionHandler) { - body = ((ProducerNodeSectionHandler) aHandler).getProducerNode(); - } - } - - public void finishSection() throws ProducerConfigExc { - Iterator i; - if (body == null) - throw new ProducerConfigExc( "Definition missing" ); - - nodeDefinition = new ScriptedProducerNodeDefinition(name); - - nodeDefinition.setBody(body); - - i = nodeParameters.keySet().iterator(); - while (i.hasNext()) { - nodeDefinition.addNodeParameter((String) i.next()); - } - - i = stringParameters.entrySet().iterator(); - while (i.hasNext()) { - Map.Entry entry = (Map.Entry) i.next(); - nodeDefinition.addStringParameter((String) entry.getKey(), (String) entry.getValue()); - } - - i = integerParameters.entrySet().iterator(); - while (i.hasNext()) { - Map.Entry entry = (Map.Entry) i.next(); - nodeDefinition.addIntegerParameter((String) entry.getKey(), (String) entry.getValue()); - } - } - - public ScriptedProducerNodeDefinition getDefinition() { - return nodeDefinition; - } - } - - private final static String NODE_DEFINITION_PARAMETER_NAME_ATTRIBUTE = "name"; - private final static String NODE_DEFINITION_PARAMETER_DEFAULTVALUE_ATTRIBUTE = "defaultvalue"; - private final static String[] NODE_DEFINITION_PARAMETER_REQUIRED_ATTRIBUTES = { NODE_DEFINITION_PARAMETER_NAME_ATTRIBUTE }; - private final static String[] NODE_DEFINITION_PARAMETER_OPTIONAL_ATTRIBUTES = { NODE_DEFINITION_PARAMETER_DEFAULTVALUE_ATTRIBUTE }; - private final static String[] NODE_DEFINITION_NODE_PARAMETER_OPTIONAL_ATTRIBUTES = { }; - - public class NodeDefinitionParametersSectionHandler extends SectionHandler { - private Map nodeParameters; - private Map stringParameters; - private Map integerParameters; - - public NodeDefinitionParametersSectionHandler() { - nodeParameters = new HashMap(); - stringParameters = new HashMap(); - integerParameters = new HashMap(); - } - - public SectionHandler startElement(String aTag, Map anAttributes) throws ProducerConfigExc { - String parameterName; - String defaultValue; - - if (aTag.equals("node")) { - ReaderTool.checkAttributes(anAttributes, NODE_DEFINITION_PARAMETER_REQUIRED_ATTRIBUTES, NODE_DEFINITION_NODE_PARAMETER_OPTIONAL_ATTRIBUTES); - parameterName = (String) anAttributes.get( NODE_DEFINITION_PARAMETER_NAME_ATTRIBUTE ); - - if (nodeParameters.containsKey(parameterName)) - throw new ProducerConfigExc("Duplicate parameter name: '" + parameterName + "'"); - - ReaderTool.checkValidIdentifier( parameterName ); - - nodeParameters.put(parameterName, parameterName); - - return new EmptySectionHandler(); - } - else if (aTag.equals("string") || aTag.equals("integer")) { - ReaderTool.checkAttributes(anAttributes, NODE_DEFINITION_PARAMETER_REQUIRED_ATTRIBUTES, NODE_DEFINITION_PARAMETER_OPTIONAL_ATTRIBUTES); - parameterName = (String) anAttributes.get( NODE_DEFINITION_PARAMETER_NAME_ATTRIBUTE ); - - if (stringParameters.containsKey(parameterName) || integerParameters.containsKey(parameterName)) - throw new ProducerConfigExc("Duplicate parameter name: '" + parameterName + "'"); - - ReaderTool.checkValidIdentifier( parameterName ); - - defaultValue = (String) anAttributes.get( NODE_DEFINITION_PARAMETER_DEFAULTVALUE_ATTRIBUTE ); - - if (aTag.equals("string")) - stringParameters.put(parameterName, defaultValue); - else - integerParameters.put(parameterName, defaultValue); - - return new EmptySectionHandler(); - } - else throw new ProducerConfigExc("Only 'string', 'integer' and 'node' tags allowed here, '" + aTag + "' encountered."); - - } - - public void endElement(SectionHandler aHandler) { - } - - public void finishSection() { - } - - public Map getNodeParameters() { - return nodeParameters; - } - - public Map getStringParameters() { - return stringParameters; - } - - public Map getIntegerParameters() { - return integerParameters; - } - } -} +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with the com.oreilly.servlet library, any library + * licensed under the Apache Software License, The Sun (tm) Java Advanced + * Imaging library (JAI), The Sun JIMI library (or with modified versions of + * the above that use the same license as the above), and distribute linked + * combinations including the two. You must obey the GNU General Public + * License in all respects for all of the code used other than the above + * mentioned libraries. If you modify this file, you may extend this exception + * to your version of the file, but you are not obligated to do so. If you do + * not wish to do so, delete this exception statement from your version. + */ + +package mir.producer.reader; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Stack; +import java.util.Vector; + +import mir.producer.CompositeProducerNode; +import mir.producer.ProducerFactory; +import mir.producer.ProducerNode; +import mir.producer.SimpleProducerVerb; + +import mir.util.*; + +public class ProducerConfigReader { + private ProducerNodeBuilderLibrary builderLibrary; + private ProducerNodeBuilderLibrary scriptedNodeBuilderLibrary; + + public ProducerConfigReader() { + super(); + }; + + public void parseFile(String aFileName, ProducerNodeBuilderLibrary aBuilderLibrary, List aProducerFactories) throws ProducerConfigFailure { + parseFile(aFileName, aBuilderLibrary, aProducerFactories, new Vector()); + } + + public void parseFile(String aFileName, ProducerNodeBuilderLibrary aBuilderLibrary, List aProducerFactories, List aUsedFiles) throws ProducerConfigFailure { + try { + XMLReader reader = new XMLReader(); + + builderLibrary = aBuilderLibrary; + scriptedNodeBuilderLibrary = new ProducerNodeBuilderLibrary(); + + reader.parseFile(aFileName, new RootSectionHandler(aProducerFactories), aUsedFiles); + } + catch (Throwable e) { + if ((e instanceof XMLReader.XMLReaderExc) && ((XMLReader.XMLReaderExc) e).getHasLocation()) { + XMLReader.XMLReaderExc f = (XMLReader.XMLReaderExc) e; + throw new ProducerConfigFailure("'" + f.getMessage()+"' in " + f.getFilename()+"(line " + f.getLineNr()+", column " + f.getColumnNr() + ")", e); + } + throw new ProducerConfigFailure( e ); + } + } + + + public class RootSectionHandler implements XMLReader.SectionHandler { + private List producers; + + public RootSectionHandler(List aProducers) { + producers = aProducers; + } + + public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc { + if (aTag.equals("producers")) { + return new ProducersSectionHandler(producers); + } + else + throw new XMLReader.XMLReaderExc("Tag 'producers' expected, tag '"+aTag+"' found"); + } + + public void endElement(XMLReader.SectionHandler aHandler) { + } + + public void finishSection() { + } + } + + + private final static String PRODUCER_NAME_ATTRIBUTE = "name"; + private final static String[] PRODUCER_REQUIRED_ATTRIBUTES = { PRODUCER_NAME_ATTRIBUTE }; + private final static String[] PRODUCER_OPTIONAL_ATTRIBUTES = { }; + + private final static String NODE_DEFINITION_NAME_ATTRIBUTE = "name"; + private final static String[] NODE_DEFINITION_REQUIRED_ATTRIBUTES = { NODE_DEFINITION_NAME_ATTRIBUTE }; + private final static String[] NODE_DEFINITION_OPTIONAL_ATTRIBUTES = { }; + + public class ProducersSectionHandler implements XMLReader.SectionHandler { + private List producers; + private Set producerNames; + private String name; + + public ProducersSectionHandler(List aProducers) { + producers = aProducers; + producerNames = new HashSet(); + } + + public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc { + if (aTag.equals("producer")) { + XMLReaderTool.checkAttributes(anAttributes, + PRODUCER_REQUIRED_ATTRIBUTES, + PRODUCER_OPTIONAL_ATTRIBUTES); + + name = (String) anAttributes.get(PRODUCER_NAME_ATTRIBUTE); + XMLReaderTool.checkValidIdentifier(name); + + if (producerNames.contains(name)) + throw new XMLReader.XMLReaderExc("Duplicate producer name: '" + + name + "'"); + + name = (String) anAttributes.get(PRODUCER_NAME_ATTRIBUTE); + + return new ProducerSectionHandler(name); + } + else if (aTag.equals("nodedefinition")) { + XMLReaderTool.checkAttributes(anAttributes, + NODE_DEFINITION_REQUIRED_ATTRIBUTES, + NODE_DEFINITION_OPTIONAL_ATTRIBUTES); + + name = (String) anAttributes.get(NODE_DEFINITION_NAME_ATTRIBUTE); + XMLReaderTool.checkValidIdentifier(name); + + name = (String) anAttributes.get(NODE_DEFINITION_NAME_ATTRIBUTE); + + return new NodeDefinitionSectionHandler(name); + } + throw new XMLReader.XMLReaderExc("Unexpected tag: " + aTag); + } + + public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc { + if (aHandler instanceof ProducerSectionHandler) { + producers.add(((ProducerSectionHandler) aHandler).getProducerFactory()); + producerNames.add(((ProducerSectionHandler) aHandler).getProducerFactory().getName()); + } + else if (aHandler instanceof NodeDefinitionSectionHandler) { + scriptedNodeBuilderLibrary.registerFactory(name, + new DefaultProducerNodeBuilders.ScriptedProducerNodeBuilder.factory( + ((NodeDefinitionSectionHandler) aHandler).getDefinition())); + } + else throw new XMLReader.XMLReaderExc("ProducersSectionHandler.endElement Internal error: Unexpected handler: " + aHandler.getClass().getName()); + } + + public void finishSection() { + } + } + + public class ProducerSectionHandler implements XMLReader.SectionHandler { + private ProducerFactory producerFactory; + private String factoryName; + + private ProducerNode body; + private Map verbNodes; + private List verbs; + private String defaultVerb; + + public ProducerSectionHandler(String aName) { + factoryName = aName; + } + + public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc { + if (aTag.equals("verbs")) { + if (verbs!=null) + throw new XMLReader.XMLReaderExc("Verbs already processed"); + if (body!=null) + throw new XMLReader.XMLReaderExc("Verbs should come before body"); + else + return new ProducerVerbsSectionHandler(); + } + else if (aTag.equals("body")) { + if (body==null) + return new ProducerNodeSectionHandler(); + else + throw new XMLReader.XMLReaderExc("Body already processed"); + } + throw new XMLReader.XMLReaderExc("Unexpected tag: '"+aTag+"'"); + } + + public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc { + if (aHandler instanceof ProducerNodeSectionHandler) { + body = ((ProducerNodeSectionHandler) aHandler).getProducerNode(); + } + else if (aHandler instanceof ProducerVerbsSectionHandler) + { + verbs = ((ProducerVerbsSectionHandler) aHandler).getVerbs(); + verbNodes = ((ProducerVerbsSectionHandler) aHandler).getVerbNodes(); + defaultVerb = ((ProducerVerbsSectionHandler) aHandler).getDefaultVerb(); + } + else throw new XMLReader.XMLReaderExc("ProducerSectionHandler.endElement Internal error: Unexpected handler: " + aHandler.getClass().getName()); + } + + public void finishSection() throws XMLReader.XMLReaderExc { + if (verbs==null) + throw new XMLReader.XMLReaderExc("No verbs defined"); + + if (body==null) + throw new XMLReader.XMLReaderExc("No body defined"); + + producerFactory = new ScriptedProducerFactory(factoryName, verbs, verbNodes, body, defaultVerb); + } + + public ProducerFactory getProducerFactory() { + return producerFactory; + } + } + + private final static String PRODUCER_VERB_NAME_ATTRIBUTE = "name"; + private final static String PRODUCER_VERB_DESCRIPTION_ATTRIBUTE = "description"; + private final static String PRODUCER_VERB_DEFAULT_ATTRIBUTE = "default"; + private final static String[] PRODUCER_VERB_REQUIRED_ATTRIBUTES = { PRODUCER_VERB_NAME_ATTRIBUTE }; + private final static String[] PRODUCER_VERB_OPTIONAL_ATTRIBUTES = { PRODUCER_VERB_DEFAULT_ATTRIBUTE, PRODUCER_VERB_DESCRIPTION_ATTRIBUTE }; + + public class ProducerVerbsSectionHandler implements XMLReader.SectionHandler { + private Map verbNodes; + private List verbs; + private String defaultVerb; + private String currentVerb; + private String currentVerbDescription; + + public ProducerVerbsSectionHandler() { + verbNodes = new HashMap(); + verbs = new Vector(); + defaultVerb = null; + } + + public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc { + if (aTag.equals("verb")) { + XMLReaderTool.checkAttributes(anAttributes, + PRODUCER_VERB_REQUIRED_ATTRIBUTES, + PRODUCER_VERB_OPTIONAL_ATTRIBUTES); + currentVerb = (String) anAttributes.get(PRODUCER_VERB_NAME_ATTRIBUTE); + + XMLReaderTool.checkValidIdentifier(currentVerb); + + if (verbNodes.containsKey(currentVerb)) + throw new XMLReader.XMLReaderExc("Duplicate definition of verb '" + + currentVerb + "'"); + + if (anAttributes.containsKey(PRODUCER_VERB_DEFAULT_ATTRIBUTE)) { + if (defaultVerb != null) + throw new XMLReader.XMLReaderExc("Default verb already declared"); + + defaultVerb = currentVerb; + } + + if (anAttributes.containsKey(PRODUCER_VERB_DESCRIPTION_ATTRIBUTE)) + currentVerbDescription = (String) anAttributes.get( + PRODUCER_VERB_DESCRIPTION_ATTRIBUTE); + else + currentVerbDescription = ""; + + return new ProducerNodeSectionHandler(); + } + else + throw new XMLReader.XMLReaderExc("Only 'verb' tags allowed here, '" + + aTag + "' encountered."); + } + + public void endElement(XMLReader.SectionHandler aHandler) { + verbNodes.put(currentVerb, ((ProducerNodeSectionHandler) aHandler).getProducerNode()); + verbs.add(new SimpleProducerVerb(currentVerb, currentVerbDescription)); + } + + public void finishSection() { + } + + public String getDefaultVerb() { + return defaultVerb; + } + + public List getVerbs() { + return verbs; + } + + public Map getVerbNodes() { + return verbNodes; + } + } + + public class EmptySectionHandler implements XMLReader.SectionHandler { + public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc { + throw new XMLReader.XMLReaderExc("No tags are allowed here"); + } + + public void endElement(XMLReader.SectionHandler aHandler) { + } + + public void finishSection() { + } + } + + public class MultiProducerNodeSectionHandler implements XMLReader.SectionHandler { + private Map nodeParameters; + private Set validNodeParameters; + private String currentNodeParameter; + private String scriptedNodeName; + private Set allowedNodeParameterReferences; + + public MultiProducerNodeSectionHandler(String aScriptedNodeName, Set anAllowedNodeParameterReferences, Set aValidNodeParameters) { + allowedNodeParameterReferences = anAllowedNodeParameterReferences; + scriptedNodeName = aScriptedNodeName; + validNodeParameters = aValidNodeParameters; + nodeParameters = new HashMap(); + } + public MultiProducerNodeSectionHandler(Set aValidNodeParameters) { + this("", new HashSet(), aValidNodeParameters); + } + + public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc { + if (!validNodeParameters.contains(aTag)) + throw new XMLReader.XMLReaderExc("Invalid node parameter: '" + aTag + "'"); + else if (nodeParameters.containsKey(aTag)) + throw new XMLReader.XMLReaderExc("Node parameter: '" + aTag + "' already specified"); + else if (anAttributes.size()>0) + throw new XMLReader.XMLReaderExc("No parameters are allowed here"); + + currentNodeParameter = aTag; + + return new ProducerNodeSectionHandler(scriptedNodeName, validNodeParameters); + } + + public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc { + if (aHandler instanceof ProducerNodeSectionHandler) { + nodeParameters.put(currentNodeParameter, ((ProducerNodeSectionHandler) aHandler).getProducerNode()); + } + else { + throw new XMLReader.XMLReaderExc("Internal error: unknown section handler '" + aHandler.getClass().getName() + "'" ); + } + } + + public Map getNodeParameters() { + return nodeParameters; + } + + public void finishSection() { + } + } + + public class ProducerNodeSectionHandler implements XMLReader.SectionHandler { + private CompositeProducerNode producerNode; + private ProducerNodeBuilder currentBuilder; + private String scriptedNodeName; + private Set allowedNodeParameterReferences; + + public ProducerNodeSectionHandler(String aScriptedNodeName, Set anAllowedNodeParameterReferences) { + producerNode = new CompositeProducerNode(); + scriptedNodeName = aScriptedNodeName; + allowedNodeParameterReferences = anAllowedNodeParameterReferences; + } + + public ProducerNodeSectionHandler() { + this("", new HashSet()); + } + + public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc { + try { + if (allowedNodeParameterReferences.contains( (aTag))) { + if (!anAttributes.isEmpty()) { + throw new XMLReader.XMLReaderExc("No attributes allowed"); + } + + currentBuilder = new DefaultProducerNodeBuilders. + ScriptedProducerParameterNodeBuilder(scriptedNodeName, aTag); + return new EmptySectionHandler(); + } + else if (scriptedNodeBuilderLibrary.hasBuilderForName(aTag) || + builderLibrary.hasBuilderForName( (aTag))) { + + if (scriptedNodeBuilderLibrary.hasBuilderForName(aTag)) + currentBuilder = scriptedNodeBuilderLibrary.constructBuilder(aTag); + else + currentBuilder = builderLibrary.constructBuilder(aTag); + + currentBuilder.setAttributes(anAttributes); + if (currentBuilder.getAvailableSubNodes().isEmpty()) { + return new EmptySectionHandler(); + } + if (currentBuilder.getAvailableSubNodes().size() > 1) + return new MultiProducerNodeSectionHandler(scriptedNodeName, + allowedNodeParameterReferences, + currentBuilder.getAvailableSubNodes()); + else if (currentBuilder.getAvailableSubNodes().size() < 1) + return new EmptySectionHandler(); + else { + return new ProducerNodeSectionHandler(scriptedNodeName, + allowedNodeParameterReferences); + } + } + else + throw new XMLReader.XMLReaderExc("Unknown producer node tag: '" + + aTag + "'"); + } + catch (Throwable t) { + throw new XMLReader.XMLReaderFailure(t); + } + } + + public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc { + try { + if (aHandler instanceof ProducerNodeSectionHandler) { + currentBuilder.setSubNode( + (String) (currentBuilder.getAvailableSubNodes().iterator().next()), + ((ProducerNodeSectionHandler) aHandler).getProducerNode()); + } + else if (aHandler instanceof MultiProducerNodeSectionHandler) { + Iterator i; + Map nodeParameters; + Map.Entry entry; + + nodeParameters = ( (MultiProducerNodeSectionHandler) aHandler). + getNodeParameters(); + i = nodeParameters.entrySet().iterator(); + while (i.hasNext()) { + entry = (Map.Entry) i.next(); + currentBuilder.setSubNode( (String) entry.getKey(), + (ProducerNode) entry.getValue()); + } + } + else if (aHandler instanceof EmptySectionHandler) { + // deliberately empty: nothing expected, so nothing to process + } + else { + throw new XMLReader.XMLReaderExc( + "Internal error: unknown section handler '" + + aHandler.getClass().getName() + "'"); + } + + producerNode.addSubNode(currentBuilder.constructNode()); + currentBuilder = null; + } + catch (Throwable t) { + throw new XMLReader.XMLReaderFailure(t); + } + } + + public ProducerNode getProducerNode() { + if (producerNode.getNrSubNodes()==1) { + return producerNode.getSubNode(0); + } + else { + return producerNode; + } + } + + public void finishSection() { + } + } + + public class NodeDefinitionSectionHandler implements XMLReader.SectionHandler { + private ScriptedProducerNodeDefinition nodeDefinition; + private ProducerNode body; + private Map stringParameters; + private Map integerParameters; + private Map nodeParameters; + private String name; + + public NodeDefinitionSectionHandler(String aName) { + body = null; + nodeParameters = null; + stringParameters = null; + integerParameters = null; + name = aName; + } + + public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc { + if (aTag.equals("parameters")) { + if (!anAttributes.isEmpty()) { + throw new XMLReader.XMLReaderExc( "No attributes allowed for tag 'parameters'" ); + } + if (nodeParameters!=null) { + throw new XMLReader.XMLReaderExc( "Parameters have already been declared" ); + } + if (body!=null) { + throw new XMLReader.XMLReaderExc( "Parameters should come before definition in nodedefinition '" + name +"'" ); + } + + return new NodeDefinitionParametersSectionHandler(); + } + else if (aTag.equals("definition")) { + if (nodeParameters==null) + throw new XMLReader.XMLReaderExc( "Parameters should come before definition in nodedefinition '" + name +"'" ); + + return new ProducerNodeSectionHandler(name, nodeParameters.keySet()); + } + else throw new XMLReader.XMLReaderExc("Only 'definition' or 'parameters' tags allowed here, '" + aTag + "' encountered."); + } + + public void endElement(XMLReader.SectionHandler aHandler) { + if (aHandler instanceof NodeDefinitionParametersSectionHandler) { + stringParameters = ((NodeDefinitionParametersSectionHandler) aHandler).getStringParameters(); + integerParameters = ((NodeDefinitionParametersSectionHandler) aHandler).getIntegerParameters(); + nodeParameters = ((NodeDefinitionParametersSectionHandler) aHandler).getNodeParameters(); + } + else if (aHandler instanceof ProducerNodeSectionHandler) { + body = ((ProducerNodeSectionHandler) aHandler).getProducerNode(); + } + } + + public void finishSection() throws XMLReader.XMLReaderExc { + Iterator i; + if (body == null) + throw new XMLReader.XMLReaderExc( "Definition missing" ); + + nodeDefinition = new ScriptedProducerNodeDefinition(name); + + nodeDefinition.setBody(body); + + i = nodeParameters.keySet().iterator(); + while (i.hasNext()) { + nodeDefinition.addNodeParameter((String) i.next()); + } + + i = stringParameters.entrySet().iterator(); + while (i.hasNext()) { + Map.Entry entry = (Map.Entry) i.next(); + nodeDefinition.addStringParameter((String) entry.getKey(), (String) entry.getValue()); + } + + i = integerParameters.entrySet().iterator(); + while (i.hasNext()) { + Map.Entry entry = (Map.Entry) i.next(); + nodeDefinition.addIntegerParameter((String) entry.getKey(), (String) entry.getValue()); + } + } + + public ScriptedProducerNodeDefinition getDefinition() { + return nodeDefinition; + } + } + + private final static String NODE_DEFINITION_PARAMETER_NAME_ATTRIBUTE = "name"; + private final static String NODE_DEFINITION_PARAMETER_DEFAULTVALUE_ATTRIBUTE = "defaultvalue"; + private final static String[] NODE_DEFINITION_PARAMETER_REQUIRED_ATTRIBUTES = { NODE_DEFINITION_PARAMETER_NAME_ATTRIBUTE }; + private final static String[] NODE_DEFINITION_PARAMETER_OPTIONAL_ATTRIBUTES = { NODE_DEFINITION_PARAMETER_DEFAULTVALUE_ATTRIBUTE }; + private final static String[] NODE_DEFINITION_NODE_PARAMETER_OPTIONAL_ATTRIBUTES = { }; + + public class NodeDefinitionParametersSectionHandler implements XMLReader.SectionHandler { + private Map nodeParameters; + private Map stringParameters; + private Map integerParameters; + + public NodeDefinitionParametersSectionHandler() { + nodeParameters = new HashMap(); + stringParameters = new HashMap(); + integerParameters = new HashMap(); + } + + public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc { + String parameterName; + String defaultValue; + + if (aTag.equals("node")) { + XMLReaderTool.checkAttributes(anAttributes, + NODE_DEFINITION_PARAMETER_REQUIRED_ATTRIBUTES, + NODE_DEFINITION_NODE_PARAMETER_OPTIONAL_ATTRIBUTES); + parameterName = (String) anAttributes.get( + NODE_DEFINITION_PARAMETER_NAME_ATTRIBUTE); + + if (nodeParameters.containsKey(parameterName)) + throw new XMLReader.XMLReaderExc("Duplicate parameter name: '" + + parameterName + "'"); + + XMLReaderTool.checkValidIdentifier(parameterName); + + nodeParameters.put(parameterName, parameterName); + + return new EmptySectionHandler(); + } + else if (aTag.equals("string") || aTag.equals("integer")) { + XMLReaderTool.checkAttributes(anAttributes, + NODE_DEFINITION_PARAMETER_REQUIRED_ATTRIBUTES, + NODE_DEFINITION_PARAMETER_OPTIONAL_ATTRIBUTES); + parameterName = (String) anAttributes.get( + NODE_DEFINITION_PARAMETER_NAME_ATTRIBUTE); + + if (stringParameters.containsKey(parameterName) || + integerParameters.containsKey(parameterName)) + throw new XMLReader.XMLReaderExc("Duplicate parameter name: '" + + parameterName + "'"); + + XMLReaderTool.checkValidIdentifier(parameterName); + + defaultValue = (String) anAttributes.get( + NODE_DEFINITION_PARAMETER_DEFAULTVALUE_ATTRIBUTE); + + if (aTag.equals("string")) + stringParameters.put(parameterName, defaultValue); + else + integerParameters.put(parameterName, defaultValue); + + return new EmptySectionHandler(); + } + else + throw new XMLReader.XMLReaderExc( + "Only 'string', 'integer' and 'node' tags allowed here, '" + aTag + "' encountered."); + } + + public void endElement(XMLReader.SectionHandler aHandler) { + } + + public void finishSection() { + } + + public Map getNodeParameters() { + return nodeParameters; + } + + public Map getStringParameters() { + return stringParameters; + } + + public Map getIntegerParameters() { + return integerParameters; + } + } +} diff --git a/source/mir/producer/reader/ProducerNodeBuilder.java b/source/mir/producer/reader/ProducerNodeBuilder.java index 03532c12..5cb16fcb 100755 --- a/source/mir/producer/reader/ProducerNodeBuilder.java +++ b/source/mir/producer/reader/ProducerNodeBuilder.java @@ -35,9 +35,10 @@ import java.util.Map; import java.util.Set; import mir.producer.ProducerNode; +import mir.util.*; public interface ProducerNodeBuilder { - public void setAttributes(Map anAttributes) throws ProducerConfigExc; + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc; public void setSubNode(String aName, ProducerNode aNode) throws ProducerConfigExc; public Set getAvailableSubNodes() throws ProducerConfigExc; public ProducerNode constructNode() throws ProducerConfigExc; @@ -49,14 +50,14 @@ public interface ProducerNodeBuilder { public class DefaultProducerNodeBuilderFactory implements ProducerNodeBuilderFactory { private Class producerNodeBuilderClass; - public DefaultProducerNodeBuilderFactory(Class aProducerNodeBuilderClass) throws ProducerConfigExc { + public DefaultProducerNodeBuilderFactory(Class aProducerNodeBuilderClass) throws ProducerConfigExc, XMLReader.XMLReaderExc { if (!ProducerNodeBuilder.class.isAssignableFrom(aProducerNodeBuilderClass)) throw new ProducerConfigExc("supplied class is not a ProducerNodeBuilder class but a " + aProducerNodeBuilderClass.getClass().getName()); producerNodeBuilderClass = aProducerNodeBuilderClass; } - public ProducerNodeBuilder makeBuilder() throws ProducerConfigExc{ + public ProducerNodeBuilder makeBuilder() throws ProducerConfigExc { try { return (ProducerNodeBuilder) producerNodeBuilderClass.newInstance(); } diff --git a/source/mir/producer/reader/ReaderTool.java b/source/mir/producer/reader/ReaderTool.java deleted file mode 100755 index eb02254a..00000000 --- a/source/mir/producer/reader/ReaderTool.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (C) 2001, 2002 The Mir-coders group - * - * This file is part of Mir. - * - * Mir is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Mir is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Mir; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with the com.oreilly.servlet library, any library - * licensed under the Apache Software License, The Sun (tm) Java Advanced - * Imaging library (JAI), The Sun JIMI library (or with modified versions of - * the above that use the same license as the above), and distribute linked - * combinations including the two. You must obey the GNU General Public - * License in all respects for all of the code used other than the above - * mentioned libraries. If you modify this file, you may extend this exception - * to your version of the file, but you are not obligated to do so. If you do - * not wish to do so, delete this exception statement from your version. - */ - -package mir.producer.reader; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -public class ReaderTool { - - public static void checkValidIdentifier(String anIdentifier) throws ProducerConfigExc { - } - - public static String getStringAttributeWithDefault(Map anAttributes, String aKey, String aDefault) { - if (anAttributes.containsKey(aKey)) - return (String) anAttributes.get(aKey); - else - return aDefault; - } - - public static void checkIntegerAttribute(Map anAttributes, String aKey) throws ProducerConfigExc { - try { - Integer.parseInt((String) anAttributes.get(aKey)); - } - catch (Throwable t) { - throw new ProducerConfigExc("attribute '"+aKey+"' is not an integer" ); - } - } - - public static int getIntegerAttributeWithDefault(Map anAttributes, String aKey, int aDefault) throws ProducerConfigExc { - String value; - - if (anAttributes.containsKey(aKey)) { - checkIntegerAttribute(anAttributes, aKey); - return Integer.parseInt((String) anAttributes.get(aKey)); - } - else - return aDefault; - } - - public static void checkAttributes(Map anAttributes, String[] aRequiredAttributes, String[] anOptionalAttributes) throws ProducerConfigExc { - checkAttributeSet(anAttributes.keySet(), - new HashSet(Arrays.asList(aRequiredAttributes)), - new HashSet(Arrays.asList(anOptionalAttributes))); - } - - public static void checkAttributeSet(Set aSet, Set aRequiredElements, Set anOptionalElements) throws ProducerConfigExc{ - Iterator i; - - i = aSet.iterator(); - while (i.hasNext()) { - Object item = i.next(); - - if (!(aRequiredElements.contains(item) || anOptionalElements.contains(item))) - throw new ProducerConfigExc("unknown attribute '" + item + "'" ); - } - - i = aRequiredElements.iterator(); - while (i.hasNext()) { - Object item = i.next(); - - if (!(aSet.contains(item))) - throw new ProducerConfigExc("missing required attribute '" + item + "'" ); - } - - } -} \ No newline at end of file diff --git a/source/mir/util/XMLReader.java b/source/mir/util/XMLReader.java new file mode 100755 index 00000000..6b53f8e1 --- /dev/null +++ b/source/mir/util/XMLReader.java @@ -0,0 +1,319 @@ +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with the com.oreilly.servlet library, any library + * licensed under the Apache Software License, The Sun (tm) Java Advanced + * Imaging library (JAI), The Sun JIMI library (or with modified versions of + * the above that use the same license as the above), and distribute linked + * combinations including the two. You must obey the GNU General Public + * License in all respects for all of the code used other than the above + * mentioned libraries. If you modify this file, you may extend this exception + * to your version of the file, but you are not obligated to do so. If you do + * not wish to do so, delete this exception statement from your version. + */ + +package mir.util; + +import java.io.*; +import java.util.*; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.xml.sax.Attributes; +import org.xml.sax.InputSource; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; +import org.xml.sax.helpers.DefaultHandler; + +import multex.Failure; +import multex.Exc; + +public class XMLReader { + private Locator locator; + private String filename; + + public void parseFile(String aFileName, SectionHandler aRootHandler, List aUsedFiles) throws XMLReaderFailure, XMLReaderExc { + try { + SAXParserFactory parserFactory = SAXParserFactory.newInstance(); + + parserFactory.setNamespaceAware(false); + parserFactory.setValidating(true); + + XMLReaderHandler handler = new XMLReaderHandler(parserFactory, aRootHandler, aUsedFiles); + + handler.includeFile(aFileName); + } + catch (Throwable e) { + Throwable t = getRootCause(e); + + if (t instanceof XMLReaderExc) { + ((XMLReaderExc) t).setLocation(filename, locator.getLineNumber(), locator.getColumnNumber()); + throw (XMLReaderExc) t; + } + + if (t instanceof XMLReaderFailure) { + throw (XMLReaderFailure) t; + } + + throw new XMLReaderFailure(t); + } + } + + private Throwable getRootCause(Throwable t) { + if (t instanceof SAXParseException && ((SAXParseException) t).getException()!=null) { + return getRootCause(((SAXParseException) t).getException()); + } + else if (t instanceof Failure && ((Failure) t).getCause()!=null) { + return getRootCause(((Failure) t).getCause()); + } + else return t; + } + + private class XMLReaderHandler extends DefaultHandler { + private Stack includeFileStack; + private SAXParserFactory parserFactory; + private SectionsManager manager; + private List usedFiles; + private InputSource inputSource; + + public XMLReaderHandler(SAXParserFactory aParserFactory, SectionHandler aRootHandler, List aUsedFiles) { + super(); + + includeFileStack=new Stack(); + parserFactory=aParserFactory; + includeFileStack = new Stack(); + manager = new SectionsManager(); + usedFiles = aUsedFiles; + manager.pushHandler(aRootHandler); + } + + public String getLocatorDescription(Locator aLocator) { + return aLocator.getPublicId()+" ("+aLocator.getLineNumber()+")"; + } + + public void setDocumentLocator(Locator aLocator) { + locator=aLocator; + } + + private void includeFile(String aFileName) throws XMLReaderExc, XMLReaderFailure, SAXParseException, SAXException { + File file; + SAXParser parser; + + try { + if (!includeFileStack.empty()) + file = new File(new File((String) includeFileStack.peek()).getParent(), aFileName); + else + file = new File(aFileName); + + System.err.println("about to include "+file.getCanonicalPath()); + + if (includeFileStack.contains(file.getCanonicalPath())) { + throw new XMLReaderExc("recursive inclusion of file "+file.getCanonicalPath()); + } + + usedFiles.add(file); + + parser=parserFactory.newSAXParser(); + + inputSource = new InputSource(new FileInputStream(file)); + inputSource.setPublicId(file.getCanonicalPath()); + + includeFileStack.push(file.getCanonicalPath()); + filename = file.getCanonicalPath(); + try { + parser.parse(inputSource, this); + } + finally { + includeFileStack.pop(); + } + if (!includeFileStack.empty()) + filename = (String) includeFileStack.peek(); + } + catch (ParserConfigurationException e) { + throw new XMLReaderExc("Internal exception while including \""+aFileName+"\": "+e.getMessage()); + } + catch (SAXParseException e) { + throw e; + } + catch (XMLReaderFailure e) { + throw e; + } + catch (FileNotFoundException e) { + throw new XMLReaderExc("Include file \""+aFileName+"\" not found: "+e.getMessage()); + } + catch (IOException e) { + throw new XMLReaderExc("unable to open include file \""+aFileName+"\": "+e.getMessage()); + } + } + + public void startElement(String aUri, String aTag, String aQualifiedName, Attributes anAttributes) throws SAXException { + Map attributesMap; + int i; + + try { + if (aQualifiedName.equals("include")) { + String fileName=anAttributes.getValue("file"); + + if (fileName==null) { + throw new XMLReaderExc("include has no file attribute"); + } + + includeFile(fileName); + } + else { + attributesMap = new HashMap(); + for (i=0; i 0) { + throw new SAXParseException("Text not allowed", null, new XMLReaderExc("Text not allowed")); + } + } + } + + private class SectionsManager { + Stack handlerStack; + + public SectionsManager() { + handlerStack = new Stack(); + } + + public void pushHandler(SectionHandler aSectionHandler) { + handlerStack.push(aSectionHandler); + } + + public SectionHandler popHandler() { + return (SectionHandler) handlerStack.pop(); + } + + public SectionHandler currentHandler() { + return (SectionHandler) handlerStack.peek(); + } + + public boolean isEmpty() { + return handlerStack.isEmpty(); + } + } + + public static interface SectionHandler { + public abstract SectionHandler startElement(String aTag, Map anAttributes) throws XMLReaderExc; + + public abstract void endElement(SectionHandler aHandler) throws XMLReaderExc; + + public void finishSection() throws XMLReaderExc; + } + + public static abstract class AbstractSectionHandler implements SectionHandler { + public SectionHandler startElement(String aTag, Map anAttributes) throws XMLReaderExc { + return null; + }; + + public void endElement(SectionHandler aHandler) throws XMLReaderExc { + }; + + public void finishSection() throws XMLReaderExc { + } + } + + public static class XMLReaderExc extends Exc { + private boolean hasLocation; + private String filename; + private int lineNr; + private int columnNr; + + public XMLReaderExc(String aMessage) { + super(aMessage); + hasLocation = false; + } + + protected void setLocation(String aFilename, int aLineNr, int aColumnNr) { + filename = aFilename; + lineNr = aLineNr; + columnNr = aColumnNr; + hasLocation = true; + } + + public boolean getHasLocation() { + return hasLocation; + } + + public int getLineNr() { + return lineNr; + } + + public int getColumnNr() { + return columnNr; + } + + public String getFilename() { + return filename; + } + } + + public static class XMLReaderFailure extends Failure { + public XMLReaderFailure(String aMessage, Throwable aCause) { + super(aMessage, aCause); + } + + public XMLReaderFailure(Throwable aCause) { + super(aCause.getMessage(), aCause); + } + } + +} \ No newline at end of file diff --git a/source/mir/util/XMLReaderTool.java b/source/mir/util/XMLReaderTool.java new file mode 100755 index 00000000..95e4322d --- /dev/null +++ b/source/mir/util/XMLReaderTool.java @@ -0,0 +1,98 @@ +package mir.util; + +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with the com.oreilly.servlet library, any library + * licensed under the Apache Software License, The Sun (tm) Java Advanced + * Imaging library (JAI), The Sun JIMI library (or with modified versions of + * the above that use the same license as the above), and distribute linked + * combinations including the two. You must obey the GNU General Public + * License in all respects for all of the code used other than the above + * mentioned libraries. If you modify this file, you may extend this exception + * to your version of the file, but you are not obligated to do so. If you do + * not wish to do so, delete this exception statement from your version. + */ + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +public class XMLReaderTool { + + public static void checkValidIdentifier(String anIdentifier) throws XMLReader.XMLReaderExc { + } + + public static String getStringAttributeWithDefault(Map anAttributes, String aKey, String aDefault) { + if (anAttributes.containsKey(aKey)) + return (String) anAttributes.get(aKey); + else + return aDefault; + } + + public static void checkIntegerAttribute(Map anAttributes, String aKey) throws XMLReader.XMLReaderExc { + try { + Integer.parseInt((String) anAttributes.get(aKey)); + } + catch (Throwable t) { + throw new XMLReader.XMLReaderExc("attribute '"+aKey+"' is not an integer" ); + } + } + + public static int getIntegerAttributeWithDefault(Map anAttributes, String aKey, int aDefault) throws XMLReader.XMLReaderExc { + String value; + + if (anAttributes.containsKey(aKey)) { + checkIntegerAttribute(anAttributes, aKey); + return Integer.parseInt((String) anAttributes.get(aKey)); + } + else + return aDefault; + } + + public static void checkAttributes(Map anAttributes, String[] aRequiredAttributes, String[] anOptionalAttributes) throws XMLReader.XMLReaderExc { + checkAttributeSet(anAttributes.keySet(), + new HashSet(Arrays.asList(aRequiredAttributes)), + new HashSet(Arrays.asList(anOptionalAttributes))); + } + + public static void checkAttributeSet(Set aSet, Set aRequiredElements, Set anOptionalElements) throws XMLReader.XMLReaderExc{ + Iterator i; + + i = aSet.iterator(); + while (i.hasNext()) { + Object item = i.next(); + + if (!(aRequiredElements.contains(item) || anOptionalElements.contains(item))) + throw new XMLReader.XMLReaderExc("unknown attribute '" + item + "'" ); + } + + i = aRequiredElements.iterator(); + while (i.hasNext()) { + Object item = i.next(); + + if (!(aSet.contains(item))) + throw new XMLReader.XMLReaderExc("missing required attribute '" + item + "'" ); + } + + } +} diff --git a/source/mircoders/producer/reader/SupplementalProducerNodeBuilders.java b/source/mircoders/producer/reader/SupplementalProducerNodeBuilders.java index f8dc2685..3004000a 100755 --- a/source/mircoders/producer/reader/SupplementalProducerNodeBuilders.java +++ b/source/mircoders/producer/reader/SupplementalProducerNodeBuilders.java @@ -38,7 +38,8 @@ import mir.producer.ProducerNode; import mir.producer.reader.DefaultProducerNodeBuilders; import mir.producer.reader.ProducerConfigExc; import mir.producer.reader.ProducerNodeBuilderLibrary; -import mir.producer.reader.ReaderTool; +import mir.util.*; + import mircoders.producer.ContentMarkingProducerNode; import mircoders.producer.ContentModifyingProducerNode; import mircoders.producer.IndexingProducerNode; @@ -47,6 +48,7 @@ import mircoders.producer.PDFGeneratingProducerNode; import mircoders.producer.PDFPreFormattingProducerNode; import mircoders.producer.UnIndexingProducerNode; + public class SupplementalProducerNodeBuilders { public static void registerBuilders(ProducerNodeBuilderLibrary aBuilderLibrary, EntityAdapterModel aModel) throws ProducerConfigExc { @@ -73,8 +75,8 @@ public class SupplementalProducerNodeBuilders { super(MARKER_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, MARKER_REQUIRED_ATTRIBUTES, MARKER_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, MARKER_REQUIRED_ATTRIBUTES, MARKER_OPTIONAL_ATTRIBUTES); key = (String) anAttributes.get(MARKER_KEY_ATTRIBUTE); }; @@ -98,8 +100,8 @@ public class SupplementalProducerNodeBuilders { super(INDEXER_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, INDEXER_REQUIRED_ATTRIBUTES, INDEXER_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, INDEXER_REQUIRED_ATTRIBUTES, INDEXER_OPTIONAL_ATTRIBUTES); key = (String) anAttributes.get(INDEXER_KEY_ATTRIBUTE); pathToIndex = (String) anAttributes.get(INDEXER_INDEX_ATTRIBUTE); @@ -125,8 +127,8 @@ public class SupplementalProducerNodeBuilders { super(UNINDEXER_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, UNINDEXER_REQUIRED_ATTRIBUTES, UNINDEXER_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, UNINDEXER_REQUIRED_ATTRIBUTES, UNINDEXER_OPTIONAL_ATTRIBUTES); key = (String) anAttributes.get(UNINDEXER_KEY_ATTRIBUTE); pathToIndex = (String) anAttributes.get(UNINDEXER_INDEX_ATTRIBUTE); @@ -154,8 +156,8 @@ public class SupplementalProducerNodeBuilders { super(CONTENT_MODIFIER_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, CONTENT_MODIFIER_REQUIRED_ATTRIBUTES, CONTENT_MODIFIER_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, CONTENT_MODIFIER_REQUIRED_ATTRIBUTES, CONTENT_MODIFIER_OPTIONAL_ATTRIBUTES); key = (String) anAttributes.get(CONTENT_MODIFIER_KEY_ATTRIBUTE); field = (String) anAttributes.get(CONTENT_MODIFIER_FIELD_ATTRIBUTE); @@ -180,8 +182,8 @@ public class SupplementalProducerNodeBuilders { super(MEDIA_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, MEDIA_REQUIRED_ATTRIBUTES, MEDIA_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, MEDIA_REQUIRED_ATTRIBUTES, MEDIA_OPTIONAL_ATTRIBUTES); key = (String) anAttributes.get(MEDIA_KEY_ATTRIBUTE); }; @@ -213,8 +215,8 @@ public class SupplementalProducerNodeBuilders { super(MARKER_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, MARKER_REQUIRED_ATTRIBUTES, MARKER_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, MARKER_REQUIRED_ATTRIBUTES, MARKER_OPTIONAL_ATTRIBUTES); key = (String) anAttributes.get(MARKER_KEY_ATTRIBUTE); numLinesBetweenImages = (String) anAttributes.get(PDF_NUM_LINES_ATTRIBUTE); @@ -249,8 +251,8 @@ public class SupplementalProducerNodeBuilders { super(MARKER_SUBNODES); } - public void setAttributes(Map anAttributes) throws ProducerConfigExc { - ReaderTool.checkAttributes(anAttributes, MARKER_REQUIRED_ATTRIBUTES, MARKER_OPTIONAL_ATTRIBUTES); + public void setAttributes(Map anAttributes) throws ProducerConfigExc, XMLReader.XMLReaderExc { + XMLReaderTool.checkAttributes(anAttributes, MARKER_REQUIRED_ATTRIBUTES, MARKER_OPTIONAL_ATTRIBUTES); generator = (String) anAttributes.get(PDF_GENERATOR_ATTRIBUTE); destination = (String) anAttributes.get(PDF_DESTINATION_ATTRIBUTE);