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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.fo;
  18. // Java
  19. import java.awt.geom.Point2D;
  20. import java.util.HashMap;
  21. import org.w3c.dom.Document;
  22. import org.w3c.dom.Element;
  23. import org.xml.sax.Attributes;
  24. import org.xml.sax.Locator;
  25. import javax.xml.parsers.DocumentBuilderFactory;
  26. // FOP
  27. import org.apache.fop.apps.FOPException;
  28. /**
  29. * Abstract class modelling generic, non-XSL-FO XML objects. Such objects are
  30. * stored in a DOM.
  31. */
  32. public abstract class XMLObj extends FONode {
  33. // temp reference for attributes
  34. private Attributes attr = null;
  35. /** DOM element representing this node */
  36. protected Element element;
  37. /** DOM document containing this node */
  38. protected Document doc;
  39. /**
  40. *
  41. * @param parent the parent formatting object
  42. */
  43. public XMLObj(FONode parent) {
  44. super(parent);
  45. }
  46. /**
  47. * @param str name of the element
  48. */
  49. public void setName(String str) {
  50. name = str;
  51. }
  52. /**
  53. * Store the attributes for this element
  54. * @param attlist Collection of attributes passed to us from the parser.
  55. * @throws FOPException for errors in the attributes
  56. */
  57. public void handleAttrs(Attributes attlist) throws FOPException {
  58. attr = attlist;
  59. }
  60. /**
  61. * @return DOM document representing this foreign XML
  62. */
  63. public Document getDOMDocument() {
  64. return doc;
  65. }
  66. public Point2D getDimension(Point2D view) {
  67. return null;
  68. }
  69. /**
  70. * @return string containing the namespace for this node
  71. */
  72. public abstract String getNameSpace();
  73. /**
  74. * @return string containing the namespace for this document (which is the
  75. * same namespace as for this node ??)
  76. */
  77. public String getDocumentNamespace() {
  78. return getNameSpace();
  79. }
  80. private static HashMap ns = new HashMap();
  81. static {
  82. ns.put("xlink", "http://www.w3.org/1999/xlink");
  83. }
  84. /**
  85. * Add an element to the DOM document
  86. * @param doc DOM document to which to add an element
  87. * @param parent the parent element of the element that is being added
  88. */
  89. public void addElement(Document doc, Element parent) {
  90. this.doc = doc;
  91. element = doc.createElementNS(getNameSpace(), name);
  92. for (int count = 0; count < attr.getLength(); count++) {
  93. String rf = attr.getValue(count);
  94. String qname = attr.getQName(count);
  95. int idx = qname.indexOf(":");
  96. if (idx == -1) {
  97. element.setAttribute(qname, rf);
  98. } else {
  99. String pref = qname.substring(0, idx);
  100. String tail = qname.substring(idx + 1);
  101. if (pref.equals("xmlns")) {
  102. ns.put(tail, rf);
  103. } else {
  104. element.setAttributeNS((String)ns.get(pref), tail, rf);
  105. }
  106. }
  107. }
  108. attr = null;
  109. parent.appendChild(element);
  110. }
  111. /**
  112. * Add the top-level element to the DOM document
  113. * @param doc DOM document
  114. * @param svgRoot non-XSL-FO element to be added as the root of this document
  115. */
  116. public void buildTopLevel(Document doc, Element svgRoot) {
  117. // build up the info for the top level element
  118. for (int count = 0; count < attr.getLength(); count++) {
  119. String rf = attr.getValue(count);
  120. String qname = attr.getQName(count);
  121. int idx = qname.indexOf(":");
  122. if (idx == -1) {
  123. element.setAttribute(qname, rf);
  124. } else {
  125. String pref = qname.substring(0, idx);
  126. String tail = qname.substring(idx + 1);
  127. if (pref.equals("xmlns")) {
  128. ns.put(tail, rf);
  129. } else {
  130. element.setAttributeNS((String)ns.get(pref), tail, rf);
  131. }
  132. }
  133. }
  134. }
  135. /**
  136. * Create an empty DOM document
  137. * @return DOM document
  138. */
  139. public Document createBasicDocument() {
  140. doc = null;
  141. element = null;
  142. try {
  143. DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
  144. fact.setNamespaceAware(true);
  145. doc = fact.newDocumentBuilder().newDocument();
  146. Element el = doc.createElement(name);
  147. doc.appendChild(el);
  148. element = doc.getDocumentElement();
  149. buildTopLevel(doc, element);
  150. } catch (Exception e) {
  151. e.printStackTrace();
  152. }
  153. return doc;
  154. }
  155. /**
  156. * @param child FONode child that should be added to this node
  157. */
  158. protected void addChild(FONode child) {
  159. if (child instanceof XMLObj) {
  160. ((XMLObj)child).addElement(doc, element);
  161. } else {
  162. // in theory someone might want to embed some defined
  163. // xml (eg. fo) inside the foreign xml
  164. // they could use a different namespace
  165. getLogger().debug("Invalid element: " + child.getName() + " inside foreign xml markup");
  166. }
  167. }
  168. /**
  169. * Add parsed characters to this object
  170. * @param data array of characters contaning the text to add
  171. * @param start starting array element to add
  172. * @param length number of characters from the array to add
  173. * @param locator location in fo source file.
  174. */
  175. protected void addCharacters(char data[], int start, int length,
  176. Locator locator) {
  177. String str = new String(data, start, length - start);
  178. org.w3c.dom.Text text = doc.createTextNode(str);
  179. element.appendChild(text);
  180. }
  181. /**
  182. * This is a hook for an FOTreeVisitor subclass to be able to access
  183. * this object.
  184. * @param fotv the FOTreeVisitor subclass that can access this object.
  185. * @see org.apache.fop.fo.FOTreeVisitor
  186. */
  187. public void acceptVisitor(FOTreeVisitor fotv) {
  188. fotv.serveXMLObj(this);
  189. }
  190. }