source: trunk/src/org/expeditee/io/Conversion.java@ 1509

Last change on this file since 1509 was 1509, checked in by bnemhaus, 4 years ago

ItemEncryptionPermission is now respected.

File size: 20.3 KB
Line 
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
19package org.expeditee.io;
20
21import java.lang.reflect.Field;
22import java.lang.reflect.Method;
23import java.util.LinkedList;
24import java.util.List;
25import java.util.StringTokenizer;
26
27import org.expeditee.actions.Actions;
28import org.expeditee.core.Colour;
29import org.expeditee.core.Font;
30import org.expeditee.core.Point;
31import org.expeditee.items.Item;
32import org.expeditee.items.Text;
33
34import com.lowagie.text.FontFactory;
35
36/**
37 * This class provides various methods for converting values to\from Java
38 * objects and Expeditee file values.
39 *
40 * @author jdm18
41 *
42 */
43public class Conversion {
44
45 public static final short RGB_MAX = Colour.COMPONENT_MAX_VALUE;
46
47 private static final float RGB_CONVERSION_FACTOR = RGB_MAX / 100.0f;
48
49 /**
50 * Returns the Color corresponding to the given Expeditee color code. For
51 * example: <br>
52 * green4 = 0% red, 40% green, 0% blue.
53 *
54 * @param colorCode
55 * The Expeditee color code to convert
56 * @return The Color object corresponding to the given code
57 */
58 public static Colour getColor(String colorCode, Colour current) {
59 if (colorCode == null) {
60 return null;
61 }
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;
66
67 // check if its a normal rgb code ie. 100 0 40
68 Colour rgb = getRGBColor(colorCode, current);
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
78 while (Character.isDigit(c)) {
79 num = c + num;
80 if (last <= 0)
81 break;
82
83 c = colorCode.charAt(--last);
84 }
85
86 final float MAX_AMOUNT = 10F;
87 float amount = MAX_AMOUNT;
88 if (num.length() > 0)
89 amount = Float.parseFloat(num);
90
91 if (amount > MAX_AMOUNT)
92 amount = MAX_AMOUNT;
93
94 float color[] = { 0, 0, 0 };
95 // Assert.assertTrue(color.length == 3);
96
97 if (colorCode.startsWith("red"))
98 color[0] = amount / MAX_AMOUNT;
99 else if (colorCode.startsWith("green"))
100 color[1] = amount / MAX_AMOUNT;
101 else if (colorCode.startsWith("blue"))
102 color[2] = amount / MAX_AMOUNT;
103 else
104 return null;
105
106 return new Colour(color[0], color[1], color[2]);
107 }
108
109 private static Colour getRGBColor(String colorCode, Colour current) {
110 int color[] = new int[4];
111 // Assert.assertTrue(color.length == 3);
112
113 try {
114 String[] values = colorCode.trim().split("\\s+");
115 // For now no transparency only RGB
116 if (values.length > color.length)
117 return null;
118
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";
122 String a = values.length > 3 ? values[3] : "100";
123
124 int red = (current == null ? 0 : toColorPercent(current.getRed()));
125 int green = (current == null ? 0 : toColorPercent(current.getGreen()));
126 int blue = (current == null ? 0 : toColorPercent(current.getBlue()));
127 int alpha = (current == null ? 0 : toColorPercent(current.getAlpha()));
128
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);
132 color[3] = (Integer) Convert(int.class, a, alpha);
133
134 for (int i = 0; i < color.length; i++) {
135 color[i] = toRGB(color[i]);
136 }
137 return new Colour(color[0], color[1], color[2], color[3]);
138 } catch (Exception e) {
139 return null;
140 }
141 }
142
143 private static Integer toColorPercent(int rgb) {
144 assert (rgb >= 0);
145 assert (rgb <= RGB_MAX);
146
147 int percent = (int) Math.round(rgb / RGB_CONVERSION_FACTOR);
148
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
156 return percent;
157 }
158
159 private static Integer toRGB(int percent) {
160 int rgb = Math.round(percent * RGB_CONVERSION_FACTOR);
161 if (rgb > RGB_MAX)
162 rgb = RGB_MAX;
163 else if (rgb < 0)
164 rgb = 0;
165
166 return rgb;
167 }
168
169 /**
170 * Converts the given Color object to the corresponding Expeditee color code
171 *
172 * @param color
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.
176 */
177 public static String getExpediteeColorCode(Colour color) {
178 if (color == null)
179 return null;
180
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));
184 int a = (int) Math.round((color.getAlpha() / RGB_CONVERSION_FACTOR));
185
186 return r + " " + g + " " + b + " " + a;
187 }
188
189 /**
190 * Converts the given Font to a corresponding Expeditee font code.
191 *
192 * @param font
193 * The Font to convert to a code.
194 * @return The Expeditee font code that corresponds to the given Font.
195 */
196 public static String getExpediteeFontCode(Font font) {
197 String fontName = font.getFamilyName();
198 String code = font.getFamilyName() + '_';
199
200 for (int i = 0; i < Text.FONT_WHEEL.length; i++) {
201 if (Text.FONT_WHEEL[i].equalsIgnoreCase(fontName)) {
202 code = "" + Text.FONT_CHARS[i];
203 break;
204 }
205 }
206
207 switch (font.getStyle()) {
208 case BOLD:
209 code += "b";
210 break;
211 case PLAIN:
212 code += "r";
213 break;
214 case ITALIC:
215 code += "i";
216 break;
217 default:
218 code += "p";
219 break;
220 }
221
222 code += font.getSize();
223 return code;
224 }
225
226 /**
227 * Converts the given Expeditee font code to a Font object.<br>
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) {
236 assert (fontCode != null);
237
238 int separator = fontCode.indexOf('_');
239 String familyName = Text.FONT_WHEEL[0];
240 if (separator > 0) {
241 familyName = Actions.getCapitalizedFontName(fontCode.substring(0, separator));
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]) {
247 familyName = Text.FONT_WHEEL[i];
248 break;
249 }
250 }
251 }
252
253 Font font = new Font(familyName);
254
255 switch (fontCode.charAt(1)) {
256 case 'r':
257 font.setStyle(Font.Style.PLAIN);
258 break;
259 case 'b':
260 font.setStyle(Font.Style.BOLD);
261 break;
262 case 'i':
263 font.setStyle(Font.Style.ITALIC);
264 break;
265 case 'p':
266 font.setStyle(Font.Style.BOLD_ITALIC);
267 break;
268 }
269
270 try {
271 int size = Integer.parseInt(fontCode.substring(2));
272
273 font.setSize(size);
274 } catch (NumberFormatException nfe) {
275 // Just keep going
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) {
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 }
301 return Integer.parseInt(num);
302 }
303
304 /**
305 * Returns the frameset poriton of the given Frame name (frame number
306 * removed) converted to lower case.
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 */
312 public static String getFramesetName(String frame) {
313 return getFramesetName(frame, true);
314 }
315
316 public static String getFramesetName(String framename,
317 boolean convertToLower) {
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 }
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
340 // Will convert from Expeditee color values to RGB...
341 public static Object Convert(Class type, String value, Object orig) {
342 // System.out.println("Orig: " + orig);
343 assert (type != null);
344
345 if (value == null)
346 return null;
347
348 String fullCaseValue = value;
349 value = fullCaseValue.trim().toLowerCase();
350
351 if (type == Font.class) {
352 return getFont(value);
353 }
354
355 if (type.equals(Colour.class)) {
356 if (value.length() == 0)
357 return null;
358
359 try {
360 // Try to decode the string as a hex or octal color code
361 return Colour.decode(value);
362 } catch (NumberFormatException nfe) {
363 try {
364 // Try to find the field in the Colour class with the same name as the given string
365 Field[] fields = Colour.class.getFields();
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 }
372 }
373 return (Colour) field.get(null);
374 } catch (Exception e) {
375 return getColor(value, (Colour) orig);
376 }
377 }
378 }
379
380 if (type.equals(int.class)) {
381 if (orig instanceof Integer && (value.startsWith("+") || value.startsWith("-"))) {
382 value = value.replace("+", "");
383
384 return ((Integer) orig) + Integer.decode(value);
385 }
386
387 if (value.length() == 0 || value.equals("null")) return Item.DEFAULT_INTEGER;
388
389 return Integer.decode(value);
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 }
402
403 if (type.equals(Float.class)) {
404 if (orig instanceof Float
405 && (value.startsWith("+") || value.startsWith("-"))) {
406 value = value.replace("+", "");
407
408 return ((Float) orig) + Float.parseFloat(value);
409 }
410
411 if (value.length() == 0 || value.equals("null"))
412 return null;
413
414 return Float.parseFloat(value);
415 }
416
417 if (type.equals(Integer.class)) {
418 if (orig instanceof Integer && (value.startsWith("+") || value.startsWith("-"))) {
419 value = value.replace("+", "");
420
421 Integer newValue = ((Integer) orig) + Integer.valueOf((int) Double.parseDouble(value));
422 if (newValue <= 0)
423 return null;
424 return newValue;
425 }
426
427 if (value.length() == 0 || value.equals("null"))
428 return null;
429
430 return Integer.valueOf((int) Double.parseDouble(value));
431 }
432
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;
467 return false;
468
469 }
470
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) {
477 p.setX(Integer.parseInt(xPos.trim()));
478 p.setY(Integer.parseInt(yPos.trim()));
479 } else {
480 assert (orig instanceof Point);
481 Point originalPoint = (Point) orig;
482 p.setX((Integer) Convert(int.class, xPos, originalPoint.getX()));
483 p.setY((Integer) Convert(int.class, yPos, originalPoint.getY()));
484 }
485 return p;
486 }
487
488 assert (type == String.class);
489 if (value.equals(""))
490 return null;
491 return fullCaseValue;
492 }
493
494 public static Object[] Convert(Method method, String value) {
495 return Convert(method, value, null);
496 }
497
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 */
511 public static Object[] Convert(Method method, String value, Object current) {
512
513 if (method == null) {
514 System.out.println("Error converting null method");
515 return null;
516 }
517
518 String name = method.getName();
519 Class[] types = method.getParameterTypes();
520
521 String fullValue = value;
522 value = value.trim();
523
524 if ((method.getParameterTypes()[0].isEnum()) || (name.matches("setPermission")) || (name.matches("setFrameEncryptionPermission")) || (name.matches("setEncryptionPermission"))) {
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;
535 }
536
537 if (name.endsWith("Arrow")) {
538 if (value.indexOf(" ") < 0)
539 return null;
540
541 Float length = null;
542 Double ratio = null;
543 Double nib_perc = null;
544
545 if (current == null) {
546 length = getArrowLength(value);
547 ratio = getArrowRatio(value);
548 nib_perc = getArrowNibPerc(value);
549 } else {
550 assert (current instanceof String);
551 float oldLength = getArrowLength(current.toString());
552 double oldRatio = getArrowRatio(current.toString());
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
574 }
575
576 Object[] vals = new Object[3];
577 vals[0] = length;
578 vals[1] = ratio;
579 vals[2] = nib_perc;
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
594 assert (types.length == 1);
595
596 Object o[] = new Object[1];
597 o[0] = Convert(types[0], fullValue, current);
598 return o;
599 }
600
601 private static float getArrowLength(String args123) {
602 return Float.parseFloat(args123.substring(0, args123.indexOf(" ")));
603 }
604
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());
615 }
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 }
632
633 public static Object ConvertToExpeditee(Method method, Object output) {
634 if (output == null)
635 return null;
636
637 assert (method != null);
638
639 String name = method.getName();
640
641 if (output instanceof List)
642 return output;
643
644 if (name.endsWith("Text")) {
645 List<String> list = new LinkedList<String>();
646 for (String s : output.toString().split("\n")) {
647 list.add(s);
648 }
649 return list;
650 }
651
652 if ((method.getReturnType().isEnum()) || (name.equals("getPermission")) || (name.equals("getFrameEncryptionPermission")) || (name.equals("getEncryptionPermission"))) {
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 }
661
662 // strings can be returned immediately
663 if (output instanceof String)
664 return (String) output;
665
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
678 if (output instanceof Integer)
679 return "" + output;
680
681 // floats can also be returned immediately
682 if (output instanceof Float) // && (Float) output >= 0) // Removed checking if >0, as some floats (e.g. letter spacing) can be negative
683 return "" + output;
684
685 // convert fonts
686 if (output instanceof Font)
687 return getExpediteeFontCode((Font) output);
688
689 // convert colors
690 if (output instanceof Colour)
691 return getExpediteeColorCode((Colour) output);
692
693 // convert points
694 if (output instanceof Point)
695 return ((Point) output).getX() + " " + ((Point) output).getY();
696
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 }
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 }
735
736 public static String getPdfFont(String family) {
737 family = family.toLowerCase();
738 if (family.equals(Text.FONT_WHEEL[0])) {
739 return FontFactory.HELVETICA;
740 } else if (family.equals(Text.FONT_WHEEL[2])) {
741 return FontFactory.TIMES_ROMAN;
742 }
743 return FontFactory.COURIER;
744 }
745
746 public static String getCssColor(Colour c) {
747 assert (c != null);
748 return "rgb(" + c.getRed255() + "," + c.getGreen255() + "," + c.getBlue255()
749 + ")";
750 }
751
752 public static String getCssFontFamily(String family) {
753 family = family.toLowerCase();
754 if (family.equals("monospaced") || family.equals("dialog")) {
755 return "courier";
756 } else if (family.equals("sansserif")) {
757 return "sans-serif";
758 } else {
759 return family;
760 }
761 }
762}
Note: See TracBrowser for help on using the repository browser.