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