001: /*
002: * XMLParser.java - The XML Parser
003: * Copyright (C) 2003 Alexandre THOMAS
004: * alexthomas@free.fr
005: * http://helpgui.sourceforge.net
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package salomeTMF_plug.helpgui.parser;
023:
024: import javax.swing.tree.DefaultTreeModel;
025: import javax.swing.tree.MutableTreeNode;
026:
027: import org.xml.sax.Attributes;
028: import org.xml.sax.SAXException;
029: import org.xml.sax.helpers.DefaultHandler;
030:
031: import salomeTMF_plug.helpgui.gui.HelpView;
032: import salomeTMF_plug.helpgui.page.Page;
033: import salomeTMF_plug.helpgui.page.PageRoot;
034:
035: /**
036: * XML Parser class
037: *
038: * @author Alexandre THOMAS
039: */
040: class XMLParser extends DefaultHandler {
041:
042: MutableTreeNode parent;
043:
044: DefaultTreeModel model;
045: boolean plugins;
046: String pluginsName;
047:
048: //static Hashtable rootBranch = new Hashtable();
049:
050: public XMLParser(HelpView helpView, boolean plugins,
051: String pluginsName) {
052: this .plugins = plugins;
053: this .pluginsName = pluginsName;
054: parent = (MutableTreeNode) helpView.getJTree().getModel()
055: .getRoot();
056: model = (DefaultTreeModel) (helpView.getJTree().getModel());
057: }
058:
059: /** Start to parse a tag */
060: public void startElement(String namespaceURI, String sName, // simple name
061: String qName, // qualified name
062: Attributes attrs) throws SAXException {
063:
064: String text = null, image = null, target = null;
065: boolean home = false;
066:
067: if (attrs != null) {
068: for (int i = 0; i < attrs.getLength(); i++) {
069: String aName = attrs.getQName(i); // Attr name
070: if (qName.equals("tocitem")) {
071: if (aName == "text") {
072: text = attrs.getValue(i);
073: } else if (aName == "image") {
074: image = attrs.getValue(i);
075: } else if (aName == "target") {
076: target = attrs.getValue(i);
077: } else if (aName == "home") {
078: home = attrs.getValue(i).equals("true");
079: }
080: }
081: }
082: }
083:
084: if (qName.equals("tocitem")) {
085: MutableTreeNode node = null;
086: if (parent instanceof Page) {
087: node = new Page(text, image, target, home,
088: (Page) parent, plugins, pluginsName);
089: model.insertNodeInto(node, parent, parent
090: .getChildCount());
091: } else if (parent instanceof PageRoot) {
092: //Object pRoot = rootBranch.get(text);
093: node = new Page(text, image, target, home,
094: (PageRoot) parent, plugins, pluginsName);
095: //if (pRoot == null) {
096: //model.insertNodeInto((MutableTreeNode) node, parent, parent.getChildCount());
097: // rootBranch.put(text,node);
098: // Api.log("[helpgui:XMLParser] Parent is : "+ parent);
099: //}
100: }
101: model.insertNodeInto(node, parent, parent.getChildCount());
102: parent = node;
103: }
104: }
105:
106: /** Finish to parse a tag */
107: public void endElement(String namespaceURI, String sName,
108: String qName) throws SAXException {
109: if (qName.equals("tocitem"))
110: parent = (MutableTreeNode) parent.getParent();
111: }
112:
113: }
|