make/jdk/src/classes/build/tools/dtdbuilder/DTDBuilder.java
author tschatzl
Fri, 22 Nov 2019 10:03:38 +0100
changeset 59219 01cc6bb2a090
parent 47216 71c04702a3d5
permissions -rw-r--r--
8233997: Some members of HeapRegion are not cleared in HeapRegion::hr_clear() Reviewed-by: sjohanss, sangheki
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21805
diff changeset
     2
 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package build.tools.dtdbuilder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.text.html.parser.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.BitSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.zip.DeflaterOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.zip.Deflater;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The representation of an SGML DTD. This is produced by the DTDParser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The resulting DTD object describes a document syntax and is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * to parse HTML documents using the Parser. It contains a list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * elements and their attributes as well as a list of entities defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * in the DTD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @see Element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @see AttributeList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @see ContentModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see DTDParser
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see Parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
class DTDBuilder extends DTD {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static PublicMapping mapping = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Hash from name to Integer
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    66
    private Hashtable<String, Integer> namesHash = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // Vector of all names
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    68
    private Vector<String> namesVector = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Create a new DTD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    protected DTDBuilder(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Save to a stream as a Java class. Instantiating this class will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * reproduce a (virtually) identical DTD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    void save(DataOutputStream out, String className) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        out.writeInt(DTD.FILE_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        buildNamesTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        int numNames = namesVector.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        out.writeShort((short) (namesVector.size()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        for (int i = 0; i < namesVector.size(); i++) {
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    90
            String nm = namesVector.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            out.writeUTF(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        saveEntities(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        out.writeShort((short) (elements.size()));
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    97
        for (Enumeration<Element> e = elements.elements() ; e.hasMoreElements() ; ) {
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    98
            saveElement(out, e.nextElement());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (namesVector.size() != numNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            System.err.println("!!! ERROR!  Names were added to the list!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private void buildNamesTable() {
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   109
        for (Enumeration<Entity> e = entityHash.elements() ; e.hasMoreElements() ; ) {
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   110
            Entity ent = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            // Do even if not isGeneral().  That way, exclusions and inclusions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // will definitely have their element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            getNameId(ent.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   115
        for (Enumeration<Element> e = elements.elements() ; e.hasMoreElements() ; ) {
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   116
            Element el = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            getNameId(el.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            for (AttributeList atts = el.getAttributes() ; atts != null ; atts = atts.getNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                getNameId(atts.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                if (atts.getValue() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    getNameId(atts.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                }
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   123
                Enumeration<?> vals = atts.getValues();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                while (vals != null && vals.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    String s = (String) vals.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    getNameId(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // The the id of a name from the list of names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private short getNameId(String name)  {
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   136
        Integer o = namesHash.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (o != null) {
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   138
            return (short) o.intValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        int i = namesVector.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        namesVector.addElement(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        namesHash.put(name, new Integer(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return (short) i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Save an entity to a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    void saveEntities(DataOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int num = 0;
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   152
        for (Enumeration<Entity> e = entityHash.elements() ; e.hasMoreElements() ; ) {
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   153
            Entity ent = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (ent.isGeneral()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                num++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        out.writeShort((short) num);
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   160
        for (Enumeration<Entity> e = entityHash.elements() ; e.hasMoreElements() ; ) {
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   161
            Entity ent = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (ent.isGeneral()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                out.writeShort(getNameId(ent.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                out.writeByte(ent.getType() & ~GENERAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                out.writeUTF(ent.getString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Save an element to a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public void saveElement(DataOutputStream out, Element elem) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        out.writeShort(getNameId(elem.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        out.writeByte(elem.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        byte flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (elem.omitStart()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            flags |= 0x01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (elem.omitEnd()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            flags |= 0x02;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        out.writeByte(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        saveContentModel(out, elem.getContent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // Exclusions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (elem.exclusions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            out.writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            short num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            for (int i = 0 ; i < elem.exclusions.size() ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if (elem.exclusions.get(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    num++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            out.writeShort(num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            for (int i = 0 ; i < elem.exclusions.size() ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                if (elem.exclusions.get(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    out.writeShort(getNameId(getElement(i).getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        // Inclusions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (elem.inclusions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            out.writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            short num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            for (int i = 0 ; i < elem.inclusions.size() ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (elem.inclusions.get(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    num++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            out.writeShort(num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            for (int i = 0 ; i < elem.inclusions.size() ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                if (elem.inclusions.get(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    out.writeShort(getNameId(getElement(i).getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        // Attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            short numAtts = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            for (AttributeList atts = elem.getAttributes() ; atts != null ; atts = atts.getNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                numAtts++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            out.writeByte(numAtts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            for (AttributeList atts = elem.getAttributes() ; atts != null ; atts = atts.getNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                out.writeShort(getNameId(atts.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                out.writeByte(atts.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                out.writeByte(atts.getModifier());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                if (atts.getValue() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    out.writeShort(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    out.writeShort(getNameId(atts.getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                if (atts.values == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    out.writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    out.writeShort((short) atts.values.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    for (int i = 0; i < atts.values.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        String s = (String) atts.values.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        out.writeShort(getNameId(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Save a content model to a stream. This does a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * recursive decent of the entire model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public void saveContentModel(DataOutputStream out, ContentModel model) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (model == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            out.writeByte(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } else if (model.content instanceof ContentModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            out.writeByte(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            out.writeByte(model.type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            saveContentModel(out, (ContentModel)model.content);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            saveContentModel(out, model.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } else if (model.content instanceof Element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            out.writeByte(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            out.writeByte(model.type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            out.writeShort(getNameId(((Element) model.content).getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            saveContentModel(out, model.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Generate a class representing this DTD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public static void main(String argv[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        String dtd_home = System.getProperty("dtd_home") + File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (dtd_home == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            System.err.println("Must set property 'dtd_home'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        DTDBuilder dtd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            dtd = new DTDBuilder(argv[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            mapping = new PublicMapping(dtd_home, "public.map");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            String path = mapping.get(argv[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            new DTDParser().parse(new FileInputStream(path), dtd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            System.err.println("Could not open DTD file "+argv[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            e.printStackTrace(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            DataOutputStream str = new DataOutputStream(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            dtd.save(str, argv[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            str.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
}