Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
344210 tycho-versions-plugin: preserve line ending style in MANIFEST.MF
Browse files Browse the repository at this point in the history
  • Loading branch information
b8 authored and oberlies committed Aug 15, 2011
1 parent 5027b3c commit 7dfcd7f
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,57 @@ public class ManifestAttribute {
private List<String> lines = new ArrayList<String>();

public ManifestAttribute(String str) {
lines.add(str);
lines.add(chopNewLine(str));
}

public ManifestAttribute(String name, String value) {
set(name, value);
}

public void add(String str) {
lines.add(str);
String choppedLine = chopNewLine(str);

if (!choppedLine.substring(0, 1).startsWith(" ")) {
throw new IllegalArgumentException("Additional attribute lines must start with a space.");
}
if (choppedLine.contains("\n") || choppedLine.contains("\r")) {
throw new IllegalArgumentException("Additional attribute line must not consist of multiple lines");
}

lines.add(choppedLine);
}

public void writeTo(Writer w) throws IOException {
for (int i = 0; i < lines.size(); i++) {
if (i > 0) {
w.write("\r\n ");
private String chopNewLine(String str) {
if (str.length() > 0) {
char lastChar = str.charAt(str.length() - 1);
if (lastChar == '\n' || lastChar == '\r' || lastChar == '\u2028' || lastChar == '\u2029'
|| lastChar == '\u0085') // see Scanner#LINE_SEPARATOR_PATTERN
{
return str.substring(0, str.length() - (str.endsWith("\r\n") ? 2 : 1));
}
w.write(lines.get(i));
}
return str;
}

public boolean hasName(String name) {
if (lines.size() > 0) {
return lines.get(0).startsWith(name + ": ");
/**
* Writes the lines to {@code w} using the given line termination chars. There will be a
* trailing newline!
*/
public void writeTo(Writer w, String lineTermination) throws IOException {
for (String line : lines) {
w.write(line);
w.write(lineTermination);
}
return false;
}

public boolean hasName(String name) {
return lines.get(0).startsWith(name + ": ");
}

public String getValue() {
StringBuilder sb = new StringBuilder();
for (String line : lines) {
sb.append(line);
StringBuilder sb = new StringBuilder(lines.get(0));
for (int i = 1; i < lines.size(); i++) {
sb.append(lines.get(i).substring(1));
}

int idx = sb.indexOf(": ");
Expand All @@ -61,12 +81,14 @@ public String getValue() {
}

public void set(String name, String value) {
String attribute = (name != null ? name.trim() : "") + ": " + (value != null ? value.trim() : "");

lines.clear();
StringBuilder sb = new StringBuilder(name + ": " + value);
for (int i = 71; i < sb.length(); i += 73) {
sb.insert(i, "\r\n ");
while (attribute.length() > 71) {
lines.add(attribute.substring(0, 70));
attribute = " " + attribute.substring(70);
}
lines.add(sb.toString());
lines.add(attribute);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PushbackReader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -32,12 +33,13 @@

public class MutableBundleManifest {

private List<ManifestAttribute> lines = new ArrayList<ManifestAttribute>();
private final List<ManifestAttribute> attributes = new ArrayList<ManifestAttribute>();

private String lineEnding = "";
private String unparsed;

public void add(ManifestAttribute line) {
lines.add(line);
public void add(ManifestAttribute attribute) {
attributes.add(attribute);
}

public static MutableBundleManifest read(File file) throws IOException {
Expand All @@ -50,32 +52,28 @@ public static MutableBundleManifest read(File file) throws IOException {
}

public static MutableBundleManifest read(InputStream is) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF8"));
PushbackReader br = new PushbackReader(new BufferedReader(new InputStreamReader(is, "UTF8")), 1);

MutableBundleManifest mf = new MutableBundleManifest();
ManifestAttribute curr = null;

String str;
while ((str = br.readLine()) != null) {
if (str.length() == 0) {
break;
}

if (str.length() == 0) {
while ((str = readLineWithLineEnding(br, mf)) != null) {
if (str.trim().length() == 0) {
break;
} else if (str.charAt(0) == ' ') {
if (curr == null) {
throw new IOException("");
}
curr.add(str.substring(1));
curr.add(str);
} else {
curr = new ManifestAttribute(str);
mf.add(curr);
}
}

if (str != null) {
StringBuilder sb = new StringBuilder("\r\n");
StringBuilder sb = new StringBuilder(str);
int ch;
while ((ch = br.read()) != -1) {
sb.append((char) ch);
Expand All @@ -86,6 +84,44 @@ public static MutableBundleManifest read(InputStream is) throws IOException {
return mf;
}

private static String readLineWithLineEnding(PushbackReader reader, MutableBundleManifest mf) throws IOException {
StringBuilder result = new StringBuilder();
int ch, lastch = -1;

while ((ch = reader.read()) != -1) {
if (lastch == '\r') {
if (ch == '\n') {
result.append((char) ch);
mf.setLineEndingWhenFirstLine("\r\n");
} else {
reader.unread(ch);
mf.setLineEndingWhenFirstLine("\r");
}
break;
}

result.append((char) ch);

if (ch == '\n' || ch == '\u2028' || ch == '\u2029' || ch == '\u0085') { // see Scanner#LINE_SEPARATOR_PATTERN
mf.setLineEndingWhenFirstLine(new String(new char[] { (char) ch }));
break;
}

lastch = ch;
}

if (result.length() > 0) {
return result.toString();
}
return null;
}

private void setLineEndingWhenFirstLine(String lineEnding) {
if (this.lineEnding.length() == 0 && lineEnding != null) {
this.lineEnding = lineEnding;
}
}

private void setUnparsed(String unparsed) {
this.unparsed = unparsed;
}
Expand All @@ -102,13 +138,9 @@ public static void write(MutableBundleManifest mf, File file) throws IOException
public static void write(MutableBundleManifest mf, OutputStream os) throws IOException {
Writer w = new OutputStreamWriter(os, "UTF8");

for (int i = 0; i < mf.lines.size(); i++) {
if (i > 0) {
w.write("\r\n");
}
mf.lines.get(i).writeTo(w);
for (ManifestAttribute attribute : mf.attributes) {
attribute.writeTo(w, mf.lineEnding);
}
w.write("\r\n");

if (mf.unparsed != null) {
w.write(mf.unparsed);
Expand All @@ -119,12 +151,18 @@ public static void write(MutableBundleManifest mf, OutputStream os) throws IOExc

public String getSymbolicName() {
ManifestElement[] id = parseHeader(Constants.BUNDLE_SYMBOLICNAME);
return id[0].getValue();
if (id != null && id.length > 0) {
return id[0].getValue();
}
return null;
}

public String getVersion() {
ManifestElement[] version = parseHeader(Constants.BUNDLE_VERSION);
return version[0].getValue();
if (version != null && version.length > 0) {
return version[0].getValue();
}
return null;
}

private ManifestElement[] parseHeader(String name) {
Expand All @@ -140,9 +178,9 @@ private ManifestElement[] parseHeader(String name) {
}

private ManifestAttribute getAttribute(String name) {
for (ManifestAttribute line : lines) {
if (line.hasName(name)) {
return line;
for (ManifestAttribute attribute : attributes) {
if (attribute.hasName(name)) {
return attribute;
}
}
return null;
Expand All @@ -153,7 +191,7 @@ public void setVersion(String version) {
if (attr != null) {
attr.set(Constants.BUNDLE_VERSION, version);
} else {
lines.add(new ManifestAttribute(Constants.BUNDLE_VERSION, version));
attributes.add(new ManifestAttribute(Constants.BUNDLE_VERSION, version));
}
}

Expand Down

0 comments on commit 7dfcd7f

Please sign in to comment.