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.

XMLObj.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.fo;
  8. // FOP
  9. import org.apache.fop.fo.*;
  10. import org.apache.fop.layout.FontState;
  11. import org.apache.fop.apps.FOPException;
  12. import org.w3c.dom.*;
  13. import org.xml.sax.Attributes;
  14. import javax.xml.parsers.DocumentBuilderFactory;
  15. import java.util.*;
  16. import java.awt.geom.Point2D;
  17. /**
  18. * Generic XML object.
  19. * This is used by xml objects (other than fo) than will build a DOM
  20. * with each element.
  21. */
  22. public abstract class XMLObj extends FONode {
  23. // temp reference for attributes
  24. Attributes attr = null;
  25. protected Element element;
  26. protected Document doc;
  27. /**
  28. *
  29. * @param parent the parent formatting object
  30. * @param propertyList the explicit properties of this object
  31. */
  32. public XMLObj(FONode parent) {
  33. super(parent);
  34. }
  35. public void setName(String str) {
  36. name = str;
  37. }
  38. public void handleAttrs(Attributes attlist) throws FOPException {
  39. attr = attlist;
  40. }
  41. public Document getDocument() {
  42. return doc;
  43. }
  44. public Point2D getDimension(Point2D view) {
  45. return null;
  46. }
  47. public abstract String getNameSpace();
  48. public String getDocumentNamespace() {
  49. return getNameSpace();
  50. }
  51. protected static HashMap ns = new HashMap();
  52. static {
  53. ns.put("xlink", "http://www.w3.org/1999/xlink");
  54. }
  55. public void addElement(Document doc, Element parent) {
  56. this.doc = doc;
  57. element = doc.createElementNS(getNameSpace(), name);
  58. for (int count = 0; count < attr.getLength(); count++) {
  59. String rf = attr.getValue(count);
  60. String qname = attr.getQName(count);
  61. int idx = qname.indexOf(":");
  62. if (idx == -1) {
  63. element.setAttribute(qname, rf);
  64. } else {
  65. String pref = qname.substring(0, idx);
  66. String tail = qname.substring(idx + 1);
  67. if (pref.equals("xmlns")) {
  68. ns.put(tail, rf);
  69. } else {
  70. element.setAttributeNS((String)ns.get(pref), tail, rf);
  71. }
  72. }
  73. }
  74. attr = null;
  75. parent.appendChild(element);
  76. }
  77. public void buildTopLevel(Document doc, Element svgRoot) {
  78. // build up the info for the top level element
  79. for (int count = 0; count < attr.getLength(); count++) {
  80. String rf = attr.getValue(count);
  81. String qname = attr.getQName(count);
  82. int idx = qname.indexOf(":");
  83. if (idx == -1) {
  84. element.setAttribute(qname, rf);
  85. } else {
  86. String pref = qname.substring(0, idx);
  87. String tail = qname.substring(idx + 1);
  88. if (pref.equals("xmlns")) {
  89. ns.put(tail, rf);
  90. } else {
  91. element.setAttributeNS((String)ns.get(pref), tail, rf);
  92. }
  93. }
  94. }
  95. }
  96. public Document createBasicDocument() {
  97. doc = null;
  98. element = null;
  99. try {
  100. DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
  101. fact.setNamespaceAware(true);
  102. doc = fact.newDocumentBuilder().newDocument();
  103. Element el = doc.createElement(name);
  104. doc.appendChild(el);
  105. element = doc.getDocumentElement();
  106. buildTopLevel(doc, element);
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. }
  110. return doc;
  111. }
  112. protected void addChild(FONode child) {
  113. if (child instanceof XMLObj) {
  114. ((XMLObj)child).addElement(doc, element);
  115. } else {
  116. // in theory someone might want to embed some defined
  117. // xml (eg. fo) inside the foreign xml
  118. // they could use a different namespace
  119. getLogger().debug("Invalid element: " + child.getName() + " inside foreign xml markup");
  120. }
  121. }
  122. protected void addCharacters(char data[], int start, int length) {
  123. String str = new String(data, start, length - start);
  124. org.w3c.dom.Text text = doc.createTextNode(str);
  125. element.appendChild(text);
  126. }
  127. }