KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensugar > cube > java2 > ManifestHelper


1 /*
2  * JEFFREE: Java(TM) Embedded Framework FREE
3  * Copyright (C) 1999-2003 - Opensugar
4  *
5  * The contents of this file are subject to the Jeffree Public License,
6  * as defined by the file JEFFREE_LICENSE.TXT
7  *
8  * You may not use this file except in compliance with the License.
9  * You may obtain a copy of the License on the Objectweb web site
10  * (www.objectweb.org).
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14  * the specific terms governing rights and limitations under the License.
15  *
16  * The Original Code is JEFFREE, including the java package com.opensugar.cube,
17  * released January 1, 2003.
18  *
19  * The Initial Developer of the Original Code is Opensugar.
20  * The Original Code is Copyright Opensugar.
21  * All Rights Reserved.
22  *
23  * Initial developer(s): Pierre Scokaert (Opensugar)
24  * Contributor(s):
25  */

26
27 package com.opensugar.cube.java2;
28
29 import java.util.Hashtable JavaDoc;
30 import java.util.zip.ZipFile JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.BufferedReader JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34
35 // This class is used for interoperability both on Java 1 and 2.
36
// In Java 2, it would be better to use the classes provided by Java to parse
37
// the jar file manifest.
38
// These utilities however are not available in Java 1.1, so we had to develop
39
// this class.
40
public class ManifestHelper {
41
42    private static final String JavaDoc MANIFEST_FILE = "META-INF/MANIFEST.MF";
43
44    // manifest main attributes
45
private Hashtable JavaDoc mainAttributes = new Hashtable JavaDoc();
46
47    public ManifestHelper( ZipFile JavaDoc archive ) throws IOException JavaDoc {
48       // in java 2, could do something like this, though we probably would use the
49
// Attributes class without converting it to a Properties.
50
/*
51       java.util.jar.Manifest manifest = new java.util.jar.Manifest( archive.getInputStream( archive.getEntry( MANIFEST_FILE ) ) );
52       java.util.jar.Attributes attr = manifest.getMainAttributes();
53       java.util.Iterator iter = attr.keySet().iterator();
54       String key;
55       while ( iter.hasNext() ) {
56          key = iter.next().toString();
57          mainAttributes.put( key, attr.getValue( key ) );
58       }
59       */

60       parseManifest( archive );
61    }
62
63    // I am not at all familiar with the augmented Backus-Naur Form. So I've done what
64
// seems sensible here, rather than go look up what should really be done. As a
65
// result, this method is probably is too laxist on manifest syntax (no limit on
66
// line length for instance) but I'd rather accept too much than too little.
67
// This method may need updating if there are valid syntax cases that I have not
68
// taken into account.
69
private void parseManifest( ZipFile JavaDoc archive ) throws IOException JavaDoc {
70       // ISO-8859-1 is the encoding that works, don't ask me why!
71
BufferedReader JavaDoc br = new BufferedReader JavaDoc( new InputStreamReader JavaDoc( archive.getInputStream( archive.getEntry( MANIFEST_FILE ) ), "ISO-8859-1" ) );
72       String JavaDoc attributeName = null;
73       String JavaDoc attributeValue = null;
74       boolean done = false;
75       int lineNumber = 0;
76
77       // main attributes are the manifest headers and values from the main section
78
// of the bundle's manifest file; that is, all lines prior to the first blank
79
// line
80
while ( !done ) {
81          String JavaDoc line = br.readLine();
82          lineNumber++;
83          if ( line == null || line.trim().length() == 0 ) {
84             // null line: we have reached the end of the manifest
85
// empty line: we have reached the end of the main section of the bundle's manifest
86

87             // record last attribute
88
if ( attributeName != null ) {
89                mainAttributes.put( attributeName, attributeValue );
90             }
91
92             // we are done
93
done = true;
94          }
95          else if ( !line.startsWith( " " ) ) {
96             // this line starts a new attribute
97

98             // record last attribute
99
if ( attributeName != null ) {
100                mainAttributes.put( attributeName, attributeValue );
101             }
102
103             // proceed to new attribute
104
int n = line.indexOf( ": " );
105             if ( n == -1 || n + 2 >= line.length() ) {
106                throw new IOException JavaDoc( "Invalid line in bundle manifest (line " + lineNumber + "): " + line );
107             }
108             else {
109                attributeName = line.substring( 0, n );
110                attributeValue = line.substring( n + 2, line.length() );
111             }
112          }
113          else {
114             // this line is a continuation line
115
if ( attributeValue == null ) {
116                throw new IOException JavaDoc( "Misplaced continuation line in bundle manifest (line " + lineNumber + "): " + line );
117             }
118             else {
119                attributeValue = attributeValue + line.substring( 1 );
120             }
121          }
122       }
123    }
124
125    public Hashtable JavaDoc getMainAttributes() {
126       return mainAttributes;
127    }
128
129 }
130
Popular Tags