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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.area;
  18. import org.apache.fop.datatypes.ColorType;
  19. import org.apache.fop.traits.BorderProps;
  20. import java.io.Serializable;
  21. import java.util.Map;
  22. import java.util.HashMap;
  23. import java.util.Iterator;
  24. // properties should be serialized by the holder
  25. /**
  26. * Area traits used for rendering.
  27. * This class represents an area trait that specifies a value for rendering.
  28. */
  29. public class Trait implements Serializable {
  30. /**
  31. * Id reference line, not resolved.
  32. * not sure if this is needed.
  33. */
  34. public static final Integer ID_LINK = new Integer(0);
  35. /**
  36. * Internal link trait.
  37. * This is resolved and provides a link to an internal area.
  38. */
  39. public static final Integer INTERNAL_LINK = new Integer(1); //resolved
  40. /**
  41. * External link. A URL link to an external resource.
  42. */
  43. public static final Integer EXTERNAL_LINK = new Integer(2);
  44. /**
  45. * The font name from the font setup.
  46. */
  47. public static final Integer FONT_NAME = new Integer(3);
  48. /**
  49. * Font size for the current font.
  50. */
  51. public static final Integer FONT_SIZE = new Integer(4);
  52. /**
  53. * The current colour.
  54. */
  55. public static final Integer COLOR = new Integer(7);
  56. /**
  57. * Don't think this is necessary.
  58. */
  59. public static final Integer ID_AREA = new Integer(8);
  60. /**
  61. * Background trait for an area.
  62. */
  63. public static final Integer BACKGROUND = new Integer(9);
  64. /**
  65. * Underline trait used when rendering inline parent.
  66. */
  67. public static final Integer UNDERLINE = new Integer(10);
  68. /**
  69. * Overline trait used when rendering inline parent.
  70. */
  71. public static final Integer OVERLINE = new Integer(11);
  72. /**
  73. * Linethrough trait used when rendering inline parent.
  74. */
  75. public static final Integer LINETHROUGH = new Integer(12);
  76. /**
  77. * Shadow offset.
  78. */
  79. public static final Integer OFFSET = new Integer(13);
  80. /**
  81. * The shadow for text.
  82. */
  83. public static final Integer SHADOW = new Integer(14);
  84. /**
  85. * The border start.
  86. */
  87. public static final Integer BORDER_START = new Integer(15);
  88. /**
  89. * The border end.
  90. */
  91. public static final Integer BORDER_END = new Integer(16);
  92. /**
  93. * The border before.
  94. */
  95. public static final Integer BORDER_BEFORE = new Integer(17);
  96. /**
  97. * The border after.
  98. */
  99. public static final Integer BORDER_AFTER = new Integer(18);
  100. /**
  101. * The padding start.
  102. */
  103. public static final Integer PADDING_START = new Integer(19);
  104. /**
  105. * The padding end.
  106. */
  107. public static final Integer PADDING_END = new Integer(20);
  108. /**
  109. * The padding before.
  110. */
  111. public static final Integer PADDING_BEFORE = new Integer(21);
  112. /**
  113. * The padding after.
  114. */
  115. public static final Integer PADDING_AFTER = new Integer(22);
  116. /**
  117. * The space start.
  118. */
  119. public static final Integer SPACE_START = new Integer(23);
  120. /**
  121. * The space end.
  122. */
  123. public static final Integer SPACE_END = new Integer(24);
  124. private static final Map TRAIT_INFO = new HashMap();
  125. private static class TraitInfo {
  126. private String name;
  127. private Class clazz; // Class of trait data
  128. public TraitInfo(String name, Class clazz) {
  129. this.name = name;
  130. this.clazz = clazz;
  131. }
  132. public String getName() {
  133. return this.name;
  134. }
  135. public Class getClazz() {
  136. return this.clazz;
  137. }
  138. }
  139. static {
  140. // Create a hashmap mapping trait code to name for external representation
  141. TRAIT_INFO.put(ID_LINK, new TraitInfo("id-link", String.class));
  142. TRAIT_INFO.put(INTERNAL_LINK,
  143. new TraitInfo("internal-link", String.class));
  144. TRAIT_INFO.put(EXTERNAL_LINK,
  145. new TraitInfo("external-link", String.class));
  146. TRAIT_INFO.put(FONT_NAME,
  147. new TraitInfo("font-family", String.class));
  148. TRAIT_INFO.put(FONT_SIZE,
  149. new TraitInfo("font-size", Integer.class));
  150. TRAIT_INFO.put(COLOR, new TraitInfo("color", String.class));
  151. TRAIT_INFO.put(ID_AREA, new TraitInfo("id-area", String.class));
  152. TRAIT_INFO.put(BACKGROUND,
  153. new TraitInfo("background", Background.class));
  154. TRAIT_INFO.put(UNDERLINE,
  155. new TraitInfo("underline", Boolean.class));
  156. TRAIT_INFO.put(OVERLINE,
  157. new TraitInfo("overline", Boolean.class));
  158. TRAIT_INFO.put(LINETHROUGH,
  159. new TraitInfo("linethrough", Boolean.class));
  160. TRAIT_INFO.put(OFFSET, new TraitInfo("offset", Integer.class));
  161. TRAIT_INFO.put(SHADOW, new TraitInfo("shadow", Integer.class));
  162. TRAIT_INFO.put(BORDER_START,
  163. new TraitInfo("border-start", BorderProps.class));
  164. TRAIT_INFO.put(BORDER_END,
  165. new TraitInfo("border-end", BorderProps.class));
  166. TRAIT_INFO.put(BORDER_BEFORE,
  167. new TraitInfo("border-before", BorderProps.class));
  168. TRAIT_INFO.put(BORDER_AFTER,
  169. new TraitInfo("border-after", BorderProps.class));
  170. TRAIT_INFO.put(PADDING_START,
  171. new TraitInfo("padding-start", Integer.class));
  172. TRAIT_INFO.put(PADDING_END,
  173. new TraitInfo("padding-end", Integer.class));
  174. TRAIT_INFO.put(PADDING_BEFORE,
  175. new TraitInfo("padding-before", Integer.class));
  176. TRAIT_INFO.put(PADDING_AFTER,
  177. new TraitInfo("padding-after", Integer.class));
  178. TRAIT_INFO.put(SPACE_START,
  179. new TraitInfo("space-start", Integer.class));
  180. TRAIT_INFO.put(SPACE_END,
  181. new TraitInfo("space-end", Integer.class));
  182. }
  183. /**
  184. * Get the trait name for a trait code.
  185. *
  186. * @param traitCode the trait code to get the name for
  187. * @return the trait name
  188. */
  189. public static String getTraitName(Object traitCode) {
  190. Object obj = TRAIT_INFO.get(traitCode);
  191. if (obj != null) {
  192. return ((TraitInfo) obj).getName();
  193. } else {
  194. return "unknown-trait-" + traitCode.toString();
  195. }
  196. }
  197. /**
  198. * Get the trait code for a trait name.
  199. *
  200. * @param sTraitName the name of the trait to find
  201. * @return the trait code object
  202. */
  203. public static Object getTraitCode(String sTraitName) {
  204. Iterator iter = TRAIT_INFO.entrySet().iterator();
  205. while (iter.hasNext()) {
  206. Map.Entry entry = (Map.Entry) iter.next();
  207. TraitInfo ti = (TraitInfo) entry.getValue();
  208. if (ti != null && ti.getName().equals(sTraitName)) {
  209. return entry.getKey();
  210. }
  211. }
  212. return null;
  213. }
  214. /**
  215. * Get the data storage class for the trait.
  216. *
  217. * @param oTraitCode the trait code to lookup
  218. * @return the class type for the trait
  219. */
  220. private static Class getTraitClass(Object oTraitCode) {
  221. TraitInfo ti = (TraitInfo) TRAIT_INFO.get(oTraitCode);
  222. return (ti != null ? ti.getClazz() : null);
  223. }
  224. /**
  225. * The type of trait for an area.
  226. */
  227. private Object propType;
  228. /**
  229. * The data value of the trait.
  230. */
  231. private Object data;
  232. /**
  233. * Create a new empty trait.
  234. */
  235. public Trait() {
  236. this.propType = null;
  237. this.data = null;
  238. }
  239. /**
  240. * Create a trait with the value and type.
  241. *
  242. * @param propType the type of trait
  243. * @param data the data value
  244. */
  245. public Trait(Object propType, Object data) {
  246. this.propType = propType;
  247. this.data = data;
  248. }
  249. /**
  250. * Returns the trait data value.
  251. * @return the trait data value
  252. */
  253. public Object getData() {
  254. return this.data;
  255. }
  256. /**
  257. * Returns the property type.
  258. * @return the property type
  259. */
  260. public Object getPropType() {
  261. return this.propType;
  262. }
  263. /**
  264. * Return the string for debugging.
  265. * @see java.lang.Object#toString()
  266. */
  267. public String toString() {
  268. return data.toString();
  269. }
  270. /**
  271. * Make a trait value.
  272. *
  273. * @param oCode trait code
  274. * @param sTraitValue trait value as String
  275. * @return the trait value as object
  276. */
  277. public static Object makeTraitValue(Object oCode, String sTraitValue) {
  278. // Get the code from the name
  279. // See what type of object it is
  280. // Convert string value to an object of that type
  281. Class tclass = getTraitClass(oCode);
  282. if (tclass == null) {
  283. return null;
  284. }
  285. if (tclass.equals(String.class)) {
  286. return sTraitValue;
  287. }
  288. if (tclass.equals(Integer.class)) {
  289. return new Integer(sTraitValue);
  290. }
  291. // See if the class has a constructor from string or can read from a string
  292. try {
  293. Object o = tclass.newInstance();
  294. //return o.fromString(sTraitValue);
  295. } catch (IllegalAccessException e1) {
  296. System.err.println("Can't create instance of "
  297. + tclass.getName());
  298. return null;
  299. } catch (InstantiationException e2) {
  300. System.err.println("Can't create instance of "
  301. + tclass.getName());
  302. return null;
  303. }
  304. return null;
  305. }
  306. /**
  307. * Background trait structure.
  308. * Used for storing back trait information which are related.
  309. */
  310. public static class Background implements Serializable {
  311. /** The background color if any. */
  312. private ColorType color = null;
  313. /** The background image url if any. */
  314. private String url = null;
  315. /** Background repeat enum for images. */
  316. private int repeat;
  317. /** Background horizontal offset for images. */
  318. private int horiz;
  319. /** Background vertical offset for images. */
  320. private int vertical;
  321. /**
  322. * Returns the background color.
  323. * @return background color, null if n/a
  324. */
  325. public ColorType getColor() {
  326. return color;
  327. }
  328. /**
  329. * Returns the horizontal offset for images.
  330. * @return the horizontal offset
  331. */
  332. public int getHoriz() {
  333. return horiz;
  334. }
  335. /**
  336. * Returns the image repetition behaviour for images.
  337. * @return the image repetition behaviour
  338. */
  339. public int getRepeat() {
  340. return repeat;
  341. }
  342. /**
  343. * Returns the URL to the background image
  344. * @return URL to the background image, null if n/a
  345. */
  346. public String getURL() {
  347. return url;
  348. }
  349. /**
  350. * Returns the vertical offset for images.
  351. * @return the vertical offset
  352. */
  353. public int getVertical() {
  354. return vertical;
  355. }
  356. /**
  357. * Sets the color.
  358. * @param color The color to set
  359. */
  360. public void setColor(ColorType color) {
  361. this.color = color;
  362. }
  363. /**
  364. * Sets the horizontal offset.
  365. * @param horiz The horizontal offset to set
  366. */
  367. public void setHoriz(int horiz) {
  368. this.horiz = horiz;
  369. }
  370. /**
  371. * Sets the image repetition behaviour for images.
  372. * @param repeat The image repetition behaviour to set
  373. */
  374. public void setRepeat(int repeat) {
  375. this.repeat = repeat;
  376. }
  377. /**
  378. * Sets the URL to the background image.
  379. * @param url The URL to set
  380. */
  381. public void setURL(String url) {
  382. this.url = url;
  383. }
  384. /**
  385. * Sets the vertical offset for images.
  386. * @param vertical The vertical offset to set
  387. */
  388. public void setVertical(int vertical) {
  389. this.vertical = vertical;
  390. }
  391. /**
  392. * Return the string for debugging.
  393. * @see java.lang.Object#toString()
  394. */
  395. public String toString() {
  396. StringBuffer sb = new StringBuffer();
  397. sb.append("color=" + color);
  398. if (url != null) {
  399. sb.append(",url=");
  400. sb.append(url);
  401. }
  402. sb.append(",repeat=" + repeat);
  403. sb.append(",horiz=" + horiz);
  404. sb.append(",vertical=" + vertical);
  405. return sb.toString();
  406. }
  407. }
  408. }