Permalink
Browse files

Some dabbling with parsing

  • Loading branch information...
djspiewak committed Mar 24, 2008
1 parent 203a8b7 commit e8f6d29693c4eccdb9fa9a339e2cb258df9d8cbb
@@ -0,0 +1,71 @@
/*
* Created on Mar 23, 2008
*/
package com.googlecode.jbencode;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import com.googlecode.jbencode.factories.DictionaryValueFactory;
import com.googlecode.jbencode.factories.IntegerValueFactory;
import com.googlecode.jbencode.factories.ListValueFactory;
import com.googlecode.jbencode.factories.StringValueFactory;
import com.googlecode.jbencode.factories.ValueFactory;
import com.googlecode.jbencode.primitive.StringValue;
/**
* @author Daniel Spiewak
*/
public final class Parser {
private final Map<Byte, ValueFactory<?>> factories;
private final ValueFactory<StringValue> stringFactory = new StringValueFactory();
public Parser() {
factories = new HashMap<Byte, ValueFactory<?>>();
addFactory(new IntegerValueFactory());
addFactory(new ListValueFactory());
addFactory(new DictionaryValueFactory());
}
public void addFactory(ValueFactory<?> factory) {
factories.put(factory.getPrefix(), factory);
}
public Value<?> parse(InputStream is) throws IOException {
int i = is.read();
if (i < 255) {
byte b = (byte) i;
ValueFactory<?> factory = factories.get(b);
if (factory != null) {
return factory.createValue(is);
} else if (b > '0' && b < '9') {
return readString(is, b - '0');
} else if (b == ' ' || b == '\n' || b == '\r' || b == '\t') {
return parse(is); // loop state
}
}
throw new IOException("Unexpected character in the parse stream: " + Character.forDigit(i, 10));
}
private StringValue readString(InputStream is, long length) throws IOException {
int i = is.read();
if (i < 255) {
byte b = (byte) i;
if (b == ':') {
return stringFactory.createValue(new SubStream(is, length));
} else if (b > '0' && b < '9') {
return readString(is, (length * 10) + b - '0');
}
}
throw new IOException("Unexpected character in the parse stream: " + Character.forDigit(i, 10));
}
}
@@ -0,0 +1,36 @@
/*
* Created on Mar 23, 2008
*/
package com.googlecode.jbencode;
import java.io.IOException;
import java.io.InputStream;
/**
* @author Daniel Spiewak
*/
class SubStream extends InputStream {
private final InputStream is;
private final long length;
private long index = 0;
public SubStream(InputStream is, long length) {
this.is = is;
this.length = length;
}
@Override
public int available() throws IOException {
return (int) length;
}
@Override
public int read() throws IOException {
if (index++ < length) {
return is.read();
}
throw new IOException("Stream segment overrun");
}
}
@@ -0,0 +1,11 @@
/*
* Created on Mar 23, 2008
*/
package com.googlecode.jbencode;
/**
* @author Daniel Spiewak
*/
public interface Value<T> {
public T resolve();
}
@@ -12,9 +12,9 @@
* @author Daniel Spiewak
*/
public abstract class CompositeType implements Type {
private final char prefix;
private final byte prefix;
public CompositeType(char prefix) {
public CompositeType(byte prefix) {
this.prefix = prefix;
}
@@ -0,0 +1,21 @@
/*
* Created on Mar 23, 2008
*/
package com.googlecode.jbencode.composite;
import java.util.Iterator;
import com.googlecode.jbencode.Value;
/**
* @author Daniel Spiewak
*/
public abstract class CompositeValue<T,V> implements Value<T>, Iterator<V>, Iterable<V> {
public Iterator<V> iterator() {
return this;
}
public void remove() {
}
}
@@ -15,7 +15,7 @@
public abstract class DictionaryType extends CompositeType {
public DictionaryType() {
super('d');
super((byte) 'd');
}
@Override
@@ -0,0 +1,22 @@
/*
* Created on Mar 23, 2008
*/
package com.googlecode.jbencode.composite;
/**
* @author Daniel Spiewak
*/
public class DictionaryValue extends CompositeValue<DictionaryValue, EntryPair> {
public DictionaryValue resolve() {
return this;
}
public boolean hasNext() {
return false;
}
public EntryPair next() {
return null;
}
}
@@ -0,0 +1,25 @@
/*
* Created on Mar 23, 2008
*/
package com.googlecode.jbencode.composite;
import com.googlecode.jbencode.Value;
import com.googlecode.jbencode.primitive.StringValue;
/**
* @author Daniel Spiewak
*/
public class EntryPair implements Value<EntryPair> {
public EntryPair resolve() {
return this;
}
public StringValue getKey() {
return null;
}
public Value<?> getValue() {
return null;
}
}
@@ -12,7 +12,7 @@
public abstract class ListType extends CompositeType {
public ListType() {
super('l');
super((byte) 'l');
}
@Override
@@ -0,0 +1,24 @@
/*
* Created on Mar 23, 2008
*/
package com.googlecode.jbencode.composite;
import com.googlecode.jbencode.Value;
/**
* @author Daniel Spiewak
*/
public class ListValue extends CompositeValue<ListValue, Value<?>> {
public ListValue resolve() {
return this;
}
public boolean hasNext() {
return false;
}
public Value<?> next() {
return null;
}
}
@@ -0,0 +1,22 @@
/*
* Created on Mar 23, 2008
*/
package com.googlecode.jbencode.factories;
import java.io.InputStream;
import com.googlecode.jbencode.composite.DictionaryValue;
/**
* @author Daniel Spiewak
*/
public class DictionaryValueFactory implements ValueFactory<DictionaryValue> {
public DictionaryValue createValue(InputStream is) {
return null;
}
public byte getPrefix() {
return 'd';
}
}
@@ -0,0 +1,22 @@
/*
* Created on Mar 23, 2008
*/
package com.googlecode.jbencode.factories;
import java.io.InputStream;
import com.googlecode.jbencode.primitive.IntegerValue;
/**
* @author Daniel Spiewak
*/
public class IntegerValueFactory implements ValueFactory<IntegerValue> {
public IntegerValue createValue(InputStream is) {
return null;
}
public byte getPrefix() {
return 'i';
}
}
@@ -0,0 +1,22 @@
/*
* Created on Mar 23, 2008
*/
package com.googlecode.jbencode.factories;
import java.io.InputStream;
import com.googlecode.jbencode.composite.ListValue;
/**
* @author Daniel Spiewak
*/
public class ListValueFactory implements ValueFactory<ListValue> {
public ListValue createValue(InputStream is) {
return null;
}
public byte getPrefix() {
return 'l';
}
}
@@ -0,0 +1,22 @@
/*
* Created on Mar 23, 2008
*/
package com.googlecode.jbencode.factories;
import java.io.InputStream;
import com.googlecode.jbencode.primitive.StringValue;
/**
* @author Daniel Spiewak
*/
public class StringValueFactory implements ValueFactory<StringValue> {
public StringValue createValue(InputStream is) {
return null;
}
public byte getPrefix() {
return 0;
}
}
@@ -0,0 +1,18 @@
/*
* Created on Mar 23, 2008
*/
package com.googlecode.jbencode.factories;
import java.io.InputStream;
import com.googlecode.jbencode.Value;
/**
* @author Daniel Spiewak
*/
public interface ValueFactory<T extends Value<?>> {
public byte getPrefix();
public T createValue(InputStream is);
}
@@ -13,7 +13,7 @@
private final long value;
public IntegerType(long value) {
super('i');
super((byte) 'i');
this.value = value;
}
@@ -0,0 +1,15 @@
/*
* Created on Mar 23, 2008
*/
package com.googlecode.jbencode.primitive;
import com.googlecode.jbencode.Value;
/**
* @author Daniel Spiewak
*/
public class IntegerValue implements Value<Integer> {
public Integer resolve() {
return null;
}
}
@@ -10,9 +10,9 @@
* @author Daniel Spiewak
*/
public abstract class InvariantPrimitiveType extends PrimitiveType {
private final char prefix;
private final byte prefix;
public InvariantPrimitiveType(char prefix) {
public InvariantPrimitiveType(byte prefix) {
this.prefix = prefix;
}
Oops, something went wrong.

0 comments on commit e8f6d29

Please sign in to comment.