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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. import java.awt.geom.Point2D;
  20. import java.util.HashMap;
  21. import javax.xml.parsers.DocumentBuilderFactory;
  22. import org.w3c.dom.Document;
  23. import org.w3c.dom.Element;
  24. import org.xml.sax.Attributes;
  25. import org.xml.sax.Locator;
  26. import org.apache.fop.apps.FOPException;
  27. import org.apache.fop.datatypes.Length;
  28. import org.apache.fop.util.ContentHandlerFactory.ObjectBuiltListener;
  29. import org.apache.fop.util.XMLConstants;
  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. // temp reference for attributes
  36. private Attributes attr;
  37. /** DOM element representing this node */
  38. protected Element element;
  39. /** DOM document containing this node */
  40. protected Document doc;
  41. /** Name of the node */
  42. protected String name;
  43. /**
  44. * Base constructor
  45. *
  46. * @param parent {@link FONode} that is the parent of this object
  47. */
  48. public XMLObj(FONode parent) {
  49. super(parent);
  50. }
  51. /**
  52. * {@inheritDoc}
  53. * <br>Here, blocks XSL-FO's from having non-FO parents.
  54. */
  55. protected void validateChildNode(Locator loc, String nsURI, String localName)
  56. throws ValidationException {
  57. if (FO_URI.equals(nsURI)) {
  58. invalidChildError(loc, nsURI, localName);
  59. }
  60. }
  61. /** {@inheritDoc} */
  62. public void processNode(String elementName, Locator locator,
  63. Attributes attlist, PropertyList propertyList) throws FOPException {
  64. setLocator(locator);
  65. name = elementName;
  66. attr = attlist;
  67. }
  68. /**
  69. * @return DOM document representing this foreign XML
  70. */
  71. public Document getDOMDocument() {
  72. return doc;
  73. }
  74. /**
  75. * Returns the dimensions of the generated area in pts.
  76. *
  77. * @param view Point2D instance to receive the dimensions
  78. * @return the requested dimensions in pts.
  79. */
  80. public Point2D getDimension(Point2D view) {
  81. return null;
  82. }
  83. /**
  84. * Retrieve the intrinsic alignment-adjust of the child element.
  85. *
  86. * @return the intrinsic alignment-adjust.
  87. */
  88. public Length getIntrinsicAlignmentAdjust() {
  89. return null;
  90. }
  91. /** {@inheritDoc} */
  92. public String getLocalName() {
  93. return name;
  94. }
  95. private static HashMap ns = new HashMap();
  96. static {
  97. ns.put(XMLConstants.XLINK_PREFIX, XMLConstants.XLINK_NAMESPACE);
  98. }
  99. /**
  100. * Add an element to the DOM document
  101. * @param doc DOM document to which to add an element
  102. * @param parent the parent element of the element that is being added
  103. */
  104. public void addElement(Document doc, Element parent) {
  105. this.doc = doc;
  106. element = doc.createElementNS(getNamespaceURI(), name);
  107. setAttributes(element, attr);
  108. attr = null;
  109. parent.appendChild(element);
  110. }
  111. private static void setAttributes(Element element, Attributes attr) {
  112. for (int count = 0; count < attr.getLength(); count++) {
  113. String rf = attr.getValue(count);
  114. String qname = attr.getQName(count);
  115. int idx = qname.indexOf(":");
  116. if (idx == -1) {
  117. element.setAttribute(qname, rf);
  118. } else {
  119. String pref = qname.substring(0, idx);
  120. String tail = qname.substring(idx + 1);
  121. if (pref.equals(XMLConstants.XMLNS_PREFIX)) {
  122. ns.put(tail, rf);
  123. } else {
  124. element.setAttributeNS((String)ns.get(pref), tail, rf);
  125. }
  126. }
  127. }
  128. }
  129. /**
  130. * Add the top-level element to the DOM document
  131. *
  132. * @param doc DOM document
  133. * @param svgRoot non-XSL-FO element to be added as the root of this document
  134. */
  135. public void buildTopLevel(Document doc, Element svgRoot) {
  136. // build up the info for the top level element
  137. setAttributes(element, attr);
  138. }
  139. /**
  140. * Create an empty DOM document
  141. *
  142. * @return DOM document
  143. */
  144. public Document createBasicDocument() {
  145. doc = null;
  146. element = null;
  147. try {
  148. DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
  149. fact.setNamespaceAware(true);
  150. doc = fact.newDocumentBuilder().newDocument();
  151. Element el = doc.createElementNS(getNamespaceURI(), name);
  152. doc.appendChild(el);
  153. element = doc.getDocumentElement();
  154. buildTopLevel(doc, element);
  155. if (!element.hasAttributeNS(
  156. XMLConstants.XMLNS_NAMESPACE_URI, XMLConstants.XMLNS_PREFIX)) {
  157. element.setAttributeNS(XMLConstants.XMLNS_NAMESPACE_URI, XMLConstants.XMLNS_PREFIX,
  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. }