001/* Generated By:JJTree&JavaCC: Do not edit this line. PtParser.java */ 002/* 003 Copyright (c) 2003-2014 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 027 028 Created : May 1998 029 030 */ 031package ptolemy.data.expr; 032 033import java.io.IOException; 034import java.io.InputStream; 035import java.io.StringReader; 036import java.net.URL; 037import java.util.Hashtable; 038 039import javax.xml.parsers.DocumentBuilder; 040import javax.xml.parsers.DocumentBuilderFactory; 041 042import org.w3c.dom.Document; 043import org.xml.sax.InputSource; 044 045/////////////////////////////////////////////////////////////////// 046//// XmlParser.java 047 048/** 049 This class parses a XML string token to a DOM document. 050 051 052 @author Yang Zhao 053 @version $Id$ 054 @since Ptolemy II 4.0 055 @Pt.ProposedRating Yellow (nsmyth) 056 @Pt.AcceptedRating Yellow (yuhong) 057 058 */ 059public class XMLParser { 060 public XMLParser() throws Exception { 061 if (_documentBuilderFactory == null) { 062 _documentBuilderFactory = DocumentBuilderFactory.newInstance(); 063 } 064 065 _documentBuilder = _documentBuilderFactory.newDocumentBuilder(); 066 _documentBuilder.setEntityResolver(new EntityResolver()); 067 } 068 069 /** Generate the document tree for the specified XML string. The Document 070 * (the root of the document tree) is 071 * returned. An exception will be thrown if the parse fails. 072 * @param str The XML string to be parsed. 073 * @exception Exception If the parse fails. 074 * @return The document for the parse tree. 075 */ 076 public Document parser(String str) throws Exception { 077 return _documentBuilder.parse(new InputSource(new StringReader(str))); 078 } 079 080 /** Generate the document tree for the specified input stream. 081 * The Document (the root of the document tree) is 082 * returned. An exception will be thrown if the parse fails. 083 * @param is The input steam to be parsed. 084 * @exception Exception If the parse fails. 085 * @return The document for the parse tree. 086 */ 087 public Document parser(InputStream is) throws Exception { 088 return _documentBuilder.parse(is); 089 } 090 091 /** The entity resolver that tries to first load a DTD file locally, and if 092 * it is not found, looks for it on the Internet using the DTD file's 093 * system ID. The DTD file, as well as its version, is uniquely identified 094 * by its public ID. 095 * 096 * @author Thomas Feng 097 * @version $Id$ 098 * @since Ptolemy II 4.0 099 * @Pt.ProposedRating Green (tfeng) 100 * @Pt.AcceptedRating Red (tfeng) 101 */ 102 public static class EntityResolver implements org.xml.sax.EntityResolver { 103 104 /** Resolve an entity (a DTD file) by first looking for it locally. If 105 * it is not found locally, this resolver tries to grab it from the 106 * Internet using its systemId. 107 * 108 * @param publicId The public ID of the entity, for example, 109 * "-//UC Berkeley//DTD MoML 1//EN" for Ptolemy MoML 1.0. 110 * @param systemId The system ID of the entity, for example, 111 * "http://ptolemy.eecs.berkeley.edu/xml/dtd/MoML_1.dtd". 112 * @return The input source that contains the content of the DTD, or 113 * null if it cannot be loaded either locally or from the Internet. 114 */ 115 @Override 116 public InputSource resolveEntity(String publicId, String systemId) { 117 if (publicId == null) { 118 return null; 119 } else { 120 if (_localResources.containsKey(publicId)) { 121 String localFile = (String) _localResources.get(publicId); 122 InputStream localStream = getClass() 123 .getResourceAsStream(localFile); 124 if (localStream != null) { 125 return new InputSource(localStream); 126 } 127 } 128 try { 129 URL url = new URL(systemId); 130 return new InputSource(url.openStream()); 131 } catch (IOException e) { 132 return null; 133 } 134 } 135 } 136 137 /** The string-to-string mapping between entities' publicId's and the 138 * names of the local files in which they are stored. The file names 139 * are relative to the path of the current class. 140 */ 141 private static final Hashtable _localResources = new Hashtable(); 142 143 static { 144 _localResources.put("-//UC Berkeley//DTD MoML 1//EN", 145 "../../moml/MoML_1.dtd"); 146 } 147 } 148 149 /////////////////////////////////////////////////////////////////// 150 //// private variables //// 151 private DocumentBuilderFactory _documentBuilderFactory = null; 152 153 private DocumentBuilder _documentBuilder; 154}