KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > XMLObj


1 /*
2  * $Id: XMLObj.java,v 1.2.2.7 2003/04/11 00:24:37 pietsch Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.fo;
52
53 // FOP
54
import org.apache.fop.layout.Area;
55 import org.apache.fop.apps.FOPException;
56 import org.apache.fop.layout.LinkSet;
57 import org.apache.fop.datatypes.IDReferences;
58
59 import org.w3c.dom.*;
60 import org.xml.sax.Attributes JavaDoc;
61
62 import java.util.ArrayList JavaDoc;
63 import java.util.HashMap JavaDoc;
64
65 /**
66  * Since SVG objects are not layed out then this class checks
67  * that this element is not being layed out inside some incorrect
68  * element.
69  */

70 public abstract class XMLObj extends FObj {
71
72     protected String JavaDoc tagName;
73
74     protected Element element;
75     protected Document doc;
76
77     /**
78      *
79      * @param parent the parent formatting object
80      * @param propertyList the explicit properties of this object
81      */

82     public XMLObj(FObj parent, PropertyList propertyList, String JavaDoc tag,
83                   String JavaDoc systemId, int line, int column) {
84         super(parent, propertyList, systemId, line, column);
85         tagName = tag;
86     }
87
88     public abstract String JavaDoc getNameSpace();
89
90     protected static HashMap JavaDoc ns = new HashMap JavaDoc();
91
92     public void addGraphic(Document doc, Element parent) {
93         this.doc = doc;
94         element = doc.createElementNS(getNameSpace(), tagName);
95
96         if(this.properties instanceof DirectPropertyListBuilder.AttrPropertyList) {
97             Attributes JavaDoc attr = ((DirectPropertyListBuilder.AttrPropertyList)this.properties).getAttributes();
98             for (int count = 0; count < attr.getLength(); count++) {
99                 String JavaDoc rf = attr.getValue(count);
100                 String JavaDoc qname = attr.getQName(count);
101                 if (qname.indexOf(":") == -1) {
102                     element.setAttribute(qname, rf);
103                 } else {
104                     String JavaDoc pref =
105                         qname.substring(0, qname.indexOf(":"));
106                     if (pref.equals("xmlns")) {
107                         ns.put(qname.substring(qname.indexOf(":")
108                                                       + 1), rf);
109                     }
110                     ns.put("xlink", "http://www.w3.org/1999/xlink");
111                     element.setAttributeNS((String JavaDoc)ns.get(pref),
112                                            qname, rf);
113                 }
114             }
115         } else {
116         }
117
118         parent.appendChild(element);
119     }
120
121     public void buildTopLevel(Document doc, Element svgRoot) {
122         // build up the info for the top level element
123
if(this.properties instanceof DirectPropertyListBuilder.AttrPropertyList) {
124             Attributes JavaDoc attr = ((DirectPropertyListBuilder.AttrPropertyList)this.properties).getAttributes();
125             for (int count = 0; count < attr.getLength(); count++) {
126                 String JavaDoc rf = attr.getValue(count);
127                 String JavaDoc qname = attr.getQName(count);
128                 if (qname.indexOf(":") == -1) {
129                     element.setAttribute(qname, rf);
130                 } else {
131                     String JavaDoc pref =
132                        qname.substring(0, qname.indexOf(":"));
133                     if (pref.equals("xmlns")) {
134                         ns.put(qname.substring(qname.indexOf(":")
135                                                       + 1), rf);
136                     }
137                     ns.put("xlink", "http://www.w3.org/1999/xlink");
138                     element.setAttributeNS((String JavaDoc)ns.get(pref),
139                                            qname, rf);
140                 }
141             }
142         } else {
143         }
144     }
145
146     public Document createBasicDocument() {
147         doc = null;
148
149         element = null;
150         try {
151             // DOMImplementation impl = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
152
// String ns = GraphElementMapping.URI;
153
// doc = impl.createDocument(ns, "graph", null);
154
doc =
155                 javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
156             Element el = doc.createElement("graph");
157             doc.appendChild(el);
158
159             element = doc.getDocumentElement();
160             buildTopLevel(doc, element);
161         } catch (Exception JavaDoc e) {
162             e.printStackTrace();
163         }
164         return doc;
165     }
166
167     protected void addChild(FONode child) {
168         if (child instanceof XMLObj) {
169             ((XMLObj)child).addGraphic(doc, element);
170         }
171     }
172
173     protected void addCharacters(char data[], int start, int length) {
174         String JavaDoc str = new String JavaDoc(data, start, length);
175         org.w3c.dom.Text JavaDoc text = doc.createTextNode(str);
176         element.appendChild(text);
177     }
178
179     /**
180      * layout this formatting object.
181      *
182      * @param area the area to layout the object into
183      * @return the status of the layout
184      */

185     public int layout(Area area) throws FOPException {
186         /* generate a warning */
187         log.error("" + this.tagName + " outside foreign xml");
188
189         /* return status */
190         return Status.OK;
191     }
192
193     public void removeID(IDReferences idReferences) {}
194
195     /**
196      * These method overrides prevent problems with the different types.
197      */

198     public void setIsInTableCell() {}
199
200     public void forceStartOffset(int offset) {}
201
202     public void forceWidth(int width) {}
203
204     public void resetMarker() {}
205
206     public void setLinkSet(LinkSet linkSet) {}
207
208     public ArrayList JavaDoc getMarkerSnapshot(ArrayList JavaDoc snapshot) {
209         return snapshot;
210     }
211
212     public void rollback(ArrayList JavaDoc snapshot) {}
213
214     protected void setWritingMode() {}
215 }
216
217
Popular Tags