~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/javahelp/jhMaster/JSearch/client/com/sun/java/help/search/BtreeDictParameters.java

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * @(#)BtreeDictParameters.java 1.15 06/10/30
 
3
 * 
 
4
 * Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
 
5
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
6
 * 
 
7
 * This code is free software; you can redistribute it and/or modify it
 
8
 * under the terms of the GNU General Public License version 2 only, as
 
9
 * published by the Free Software Foundation.  Sun designates this
 
10
 * particular file as subject to the "Classpath" exception as provided
 
11
 * by Sun in the LICENSE file that accompanied this code.
 
12
 * 
 
13
 * This code is distributed in the hope that it will be useful, but WITHOUT
 
14
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
15
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
16
 * version 2 for more details (a copy is included in the LICENSE file that
 
17
 * accompanied this code).
 
18
 * 
 
19
 * You should have received a copy of the GNU General Public License version
 
20
 * 2 along with this work; if not, write to the Free Software Foundation,
 
21
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
22
 * 
 
23
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 
24
 * CA 95054 USA or visit www.sun.com if you need additional information or
 
25
 * have any questions.
 
26
 */
 
27
 
 
28
/*
 
29
 * @(#) BtreeDictParameters.java 1.5 - last change made 04/01/98
 
30
 */
 
31
 
 
32
package com.sun.java.help.search;
 
33
 
 
34
import java.net.URL;
 
35
import java.net.URLConnection;
 
36
import java.util.StringTokenizer;
 
37
import java.io.*;
 
38
 
 
39
/**
 
40
 *
 
41
 * @version     1.5     04/01/98
 
42
 * @author Jacek R. Ambroziak
 
43
 * @author Roger D. Brinkley
 
44
 * @author Eduardo Pelegri-Llopart
 
45
 */
 
46
 
 
47
class BtreeDictParameters extends BlockManagerParameters
 
48
{
 
49
  private int id1;
 
50
  private String dirName;
 
51
  
 
52
  // some form of this is needed for starting new DBs
 
53
  public BtreeDictParameters(URL fileName, int blockSize, int root,
 
54
                             int freeID)
 
55
  {
 
56
    super(fileName, blockSize, root);
 
57
    id1 = freeID;
 
58
  }
 
59
    
 
60
  public BtreeDictParameters(Schema schema, String partName)
 
61
    throws Exception
 
62
  {
 
63
    super(schema, partName);
 
64
  }
 
65
  
 
66
  public boolean readState()
 
67
  {
 
68
    if (super.readState())
 
69
      {
 
70
        setFreeID(integerParameter("id1"));
 
71
        return true;
 
72
      }
 
73
    else
 
74
      return false;
 
75
  }
 
76
 
 
77
  public void writeState() {
 
78
  }
 
79
  
 
80
  public int getFreeID() {
 
81
    return id1;
 
82
  }
 
83
 
 
84
  public final void setFreeID(int id) {
 
85
    id1 = id;
 
86
  }
 
87
 
 
88
  private void setDirName(String dirName) {
 
89
    this.dirName = dirName;
 
90
  }
 
91
 
 
92
  public static BtreeDictParameters create(URL dirName)
 
93
  {
 
94
    try {
 
95
      URL url = new URL(dirName, "TMAP");
 
96
      BtreeDictParameters bdp = null;
 
97
      //        new BtreeDictParameters(url, 2048, 0, 1);
 
98
      bdp.setDirName(dirName.getFile());
 
99
      return bdp;
 
100
    } catch (java.net.MalformedURLException e) {
 
101
      System.out.println ("Couldn't create " + dirName + File.separator + "TMAP");
 
102
    }
 
103
    return null;
 
104
  }
 
105
 
 
106
  public static BtreeDictParameters read(String dir, URL hsBase)
 
107
    throws Exception
 
108
  {
 
109
    URL baseURL=null, url, tmapURL=null;
 
110
    URLConnection connect;
 
111
    BufferedReader in;
 
112
    File file;
 
113
 
 
114
    int blockSize = -1;
 
115
    int rootPosition = -1;
 
116
    int freeID = -1;
 
117
 
 
118
    if (hsBase == null) {
 
119
        file = new File(dir);
 
120
        if (file.exists()) {
 
121
            // On Win32 we need to convert all "\" to "/"
 
122
            if (File.separatorChar != '/') {
 
123
                dir = dir.replace(File.separatorChar, '/');
 
124
            }
 
125
            // Make sure the last character is a file separator
 
126
            if (dir.lastIndexOf(File.separatorChar)
 
127
                != dir.length() - 1) {
 
128
                dir = dir.concat(File.separator);
 
129
            }
 
130
            debug ("file:" + dir);
 
131
            // Use a file protocol
 
132
            baseURL = new URL("file", "", dir);
 
133
        } else {
 
134
            // Assume that some protocol was specified and try it
 
135
        baseURL = new URL(dir);
 
136
      }
 
137
    }
 
138
      
 
139
    // Read the SCHEMA data
 
140
    if (hsBase != null) {
 
141
      url = new URL(hsBase, dir + "/SCHEMA");
 
142
    } else {
 
143
      url = new URL(baseURL, "SCHEMA");
 
144
    }
 
145
    connect = url.openConnection();
 
146
 
 
147
    in = new BufferedReader
 
148
      (new InputStreamReader(connect.getInputStream()));
 
149
 
 
150
    // This needs to be replaced with our XML Parser
 
151
    String line;
 
152
    do {
 
153
      line = in.readLine();
 
154
    }
 
155
    while (!line.startsWith("TMAP"));
 
156
    in.close();
 
157
    StringTokenizer tokens = new StringTokenizer(line, " =");
 
158
    tokens.nextToken();         // skip over 'TMAP'
 
159
    while (tokens.hasMoreTokens())
 
160
      {
 
161
        String token = tokens.nextToken();
 
162
        if (token.equals("bs"))
 
163
          blockSize = Integer.parseInt(tokens.nextToken());
 
164
        else if (token.equals("rt"))
 
165
          rootPosition = Integer.parseInt(tokens.nextToken());
 
166
        else if (token.equals("id1"))
 
167
          freeID = Integer.parseInt(tokens.nextToken());
 
168
      }
 
169
 
 
170
    if (hsBase != null) {
 
171
      tmapURL = new URL(hsBase, dir + "/TMAP");
 
172
    } else {
 
173
      tmapURL = new URL(baseURL, "TMAP");
 
174
    }
 
175
    BtreeDictParameters bdp = null;
 
176
    //      new BtreeDictParameters(tmapURL, blockSize, rootPosition, freeID);
 
177
 
 
178
    if (hsBase == null) {
 
179
      bdp.setDirName(Utilities.URLDecoder(baseURL.getFile()));
 
180
    }
 
181
    return bdp;
 
182
  }
 
183
 
 
184
  public void updateSchema() {
 
185
    super.updateSchema("id1="+id1+" id2=1");
 
186
  }
 
187
    
 
188
  public void write() throws java.io.IOException
 
189
  {
 
190
    FileWriter out = new FileWriter(dirName + "/SCHEMA");
 
191
    out.write("JavaSearch 1.0\n");
 
192
    out.write("TMAP bs=2048 rt="+root+" fl=-1 id1="+id1+" id2=1\n");
 
193
    out.close();
 
194
  }
 
195
 
 
196
  /**
 
197
   * For printf debugging.
 
198
   */
 
199
  private static boolean debugFlag = false;
 
200
  private static void debug(String str) {
 
201
    if( debugFlag ) {
 
202
      System.out.println("BtreeDictParamters: " + str);
 
203
    }
 
204
  }
 
205
}