[919] | 1 | /**
|
---|
| 2 | * Conversion.java
|
---|
| 3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
| 4 | *
|
---|
| 5 | * This program is free software: you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 17 | */
|
---|
| 18 |
|
---|
[4] | 19 | package org.expeditee.io;
|
---|
| 20 |
|
---|
| 21 | import java.lang.reflect.Field;
|
---|
| 22 | import java.lang.reflect.Method;
|
---|
| 23 | import java.util.LinkedList;
|
---|
| 24 | import java.util.List;
|
---|
| 25 | import java.util.StringTokenizer;
|
---|
| 26 |
|
---|
[108] | 27 | import org.expeditee.actions.Actions;
|
---|
[1102] | 28 | import org.expeditee.core.Colour;
|
---|
| 29 | import org.expeditee.core.Font;
|
---|
| 30 | import org.expeditee.core.Point;
|
---|
[4] | 31 | import org.expeditee.items.Item;
|
---|
| 32 | import org.expeditee.items.Text;
|
---|
| 33 |
|
---|
[226] | 34 | import com.lowagie.text.FontFactory;
|
---|
| 35 |
|
---|
[4] | 36 | /**
|
---|
| 37 | * This class provides various methods for converting values to\from Java
|
---|
[80] | 38 | * objects and Expeditee file values.
|
---|
[4] | 39 | *
|
---|
| 40 | * @author jdm18
|
---|
| 41 | *
|
---|
| 42 | */
|
---|
| 43 | public class Conversion {
|
---|
| 44 |
|
---|
[1102] | 45 | public static final short RGB_MAX = Colour.COMPONENT_MAX_VALUE;
|
---|
[4] | 46 |
|
---|
[1102] | 47 | private static final float RGB_CONVERSION_FACTOR = RGB_MAX / 100.0f;
|
---|
[80] | 48 |
|
---|
[4] | 49 | /**
|
---|
[80] | 50 | * Returns the Color corresponding to the given Expeditee color code. For
|
---|
| 51 | * example: <br>
|
---|
[4] | 52 | * green4 = 0% red, 40% green, 0% blue.
|
---|
| 53 | *
|
---|
| 54 | * @param colorCode
|
---|
[80] | 55 | * The Expeditee color code to convert
|
---|
[4] | 56 | * @return The Color object corresponding to the given code
|
---|
| 57 | */
|
---|
[1102] | 58 | public static Colour getColor(String colorCode, Colour current) {
|
---|
[80] | 59 | if (colorCode == null) {
|
---|
| 60 | return null;
|
---|
[4] | 61 | }
|
---|
[80] | 62 | // The if we dont do this then we end up getting black
|
---|
| 63 | colorCode = colorCode.toLowerCase();
|
---|
| 64 | if (colorCode.equals("null"))
|
---|
| 65 | return null;
|
---|
[4] | 66 |
|
---|
[67] | 67 | // check if its a normal rgb code ie. 100 0 40
|
---|
[1102] | 68 | Colour rgb = getRGBColor(colorCode, current);
|
---|
[4] | 69 | if (rgb != null)
|
---|
| 70 | return rgb;
|
---|
| 71 |
|
---|
| 72 | // separate percentage from color (if necessary)
|
---|
| 73 | String num = "";
|
---|
| 74 | int last = colorCode.length() - 1;
|
---|
| 75 |
|
---|
| 76 | char c = colorCode.charAt(last);
|
---|
| 77 |
|
---|
[67] | 78 | while (Character.isDigit(c)) {
|
---|
[4] | 79 | num = c + num;
|
---|
| 80 | if (last <= 0)
|
---|
| 81 | break;
|
---|
| 82 |
|
---|
| 83 | c = colorCode.charAt(--last);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[72] | 86 | final float MAX_AMOUNT = 10F;
|
---|
| 87 | float amount = MAX_AMOUNT;
|
---|
[4] | 88 | if (num.length() > 0)
|
---|
| 89 | amount = Float.parseFloat(num);
|
---|
[71] | 90 |
|
---|
[72] | 91 | if (amount > MAX_AMOUNT)
|
---|
| 92 | amount = MAX_AMOUNT;
|
---|
[4] | 93 |
|
---|
| 94 | float color[] = { 0, 0, 0 };
|
---|
[108] | 95 | // Assert.assertTrue(color.length == 3);
|
---|
[4] | 96 |
|
---|
[80] | 97 | if (colorCode.startsWith("red"))
|
---|
[72] | 98 | color[0] = amount / MAX_AMOUNT;
|
---|
[80] | 99 | else if (colorCode.startsWith("green"))
|
---|
[72] | 100 | color[1] = amount / MAX_AMOUNT;
|
---|
[80] | 101 | else if (colorCode.startsWith("blue"))
|
---|
[72] | 102 | color[2] = amount / MAX_AMOUNT;
|
---|
[67] | 103 | else
|
---|
[80] | 104 | return null;
|
---|
[4] | 105 |
|
---|
[1102] | 106 | return new Colour(color[0], color[1], color[2]);
|
---|
[4] | 107 | }
|
---|
| 108 |
|
---|
[1102] | 109 | private static Colour getRGBColor(String colorCode, Colour current) {
|
---|
[562] | 110 | int color[] = new int[4];
|
---|
[108] | 111 | // Assert.assertTrue(color.length == 3);
|
---|
[4] | 112 |
|
---|
| 113 | try {
|
---|
[67] | 114 | String[] values = colorCode.trim().split("\\s+");
|
---|
[80] | 115 | // For now no transparency only RGB
|
---|
| 116 | if (values.length > color.length)
|
---|
| 117 | return null;
|
---|
[71] | 118 |
|
---|
[67] | 119 | String r = values.length > 0 ? values[0] : "0";
|
---|
| 120 | String g = values.length > 1 ? values[1] : "0";
|
---|
| 121 | String b = values.length > 2 ? values[2] : "0";
|
---|
[562] | 122 | String a = values.length > 3 ? values[3] : "100";
|
---|
[4] | 123 |
|
---|
[71] | 124 | int red = (current == null ? 0 : toColorPercent(current.getRed()));
|
---|
[562] | 125 | int green = (current == null ? 0 : toColorPercent(current.getGreen()));
|
---|
[71] | 126 | int blue = (current == null ? 0 : toColorPercent(current.getBlue()));
|
---|
[562] | 127 | int alpha = (current == null ? 0 : toColorPercent(current.getAlpha()));
|
---|
| 128 |
|
---|
[71] | 129 | color[0] = (Integer) Convert(int.class, r, red);
|
---|
| 130 | color[1] = (Integer) Convert(int.class, g, green);
|
---|
| 131 | color[2] = (Integer) Convert(int.class, b, blue);
|
---|
[562] | 132 | color[3] = (Integer) Convert(int.class, a, alpha);
|
---|
[4] | 133 |
|
---|
| 134 | for (int i = 0; i < color.length; i++) {
|
---|
[71] | 135 | color[i] = toRGB(color[i]);
|
---|
[4] | 136 | }
|
---|
[1102] | 137 | return new Colour(color[0], color[1], color[2], color[3]);
|
---|
[4] | 138 | } catch (Exception e) {
|
---|
[67] | 139 | return null;
|
---|
[4] | 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[71] | 143 | private static Integer toColorPercent(int rgb) {
|
---|
[80] | 144 | assert (rgb >= 0);
|
---|
| 145 | assert (rgb <= RGB_MAX);
|
---|
| 146 |
|
---|
[72] | 147 | int percent = (int) Math.round(rgb / RGB_CONVERSION_FACTOR);
|
---|
| 148 |
|
---|
[80] | 149 | // Dont need to do the checking below because this method will always be
|
---|
| 150 | // called with good values
|
---|
| 151 | // if (percent > PERCENT_MAX)
|
---|
| 152 | // percent = PERCENT_MAX;
|
---|
| 153 | // else if (percent < 0)
|
---|
| 154 | // percent = 0;
|
---|
| 155 |
|
---|
[71] | 156 | return percent;
|
---|
[4] | 157 | }
|
---|
[72] | 158 |
|
---|
[71] | 159 | private static Integer toRGB(int percent) {
|
---|
[80] | 160 | int rgb = Math.round(percent * RGB_CONVERSION_FACTOR);
|
---|
[72] | 161 | if (rgb > RGB_MAX)
|
---|
| 162 | rgb = RGB_MAX;
|
---|
[71] | 163 | else if (rgb < 0)
|
---|
| 164 | rgb = 0;
|
---|
[72] | 165 |
|
---|
[71] | 166 | return rgb;
|
---|
| 167 | }
|
---|
[4] | 168 |
|
---|
| 169 | /**
|
---|
[80] | 170 | * Converts the given Color object to the corresponding Expeditee color code
|
---|
[4] | 171 | *
|
---|
| 172 | * @param color
|
---|
[80] | 173 | * The Color to be turned into Expeditee color code.
|
---|
| 174 | * @return A String containing the Expeditee color code, NULL if the color
|
---|
| 175 | * is black.
|
---|
[4] | 176 | */
|
---|
[1102] | 177 | public static String getExpediteeColorCode(Colour color) {
|
---|
[4] | 178 | if (color == null)
|
---|
| 179 | return null;
|
---|
| 180 |
|
---|
[72] | 181 | int r = (int) Math.round((color.getRed() / RGB_CONVERSION_FACTOR));
|
---|
| 182 | int g = (int) Math.round((color.getGreen() / RGB_CONVERSION_FACTOR));
|
---|
| 183 | int b = (int) Math.round((color.getBlue() / RGB_CONVERSION_FACTOR));
|
---|
[562] | 184 | int a = (int) Math.round((color.getAlpha() / RGB_CONVERSION_FACTOR));
|
---|
[4] | 185 |
|
---|
[562] | 186 | return r + " " + g + " " + b + " " + a;
|
---|
[4] | 187 | }
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
[80] | 190 | * Converts the given Font to a corresponding Expeditee font code.
|
---|
[4] | 191 | *
|
---|
| 192 | * @param font
|
---|
| 193 | * The Font to convert to a code.
|
---|
[80] | 194 | * @return The Expeditee font code that corresponds to the given Font.
|
---|
[4] | 195 | */
|
---|
[80] | 196 | public static String getExpediteeFontCode(Font font) {
|
---|
[1102] | 197 | String fontName = font.getFamilyName();
|
---|
| 198 | String code = font.getFamilyName() + '_';
|
---|
[67] | 199 |
|
---|
[4] | 200 | for (int i = 0; i < Text.FONT_WHEEL.length; i++) {
|
---|
[108] | 201 | if (Text.FONT_WHEEL[i].equalsIgnoreCase(fontName)) {
|
---|
[4] | 202 | code = "" + Text.FONT_CHARS[i];
|
---|
| 203 | break;
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
[67] | 206 |
|
---|
[4] | 207 | switch (font.getStyle()) {
|
---|
[1102] | 208 | case BOLD:
|
---|
[4] | 209 | code += "b";
|
---|
| 210 | break;
|
---|
[1102] | 211 | case PLAIN:
|
---|
[4] | 212 | code += "r";
|
---|
| 213 | break;
|
---|
[1102] | 214 | case ITALIC:
|
---|
[4] | 215 | code += "i";
|
---|
| 216 | break;
|
---|
| 217 | default:
|
---|
| 218 | code += "p";
|
---|
| 219 | break;
|
---|
| 220 | }
|
---|
[80] | 221 |
|
---|
[72] | 222 | code += font.getSize();
|
---|
[4] | 223 | return code;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | /**
|
---|
[80] | 227 | * Converts the given Expeditee font code to a Font object.<br>
|
---|
[4] | 228 | * For example: <br>
|
---|
| 229 | * tr16 = times, roman, 16pt
|
---|
| 230 | *
|
---|
| 231 | * @param fontCode
|
---|
| 232 | * The font code to convert to a Font
|
---|
| 233 | * @return the Font that corresponds to the given font code.
|
---|
| 234 | */
|
---|
| 235 | public static Font getFont(String fontCode) {
|
---|
[80] | 236 | assert (fontCode != null);
|
---|
| 237 |
|
---|
[108] | 238 | int separator = fontCode.indexOf('_');
|
---|
[1102] | 239 | String familyName = Text.FONT_WHEEL[0];
|
---|
[108] | 240 | if (separator > 0) {
|
---|
[1102] | 241 | familyName = Actions.getCapitalizedFontName(fontCode.substring(0, separator));
|
---|
[108] | 242 | fontCode = fontCode.substring(separator);
|
---|
| 243 | } else {
|
---|
| 244 | char c = fontCode.charAt(0);
|
---|
| 245 | for (int i = 0; i < Text.FONT_CHARS.length; i++) {
|
---|
| 246 | if (c == Text.FONT_CHARS[i]) {
|
---|
[1102] | 247 | familyName = Text.FONT_WHEEL[i];
|
---|
[108] | 248 | break;
|
---|
| 249 | }
|
---|
[4] | 250 | }
|
---|
| 251 | }
|
---|
[1102] | 252 |
|
---|
| 253 | Font font = new Font(familyName);
|
---|
[4] | 254 |
|
---|
| 255 | switch (fontCode.charAt(1)) {
|
---|
| 256 | case 'r':
|
---|
[1102] | 257 | font.setStyle(Font.Style.PLAIN);
|
---|
[4] | 258 | break;
|
---|
| 259 | case 'b':
|
---|
[1102] | 260 | font.setStyle(Font.Style.BOLD);
|
---|
[4] | 261 | break;
|
---|
| 262 | case 'i':
|
---|
[1102] | 263 | font.setStyle(Font.Style.ITALIC);
|
---|
[4] | 264 | break;
|
---|
| 265 | case 'p':
|
---|
[1102] | 266 | font.setStyle(Font.Style.BOLD_ITALIC);
|
---|
[4] | 267 | break;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | try {
|
---|
| 271 | int size = Integer.parseInt(fontCode.substring(2));
|
---|
[1102] | 272 |
|
---|
| 273 | font.setSize(size);
|
---|
[4] | 274 | } catch (NumberFormatException nfe) {
|
---|
[1102] | 275 | // Just keep going
|
---|
[4] | 276 | }
|
---|
| 277 |
|
---|
| 278 | return font;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | /**
|
---|
| 282 | * Returns the number portion (Frame Number) of the given Frame name.
|
---|
| 283 | *
|
---|
| 284 | * @param framename
|
---|
| 285 | * The Frame name to extract the number from
|
---|
| 286 | * @return The Frame number
|
---|
| 287 | */
|
---|
| 288 | public static int getFrameNumber(String framename) {
|
---|
[67] | 289 | String num = null;
|
---|
| 290 | // The framename must end in a digit
|
---|
| 291 | assert (Character.isDigit(framename.charAt(framename.length() - 1)));
|
---|
| 292 | // And start with a letter
|
---|
| 293 | assert (Character.isLetter(framename.charAt(0)));
|
---|
| 294 | // start at the end and find the first non digit char
|
---|
| 295 | for (int i = framename.length() - 2; i >= 0; i--) {
|
---|
| 296 | if (!Character.isDigit(framename.charAt(i))) {
|
---|
| 297 | num = framename.substring(i + 1);
|
---|
| 298 | break;
|
---|
| 299 | }
|
---|
| 300 | }
|
---|
[80] | 301 | return Integer.parseInt(num);
|
---|
[4] | 302 | }
|
---|
| 303 |
|
---|
| 304 | /**
|
---|
| 305 | * Returns the frameset poriton of the given Frame name (frame number
|
---|
[67] | 306 | * removed) converted to lower case.
|
---|
[4] | 307 | *
|
---|
| 308 | * @param frame
|
---|
| 309 | * The full name to extract the Frameset name from
|
---|
| 310 | * @return the name of the Frameset extracted from the given Frame name.
|
---|
| 311 | */
|
---|
[97] | 312 | public static String getFramesetName(String frame) {
|
---|
| 313 | return getFramesetName(frame, true);
|
---|
[4] | 314 | }
|
---|
| 315 |
|
---|
[295] | 316 | public static String getFramesetName(String framename,
|
---|
[108] | 317 | boolean convertToLower) {
|
---|
[67] | 318 | String set = null;
|
---|
| 319 | assert (Character.isDigit(framename.charAt(framename.length() - 1)));
|
---|
| 320 | // And start with a letter
|
---|
| 321 | assert (Character.isLetter(framename.charAt(0)));
|
---|
| 322 | // start at the end and find the first non digit char
|
---|
| 323 | for (int i = framename.length() - 2; i >= 0; i--) {
|
---|
| 324 | if (!Character.isDigit(framename.charAt(i))) {
|
---|
| 325 | set = framename.substring(0, i + 1);
|
---|
| 326 | break;
|
---|
| 327 | }
|
---|
[4] | 328 | }
|
---|
| 329 |
|
---|
| 330 | if (convertToLower)
|
---|
| 331 | return set.toLowerCase();
|
---|
| 332 |
|
---|
| 333 | return set;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | public static Object Convert(Class type, String value) {
|
---|
| 337 | return Convert(type, value, null);
|
---|
| 338 | }
|
---|
| 339 |
|
---|
[80] | 340 | // Will convert from Expeditee color values to RGB...
|
---|
[4] | 341 | public static Object Convert(Class type, String value, Object orig) {
|
---|
| 342 | // System.out.println("Orig: " + orig);
|
---|
[80] | 343 | assert (type != null);
|
---|
| 344 |
|
---|
[72] | 345 | if (value == null)
|
---|
| 346 | return null;
|
---|
[4] | 347 |
|
---|
[86] | 348 | String fullCaseValue = value;
|
---|
| 349 | value = fullCaseValue.trim().toLowerCase();
|
---|
[4] | 350 |
|
---|
| 351 | if (type == Font.class) {
|
---|
| 352 | return getFont(value);
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[1102] | 355 | if (type.equals(Colour.class)) {
|
---|
[4] | 356 | if (value.length() == 0)
|
---|
| 357 | return null;
|
---|
| 358 |
|
---|
| 359 | try {
|
---|
[790] | 360 | // Try to decode the string as a hex or octal color code
|
---|
[1102] | 361 | return Colour.decode(value);
|
---|
[790] | 362 | } catch (NumberFormatException nfe) {
|
---|
| 363 | try {
|
---|
[1102] | 364 | // Try to find the field in the Colour class with the same name as the given string
|
---|
| 365 | Field[] fields = Colour.class.getFields();
|
---|
[790] | 366 | Field field = null;
|
---|
| 367 | for (int i = 0; i < fields.length; i++) {
|
---|
| 368 | if (fields[i].getName().equalsIgnoreCase(value)) {
|
---|
| 369 | field = fields[i];
|
---|
| 370 | break;
|
---|
| 371 | }
|
---|
[72] | 372 | }
|
---|
[1102] | 373 | return (Colour) field.get(null);
|
---|
[790] | 374 | } catch (Exception e) {
|
---|
[1102] | 375 | return getColor(value, (Colour) orig);
|
---|
[72] | 376 | }
|
---|
[4] | 377 | }
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | if (type.equals(int.class)) {
|
---|
[1102] | 381 | if (orig instanceof Integer && (value.startsWith("+") || value.startsWith("-"))) {
|
---|
[4] | 382 | value = value.replace("+", "");
|
---|
| 383 |
|
---|
[115] | 384 | return ((Integer) orig) + Integer.decode(value);
|
---|
[4] | 385 | }
|
---|
| 386 |
|
---|
[1102] | 387 | if (value.length() == 0 || value.equals("null")) return Item.DEFAULT_INTEGER;
|
---|
[4] | 388 |
|
---|
[115] | 389 | return Integer.decode(value);
|
---|
[4] | 390 | }
|
---|
| 391 |
|
---|
| 392 | if (type.equals(float.class)) {
|
---|
| 393 | if (orig instanceof Float
|
---|
| 394 | && (value.startsWith("+") || value.startsWith("-"))) {
|
---|
| 395 | value = value.replace("+", "");
|
---|
| 396 |
|
---|
| 397 | return ((Float) orig) + Float.parseFloat(value);
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | return Float.parseFloat(value);
|
---|
| 401 | }
|
---|
[289] | 402 |
|
---|
[147] | 403 | if (type.equals(Float.class)) {
|
---|
| 404 | if (orig instanceof Float
|
---|
| 405 | && (value.startsWith("+") || value.startsWith("-"))) {
|
---|
| 406 | value = value.replace("+", "");
|
---|
[4] | 407 |
|
---|
[147] | 408 | return ((Float) orig) + Float.parseFloat(value);
|
---|
| 409 | }
|
---|
[289] | 410 |
|
---|
| 411 | if (value.length() == 0 || value.equals("null"))
|
---|
[147] | 412 | return null;
|
---|
| 413 |
|
---|
| 414 | return Float.parseFloat(value);
|
---|
| 415 | }
|
---|
[427] | 416 |
|
---|
[400] | 417 | if (type.equals(Integer.class)) {
|
---|
[1102] | 418 | if (orig instanceof Integer && (value.startsWith("+") || value.startsWith("-"))) {
|
---|
[400] | 419 | value = value.replace("+", "");
|
---|
[147] | 420 |
|
---|
[1102] | 421 | Integer newValue = ((Integer) orig) + Integer.valueOf((int) Double.parseDouble(value));
|
---|
[427] | 422 | if (newValue <= 0)
|
---|
[421] | 423 | return null;
|
---|
| 424 | return newValue;
|
---|
[400] | 425 | }
|
---|
| 426 |
|
---|
| 427 | if (value.length() == 0 || value.equals("null"))
|
---|
| 428 | return null;
|
---|
| 429 |
|
---|
[1102] | 430 | return Integer.valueOf((int) Double.parseDouble(value));
|
---|
[400] | 431 | }
|
---|
| 432 |
|
---|
[4] | 433 | if (type.equals(double.class)) {
|
---|
| 434 | if (orig instanceof Double
|
---|
| 435 | && (value.startsWith("+") || value.startsWith("-"))) {
|
---|
| 436 | value = value.replace("+", "");
|
---|
| 437 |
|
---|
| 438 | return ((Double) orig) + Double.parseDouble(value);
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | return Double.parseDouble(value);
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | if (type.equals(int[].class)) {
|
---|
| 445 | StringTokenizer st = new StringTokenizer(value, " ");
|
---|
| 446 | int[] param = new int[st.countTokens()];
|
---|
| 447 | for (int i = 0; i < param.length; i++) {
|
---|
| 448 | try {
|
---|
| 449 | param[i] = Integer.parseInt(st.nextToken());
|
---|
| 450 | } catch (Exception e) {
|
---|
| 451 | return null;
|
---|
| 452 | }
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | return param;
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | if (type.equals(Font.class)) {
|
---|
| 459 | return Conversion.getFont(value);
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | if (type.equals(boolean.class)) {
|
---|
| 463 | if (value.equals("t") || value.equals("true")
|
---|
| 464 | || value.equals("yes") || value.equals("y")
|
---|
| 465 | || value.equals(""))
|
---|
| 466 | return true;
|
---|
[80] | 467 | return false;
|
---|
[4] | 468 |
|
---|
[80] | 469 | }
|
---|
[4] | 470 |
|
---|
[80] | 471 | if (type.equals(Point.class)) {
|
---|
| 472 | Point p = new Point();
|
---|
| 473 | String xPos = value.substring(0, value.indexOf(" "));
|
---|
| 474 | String yPos = value.substring(value.indexOf(" ") + 1);
|
---|
| 475 |
|
---|
| 476 | if (orig == null) {
|
---|
[1144] | 477 | p.setX(Integer.parseInt(xPos.trim()));
|
---|
| 478 | p.setY(Integer.parseInt(yPos.trim()));
|
---|
[80] | 479 | } else {
|
---|
| 480 | assert (orig instanceof Point);
|
---|
| 481 | Point originalPoint = (Point) orig;
|
---|
[1144] | 482 | p.setX((Integer) Convert(int.class, xPos, originalPoint.getX()));
|
---|
| 483 | p.setY((Integer) Convert(int.class, yPos, originalPoint.getY()));
|
---|
[80] | 484 | }
|
---|
| 485 | return p;
|
---|
[4] | 486 | }
|
---|
| 487 |
|
---|
[80] | 488 | assert (type == String.class);
|
---|
| 489 | if (value.equals(""))
|
---|
| 490 | return null;
|
---|
| 491 | return fullCaseValue;
|
---|
[4] | 492 | }
|
---|
| 493 |
|
---|
| 494 | public static Object[] Convert(Method method, String value) {
|
---|
| 495 | return Convert(method, value, null);
|
---|
| 496 | }
|
---|
| 497 |
|
---|
[80] | 498 | /**
|
---|
| 499 | * Converts parameters for setting an attribute from a string form into an
|
---|
| 500 | * object array form. The object array can then be passed when invoke the
|
---|
| 501 | * set method via reflection.
|
---|
| 502 | *
|
---|
| 503 | * @param method
|
---|
| 504 | * a method which sets an attribute
|
---|
| 505 | * @param value
|
---|
| 506 | * new value for the attribute
|
---|
| 507 | * @param current
|
---|
| 508 | * current value of the attribute
|
---|
| 509 | * @return
|
---|
| 510 | */
|
---|
[4] | 511 | public static Object[] Convert(Method method, String value, Object current) {
|
---|
| 512 |
|
---|
[427] | 513 | if (method == null) {
|
---|
[390] | 514 | System.out.println("Error converting null method");
|
---|
| 515 | return null;
|
---|
| 516 | }
|
---|
[427] | 517 |
|
---|
[4] | 518 | String name = method.getName();
|
---|
| 519 | Class[] types = method.getParameterTypes();
|
---|
[108] | 520 |
|
---|
[86] | 521 | String fullValue = value;
|
---|
| 522 | value = value.trim();
|
---|
[71] | 523 |
|
---|
[1509] | 524 | if ((method.getParameterTypes()[0].isEnum()) || (name.matches("setPermission")) || (name.matches("setFrameEncryptionPermission")) || (name.matches("setEncryptionPermission"))) {
|
---|
[427] | 525 | Method convertString;
|
---|
| 526 | Object[] objects = new Object[1];
|
---|
| 527 | try {
|
---|
| 528 | convertString = method.getParameterTypes()[0].getMethod(
|
---|
| 529 | "convertString", new Class[] { String.class });
|
---|
| 530 | objects[0] = convertString.invoke(null, new Object[] { value });
|
---|
| 531 | } catch (Exception e) {
|
---|
| 532 | e.printStackTrace();
|
---|
| 533 | }
|
---|
| 534 | return objects;
|
---|
[4] | 535 | }
|
---|
| 536 |
|
---|
| 537 | if (name.endsWith("Arrow")) {
|
---|
| 538 | if (value.indexOf(" ") < 0)
|
---|
| 539 | return null;
|
---|
| 540 |
|
---|
[108] | 541 | Float length = null;
|
---|
[80] | 542 | Double ratio = null;
|
---|
[476] | 543 | Double nib_perc = null;
|
---|
| 544 |
|
---|
[80] | 545 | if (current == null) {
|
---|
| 546 | length = getArrowLength(value);
|
---|
| 547 | ratio = getArrowRatio(value);
|
---|
[476] | 548 | nib_perc = getArrowNibPerc(value);
|
---|
[80] | 549 | } else {
|
---|
| 550 | assert (current instanceof String);
|
---|
[108] | 551 | float oldLength = getArrowLength(current.toString());
|
---|
[80] | 552 | double oldRatio = getArrowRatio(current.toString());
|
---|
[476] | 553 | double oldNibPerc = getArrowNibPerc(current.toString());
|
---|
| 554 |
|
---|
| 555 | int first_space_pos = value.indexOf(" ");
|
---|
| 556 | String args23 = value.substring(first_space_pos).trim();
|
---|
| 557 | int second_space_pos = args23.indexOf(" ");
|
---|
| 558 |
|
---|
| 559 | if (second_space_pos<0) {
|
---|
| 560 | // two argument form of arrow data (no nib value)
|
---|
| 561 | second_space_pos = args23.length();
|
---|
| 562 | }
|
---|
| 563 |
|
---|
| 564 | length = (Float) Convert(float.class, value.substring(0, value.indexOf(" ")), oldLength);
|
---|
| 565 | ratio = (Double) Convert(double.class, args23.substring(0,second_space_pos).trim(), oldRatio);
|
---|
| 566 |
|
---|
| 567 | if (second_space_pos==args23.length()) {
|
---|
| 568 | nib_perc = oldNibPerc;
|
---|
| 569 | }
|
---|
| 570 | else {
|
---|
| 571 | nib_perc = (Double) Convert(double.class, args23.substring(second_space_pos).trim(), oldNibPerc);
|
---|
| 572 | }
|
---|
| 573 |
|
---|
[80] | 574 | }
|
---|
| 575 |
|
---|
[476] | 576 | Object[] vals = new Object[3];
|
---|
[80] | 577 | vals[0] = length;
|
---|
| 578 | vals[1] = ratio;
|
---|
[476] | 579 | vals[2] = nib_perc;
|
---|
[4] | 580 | return vals;
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | if (types.length == 1 && types[0] == List.class) {
|
---|
| 584 | StringTokenizer st = new StringTokenizer(value, "\n");
|
---|
| 585 | List<String> list = new LinkedList<String>();
|
---|
| 586 | while (st.hasMoreTokens())
|
---|
| 587 | list.add(st.nextToken());
|
---|
| 588 |
|
---|
| 589 | Object[] vals = new Object[1];
|
---|
| 590 | vals[0] = list;
|
---|
| 591 | return vals;
|
---|
| 592 | }
|
---|
| 593 |
|
---|
[80] | 594 | assert (types.length == 1);
|
---|
[4] | 595 |
|
---|
[80] | 596 | Object o[] = new Object[1];
|
---|
[86] | 597 | o[0] = Convert(types[0], fullValue, current);
|
---|
[80] | 598 | return o;
|
---|
[4] | 599 | }
|
---|
| 600 |
|
---|
[476] | 601 | private static float getArrowLength(String args123) {
|
---|
| 602 | return Float.parseFloat(args123.substring(0, args123.indexOf(" ")));
|
---|
[80] | 603 | }
|
---|
| 604 |
|
---|
[476] | 605 | private static double getArrowRatio(String args123) {
|
---|
| 606 | int first_space_pos = args123.indexOf(" ");
|
---|
| 607 | String args23 = args123.substring(first_space_pos).trim();
|
---|
| 608 | int second_space_pos = args23.indexOf(" ");
|
---|
| 609 |
|
---|
| 610 | if (second_space_pos<0) {
|
---|
| 611 | // two argument form of arrow data (no nib value)
|
---|
| 612 | second_space_pos = args23.length();
|
---|
| 613 | }
|
---|
| 614 | return Double.parseDouble(args23.substring(0,second_space_pos).trim());
|
---|
[80] | 615 | }
|
---|
[476] | 616 |
|
---|
| 617 | private static double getArrowNibPerc(String args123) {
|
---|
| 618 | int first_space_pos = args123.indexOf(" ");
|
---|
| 619 | String args23 = args123.substring(first_space_pos).trim();
|
---|
| 620 | int second_space_pos = args23.indexOf(" ");
|
---|
| 621 |
|
---|
| 622 | double nib_perc = Item.DEFAULT_ARROWHEAD_NIB_PERC;
|
---|
| 623 |
|
---|
| 624 | if (second_space_pos>0) {
|
---|
| 625 | String nib_perc_str = args23.substring(second_space_pos).trim();
|
---|
| 626 | nib_perc = Double.parseDouble(nib_perc_str);
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | return nib_perc;
|
---|
| 630 |
|
---|
| 631 | }
|
---|
[80] | 632 |
|
---|
| 633 | public static Object ConvertToExpeditee(Method method, Object output) {
|
---|
[4] | 634 | if (output == null)
|
---|
| 635 | return null;
|
---|
| 636 |
|
---|
[80] | 637 | assert (method != null);
|
---|
[4] | 638 |
|
---|
| 639 | String name = method.getName();
|
---|
| 640 |
|
---|
| 641 | if (output instanceof List)
|
---|
| 642 | return output;
|
---|
| 643 |
|
---|
[289] | 644 | if (name.endsWith("Text")) {
|
---|
[147] | 645 | List<String> list = new LinkedList<String>();
|
---|
[289] | 646 | for (String s : output.toString().split("\n")) {
|
---|
[147] | 647 | list.add(s);
|
---|
| 648 | }
|
---|
| 649 | return list;
|
---|
| 650 | }
|
---|
[289] | 651 |
|
---|
[1509] | 652 | if ((method.getReturnType().isEnum()) || (name.equals("getPermission")) || (name.equals("getFrameEncryptionPermission")) || (name.equals("getEncryptionPermission"))) {
|
---|
[427] | 653 | try {
|
---|
| 654 | return output.getClass().getMethod("getCode", new Class[] {})
|
---|
| 655 | .invoke(output, new Object[] {});
|
---|
| 656 | } catch (Exception e) {
|
---|
| 657 | e.printStackTrace();
|
---|
| 658 | }
|
---|
| 659 | return null;
|
---|
| 660 | }
|
---|
[4] | 661 |
|
---|
| 662 | // strings can be returned immediately
|
---|
| 663 | if (output instanceof String)
|
---|
| 664 | return (String) output;
|
---|
| 665 |
|
---|
[427] | 666 | // For int's... negative numbers signal NULL
|
---|
| 667 | /*
|
---|
| 668 | * TODO change so that all items use Integer class... where null,
|
---|
| 669 | * signals null
|
---|
| 670 | */
|
---|
| 671 | if (method.getReturnType().equals(int.class)) {
|
---|
| 672 | if ((Integer) output >= 0)
|
---|
| 673 | return output + "";
|
---|
| 674 | return null;
|
---|
| 675 | }
|
---|
| 676 |
|
---|
| 677 | // integers can also(Float) output >= 0 be returned immediately
|
---|
[400] | 678 | if (output instanceof Integer)
|
---|
[4] | 679 | return "" + output;
|
---|
| 680 |
|
---|
| 681 | // floats can also be returned immediately
|
---|
[659] | 682 | if (output instanceof Float) // && (Float) output >= 0) // Removed checking if >0, as some floats (e.g. letter spacing) can be negative
|
---|
[4] | 683 | return "" + output;
|
---|
| 684 |
|
---|
| 685 | // convert fonts
|
---|
| 686 | if (output instanceof Font)
|
---|
[80] | 687 | return getExpediteeFontCode((Font) output);
|
---|
[4] | 688 |
|
---|
| 689 | // convert colors
|
---|
[1102] | 690 | if (output instanceof Colour)
|
---|
| 691 | return getExpediteeColorCode((Colour) output);
|
---|
[4] | 692 |
|
---|
[1415] | 693 | // convert points
|
---|
[4] | 694 | if (output instanceof Point)
|
---|
[1144] | 695 | return ((Point) output).getX() + " " + ((Point) output).getY();
|
---|
[4] | 696 |
|
---|
[1415] | 697 | if (output instanceof Boolean) {
|
---|
| 698 | try {
|
---|
| 699 | Class<?> parentClass = method.getDeclaringClass();
|
---|
| 700 | Field defaultValueField = parentClass.getField(method.getName() + "Default");
|
---|
| 701 | boolean defaultValue = defaultValueField.getBoolean(null);
|
---|
| 702 | if (defaultValue == (boolean) output) {
|
---|
| 703 | return null;
|
---|
| 704 | }
|
---|
| 705 | } catch (IllegalArgumentException e) {
|
---|
| 706 | e.printStackTrace();
|
---|
| 707 | } catch (SecurityException e) {
|
---|
| 708 | e.printStackTrace();
|
---|
| 709 | } catch (NoSuchFieldException e) {
|
---|
| 710 | // true is the default for boolean values when no other default is provided
|
---|
| 711 | if ((boolean) output) {
|
---|
| 712 | return null;
|
---|
| 713 | } else {
|
---|
| 714 | return "F";
|
---|
| 715 | }
|
---|
| 716 | } catch (IllegalAccessException e) {
|
---|
| 717 | e.printStackTrace();
|
---|
| 718 | }
|
---|
| 719 | return output;
|
---|
| 720 | }
|
---|
[4] | 721 |
|
---|
| 722 | if (output instanceof int[]) {
|
---|
| 723 | int[] out = (int[]) output;
|
---|
| 724 | String res = "";
|
---|
| 725 | for (int i : out)
|
---|
| 726 | res += i + " ";
|
---|
| 727 |
|
---|
| 728 | res = res.trim();
|
---|
| 729 | if (res.length() > 0)
|
---|
| 730 | return res;
|
---|
| 731 | }
|
---|
| 732 | // default
|
---|
| 733 | return null;
|
---|
| 734 | }
|
---|
[226] | 735 |
|
---|
| 736 | public static String getPdfFont(String family) {
|
---|
| 737 | family = family.toLowerCase();
|
---|
[289] | 738 | if (family.equals(Text.FONT_WHEEL[0])) {
|
---|
[226] | 739 | return FontFactory.HELVETICA;
|
---|
[289] | 740 | } else if (family.equals(Text.FONT_WHEEL[2])) {
|
---|
[226] | 741 | return FontFactory.TIMES_ROMAN;
|
---|
| 742 | }
|
---|
| 743 | return FontFactory.COURIER;
|
---|
| 744 | }
|
---|
[289] | 745 |
|
---|
[1102] | 746 | public static String getCssColor(Colour c) {
|
---|
[289] | 747 | assert (c != null);
|
---|
[1102] | 748 | return "rgb(" + c.getRed255() + "," + c.getGreen255() + "," + c.getBlue255()
|
---|
[289] | 749 | + ")";
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | public static String getCssFontFamily(String family) {
|
---|
| 753 | family = family.toLowerCase();
|
---|
[427] | 754 | if (family.equals("monospaced") || family.equals("dialog")) {
|
---|
[289] | 755 | return "courier";
|
---|
[427] | 756 | } else if (family.equals("sansserif")) {
|
---|
[289] | 757 | return "sans-serif";
|
---|
[427] | 758 | } else {
|
---|
[289] | 759 | return family;
|
---|
| 760 | }
|
---|
| 761 | }
|
---|
[4] | 762 | }
|
---|