PublicMapping.java revision 9330:8b1f1c2a400f
1/*
2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package build.tools.dtdbuilder;
27
28import javax.swing.text.html.parser.*;
29import java.io.File;
30import java.io.IOException;
31import java.io.FileInputStream;
32import java.io.InputStream;
33import java.io.InputStreamReader;
34import java.io.BufferedReader;
35import java.util.Hashtable;
36
37/**
38 * A class for mapping public identifiers to locations.
39 * Note: I'm not sure if there is a more natural mapping
40 * then this. Maybe we should use properties instead?
41 *
42 * @author      Arthur van Hoff
43 */
44final class PublicMapping {
45
46    String baseStr;
47    Hashtable<String, String> tab = new Hashtable<>();
48
49    /**
50     * Create a mapping.
51     */
52    public PublicMapping (String baseStr, String mapFile) throws IOException {
53        // System.err.println("start loading " + baseStr);
54        this.baseStr = baseStr;
55        load(new FileInputStream(baseStr+mapFile));
56        // System.err.println("stop loading");
57    }
58
59    /**
60     * Load a set of mappings from a stream.
61     */
62    public void load(InputStream in) throws IOException {
63        InputStreamReader reader = new InputStreamReader(in);
64        BufferedReader data = new BufferedReader(reader);
65
66        for (String ln = data.readLine() ; ln != null ; ln = data.readLine()) {
67            if (ln.startsWith("PUBLIC")) {
68                int len = ln.length();
69                int i = 6;
70                while ((i < len) && (ln.charAt(i) != '"')) i++;
71                int j = ++i;
72                while ((j < len) && (ln.charAt(j) != '"')) j++;
73                String id = ln.substring(i, j);
74                i = ++j;
75                while ((i < len) && ((ln.charAt(i) == ' ') || (ln.charAt(i) == '\t'))) i++;
76                j = i + 1;
77                while ((j < len) && (ln.charAt(j) != ' ') && (ln.charAt(j) != '\t')) j++;
78                String where = ln.substring(i, j);
79                put(id, baseStr + where);
80            }
81        }
82        data.close();
83    }
84
85    /**
86     * Add a mapping from a public identifier to a path.
87     */
88    public void put(String id, String str) {
89
90        // System.err.println("ADD = '" + id + "' = " + str);
91        tab.put(id, str);
92        if (str.endsWith(".dtd")) {
93            int i = str.lastIndexOf(File.separator);
94            if (i >= 0) {
95                tab.put(str.substring(i + 1, str.length() - 4), str);
96            }
97        }
98    }
99
100    /**
101     * Map a public identifier to a path.. You can also map
102     * a DTD file name (without the .dtd) to its path.
103     */
104    public String get(String id) {
105        // System.err.println(" id = "+id);
106        return tab.get(id);
107    }
108}
109