001/* 002 * (C) Copyright 2006-2010 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 * bstefanescu 018 */ 019package org.nuxeo.ecm.automation.client.jaxrs.util; 020 021import java.io.IOException; 022import java.util.ArrayList; 023 024import org.nuxeo.ecm.automation.client.model.OperationDocumentation; 025import org.nuxeo.ecm.automation.client.model.OperationDocumentation.Param; 026 027import com.fasterxml.jackson.core.JsonParser; 028import com.fasterxml.jackson.core.JsonToken; 029 030/** 031 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 032 */ 033public class JsonOperationMarshaller { 034 035 public static OperationDocumentation read(JsonParser jp) throws IOException { 036 OperationDocumentation op = new OperationDocumentation(); 037 JsonToken tok = jp.nextToken(); // skip { 038 while (tok != null && tok != JsonToken.END_OBJECT) { 039 String key = jp.getCurrentName(); 040 jp.nextToken(); 041 if ("id".equals(key)) { 042 op.id = jp.getText(); 043 } else if ("aliases".equals(key)) { 044 op.aliases = readStringArray(jp); 045 } else if ("label".equals(key)) { 046 op.label = jp.getText(); 047 } else if ("category".equals(key)) { 048 op.category = jp.getText(); 049 } else if ("requires".equals(key)) { 050 op.requires = jp.getText(); 051 } else if ("description".equals(key)) { 052 op.description = jp.getText(); 053 } else if ("url".equals(key)) { 054 op.url = jp.getText(); 055 } else if ("since".equals(key)) { 056 op.since = jp.getText(); 057 } else if ("signature".equals(key)) { 058 op.signature = readStringArray(jp); 059 } else if ("params".equals(key)) { 060 readParams(jp, op); 061 } else { 062 jp.skipChildren(); 063 } 064 tok = jp.nextToken(); 065 } 066 if (tok == null) { 067 throw new IllegalArgumentException("Unexpected end of stream."); 068 } 069 return op; 070 } 071 072 public static String[] readStringArray(JsonParser jp) throws IOException { 073 JsonToken tok = jp.nextToken(); // skip [ 074 if (tok == JsonToken.END_ARRAY) { 075 return null; 076 } 077 ArrayList<String> list = new ArrayList<String>(); 078 do { 079 list.add(jp.getText()); 080 tok = jp.nextToken(); 081 } while (tok != JsonToken.END_ARRAY); 082 return list.toArray(new String[list.size()]); 083 } 084 085 private static void readParams(JsonParser jp, OperationDocumentation op) throws IOException { 086 JsonToken tok = jp.nextToken(); // skip [ 087 if (tok == JsonToken.END_ARRAY) { 088 return; 089 } 090 do { 091 readParam(jp, op); 092 tok = jp.nextToken(); 093 } while (tok != JsonToken.END_ARRAY); 094 } 095 096 private static void readParam(JsonParser jp, OperationDocumentation op) throws IOException { 097 Param para = new Param(); 098 JsonToken tok = jp.nextToken(); // skip { 099 while (tok != null && tok != JsonToken.END_OBJECT) { 100 String key = jp.getCurrentName(); 101 jp.nextToken(); 102 if ("name".equals(key)) { 103 para.name = jp.getText(); 104 } else if ("type".equals(key)) { 105 para.type = jp.getText(); 106 } else if ("description".equals(key)) { 107 para.description = jp.getText(); 108 } else if ("required".equals(key)) { 109 para.isRequired = jp.getBooleanValue(); 110 } else if ("widget".equals(key)) { 111 para.widget = jp.getText(); 112 } else if ("values".equals(key)) { 113 para.values = readStringArray(jp); 114 } 115 tok = jp.nextToken(); 116 } 117 op.params.add(para); 118 } 119 120 public static void write(JsonParser jp, OperationDocumentation op) { 121 122 } 123 124}