001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util.jar;
038:
039: import edu.rice.cs.util.StringOps;
040: import edu.rice.cs.util.UnexpectedException;
041: import java.util.jar.Manifest;
042: import java.util.jar.Attributes;
043: import java.util.LinkedList;
044: import java.util.List;
045: import java.util.Iterator;
046: import java.io.*;
047:
048: /** Writes manifest objects. Useful for creating Manifest files without writing them to files. */
049: public class ManifestWriter {
050: private List<String> _classPaths;
051: private String _mainClass;
052: public static final Manifest DEFAULT = new ManifestWriter()
053: .getManifest();
054:
055: /** Create a new manifest file */
056: public ManifestWriter() {
057: _classPaths = new LinkedList<String>();
058: _mainClass = null;
059: }
060:
061: /** Add a class path to the Manifest
062: * @param path the path to be added
063: */
064: public void addClassPath(String path) {
065: _classPaths.add(_classPaths.size(), path);
066: }
067:
068: /**
069: * Set the main class of the Manifest
070: * @param mainClass
071: */
072: public void setMainClass(String mainClass) {
073: _mainClass = mainClass;
074: }
075:
076: /** Get an input stream to the contents of the manifest file
077: * @return an InputStream whose contents are the contents of the Manifest file
078: */
079: protected InputStream getInputStream() {
080: // NOTE: All significant lines in the manifest MUST end in the end of line character
081:
082: final StringBuilder sbuf = new StringBuilder();
083: sbuf.append(Attributes.Name.MANIFEST_VERSION.toString());
084: sbuf.append(": 1.0" + StringOps.EOL);
085: if (!_classPaths.isEmpty()) {
086: Iterator<String> iter = _classPaths.iterator();
087: sbuf.append(Attributes.Name.CLASS_PATH.toString());
088: sbuf.append(":");
089: while (iter.hasNext()) {
090: sbuf.append(" ");
091: sbuf.append(iter.next());
092: }
093: sbuf.append(StringOps.EOL);
094: }
095: if (_mainClass != null) {
096: sbuf.append(Attributes.Name.MAIN_CLASS.toString());
097: sbuf.append(": ");
098: sbuf.append(_mainClass);
099: sbuf.append(StringOps.EOL);
100: }
101: try {
102: return new ByteArrayInputStream(sbuf.toString().getBytes(
103: "UTF-8"));
104: } catch (UnsupportedEncodingException e) {
105: throw new UnexpectedException(e);
106: }
107: }
108:
109: /**
110: * Get the Manifest object that this object created.
111: * @return the Manifest that this builder created
112: */
113: public Manifest getManifest() {
114: try {
115: Manifest m = new Manifest();
116: m.read(getInputStream());
117: return m;
118: } catch (IOException e) {
119: e.printStackTrace();
120: return null;
121: }
122: }
123: }
|