001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * tdelprat 018 */ 019package org.nuxeo.ecm.restapi.jaxrs.io.types; 020 021import java.io.IOException; 022import java.io.OutputStream; 023 024import org.codehaus.jackson.JsonEncoding; 025import org.codehaus.jackson.JsonFactory; 026import org.codehaus.jackson.JsonGenerator; 027import org.nuxeo.ecm.core.schema.DocumentType; 028import org.nuxeo.ecm.core.schema.types.ComplexType; 029import org.nuxeo.ecm.core.schema.types.CompositeType; 030import org.nuxeo.ecm.core.schema.types.Field; 031import org.nuxeo.ecm.core.schema.types.ListType; 032import org.nuxeo.ecm.core.schema.types.Schema; 033import org.nuxeo.ecm.core.schema.types.SimpleType; 034import org.nuxeo.ecm.core.schema.types.Type; 035 036public class AbstractTypeDefWriter { 037 038 public AbstractTypeDefWriter() { 039 super(); 040 } 041 042 protected JsonGenerator getGenerator(OutputStream entityStream) throws IOException { 043 JsonFactory factory = new JsonFactory(); 044 JsonGenerator jg = factory.createJsonGenerator(entityStream, JsonEncoding.UTF8); 045 jg.useDefaultPrettyPrinter(); 046 return jg; 047 } 048 049 protected void writeSchema(JsonGenerator jg, Schema schema) throws IOException { 050 jg.writeObjectFieldStart(schema.getName()); 051 jg.writeStringField("@prefix", schema.getNamespace().prefix); 052 for (Field field : schema.getFields()) { 053 writeField(jg, field); 054 } 055 jg.writeEndObject(); 056 } 057 058 protected void writeSchemaObject(JsonGenerator jg, Schema schema) throws IOException { 059 jg.writeStartObject(); 060 jg.writeStringField("name", schema.getName()); 061 jg.writeStringField("@prefix", schema.getNamespace().prefix); 062 jg.writeObjectFieldStart("fields"); 063 for (Field field : schema.getFields()) { 064 writeField(jg, field); 065 } 066 jg.writeEndObject(); 067 jg.writeEndObject(); 068 } 069 070 protected void writeDocType(JsonGenerator jg, DocumentType docType, boolean expandSchemas) throws IOException { 071 072 if (docType.getSuperType() != null) { 073 jg.writeStringField("parent", docType.getSuperType().getName()); 074 } else { 075 jg.writeStringField("parent", "None!!!"); 076 } 077 078 jg.writeArrayFieldStart("facets"); 079 for (String facet : docType.getFacets()) { 080 jg.writeString(facet); 081 } 082 jg.writeEndArray(); 083 084 jg.writeArrayFieldStart("schemas"); 085 if (expandSchemas) { 086 for (Schema schema : docType.getSchemas()) { 087 writeSchemaObject(jg, schema); 088 } 089 } else { 090 for (String schema : docType.getSchemaNames()) { 091 jg.writeString(schema); 092 } 093 } 094 jg.writeEndArray(); 095 096 } 097 098 protected void writeField(JsonGenerator jg, Field field) throws IOException { 099 Type type = field.getType(); 100 if (!type.isComplexType()) { 101 if (type.isListType()) { 102 ListType lt = (ListType) type; 103 if (lt.getFieldType().isComplexType()) { 104 if (lt.getFieldType().getName().equals("content")) { 105 jg.writeStringField(field.getName().getLocalName(), "blob[]"); 106 107 } else { 108 jg.writeObjectFieldStart(field.getName().getLocalName()); 109 buildComplexFields(jg, lt.getField()); 110 jg.writeStringField("type", "complex[]"); 111 jg.writeEndObject(); 112 } 113 } else { 114 Type fieldType = lt.getFieldType(); 115 if (fieldType instanceof SimpleType) { 116 SimpleType stype = (SimpleType) fieldType; 117 fieldType = stype.getPrimitiveType(); 118 } 119 jg.writeStringField(field.getName().getLocalName(), fieldType.getName() + "[]"); 120 } 121 } else { 122 if (type instanceof SimpleType) { 123 SimpleType stype = (SimpleType) type; 124 type = stype.getPrimitiveType(); 125 } 126 jg.writeStringField(field.getName().getLocalName(), type.getName()); 127 } 128 } else { 129 if (type.getName().equals("content")) { 130 jg.writeStringField(field.getName().getLocalName(), "blob"); 131 } else { 132 133 jg.writeObjectFieldStart(field.getName().getLocalName()); 134 buildComplexFields(jg, field); 135 jg.writeStringField("type", "complex"); 136 jg.writeEndObject(); 137 } 138 } 139 140 } 141 142 protected void buildComplexFields(JsonGenerator jg, Field field) throws IOException { 143 ComplexType cplXType = (ComplexType) field.getType(); 144 jg.writeObjectFieldStart("fields"); 145 for (Field subField : cplXType.getFields()) { 146 writeField(jg, subField); 147 } 148 jg.writeEndObject(); 149 } 150 151 protected void writeFacet(JsonGenerator jg, CompositeType facet, boolean expandSchemas) throws IOException { 152 153 jg.writeStringField("name", facet.getName()); 154 if (facet.getSchemaNames() != null && facet.getSchemaNames().length > 0) { 155 jg.writeArrayFieldStart("schemas"); 156 if (expandSchemas) { 157 for (Schema schema : facet.getSchemas()) { 158 writeSchemaObject(jg, schema); 159 } 160 } else { 161 for (String schemaName : facet.getSchemaNames()) { 162 jg.writeString(schemaName); 163 } 164 } 165 jg.writeEndArray(); 166 } 167 168 } 169}