You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Trait.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.area;
  8. import org.apache.fop.datatypes.ColorType;
  9. import org.apache.fop.traits.BorderProps;
  10. import org.apache.fop.layout.FontState;
  11. import java.io.Serializable;
  12. import java.util.Map;
  13. import java.util.HashMap;
  14. import java.util.Iterator;
  15. // properties should be serialized by the holder
  16. public class Trait implements Serializable {
  17. public static final Integer ID_LINK = new Integer(0);
  18. public static final Integer INTERNAL_LINK = new Integer(1); //resolved
  19. public static final Integer EXTERNAL_LINK = new Integer(2);
  20. public static final Integer FONT_NAME = new Integer(3);
  21. public static final Integer FONT_SIZE = new Integer(4);
  22. public static final Integer COLOR = new Integer(7);
  23. public static final Integer ID_AREA = new Integer(8);
  24. public static final Integer BACKGROUND = new Integer(9);
  25. public static final Integer UNDERLINE = new Integer(10);
  26. public static final Integer OVERLINE = new Integer(11);
  27. public static final Integer LINETHROUGH = new Integer(12);
  28. public static final Integer OFFSET = new Integer(13);
  29. public static final Integer SHADOW = new Integer(14);
  30. public static final Integer BORDER_START = new Integer(15);
  31. public static final Integer BORDER_END = new Integer(16);
  32. public static final Integer BORDER_BEFORE = new Integer(17);
  33. public static final Integer BORDER_AFTER = new Integer(18);
  34. public static final Integer PADDING_START = new Integer(19);
  35. public static final Integer PADDING_END = new Integer(20);
  36. public static final Integer PADDING_BEFORE = new Integer(21);
  37. public static final Integer PADDING_AFTER = new Integer(22);
  38. static HashMap s_hmTraitInfo;
  39. private static class TraitInfo {
  40. String sName;
  41. Class sClass; // Class of trait data
  42. TraitInfo(String sName, Class sClass) {
  43. this.sName = sName;
  44. this.sClass = sClass;
  45. }
  46. }
  47. static {
  48. // Create a hashmap mapping trait code to name for external representation
  49. s_hmTraitInfo = new HashMap();
  50. s_hmTraitInfo.put(ID_LINK,
  51. new TraitInfo("id-link", String.class));
  52. s_hmTraitInfo.put(INTERNAL_LINK,
  53. new TraitInfo("internal-link", PageViewport.class));
  54. s_hmTraitInfo.put(EXTERNAL_LINK,
  55. new TraitInfo("external-link", String.class));
  56. s_hmTraitInfo.put(FONT_NAME,
  57. new TraitInfo("font-family", String.class));
  58. s_hmTraitInfo.put(FONT_SIZE,
  59. new TraitInfo("font-size", Integer.class));
  60. s_hmTraitInfo.put(COLOR,
  61. new TraitInfo("color", String.class));
  62. s_hmTraitInfo.put(ID_AREA,
  63. new TraitInfo("id-area", String.class));
  64. s_hmTraitInfo.put(BACKGROUND,
  65. new TraitInfo("background", String.class));
  66. s_hmTraitInfo.put(UNDERLINE,
  67. new TraitInfo("underline", Integer.class));
  68. s_hmTraitInfo.put(OVERLINE,
  69. new TraitInfo("overline", Integer.class));
  70. s_hmTraitInfo.put(LINETHROUGH,
  71. new TraitInfo("linethrough", Integer.class));
  72. s_hmTraitInfo.put(OFFSET,
  73. new TraitInfo("offset", Integer.class));
  74. s_hmTraitInfo.put(SHADOW,
  75. new TraitInfo("shadow", Integer.class));
  76. s_hmTraitInfo.put(BORDER_START,
  77. new TraitInfo("border-start", BorderProps.class));
  78. s_hmTraitInfo.put(BORDER_END,
  79. new TraitInfo("border-end", BorderProps.class));
  80. s_hmTraitInfo.put(BORDER_BEFORE,
  81. new TraitInfo("border-before", BorderProps.class));
  82. s_hmTraitInfo.put(BORDER_AFTER,
  83. new TraitInfo("border-after", BorderProps.class));
  84. s_hmTraitInfo.put(PADDING_START,
  85. new TraitInfo("padding-start", Integer.class));
  86. s_hmTraitInfo.put(PADDING_END,
  87. new TraitInfo("padding-end", Integer.class));
  88. s_hmTraitInfo.put(PADDING_BEFORE,
  89. new TraitInfo("padding-before", Integer.class));
  90. s_hmTraitInfo.put(PADDING_AFTER,
  91. new TraitInfo("padding-after", Integer.class));
  92. }
  93. public static String getTraitName(Object traitCode) {
  94. Object obj = s_hmTraitInfo.get(traitCode);
  95. if (obj != null) {
  96. return ((TraitInfo)obj).sName;
  97. }
  98. else {
  99. return "unknown-trait-" + traitCode.toString();
  100. }
  101. }
  102. public static Object getTraitCode(String sTraitName) {
  103. Iterator iter = s_hmTraitInfo.entrySet().iterator();
  104. while (iter.hasNext()) {
  105. Map.Entry entry = (Map.Entry)iter.next();
  106. TraitInfo ti = (TraitInfo)entry.getValue();
  107. if (ti != null && ti.sName.equals(sTraitName)) {
  108. return entry.getKey();
  109. }
  110. }
  111. return null;
  112. }
  113. private static Class getTraitClass(Object oTraitCode) {
  114. TraitInfo ti = (TraitInfo)s_hmTraitInfo.get(oTraitCode);
  115. return (ti != null? ti.sClass : null);
  116. }
  117. public Object propType;
  118. public Object data;
  119. public Trait() {
  120. this.propType = null;
  121. this.data = null;
  122. }
  123. public Trait(Object propType, Object data) {
  124. this.propType = propType;
  125. this.data = data;
  126. }
  127. public String toString() {
  128. return data.toString();
  129. }
  130. public static Object makeTraitValue(Object oCode, String sTraitValue) {
  131. // Get the code from the name
  132. // See what type of object it is
  133. // Convert string value to an object of that type
  134. Class tclass = getTraitClass(oCode);
  135. if (tclass == null) return null;
  136. if (tclass.equals(String.class)) {
  137. return sTraitValue;
  138. }
  139. if (tclass.equals(Integer.class)) {
  140. return new Integer(sTraitValue);
  141. }
  142. // See if the class has a constructor from string or can read from a string
  143. try {
  144. Object o = tclass.newInstance();
  145. //return o.fromString(sTraitValue);
  146. } catch (IllegalAccessException e1) {
  147. System.err.println("Can't create instance of " + tclass.getName());
  148. return null;
  149. } catch (InstantiationException e2) {
  150. System.err.println("Can't create instance of " + tclass.getName());
  151. return null;
  152. }
  153. return null;
  154. }
  155. public static class Background {
  156. ColorType color;
  157. String url;
  158. int repeat;
  159. int horiz;
  160. int vertical;
  161. }
  162. }