http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core-test/src/test/java/org/apache/juneau/XmlValidatorParser.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/XmlValidatorParser.java b/juneau-core-test/src/test/java/org/apache/juneau/XmlValidatorParser.java index 9bd8270..1ff1add 100755 --- a/juneau-core-test/src/test/java/org/apache/juneau/XmlValidatorParser.java +++ b/juneau-core-test/src/test/java/org/apache/juneau/XmlValidatorParser.java @@ -34,8 +34,24 @@ public class XmlValidatorParser extends XmlParser { } @Override /* Parser */ - protected <T> T doParse(ParserSession session, ClassMeta<T> type) throws Exception { - return (T)validate(session.getReader()); + public ReaderParserSession createSession(ParserSessionArgs args) { + return new ReaderParserSession(null, args) { + + @Override + protected <T> T doParse(ParserPipe pipe, ClassMeta<T> type) throws Exception { + return (T)validate(pipe.getReader()); + } + + @Override /* ReaderParser */ + protected <K,V> Map<K,V> doParseIntoMap(ParserPipe pipe, Map<K,V> m, Type keyType, Type valueType) throws Exception { + return (Map<K,V>)validate(pipe.getReader()); + } + + @Override /* ReaderParser */ + protected <E> Collection<E> doParseIntoCollection(ParserPipe pipe, Collection<E> c, Type elementType) throws Exception { + return (Collection<E>)validate(pipe.getReader()); + } + }; } public <T> T validate(Reader r) throws Exception { @@ -44,16 +60,6 @@ public class XmlValidatorParser extends XmlParser { return null; } - @Override /* ReaderParser */ - protected <K,V> Map<K,V> doParseIntoMap(ParserSession session, Map<K,V> m, Type keyType, Type valueType) throws Exception { - return (Map<K,V>)validate(session.getReader()); - } - - @Override /* ReaderParser */ - protected <E> Collection<E> doParseIntoCollection(ParserSession session, Collection<E> c, Type elementType) throws Exception { - return (Collection<E>)validate(session.getReader()); - } - protected XMLStreamReader getStaxReader(Reader in) throws Exception { XMLInputFactory factory = XMLInputFactory.newInstance(); factory.setProperty("javax.xml.stream.isNamespaceAware", false);
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core-test/src/test/java/org/apache/juneau/html/CommonParserTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/html/CommonParserTest.java b/juneau-core-test/src/test/java/org/apache/juneau/html/CommonParserTest.java index 865ab54..ac87876 100755 --- a/juneau-core-test/src/test/java/org/apache/juneau/html/CommonParserTest.java +++ b/juneau-core-test/src/test/java/org/apache/juneau/html/CommonParserTest.java @@ -154,7 +154,7 @@ public class CommonParserTest { final static List<String> events = new LinkedList<String>(); @Override /* ParserListener */ - public <T> void onUnknownBeanProperty(ParserSession session, String propertyName, Class<T> beanClass, T bean, int line, int col) { + public <T> void onUnknownBeanProperty(ParserSession session, ParserPipe pipe, String propertyName, Class<T> beanClass, T bean, int line, int col) { events.add(propertyName + "," + line + "," + col); } } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonParserTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonParserTest.java b/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonParserTest.java index cac45d5..f424533 100755 --- a/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonParserTest.java +++ b/juneau-core-test/src/test/java/org/apache/juneau/jena/CommonParserTest.java @@ -198,7 +198,7 @@ public class CommonParserTest { final static List<String> events = new LinkedList<String>(); @Override /* ParserListener */ - public <T> void onUnknownBeanProperty(ParserSession session, String propertyName, Class<T> beanClass, T bean, int line, int col) { + public <T> void onUnknownBeanProperty(ParserSession session, ParserPipe pipe, String propertyName, Class<T> beanClass, T bean, int line, int col) { events.add(propertyName + "," + line + "," + col); } } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core-test/src/test/java/org/apache/juneau/json/CommonParserTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/json/CommonParserTest.java b/juneau-core-test/src/test/java/org/apache/juneau/json/CommonParserTest.java index d16329d..24c013b 100755 --- a/juneau-core-test/src/test/java/org/apache/juneau/json/CommonParserTest.java +++ b/juneau-core-test/src/test/java/org/apache/juneau/json/CommonParserTest.java @@ -173,7 +173,7 @@ public class CommonParserTest { final static List<String> events = new LinkedList<String>(); @Override /* ParserListener */ - public <T> void onUnknownBeanProperty(ParserSession session, String propertyName, Class<T> beanClass, T bean, int line, int col) { + public <T> void onUnknownBeanProperty(ParserSession session, ParserPipe pipe, String propertyName, Class<T> beanClass, T bean, int line, int col) { events.add(propertyName + "," + line + "," + col); } } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core-test/src/test/java/org/apache/juneau/transforms/LocalizedDatesTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/transforms/LocalizedDatesTest.java b/juneau-core-test/src/test/java/org/apache/juneau/transforms/LocalizedDatesTest.java index 9aaca66..4dc9195 100644 --- a/juneau-core-test/src/test/java/org/apache/juneau/transforms/LocalizedDatesTest.java +++ b/juneau-core-test/src/test/java/org/apache/juneau/transforms/LocalizedDatesTest.java @@ -625,7 +625,7 @@ public class LocalizedDatesTest { @Test public void test() { - BeanSession session = ps.getBeanContext().createSession(null, sessionLocale, sessionTimeZone, null); + BeanSession session = ps.getBeanContext().createSession(new BeanSessionArgs(null, sessionLocale, sessionTimeZone, null)); String actual = session.convertToType(calendar, String.class); assertEquals(expected, actual); Object c2 = session.convertToType(actual, calendar.getClass()); http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/CommonParser_UonTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/CommonParser_UonTest.java b/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/CommonParser_UonTest.java index 088f91b..b3aa2f2 100755 --- a/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/CommonParser_UonTest.java +++ b/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/CommonParser_UonTest.java @@ -162,7 +162,7 @@ public class CommonParser_UonTest { final static List<String> events = new LinkedList<String>(); @Override /* ParserListener */ - public <T> void onUnknownBeanProperty(ParserSession session, String propertyName, Class<T> beanClass, T bean, int line, int col) { + public <T> void onUnknownBeanProperty(ParserSession session, ParserPipe pipe, String propertyName, Class<T> beanClass, T bean, int line, int col) { events.add(propertyName + "," + line + "," + col); } } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/CommonParser_UrlEncodingTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/CommonParser_UrlEncodingTest.java b/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/CommonParser_UrlEncodingTest.java index a8f4e1a..3b5fadd 100755 --- a/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/CommonParser_UrlEncodingTest.java +++ b/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/CommonParser_UrlEncodingTest.java @@ -164,7 +164,7 @@ public class CommonParser_UrlEncodingTest { final static List<String> events = new LinkedList<String>(); @Override /* ParserListener */ - public <T> void onUnknownBeanProperty(ParserSession session, String propertyName, Class<T> beanClass, T bean, int line, int col) { + public <T> void onUnknownBeanProperty(ParserSession session, ParserPipe pipe, String propertyName, Class<T> beanClass, T bean, int line, int col) { events.add(propertyName + "," + line + "," + col); } } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/UonParserReaderTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/UonParserReaderTest.java b/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/UonParserReaderTest.java index 32833b9..5e05128 100755 --- a/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/UonParserReaderTest.java +++ b/juneau-core-test/src/test/java/org/apache/juneau/urlencoding/UonParserReaderTest.java @@ -17,6 +17,7 @@ import static org.junit.Assert.*; import java.io.*; import org.apache.juneau.*; +import org.apache.juneau.parser.*; import org.apache.juneau.uon.*; import org.junit.*; @@ -170,7 +171,7 @@ public class UonParserReaderTest { // Reader that only returns 1 character at a time; s = "x¢â¬ð¤¢x¢â¬ð¤¢"; in = "x" + escape("¢â¬ð¤¢") + "x" + escape("¢â¬ð¤¢"); - r = new UonReader(new SlowStringReader(in), true); + r = new UonReader(new ParserPipe(new SlowStringReader(in)), true); assertEquals(s.codePointAt(0), r.readCodePoint()); assertEquals(s.codePointAt(1), r.readCodePoint()); assertEquals(s.codePointAt(2), r.readCodePoint()); @@ -190,8 +191,8 @@ public class UonParserReaderTest { return sb.toString(); } - private UonReader r(String in, boolean decodeChars) { - return new UonReader(in, decodeChars); + private UonReader r(String in, boolean decodeChars) throws Exception { + return new UonReader(new ParserPipe(in), decodeChars); } private static class SlowStringReader extends Reader { http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core-test/src/test/java/org/apache/juneau/utils/ParserReaderTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/utils/ParserReaderTest.java b/juneau-core-test/src/test/java/org/apache/juneau/utils/ParserReaderTest.java index 3796e87..5af56af 100755 --- a/juneau-core-test/src/test/java/org/apache/juneau/utils/ParserReaderTest.java +++ b/juneau-core-test/src/test/java/org/apache/juneau/utils/ParserReaderTest.java @@ -25,7 +25,7 @@ public class ParserReaderTest { //==================================================================================================== @Test public void test() throws Exception { - ParserReader r = new ParserReader("abc123"); + ParserReader r = new ParserReader(new ParserPipe("abc123")); try { assertEquals('a', r.read()); r.unread(); http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core-test/src/test/java/org/apache/juneau/utils/StringUtilsTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/utils/StringUtilsTest.java b/juneau-core-test/src/test/java/org/apache/juneau/utils/StringUtilsTest.java index 2f0aec3..7143b97 100755 --- a/juneau-core-test/src/test/java/org/apache/juneau/utils/StringUtilsTest.java +++ b/juneau-core-test/src/test/java/org/apache/juneau/utils/StringUtilsTest.java @@ -175,7 +175,7 @@ public class StringUtilsTest { Number n; for (Class c : new Class[]{ Integer.class, Double.class, Float.class, Long.class, Short.class, Byte.class, BigInteger.class, BigDecimal.class, Number.class, AtomicInteger.class, AtomicLong.class}) { - in = new ParserReader("123'"); + in = new ParserReader(new ParserPipe("123'")); n = parseNumber(in, c); assertTrue(c.isInstance(n)); assertEquals(123, n.intValue()); http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core-test/src/test/java/org/apache/juneau/xml/CommonParserTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/xml/CommonParserTest.java b/juneau-core-test/src/test/java/org/apache/juneau/xml/CommonParserTest.java index becaff9..5fba9a1 100755 --- a/juneau-core-test/src/test/java/org/apache/juneau/xml/CommonParserTest.java +++ b/juneau-core-test/src/test/java/org/apache/juneau/xml/CommonParserTest.java @@ -171,7 +171,7 @@ public class CommonParserTest { final static List<String> events = new LinkedList<String>(); @Override /* ParserListener */ - public <T> void onUnknownBeanProperty(ParserSession session, String propertyName, Class<T> beanClass, T bean, int line, int col) { + public <T> void onUnknownBeanProperty(ParserSession session, ParserPipe pipe, String propertyName, Class<T> beanClass, T bean, int line, int col) { events.add(propertyName + "," + line + "," + col); } } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlContentTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlContentTest.java b/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlContentTest.java index 18f0ed6..21f778a 100755 --- a/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlContentTest.java +++ b/juneau-core-test/src/test/java/org/apache/juneau/xml/XmlContentTest.java @@ -37,7 +37,7 @@ public class XmlContentTest { XmlSerializer s1 = XmlSerializer.DEFAULT_SQ, s2 = new XmlSerializerBuilder().sq().ws().enableNamespaces(false).build(); XmlParser p = XmlParser.DEFAULT; - XmlSerializerSession session; + WriterSerializerSession session; String r; StringWriter sw; @@ -47,18 +47,16 @@ public class XmlContentTest { t.f2 = null; sw = new StringWriter(); - SerializerOutput out = new SerializerOutput(sw); - session = s1.createSession(new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null, null, null, null, null); - s1.serialize(session, out, t); + session = s1.createSession(new SerializerSessionArgs(new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null, null, null, null, null)); + session.serialize(sw, t); r = sw.toString(); assertEquals("<A f1='f1'>_x0000_</A>", r); t2 = p.parse(r, A.class); assertEqualObjects(t, t2); sw = new StringWriter(); - out = new SerializerOutput(sw); - session = s2.createSession(new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null, null, null, null, null); - s2.serialize(session, out, t); + session = s2.createSession(new SerializerSessionArgs(new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null, null, null, null, null)); + session.serialize(sw, t); r = sw.toString(); assertEquals("<A f1='f1'>_x0000_</A>\n", r); t2 = p.parse(r, A.class); @@ -147,7 +145,7 @@ public class XmlContentTest { XmlSerializer s1 = XmlSerializer.DEFAULT_SQ, s2 = new XmlSerializerBuilder().sq().ws().enableNamespaces(false).build(); XmlParser p = XmlParser.DEFAULT; - XmlSerializerSession session; + WriterSerializerSession session; String r; StringWriter sw; @@ -157,18 +155,16 @@ public class XmlContentTest { t.f2 = null; sw = new StringWriter(); - SerializerOutput out = new SerializerOutput(sw); - session = s1.createSession(new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null, null, null, null, null); - s1.serialize(session, out, t); + session = s1.createSession(new SerializerSessionArgs(new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null, null, null, null, null)); + session.serialize(sw, t); r = sw.toString(); assertEquals("<A f1='f1'>_x0000_</A>", r); t2 = p.parse(r, B.class); assertEqualObjects(t, t2); sw = new StringWriter(); - out = new SerializerOutput(sw); - session = s2.createSession(new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null, null, null, null, null); - s2.serialize(session, out, t); + session = s2.createSession(new SerializerSessionArgs(new ObjectMap("{"+SERIALIZER_trimNullProperties+":false}"), null, null, null, null, null)); + session.serialize(sw, t); r = sw.toString(); assertEquals("<A f1='f1'>_x0000_</A>\n", r); t2 = p.parse(r, B.class); http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/BeanContext.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/BeanContext.java b/juneau-core/src/main/java/org/apache/juneau/BeanContext.java index c96ac5e..52ec112 100644 --- a/juneau-core/src/main/java/org/apache/juneau/BeanContext.java +++ b/juneau-core/src/main/java/org/apache/juneau/BeanContext.java @@ -227,7 +227,7 @@ import org.apache.juneau.transform.*; * <p> * See {@link ClassMeta} for more information. */ -@SuppressWarnings({"unchecked","rawtypes","hiding"}) +@SuppressWarnings({"unchecked","rawtypes"}) public class BeanContext extends Context { /** @@ -1079,24 +1079,12 @@ public class BeanContext extends Context { /** * Create a new bean session based on the properties defined on this context. * - * @param op - * The override properties. - * This map can contain values to override properties defined on this context. - * Note that only session-overridable settings can be overridden. - * @param locale - * The bean session locale. - * Typically used by {@link PojoSwap PojoSwaps} to provide locale-specific output. - * If <jk>null</jk>, the system default locale is assumed. - * @param timeZone - * The bean session timezone. - * Typically used by time-sensitive {@link PojoSwap PojoSwaps} to provide timezone-specific output. - * If <jk>null</jk> the system default timezone is assumed on {@link java.util.Date} objects, or the - * locale specified on {@link Calendar} objects are used. - * @param mediaType The session media type (e.g. <js>"application/json"</js>). + * @param args + * The session arguments. * @return A new session object. */ - public BeanSession createSession(ObjectMap op, Locale locale, TimeZone timeZone, MediaType mediaType) { - return new BeanSession(this, op, locale, timeZone, mediaType); + public BeanSession createSession(BeanSessionArgs args) { + return new BeanSession(this, args); } /** @@ -1109,7 +1097,7 @@ public class BeanContext extends Context { * @return A new session object. */ public BeanSession createSession() { - return new BeanSession(this, null, this.locale, this.timeZone, this.mediaType); + return new BeanSession(this, new BeanSessionArgs(null, this.locale, this.timeZone, this.mediaType)); } /** http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/BeanSession.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/BeanSession.java b/juneau-core/src/main/java/org/apache/juneau/BeanSession.java index 353c65b..e7c059d 100644 --- a/juneau-core/src/main/java/org/apache/juneau/BeanSession.java +++ b/juneau-core/src/main/java/org/apache/juneau/BeanSession.java @@ -47,34 +47,27 @@ public class BeanSession extends Session { /** * Create a new session using properties specified in the context. * - * @param op - * The override properties. - * These override any context properties defined in the context. * @param ctx * The context creating this session object. * The context contains all the configuration settings for this object. - * @param locale - * The session locale. - * If <jk>null</jk>, then the locale defined on the context is used. - * @param timeZone - * The session timezone. - * If <jk>null</jk>, then the timezone defined on the context is used. - * @param mediaType The session media type (e.g. <js>"application/json"</js>). + * @param args + * Runtime session arguments. */ - protected BeanSession(BeanContext ctx, ObjectMap op, Locale locale, TimeZone timeZone, MediaType mediaType) { - super(ctx, op); + protected BeanSession(BeanContext ctx, BeanSessionArgs args) { + super(ctx, args); this.ctx = ctx; Locale _locale = null; - if (op == null || op.isEmpty()) { - _locale = (locale != null ? locale : ctx.locale); - this.timeZone = (timeZone != null ? timeZone : ctx.timeZone); + ObjectMap p = getProperties(); + if (p == null || p.isEmpty()) { + _locale = (args.locale != null ? args.locale : ctx.locale); + this.timeZone = (args.timeZone != null ? args.timeZone : ctx.timeZone); this.debug = ctx.debug; - this.mediaType = mediaType != null ? mediaType : ctx.mediaType; + this.mediaType = args.mediaType != null ? args.mediaType : ctx.mediaType; } else { - _locale = (locale == null ? op.get(Locale.class, BEAN_locale, ctx.locale) : locale); - this.timeZone = (timeZone == null ? op.get(TimeZone.class, BEAN_timeZone, ctx.timeZone) : timeZone); - this.debug = op.getBoolean(BEAN_debug, false); - this.mediaType = (mediaType == null ? op.get(MediaType.class, BEAN_mediaType, ctx.mediaType) : mediaType); + _locale = (args.locale != null ? args.locale : getProperty(Locale.class, BEAN_locale, ctx.locale)); + this.timeZone = (args.timeZone != null ? args.timeZone : getProperty(TimeZone.class, BEAN_timeZone, ctx.timeZone)); + this.debug = getProperty(boolean.class, BEAN_debug, false); + this.mediaType = (args.mediaType != null ? args.mediaType : getProperty(MediaType.class, BEAN_mediaType, ctx.mediaType)); } this.locale = _locale == null ? Locale.getDefault() : _locale; } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/BeanSessionArgs.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/BeanSessionArgs.java b/juneau-core/src/main/java/org/apache/juneau/BeanSessionArgs.java new file mode 100644 index 0000000..828846a --- /dev/null +++ b/juneau-core/src/main/java/org/apache/juneau/BeanSessionArgs.java @@ -0,0 +1,51 @@ +// *************************************************************************************************************************** +// * 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.juneau; + +import java.util.*; + +import org.apache.juneau.http.*; + +/** + * Runtime arguments common to all bean, serializer, and parser sessions. + */ +public class BeanSessionArgs extends SessionArgs { + + final Locale locale; + final TimeZone timeZone; + final MediaType mediaType; + + /** + * Constructor. + * + * @param properties + * Session-level properties. + * These override context-level properties. + * Can be <jk>null</jk>. + * @param locale + * The session locale. + * If <jk>null</jk>, then the locale defined on the context is used. + * @param timeZone + * The session timezone. + * If <jk>null</jk>, then the timezone defined on the context is used. + * @param mediaType + * The session media type (e.g. <js>"application/json"</js>). + * Can be <jk>null</jk>. + */ + public BeanSessionArgs(ObjectMap properties, Locale locale, TimeZone timeZone, MediaType mediaType) { + super(properties); + this.locale = locale; + this.timeZone = timeZone; + this.mediaType = mediaType; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/Context.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/Context.java b/juneau-core/src/main/java/org/apache/juneau/Context.java index 820dfc2..280b65a 100644 --- a/juneau-core/src/main/java/org/apache/juneau/Context.java +++ b/juneau-core/src/main/java/org/apache/juneau/Context.java @@ -44,7 +44,7 @@ public abstract class Context { * @param propertyStore The factory that created this config. */ public Context(PropertyStore propertyStore) { - this.propertyStore = PropertyStore.create(propertyStore); + this.propertyStore = propertyStore == null ? PropertyStore.create() : propertyStore.copy(); } /** http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/CoreObject.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/CoreObject.java b/juneau-core/src/main/java/org/apache/juneau/CoreObject.java index de19fde..9e92d8d 100644 --- a/juneau-core/src/main/java/org/apache/juneau/CoreObject.java +++ b/juneau-core/src/main/java/org/apache/juneau/CoreObject.java @@ -28,7 +28,7 @@ public abstract class CoreObject { * @param propertyStore The property store containing all the settings for this object. */ protected CoreObject(PropertyStore propertyStore) { - this.propertyStore = propertyStore.create(getOverrideProperties()); + this.propertyStore = propertyStore.copy(); this.beanContext = createContext(BeanContext.class); } @@ -43,28 +43,14 @@ public abstract class CoreObject { } /** - * Method used by subclasses to override context factory properties for just this instance without modifying the - * context factory itself. - * - * <p> - * Subclasses can call this parent method to append to override properties defined higher in the parent chain. - * - * @return The override properties. Never <jk>null</jk>. - */ - protected ObjectMap getOverrideProperties() { - return new ObjectMap(); - } - - /** - * Returns a copy of the context factory passed in to the constructor with any override properties applied from the - * {@link #getOverrideProperties()}. + * Returns a copy of the context factory passed in to the constructor. * * @return - * The context factory on this class. - * Multiple calls to this method returns the same factory. + * A copy of the property store on this class. + * Multiple calls to this method returns new modifiable copies. */ public PropertyStore createPropertyStore() { - return PropertyStore.create(propertyStore); + return propertyStore.copy(); } /** http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/CoreObjectBuilder.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/CoreObjectBuilder.java b/juneau-core/src/main/java/org/apache/juneau/CoreObjectBuilder.java index 0b59ca4..296aca1 100644 --- a/juneau-core/src/main/java/org/apache/juneau/CoreObjectBuilder.java +++ b/juneau-core/src/main/java/org/apache/juneau/CoreObjectBuilder.java @@ -48,7 +48,7 @@ public abstract class CoreObjectBuilder { * @param propertyStore The initial configuration settings for this builder. */ public CoreObjectBuilder(PropertyStore propertyStore) { - this.propertyStore = PropertyStore.create(propertyStore); + this.propertyStore = propertyStore.copy(); } /** http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/PropertyStore.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/PropertyStore.java b/juneau-core/src/main/java/org/apache/juneau/PropertyStore.java index b03dfb4..5388c81 100644 --- a/juneau-core/src/main/java/org/apache/juneau/PropertyStore.java +++ b/juneau-core/src/main/java/org/apache/juneau/PropertyStore.java @@ -298,17 +298,6 @@ public final class PropertyStore { return f; } - /** - * Create a new property store with settings copied from the specified store. - * - * @param copyFrom The existing store to copy properties from. - * @return A new property store with default settings. - */ - public static PropertyStore create(PropertyStore copyFrom) { - return new PropertyStore().copyFrom(copyFrom); - } - - PropertyStore() {} /** @@ -341,13 +330,12 @@ public final class PropertyStore { } /** - * Creates a new store with the specified override properties applied to this store. + * Creates a new modifiable copy of this property store. * - * @param overrideProperties The properties to apply to the copy of this store. - * @return Either this unmodified store, or a new store with override properties applied. + * @return A new modifiable copy of this property store. */ - public PropertyStore create(Map<String,Object> overrideProperties) { - return new PropertyStore(this).setProperties(overrideProperties); + public PropertyStore copy() { + return new PropertyStore(this); } /** @@ -435,6 +423,27 @@ public final class PropertyStore { } /** + * Synonym for {@link #setProperty(String, Object)}. + * + * @param name + * The configuration property name. + * <br>If name ends with <l>.add</l>, then the specified value is added to the existing property value as an entry + * in a SET or LIST property. + * <br>If name ends with <l>.put</l>, then the specified value is added to the existing property value as a + * key/value pair in a MAP property. + * <br>If name ends with <l>.remove</l>, then the specified value is removed from the existing property property + * value in a SET or LIST property. + * @param value + * The new value. + * If <jk>null</jk>, the property value is deleted. + * In general, the value type can be anything. + * @return This object (for method chaining). + */ + public PropertyStore append(String name, Object value) { + return setProperty(name, value); + } + + /** * Convenience method for setting multiple properties in one call. * * <p> http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/Session.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/Session.java b/juneau-core/src/main/java/org/apache/juneau/Session.java index 1bf8292..256ce16 100644 --- a/juneau-core/src/main/java/org/apache/juneau/Session.java +++ b/juneau-core/src/main/java/org/apache/juneau/Session.java @@ -41,17 +41,28 @@ public abstract class Session { private boolean closed; private List<String> warnings; // Any warnings encountered. + /** * Default constructor. * * @param ctx * The context creating this session object. * The context contains all the configuration settings for the session. - * @param op Properties associated with this session. + * @param args + * Runtime arguments. */ - protected Session(final Context ctx, ObjectMap op) { + protected Session(final Context ctx, SessionArgs args) { this.ctx = ctx; - this.properties = op != null ? op : ObjectMap.EMPTY_MAP; + this.properties = args.properties != null ? args.properties : ObjectMap.EMPTY_MAP; + } + + /** + * Returns the session-level properties. + * + * @return The session-level properties. + */ + protected final ObjectMap getProperties() { + return properties; } /** @@ -68,7 +79,7 @@ public abstract class Session { * @param key The property key. * @return The property value, or <jk>null</jk> if it doesn't exist. */ - public String getProperty(String key) { + public final String getProperty(String key) { return getProperty(key, null); } @@ -79,7 +90,7 @@ public abstract class Session { * @param def The default value if the property doesn't exist or is <jk>null</jk>. * @return The property value. */ - public String getProperty(String key, String def) { + public final String getProperty(String key, String def) { Object v = properties.get(key); if (v == null) v = ctx.getPropertyStore().getProperty(key, String.class, null); @@ -95,7 +106,7 @@ public abstract class Session { * @param key The property key. * @return The property value. */ - public <T> T getProperty(Class<T> type, String key) { + public final <T> T getProperty(Class<T> type, String key) { return getProperty(type, key, null); } @@ -107,7 +118,7 @@ public abstract class Session { * @param def The default value if the property doesn't exist or is <jk>null</jk>. * @return The property value. */ - public <T> T getProperty(Class<T> type, String key, T def) { + public final <T> T getProperty(Class<T> type, String key, T def) { T t = properties.get(type, key); if (t == null) t = ctx.getPropertyStore().getProperty(key, type, def); @@ -123,7 +134,7 @@ public abstract class Session { * @param key The key. Can be any string. * @param val The cached object. */ - public void addToCache(String key, Object val) { + public final void addToCache(String key, Object val) { if (cache == null) cache = new TreeMap<String,Object>(); cache.put(key, val); @@ -139,7 +150,7 @@ public abstract class Session { * The objects to add to this session's cache. * No-op if <jk>null</jk>. */ - public void addToCache(Map<String,Object> cacheObjects) { + public final void addToCache(Map<String,Object> cacheObjects) { if (cacheObjects != null) { if (cache == null) cache = new TreeMap<String,Object>(); @@ -155,7 +166,7 @@ public abstract class Session { * @return The cached object, or <jk>null</jk> if it doesn't exist. */ @SuppressWarnings("unchecked") - public <T> T getFromCache(Class<T> c, String key) { + public final <T> T getFromCache(Class<T> c, String key) { return cache == null ? null : (T)cache.get(key); } @@ -177,7 +188,7 @@ public abstract class Session { * * @return <jk>true</jk> if warnings occurred in this session. */ - public boolean hasWarnings() { + public final boolean hasWarnings() { return warnings != null && warnings.size() > 0; } @@ -186,7 +197,7 @@ public abstract class Session { * * @return The warnings that occurred in this session, or <jk>null</jk> if no warnings occurred. */ - public List<String> getWarnings() { + public final List<String> getWarnings() { return warnings; } @@ -198,7 +209,7 @@ public abstract class Session { * * @return The logger associated with this session. */ - protected JuneauLogger getLogger() { + protected final JuneauLogger getLogger() { if (logger == null) logger = JuneauLogger.getLogger(getClass()); return logger; http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/SessionArgs.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/SessionArgs.java b/juneau-core/src/main/java/org/apache/juneau/SessionArgs.java new file mode 100644 index 0000000..b59d2de --- /dev/null +++ b/juneau-core/src/main/java/org/apache/juneau/SessionArgs.java @@ -0,0 +1,33 @@ +// *************************************************************************************************************************** +// * 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.juneau; + +/** + * Runtime arguments common to all bean, serializer, and parser sessions. + */ +public class SessionArgs { + + final ObjectMap properties; + + /** + * Constructor. + * + * @param properties + * Session-level properties. + * These override context-level properties. + * Can be <jk>null</jk>. + */ + public SessionArgs(ObjectMap properties) { + this.properties = properties != null ? properties : ObjectMap.EMPTY_MAP; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/csv/CsvParser.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/csv/CsvParser.java b/juneau-core/src/main/java/org/apache/juneau/csv/CsvParser.java index c42c84c..6a63d0e 100644 --- a/juneau-core/src/main/java/org/apache/juneau/csv/CsvParser.java +++ b/juneau-core/src/main/java/org/apache/juneau/csv/CsvParser.java @@ -25,6 +25,7 @@ public class CsvParser extends ReaderParser { /** Default parser, all default settings.*/ public static final CsvParser DEFAULT = new CsvParser(PropertyStore.create()); + private final CsvParserContext ctx; /** * Constructor. @@ -33,6 +34,7 @@ public class CsvParser extends ReaderParser { */ public CsvParser(PropertyStore propertyStore) { super(propertyStore); + this.ctx = createContext(CsvParserContext.class); } @Override /* CoreObject */ @@ -40,22 +42,8 @@ public class CsvParser extends ReaderParser { return new CsvParserBuilder(propertyStore); } - @SuppressWarnings("unused") - private static <T> T parseAnything(ParserSession session, ClassMeta<T> eType, ParserReader r, Object outer, BeanPropertyMeta pMeta) throws Exception { - throw new NoSuchMethodException("Not implemented."); - } - - //-------------------------------------------------------------------------------- - // Entry point methods - //-------------------------------------------------------------------------------- - @Override /* Parser */ - protected <T> T doParse(ParserSession session, ClassMeta<T> type) throws Exception { - CsvParserSession s = (CsvParserSession)session; - ParserReader r = s.getReader(); - if (r == null) - return null; - T o = parseAnything(s, type, r, s.getOuter(), null); - return o; + public ReaderParserSession createSession(ParserSessionArgs args) { + return new CsvParserSession(ctx, args); } } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/csv/CsvParserSession.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/csv/CsvParserSession.java b/juneau-core/src/main/java/org/apache/juneau/csv/CsvParserSession.java index 97a24aa..48b42d7 100644 --- a/juneau-core/src/main/java/org/apache/juneau/csv/CsvParserSession.java +++ b/juneau-core/src/main/java/org/apache/juneau/csv/CsvParserSession.java @@ -12,23 +12,17 @@ // *************************************************************************************************************************** package org.apache.juneau.csv; -import java.io.*; -import java.lang.reflect.*; -import java.util.*; - import org.apache.juneau.*; -import org.apache.juneau.http.*; import org.apache.juneau.parser.*; /** * Session object that lives for the duration of a single use of {@link CsvParser}. * * <p> - * This class is NOT thread safe. It is meant to be discarded after one-time use. + * This class is NOT thread safe. + * It is typically discarded after one-time use although it can be reused against multiple inputs. */ -public final class CsvParserSession extends ParserSession { - - private ParserReader reader; +public final class CsvParserSession extends ReaderParserSession { /** * Create a new session using properties specified in the context. @@ -36,84 +30,24 @@ public final class CsvParserSession extends ParserSession { * @param ctx * The context creating this session object. * The context contains all the configuration settings for this object. - * @param input - * The input. Can be any of the following types: - * <ul> - * <li><jk>null</jk> - * <li>{@link Reader} - * <li>{@link CharSequence} - * <li>{@link InputStream} containing UTF-8 encoded text. - * <li>{@link File} containing system encoded text. - * </ul> - * @param op - * The override properties. - * These override any context properties defined in the context. - * @param javaMethod The java method that called this parser, usually the method in a REST servlet. - * @param outer The outer object for instantiating top-level non-static inner classes. - * @param locale - * The session locale. - * If <jk>null</jk>, then the locale defined on the context is used. - * @param timeZone - * The session timezone. - * If <jk>null</jk>, then the timezone defined on the context is used. - * @param mediaType The session media type (e.g. <js>"application/json"</js>). + * @param args + * Runtime session arguments. */ - public CsvParserSession(CsvParserContext ctx, ObjectMap op, Object input, Method javaMethod, Object outer, Locale locale, TimeZone timeZone, MediaType mediaType) { - super(ctx, op, input, javaMethod, outer, locale, timeZone, mediaType); + protected CsvParserSession(CsvParserContext ctx, ParserSessionArgs args) { + super(ctx, args); } @Override /* ParserSession */ - public ParserReader getReader() throws Exception { - if (reader == null) { - Object input = getInput(); - if (input == null) - return null; - if (input instanceof CharSequence) - reader = new ParserReader((CharSequence)input); - else - reader = new ParserReader(super.getReader()); - } - return reader; - } - - /** - * Returns <jk>true</jk> if the specified character is whitespace. - * - * <p> - * The definition of whitespace is different for strict vs lax mode. - * Strict mode only interprets 0x20 (space), 0x09 (tab), 0x0A (line feed) and 0x0D (carriage return) as whitespace. - * Lax mode uses {@link Character#isWhitespace(int)} to make the determination. - * - * @param cp The codepoint. - * @return <jk>true</jk> if the specified character is whitespace. - */ - public final boolean isWhitespace(int cp) { - if (isStrict()) - return cp <= 0x20 && (cp == 0x09 || cp == 0x0A || cp == 0x0D || cp == 0x20); - return Character.isWhitespace(cp); + protected <T> T doParse(ParserPipe pipe, ClassMeta<T> type) throws Exception { + ParserReader r = pipe.getParserReader(); + if (r == null) + return null; + T o = parseAnything(type, r, getOuter(), null); + return o; } - /** - * Returns <jk>true</jk> if the specified character is whitespace or '/'. - * - * @param cp The codepoint. - * @return <jk>true</jk> if the specified character is whitespace or '/'. - */ - public final boolean isCommentOrWhitespace(int cp) { - if (cp == '/') - return true; - if (isStrict()) - return cp <= 0x20 && (cp == 0x09 || cp == 0x0A || cp == 0x0D || cp == 0x20); - return Character.isWhitespace(cp); - } - - @Override /* ParserSession */ - public Map<String,Object> getLastLocation() { - Map<String,Object> m = super.getLastLocation(); - if (reader != null) { - m.put("line", reader.getLine()); - m.put("column", reader.getColumn()); - } - return m; + @SuppressWarnings({"static-method","unused"}) + private <T> T parseAnything(ClassMeta<T> eType, ParserReader r, Object outer, BeanPropertyMeta pMeta) throws Exception { + throw new NoSuchMethodException("Not implemented."); } } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/csv/CsvSerializer.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/csv/CsvSerializer.java b/juneau-core/src/main/java/org/apache/juneau/csv/CsvSerializer.java index c9f2afc..ac59ae0 100644 --- a/juneau-core/src/main/java/org/apache/juneau/csv/CsvSerializer.java +++ b/juneau-core/src/main/java/org/apache/juneau/csv/CsvSerializer.java @@ -12,9 +12,6 @@ // *************************************************************************************************************************** package org.apache.juneau.csv; -import java.io.*; -import java.util.*; - import org.apache.juneau.*; import org.apache.juneau.annotation.*; import org.apache.juneau.serializer.*; @@ -23,12 +20,12 @@ import org.apache.juneau.serializer.*; * TODO - Work in progress. CSV serializer. */ @Produces("text/csv") -@SuppressWarnings({"rawtypes"}) public final class CsvSerializer extends WriterSerializer { /** Default serializer, all default settings.*/ public static final CsvSerializer DEFAULT = new CsvSerializer(PropertyStore.create()); + private final CsvSerializerContext ctx; /** * Constructor. @@ -37,6 +34,7 @@ public final class CsvSerializer extends WriterSerializer { */ public CsvSerializer(PropertyStore propertyStore) { super(propertyStore); + this.ctx = createContext(CsvSerializerContext.class); } @Override /* CoreObject */ @@ -44,61 +42,8 @@ public final class CsvSerializer extends WriterSerializer { return new CsvSerializerBuilder(propertyStore); } - //-------------------------------------------------------------------------------- - // Entry point methods - //-------------------------------------------------------------------------------- - @Override /* Serializer */ - protected void doSerialize(SerializerSession session, SerializerOutput out, Object o) throws Exception { - Writer w = out.getWriter(); - ClassMeta cm = session.getClassMetaForObject(o); - Collection l = null; - if (cm.isArray()) { - l = Arrays.asList((Object[])o); - } else { - l = (Collection)o; - } - // TODO - Doesn't support DynaBeans. - if (l.size() > 0) { - ClassMeta entryType = session.getClassMetaForObject(l.iterator().next()); - if (entryType.isBean()) { - BeanMeta<?> bm = entryType.getBeanMeta(); - int i = 0; - for (BeanPropertyMeta pm : bm.getPropertyMetas()) { - if (i++ > 0) - w.append(','); - append(w, pm.getName()); - } - w.append('\n'); - for (Object o2 : l) { - i = 0; - BeanMap bean = session.toBeanMap(o2); - for (BeanPropertyMeta pm : bm.getPropertyMetas()) { - if (i++ > 0) - w.append(','); - append(w, pm.get(bean, pm.getName())); - } - w.append('\n'); - } - } - } - } - - private static void append(Writer w, Object o) throws IOException { - if (o == null) - w.append("null"); - else { - String s = o.toString(); - boolean mustQuote = false; - for (int i = 0; i < s.length() && ! mustQuote; i++) { - char c = s.charAt(i); - if (Character.isWhitespace(c) || c == ',') - mustQuote = true; - } - if (mustQuote) - w.append('"').append(s).append('"'); - else - w.append(s); - } + public WriterSerializerSession createSession(SerializerSessionArgs args) { + return new CsvSerializerSession(ctx, args); } } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/csv/CsvSerializerSession.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/csv/CsvSerializerSession.java b/juneau-core/src/main/java/org/apache/juneau/csv/CsvSerializerSession.java index 6cb3e1b..80986ea 100644 --- a/juneau-core/src/main/java/org/apache/juneau/csv/CsvSerializerSession.java +++ b/juneau-core/src/main/java/org/apache/juneau/csv/CsvSerializerSession.java @@ -12,20 +12,20 @@ // *************************************************************************************************************************** package org.apache.juneau.csv; -import java.lang.reflect.*; +import java.io.*; import java.util.*; import org.apache.juneau.*; -import org.apache.juneau.http.*; import org.apache.juneau.serializer.*; /** * Session object that lives for the duration of a single use of {@link CsvSerializer}. * * <p> - * This class is NOT thread safe. It is meant to be discarded after one-time use. + * This class is NOT thread safe. + * It is typically discarded after one-time use although it can be reused within the same thread. */ -public final class CsvSerializerSession extends SerializerSession { +public final class CsvSerializerSession extends WriterSerializerSession { /** * Create a new session using properties specified in the context. @@ -33,22 +33,68 @@ public final class CsvSerializerSession extends SerializerSession { * @param ctx * The context creating this session object. * The context contains all the configuration settings for this object. - * @param op - * The override properties. - * These override any context properties defined in the context. - * @param javaMethod The java method that called this serializer, usually the method in a REST servlet. - * @param locale - * The session locale. - * If <jk>null</jk>, then the locale defined on the context is used. - * @param timeZone - * The session timezone. - * If <jk>null</jk>, then the timezone defined on the context is used. - * @param mediaType The session media type (e.g. <js>"application/json"</js>). - * @param uriContext The URI context. - * Identifies the current request URI used for resolution of URIs to absolute or root-relative form. + * @param args + * Runtime arguments. + * These specify session-level information such as locale and URI context. + * It also include session-level properties that override the properties defined on the bean and + * serializer contexts. + * <br>If <jk>null</jk>, defaults to {@link SerializerSessionArgs#DEFAULT}. */ - protected CsvSerializerSession(CsvSerializerContext ctx, ObjectMap op, Method javaMethod, - Locale locale, TimeZone timeZone, MediaType mediaType, UriContext uriContext) { - super(ctx, op, javaMethod, locale, timeZone, mediaType, uriContext); + protected CsvSerializerSession(CsvSerializerContext ctx, SerializerSessionArgs args) { + super(ctx, args); + } + + @Override /* SerializerSession */ + protected final void doSerialize(SerializerPipe out, Object o) throws Exception { + Writer w = out.getWriter(); + ClassMeta<?> cm = getClassMetaForObject(o); + Collection<?> l = null; + if (cm.isArray()) { + l = Arrays.asList((Object[])o); + } else { + l = (Collection<?>)o; + } + // TODO - Doesn't support DynaBeans. + if (l.size() > 0) { + ClassMeta<?> entryType = getClassMetaForObject(l.iterator().next()); + if (entryType.isBean()) { + BeanMeta<?> bm = entryType.getBeanMeta(); + int i = 0; + for (BeanPropertyMeta pm : bm.getPropertyMetas()) { + if (i++ > 0) + w.append(','); + append(w, pm.getName()); + } + w.append('\n'); + for (Object o2 : l) { + i = 0; + BeanMap<?> bean = toBeanMap(o2); + for (BeanPropertyMeta pm : bm.getPropertyMetas()) { + if (i++ > 0) + w.append(','); + append(w, pm.get(bean, pm.getName())); + } + w.append('\n'); + } + } + } + } + + private static void append(Writer w, Object o) throws IOException { + if (o == null) + w.append("null"); + else { + String s = o.toString(); + boolean mustQuote = false; + for (int i = 0; i < s.length() && ! mustQuote; i++) { + char c = s.charAt(i); + if (Character.isWhitespace(c) || c == ',') + mustQuote = true; + } + if (mustQuote) + w.append('"').append(s).append('"'); + else + w.append(s); + } } } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocSerializer.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocSerializer.java b/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocSerializer.java index 58ac1cd..69d6f44 100644 --- a/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocSerializer.java +++ b/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocSerializer.java @@ -12,12 +12,8 @@ // *************************************************************************************************************************** package org.apache.juneau.html; -import java.lang.reflect.*; -import java.util.*; - import org.apache.juneau.*; import org.apache.juneau.annotation.*; -import org.apache.juneau.http.*; import org.apache.juneau.serializer.*; /** @@ -62,44 +58,8 @@ public class HtmlDocSerializer extends HtmlStrippedDocSerializer { this.ctx = createContext(HtmlDocSerializerContext.class); } - - //-------------------------------------------------------------------------------- - // Overridden methods - //-------------------------------------------------------------------------------- - @Override /* Serializer */ - public HtmlDocSerializerSession createSession(ObjectMap op, Method javaMethod, Locale locale, - TimeZone timeZone, MediaType mediaType, UriContext uriContext) { - return new HtmlDocSerializerSession(ctx, op, javaMethod, locale, timeZone, mediaType, uriContext); - } - - @Override /* Serializer */ - protected void doSerialize(SerializerSession session, SerializerOutput out, Object o) throws Exception { - - HtmlDocSerializerSession s = (HtmlDocSerializerSession)session; - HtmlWriter w = s.getHtmlWriter(out); - HtmlDocTemplate t = s.getTemplate(); - - w.sTag("html").nl(0); - w.sTag(1, "head").nl(1); - t.head(s, w, this, o); - w.eTag(1, "head").nl(1); - w.sTag(1, "body").nl(1); - t.body(s, w, this, o); - w.eTag(1, "body").nl(1); - w.eTag("html").nl(0); - } - - /** - * Calls the parent {@link #doSerialize(SerializerSession, SerializerOutput, Object)} method which invokes just the HTML serializer. - * - * @param session The serializer session. - * @param out - * Where to send the output from the serializer. - * @param o The object being serialized. - * @throws Exception - */ - public void parentSerialize(SerializerSession session, SerializerOutput out, Object o) throws Exception { - super.doSerialize(session, out, o); + public HtmlDocSerializerSession createSession(SerializerSessionArgs args) { + return new HtmlDocSerializerSession(ctx, args); } } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocSerializerSession.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocSerializerSession.java b/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocSerializerSession.java index 0b8179f..c51a3a0 100644 --- a/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocSerializerSession.java +++ b/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocSerializerSession.java @@ -14,11 +14,7 @@ package org.apache.juneau.html; import static org.apache.juneau.html.HtmlDocSerializerContext.*; -import java.lang.reflect.*; -import java.util.*; - import org.apache.juneau.*; -import org.apache.juneau.http.*; import org.apache.juneau.internal.*; import org.apache.juneau.serializer.*; @@ -31,7 +27,7 @@ import org.apache.juneau.serializer.*; * <p> * This class is NOT thread safe. It is meant to be discarded after one-time use. */ -public final class HtmlDocSerializerSession extends HtmlSerializerSession { +public class HtmlDocSerializerSession extends HtmlStrippedDocSerializerSession { private final String header, nav, aside, footer, noResultsMessage; private final String[] style, stylesheet, script, links; @@ -44,25 +40,13 @@ public final class HtmlDocSerializerSession extends HtmlSerializerSession { * @param ctx * The context creating this session object. * The context contains all the configuration settings for this object. - * @param op - * The override properties. - * These override any context properties defined in the context. - * @param javaMethod The java method that called this serializer, usually the method in a REST servlet. - * @param locale - * The session locale. - * If <jk>null</jk>, then the locale defined on the context is used. - * @param timeZone - * The session timezone. - * If <jk>null</jk>, then the timezone defined on the context is used. - * @param mediaType The session media type (e.g. <js>"application/json"</js>). - * @param uriContext - * The URI context. - * Identifies the current request URI used for resolution of URIs to absolute or root-relative form. + * @param args + * Runtime arguments. */ - protected HtmlDocSerializerSession(HtmlDocSerializerContext ctx, ObjectMap op, Method javaMethod, - Locale locale, TimeZone timeZone, MediaType mediaType, UriContext uriContext) { - super(ctx, op, javaMethod, locale, timeZone, mediaType, uriContext); - if (op == null || op.isEmpty()) { + protected HtmlDocSerializerSession(HtmlDocSerializerContext ctx, SerializerSessionArgs args) { + super(ctx, args); + ObjectMap p = getProperties(); + if (p.isEmpty()) { header = ctx.header; nav = ctx.nav; aside = ctx.aside; @@ -75,17 +59,17 @@ public final class HtmlDocSerializerSession extends HtmlSerializerSession { noResultsMessage = ctx.noResultsMessage; template = ClassUtils.newInstance(HtmlDocTemplate.class, ctx.template); } else { - header = op.getString(HTMLDOC_header, ctx.nav); - nav = op.getString(HTMLDOC_nav, ctx.nav); - aside = op.getString(HTMLDOC_aside, ctx.aside); - footer = op.getString(HTMLDOC_footer, ctx.footer); - links = op.getStringArray(HTMLDOC_links, ctx.links); - style = op.getStringArray(HTMLDOC_style, ctx.style); - stylesheet = op.getStringArray(HTMLDOC_stylesheet, ctx.stylesheet); - script = op.getStringArray(HTMLDOC_script, ctx.script); - nowrap = op.getBoolean(HTMLDOC_nowrap, ctx.nowrap); - noResultsMessage = op.getString(HTMLDOC_noResultsMessage, ctx.noResultsMessage); - template = ClassUtils.newInstance(HtmlDocTemplate.class, op.get(HTMLDOC_template, ctx.template)); + header = p.getString(HTMLDOC_header, ctx.nav); + nav = p.getString(HTMLDOC_nav, ctx.nav); + aside = p.getString(HTMLDOC_aside, ctx.aside); + footer = p.getString(HTMLDOC_footer, ctx.footer); + links = p.getStringArray(HTMLDOC_links, ctx.links); + style = p.getStringArray(HTMLDOC_style, ctx.style); + stylesheet = p.getStringArray(HTMLDOC_stylesheet, ctx.stylesheet); + script = p.getStringArray(HTMLDOC_script, ctx.script); + nowrap = p.getBoolean(HTMLDOC_nowrap, ctx.nowrap); + noResultsMessage = p.getString(HTMLDOC_noResultsMessage, ctx.noResultsMessage); + template = ClassUtils.newInstance(HtmlDocTemplate.class, p.get(HTMLDOC_template, ctx.template)); } } @@ -216,4 +200,37 @@ public final class HtmlDocSerializerSession extends HtmlSerializerSession { public final String getNoResultsMessage() { return noResultsMessage; } + + @Override /* Serializer */ + protected void doSerialize(SerializerPipe out, Object o) throws Exception { + + HtmlWriter w = getHtmlWriter(out); + HtmlDocTemplate t = getTemplate(); + + w.sTag("html").nl(0); + w.sTag(1, "head").nl(1); + t.head(this, w, o); + w.eTag(1, "head").nl(1); + w.sTag(1, "body").nl(1); + t.body(this, w, o); + w.eTag(1, "body").nl(1); + w.eTag("html").nl(0); + } + + /** + * Calls the parent {@link #doSerialize(SerializerPipe, Object)} method which invokes just the HTML serializer. + * + * @param out + * Where to send the output from the serializer. + * @param o The object being serialized. + * @throws Exception + */ + public void parentSerialize(Object out, Object o) throws Exception { + SerializerPipe pipe = createPipe(out); + try { + super.doSerialize(pipe, o); + } finally { + pipe.close(); + } + } } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocTemplate.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocTemplate.java b/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocTemplate.java index 7633eb6..562bac9 100644 --- a/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocTemplate.java +++ b/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocTemplate.java @@ -60,99 +60,90 @@ public interface HtmlDocTemplate { * * @param session The current serializer session. * @param w The writer being written to. - * @param s The serializer calling this method. * @param o The object being serialized. * @throws Exception Any exception can be thrown. */ - public void head(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception; + public void head(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception; /** * Renders the contents of the <code><xt><head></xt>/<xt><style></xt></code> element. * * @param session The current serializer session. * @param w The writer being written to. - * @param s The serializer calling this method. * @param o The object being serialized. * @throws Exception Any exception can be thrown. */ - public void style(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception; + public void style(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception; /** * Renders the contents of the <code><xt><head></xt>/<xt><script></xt></code> element. * * @param session The current serializer session. * @param w The writer being written to. - * @param s The serializer calling this method. * @param o The object being serialized. * @throws Exception Any exception can be thrown. */ - public void script(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception; + public void script(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception; /** * Renders the contents of the <code><xt><body></xt></code> element. * * @param session The current serializer session. * @param w The writer being written to. - * @param s The serializer calling this method. * @param o The object being serialized. * @throws Exception Any exception can be thrown. */ - public void body(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception; + public void body(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception; /** * Renders the contents of the <code><xt><body></xt>/<xt><header></xt></code> element. * * @param session The current serializer session. * @param w The writer being written to. - * @param s The serializer calling this method. * @param o The object being serialized. * @throws Exception Any exception can be thrown. */ - public void header(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception; + public void header(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception; /** * Renders the contents of the <code><xt><body></xt>/<xt><nav></xt></code> element. * * @param session The current serializer session. * @param w The writer being written to. - * @param s The serializer calling this method. * @param o The object being serialized. * @throws Exception Any exception can be thrown. */ - public void nav(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception; + public void nav(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception; /** * Renders the contents of the <code><xt><body></xt>/<xt><article></xt></code> element. * * @param session The current serializer session. * @param w The writer being written to. - * @param s The serializer calling this method. * @param o The object being serialized. * @throws Exception Any exception can be thrown. */ - public void article(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception; + public void article(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception; /** * Renders the contents of the <code><xt><body></xt>/<xt><aside></xt></code> element. * * @param session The current serializer session. * @param w The writer being written to. - * @param s The serializer calling this method. * @param o The object being serialized. * @throws Exception Any exception can be thrown. */ - public void aside(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception; + public void aside(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception; /** * Renders the contents of the <code><xt><body></xt>/<xt><footer></xt></code> element. * * @param session The current serializer session. * @param w The writer being written to. - * @param s The serializer calling this method. * @param o The object being serialized. * @throws Exception Any exception can be thrown. */ - public void footer(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception; + public void footer(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception; /** * Returns <jk>true</jk> if this page should render a <code><xt><head></xt>/<xt><style></xt></code> element. http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2a37f310/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocTemplateBasic.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocTemplateBasic.java b/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocTemplateBasic.java index 126097b..06a4ccb 100644 --- a/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocTemplateBasic.java +++ b/juneau-core/src/main/java/org/apache/juneau/html/HtmlDocTemplateBasic.java @@ -13,7 +13,6 @@ package org.apache.juneau.html; import org.apache.juneau.internal.*; -import org.apache.juneau.serializer.*; /** * A basic template for the HTML doc serializer. @@ -24,21 +23,21 @@ import org.apache.juneau.serializer.*; public class HtmlDocTemplateBasic implements HtmlDocTemplate { @Override /* HtmlDocTemplate */ - public void head(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception { + public void head(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception { if (hasStyle(session)) { w.sTag(2, "style").nl(2); - style(session, w, s, o); + style(session, w, o); w.ie(2).eTag("style").nl(2); } if (hasScript(session)) { w.sTag(2, "script").nl(2); - script(session, w, s, o); + script(session, w, o); w.ie(2).eTag("script").nl(2); } } @Override /* HtmlDocTemplate */ - public void style(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception { + public void style(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception { String[] stylesheet = session.getStylesheet(); if (! ArrayUtils.contains("NONE", stylesheet)) @@ -54,7 +53,7 @@ public class HtmlDocTemplateBasic implements HtmlDocTemplate { } @Override /* HtmlDocTemplate */ - public void script(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception { + public void script(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception { if (session.getScript() != null) for (String script : session.getScript()) @@ -62,29 +61,29 @@ public class HtmlDocTemplateBasic implements HtmlDocTemplate { } @Override /* HtmlDocTemplate */ - public void body(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception { + public void body(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception { if (hasHeader(session)) { w.sTag(2, "header").nl(2); - header(session, w, s, o); + header(session, w, o); w.ie(2).eTag("header").nl(2); } if (hasNav(session)) { w.sTag(2, "nav").nl(2); - nav(session, w, s, o); + nav(session, w, o); w.ie(2).eTag("nav").nl(2); } w.sTag(2, "section").nl(2); w.sTag(3, "article").nl(3); - article(session, w, s, o); + article(session, w, o); w.ie(3).eTag("article").nl(3); if (hasAside(session)) { w.sTag(3, "aside").nl(3); - aside(session, w, s, o); + aside(session, w, o); w.ie(3).eTag("aside").nl(3); } @@ -92,13 +91,13 @@ public class HtmlDocTemplateBasic implements HtmlDocTemplate { if (hasFooter(session)) { w.sTag(2, "footer").nl(2); - footer(session, w, s, o); + footer(session, w, o); w.ie(2).eTag("footer").nl(2); } } @Override /* HtmlDocTemplate */ - public void header(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception { + public void header(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception { // Write the title of the page. String header = session.getHeader(); if (exists(header)) @@ -107,7 +106,7 @@ public class HtmlDocTemplateBasic implements HtmlDocTemplate { @Override /* HtmlDocTemplate */ - public void nav(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception { + public void nav(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception { String nav = session.getNav(); if (nav != null) { if (exists(nav)) @@ -138,14 +137,14 @@ public class HtmlDocTemplateBasic implements HtmlDocTemplate { } @Override /* HtmlDocTemplate */ - public void aside(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception { + public void aside(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception { String aside = session.getAside(); if (exists(aside)) w.append(4, aside); } @Override /* HtmlDocTemplate */ - public void article(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception { + public void article(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception { // To allow for page formatting using CSS, we encapsulate the data inside two div tags: // <div class='outerdata'><div class='data' id='data'>...</div></div> w.oTag(4, "div").attr("class","outerdata").append('>').nl(4); @@ -160,7 +159,7 @@ public class HtmlDocTemplateBasic implements HtmlDocTemplate { } else { session.indent = 6; w.flush(); - s.parentSerialize(session, new SerializerOutput(w), o); + session.parentSerialize(w, o); } w.ie(5).eTag("div").nl(5); @@ -168,7 +167,7 @@ public class HtmlDocTemplateBasic implements HtmlDocTemplate { } @Override /* HtmlDocTemplate */ - public void footer(HtmlDocSerializerSession session, HtmlWriter w, HtmlDocSerializer s, Object o) throws Exception { + public void footer(HtmlDocSerializerSession session, HtmlWriter w, Object o) throws Exception { String footer = session.getFooter(); if (exists(footer)) w.append(3, footer).nl(3);
