|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ManifestWriter.java | 70% | 78.4% | 83.3% | 77.4% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.util.jar; | |
| 38 | ||
| 39 | import edu.rice.cs.util.StringOps; | |
| 40 | import edu.rice.cs.util.UnexpectedException; | |
| 41 | import java.util.jar.Manifest; | |
| 42 | import java.util.jar.Attributes; | |
| 43 | import java.util.LinkedList; | |
| 44 | import java.util.List; | |
| 45 | import java.util.Iterator; | |
| 46 | import java.io.*; | |
| 47 | ||
| 48 | /** Writes manifest objects. Useful for creating Manifest files without writing them to files. */ | |
| 49 | public class ManifestWriter { | |
| 50 | private List<String> _classPaths; | |
| 51 | private String _mainClass; | |
| 52 | private String _rawManifest; | |
| 53 | public static final Manifest DEFAULT = new ManifestWriter().getManifest(); | |
| 54 | ||
| 55 | /** Create a new manifest file */ | |
| 56 | 3 | public ManifestWriter() { |
| 57 | 3 | _classPaths = new LinkedList<String>(); |
| 58 | 3 | _mainClass = null; |
| 59 | 3 | _rawManifest = null; |
| 60 | } | |
| 61 | ||
| 62 | /** Add a class path to the Manifest | |
| 63 | * @param path the path to be added | |
| 64 | */ | |
| 65 | 2 | public void addClassPath(String path) { |
| 66 | 2 | _classPaths.add(_classPaths.size(), path); |
| 67 | } | |
| 68 | ||
| 69 | /** Set the main class of the Manifest | |
| 70 | * @param mainClass | |
| 71 | */ | |
| 72 | 2 | public void setMainClass(String mainClass) { |
| 73 | 2 | _mainClass = mainClass; |
| 74 | 2 | _rawManifest = null; |
| 75 | } | |
| 76 | ||
| 77 | 0 | public void setManifestContents(String rawManifest) { |
| 78 | 0 | _rawManifest = rawManifest; |
| 79 | 0 | _mainClass = null; |
| 80 | } | |
| 81 | ||
| 82 | /** Get an input stream to the contents of the manifest file | |
| 83 | * @return an InputStream whose contents are the contents of the Manifest file | |
| 84 | */ | |
| 85 | 6 | protected InputStream getInputStream() { |
| 86 | // NOTE: All significant lines in the manifest MUST end in the end of line character | |
| 87 | ||
| 88 | 6 | final StringBuilder sbuf = new StringBuilder(); |
| 89 | 6 | sbuf.append(Attributes.Name.MANIFEST_VERSION.toString()); |
| 90 | 6 | sbuf.append(": 1.0" + StringOps.EOL); |
| 91 | 6 | if( !_classPaths.isEmpty() ) { |
| 92 | 3 | Iterator<String> iter = _classPaths.iterator(); |
| 93 | 3 | sbuf.append(Attributes.Name.CLASS_PATH.toString()); |
| 94 | 3 | sbuf.append(":"); |
| 95 | 3 | while (iter.hasNext()) { |
| 96 | 5 | sbuf.append(" "); |
| 97 | 5 | sbuf.append(iter.next()); |
| 98 | } | |
| 99 | 3 | sbuf.append(StringOps.EOL); |
| 100 | } | |
| 101 | 6 | if( _mainClass != null ) { |
| 102 | 2 | sbuf.append(Attributes.Name.MAIN_CLASS.toString()); |
| 103 | 2 | sbuf.append(": "); |
| 104 | 2 | sbuf.append(_mainClass); |
| 105 | 2 | sbuf.append(StringOps.EOL); |
| 106 | } | |
| 107 | ||
| 108 | 6 | if(_rawManifest != null) { |
| 109 | 0 | sbuf.append(_rawManifest); |
| 110 | ||
| 111 | 0 | if(!_rawManifest.endsWith(StringOps.EOL)) |
| 112 | 0 | sbuf.append(StringOps.EOL); |
| 113 | } | |
| 114 | ||
| 115 | 6 | try { return new ByteArrayInputStream(sbuf.toString().getBytes("UTF-8")); } |
| 116 | 0 | catch (UnsupportedEncodingException e) { throw new UnexpectedException(e); } |
| 117 | } | |
| 118 | ||
| 119 | /** Get the Manifest object that this object created. | |
| 120 | * @return the Manifest that this builder created | |
| 121 | */ | |
| 122 | 6 | public Manifest getManifest() { |
| 123 | 6 | try { |
| 124 | 6 | Manifest m = new Manifest(); |
| 125 | 6 | m.read(getInputStream()); |
| 126 | 6 | return m; |
| 127 | } | |
| 128 | catch (IOException e) { | |
| 129 | 0 | e.printStackTrace(); |
| 130 | 0 | return null; |
| 131 | } | |
| 132 | } | |
| 133 | } |
|
||||||||||