Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/dtdbuilder/DTDBuilder.java
32287 views
/*1* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package build.tools.dtdbuilder;2627import javax.swing.text.html.parser.*;28import java.io.DataOutputStream;29import java.io.File;30import java.io.FileInputStream;31import java.io.IOException;32import java.io.FileNotFoundException;33import java.io.BufferedInputStream;34import java.io.OutputStream;35import java.util.Hashtable;36import java.util.Vector;37import java.util.BitSet;38import java.util.StringTokenizer;39import java.util.Enumeration;40import java.util.Properties;41import java.util.zip.DeflaterOutputStream;42import java.util.zip.Deflater;43import java.net.URL;4445/**46* The representation of an SGML DTD. This is produced by the DTDParser.47* The resulting DTD object describes a document syntax and is needed48* to parse HTML documents using the Parser. It contains a list of49* elements and their attributes as well as a list of entities defined50* in the DTD.51*52* @see Element53* @see AttributeList54* @see ContentModel55* @see DTDParser56* @see Parser57* @author Arthur van Hoff58*/59public60class DTDBuilder extends DTD {6162static PublicMapping mapping = null;6364// Hash from name to Integer65private Hashtable<String, Integer> namesHash = new Hashtable<>();66// Vector of all names67private Vector<String> namesVector = new Vector<>();6869/**70* Create a new DTD.71*/72protected DTDBuilder(String name) {73super(name);74}757677/**78* Save to a stream as a Java class. Instantiating this class will79* reproduce a (virtually) identical DTD.80*/81void save(DataOutputStream out, String className) throws IOException {8283out.writeInt(DTD.FILE_VERSION);8485buildNamesTable();86int numNames = namesVector.size();87out.writeShort((short) (namesVector.size()));88for (int i = 0; i < namesVector.size(); i++) {89String nm = namesVector.elementAt(i);90out.writeUTF(nm);91}9293saveEntities(out);9495out.writeShort((short) (elements.size()));96for (Enumeration<Element> e = elements.elements() ; e.hasMoreElements() ; ) {97saveElement(out, e.nextElement());98}99100if (namesVector.size() != numNames) {101System.err.println("!!! ERROR! Names were added to the list!");102Thread.dumpStack();103System.exit(1);104}105}106107private void buildNamesTable() {108for (Enumeration<Entity> e = entityHash.elements() ; e.hasMoreElements() ; ) {109Entity ent = e.nextElement();110// Do even if not isGeneral(). That way, exclusions and inclusions111// will definitely have their element.112getNameId(ent.getName());113}114for (Enumeration<Element> e = elements.elements() ; e.hasMoreElements() ; ) {115Element el = e.nextElement();116getNameId(el.getName());117for (AttributeList atts = el.getAttributes() ; atts != null ; atts = atts.getNext()) {118getNameId(atts.getName());119if (atts.getValue() != null) {120getNameId(atts.getValue());121}122Enumeration<?> vals = atts.getValues();123while (vals != null && vals.hasMoreElements()) {124String s = (String) vals.nextElement();125getNameId(s);126}127}128}129}130131//132// The the id of a name from the list of names133//134private short getNameId(String name) {135Integer o = namesHash.get(name);136if (o != null) {137return (short) o.intValue();138}139int i = namesVector.size();140namesVector.addElement(name);141namesHash.put(name, new Integer(i));142return (short) i;143}144145146/**147* Save an entity to a stream.148*/149void saveEntities(DataOutputStream out) throws IOException {150int num = 0;151for (Enumeration<Entity> e = entityHash.elements() ; e.hasMoreElements() ; ) {152Entity ent = e.nextElement();153if (ent.isGeneral()) {154num++;155}156}157158out.writeShort((short) num);159for (Enumeration<Entity> e = entityHash.elements() ; e.hasMoreElements() ; ) {160Entity ent = e.nextElement();161if (ent.isGeneral()) {162out.writeShort(getNameId(ent.getName()));163out.writeByte(ent.getType() & ~GENERAL);164out.writeUTF(ent.getString());165}166}167}168169170/**171* Save an element to a stream.172*/173174public void saveElement(DataOutputStream out, Element elem) throws IOException {175176out.writeShort(getNameId(elem.getName()));177out.writeByte(elem.getType());178179byte flags = 0;180if (elem.omitStart()) {181flags |= 0x01;182}183if (elem.omitEnd()) {184flags |= 0x02;185}186out.writeByte(flags);187saveContentModel(out, elem.getContent());188189// Exclusions190if (elem.exclusions == null) {191out.writeShort(0);192} else {193short num = 0;194for (int i = 0 ; i < elem.exclusions.size() ; i++) {195if (elem.exclusions.get(i)) {196num++;197}198}199out.writeShort(num);200for (int i = 0 ; i < elem.exclusions.size() ; i++) {201if (elem.exclusions.get(i)) {202out.writeShort(getNameId(getElement(i).getName()));203}204}205}206207// Inclusions208if (elem.inclusions == null) {209out.writeShort(0);210} else {211short num = 0;212for (int i = 0 ; i < elem.inclusions.size() ; i++) {213if (elem.inclusions.get(i)) {214num++;215}216}217out.writeShort(num);218for (int i = 0 ; i < elem.inclusions.size() ; i++) {219if (elem.inclusions.get(i)) {220out.writeShort(getNameId(getElement(i).getName()));221}222}223}224225// Attributes226{227short numAtts = 0;228for (AttributeList atts = elem.getAttributes() ; atts != null ; atts = atts.getNext()) {229numAtts++;230}231out.writeByte(numAtts);232for (AttributeList atts = elem.getAttributes() ; atts != null ; atts = atts.getNext()) {233out.writeShort(getNameId(atts.getName()));234out.writeByte(atts.getType());235out.writeByte(atts.getModifier());236if (atts.getValue() == null) {237out.writeShort(-1);238} else {239out.writeShort(getNameId(atts.getValue()));240}241if (atts.values == null) {242out.writeShort(0);243} else {244out.writeShort((short) atts.values.size());245for (int i = 0; i < atts.values.size(); i++) {246String s = (String) atts.values.elementAt(i);247out.writeShort(getNameId(s));248}249}250}251}252}253254255/**256* Save a content model to a stream. This does a257* recursive decent of the entire model.258*/259public void saveContentModel(DataOutputStream out, ContentModel model) throws IOException {260if (model == null) {261out.writeByte(0);262} else if (model.content instanceof ContentModel) {263out.writeByte(1);264out.writeByte(model.type);265saveContentModel(out, (ContentModel)model.content);266267saveContentModel(out, model.next);268} else if (model.content instanceof Element) {269out.writeByte(2);270out.writeByte(model.type);271out.writeShort(getNameId(((Element) model.content).getName()));272273saveContentModel(out, model.next);274}275}276277278/**279* Generate a class representing this DTD.280*/281282public static void main(String argv[]) {283284String dtd_home = System.getProperty("dtd_home") + File.separator;285if (dtd_home == null) {286System.err.println("Must set property 'dtd_home'");287return;288}289290DTDBuilder dtd = null;291try {292dtd = new DTDBuilder(argv[0]);293mapping = new PublicMapping(dtd_home, "public.map");294String path = mapping.get(argv[0]);295new DTDParser().parse(new FileInputStream(path), dtd);296297} catch (IOException e) {298System.err.println("Could not open DTD file "+argv[0]);299e.printStackTrace(System.err);300System.exit(1);301}302try {303DataOutputStream str = new DataOutputStream(System.out);304dtd.save(str, argv[0]);305str.close();306} catch (IOException ex) {307ex.printStackTrace();308System.exit(1);309}310}311312}313314315