Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id A6E04200AF7 for ; Tue, 14 Jun 2016 12:38:50 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id A591B160A47; Tue, 14 Jun 2016 10:38:50 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 9D3371602C5 for ; Tue, 14 Jun 2016 12:38:49 +0200 (CEST) Received: (qmail 53737 invoked by uid 500); 14 Jun 2016 10:38:48 -0000 Mailing-List: contact commits-help@felix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@felix.apache.org Delivered-To: mailing list commits@felix.apache.org Received: (qmail 53728 invoked by uid 99); 14 Jun 2016 10:38:48 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 14 Jun 2016 10:38:48 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 5B1721805B4 for ; Tue, 14 Jun 2016 10:38:48 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.374 X-Spam-Level: X-Spam-Status: No, score=0.374 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-1.426] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id jm5IOkGbcB_a for ; Tue, 14 Jun 2016 10:38:45 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTP id F3B725F297 for ; Tue, 14 Jun 2016 10:38:44 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 26B45E0456 for ; Tue, 14 Jun 2016 10:38:44 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 2725D3A00EC for ; Tue, 14 Jun 2016 10:38:44 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1748381 - in /felix/trunk/converter/src: main/java/org/apache/felix/converter/impl/json/ main/java/org/apache/felix/converter/impl/yaml/ main/java/org/osgi/service/converter/ test/java/org/apache/felix/converter/impl/yaml/ Date: Tue, 14 Jun 2016 10:38:44 -0000 To: commits@felix.apache.org From: davidb@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160614103844.2725D3A00EC@svn01-us-west.apache.org> archived-at: Tue, 14 Jun 2016 10:38:50 -0000 Author: davidb Date: Tue Jun 14 10:38:43 2016 New Revision: 1748381 URL: http://svn.apache.org/viewvc?rev=1748381&view=rev Log: Felix Converter Service - Initial YAML codec Added: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java felix/trunk/converter/src/main/java/org/osgi/service/converter/Encoding.java Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java?rev=1748381&r1=1748380&r2=1748381&view=diff ============================================================================== --- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java (original) +++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java Tue Jun 14 10:38:43 2016 @@ -34,7 +34,7 @@ import org.osgi.service.converter.TypeRe public class JsonCodecImpl implements Codec { private Map configuration = new ConcurrentHashMap<>(); private ThreadLocal threadLocal = new ThreadLocal<>(); - private Converter converter = new ConverterImpl(); // TODO inject? + private Converter converter = new ConverterImpl(); @Override public Codec with(Converter c) { Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java?rev=1748381&r1=1748380&r2=1748381&view=diff ============================================================================== --- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java (original) +++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java Tue Jun 14 10:38:43 2016 @@ -20,7 +20,6 @@ import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Array; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -47,9 +46,9 @@ public class JsonEncodingImpl implements } @Override - public void to(OutputStream os) { + public void to(OutputStream os, Charset charset) { try { - os.write(encode(object).getBytes(StandardCharsets.UTF_8)); + os.write(encode(object).getBytes(charset)); } catch (IOException e) { throw new RuntimeException(e); } @@ -61,7 +60,7 @@ public class JsonEncodingImpl implements } @SuppressWarnings("rawtypes") - public String encode(Object obj) { + private String encode(Object obj) { if (obj == null) { return ignoreNull() ? "" : "null"; } @@ -111,7 +110,7 @@ public class JsonEncodingImpl implements @SuppressWarnings({ "rawtypes", "unchecked" }) private String encodeMap(Map m) { StringBuilder sb = new StringBuilder("{"); - for (Entry entry : (Set) m.entrySet()) { + for (Entry entry : (Set) m.entrySet()) { if (entry.getKey() == null || entry.getValue() == null) if (ignoreNull()) continue; @@ -135,12 +134,6 @@ public class JsonEncodingImpl implements } @Override - public void to(OutputStream out, Charset charset) { - // TODO Auto-generated method stub - - } - - @Override public Appendable to(Appendable out) { // TODO Auto-generated method stub return null; Added: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java?rev=1748381&view=auto ============================================================================== --- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java (added) +++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java Tue Jun 14 10:38:43 2016 @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.felix.converter.impl.yaml; + +import java.lang.reflect.Type; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.felix.converter.impl.ConverterImpl; +import org.osgi.service.converter.Codec; +import org.osgi.service.converter.Converter; +import org.osgi.service.converter.Decoding; +import org.osgi.service.converter.Encoding; +import org.osgi.service.converter.TypeReference; + +public class YamlCodecImpl implements Codec { + private Map configuration = new ConcurrentHashMap<>(); + private Converter converter = new ConverterImpl(); + + @Override + public Codec with(Converter c) { + converter = c; + return this; + } + + @Override + public Decoding decode(Class cls) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Decoding decode(TypeReference ref) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Decoding decode(Type type) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Encoding encode(Object obj) { + return new YamlEncodingImpl(converter, configuration, obj); + } +} Added: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java?rev=1748381&view=auto ============================================================================== --- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java (added) +++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java Tue Jun 14 10:38:43 2016 @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.felix.converter.impl.yaml; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.osgi.service.converter.Converter; +import org.osgi.service.converter.Encoding; + +public class YamlEncodingImpl implements Encoding { + private final Converter converter; + private final Map configuration; + private final Object object; + private final int indentation = 2; + + public YamlEncodingImpl(Converter c, Map cfg, Object obj) { + converter = c; + configuration = cfg; + object = obj; + } + + @Override + public Encoding pretty() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void to(OutputStream os, Charset charset) { + try { + os.write(encode(object).getBytes(charset)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public Appendable to(Appendable out) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String toString() { + return encode(object); + } + + private String encode(Object obj) { + return encode(obj, 0); + } + + @SuppressWarnings("rawtypes") + private String encode(Object obj, int level) { + if (obj == null) + return ""; + + if (obj instanceof Map) { + return encodeMap((Map) obj, level); + } else if (obj instanceof Number) { + return obj.toString(); + } else if (obj instanceof Boolean) { + return obj.toString(); + } + + return "'" + converter.convert(obj).to(String.class) + "'"; + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private String encodeMap(Map m, int level) { + StringBuilder sb = new StringBuilder(); + for (Entry entry : (Set) m.entrySet()) { + sb.append(getIdentPrefix(level)); + sb.append(entry.getKey().toString()); + sb.append(": "); + sb.append(needsNewlineBefore(entry.getValue())); + sb.append(encode(entry.getValue(), level + 1)); + sb.append("\n"); + } + + return sb.toString(); + } + + private Object needsNewlineBefore(Object value) { + if (value instanceof Map) + return "\n"; + + return ""; + } + + private String getIdentPrefix(int level) { + int numSpaces = indentation * level; + StringBuilder sb = new StringBuilder(numSpaces); + for (int i=0; i < numSpaces; i++) + sb.append(' '); + return sb.toString(); + } +} Modified: felix/trunk/converter/src/main/java/org/osgi/service/converter/Encoding.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/osgi/service/converter/Encoding.java?rev=1748381&r1=1748380&r2=1748381&view=diff ============================================================================== --- felix/trunk/converter/src/main/java/org/osgi/service/converter/Encoding.java (original) +++ felix/trunk/converter/src/main/java/org/osgi/service/converter/Encoding.java Tue Jun 14 10:38:43 2016 @@ -17,6 +17,7 @@ package org.osgi.service.converter; import java.io.OutputStream; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; /** * Interface to specify the target of the encoding operation. @@ -39,7 +40,9 @@ public interface Encoding { * * @param out The output stream to use. */ - void to(OutputStream out); + default void to(OutputStream os) { + to(os, StandardCharsets.UTF_8); + } /** * Use an output stream as the target of the encoding operation. Added: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java?rev=1748381&view=auto ============================================================================== --- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java (added) +++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java Tue Jun 14 10:38:43 2016 @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.felix.converter.impl.yaml; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class YamlSerializationTest { + @Test + public void testComplexMapSerialization() { + Map m = new LinkedHashMap<>(); + m.put("sKey", "a string"); + m.put("iKey", 42); + m.put("bKey", true); + m.put("noKey", null); + m.put("simpleArray", new int[] {1,2,3}); + + Map m1 = new HashMap<>(); + m1.put("a", 1L); + m1.put("b", "hello"); + m.put("simpleObject", m1); + + String expected = ""; + assertEquals(expected, new YamlCodecImpl().encode(m).toString()); + } +}