From: Stefan T. <th...@us...> - 2002-02-14 17:53:58
|
Update of /cvsroot/xpg-xml/edu/iicm/xpg/statemachine In directory usw-pr-cvs1:/tmp/cvs-serv2060 Added Files: XMLHandler.java InitInitializeStateMachine.java Parser.java InitParser.java Log Message: new files for parsing --- NEW FILE: XMLHandler.java --- /*********************************************************************** * @(#)$RCSfile: XMLHandler.java,v $ $Revision: 1.1 $ $Date: 2002/02/14 17:53:48 $ * * Copyright (c) 2002 stefan thalauer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License (LGPL) * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This program 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ***********************************************************************/ package edu.iicm.xpg.statemachine; import org.xml.sax.Attributes; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler; import org.dinopolis.util.DFactory; //---------------------------------------------------------------------- /** * <p> * This class extends the DefaultHandler of the SAX2.0 API. * It sends the XMLInputs to the statemachine. * This class replaces the callback functionality of * the depricated classes: * - InitStatemachine * - Generator (Package: .xpg.generator) * * @author Stefan Thalauer * @version $revision$ */ public class XMLHandler extends DefaultHandler { protected PrimitiveStateMachine state_machine_; protected DataObject data_; //---------------------------------------------------------------------- /** * @param configfile the filename of the configfile * @param filename the filename of the inputfile */ public XMLHandler(PrimitiveStateMachine state_machine,DataObject data) { super(); state_machine_=state_machine; data_=data; } //---------------------------------------------------------------------- /** * @return */ public void startDocument() { state_machine_.initializeMachine(); } //---------------------------------------------------------------------- /** * @return */ public void endDocument() { try { state_machine_.input(new XMLInput(Const.XML_END_DOCUMENT,null)); } catch(Exception exc) { exc.printStackTrace(); } } //---------------------------------------------------------------------- /** * @param chars the character array * @param start start index of the char array to work with * @param length number of chars in the char array to work with * @return */ public void characters(char[] chars,int start,int length) { try { // FIXXME (Stefan Thalauer, Feb11 2002 17:32) -> why pop element and than push the characters on the element_stack ??? // data_.popElement(); // data_.pushElement(new String(chars)); // END FIXXME (Stefan Thalauer, Feb11 2002 17:35) state_machine_.input(new XMLInput(Const.XML_CHARS,chars,start,length)); } catch(Exception exc) { exc.printStackTrace(); System.exit(1); } } //---------------------------------------------------------------------- /** * @param uri element uri * @param name element name * @param qname ??? * @param attributes element attributes * @return */ public void startElement(String uri,String name,String qname,Attributes attributes) { try { data_.pushElement(name); data_.pushAttributes(attributes); state_machine_.input(new XMLInput(Const.XML_START_TAG,name)); } catch(Exception exc) { exc.printStackTrace(); } } //---------------------------------------------------------------------- /** * @param uri element uri * @param name element name * @param qname ??? * @return */ public void endElement(String uri,String name,String qname) { try { state_machine_.input(new XMLInput(Const.XML_END_TAG,name)); data_.popAttributes(); data_.popElement(); } catch(Exception exc) { exc.printStackTrace(); } } //---------------------------------------------------------------------- /** * @param ch The whitespace characters. * @param start The start position in the character array. * @param length The number of characters tcharacter array. */ public void ignorableWhitespace(char[] ch,int start,int length) { } //---------------------------------------------------------------------- /** * @param exc the error that occured * @return */ public void error(SAXParseException exc) { System.err.println("SAX Error: " + ", line " + exc.getLineNumber() + "\n, uri " + exc.getSystemId()); System.err.println(" " + exc.getMessage()); } //---------------------------------------------------------------------- /** * @param exc the error that occured * @return */ public void fatalError(SAXParseException exc) { System.err.println("SAX Fatal Error: " + ", line " + exc.getLineNumber() + "\n, uri " + exc.getSystemId()); System.err.println(" " + exc.getMessage()); exc.printStackTrace(); System.exit(-2); } //---------------------------------------------------------------------- /** * @param exc the warning that occured * @return */ public void warning(SAXParseException exc) { System.err.println("SAX Warning: " + ", line " + exc.getLineNumber() + "\n, uri " + exc.getSystemId()); System.err.println(" " + exc.getMessage()); exc.printStackTrace(); } //---------------------------------------------------------------------- /** * @return the object that has been generated */ public synchronized Object getResult() { return(data_.getResult().toString()); } } --- NEW FILE: InitInitializeStateMachine.java --- /*********************************************************************** * @(#)$RCSfile: InitInitializeStateMachine.java,v $ $Revision: 1.1 $ $Date: 2002/02/14 17:53:48 $ * * Copyright (c) 2002 stefan thalauer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License (LGPL) * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This program 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ***********************************************************************/ package edu.iicm.xpg.statemachine; import edu.iicm.xpg.transitions.PrimitiveTransition; import edu.iicm.xpg.transitions.StopMachineTransition; import edu.iicm.xpg.transitions.IgnoreCharsTransition; import edu.iicm.xpg.transitions.RegisterStartStateTransition; import edu.iicm.xpg.transitions.RegisterStateTransition; import edu.iicm.xpg.transitions.StoreDataTransition; import edu.iicm.xpg.transitions.RegisterTransitionTransition; import edu.iicm.xpg.transitions.SetSearchPathTransition; //---------------------------------------------------------------------- /** * This class implements the Initializer Interface. * It register the States and Transitions for the * Statemachine which is used to initialize the * Document Statemachine. * The States and Transisitions are hardcoded in this class. * The Initialized Statemachine is able to process a * statemachine config file and initialize the * Document Statemachine. * * @author Stefan Thalauer * @version $Revision: 1.1 $ */ public class InitInitializeStateMachine implements Initializer { protected PrimitiveStateMachine state_machine_; // elements public final static String XML_STM_CONFIG = "statemachine"; public final static String XML_PATH_DEF = "path"; public final static String XML_STATE_LIST = "states"; public final static String XML_START_STATE_DEF = "startstate"; public final static String XML_STATE_DEF = "state"; public final static String XML_TRANSITION_LIST = "transitions"; public final static String XML_TRANSITION_DEF = "transition"; public final static String XML_BEGINSTATE = "beginstate"; public final static String XML_NEXTSTATE = "nextstate"; public final static String XML_ELEMENT = "element"; public final static String XML_CLASSNAME = "classname"; // machine states public final static String STATE_TOP_LEVEL_FILE = "file top"; public final static String STATE_TOP_LEVEL_DOC_TYPE = "doctype top"; public final static String STATE_PATH_DEF = "path def"; public final static String STATE_STATE_LIST = "state list"; public final static String STATE_START_STATE_DEF = "start state def"; public final static String STATE_STATE_DEF = "state def"; public final static String STATE_TRANSITION_LIST = "transition list"; public final static String STATE_TRANSITION_DEF = "transition def"; public final static String STATE_BEGINSTATE = "beginstate"; public final static String STATE_NEXTSTATE = "nextstate"; public final static String STATE_ELEMENT = "element"; public final static String STATE_CLASSNAME = "classname"; public final static String STATE_FINISHED = "finished"; public InitInitializeStateMachine() { state_machine_=null; } //---------------------------------------------------------------------- /** * @return the initialized Statemachine */ public PrimitiveStateMachine getStateMachine() { return state_machine_; } //---------------------------------------------------------------------- /** * @param filename a file * @return * @deprecated this function uses the wrong param list */ public void initialize(String filename) { System.err.println("void initialize(String filename) is DEPRICATED!"); } //---------------------------------------------------------------------- /** * The following state machine is built here: Init a Statemachine * * @param state_machine the statemachine * @return */ public void initialize(PrimitiveStateMachine state_machine) { state_machine_ = state_machine; // state_machine_.setUserDefinedDataObject(data_); state_machine_.registerState(new PrimitiveState(),STATE_TOP_LEVEL_FILE); state_machine_.registerState(new PrimitiveState(),STATE_TOP_LEVEL_DOC_TYPE); state_machine_.registerState(new PrimitiveState(),STATE_PATH_DEF); state_machine_.registerState(new PrimitiveState(),STATE_STATE_LIST); state_machine_.registerState(new PrimitiveState(),STATE_START_STATE_DEF); state_machine_.registerState(new PrimitiveState(),STATE_STATE_DEF); state_machine_.registerState(new PrimitiveState(),STATE_TRANSITION_LIST); state_machine_.registerState(new PrimitiveState(),STATE_TRANSITION_DEF); state_machine_.registerState(new PrimitiveState(),STATE_BEGINSTATE); state_machine_.registerState(new PrimitiveState(),STATE_NEXTSTATE); state_machine_.registerState(new PrimitiveState(),STATE_ELEMENT); state_machine_.registerState(new PrimitiveState(),STATE_CLASSNAME); state_machine_.registerState(new PrimitiveState(),STATE_FINISHED); state_machine_.setStartState(STATE_TOP_LEVEL_FILE); try { // STATE_TOP_LEVEL_FILE state_machine_.registerTransition(new XMLInput(Const.XML_START_TAG,XML_STM_CONFIG), new PrimitiveTransition(), STATE_TOP_LEVEL_FILE, STATE_TOP_LEVEL_DOC_TYPE); state_machine_.registerTransition(new XMLInput(Const.XML_END_DOCUMENT,null), new StopMachineTransition(), STATE_TOP_LEVEL_FILE, STATE_FINISHED); state_machine_.registerDefaultTransition(new IgnoreCharsTransition(), STATE_TOP_LEVEL_FILE, STATE_TOP_LEVEL_FILE); // STATE_TOP_LEVEL_DOC_TYPE state_machine_.registerTransition(new XMLInput(Const.XML_END_TAG,XML_STM_CONFIG), new PrimitiveTransition(), STATE_TOP_LEVEL_DOC_TYPE, STATE_TOP_LEVEL_FILE); state_machine_.registerTransition(new XMLInput(Const.XML_START_TAG,XML_PATH_DEF), new PrimitiveTransition(), STATE_TOP_LEVEL_DOC_TYPE, STATE_PATH_DEF); state_machine_.registerTransition(new XMLInput(Const.XML_START_TAG,XML_STATE_LIST), new PrimitiveTransition(), STATE_TOP_LEVEL_DOC_TYPE, STATE_STATE_LIST); state_machine_.registerTransition(new XMLInput(Const.XML_START_TAG,XML_TRANSITION_LIST), new PrimitiveTransition(), STATE_TOP_LEVEL_DOC_TYPE, STATE_TRANSITION_LIST); state_machine_.registerDefaultTransition(new IgnoreCharsTransition(), STATE_TOP_LEVEL_DOC_TYPE, STATE_TOP_LEVEL_DOC_TYPE); // STATE_PATH_DEF state_machine_.registerTransition(new XMLInput(Const.XML_END_TAG,XML_PATH_DEF), new PrimitiveTransition(), STATE_PATH_DEF, STATE_TOP_LEVEL_DOC_TYPE); state_machine_.registerDefaultTransition(new SetSearchPathTransition(), STATE_PATH_DEF, STATE_PATH_DEF); // STATE_STATE_LIST state_machine_.registerTransition(new XMLInput(Const.XML_END_TAG,XML_STATE_LIST), new PrimitiveTransition(), STATE_STATE_LIST, STATE_TOP_LEVEL_DOC_TYPE); state_machine_.registerTransition(new XMLInput(Const.XML_START_TAG,XML_START_STATE_DEF), new PrimitiveTransition(), STATE_STATE_LIST, STATE_START_STATE_DEF); state_machine_.registerTransition(new XMLInput(Const.XML_START_TAG,XML_STATE_DEF), new PrimitiveTransition(), STATE_STATE_LIST, STATE_STATE_DEF); state_machine_.registerDefaultTransition(new IgnoreCharsTransition(), STATE_STATE_LIST, STATE_STATE_LIST); // STATE_START_STATE_DEF state_machine_.registerTransition(new XMLInput(Const.XML_END_TAG,XML_START_STATE_DEF), new PrimitiveTransition(), STATE_START_STATE_DEF, STATE_STATE_LIST); state_machine_.registerDefaultTransition(new RegisterStartStateTransition(), STATE_START_STATE_DEF, STATE_START_STATE_DEF); // STATE_STATE_DEF state_machine_.registerTransition(new XMLInput(Const.XML_END_TAG,XML_STATE_DEF), new PrimitiveTransition(), STATE_STATE_DEF, STATE_STATE_LIST); state_machine_.registerDefaultTransition(new RegisterStateTransition(), STATE_STATE_DEF, STATE_STATE_DEF); // STATE_TRANSITION_LIST state_machine_.registerTransition(new XMLInput(Const.XML_END_TAG,XML_TRANSITION_LIST), new PrimitiveTransition(), STATE_TRANSITION_LIST, STATE_TOP_LEVEL_DOC_TYPE); state_machine_.registerTransition(new XMLInput(Const.XML_START_TAG,XML_TRANSITION_DEF), new PrimitiveTransition(), STATE_TRANSITION_LIST, STATE_TRANSITION_DEF); state_machine_.registerDefaultTransition(new IgnoreCharsTransition(), STATE_TRANSITION_LIST, STATE_TRANSITION_LIST); // STATE_TRANSITION_DEF state_machine_.registerTransition(new XMLInput(Const.XML_END_TAG,XML_TRANSITION_DEF), new RegisterTransitionTransition(), STATE_TRANSITION_DEF, STATE_TRANSITION_LIST); state_machine_.registerTransition(new XMLInput(Const.XML_START_TAG,XML_BEGINSTATE), new PrimitiveTransition(), STATE_TRANSITION_DEF, STATE_BEGINSTATE); state_machine_.registerTransition(new XMLInput(Const.XML_START_TAG,XML_NEXTSTATE), new PrimitiveTransition(), STATE_TRANSITION_DEF, STATE_NEXTSTATE); state_machine_.registerTransition(new XMLInput(Const.XML_START_TAG,XML_ELEMENT), new PrimitiveTransition(), STATE_TRANSITION_DEF, STATE_ELEMENT); state_machine_.registerTransition(new XMLInput(Const.XML_START_TAG,XML_CLASSNAME), new PrimitiveTransition(), STATE_TRANSITION_DEF, STATE_CLASSNAME); state_machine_.registerDefaultTransition(new IgnoreCharsTransition(), STATE_TRANSITION_DEF, STATE_TRANSITION_DEF); // STATE_BEGINSTATE state_machine_.registerTransition(new XMLInput(Const.XML_END_TAG,XML_BEGINSTATE), new PrimitiveTransition(), STATE_BEGINSTATE, STATE_TRANSITION_DEF); state_machine_.registerDefaultTransition(new StoreDataTransition(StoreDataTransition.BEGINSTATE), STATE_BEGINSTATE, STATE_BEGINSTATE); // STATE_NEXTSTATE state_machine_.registerTransition(new XMLInput(Const.XML_END_TAG,XML_NEXTSTATE), new PrimitiveTransition(), STATE_NEXTSTATE, STATE_TRANSITION_DEF); state_machine_.registerDefaultTransition(new StoreDataTransition(StoreDataTransition.NEXTSTATE), STATE_NEXTSTATE, STATE_NEXTSTATE); // STATE_ELEMENT state_machine_.registerTransition(new XMLInput(Const.XML_END_TAG,XML_ELEMENT), new PrimitiveTransition(), STATE_ELEMENT, STATE_TRANSITION_DEF); state_machine_.registerDefaultTransition(new StoreDataTransition(StoreDataTransition.ELEMENT), STATE_ELEMENT, STATE_ELEMENT); // STATE_CLASSNAME state_machine_.registerTransition(new XMLInput(Const.XML_END_TAG,XML_CLASSNAME), new PrimitiveTransition(), STATE_CLASSNAME, STATE_TRANSITION_DEF); state_machine_.registerDefaultTransition(new StoreDataTransition(StoreDataTransition.CLASSNAME), STATE_CLASSNAME, STATE_CLASSNAME); } catch (Exception exc) { exc.printStackTrace(); } } } --- NEW FILE: Parser.java --- /*********************************************************************** * @(#)$RCSfile: Parser.java,v $ $Revision: 1.1 $ $Date: 2002/02/14 17:53:48 $ * * Copyright (c) 2002 stefan thalauer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License (LGPL) * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This program 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ***********************************************************************/ package edu.iicm.xpg.statemachine; import java.io.FileReader; import java.io.File; import org.xml.sax.XMLReader; import org.xml.sax.InputSource; import org.xml.sax.helpers.XMLReaderFactory; import org.xml.sax.SAXException; import org.dinopolis.util.DFactory; //---------------------------------------------------------------------- /** * @author Stefan Thalauer * @version $revision$ */ public class Parser { protected DataObject data_; protected XMLReader reader_; protected XMLHandler handler_; protected PrimitiveStateMachine state_machine_; protected final static String XML_READER = "org.apache.xerces.parsers.SAXParser"; protected final static String PARSER_FEATURE_SCHEMA = "http://xml.org/sax/features/validation"; protected final static String PARSER_FEATURE_VALIDATING = "http://apache.org/xml/features/validation/schema"; protected final static String PARSER_FEATURE_VALIDATING_DYNAMIC ="http://apache.org/xml/features/validation/dynamic"; protected final static String STATE_MACHINE="doc_state_machine"; protected final static String TRANSITION_FATORY="transition_factory"; //---------------------------------------------------------------------- /** */ public Parser() { data_ = new DataObject(); DFactory transition_factory = new DFactory(); data_.putObject(TRANSITION_FATORY, transition_factory); try { reader_ = XMLReaderFactory.createXMLReader(XML_READER); } catch (SAXException exc) { exc.printStackTrace(); } } //---------------------------------------------------------------------- /** * @param state_machine */ public Parser(PrimitiveStateMachine state_machine) { this(); state_machine_=state_machine; state_machine_.setUserDefinedDataObject(data_); handler_ = new XMLHandler(state_machine_,data_); } //---------------------------------------------------------------------- /** * Parse a file , calls the XMLHandler * @param file the xml file to parse * @return public */ public void parseFile(String filename) { File input_file = new File(filename); if (!input_file.exists()) { System.err.println("Parser: File " + filename + " not existing"); System.exit(-1); } try { FileReader file_reader = new FileReader(input_file); reader_.parse(new InputSource(file_reader)); } catch (Exception exc) { System.err.println(exc); exc.printStackTrace(); System.exit(1); } } //---------------------------------------------------------------------- /** * @return the object that has been generated */ public synchronized Object getResult() { return(data_.getResult().toString()); } //---------------------------------------------------------------------- /** * This was just a test function * @return the object that has been generated */ // public synchronized PrimitiveStateMachine getStateMachine() // { // return (PrimitiveStateMachine) data_.getObject(STATE_MACHINE); // } //---------------------------------------------------------------------- /** * @return */ public void setHandlers(XMLHandler handler) { reader_.setContentHandler(handler); reader_.setErrorHandler(handler); reader_.setDTDHandler(handler); } //---------------------------------------------------------------------- /** * @return */ public void setHandlers() { reader_.setContentHandler(handler_); reader_.setErrorHandler(handler_); reader_.setDTDHandler(handler_); } //---------------------------------------------------------------------- /** * @param * @return */ public void setFeatures() { try { reader_.setFeature(PARSER_FEATURE_VALIDATING, true); } catch (SAXException e) { System.err.println("Cannot activate validation."); } try { reader_.setFeature(PARSER_FEATURE_VALIDATING_DYNAMIC, true); } catch (SAXException e) { System.err.println("Cannot activate Dynamic validation."); } try { reader_.setFeature(PARSER_FEATURE_SCHEMA, true); } catch (SAXException e) { System.err.println("Cannot activate SCHEMA validation."); } } } --- NEW FILE: InitParser.java --- /*********************************************************************** * @(#)$RCSfile: InitParser.java,v $ $Revision: 1.1 $ $Date: 2002/02/14 17:53:48 $ * * Copyright (c) 2002 stefan thalauer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License (LGPL) * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This program 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ***********************************************************************/ package edu.iicm.xpg.statemachine; import org.xml.sax.helpers.XMLReaderFactory; import org.xml.sax.SAXException; import org.dinopolis.util.DFactory; //---------------------------------------------------------------------- /** * @author Stefan Thalauer * @version $revision$ */ public class InitParser extends Parser implements Initializer { protected String config_file_; protected Initializer initializer_; //---------------------------------------------------------------------- /** * @param config_file */ public InitParser(String config_file) { super(); config_file_= config_file; initializer_ = new InitInitializeStateMachine(); state_machine_ = new PrimitiveStateMachine(); handler_ = new XMLHandler(state_machine_,data_); } //---------------------------------------------------------------------- /** * The following state machine is built here: * * @return * @exception Exception whatever can happen during initialization */ public void initialize(PrimitiveStateMachine new_state_machine) { setFeatures(); setHandlers(); initializer_.initialize(state_machine_); state_machine_=initializer_.getStateMachine(); data_.putObject(STATE_MACHINE,new_state_machine); state_machine_.setUserDefinedDataObject(data_); parseFile(config_file_); } //---------------------------------------------------------------------- /** * @return the initialized Statemachine */ public PrimitiveStateMachine getStateMachine() { return (PrimitiveStateMachine) data_.getObject(STATE_MACHINE); } //---------------------------------------------------------------------- /** * The following state machine is built here: * * @return * @deprecated * @exception Exception whatever can happen during initialization */ public void initialize(String filename) // throws Exception { System.err.println("void initialize(String filename) is DEPRICATED!"); } } |