001/* 002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * bstefanescu 016 */ 017package org.nuxeo.ecm.automation.client.jaxrs.util; 018 019import java.io.IOException; 020import java.util.ArrayList; 021 022import org.codehaus.jackson.JsonParser; 023import org.codehaus.jackson.JsonToken; 024import org.nuxeo.ecm.automation.client.model.OperationDocumentation; 025import org.nuxeo.ecm.automation.client.model.OperationDocumentation.Param; 026 027/** 028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 029 */ 030public class JsonOperationMarshaller { 031 032 public static OperationDocumentation read(JsonParser jp) throws IOException { 033 OperationDocumentation op = new OperationDocumentation(); 034 JsonToken tok = jp.nextToken(); // skip { 035 while (tok != null && tok != JsonToken.END_OBJECT) { 036 String key = jp.getCurrentName(); 037 jp.nextToken(); 038 if ("id".equals(key)) { 039 op.id = jp.getText(); 040 } else if ("aliases".equals(key)) { 041 op.aliases = readStringArray(jp); 042 } else if ("label".equals(key)) { 043 op.label = jp.getText(); 044 } else if ("category".equals(key)) { 045 op.category = jp.getText(); 046 } else if ("requires".equals(key)) { 047 op.requires = jp.getText(); 048 } else if ("description".equals(key)) { 049 op.description = jp.getText(); 050 } else if ("url".equals(key)) { 051 op.url = jp.getText(); 052 } else if ("since".equals(key)) { 053 op.since = jp.getText(); 054 } else if ("signature".equals(key)) { 055 op.signature = readStringArray(jp); 056 } else if ("params".equals(key)) { 057 readParams(jp, op); 058 } else { 059 jp.skipChildren(); 060 } 061 tok = jp.nextToken(); 062 } 063 if (tok == null) { 064 throw new IllegalArgumentException("Unexpected end of stream."); 065 } 066 return op; 067 } 068 069 public static String[] readStringArray(JsonParser jp) throws IOException { 070 JsonToken tok = jp.nextToken(); // skip [ 071 if (tok == JsonToken.END_ARRAY) { 072 return null; 073 } 074 ArrayList<String> list = new ArrayList<String>(); 075 do { 076 list.add(jp.getText()); 077 tok = jp.nextToken(); 078 } while (tok != JsonToken.END_ARRAY); 079 return list.toArray(new String[list.size()]); 080 } 081 082 private static void readParams(JsonParser jp, OperationDocumentation op) throws IOException { 083 JsonToken tok = jp.nextToken(); // skip [ 084 if (tok == JsonToken.END_ARRAY) { 085 return; 086 } 087 do { 088 readParam(jp, op); 089 tok = jp.nextToken(); 090 } while (tok != JsonToken.END_ARRAY); 091 } 092 093 private static void readParam(JsonParser jp, OperationDocumentation op) throws IOException { 094 Param para = new Param(); 095 JsonToken tok = jp.nextToken(); // skip { 096 while (tok != null && tok != JsonToken.END_OBJECT) { 097 String key = jp.getCurrentName(); 098 jp.nextToken(); 099 if ("name".equals(key)) { 100 para.name = jp.getText(); 101 } else if ("type".equals(key)) { 102 para.type = jp.getText(); 103 } else if ("description".equals(key)) { 104 para.description = jp.getText(); 105 } else if ("required".equals(key)) { 106 para.isRequired = jp.getBooleanValue(); 107 } else if ("widget".equals(key)) { 108 para.widget = jp.getText(); 109 } else if ("values".equals(key)) { 110 para.values = readStringArray(jp); 111 } 112 tok = jp.nextToken(); 113 } 114 op.params.add(para); 115 } 116 117 public static void write(JsonParser jp, OperationDocumentation op) { 118 119 } 120 121}