Source for gnu.java.net.IndexListParser

   1: /* IndexListParser.java -- 
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.java.net;
  40: 
  41: import java.io.BufferedReader;
  42: import java.io.InputStreamReader;
  43: import java.net.URL;
  44: import java.util.HashSet;
  45: import java.util.LinkedHashMap;
  46: import java.util.jar.JarFile;
  47: 
  48: /**
  49:  * The INDEX.LIST file contains sections each separated by a blank line. 
  50:  * Each section defines the content of a jar, with a
  51:  * header defining the jar file path name, followed by a list of paths.
  52:  * The jar file paths are relative to the codebase of the root jar.
  53:  * 
  54:     Specification
  55:     index file :            version-info blankline section*
  56:     version-info :          JarIndex-Version: version-number
  57:     version-number :        digit+{.digit+}*
  58:     section :               body blankline
  59:     body :                  header name*
  60:     header :                char+.jar newline
  61:     name :                  char+ newline
  62:     
  63:  * @author langel at redhat dot com
  64:  */
  65: public class IndexListParser
  66: {
  67:   public static final String JAR_INDEX_FILE = "META-INF/INDEX.LIST";
  68:   public static final String JAR_INDEX_VERSION_KEY = "JarIndex-Version: ";
  69: 
  70:   double versionNumber;
  71:   // Map each jar to the prefixes defined for the jar.
  72:   // This is intentionally kept in insertion order.
  73:   LinkedHashMap prefixes = new LinkedHashMap();
  74:   
  75:   /**
  76:    * Parses the given jarfile's INDEX.LIST file if it exists.
  77:    * 
  78:    * @param jarfile - the given jar file
  79:    * @param baseJarURL - the codebase of the jar file
  80:    * @param baseURL - the base url for the headers
  81:    */
  82:   public IndexListParser(JarFile jarfile, URL baseJarURL, URL baseURL)
  83:   {
  84:     try
  85:     {
  86:     // Parse INDEX.LIST if it exists
  87:     if (jarfile.getEntry(JAR_INDEX_FILE) != null)
  88:       {
  89:         BufferedReader br = new BufferedReader(new InputStreamReader(new URL(baseJarURL,
  90:                                                                              JAR_INDEX_FILE).openStream()));
  91:         
  92:         // Must start with version info
  93:         String line = br.readLine();
  94:         if (!line.startsWith(JAR_INDEX_VERSION_KEY))
  95:           return;
  96:         versionNumber = Double.parseDouble(line.substring(JAR_INDEX_VERSION_KEY.length()).trim());
  97:         
  98:         // Blank line must be next
  99:         line = br.readLine();
 100:         if (! "".equals(line))
 101:           {
 102:             clearAll();
 103:             return;
 104:           }
 105:         
 106:         // May contain sections.
 107:         while ((line = br.readLine()) != null)
 108:           {
 109:             URL jarURL = new URL(baseURL, line);
 110:             HashSet values = new HashSet();
 111:             
 112:             // Read the names in the section.
 113:             while ((line = br.readLine()) != null)
 114:               {
 115:                 // Stop at section boundary.
 116:                 if ("".equals(line))
 117:                   break;
 118:                 values.add(line.trim());
 119:               }
 120:             prefixes.put(jarURL, values);
 121:             // Might have seen an early EOF.
 122:             if (line == null)
 123:               break;
 124:           }
 125: 
 126:         br.close();
 127:       }
 128:     else
 129:       {
 130:         // INDEX.LIST does not exist
 131:         clearAll();
 132:       }
 133:     }
 134:     catch (Exception ex)
 135:     {
 136:       clearAll();
 137:     }
 138:   }
 139:   
 140:   /**
 141:    * Clears all the variables. This is called when parsing fails.
 142:    */
 143:   void clearAll()
 144:   {
 145:     versionNumber = 0;
 146:     prefixes = null;
 147:   }
 148:   
 149:   /**
 150:    * Gets the version info for the file.
 151:    * 
 152:    * @return the version info.
 153:    */
 154:   public String getVersionInfo()
 155:   {
 156:     return JAR_INDEX_VERSION_KEY + getVersionNumber();
 157:   }
 158:   
 159:   /**
 160:    * Gets the version number of the file.
 161:    * 
 162:    * @return the version number.
 163:    */
 164:   public double getVersionNumber()
 165:   {
 166:     return versionNumber;
 167:   }
 168:   
 169:   /**
 170:    * Gets the map of all the headers found in the file.
 171:    * The keys in the map are URLs of jars.  The values in the map
 172:    * are Sets of package prefixes (and top-level file names), as
 173:    * specifed in INDEX.LIST.
 174:    * 
 175:    * @return an map of all the headers, or null if no INDEX.LIST was found
 176:    */
 177:   public LinkedHashMap getHeaders()
 178:   {
 179:     return prefixes;
 180:   }
 181: }