I have written a parser in java for parsing a XMI file...I want the content i fetched to be stored in a hash table..Im working on it not getting how to store the Id and name in a hash table...please help
XMI parser written is as follows
import java.io.*; import java.io.IOException; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; class States { private String sxmiid; private String sname; public States() { } public States(String sxmiid,String sname) { this.sxmiid = sxmiid; this.sname = sname; } public String getSxmiid() { return sxmiid; } public void setSxmiid(String sxmiid) { this.sxmiid = sxmiid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String toString() { StringBuffer sb = new StringBuffer(); sb.append("State id:" + getSxmiid()); sb.append(", "); sb.append("State name:" + getSname()); sb.append(". "); return sb.toString(); } } class Events { private String exmiid; private String ename; private String esource; private String etarget; public Events(){ } public Events(String exmiid,String ename,String esource,String etarget) { this.exmiid = exmiid; this.ename = ename; this.esource = esource; this.etarget = etarget; } public String getExmiid() { return exmiid; } public void setExmiid(String exmiid) { this.exmiid = exmiid; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getEsource() { return esource; } public void setEsource(String esource) { this.esource = esource; } public String getEtarget() { return etarget; } public void setEtarget(String etarget) { this.etarget = etarget; } public String toString() { StringBuffer sb = new StringBuffer(); sb.append("Event id:" + getExmiid()); sb.append(", "); sb.append("Event name:" + getEname()); sb.append(". "); return sb.toString(); } } public class XMIParser extends DefaultHandler { //List myEmpls; List myStates; List myEvents; String file; private String tempVal; private States tempStates; private Events tempEvents; public XMIParser() { myStates = new ArrayList(); myEvents = new ArrayList(); } public void runParser() throws IOException { parseDocument(); printData(); printFile(); } private void parseDocument() { //get a factory SAXParserFactory spf = SAXParserFactory.newInstance(); try { //get a new instance of parser SAXParser sp = spf.newSAXParser(); //parse the file and also register this class for call backs sp.parse("cwm.xmi",this); } catch(SAXException se) { se.printStackTrace(); } catch(ParserConfigurationException pce) { pce.printStackTrace(); } catch (IOException ie) { ie.printStackTrace(); } } /** * Iterate through the list and print * the contents */ private void printData() { //System.out.println("STATES_BEGIN"); file="STATES_BEGIN\r\n\r\n"; Iterator its = myStates.iterator(); while(its.hasNext()) { //System.out.println(its.next().toString()); file=file+its.next().toString()+"\r\n"; } //System.out.println("STATES_END\n\n"); file=file+"\r\nSTATES_END\r\n\r\n\r\n"; //System.out.println("TRANSITION_BEGIN"); file=file+"TRANSITION_BEGIN\r\n\r\n"; Iterator ite = myEvents.iterator(); while(ite.hasNext()) { //System.out.println(ite.next().toString()); file=file+ite.next().toString()+"\r\n"; } //System.out.println("TRANSITION_END\n\n"); file=file+"\r\nTRANSITION_END\r\n"; } private void printFile() throws FileNotFoundException,IOException { byte buff[]=file.getBytes(); OutputStream fo=new FileOutputStream("file2.txt"); for(int i=0;i<buff.length;i++) fo.write(buff[i]); fo.close(); } //Event Handlers public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { tempVal = ""; if(qName.equalsIgnoreCase("subvertex")) { tempStates = new States(); tempStates.setSxmiid(attributes.getValue("xmi:id")); tempStates.setSname(attributes.getValue("name")); } else if(qName.equalsIgnoreCase("transition")) { tempEvents = new Events(); tempEvents.setExmiid(attributes.getValue("xmi:id")); tempEvents.setEname(attributes.getValue("name")); } } public void characters(char[] ch, int start, int length) throws SAXException { tempVal = new String(ch,start,length); } public void endElement(String uri, String localName, String qName) throws SAXException { if(qName.equalsIgnoreCase("subvertex")) { myStates.add(tempStates); } else if (qName.equalsIgnoreCase("transition")) { myEvents.add(tempEvents); } } public static void main(String[] args)throws IOException{ XMIParser xp = new XMIParser(); xp.runParser(); } } /*end of XMI parser*/
The XMI file parsed is shown below
cwm.xmi
The content I fetched is stored in the file2.txt<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.1" xmlns:uml="http://schema.omg.org/spec/UML/2.1" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1">
<xmi:Documentation exporter="Bouml" exporterVersion="1.14.1"/>
<uml:Model xmi:type="uml:Model" xmi:id="themodel" name="cwm">
<packagedElement xmi:type="uml:Package" xmi:id="BOUML_0x81_22" name ="cwm">
<packagedElement xmi:type="uml:StateMachine" xmi:id="BOUML_0x1f464_23" name="Coffee">
<region xmi:type="uml:Region" xmi:id="BOUML_IMPLICIT_REGION_0x1f464_23" name="Bouml_Implicit_Region">
<subvertex xmi:type="uml:State" xmi:id="BOUML_0x1f4e4_23" name="Idle">
<incoming xmi:idref="BOUML_0x1f864_24"/>
<incoming xmi:idref="BOUML_0x22be4_24"/>
<incoming xmi:idref="BOUML_0x1f464_24"/>
<region xmi:type="uml:Region" xmi:id="BOUML_IMPLICIT_REGION_0x1f4e4_23" name="Bouml_Implicit_Region">
<outgoing xmi:idref="BOUML_0x1f4e4_24"/>
</region>
</subvertex>
<subvertex xmi:type="uml:State" xmi:id="BOUML_0x1f564_23" name="Waiting">
<incoming xmi:idref="BOUML_0x1f4e4_24"/>
<region xmi:type="uml:Region" xmi:id="BOUML_IMPLICIT_REGION_0x1f564_23" name="Bouml_Implicit_Region">
<outgoing xmi:idref="BOUML_0x1f564_24"/>
</region>
</subvertex>
<subvertex xmi:type="uml:State" xmi:id="BOUML_0x1f5e4_23" name="Verification">
<incoming xmi:idref="BOUML_0x1f564_24"/>
<region xmi:type="uml:Region" xmi:id="BOUML_IMPLICIT_REGION_0x1f5e4_23" name="Bouml_Implicit_Region">
<outgoing xmi:idref="BOUML_0x1f5e4_24"/>
<outgoing xmi:idref="BOUML_0x1f764_24"/>
</region>
</subvertex>
<subvertex xmi:type="uml:State" xmi:id="BOUML_0x1f664_23" name="Valve open">
<incoming xmi:idref="BOUML_0x1f5e4_24"/>
<region xmi:type="uml:Region" xmi:id="BOUML_IMPLICIT_REGION_0x1f664_23" name="Bouml_Implicit_Region">
<outgoing xmi:idref="BOUML_0x1f664_24"/>
</region>
</subvertex>
<subvertex xmi:type="uml:State" xmi:id="BOUML_0x1f6e4_23" name="Valve close">
<incoming xmi:idref="BOUML_0x1f664_24"/>
<region xmi:type="uml:Region" xmi:id="BOUML_IMPLICIT_REGION_0x1f6e4_23" name="Bouml_Implicit_Region">
<outgoing xmi:idref="BOUML_0x1f864_24"/>
</region>
</subvertex>
<subvertex xmi:type="uml:State" xmi:id="BOUML_0x1f764_23" name="Error">
<incoming xmi:idref="BOUML_0x1f764_24"/>
<region xmi:type="uml:Region" xmi:id="BOUML_IMPLICIT_REGION_0x1f764_23" name="Bouml_Implicit_Region">
<outgoing xmi:idref="BOUML_0x22be4_24"/>
</region>
</subvertex>
<subvertex xmi:type="uml:Pseudostate" xmi:id="BOUML_0x1f464_28" kind="initial">
<outgoing xmi:idref="BOUML_0x1f464_24"/>
</subvertex>
</region>
<transition xmi:type="uml:Transition" xmi:id="BOUML_0x1f4e4_24" name="Inserting 1st coin" source="BOUML_0x1f4e4_23" target="BOUML_0x1f564_23">
</transition>
<transition xmi:type="uml:Transition" xmi:id="BOUML_0x1f564_24" name="Inserting 2nd coin" source="BOUML_0x1f564_23" target="BOUML_0x1f5e4_23">
</transition>
<transition xmi:type="uml:Transition" xmi:id="BOUML_0x1f5e4_24" name="Signaling valve" source="BOUML_0x1f5e4_23" target="BOUML_0x1f664_23">
</transition>
<transition xmi:type="uml:Transition" xmi:id="BOUML_0x1f764_24" name="Inserting wrong coin" source="BOUML_0x1f5e4_23" target="BOUML_0x1f764_23">
</transition>
<transition xmi:type="uml:Transition" xmi:id="BOUML_0x1f664_24" name="Supply of coffee" source="BOUML_0x1f664_23" target="BOUML_0x1f6e4_23">
</transition>
<transition xmi:type="uml:Transition" xmi:id="BOUML_0x1f864_24" name="System signal" source="BOUML_0x1f6e4_23" target="BOUML_0x1f4e4_23">
</transition>
<transition xmi:type="uml:Transition" xmi:id="BOUML_0x22be4_24" name="reset signal" source="BOUML_0x1f764_23" target="BOUML_0x1f4e4_23">
</transition>
<transition xmi:type="uml:Transition" xmi:id="BOUML_0x1f464_24" name="process start" source="BOUML_0x1f464_28" target="BOUML_0x1f4e4_23">
</transition>
</packagedElement>
</packagedElement>
</uml:Model>
</xmi:XMI>
/*end of xmi file*/
the file is shown below
STATES_BEGIN
State id:BOUML_0x1f4e4_23, State name:Idle.
State id:BOUML_0x1f564_23, State name:Waiting.
State id:BOUML_0x1f5e4_23, State name:Verification.
State id:BOUML_0x1f664_23, State name:Valve open.
State id:BOUML_0x1f6e4_23, State name:Valve close.
State id:BOUML_0x1f764_23, State name:Error.
State id:BOUML_0x1f464_28, State name:null.
STATES_END
TRANSITION_BEGIN
Event id:BOUML_0x1f4e4_24, Event name:Inserting 1st coin.
Event id:BOUML_0x1f564_24, Event name:Inserting 2nd coin.
Event id:BOUML_0x1f5e4_24, Event name:Signaling valve.
Event id:BOUML_0x1f764_24, Event name:Inserting wrong coin.
Event id:BOUML_0x1f664_24, Event name:Supply of coffee.
Event id:BOUML_0x1f864_24, Event name:System signal.
Event id:BOUML_0x22be4_24, Event name:reset signal.
Event id:BOUML_0x1f464_24, Event name:process start.
TRANSITION_END
/*end of file2.txt*/