enhanced template engine

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7783 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 14 years ago
parent aa6c32d753
commit f30d36b101

@ -147,6 +147,13 @@ public final class TemplateEngine {
public final static byte[] ul = "_".getBytes();
private final static byte[] alternative_which = ASCII.getBytes(" type=\"alternative\" which=\"");
private final static byte[] multi_num = ASCII.getBytes(" type=\"multi\" num=\"");
private final static byte[] open_endtag = ASCII.getBytes("</");
private final static byte[] close_quotetagn = ASCII.getBytes("\">\n");
private final static byte[] close_tagn = ASCII.getBytes(">\n");
/**
* transfer until a specified pattern is found; everything but the pattern is transfered so far
* the function returns true, if the pattern is found
@ -237,16 +244,16 @@ public final class TemplateEngine {
structure.append('<')
.append(multi_key)
.append(UTF8.getBytes(" type=\"multi\" num=\""))
.append(UTF8.getBytes(Integer.toString(num)))
.append(UTF8.getBytes("\">\n"));
.append(multi_num)
.append(ASCII.getBytes(Integer.toString(num)))
.append(close_quotetagn);
for(int i=0;i < num;i++) {
final PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(text));
//System.out.println("recursing with text(prefix="+ multi_key + "_" + i + "_" +"):"); //DEBUG
//System.out.println(text);
structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,multi_key,i)));
}//for
structure.append(UTF8.getBytes("</")).append(multi_key).append(UTF8.getBytes(">\n"));
structure.append(open_endtag).append(multi_key).append(close_tagn);
} else {//transferUntil
Log.logSevere("TEMPLATE", "No Close Key found for #{"+UTF8.String(multi_key)+"}#"); //prefix here?
}
@ -283,7 +290,7 @@ public final class TemplateEngine {
PushbackInputStream pis2;
if (byName) {
//TODO: better Error Handling
transferUntil(pis, keyStream, appendBytes(UTF8.getBytes("%%"), patternName, null, null));
transferUntil(pis, keyStream, appendBytes(ASCII.getBytes("%%"), patternName, null, null));
if(pis.available()==0){
Log.logSevere("TEMPLATE", "No such Template: %%" + UTF8.String(patternName));
return structure.getBytes();
@ -292,7 +299,7 @@ public final class TemplateEngine {
transferUntil(pis, keyStream, dpdpa);
pis2 = new PushbackInputStream(new ByteArrayInputStream(keyStream.toByteArray()));
structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key)));
transferUntil(pis, keyStream, appendBytes(UTF8.getBytes("#(/"), key, UTF8.getBytes(")#"), null));
transferUntil(pis, keyStream, appendBytes(ASCII.getBytes("#(/"), key, ASCII.getBytes(")#"), null));
if(pis.available()==0){
Log.logSevere("TEMPLATE", "No Close Key found for #("+UTF8.String(key)+")# (by Name)");
}
@ -308,16 +315,16 @@ public final class TemplateEngine {
if (java.util.Arrays.equals(keyStream.toByteArray(),appendBytes(slashChar, key, null,null))) {
pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes()));
//this maybe the wrong, but its the last
structure.append('<').append(key).append(UTF8.getBytes(" type=\"alternative\" which=\"")).append(UTF8.getBytes(Integer.toString(whichPattern))).append(UTF8.getBytes("\" found=\"0\">\n"));
structure.append('<').append(key).append(alternative_which).append(ASCII.getBytes(Integer.toString(whichPattern))).append(ASCII.getBytes("\" found=\"0\">\n"));
structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key)));
structure.append(UTF8.getBytes("</")).append(key).append(UTF8.getBytes(">\n"));
structure.append(open_endtag).append(key).append(close_tagn);
found=true;
}else if(others >0 && keyStream.toString().startsWith("/")){ //close nested
others--;
text.append(aOpen).append(keyStream.toByteArray()).append(UTF8.getBytes(")#"));
text.append(aOpen).append(keyStream.toByteArray()).append(ASCII.getBytes(")#"));
} else { //nested
others++;
text.append(aOpen).append(keyStream.toByteArray()).append(UTF8.getBytes(")#"));
text.append(aOpen).append(keyStream.toByteArray()).append(ASCII.getBytes(")#"));
}
keyStream.reset(); //reset stream
continue;
@ -330,11 +337,11 @@ public final class TemplateEngine {
if ((bb & 0xFF) == ':'){
if(currentPattern == whichPattern){ //found the pattern
pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes()));
structure.append('<').append(key).append(UTF8.getBytes(" type=\"alternative\" which=\"")).append(UTF8.getBytes(Integer.toString(whichPattern))).append(UTF8.getBytes("\" found=\"0\">\n"));
structure.append('<').append(key).append(alternative_which).append(ASCII.getBytes(Integer.toString(whichPattern))).append(ASCII.getBytes("\" found=\"0\">\n"));
structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key)));
structure.append(UTF8.getBytes("</")).append(key).append(UTF8.getBytes(">\n"));
structure.append(open_endtag).append(key).append(close_tagn);
transferUntil(pis, keyStream, appendBytes(UTF8.getBytes("#(/"),key,UTF8.getBytes(")#"),null));//to #(/key)#.
transferUntil(pis, keyStream, appendBytes(ASCII.getBytes("#(/"),key,ASCII.getBytes(")#"),null));//to #(/key)#.
found=true;
}
@ -342,7 +349,7 @@ public final class TemplateEngine {
text.clear();
continue;
}
text.append(UTF8.getBytes(":"));
text.append(':');
}
if(!found){
text.append((byte)bb);/*
@ -361,11 +368,11 @@ public final class TemplateEngine {
key = keyStream.toByteArray();
final String patternKey = getPatternKey(prefix, key);
replacement = replacePattern(patternKey, pattern, dflt); //replace
structure.append(ASCII.getBytes("<")).append(key)
structure.append('<').append(key)
.append(ASCII.getBytes(" type=\"normal\">\n"));
structure.append(replacement);
structure.append(ASCII.getBytes("</")).append(key)
.append(ASCII.getBytes(">\n"));
.append(close_tagn);
FileUtils.copy(replacement, out);
} else {
@ -395,7 +402,7 @@ public final class TemplateEngine {
//Read the Include
String line = "";
while ((line = br.readLine()) != null) {
include.append(UTF8.getBytes(line)).append(UTF8.getBytes(de.anomic.server.serverCore.CRLF_STRING));
include.append(UTF8.getBytes(line)).append(ASCII.getBytes(de.anomic.server.serverCore.CRLF_STRING));
}
} catch (final IOException e) {
//file not found?
@ -404,9 +411,9 @@ public final class TemplateEngine {
if (br != null) try { br.close(); br=null; } catch (final Exception e) {}
}
final PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(include.getBytes()));
structure.append(UTF8.getBytes("<fileinclude file=\"")).append(filename).append(UTF8.getBytes(">\n"));
structure.append(ASCII.getBytes("<fileinclude file=\"")).append(filename).append(close_tagn);
structure.append(writeTemplate(pis2, out, pattern, dflt, prefix));
structure.append(UTF8.getBytes("</fileinclude>\n"));
structure.append(ASCII.getBytes("</fileinclude>\n"));
}
}
@ -453,7 +460,7 @@ public final class TemplateEngine {
private final static byte[] newPrefix(final byte[] oldPrefix, final byte[] multi_key, final int i) {
final ByteBuffer newPrefix = new ByteBuffer(oldPrefix.length + multi_key.length + 8);
newPrefix.append(oldPrefix).append(multi_key).append(ul).append(UTF8.getBytes(Integer.toString(i))).append(ul);
newPrefix.append(oldPrefix).append(multi_key).append(ul).append(ASCII.getBytes(Integer.toString(i))).append(ul);
try {
newPrefix.close();
} catch (final IOException e) {

@ -1,4 +1,4 @@
// ByteBuffer.java
// ByteBuffer.java
// ---------------------------
// (C) by Michael Peter Christen; mc@yacy.net
// first published on http://www.anomic.de
@ -38,73 +38,73 @@ import java.util.Properties;
import net.yacy.cora.document.UTF8;
public final class ByteBuffer extends OutputStream {
public static final byte singlequote = (byte) 39;
public static final byte doublequote = (byte) 34;
public static final byte equal = (byte) '=';
private byte[] buffer;
private int offset;
private int length;
public ByteBuffer() {
buffer = new byte[10];
length = 0;
offset = 0;
this.buffer = new byte[10];
this.length = 0;
this.offset = 0;
}
public ByteBuffer(final int initLength) {
this.buffer = new byte[initLength];
this.length = 0;
this.offset = 0;
}
public ByteBuffer(final byte[] bb) {
buffer = bb;
length = bb.length;
offset = 0;
this.buffer = bb;
this.length = bb.length;
this.offset = 0;
}
public ByteBuffer(final String s) {
buffer = s.getBytes(UTF8.charset);
length = buffer.length;
offset = 0;
this.buffer = s.getBytes(UTF8.charset);
this.length = this.buffer.length;
this.offset = 0;
}
public ByteBuffer(final byte[] bb, final int initLength) {
this.buffer = new byte[initLength];
System.arraycopy(bb, 0, buffer, 0, bb.length);
length = bb.length;
offset = 0;
System.arraycopy(bb, 0, this.buffer, 0, bb.length);
this.length = bb.length;
this.offset = 0;
}
public ByteBuffer(final byte[] bb, final int of, final int le) {
if (of * 2 > bb.length) {
buffer = new byte[le];
System.arraycopy(bb, of, buffer, 0, le);
length = le;
offset = 0;
this.buffer = new byte[le];
System.arraycopy(bb, of, this.buffer, 0, le);
this.length = le;
this.offset = 0;
} else {
buffer = bb;
length = le;
offset = of;
this.buffer = bb;
this.length = le;
this.offset = of;
}
}
public ByteBuffer(final ByteBuffer bb) {
buffer = bb.buffer;
length = bb.length;
offset = bb.offset;
this.buffer = bb.buffer;
this.length = bb.length;
this.offset = bb.offset;
}
public ByteBuffer(final File f) throws IOException {
// initially fill the byte buffer with the content of a file
if (f.length() > Integer.MAX_VALUE) throw new IOException("file is too large for buffering");
length = (int) f.length();
buffer = new byte[length];
offset = 0;
this.length = (int) f.length();
this.buffer = new byte[this.length];
this.offset = 0;
try {
final FileInputStream fis = new FileInputStream(f);
@ -113,7 +113,7 @@ public final class ByteBuffer extends OutputStream {
// while ((l = fis.read(buf)) > 0) {
// System.arraycopy(buf, 0, buffer, p, l);
// p += l;
/*int l =*/ fis.read(buffer);
/*int l =*/ fis.read(this.buffer);
// }
fis.close();
} catch (final FileNotFoundException e) {
@ -126,68 +126,77 @@ public final class ByteBuffer extends OutputStream {
this.length = 0;
this.offset = 0;
}
public int length() {
return length;
return this.length;
}
private void grow() {
int newsize = buffer.length * 2 + 1;
int newsize = this.buffer.length * 2 + 1;
if (newsize < 256) newsize = 256;
byte[] tmp = new byte[newsize];
System.arraycopy(buffer, offset, tmp, 0, length);
buffer = tmp;
offset = 0;
final byte[] tmp = new byte[newsize];
System.arraycopy(this.buffer, this.offset, tmp, 0, this.length);
this.buffer = tmp;
this.offset = 0;
}
public void write(final int b) {
write((byte) (b & 0xff));
}
public void write(final char b) {
write((byte) b);
}
public void write(final byte b) {
if (offset + length + 1 > buffer.length) grow();
buffer[offset + length++] = b;
if (this.offset + this.length + 1 > this.buffer.length) grow();
this.buffer[this.offset + this.length++] = b;
}
@Override
public void write(final byte[] bb) {
write(bb, 0, bb.length);
}
@Override
public void write(final byte[] bb, final int of, final int le) {
while (offset + length + le > buffer.length) grow();
System.arraycopy(bb, of, buffer, offset + length, le);
length += le;
while (this.offset + this.length + le > this.buffer.length) grow();
System.arraycopy(bb, of, this.buffer, this.offset + this.length, le);
this.length += le;
}
// overwrite does not increase the 'length' write position pointer!
public void overwrite(final int pos, final int b) {
overwrite(pos, (byte) (b & 0xff));
}
public void overwrite(final int pos, final byte b) {
if (offset + pos + 1 > buffer.length) grow();
buffer[offset + pos] = b;
if (pos >= length) length = pos + 1;
if (this.offset + pos + 1 > this.buffer.length) grow();
this.buffer[this.offset + pos] = b;
if (pos >= this.length) this.length = pos + 1;
}
public void overwrite(final int pos, final byte[] bb) {
overwrite(pos, bb, 0, bb.length);
}
public void overwrite(final int pos, final byte[] bb, final int of, final int le) {
while (offset + pos + le > buffer.length) grow();
System.arraycopy(bb, of, buffer, offset + pos, le);
if (pos + le > length) length = pos + le;
while (this.offset + pos + le > this.buffer.length) grow();
System.arraycopy(bb, of, this.buffer, this.offset + pos, le);
if (pos + le > this.length) this.length = pos + le;
}
public ByteBuffer append(final byte b) {
write(b);
return this;
}
public ByteBuffer append(final char b) {
write(b);
return this;
}
public ByteBuffer append(final int i) {
write((byte) (i & 0xFF));
return this;
@ -206,10 +215,10 @@ public final class ByteBuffer extends OutputStream {
public ByteBuffer append(final String s) {
return append(UTF8.getBytes(s));
}
public ByteBuffer append(final String s, final String charset) throws UnsupportedEncodingException {
return append(s.getBytes(charset));
}
}
public ByteBuffer append(final ByteBuffer bb) {
return append(bb.buffer, bb.offset, bb.length);
@ -220,22 +229,22 @@ public final class ByteBuffer extends OutputStream {
if (o instanceof byte[]) return append((byte[]) o);
return null;
}
public byte byteAt(final int pos) {
if (pos > length) return -1;
return buffer[offset + pos];
if (pos > this.length) return -1;
return this.buffer[this.offset + pos];
}
public void deleteByteAt(final int pos) {
if (pos < 0) return;
if (pos >= length) return;
if (pos == length - 1) {
length--;
if (pos >= this.length) return;
if (pos == this.length - 1) {
this.length--;
} else {
System.arraycopy(buffer, offset + pos + 1, buffer, offset + pos, length - pos - 1);
System.arraycopy(this.buffer, this.offset + pos + 1, this.buffer, this.offset + pos, this.length - pos - 1);
}
}
public int indexOf(final byte b) {
return indexOf(b, 0);
}
@ -245,22 +254,22 @@ public final class ByteBuffer extends OutputStream {
}
public int indexOf(final byte b, final int start) {
if (start >= length) return -1;
for (int i = start; i < length; i++) if (buffer[offset + i] == b) return i;
if (start >= this.length) return -1;
for (int i = start; i < this.length; i++) if (this.buffer[this.offset + i] == b) return i;
return -1;
}
public int indexOf(final byte[] bs, final int start) {
if (start + bs.length > length) return -1;
loop: for (int i = start; i <= length - bs.length; i++) {
if (start + bs.length > this.length) return -1;
loop: for (int i = start; i <= this.length - bs.length; i++) {
// first test only first byte
if (buffer[offset + i] != bs[0]) continue loop;
if (this.buffer[this.offset + i] != bs[0]) continue loop;
// then test all remaining bytes
for (int j = 1; j < bs.length; j++) {
if (buffer[offset + i + j] != bs[j]) continue loop;
if (this.buffer[this.offset + i + j] != bs[j]) continue loop;
}
// found hit
return i;
}
@ -268,22 +277,22 @@ public final class ByteBuffer extends OutputStream {
}
public int lastIndexOf(final byte b) {
for (int i = length - 1; i >= 0; i--) if (buffer[offset + i] == b) return i;
for (int i = this.length - 1; i >= 0; i--) if (this.buffer[this.offset + i] == b) return i;
return -1;
}
public boolean startsWith(final byte[] bs) {
return startsWith(bs, 0);
}
public boolean startsWith(final byte[] bs, final int start) {
if (length - start < bs.length) return false;
if (this.length - start < bs.length) return false;
for (int i = 0; i < bs.length; i++) {
if (buffer[offset + i + start] != bs[i]) return false;
if (this.buffer[this.offset + i + start] != bs[i]) return false;
}
return true;
}
public byte[] getBytes() {
return getBytes(0);
}
@ -291,17 +300,17 @@ public final class ByteBuffer extends OutputStream {
public byte[] getBytes(final int start) {
return getBytes(start, this.length);
}
public byte[] getBytes(final int start, final int len) {
// start is inclusive, end is exclusive
if (len > length) throw new IndexOutOfBoundsException("getBytes: len > length");
if (start > length) throw new IndexOutOfBoundsException("getBytes: start > length");
if ((start == 0) && (len == length) && (len == buffer.length)) return buffer;
if (len > this.length) throw new IndexOutOfBoundsException("getBytes: len > length");
if (start > this.length) throw new IndexOutOfBoundsException("getBytes: start > length");
if ((start == 0) && (len == this.length) && (len == this.buffer.length)) return this.buffer;
final byte[] tmp = new byte[len];
System.arraycopy(buffer, offset + start, tmp, 0, len);
System.arraycopy(this.buffer, this.offset + start, tmp, 0, len);
return tmp;
}
public ByteBuffer trim(final int start) {
this.offset += start;
this.length -= start;
@ -317,14 +326,14 @@ public final class ByteBuffer extends OutputStream {
public ByteBuffer trim() {
int l = 0;
while ((l < length) && (buffer[offset + l] <= 32)) {
while ((l < this.length) && (this.buffer[this.offset + l] <= 32)) {
l++;
}
int r = length - 1;
while ((r > l) && (buffer[offset + r] <= 32)) r--;
int r = this.length - 1;
while ((r > l) && (this.buffer[this.offset + r] <= 32)) r--;
return trim(l, r - l + 1);
}
public int isUTF8char(final int start) {
// a sequence of bytes is a utf-8 character, if one of the following 4 conditions is true:
// - ASCII equivalence range; (first) byte begins with zero
@ -332,20 +341,20 @@ public final class ByteBuffer extends OutputStream {
// - first byte begins with 1110, the following two bytes begin with 10
// - First byte begins with 11110, the following three bytes begin with 10
// if an utf-8 sequence is detected, the length of the sequence is returned. -1 otherwise
if ((start < length) &&
((buffer[offset + start] & 0x80) != 0)) return 1;
if ((start < length - 1) &&
((buffer[offset + start ] & 0xE0) == 0xC0) &&
((buffer[offset + start + 1] & 0xC0) == 0x80)) return 2;
if ((start < length - 2) &&
((buffer[offset + start ] & 0xF0) == 0xE0) &&
((buffer[offset + start + 1] & 0xC0) == 0x80) &&
((buffer[offset + start + 2] & 0xC0) == 0x80)) return 3;
if ((start < length - 3) &&
((buffer[offset + start ] & 0xF8) == 0xF0) &&
((buffer[offset + start + 1] & 0xC0) == 0x80) &&
((buffer[offset + start + 2] & 0xC0) == 0x80) &&
((buffer[offset + start + 3] & 0xC0) == 0x80)) return 4;
if ((start < this.length) &&
((this.buffer[this.offset + start] & 0x80) != 0)) return 1;
if ((start < this.length - 1) &&
((this.buffer[this.offset + start ] & 0xE0) == 0xC0) &&
((this.buffer[this.offset + start + 1] & 0xC0) == 0x80)) return 2;
if ((start < this.length - 2) &&
((this.buffer[this.offset + start ] & 0xF0) == 0xE0) &&
((this.buffer[this.offset + start + 1] & 0xC0) == 0x80) &&
((this.buffer[this.offset + start + 2] & 0xC0) == 0x80)) return 3;
if ((start < this.length - 3) &&
((this.buffer[this.offset + start ] & 0xF8) == 0xF0) &&
((this.buffer[this.offset + start + 1] & 0xC0) == 0x80) &&
((this.buffer[this.offset + start + 2] & 0xC0) == 0x80) &&
((this.buffer[this.offset + start + 3] & 0xC0) == 0x80)) return 4;
return -1;
}
@ -353,52 +362,52 @@ public final class ByteBuffer extends OutputStream {
// returns true, if trim() would result in an empty serverByteBuffer
if (includeNonLetterBytes) {
byte b;
for (int i = 0; i < length; i++) {
b = buffer[offset + i];
for (int i = 0; i < this.length; i++) {
b = this.buffer[this.offset + i];
if (((b >= '0') && (b <= '9')) || ((b >= 'A') && (b <= 'Z')) || ((b >= 'a') && (b <= 'z'))) return false;
}
} else {
for (int i = 0; i < length; i++) if (buffer[offset + i] > 32) return false;
for (int i = 0; i < this.length; i++) if (this.buffer[this.offset + i] > 32) return false;
}
return true;
}
public int whitespaceStart(final boolean includeNonLetterBytes) {
// returns number of whitespace bytes at the beginning of text
if (includeNonLetterBytes) {
byte b;
for (int i = 0; i < length; i++) {
b = buffer[offset + i];
for (int i = 0; i < this.length; i++) {
b = this.buffer[this.offset + i];
if (((b >= '0') && (b <= '9')) || ((b >= 'A') && (b <= 'Z')) || ((b >= 'a') && (b <= 'z'))) return i;
}
} else {
for (int i = 0; i < length; i++) if (buffer[offset + i] > 32) return i;
for (int i = 0; i < this.length; i++) if (this.buffer[this.offset + i] > 32) return i;
}
return length;
return this.length;
}
public int whitespaceEnd(final boolean includeNonLetterBytes) {
// returns position of whitespace at the end of text
if (includeNonLetterBytes) {
byte b;
for (int i = length - 1; i >= 0; i--) {
b = buffer[offset + i];
for (int i = this.length - 1; i >= 0; i--) {
b = this.buffer[this.offset + i];
if (((b >= '0') && (b <= '9')) || ((b >= 'A') && (b <= 'Z')) || ((b >= 'a') && (b <= 'z'))) return i + 1;
}
} else {
for (int i = length - 1; i >= 0; i--) if (buffer[offset + i] > 32) return i + 1;
for (int i = this.length - 1; i >= 0; i--) if (this.buffer[this.offset + i] > 32) return i + 1;
}
return 0;
}
@Override
public String toString() {
return UTF8.String(buffer, offset, length);
return UTF8.String(this.buffer, this.offset, this.length);
}
public String toString(final int left, final int length) {
return UTF8.String(buffer, offset + left, length);
return UTF8.String(this.buffer, this.offset + left, length);
}
public StringBuilder toStringBuilder(final int left, final int length, final int sblength) {
@ -406,85 +415,85 @@ public final class ByteBuffer extends OutputStream {
final StringBuilder sb = new StringBuilder(sblength);
int i = 0;
sb.setLength(length);
for (int j = left; j < left + length; j++) sb.setCharAt(i++, (char) buffer[j]);
for (int j = left; j < left + length; j++) sb.setCharAt(i++, (char) this.buffer[j]);
return sb;
}
public Properties propParser(final String charset) {
// extract a=b or a="b" - relations from the buffer
int pos = offset;
int pos = this.offset;
int start;
String key;
final Properties p = new Properties();
// eat up spaces at beginning
while ((pos < length) && (buffer[pos] <= 32)) pos++;
while (pos < length) {
while ((pos < this.length) && (this.buffer[pos] <= 32)) pos++;
while (pos < this.length) {
// pos is at start of next key
start = pos;
while ((pos < length) && (buffer[pos] != equal)) pos++;
if (pos >= length) break; // this is the case if we found no equal
while ((pos < this.length) && (this.buffer[pos] != equal)) pos++;
if (pos >= this.length) break; // this is the case if we found no equal
try {
key = new String(buffer, start, pos - start, charset).trim().toLowerCase();
key = new String(this.buffer, start, pos - start, charset).trim().toLowerCase();
} catch (final UnsupportedEncodingException e1) {
key = UTF8.String(buffer, start, pos - start).trim().toLowerCase();
key = UTF8.String(this.buffer, start, pos - start).trim().toLowerCase();
}
// we have a key
pos++;
// find start of value
while ((pos < length) && (buffer[pos] <= 32)) pos++;
while ((pos < this.length) && (this.buffer[pos] <= 32)) pos++;
// doublequotes are obligatory. However, we want to be fuzzy if they
// are ommittet
if (pos >= length) {
if (pos >= this.length) {
// error case: input ended too early
break;
} else if (buffer[pos] == doublequote) {
} else if (this.buffer[pos] == doublequote) {
// search next doublequote
pos++;
start = pos;
while ((pos < length) && (buffer[pos] != doublequote)) pos++;
if (pos >= length) break; // this is the case if we found no parent doublequote
while ((pos < this.length) && (this.buffer[pos] != doublequote)) pos++;
if (pos >= this.length) break; // this is the case if we found no parent doublequote
try {
p.setProperty(key, new String(buffer, start, pos - start,charset).trim());
p.setProperty(key, new String(this.buffer, start, pos - start,charset).trim());
} catch (final UnsupportedEncodingException e) {
p.setProperty(key, UTF8.String(buffer, start, pos - start).trim());
}
p.setProperty(key, UTF8.String(this.buffer, start, pos - start).trim());
}
pos++;
} else if (buffer[pos] == singlequote) {
} else if (this.buffer[pos] == singlequote) {
// search next singlequote
pos++;
start = pos;
while ((pos < length) && (buffer[pos] != singlequote)) pos++;
if (pos >= length) break; // this is the case if we found no parent singlequote
while ((pos < this.length) && (this.buffer[pos] != singlequote)) pos++;
if (pos >= this.length) break; // this is the case if we found no parent singlequote
try {
p.setProperty(key, new String(buffer, start, pos - start,charset).trim());
p.setProperty(key, new String(this.buffer, start, pos - start,charset).trim());
} catch (final UnsupportedEncodingException e) {
p.setProperty(key, UTF8.String(buffer, start, pos - start).trim());
p.setProperty(key, UTF8.String(this.buffer, start, pos - start).trim());
}
pos++;
} else {
// search next whitespace
start = pos;
while ((pos < length) && (buffer[pos] > 32)) pos++;
while ((pos < this.length) && (this.buffer[pos] > 32)) pos++;
try {
p.setProperty(key, new String(buffer, start, pos - start,charset).trim());
p.setProperty(key, new String(this.buffer, start, pos - start,charset).trim());
} catch (final UnsupportedEncodingException e) {
p.setProperty(key, UTF8.String(buffer, start, pos - start).trim());
p.setProperty(key, UTF8.String(this.buffer, start, pos - start).trim());
}
}
// pos should point now to a whitespace: eat up spaces
while ((pos < length) && (buffer[pos] <= 32)) pos++;
while ((pos < this.length) && (this.buffer[pos] <= 32)) pos++;
// go on with next loop
}
return p;
}
public static boolean equals(final byte[] buffer, final byte[] pattern) {
// compares two byte arrays: true, if pattern appears completely at offset position
if (buffer.length < pattern.length) return false;
for (int i = 0; i < pattern.length; i++) if (buffer[i] != pattern[i]) return false;
return true;
}
public static boolean equals(final byte[] buffer, final int offset, final byte[] pattern) {
// compares two byte arrays: true, if pattern appears completely at offset position
if (buffer.length < offset + pattern.length) return false;
@ -495,34 +504,34 @@ public final class ByteBuffer extends OutputStream {
public void reset() {
this.length = 0;
this.offset = 0;
}
public void reset(final int newSize) {
this.resize(newSize);
}
public void reset(final int newSize) {
resize(newSize);
this.reset();
}
}
public void resize(final int newSize) {
if(newSize < 0) throw new IllegalArgumentException("Illegal array size: " + newSize);
final byte[] v = new byte[newSize];
System.arraycopy(this.buffer,0,v,0,newSize > this.buffer.length ? this.buffer.length : newSize);
this.buffer = v;
this.buffer = v;
}
public void writeTo(final OutputStream dest) throws IOException {
dest.write(this.buffer, this.offset, this.length);
dest.flush();
}
public static boolean contains(Collection<byte[]> collection, byte[] key) {
for (byte[] v: collection) {
public static boolean contains(final Collection<byte[]> collection, final byte[] key) {
for (final byte[] v: collection) {
if (equals(v, key)) return true;
}
return false;
}
public static List<byte[]> split(byte[] b, byte s) {
ArrayList<byte[]> a = new ArrayList<byte[]>();
public static List<byte[]> split(final byte[] b, final byte s) {
final ArrayList<byte[]> a = new ArrayList<byte[]>();
int c = 0;
loop: while (c < b.length) {
int i = c;
@ -532,13 +541,13 @@ public final class ByteBuffer extends OutputStream {
}
if (i >= b.length) {
// nothing found; this is the end of the search
byte[] bb = new byte[b.length - c];
final byte[] bb = new byte[b.length - c];
System.arraycopy(b, c, bb, 0, bb.length);
a.add(bb);
break loop;
} else {
// found a separator
byte[] bb = new byte[i - c];
final byte[] bb = new byte[i - c];
System.arraycopy(b, c, bb, 0, bb.length);
a.add(bb);
c = i + 1;
@ -546,5 +555,5 @@ public final class ByteBuffer extends OutputStream {
}
return a;
}
}
Loading…
Cancel
Save