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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fo;
  19. // Java
  20. import java.awt.geom.Point2D;
  21. import java.util.HashMap;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.Element;
  25. import org.xml.sax.Attributes;
  26. import org.xml.sax.Locator;
  27. import org.apache.fop.apps.FOPException;
  28. import org.apache.fop.datatypes.Length;
  29. import org.apache.fop.util.ContentHandlerFactory.ObjectBuiltListener;
  30. /**
  31. * Abstract class modelling generic, non-XSL-FO XML objects. Such objects are
  32. * stored in a DOM.
  33. */
  34. public abstract class XMLObj extends FONode implements ObjectBuiltListener {
  35. private static final String XMLNS_NAMESPACE_URI = "http://www.w3.org/2000/xmlns/";
  36. // temp reference for attributes
  37. private Attributes attr = null;
  38. /** DOM element representing this node */
  39. protected Element element;
  40. /** DOM document containing this node */
  41. protected Document doc;
  42. /** Name of the node */
  43. protected String name;
  44. /**
  45. * Base constructor
  46. *
  47. * @param parent {@link FONode} that is the parent of this object
  48. */
  49. public XMLObj(FONode parent) {
  50. super(parent);
  51. }
  52. /**
  53. * {@inheritDoc}
  54. * <br>Here, blocks XSL-FO's from having non-FO parents.
  55. */
  56. protected void validateChildNode(Locator loc, String nsURI, String localName)
  57. throws ValidationException {
  58. if (FO_URI.equals(nsURI)) {
  59. invalidChildError(loc, nsURI, localName);
  60. }
  61. }
  62. /** {@inheritDoc} */
  63. public void processNode(String elementName, Locator locator,
  64. Attributes attlist, PropertyList propertyList) throws FOPException {
  65. setLocator(locator);
  66. name = elementName;
  67. attr = attlist;
  68. }
  69. /**
  70. * @return DOM document representing this foreign XML
  71. */
  72. public Document getDOMDocument() {
  73. return doc;
  74. }
  75. /**
  76. * Returns the dimensions of the generated area in pts.
  77. *
  78. * @param view Point2D instance to receive the dimensions
  79. * @return the requested dimensions in pts.
  80. */
  81. public Point2D getDimension(Point2D view) {
  82. return null;
  83. }
  84. /**
  85. * Retrieve the intrinsic alignment-adjust of the child element.
  86. *
  87. * @return the intrinsic alignment-adjust.
  88. */
  89. public Length getIntrinsicAlignmentAdjust() {
  90. return null;
  91. }
  92. /** {@inheritDoc} */
  93. public String getLocalName() {
  94. return name;
  95. }
  96. private static HashMap ns = new HashMap();
  97. static {
  98. ns.put("xlink", "http://www.w3.org/1999/xlink");
  99. }
  100. /**
  101. * Add an element to the DOM document
  102. * @param doc DOM document to which to add an element
  103. * @param parent the parent element of the element that is being added
  104. */
  105. public void addElement(Document doc, Element parent) {
  106. this.doc = doc;
  107. element = doc.createElementNS(getNamespaceURI(), name);
  108. setAttributes(element, attr);
  109. attr = null;
  110. parent.appendChild(element);
  111. }
  112. private static void setAttributes(Element element, Attributes attr) {
  113. for (int count = 0; count < attr.getLength(); count++) {
  114. String rf = attr.getValue(count);
  115. String qname = attr.getQName(count);
  116. int idx = qname.indexOf(":");
  117. if (idx == -1) {
  118. element.setAttribute(qname, rf);
  119. } else {
  120. String pref = qname.substring(0, idx);
  121. String tail = qname.substring(idx + 1);
  122. if (pref.equals("xmlns")) {
  123. ns.put(tail, rf);
  124. } else {
  125. element.setAttributeNS((String)ns.get(pref), tail, rf);
  126. }
  127. }
  128. }
  129. }
  130. /**
  131. * Add the top-level element to the DOM document
  132. *
  133. * @param doc DOM document
  134. * @param svgRoot non-XSL-FO element to be added as the root of this document
  135. */
  136. public void buildTopLevel(Document doc, Element svgRoot) {
  137. // build up the info for the top level element
  138. setAttributes(element, attr);
  139. }
  140. /**
  141. * Create an empty DOM document
  142. *
  143. * @return DOM document
  144. */
  145. public Document createBasicDocument() {
  146. doc = null;
  147. element = null;
  148. try {
  149. DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
  150. fact.setNamespaceAware(true);
  151. doc = fact.newDocumentBuilder().newDocument();
  152. Element el = doc.createElementNS(getNamespaceURI(), name);
  153. doc.appendChild(el);
  154. element = doc.getDocumentElement();
  155. buildTopLevel(doc, element);
  156. if (!element.hasAttributeNS(XMLNS_NAMESPACE_URI, "xmlns")) {
  157. element.setAttributeNS(XMLNS_NAMESPACE_URI, "xmlns",
  158. getNamespaceURI());
  159. }
  160. } catch (Exception e) {
  161. //TODO this is ugly because there may be subsequent failures like NPEs
  162. log.error("Error while trying to instantiate a DOM Document", e);
  163. }
  164. return doc;
  165. }
  166. /** {@inheritDoc} */
  167. protected void addChildNode(FONode child) {
  168. if (child instanceof XMLObj) {
  169. ((XMLObj)child).addElement(doc, element);
  170. } else {
  171. // in theory someone might want to embed some defined
  172. // xml (eg. fo) inside the foreign xml
  173. // they could use a different namespace
  174. log.debug("Invalid element: " + child.getName() + " inside foreign xml markup");
  175. }
  176. }
  177. /** {@inheritDoc} */
  178. protected void characters(char[] data, int start, int length,
  179. PropertyList pList, Locator locator) throws FOPException {
  180. super.characters(data, start, length, pList, locator);
  181. String str = new String(data, start, length);
  182. org.w3c.dom.Text text = doc.createTextNode(str);
  183. element.appendChild(text);
  184. }
  185. /** {@inheritDoc} */
  186. public void notifyObjectBuilt(Object obj) {
  187. this.doc = (Document)obj;
  188. this.element = this.doc.getDocumentElement();
  189. }
  190. }