diff --git a/java/src/main/java/org/msgpack/AbstractTemplate.java b/java/src/main/java/org/msgpack/AbstractTemplate.java new file mode 100644 index 00000000..5b4442e9 --- /dev/null +++ b/java/src/main/java/org/msgpack/AbstractTemplate.java @@ -0,0 +1,27 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack; + +import java.io.IOException; + +public abstract class AbstractTemplate implements Template { + public Object unpack(Unpacker pac) throws IOException, MessageTypeException { + return convert(pac.unpackObject()); + } +} + diff --git a/java/src/main/java/org/msgpack/MessageConverter.java b/java/src/main/java/org/msgpack/MessageConverter.java new file mode 100644 index 00000000..8388ddc9 --- /dev/null +++ b/java/src/main/java/org/msgpack/MessageConverter.java @@ -0,0 +1,25 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack; + +import java.io.IOException; + +public interface MessageConverter { + public Object convert(MessagePackObject obj) throws MessageTypeException; +} + diff --git a/java/src/main/java/org/msgpack/MessagePackObject.java b/java/src/main/java/org/msgpack/MessagePackObject.java index 2424446f..f7e9e0e6 100644 --- a/java/src/main/java/org/msgpack/MessagePackObject.java +++ b/java/src/main/java/org/msgpack/MessagePackObject.java @@ -132,5 +132,9 @@ public abstract class MessagePackObject implements Cloneable, MessagePackable { } abstract public Object clone(); + + public Object convert(Template tmpl) throws MessageTypeException { + return tmpl.convert(this); + } } diff --git a/java/src/main/java/org/msgpack/MessagePacker.java b/java/src/main/java/org/msgpack/MessagePacker.java new file mode 100644 index 00000000..05d4de5d --- /dev/null +++ b/java/src/main/java/org/msgpack/MessagePacker.java @@ -0,0 +1,25 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack; + +import java.io.IOException; + +public interface MessagePacker { + public void pack(Packer pk, Object target) throws IOException; +} + diff --git a/java/src/main/java/org/msgpack/MessageUnpacker.java b/java/src/main/java/org/msgpack/MessageUnpacker.java new file mode 100644 index 00000000..18172697 --- /dev/null +++ b/java/src/main/java/org/msgpack/MessageUnpacker.java @@ -0,0 +1,25 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack; + +import java.io.IOException; + +public interface MessageUnpacker { + public Object unpack(Unpacker pac) throws IOException, MessageTypeException; +} + diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java index 139b3b15..687f09cc 100644 --- a/java/src/main/java/org/msgpack/Packer.java +++ b/java/src/main/java/org/msgpack/Packer.java @@ -474,6 +474,8 @@ public class Packer { } else if(o instanceof BigInteger) { return packBigInteger((BigInteger)o); } else { + // FIXME check CustomPacker.get(o.getClass()); + // FIXME check annotations throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")"); } } diff --git a/java/src/main/java/org/msgpack/Template.java b/java/src/main/java/org/msgpack/Template.java new file mode 100644 index 00000000..71e64e06 --- /dev/null +++ b/java/src/main/java/org/msgpack/Template.java @@ -0,0 +1,22 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack; + +public interface Template extends MessageUnpacker, MessageConverter { +} + diff --git a/java/src/main/java/org/msgpack/Templates.java b/java/src/main/java/org/msgpack/Templates.java new file mode 100644 index 00000000..4c3fdaee --- /dev/null +++ b/java/src/main/java/org/msgpack/Templates.java @@ -0,0 +1,45 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack; + +import org.msgpack.template.*; + +public class Templates { + public static Template tList(Template elementTemplate) { + return new ListTemplate(elementTemplate); + } + + public static Template tMap(Template keyTemplate, Template valueTemplate) { + return new MapTemplate(keyTemplate, valueTemplate); + } + + + public static final Template TString = StringTemplate.getInstance(); + + public static Template tString() { + return TString; + } + + + public static final Template TByteArray = ByteArrayTemplate.getInstance(); + + public static Template tByteArray() { + return TByteArray; + } +} + diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java index 3cae502e..1b5621f3 100644 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ b/java/src/main/java/org/msgpack/Unpacker.java @@ -561,12 +561,23 @@ public class Unpacker implements Iterable { return impl.unpackObject(); } + final public boolean tryUnpackNull() throws IOException { + return impl.tryUnpackNull(); + } + + final public Object unpack(MessageUnpacker unpacker) throws IOException, MessageTypeException { + return unpacker.unpack(this); + } + final public void unpack(MessageUnpackable obj) throws IOException, MessageTypeException { obj.messageUnpack(this); } - final public boolean tryUnpackNull() throws IOException { - return impl.tryUnpackNull(); + final public Object unpack(Class klass) throws MessageTypeException { + // FIXME check MessageUnpackable + // FIXME check CustomPacker + // FIXME check annotations + throw new MessageTypeException(); } } diff --git a/java/src/main/java/org/msgpack/template/ByteArrayTemplate.java b/java/src/main/java/org/msgpack/template/ByteArrayTemplate.java new file mode 100644 index 00000000..fe263693 --- /dev/null +++ b/java/src/main/java/org/msgpack/template/ByteArrayTemplate.java @@ -0,0 +1,44 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.template; + +import java.io.IOException; +import org.msgpack.*; + +public class ByteArrayTemplate implements Template { + private ByteArrayTemplate() { } + + public Object unpack(Unpacker pac) throws IOException, MessageTypeException { + return pac.unpackByteArray(); + } + + public Object convert(MessagePackObject from) throws MessageTypeException { + return from.asByteArray(); + } + + static public ByteArrayTemplate getInstance() { + return instance; + } + + static final ByteArrayTemplate instance = new ByteArrayTemplate(); + + static { + CustomMessage.registerTemplate(byte[].class, instance); + } +} + diff --git a/java/src/main/java/org/msgpack/template/ClassTemplate.java b/java/src/main/java/org/msgpack/template/ClassTemplate.java new file mode 100644 index 00000000..b5ed854d --- /dev/null +++ b/java/src/main/java/org/msgpack/template/ClassTemplate.java @@ -0,0 +1,45 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.template; + +import java.io.IOException; +import org.msgpack.*; + +public class ClassTemplate implements Template { + private Class klass; + + public ClassTemplate(Class klass) { + this.klass = klass; + } + + public Object unpack(Unpacker pac) throws IOException, MessageTypeException { + return pac.unpack(klass); + } + + public Object convert(MessagePackObject from) throws MessageTypeException { + MessageConverter converter = CustomConverter.get(klass); + if(converter != null) { + return converter.convert(from); + } + + // FIXME check annotations + + throw new MessageTypeException(); + } +} + diff --git a/java/src/main/java/org/msgpack/template/ListTemplate.java b/java/src/main/java/org/msgpack/template/ListTemplate.java new file mode 100644 index 00000000..54975f89 --- /dev/null +++ b/java/src/main/java/org/msgpack/template/ListTemplate.java @@ -0,0 +1,50 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.template; + +import java.util.List; +import java.util.ArrayList; +import java.io.IOException; +import org.msgpack.*; + +public class ListTemplate implements Template { + private Template elementTemplate; + + public ListTemplate(Template elementTemplate) { + this.elementTemplate = elementTemplate; + } + + public Object unpack(Unpacker pac) throws IOException, MessageTypeException { + int length = pac.unpackArray(); + List list = new ArrayList(length); + for(; length > 0; length--) { + list.add( elementTemplate.unpack(pac) ); + } + return list; + } + + public Object convert(MessagePackObject from) throws MessageTypeException { + MessagePackObject[] array = from.asArray(); + List list = new ArrayList(array.length); + for(MessagePackObject element : array) { + list.add( elementTemplate.convert(element) ); + } + return list; + } +} + diff --git a/java/src/main/java/org/msgpack/template/MapTemplate.java b/java/src/main/java/org/msgpack/template/MapTemplate.java new file mode 100644 index 00000000..d2b4effc --- /dev/null +++ b/java/src/main/java/org/msgpack/template/MapTemplate.java @@ -0,0 +1,56 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.template; + +import java.util.Map; +import java.util.HashMap; +import java.io.IOException; +import org.msgpack.*; + +public class MapTemplate implements Template { + private Template keyTemplate; + private Template valueTemplate; + + public MapTemplate(Template keyTemplate, Template valueTemplate) { + this.keyTemplate = keyTemplate; + this.valueTemplate = valueTemplate; + } + + public Object unpack(Unpacker pac) throws IOException, MessageTypeException { + int length = pac.unpackMap(); + Map map = new HashMap(length); + for(; length > 0; length--) { + Object key = keyTemplate.unpack(pac); + Object value = valueTemplate.unpack(pac); + map.put(key, value); + } + return map; + } + + public Object convert(MessagePackObject from) throws MessageTypeException { + Map src = from.asMap(); + Map map = new HashMap(); + for(Map.Entry pair : src.entrySet()) { + Object key = keyTemplate.convert(pair.getKey()); + Object value = valueTemplate.convert(pair.getValue()); + map.put(key, value); + } + return map; + } +} + diff --git a/java/src/main/java/org/msgpack/template/StringTemplate.java b/java/src/main/java/org/msgpack/template/StringTemplate.java new file mode 100644 index 00000000..563112dd --- /dev/null +++ b/java/src/main/java/org/msgpack/template/StringTemplate.java @@ -0,0 +1,44 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.template; + +import java.io.IOException; +import org.msgpack.*; + +public class StringTemplate implements Template { + private StringTemplate() { } + + public Object unpack(Unpacker pac) throws IOException, MessageTypeException { + return pac.unpackString(); + } + + public Object convert(MessagePackObject from) throws MessageTypeException { + return from.asString(); + } + + static public StringTemplate getInstance() { + return instance; + } + + static final StringTemplate instance = new StringTemplate(); + + static { + CustomMessage.registerTemplate(String.class, instance); + } +} +