Richard S. Hall | af656a0 | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.osgi.service.command; |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * A converter is a service that can help create specific object types from a |
| 24 | * string, and vice versa. |
| 25 | * |
| 26 | * The shell is capable of coercing arguments to the their proper type. However, |
| 27 | * sometimes commands require extra help to do this conversion. This service can |
| 28 | * implement a converter for a number of types. |
| 29 | * |
| 30 | * The command shell will rank these services in order of service.ranking and |
| 31 | * will then call them until one of the converters succeeds. |
| 32 | * |
| 33 | */ |
| 34 | public interface Converter { |
| 35 | /** |
| 36 | * This property is a string, or array of strings, and defines the classes |
| 37 | * or interfaces that this converter recognizes. Recognized classes can be |
| 38 | * converted from a string to a class and they can be printed in 3 different |
| 39 | * modes. |
| 40 | */ |
| 41 | String CONVERTER_CLASSES = "osgi.converter.classes"; |
| 42 | |
| 43 | /** |
| 44 | * Print the object in detail. This can contain multiple lines. |
| 45 | */ |
| 46 | int INSPECT = 0; |
| 47 | |
| 48 | /** |
| 49 | * Print the object as a row in a table. The columns should align for |
| 50 | * multiple objects printed beneath each other. The print may run over |
| 51 | * multiple lines but must not end in a CR. |
| 52 | */ |
| 53 | int LINE = 1; |
| 54 | |
| 55 | /** |
| 56 | * Print the value in a small format so that it is identifiable. This |
| 57 | * printed format must be recognizable by the conversion method. |
| 58 | */ |
| 59 | int PART = 2; |
| 60 | |
| 61 | /** |
| 62 | * Convert an object to the desired type. |
| 63 | * |
| 64 | * Return null if the conversion can not be done. Otherwise return and |
| 65 | * object that extends the desired type or implements it. |
| 66 | * |
| 67 | * @param desiredType |
| 68 | * The type that the returned object can be assigned to |
| 69 | * @param in |
| 70 | * The object that must be converted |
| 71 | * @return An object that can be assigned to the desired type or null. |
| 72 | * @throws Exception |
| 73 | */ |
| 74 | Object convert(Class<?> desiredType, Object in) throws Exception; |
| 75 | |
| 76 | /** |
| 77 | * Convert an objet to a CharSequence object in the requested format. The |
| 78 | * format can be INSPECT, LINE, or PART. Other values must throw |
| 79 | * IllegalArgumentException. |
| 80 | * |
| 81 | * @param target |
| 82 | * The object to be converted to a String |
| 83 | * @param level |
| 84 | * One of INSPECT, LINE, or PART. |
| 85 | * @param escape |
| 86 | * Use this object to format sub ordinate objects. |
| 87 | * @return A printed object of potentially multiple lines |
| 88 | * @throws Exception |
| 89 | */ |
| 90 | CharSequence format(Object target, int level, Converter escape) throws Exception; |
| 91 | } |