http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/DataSet.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/DataSet.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/DataSet.java new file mode 100755 index 0000000..3b107b5 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/DataSet.java @@ -0,0 +1,188 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.dto.cognos; + +import java.util.*; + +import com.ibm.juno.core.*; +import com.ibm.juno.core.annotation.*; +import com.ibm.juno.core.xml.annotation.*; + +/** + * Represents a Cognos dataset. + * <p> + * When serialized to XML, creates the following construct (example pulled from <code>AddressBookResource</code>): + * <p class='bcode'> + * <xt><?xml</xt> <xa>version</xa>=<xs>'1.0'</xs> <xa>encoding</xa>=<xs>'UTF-8'</xs><xt>?></xt> + * <xt><c:dataset <xa>xmlns:c</xa>=<xs>'http://developer.cognos.com/schemas/xmldata/1/'</xs>></xt> + * <xt><c:metadata></xt> + * <xt><c:item</xt> <xa>name</xa>=<xs>'name'</xs> <xa>type</xa>=<xs>'xs:String'</xs> <xa>length</xa>=<xs>'255'</xs><xt>/></xt> + * <xt><c:item</xt> <xa>name</xa>=<xs>'age'</xs> <xa>type</xa>=<xs>'xs:int'</xs><xt>/></xt> + * <xt><c:item</xt> <xa>name</xa>=<xs>'numAddresses'</xs> <xa>type</xa>=<xs>'xs:int'</xs><xt>/></xt> + * <xt></c:metadata></xt> + * <xt><c:data></xt> + * <xt><c:row></xt> + * <xt><c:value></xt>Barack Obama<xt></c:value></xt> + * <xt><c:value></xt>52<xt></c:value></xt> + * <xt><c:value></xt>2<xt></c:value></xt> + * <xt></c:row></xt> + * <xt><c:row></xt> + * <xt><c:value></xt>George Walker Bush<xt></c:value></xt> + * <xt><c:value></xt>67<xt></c:value></xt> + * <xt><c:value></xt>2<xt></c:value></xt> + * <xt></c:row></xt> + * <xt></c:data></xt> + * <xt></c:dataset></xt> + * </p> + * <p> + * Only 2-dimentional POJOs (arrays or collections of maps or beans) can be serialized to Cognos. + * + * <h6 class='topic'>Example</h6> + * <p> + * The construct shown above is a serialized <code>AddressBook</code> object which is a subclass of <code>LinkedList<Person></code>. + * The code for generating the XML is as follows... + * </p> + * <p class='bcode'> + * Column[] items = { + * <jk>new</jk> Column(<js>"name"</js>, <js>"xs:String"</js>, 255), + * <jk>new</jk> Column(<js>"age"</js>, <js>"xs:int"</js>), + * <jk>new</jk> Column(<js>"numAddresses"</js>, <js>"xs:int"</js>) + * .addFilter( + * <jk>new</jk> PojoFilter<Person,Integer>() { + * <ja>@Override</ja> + * <jk>public</jk> Integer filter(Person p) { + * <jk>return</jk> p.<jf>addresses</jf>.size(); + * } + * } + * ) + * }; + * + * DataSet ds = <jk>new</jk> DataSet(items, <jsf>addressBook</jsf>, BeanContext.<jsf>DEFAULT</jsf>); + * + * String xml = XmlSerializer.<jsf>DEFAULT_SQ</jsf>.serialize(ds); + * </p> + * + * @author James Bognar ([email protected]) + */ +@Xml(name="dataset") +@SuppressWarnings("unchecked") +public class DataSet { + + private Column[] metaData; + private List<Row> data; + + /** Bean constructor. */ + public DataSet() {} + + /** + * Constructor. + * + * @param columns The meta-data that represents the columns in the dataset. + * @param o The POJO being serialized to Cognos. + * Must be an array/collection of beans/maps. + * @param beanContext The bean context used to convert POJOs to strings. + * @throws Exception An error occurred trying to serialize the POJO. + */ + public DataSet(Column[] columns, Object o, BeanContext beanContext) throws Exception { + metaData = columns; + data = new LinkedList<Row>(); + if (o != null) { + if (o.getClass().isArray()) + o = Arrays.asList((Object[])o); + if (o instanceof Collection) { + Collection<?> c = (Collection<?>)o; + for (Object o2 : c) { + Row r = new Row(); + Map<?,?> m = null; + if (o2 instanceof Map) + m = (Map<?,?>)o2; + else + m = beanContext.forBean(o2); + for (Column col : columns) { + Object v; + if (col.filter != null) + v = col.filter.filter(o2); + else + v = m.get(col.getName()); + r.add(v == null ? null : v.toString()); + } + data.add(r); + } + } + } + } + + /** + * Represents a row of data. + * <p> + * When serialized to XML, creates the following construct (example pulled from <code>AddressBookResource</code>): + * <p class='bcode'> + * <xt><row></xt> + * <xt><value></xt>Barack Obama<xt></value></xt> + * <xt><value></xt>52<xt></value></xt> + * <xt><value></xt>2<xt></value></xt> + * <xt></row></xt> + * </p> + * + * @author James Bognar ([email protected]) + */ + @Xml(name="row", childName="value") + public static class Row extends LinkedList<String> { + private static final long serialVersionUID = 1L; + } + + + //-------------------------------------------------------------------------------- + // Bean properties + //-------------------------------------------------------------------------------- + + /** + * Bean property getter: <property>metadata</property>. + * + * @return The value of the <property>metadata</property> property on this bean, or <jk>null</jk> if it is not set. + */ + @BeanProperty(name="metadata") + public Column[] getMetaData() { + return metaData; + } + + /** + * Bean property setter: <property>metadata</property>. + * + * @param metaData The new value for the <property>metadata</property> property on this bean. + * @return This object (for method chaining). + */ + @BeanProperty(name="metadata") + public DataSet setMetaData(Column[] metaData) { + this.metaData = metaData; + return this; + } + + /** + * Bean property getter: <property>data</property>. + * + * @return The value of the <property>data</property> property on this bean, or <jk>null</jk> if it is not set. + */ + @BeanProperty(name="data") + public List<Row> getData() { + return data; + } + + /** + * Bean property setter: <property>data</property>. + * + * @param data The new value for the <property>data</property> property on this bean. + * @return This object (for method chaining). + */ + @BeanProperty(name="data") + public DataSet setData(List<Row> data) { + this.data = data; + return this; + } +}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/doc-files/HTML.png ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/doc-files/HTML.png b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/doc-files/HTML.png new file mode 100755 index 0000000..fd63c23 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/doc-files/HTML.png differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/doc-files/JSON.png ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/doc-files/JSON.png b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/doc-files/JSON.png new file mode 100755 index 0000000..44785ad Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/doc-files/JSON.png differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/doc-files/RDFXML.png ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/doc-files/RDFXML.png b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/doc-files/RDFXML.png new file mode 100755 index 0000000..081e949 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/doc-files/RDFXML.png differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/package-info.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/package-info.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/package-info.class new file mode 100755 index 0000000..76e935c Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/package-info.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/package-info.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/package-info.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/package-info.java new file mode 100755 index 0000000..9919e9b --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/package-info.java @@ -0,0 +1,13 @@ +/* ***************************************************************************** + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014. All Rights Reserved. + * + * Note to U.S. Government Users Restricted Rights: Use, + * duplication or disclosure restricted by GSA ADP Schedule + * Contract with IBM Corp. + *******************************************************************************/ +// XML namespaces used in this package +@XmlSchema(prefix="cognos", namespace="http://developer.cognos.com/schemas/xmldata/1/") +package com.ibm.juno.core.dto.cognos; +import com.ibm.juno.core.xml.annotation.*; + http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/package.html ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/package.html b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/package.html new file mode 100755 index 0000000..2b7cf71 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/cognos/package.html @@ -0,0 +1,170 @@ +<!DOCTYPE HTML> +<!-- + Licensed Materials - Property of IBM + (c) Copyright IBM Corporation 2014. All Rights Reserved. + + Note to U.S. Government Users Restricted Rights: + Use, duplication or disclosure restricted by GSA ADP Schedule + Contract with IBM Corp. + --> +<html> +<head> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> + <style type="text/css"> + /* For viewing in Page Designer */ + @IMPORT url("../../../../../../javadoc.css"); + + /* For viewing in REST interface */ + @IMPORT url("../htdocs/javadoc.css"); + body { + margin: 20px; + } + </style> + <script> + /* Replace all @code and @link tags. */ + window.onload = function() { + document.body.innerHTML = document.body.innerHTML.replace(/\{\@code ([^\}]+)\}/g, '<code>$1</code>'); + document.body.innerHTML = document.body.innerHTML.replace(/\{\@link (([^\}]+)\.)?([^\.\}]+)\}/g, '<code>$3</code>'); + } + </script> +</head> +<body> +<p>Cognos Data Transfer Objects</p> +<script> + function toggle(x) { + var div = x.nextSibling; + while (div != null && div.nodeType != 1) + div = div.nextSibling; + if (div != null) { + var d = div.style.display; + if (d == 'block' || d == '') { + div.style.display = 'none'; + x.className += " closed"; + } else { + div.style.display = 'block'; + x.className = x.className.replace(/(?:^|\s)closed(?!\S)/g , '' ); + } + } + } +</script> +<a id='TOC'></a><h5 class='toc'>Table of Contents</h5> +<ol class='toc'> + <li><p><a class='doclink' href='#CognosSerializer'>Cognos serialization support</a></p> + <li><p><a class='doclink' href='#CognosParser'>Cognos parsing support</a></p> +</ol> + +<!-- ======================================================================================================== --> +<a id="CognosSerializer"></a> +<h2 class='topic' onclick='toggle(this)'>1 - Cognos serialization support</h2> +<div class='topic'> + <p> + The {@link com.ibm.juno.core.dto.cognos.DataSet} class is a DTO used to convert POJO models directly to Cognos-XML. + </p> + <p> + Because of the nature of the Cognos XML syntax, only <i>2-dimensional</i> POJO data structures can be serialized to Cognos-XML. + </p> + <p> + For example... + </p> + <ul class='normal'> + <li><code>Collection<Bean></code> + <li><code>Collection<Map></code> + <li>{@code MyBean[]} + <li>{@code HashMap[]} + </ul> + <h6 class='topic'>Example</h6> + <p> + The following example shows how to generate Cognos-XML from a POJO. + The example uses the <a class='doclink' href='../../doc-files/AddressBook.html'>AddressBook</a> sample POJO. + It should be noted that since the {@code AddressBook} class is a subclass of {@code LinkedList}, it fulfills + the requirement of being a tabular data structure. + </p> + <p class='bcode'> + <jc>// Create our POJO with some entries.</jc> + AddressBook addressBook = <jk>new</jk> AddressBook(); + addressBook.add( + <jk>new</jk> Person(<js>"Barack Obama"</js>, <js>"Aug 4, 1961"</js>, + <jk>new</jk> Address(<js>"1600 Pennsylvania Ave"</js>, <js>"Washington"</js>, <js>"DC"</js>, 20500, <jk>true</jk>), + <jk>new</jk> Address(<js>"5046 S Greenwood Ave"</js>, <js>"Chicago"</js>, <js>"IL"</js>, 60615, <jk>false</jk>) + ) + ); + addressBook.add( + <jk>new</jk> Person(<js>"George Walker Bush"</js>, <js>"Jul 6, 1946"</js>, + <jk>new</jk> Address(<js>"43 Prairie Chapel Rd"</js>, <js>"Crawford"</js>, <js>"TX"</js>, 76638, <jk>true</jk>), + <jk>new</jk> Address(<js>"1600 Pennsylvania Ave"</js>, <js>"Washington"</js>, <js>"DC"</js>, 20500, <jk>false</jk>) + ) + ); + + <jc>// Define the Cognos metadata</jc> + Column[] items = { + <jk>new</jk> Column(<js>"name"</js>, <js>"xs:String"</js>, 255), + <jk>new</jk> Column(<js>"age"</js>, <js>"xs:int"</js>), + <jk>new</jk> Column(<js>"numAddresses"</js>, <js>"xs:int"</js>) + .addFilter( + <jk>new</jk> PojoFilter<Person,Integer>() { + <ja>@Override</ja> + <jk>public</jk> Integer filter(Person p) { + <jk>return</jk> p.<jf>addresses</jf>.size(); + } + } + ) + }; + + <jc>// Create the Cognos DataSet object</jc> + DataSet ds = <jk>new</jk> DataSet(items, <jsf>addressBook</jsf>, BeanContext.<jsf>DEFAULT</jsf>); + + <jc>// Serialize it to XML</jc> + String xml = XmlSerializer.<jsf>DEFAULT_SQ</jsf>.serialize(ds); + </p> + <p> + When run, this code produces the following XML... + </p> + <p class='bcode'> + <xt><?xml</xt> <xa>version</xa>=<xs>'1.0'</xs> <xa>encoding</xa>=<xs>'UTF-8'</xs><xt>?></xt> + <xt><c:dataset <xa>xmlns:c</xa>=<xs>'http://developer.cognos.com/schemas/xmldata/1/'</xs>></xt> + <xt><c:metadata></xt> + <xt><c:item</xt> <xa>name</xa>=<xs>'name'</xs> <xa>type</xa>=<xs>'xs:String'</xs> <xa>length</xa>=<xs>'255'</xs><xt>/></xt> + <xt><c:item</xt> <xa>name</xa>=<xs>'age'</xs> <xa>type</xa>=<xs>'xs:int'</xs><xt>/></xt> + <xt><c:item</xt> <xa>name</xa>=<xs>'numAddresses'</xs> <xa>type</xa>=<xs>'xs:int'</xs><xt>/></xt> + <xt></c:metadata></xt> + <xt><c:data></xt> + <xt><c:row></xt> + <xt><c:value></xt>Barack Obama<xt></c:value></xt> + <xt><c:value></xt>52<xt></c:value></xt> + <xt><c:value></xt>2<xt></c:value></xt> + <xt></c:row></xt> + <xt><c:row></xt> + <xt><c:value></xt>George Walker Bush<xt></c:value></xt> + <xt><c:value></xt>67<xt></c:value></xt> + <xt><c:value></xt>2<xt></c:value></xt> + <xt></c:row></xt> + <xt></c:data></xt> + <xt></c:dataset></xt> + </p> + <h6 class='topic'>Other data formats</h6> + <p> + The following shows examples of what this data structure looks like when serialized to other formats: + </p> + <h6 class='figure'>HTML</h6> + <img class='bordered' src='doc-files/HTML.png'> + <h6 class='figure'>JSON</h6> + <img class='bordered' src='doc-files/JSON.png'> + <h6 class='figure'>RDF/XML</h6> + <img class='bordered' src='doc-files/RDFXML.png'> +</div> + +<!-- ======================================================================================================== --> +<a id="CognosParser"></a> +<h2 class='topic' onclick='toggle(this)'>2 - Cognos parsing support</h2> +<div class='topic'> + <p> + The {@link com.ibm.juno.core.dto.cognos.DataSet} class can be reconstructed from Cognos/XML using one of the standard XML parsers. + </p> + <h6 class='topic'>Example</h6> + <p class='bcode'> + <jc>// Parse XML back into original DataSet</jc> + DataSet ds = XmlParser.<jsf>DEFAULT</jsf>.parse(xml, DataSet.<jk>class</jk>); + </p> +</div> +</body> +</html> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonType.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonType.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonType.class new file mode 100755 index 0000000..0ad6388 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonType.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonType.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonType.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonType.java new file mode 100755 index 0000000..c9daf4c --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonType.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.dto.jsonschema; + +/** + * Represents possible JSON types in the JSON-Schema core specification. + * <p> + * Implements custom <code>toString()</code> and <code>fromString(String)</code> methods + * that override the default serialization/parsing behavior of <code>Enum</code> types + * so that they are represented in lowercase form (as per the specification). + * + * <h6 class='figure'>Example</h6> + * <p class='bcode'> + * // Produces 'number', not 'NUMBER'. + * String json = JsonSerializer.DEFAULT.serialize(JsonType.NUMBER); + * </p> + * + * <p> + * Refer to {@link com.ibm.juno.core.dto.jsonschema} for usage information. + * + * @author James Bognar ([email protected]) + */ +public enum JsonType { + + /** array */ + ARRAY("array"), + + /** boolean */ + BOOLEAN("boolean"), + + /** integer */ + INTEGER("integer"), + + /** null */ + NULL("null"), + + /** number */ + NUMBER("number"), + + /** object */ + OBJECT("object"), + + /** string */ + STRING("string"), + + /** any */ + ANY("any"); + + private final String value; // The serialized format of the enum. + + private JsonType(String value) { + this.value = value; + } + + /** + * Returns the lowercase form of this enum that's compatible with the JSON-Schema specification. + */ + @Override /* Object */ + public String toString() { + return value; + } + + /** + * Converts the specified lowercase form of the enum back into an <code>Enum</code>. + * + * @param value The lowercase form of the enum (e.g. <js>"array"</js>). + * @return The matching <code>Enum</code>, or <jk>null</jk> if no match found. + */ + public static JsonType fromString(String value) { + if (value == null || value.length() < 4) + return null; + char c = value.charAt(0); + if (c == 'a') { + if (value.equals("array")) + return ARRAY; + if (value.equals("any")) + return ANY; + } + if (c == 'b' && value.equals("boolean")) + return BOOLEAN; + if (c == 'i' && value.equals("integer")) + return INTEGER; + if (c == 'n') { + c = value.charAt(2); + if (c == 'l' && value.equals("null")) + return NULL; + if (c == 'm' && value.equals("number")) + return NUMBER; + return null; + } + if (c == 'o' && value.equals("object")) + return OBJECT; + if (c == 's' && value.equals("string")) + return STRING; + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonTypeArray.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonTypeArray.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonTypeArray.class new file mode 100755 index 0000000..5a10330 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonTypeArray.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonTypeArray.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonTypeArray.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonTypeArray.java new file mode 100755 index 0000000..d383162 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/JsonTypeArray.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.dto.jsonschema; + +import java.util.*; + +/** + * Represents a list of {@link JsonType} objects. + * <p> + * Refer to {@link com.ibm.juno.core.dto.jsonschema} for usage information. + * + * @author James Bognar ([email protected]) + */ +public final class JsonTypeArray extends LinkedList<JsonType> { + + private static final long serialVersionUID = 1L; + + /** + * Default constructor. + */ + public JsonTypeArray() {} + + /** + * Constructor with predefined types to add to this list. + * + * @param types The list of types to add to the list. + */ + public JsonTypeArray(JsonType...types) { + addAll(types); + } + + /** + * Convenience method for adding one or more {@link JsonType} objects to + * this array. + * + * @param types The {@link JsonType} objects to add to this array. + * @return This object (for method chaining). + */ + public JsonTypeArray addAll(JsonType...types) { + for (JsonType t : types) + add(t); + return this; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Sample$1.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Sample$1.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Sample$1.class new file mode 100755 index 0000000..d967e1c Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Sample$1.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Sample.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Sample.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Sample.class new file mode 100755 index 0000000..375479e Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Sample.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Sample.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Sample.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Sample.java new file mode 100755 index 0000000..636755d --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Sample.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.dto.jsonschema; + +import java.io.*; +import java.net.*; + +import com.ibm.juno.core.json.*; +import com.ibm.juno.core.utils.*; + +@SuppressWarnings("serial") +class Sample { + + public static void main(String[] args) { + + // Create a SchemaMap for looking up schemas. + SchemaMap schemaMap = new SchemaMap() { + + @Override /* SchemaMap */ + public Schema load(URI uri) { + Reader r = null; + try { + r = new InputStreamReader(uri.toURL().openStream(), IOUtils.UTF8); + Schema s = JsonParser.DEFAULT.parse(r, -1, Schema.class); + return s; + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + if (r != null) { + try { + r.close(); + } catch (IOException e) { + } + } + } + } + }; + + // Get schema from the schema map. + Schema purchaseOrderSchema = schemaMap.get("http://www.ibm.com/purchase-order/PurchaseOrder#"); + + JsonType streetType = purchaseOrderSchema + .getProperty("address",true) // Get "address" property, resolved to Address schema. + .getProperty("street") // Get "street" property. + .getTypeAsJsonType(); // Get data type. + System.err.println("streetType=" + streetType); // Prints "streetType=string" + + JsonType productIdType = purchaseOrderSchema + .getProperty("product") // Get "product" property + .getItemsAsSchemaArray() // Get "items". + .get(0) // Get first entry. + .resolve() // Resolve to Product schema. + .getProperty("productId") // Get "productId" property. + .getTypeAsJsonType(); // Get data type. + System.err.println("productIdType=" + productIdType); // Prints "productIdType=number" + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$BooleanOrSchemaArrayFilter.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$BooleanOrSchemaArrayFilter.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$BooleanOrSchemaArrayFilter.class new file mode 100755 index 0000000..740164b Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$BooleanOrSchemaArrayFilter.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$BooleanOrSchemaFilter.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$BooleanOrSchemaFilter.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$BooleanOrSchemaFilter.class new file mode 100755 index 0000000..f3f9e32 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$BooleanOrSchemaFilter.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$JsonTypeOrJsonTypeArrayFilter.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$JsonTypeOrJsonTypeArrayFilter.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$JsonTypeOrJsonTypeArrayFilter.class new file mode 100755 index 0000000..df1cabb Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$JsonTypeOrJsonTypeArrayFilter.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$SchemaOrSchemaArrayFilter.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$SchemaOrSchemaArrayFilter.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$SchemaOrSchemaArrayFilter.class new file mode 100755 index 0000000..6000f8b Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema$SchemaOrSchemaArrayFilter.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema.class new file mode 100755 index 0000000..496ead5 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema.java new file mode 100755 index 0000000..7772f05 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/Schema.java @@ -0,0 +1,1392 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.dto.jsonschema; + +import java.net.URI; +import java.util.*; + +import com.ibm.juno.core.*; +import com.ibm.juno.core.annotation.*; +import com.ibm.juno.core.filter.*; +import com.ibm.juno.core.parser.*; +import com.ibm.juno.core.serializer.*; +import com.ibm.juno.core.xml.annotation.*; + +/** + * Represents a top-level schema object bean in the JSON-Schema core specification. + * <p> + * Refer to {@link com.ibm.juno.core.dto.jsonschema} for usage information. + * + * @author James Bognar ([email protected]) + */ +@Xml(name="schema") +@SuppressWarnings("hiding") +public class Schema { + private String name; // Property name. Not serialized. + private URI id; + private URI schemaVersion; + private String title; + private String description; + private JsonType typeJsonType; // JsonType representation of type + private JsonTypeArray typeJsonTypeArray; // JsonTypeArray representation of type + private Map<String,Schema> definitions; + private Map<String,Schema> properties; + private Map<String,Schema> patternProperties; + private Map<String,Schema> dependencies; + private Schema itemsSchema; // Schema representation of items + private SchemaArray itemsSchemaArray; // SchemaArray representation of items + private Number multipleOf; + private Number maximum; + private Boolean exclusiveMaximum; + private Number minimum; + private Boolean exclusiveMinimum; + private Integer maxLength; + private Integer minLength; + private String pattern; + private Boolean additionalItemsBoolean; // Boolean representation of additionalItems + private SchemaArray additionalItemsSchemaArray; // SchemaArray representation of additionalItems + private Integer maxItems; + private Integer minItems; + private Boolean uniqueItems; + private Integer maxProperties; + private Integer minProperties; + private List<String> required; + private Boolean additionalPropertiesBoolean; // Boolean representation of additionalProperties + private Schema additionalPropertiesSchema; // Schema representation of additionalProperties + private List<String> _enum; + private List<Schema> allOf; + private List<Schema> anyOf; + private List<Schema> oneOf; + private Schema not; + private URI ref; + private SchemaMap schemaMap; + private Schema master = this; + + /** + * Default constructor. + */ + public Schema() {} + + //-------------------------------------------------------------------------------- + // Bean properties + //-------------------------------------------------------------------------------- + + /** + * Bean property getter: <property>name</property>. + * + * @return The value of the <property>name</property> property on this bean, or <jk>null</jk> if it is not set. + */ + @BeanIgnore + public String getName() { + return name; + } + + /** + * Bean property setter: <property>name</property>. + * + * @param name The new value for the <property>name</property> property on this bean. + * @return This object (for method chaining). + */ + @BeanIgnore + public Schema setName(String name) { + this.name = name; + return this; + } + + /** + * Bean property getter: <property>id</property>. + * + * @return The value of the <property>id</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public URI getId() { + return id; + } + + /** + * Bean property setter: <property>id</property>. + * + * @param id The new value for the <property>id</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setId(URI id) { + this.id = id; + return this; + } + + /** + * Bean property setter: <property>id</property>. + * + * @param id The new value for the <property>id</property> property on this bean. + * The parameter must be a valid URI. It can be <jk>null</jk>. + * @return This object (for method chaining). + */ + public Schema setId(String id) { + return setId(id == null ? null : URI.create(id)); + } + + /** + * Bean property getter: <property>$schema</property>. + * + * @return The value of the <property>$schema</property> property on this bean, or <jk>null</jk> if it is not set. + */ + @BeanProperty(name="$schema") + public URI getSchemaVersionUri() { + return schemaVersion; + } + + /** + * Bean property setter: <property>$schema</property>. + * + * @param schemaVersion The new value for the <property>schemaVersion</property> property on this bean. + * @return This object (for method chaining). + */ + @BeanProperty(name="$schema") + public Schema setSchemaVersionUri(URI schemaVersion) { + this.schemaVersion = schemaVersion; + return this; + } + + /** + * Bean property setter: <property>schemaVersion</property>. + * + * @param schemaVersion The new value for the <property>schemaVersion</property> property on this bean. + * The parameter must be a valid URI. It can be <jk>null</jk>. + * @return This object (for method chaining). + */ + public Schema setSchemaVersionUri(String schemaVersion) { + return setSchemaVersionUri(schemaVersion == null ? null : URI.create(schemaVersion)); + } + + /** + * Bean property getter: <property>title</property>. + * + * @return The value of the <property>title</property> property, or <jk>null</jk> if it is not set. + */ + public String getTitle() { + return title; + } + + /** + * Bean property setter: <property>title</property>. + * + * @param title The new value for the <property>title</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setTitle(String title) { + this.title = title; + return this; + } + + /** + * Bean property getter: <property>description</property>. + * + * @return The value of the <property>description</property> property, or <jk>null</jk> if it is not set. + */ + public String getDescription() { + return description; + } + + /** + * Bean property setter: <property>description</property>. + * + * @param description The new value for the <property>description</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setDescription(String description) { + this.description = description; + return this; + } + + /** + * Bean property getter: <property>type</property>. + * + * @return The value of the <property>type</property> property on this bean, or <jk>null</jk> if it is not set. + * Can be either a {@link JsonType} or {@link JsonTypeArray} depending on what value was used to set it. + */ + @BeanProperty(filter=JsonTypeOrJsonTypeArrayFilter.class) + public Object getType() { + if (typeJsonType != null) + return typeJsonType; + return typeJsonTypeArray; + } + + /** + * Bean property getter: <property>type</property>. + * <p> + * Convenience method for returning the <property>type</property> property when it is a {@link JsonType} value. + * + * @return The currently set value, or <jk>null</jk> if the property is not set, or is set as a {@link JsonTypeArray}. + */ + @BeanIgnore + public JsonType getTypeAsJsonType() { + return typeJsonType; + } + + /** + * Bean property getter: <property>type</property>. + * <p> + * Convenience method for returning the <property>type</property> property when it is a {@link JsonTypeArray} value. + * + * @return The currently set value, or <jk>null</jk> if the property is not set, or is set as a {@link JsonType}. + */ + @BeanIgnore + public JsonTypeArray getTypeAsJsonTypeArray() { + return typeJsonTypeArray; + } + + /** + * Bean property setter: <property>type</property>. + * + * @param type The new value for the <property>type</property> property on this bean. + * This object must be of type {@link JsonType} or {@link JsonTypeArray}. + * @return This object (for method chaining). + * @throws BeanRuntimeException If invalid object type passed in. + */ + public Schema setType(Object type) { + this.typeJsonType = null; + this.typeJsonTypeArray = null; + if (type != null) { + if (type instanceof JsonType) + this.typeJsonType = (JsonType)type; + else if (type instanceof JsonTypeArray) + this.typeJsonTypeArray = (JsonTypeArray)type; + else + throw new BeanRuntimeException(SchemaProperty.class, "Invalid attribute type ''{0}'' passed in. Must be one of the following: SimpleType, SimpleTypeArray", type.getClass().getName()); + } + return this; + } + + /** + * Bean property appender: <property>type</property>. + * + * @param types The list of items to append to the <property>type</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema addTypes(JsonType...types) { + if (this.typeJsonTypeArray == null) + this.typeJsonTypeArray = new JsonTypeArray(); + this.typeJsonTypeArray.addAll(types); + return this; + } + + /** + * Used during parsing to convert the <property>type</property> property to the correct class type. + * <ul> + * <li>If parsing a JSON-array, converts to a {@link JsonTypeArray}. + * <li>If parsing a JSON-object, converts to a {@link JsonType}. + * </ul> + * Serialization method is a no-op. + */ + public static class JsonTypeOrJsonTypeArrayFilter extends PojoFilter<Object,Object> { + + @Override /* PojoFilter */ + public Object filter(Object o) throws SerializeException { + return o; + } + + @Override /* PojoFilter */ + public Object unfilter(Object o, ClassMeta<?> hint) throws ParseException { + BeanContext bc = getBeanContext(); + ClassMeta<?> cm = (o instanceof Collection ? bc.getClassMeta(JsonTypeArray.class) : bc.getClassMeta(JsonType.class)); + return bc.convertToType(o, cm); + } + } + + /** + * Bean property getter: <property>definitions</property>. + * + * @return The value of the <property>definitions</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Map<String,Schema> getDefinitions() { + return definitions; + } + + /** + * Bean property setter: <property>definitions</property>. + * + * @param definitions The new value for the <property>definitions</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setDefinitions(Map<String,Schema> definitions) { + this.definitions = definitions; + if (definitions != null) + setMasterOn(definitions.values()); + return this; + } + + /** + * Bean property appender: <property>definitions</property>. + * + * @param name The key in the definitions map entry. + * @param definition The value in the definitions map entry. + * @return This object (for method chaining). + */ + public Schema addDefinition(String name, Schema definition) { + if (this.definitions == null) + this.definitions = new LinkedHashMap<String,Schema>(); + this.definitions.put(name, definition); + setMasterOn(definition); + return this; + } + + /** + * Bean property getter: <property>properties</property>. + * + * @return The value of the <property>properties</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Map<String,Schema> getProperties() { + return properties; + } + + /** + * Returns the property with the specified name. + * This is equivalent to calling <property>getProperty(name, <jk>false</jk>)</property>. + * + * @param name The property name. + * @return The property with the specified name, or <jk>null</jk> if no property is specified. + */ + public Schema getProperty(String name) { + return getProperty(name, false); + } + + /** + * Returns the property with the specified name. + * If <property>resolve</property> is <jk>true</jk>, the property object will automatically be + * resolved by calling {@link #resolve()}. + * Therefore, <property>getProperty(name, <jk>true</jk>)</property> is equivalent to calling + * <property>getProperty(name).resolve()</property>, except it's safe from a potential <property>NullPointerException</property>. + * + * @param name The property name. + * @param resolve If <jk>true</jk>, calls {@link #resolve()} on object before returning. + * @return The property with the specified name, or <jk>null</jk> if no property is specified. + */ + public Schema getProperty(String name, boolean resolve) { + if (properties == null) + return null; + Schema s = properties.get(name); + if (s == null) + return null; + if (resolve) + s = s.resolve(); + return s; + } + + /** + * Bean property setter: <property>properties</property>. + * + * @param properties The new value for the <property>properties</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setProperties(Map<String,Schema> properties) { + this.properties = properties; + if (properties != null) + for (Map.Entry<String,Schema> e : properties.entrySet()) { + Schema value = e.getValue(); + setMasterOn(value); + value.setName(e.getKey()); + } + return this; + } + + /** + * Bean property appender: <property>properties</property>. + * <p> + * Properties must have their <property>name</property> property set on them when using this method. + * + * @param properties The list of items to append to the <property>properties</property> property on this bean. + * @return This object (for method chaining). + * @throws BeanRuntimeException If property is found without a set <property>name</property> property. + */ + public Schema addProperties(Schema...properties) { + if (this.properties == null) + this.properties = new LinkedHashMap<String,Schema>(); + for (Schema p : properties) { + if (p.getName() == null) + throw new BeanRuntimeException(Schema.class, "Invalid property passed to Schema.addProperties(). Property name was null."); + setMasterOn(p); + this.properties.put(p.getName(), p); + } + return this; + } + + /** + * Bean property getter: <property>patternProperties</property>. + * + * @return The value of the <property>patternProperties</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Map<String,Schema> getPatternProperties() { + return patternProperties; + } + + /** + * Bean property setter: <property>patternProperties</property>. + * + * @param patternProperties The new value for the <property>patternProperties</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setPatternProperties(Map<String,Schema> patternProperties) { + this.patternProperties = patternProperties; + if (patternProperties != null) + for (Map.Entry<String,Schema> e : patternProperties.entrySet()) { + Schema s = e.getValue(); + setMasterOn(s); + s.setName(e.getKey()); + } + return this; + } + + /** + * Bean property appender: <property>patternProperties</property>. + * <p> + * Properties must have their <property>name</property> property set to the pattern string when using this method. + * + * @param properties The list of items to append to the <property>patternProperties</property> property on this bean. + * @return This object (for method chaining). + * @throws BeanRuntimeException If property is found without a set <property>name</property> property. + */ + public Schema addPatternProperties(SchemaProperty...properties) { + if (this.patternProperties == null) + this.patternProperties = new LinkedHashMap<String,Schema>(); + for (Schema p : properties) { + if (p.getName() == null) + throw new BeanRuntimeException(Schema.class, "Invalid property passed to Schema.addProperties(). Property name was null."); + setMasterOn(p); + this.patternProperties.put(p.getName(), p); + } + return this; + } + + /** + * Bean property getter: <property>dependencies</property>. + * + * @return The value of the <property>dependencies</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Map<String,Schema> getDependencies() { + return dependencies; + } + + /** + * Bean property setter: <property>dependencies</property>. + * + * @param dependencies The new value for the <property>dependencies</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setDependencies(Map<String,Schema> dependencies) { + this.dependencies = dependencies; + if (dependencies != null) + setMasterOn(dependencies.values()); + return this; + } + + /** + * Bean property appender: <property>dependencies</property>. + * + * @param name The key of the entry in the dependencies map. + * @param dependency The value of the entry in the dependencies map. + * @return This object (for method chaining). + */ + public Schema addDependency(String name, Schema dependency) { + if (this.dependencies == null) + this.dependencies = new LinkedHashMap<String,Schema>(); + this.dependencies.put(name, dependency); + setMasterOn(dependency); + return this; + } + + /** + * Bean property getter: <property>items</property>. + * + * @return The value of the <property>items</property> property on this bean, or <jk>null</jk> if it is not set. + * Can be either a {@link Schema} or {@link SchemaArray} depending on what value was used to set it. + */ + @BeanProperty(filter=SchemaOrSchemaArrayFilter.class) + public Object getItems() { + if (itemsSchema != null) + return itemsSchema; + return itemsSchemaArray; + } + + /** + * Bean property getter: <property>items</property>. + * <p> + * Convenience method for returning the <property>items</property> property when it is a {@link Schema} value. + * + * @return The currently set value, or <jk>null</jk> if the property is not set, or is set as a {@link SchemaArray}. + */ + @BeanIgnore + public Schema getItemsAsSchema() { + return itemsSchema; + } + + /** + * Bean property getter: <property>items</property>. + * <p> + * Convenience method for returning the <property>items</property> property when it is a {@link SchemaArray} value. + * + * @return The currently set value, or <jk>null</jk> if the property is not set, or is set as a {@link Schema}. + */ + @BeanIgnore + public SchemaArray getItemsAsSchemaArray() { + return itemsSchemaArray; + } + + /** + * Used during parsing to convert the <property>items</property> property to the correct class type. + * <ul> + * <li>If parsing a JSON-array, converts to a {@link SchemaArray}. + * <li>If parsing a JSON-object, converts to a {@link Schema}. + * </ul> + * Serialization method is a no-op. + */ + public static class SchemaOrSchemaArrayFilter extends PojoFilter<Object,Object> { + + @Override /* PojoFilter */ + public Object filter(Object o) throws SerializeException { + return o; + } + + @Override /* PojoFilter */ + public Object unfilter(Object o, ClassMeta<?> hint) throws ParseException { + BeanContext bc = getBeanContext(); + ClassMeta<?> cm = (o instanceof Collection ? bc.getClassMeta(SchemaArray.class) : bc.getClassMeta(Schema.class)); + return bc.convertToType(o, cm); + } + } + + /** + * Bean property setter: <property>items</property>. + * + * @param items The new value for the <property>items</property> property on this bean. + * This object must be of type {@link Schema} or {@link SchemaArray}. + * @return This object (for method chaining). + * @throws BeanRuntimeException If invalid object type passed in. + */ + public Schema setItems(Object items) { + this.itemsSchema = null; + this.itemsSchemaArray = null; + if (items != null) { + if (items instanceof Schema) { + this.itemsSchema = (Schema)items; + setMasterOn(this.itemsSchema); + } else if (items instanceof SchemaArray) { + this.itemsSchemaArray = (SchemaArray)items; + setMasterOn(this.itemsSchemaArray); + } else + throw new BeanRuntimeException(SchemaProperty.class, "Invalid attribute type ''{0}'' passed in. Must be one of the following: Schema, SchemaArray", items.getClass().getName()); + } + return this; + } + + /** + * Bean property appender: <property>items</property>. + * + * @param items The list of items to append to the <property>items</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema addItems(Schema...items) { + if (this.itemsSchemaArray == null) + this.itemsSchemaArray = new SchemaArray(); + this.itemsSchemaArray.addAll(items); + setMasterOn(items); + return this; + } + + /** + * Bean property getter: <property>multipleOf</property>. + * + * @return The value of the <property>multipleOf</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Number getMultipleOf() { + return multipleOf; + } + + /** + * Bean property setter: <property>multipleOf</property>. + * + * @param multipleOf The new value for the <property>multipleOf</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setMultipleOf(Number multipleOf) { + this.multipleOf = multipleOf; + return this; + } + + /** + * Bean property getter: <property>maximum</property>. + * + * @return The value of the <property>maximum</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Number getMaximum() { + return maximum; + } + + /** + * Bean property setter: <property>maximum</property>. + * + * @param maximum The new value for the <property>maximum</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setMaximum(Number maximum) { + this.maximum = maximum; + return this; + } + + /** + * Bean property getter: <property>exclusiveMaximum</property>. + * + * @return The value of the <property>exclusiveMaximum</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Boolean isExclusiveMaximum() { + return exclusiveMaximum; + } + + /** + * Bean property setter: <property>exclusiveMaximum</property>. + * + * @param exclusiveMaximum The new value for the <property>exclusiveMaximum</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setExclusiveMaximum(Boolean exclusiveMaximum) { + this.exclusiveMaximum = exclusiveMaximum; + return this; + } + + /** + * Bean property getter: <property>minimum</property>. + * + * @return The value of the <property>minimum</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Number getMinimum() { + return minimum; + } + + /** + * Bean property setter: <property>minimum</property>. + * + * @param minimum The new value for the <property>minimum</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setMinimum(Number minimum) { + this.minimum = minimum; + return this; + } + + /** + * Bean property getter: <property>exclusiveMinimum</property>. + * + * @return The value of the <property>exclusiveMinimum</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Boolean isExclusiveMinimum() { + return exclusiveMinimum; + } + + /** + * Bean property setter: <property>exclusiveMinimum</property>. + * + * @param exclusiveMinimum The new value for the <property>exclusiveMinimum</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setExclusiveMinimum(Boolean exclusiveMinimum) { + this.exclusiveMinimum = exclusiveMinimum; + return this; + } + + /** + * Bean property getter: <property>maxLength</property>. + * + * @return The value of the <property>maxLength</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Integer getMaxLength() { + return maxLength; + } + + /** + * Bean property setter: <property>maxLength</property>. + * + * @param maxLength The new value for the <property>maxLength</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setMaxLength(Integer maxLength) { + this.maxLength = maxLength; + return this; + } + + /** + * Bean property getter: <property>minLength</property>. + * + * @return The value of the <property>minLength</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Integer getMinLength() { + return minLength; + } + + /** + * Bean property setter: <property>minLength</property>. + * + * @param minLength The new value for the <property>minLength</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setMinLength(Integer minLength) { + this.minLength = minLength; + return this; + } + + /** + * Bean property getter: <property>pattern</property>. + * + * @return The value of the <property>pattern</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public String getPattern() { + return pattern; + } + + /** + * Bean property setter: <property>pattern</property>. + * + * @param pattern The new value for the <property>pattern</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setPattern(String pattern) { + this.pattern = pattern; + return this; + } + + /** + * Bean property getter: <property>additionalItems</property>. + * + * @return The value of the <property>additionalItems</property> property on this bean, or <jk>null</jk> if it is not set. + * Can be either a {@link Boolean} or {@link SchemaArray} depending on what value was used to set it. + */ + @BeanProperty(filter=BooleanOrSchemaArrayFilter.class) + public Object getAdditionalItems() { + if (additionalItemsBoolean != null) + return additionalItemsBoolean; + return additionalItemsSchemaArray; + } + + /** + * Bean property getter: <property>additionalItems</property>. + * <p> + * Convenience method for returning the <property>additionalItems</property> property when it is a {@link Boolean} value. + * + * @return The currently set value, or <jk>null</jk> if the property is not set, or is set as a {@link SchemaArray}. + */ + @BeanIgnore + public Boolean getAdditionalItemsAsBoolean() { + return additionalItemsBoolean; + } + + /** + * Bean property getter: <property>additionalItems</property>. + * <p> + * Convenience method for returning the <property>additionalItems</property> property when it is a {@link SchemaArray} value. + * + * @return The currently set value, or <jk>null</jk> if the property is not set, or is set as a {@link Boolean}. + */ + @BeanIgnore + public List<Schema> getAdditionalItemsAsSchemaArray() { + return additionalItemsSchemaArray; + } + + /** + * Bean property setter: <property>additionalItems</property>. + * + * @param additionalItems The new value for the <property>additionalItems</property> property on this bean. + * This object must be of type {@link Boolean} or {@link SchemaArray}. + * @return This object (for method chaining). + * @throws BeanRuntimeException If invalid object type passed in. + */ + public Schema setAdditionalItems(Object additionalItems) { + this.additionalItemsBoolean = null; + this.additionalItemsSchemaArray = null; + if (additionalItems != null) { + if (additionalItems instanceof Boolean) + this.additionalItemsBoolean = (Boolean)additionalItems; + else if (additionalItems instanceof SchemaArray) { + this.additionalItemsSchemaArray = (SchemaArray)additionalItems; + setMasterOn(this.additionalItemsSchemaArray); + } else + throw new BeanRuntimeException(SchemaProperty.class, "Invalid attribute type ''{0}'' passed in. Must be one of the following: Boolean, SchemaArray", additionalItems.getClass().getName()); + } + return this; + } + + /** + * Bean property appender: <property>additionalItems</property>. + * + * @param additionalItems The list of items to append to the <property>additionalItems</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema addAdditionalItems(Schema...additionalItems) { + if (this.additionalItemsSchemaArray == null) + this.additionalItemsSchemaArray = new SchemaArray(); + this.additionalItemsSchemaArray.addAll(additionalItems); + setMasterOn(additionalItems); + return this; + } + + /** + * Used during parsing to convert the <property>additionalItems</property> property to the correct class type. + * <ul> + * <li>If parsing a JSON-array, converts to a {@link SchemaArray}. + * <li>If parsing a JSON-boolean, converts to a {@link Boolean}. + * </ul> + * Serialization method is a no-op. + */ + public static class BooleanOrSchemaArrayFilter extends PojoFilter<Object,Object> { + + @Override /* PojoFilter */ + public Object filter(Object o) throws SerializeException { + return o; + } + + @Override /* PojoFilter */ + public Object unfilter(Object o, ClassMeta<?> hint) throws ParseException { + BeanContext bc = getBeanContext(); + ClassMeta<?> cm = (o instanceof Collection ? bc.getClassMeta(SchemaArray.class) : bc.getClassMeta(Boolean.class)); + return bc.convertToType(o, cm); + } + } + + /** + * Bean property getter: <property>maxItems</property>. + * + * @return The value of the <property>maxItems</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Integer getMaxItems() { + return maxItems; + } + + /** + * Bean property setter: <property>maxItems</property>. + * + * @param maxItems The new value for the <property>maxItems</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setMaxItems(Integer maxItems) { + this.maxItems = maxItems; + return this; + } + + /** + * Bean property getter: <property>minItems</property>. + * + * @return The value of the <property>minItems</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Integer getMinItems() { + return minItems; + } + + /** + * Bean property setter: <property>minItems</property>. + * + * @param minItems The new value for the <property>minItems</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setMinItems(Integer minItems) { + this.minItems = minItems; + return this; + } + + /** + * Bean property getter: <property>uniqueItems</property>. + * + * @return The value of the <property>uniqueItems</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Boolean getUniqueItems() { + return uniqueItems; + } + + /** + * Bean property setter: <property>uniqueItems</property>. + * + * @param uniqueItems The new value for the <property>uniqueItems</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setUniqueItems(Boolean uniqueItems) { + this.uniqueItems = uniqueItems; + return this; + } + + /** + * Bean property getter: <property>maxProperties</property>. + * + * @return The value of the <property>maxProperties</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Integer getMaxProperties() { + return maxProperties; + } + + /** + * Bean property setter: <property>maxProperties</property>. + * + * @param maxProperties The new value for the <property>maxProperties</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setMaxProperties(Integer maxProperties) { + this.maxProperties = maxProperties; + return this; + } + + /** + * Bean property getter: <property>minProperties</property>. + * + * @return The value of the <property>minProperties</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Integer getMinProperties() { + return minProperties; + } + + /** + * Bean property setter: <property>minProperties</property>. + * + * @param minProperties The new value for the <property>minProperties</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setMinProperties(Integer minProperties) { + this.minProperties = minProperties; + return this; + } + + /** + * Bean property getter: <property>required</property>. + * + * @return The value of the <property>required</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public List<String> getRequired() { + return required; + } + + /** + * Bean property setter: <property>required</property>. + * + * @param required The new value for the <property>required</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setRequired(List<String> required) { + this.required = required; + return this; + } + + /** + * Bean property appender: <property>required</property>. + * + * @param required The list of items to append to the <property>required</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema addRequired(List<String> required) { + if (this.required == null) + this.required = new LinkedList<String>(); + for (String r : required) + this.required.add(r); + return this; + } + + /** + * Bean property appender: <property>required</property>. + * + * @param required The list of items to append to the <property>required</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema addRequired(String...required) { + if (this.required == null) + this.required = new LinkedList<String>(); + for (String r : required) + this.required.add(r); + return this; + } + + /** + * Bean property appender: <property>required</property>. + * + * @param properties The list of items to append to the <property>required</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema addRequired(SchemaProperty...properties) { + if (this.required == null) + this.required = new LinkedList<String>(); + for (SchemaProperty p : properties) + this.required.add(p.getName()); + return this; + } + + /** + * Bean property getter: <property>additionalProperties</property>. + * + * @return The value of the <property>additionalProperties</property> property on this bean, or <jk>null</jk> if it is not set. + * Can be either a {@link Boolean} or {@link SchemaArray} depending on what value was used to set it. + */ + @BeanProperty(filter=BooleanOrSchemaFilter.class) + public Object getAdditionalProperties() { + if (additionalPropertiesBoolean != null) + return additionalItemsBoolean; + return additionalPropertiesSchema; + } + + /** + * Bean property getter: <property>additionalProperties</property>. + * <p> + * Convenience method for returning the <property>additionalProperties</property> property when it is a {@link Boolean} value. + * + * @return The currently set value, or <jk>null</jk> if the property is not set, or is set as a {@link Schema}. + */ + @BeanIgnore + public Boolean getAdditionalPropertiesAsBoolean() { + return additionalPropertiesBoolean; + } + + /** + * Bean property getter: <property>additionalProperties</property>. + * <p> + * Convenience method for returning the <property>additionalProperties</property> property when it is a {@link Schema} value. + * + * @return The currently set value, or <jk>null</jk> if the property is not set, or is set as a {@link Boolean}. + */ + @BeanIgnore + public Schema getAdditionalPropertiesAsSchema() { + return additionalPropertiesSchema; + } + + /** + * Bean property setter: <property>additionalProperties</property>. + * + * @param additionalProperties The new value for the <property>additionalProperties</property> property on this bean. + * This object must be of type {@link Boolean} or {@link Schema}. + * @return This object (for method chaining). + * @throws BeanRuntimeException If invalid object type passed in. + */ + public Schema setAdditionalProperties(Object additionalProperties) { + this.additionalPropertiesBoolean = null; + this.additionalPropertiesSchema = null; + if (additionalProperties != null) { + if (additionalProperties instanceof Boolean) + this.additionalPropertiesBoolean = (Boolean)additionalProperties; + else if (additionalProperties instanceof Schema) { + this.additionalPropertiesSchema = (Schema)additionalProperties; + setMasterOn(this.additionalPropertiesSchema); + } else + throw new BeanRuntimeException(SchemaProperty.class, "Invalid attribute type ''{0}'' passed in. Must be one of the following: Boolean, Schema", additionalProperties.getClass().getName()); + } + return this; + } + + /** + * Used during parsing to convert the <property>additionalProperties</property> property to the correct class type. + * <ul> + * <li>If parsing a JSON-object, converts to a {@link Schema}. + * <li>If parsing a JSON-boolean, converts to a {@link Boolean}. + * </ul> + * Serialization method is a no-op. + */ + public static class BooleanOrSchemaFilter extends PojoFilter<Object,Object> { + + @Override /* PojoFilter */ + public Object filter(Object o) throws SerializeException { + return o; + } + + @Override /* PojoFilter */ + public Object unfilter(Object o, ClassMeta<?> hint) throws ParseException { + BeanContext bc = getBeanContext(); + ClassMeta<?> cm = (o instanceof Boolean ? bc.getClassMeta(Boolean.class) : bc.getClassMeta(Schema.class)); + return bc.convertToType(o, cm); + } + } + + /** + * Bean property getter: <property>enum</property>. + * + * @return The value of the <property>enum</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public List<String> getEnum() { + return _enum; + } + + /** + * Bean property setter: <property>enum</property>. + * + * @param _enum The new value for the <property>enum</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setEnum(List<String> _enum) { + this._enum = _enum; + return this; + } + + /** + * Bean property appender: <property>enum</property>. + * + * @param _enum The list of items to append to the <property>enum</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema addEnum(String..._enum) { + if (this._enum == null) + this._enum = new LinkedList<String>(); + for (String e : _enum) + this._enum.add(e); + return this; + } + + /** + * Bean property getter: <property>allOf</property>. + * + * @return The value of the <property>allOf</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public List<Schema> getAllOf() { + return allOf; + } + + /** + * Bean property setter: <property>allOf</property>. + * + * @param allOf The new value for the <property>allOf</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setAllOf(List<Schema> allOf) { + this.allOf = allOf; + setMasterOn(allOf); + return this; + } + + /** + * Bean property appender: <property>allOf</property>. + * + * @param allOf The list of items to append to the <property>allOf</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema addAllOf(Schema...allOf) { + if (this.allOf == null) + this.allOf = new LinkedList<Schema>(); + setMasterOn(allOf); + for (Schema s : allOf) + this.allOf.add(s); + return this; + } + + /** + * Bean property getter: <property>anyOf</property>. + * + * @return The value of the <property>anyOf</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public List<Schema> getAnyOf() { + return anyOf; + } + + /** + * Bean property setter: <property>anyOf</property>. + * + * @param anyOf The new value of the <property>anyOf</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setAnyOf(List<Schema> anyOf) { + this.anyOf = anyOf; + setMasterOn(anyOf); + return this; + } + + /** + * Bean property appender: <property>anyOf</property>. + * + * @param anyOf The list of items to append to the <property>anyOf</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema addAnyOf(Schema...anyOf) { + if (this.anyOf == null) + this.anyOf = new LinkedList<Schema>(); + setMasterOn(anyOf); + for (Schema s : anyOf) + this.anyOf.add(s); + return this; + } + + /** + * Bean property getter: <property>oneOf</property>. + * + * @return The value of the <property>oneOf</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public List<Schema> getOneOf() { + return oneOf; + } + + /** + * Bean property setter: <property>oneOf</property>. + * + * @param oneOf The new value for the <property>oneOf</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setOneOf(List<Schema> oneOf) { + this.oneOf = oneOf; + setMasterOn(oneOf); + return this; + } + + /** + * Bean property appender: <property>oneOf</property>. + * + * @param oneOf The list of items to append to the <property>oneOf</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema addOneOf(Schema...oneOf) { + if (this.oneOf == null) + this.oneOf = new LinkedList<Schema>(); + setMasterOn(oneOf); + for (Schema s : oneOf) + this.oneOf.add(s); + return this; + } + + /** + * Bean property getter: <property>not</property>. + * + * @return The value of the <property>not</property> property on this bean, or <jk>null</jk> if it is not set. + */ + public Schema getNot() { + return not; + } + + /** + * Bean property setter: <property>not</property>. + * + * @param not The new value for the <property>not</property> property on this bean. + * @return This object (for method chaining). + */ + public Schema setNot(Schema not) { + this.not = not; + setMasterOn(not); + return this; + } + + /** + * Bean property getter: <property>$ref</property>. + * + * @return The value of the <property>$ref</property> property on this bean, or <jk>null</jk> if it is not set. + */ + @BeanProperty(name="$ref") + public URI getRef() { + return ref; + } + + /** + * Bean property setter: <property>$ref</property>. + * + * @param ref The new value for the <property>$ref</property> property on this bean. + * @return This object (for method chaining). + */ + @BeanProperty(name="$ref") + public Schema setRef(URI ref) { + this.ref = ref; + return this; + } + + private void setMasterOn(Schema s) { + if (s != null) + s.setMaster(this); + } + + private void setMasterOn(Schema[] ss) { + if (ss != null) + for (Schema s : ss) + setMasterOn(s); + } + + private void setMasterOn(Collection<Schema> ss) { + if (ss != null) + for (Schema s : ss) + setMasterOn(s); + } + + private void setMasterOn(SchemaArray ss) { + if (ss != null) + for (Schema s : ss) + setMasterOn(s); + } + + /** + * Sets the master schema for this schema and all child schema objects. + * <p> + * All child elements in a schema should point to a single "master" schema in order to + * locate registered SchemaMap objects for resolving external schemas. + * + * @param master The master schema to associate on this and all children. Can be <jk>null</jk>. + */ + protected void setMaster(Schema master) { + this.master = master; + if (definitions != null) + for (Schema s : definitions.values()) + s.setMaster(master); + if (properties != null) + for (Schema s : properties.values()) + s.setMaster(master); + if (patternProperties != null) + for (Schema s : patternProperties.values()) + s.setMaster(master); + if (dependencies != null) + for (Schema s : dependencies.values()) + s.setMaster(master); + if (itemsSchema != null) + itemsSchema.setMaster(master); + if (itemsSchemaArray != null) + for (Schema s : itemsSchemaArray) + s.setMaster(master); + if (additionalItemsSchemaArray != null) + for (Schema s : additionalItemsSchemaArray) + s.setMaster(master); + if (additionalPropertiesSchema != null) + additionalPropertiesSchema.setMaster(master); + if (allOf != null) + for (Schema s : allOf) + s.setMaster(master); + if (anyOf != null) + for (Schema s : anyOf) + s.setMaster(master); + if (oneOf != null) + for (Schema s : oneOf) + s.setMaster(master); + if (not != null) + not.setMaster(master); + } + + + /** + * Bean property setter: <property>$ref</property>. + * + * @param ref The new value for the <property>$ref</property> property on this bean. + * The parameter must be a valid URI. It can be <jk>null</jk>. + * @return This object (for method chaining). + */ + public Schema setRef(String ref) { + return setRef(ref == null ? null : URI.create(ref)); + } + + /** + * If this schema is a reference to another schema (i.e. has its <property>$ref</property> property set), + * this method will retrieve the referenced schema from the schema map registered with this schema. + * If this schema is not a reference, or no schema map is registered with this schema, this method + * is a no-op and simply returns this object. + * + * @return The referenced schema, or <jk>null</jk>. + */ + public Schema resolve() { + if (ref == null || master.schemaMap == null) + return this; + return master.schemaMap.get(ref); + } + + /** + * Associates a schema map with this schema for resolving other schemas identified + * through <property>$ref</property> properties. + * + * @param schemaMap The schema map to associate with this schema. Can be <jk>null</jk>. + * @return This object (for method chaining). + */ + public Schema setSchemaMap(SchemaMap schemaMap) { + this.schemaMap = schemaMap; + return this; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaArray.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaArray.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaArray.class new file mode 100755 index 0000000..3ca3475 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaArray.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaArray.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaArray.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaArray.java new file mode 100755 index 0000000..a789d1b --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaArray.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.dto.jsonschema; + +import java.util.*; + +/** + * Represents a list of {@link Schema} objects. + * <p> + * Refer to {@link com.ibm.juno.core.dto.jsonschema} for usage information. + * + * @author James Bognar ([email protected]) + */ +public class SchemaArray extends LinkedList<Schema> { + + private static final long serialVersionUID = 1L; + + /** + * Default constructor. + */ + public SchemaArray() {} + + /** + * Constructor with predefined types to add to this list. + * + * @param schemas The list of schemas in this array. + */ + public SchemaArray(Schema...schemas) { + addAll(schemas); + } + + /** + * Convenience method for adding one or more {@link Schema} objects to + * this array. + * + * @param schemas The {@link Schema} objects to add to this array. + * @return This object (for method chaining). + */ + public SchemaArray addAll(Schema...schemas) { + for (Schema s : schemas) + add(s); + return this; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaMap.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaMap.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaMap.class new file mode 100755 index 0000000..6af7a4e Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaMap.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaMap.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaMap.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaMap.java new file mode 100755 index 0000000..225cd2f --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaMap.java @@ -0,0 +1,119 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.dto.jsonschema; + +import java.io.*; +import java.net.*; +import java.util.concurrent.*; + +import com.ibm.juno.core.json.*; + +/** + * A container for retrieving JSON {@link Schema} objects by URI. + * <p> + * Subclasses must implement one of the following methods to load schemas from external sources: + * <ul> + * <li>{@link #getReader(URI)} - If schemas should be loaded from readers and automatically parsed. + * <li>{@link #load(URI)} - If you want control over construction of {@link Schema} objects. + * </ul> + * + * @author James Bognar ([email protected]) + */ +public abstract class SchemaMap extends ConcurrentHashMap<URI,Schema> { + + private static final long serialVersionUID = 1L; + + @Override /* Map */ + public Schema get(Object uri) { + if (uri == null) + return null; + return get(URI.create(uri.toString())); + } + + /** + * Return the {@link Schema} object at the specified URI. + * If this schema object has not been loaded yet, calls {@link #load(URI)}. + * + * @param uri The URI of the schema to retrieve. + * @return The Schema, or <jk>null</jk> if schema was not located and could not be loaded. + */ + public Schema get(URI uri) { + Schema s = super.get(uri); + if (s != null) + return s; + synchronized(this) { + s = load(uri); + if (s != null) { + // Note: Can't use add(Schema...) since the ID property may not be set. + s.setSchemaMap(this); + put(uri, s); + } + return s; + } + } + + /** + * Convenience method for prepopulating this map with the specified schemas. + * <p> + * The schemas passed in through this method MUST have their ID properties set. + * + * @param schemas The set of schemas to add to this map. + * @return This object (for method chaining). + * @throws RuntimeException If one or more schema objects did not have their ID property set. + */ + public SchemaMap add(Schema...schemas) { + for (Schema schema : schemas) { + if (schema.getId() == null) + throw new RuntimeException("Schema with no ID passed to SchemaMap.add(Schema...)"); + put(schema.getId(), schema); + schema.setSchemaMap(this); + } + return this; + } + + /** + * Subclasses must implement either this method or {@link #getReader(URI)} to load the schema with the specified URI. + * It's up to the implementer to decide where these come from. + * <p> + * The default implementation calls {@link #getReader(URI)} and parses the schema document. + * If {@link #getReader(URI)} returns <jk>null</jk>, this method returns <jk>null</jk> indicating this is an unreachable document. + * + * @param uri The URI to load the schema from. + * @return The parsed schema. + */ + public Schema load(URI uri) { + Reader r = getReader(uri); + if (r == null) + return null; + try { + return JsonParser.DEFAULT.parse(r, -1, Schema.class); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + try { + r.close(); + } catch (IOException e) { + // Ignore + } + } + } + + /** + * Subclasses must implement either this method or {@link #load(URI)} to load the schema with the specified URI. + * It's up to the implementer to decide where these come from. + * <p> + * The default implementation returns <jk>null</jk>. + * + * @param uri The URI to connect to and retrieve the contents. + * @return The reader from reading the specified URI. + */ + public Reader getReader(URI uri) { + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaProperty.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaProperty.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaProperty.class new file mode 100755 index 0000000..6392fc1 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/dto/jsonschema/SchemaProperty.class differ
