001/*
002 * Copyright (c) 2002-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.geon;
031
032import java.io.File;
033import java.io.FileInputStream;
034import java.io.InputStream;
035import java.util.Vector;
036
037import javax.xml.parsers.DocumentBuilder;
038import javax.xml.parsers.DocumentBuilderFactory;
039
040import org.w3c.dom.Document;
041import org.w3c.dom.Element;
042import org.w3c.dom.NodeList;
043
044//////////////////////////////////////////////////////////////////////////
045////GeonXMLUtil
046/**
047 * @author Efrat Jaeger
048 * @version $Id: GeonXMLUtil.java 24234 2010-05-06 05:21:26Z welker $
049 * @since Ptolemy II 3.0.2
050 */
051public class GeonXMLUtil {
052
053        public GeonXMLUtil() {
054
055        }
056
057        public Vector getAttrValue(File input, String tag, String attribute) {
058                Vector stringAttr = new Vector();
059                try {
060                        DocumentBuilderFactory factory = DocumentBuilderFactory
061                                        .newInstance();
062                        factory.setValidating(false);
063                        DocumentBuilder builder = factory.newDocumentBuilder();
064                        InputStream is = new FileInputStream(input);
065                        Document doc = builder.parse(is);
066
067                        NodeList nodes = doc.getElementsByTagName(tag);
068                        for (int i = 0; i < nodes.getLength(); i++) {
069                                String attr = ((Element) nodes.item(i)).getAttribute(attribute);
070                                stringAttr.addElement(attr);
071                        }
072                } catch (Exception e) {
073                        e.printStackTrace();
074                }
075                return stringAttr;
076        }
077
078        public Vector getElementsById(File input, String tag, String attribute) { // temporary
079                                                                                                                                                                // function
080                                                                                                                                                                // for
081                                                                                                                                                                // text
082                                                                                                                                                                // tags.
083                Vector stringAttr = new Vector();
084                try {
085                        DocumentBuilderFactory factory = DocumentBuilderFactory
086                                        .newInstance();
087                        factory.setValidating(false);
088                        DocumentBuilder builder = factory.newDocumentBuilder();
089                        InputStream is = new FileInputStream(input);
090                        Document doc = builder.parse(is);
091
092                        NodeList nodes = doc.getElementsByTagName(tag);
093                        for (int i = 0; i < nodes.getLength(); i++) {
094                                String attr = ((Element) nodes.item(i)).getAttribute(attribute);
095                                int beginInd = attr.indexOf('(');
096                                int endInd = attr.indexOf(')');
097                                attr = attr.substring(beginInd + 2, endInd - 1);
098                                // System.out.println("attr = " + attr);
099                                Element elem = doc.getElementById(attr);
100                                // System.out.println(elem.getFirstChild().getNodeValue());
101                                stringAttr.addElement(elem.getFirstChild().getNodeValue());
102                        }
103                } catch (Exception e) {
104                        e.printStackTrace();
105                }
106                return stringAttr;
107        }
108
109        public String getElementsByTagId(File input, String tag, String id) { // temporary
110                                                                                                                                                        // function
111                                                                                                                                                        // for
112                                                                                                                                                        // text
113                                                                                                                                                        // tags.
114                Vector stringAttr = new Vector();
115                try {
116                        DocumentBuilderFactory factory = DocumentBuilderFactory
117                                        .newInstance();
118                        factory.setValidating(false);
119                        DocumentBuilder builder = factory.newDocumentBuilder();
120                        InputStream is = new FileInputStream(input);
121                        Document doc = builder.parse(is);
122
123                        NodeList nodes = doc.getElementsByTagName(tag);
124                        for (int i = 0; i < nodes.getLength(); i++) {
125                                String _id = ((Element) nodes.item(i)).getAttribute("id");
126                                if (id.equals(_id)) {
127                                        String value = ((Element) nodes.item(i)).getAttribute(id);
128                                        return value;
129                                }
130                        }
131                } catch (Exception e) {
132                        e.printStackTrace();
133                }
134                return "";
135        }
136
137}