001: // The UMLet source code is distributed under the terms of the GPL; see license.txt
002: package com.umlet.control;
003:
004: import javax.swing.*;
005:
006: import org.xml.sax.*;
007: import org.xml.sax.helpers.*;
008: import com.umlet.element.base.*;
009: import com.umlet.control.io.*;
010: import java.io.*;
011: import java.util.ArrayList;
012: import java.util.List;
013:
014: //TODO read the helppanel
015: public class XMLContentHandler extends DefaultHandler {
016: private JPanel _p = null;
017: private Entity e = null;
018: private String elementtext;
019: private int x;
020: private int y;
021: private int w;
022: private int h;
023:
024: private int customTypesLoaded = 0;
025: private int customTypesLoadedWError = 0;
026: private Group currentGroup;
027:
028: // to be backward compatible - add list of old elements that were removed so that they are ignored when loading old files
029: private List<String> ignoreElements;
030:
031: public XMLContentHandler(JPanel p) {
032: _p = p;
033: ignoreElements = new ArrayList<String>();
034: ignoreElements.add("com.umlet.control.Group");
035: currentGroup = null;
036: }
037:
038: public void startElement(java.lang.String uri,
039: java.lang.String localName, java.lang.String qName,
040: Attributes attributes) {
041: elementtext = "";
042: if (qName.equals("group")) {
043: Group g = new Group();
044: if (currentGroup != null)
045: currentGroup.addMember(g);
046: currentGroup = g;
047: }
048: }
049:
050: public void endElement(java.lang.String uri,
051: java.lang.String localName, java.lang.String qName) {
052: String elementname = qName; //[UB]: we are not name-space aware, so use the qualified name
053:
054: if (elementname.equals("help_text")) {
055: Controller.getInstance().executeCommand(
056: new HelpPanelChanged(elementtext));
057: } else if (elementname.equals("group")) {
058: if (this .currentGroup != null) {
059: this .currentGroup.adjustSize();
060: _p.add(this .currentGroup);
061: this .currentGroup = this .currentGroup.getGroup();
062: }
063: } else if (elementname.equals("element")) {
064: if (e != null) {
065: if (this .currentGroup != null)
066: this .currentGroup.addMember(e);
067: _p.add(e);
068: }
069: } else if (elementname.equals("type")) {
070: java.lang.Class c = null;
071:
072: try {
073: //LME3 use own class loader
074: String elementPath = elementtext.replace('.',
075: File.separatorChar);
076: String homePath = Umlet.getInstance().getHomePath();
077: File classFile = new File(homePath
078: + Umlet.CUSTOM_ELEMENTS_PATH + elementPath
079: + ".class");
080: File javaFile = new File(homePath
081: + Umlet.CUSTOM_ELEMENTS_PATH + elementPath
082: + ".java");
083: if (classFile.exists() || javaFile.exists()) {
084: String[] fileNameStrArr = {
085: homePath + Umlet.CUSTOM_ELEMENTS_PATH
086: + elementPath + ".java",
087: elementtext };
088: e = CustomElementLoader.getInstance().doLoadClass(
089: fileNameStrArr, false, true); //compile and load the modified class, silent error reporting
090: customTypesLoaded++; //LME4 count loaded classes
091: customTypesLoadedWError = CustomElementLoader
092: .getInstance().getErrorCounter(); //count errors while loading classes
093: } else if (!this .ignoreElements.contains(elementtext)) { //load jar-classes conventianally
094:
095: c = java.lang.Class.forName(elementtext);
096: e = (Entity) c.newInstance();
097: } else
098: e = null;
099: } catch (Exception e) {
100: System.err
101: .println("UMLet -> XMLContentHandler -> endElement(): "
102: + e); //LME3
103: }
104: } else if (e != null) {
105: if (elementname.equals("x")) {
106: Integer i = new Integer(elementtext);
107: x = i.intValue();
108: return;
109: } else if (elementname.equals("y")) {
110: Integer i = new Integer(elementtext);
111: y = i.intValue();
112: return;
113: } else if (elementname.equals("w")) {
114: Integer i = new Integer(elementtext);
115: w = i.intValue();
116: return;
117: } else if (elementname.equals("h")) {
118: Integer i = new Integer(elementtext);
119: h = i.intValue();
120:
121: e.setLocation(x, y);
122: e.setSize(w, h);
123: return;
124: } else if (elementname.equals("panel_attributes")) {
125: e.setState(elementtext);
126: return;
127: } else if (elementname.equals("additional_attributes")) {
128: e.setAdditionalAttributes(elementtext);
129: return;
130: }
131: }
132: }
133:
134: public void characters(char[] ch, int start, int length) {
135: elementtext += (new String(ch))
136: .substring(start, start + length);
137: }
138:
139: }
|