1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.converter.xml;
25 
26 import org.w3c.dom.Node;
27 import org.w3c.dom.NamedNodeMap;
28 
29 /**
30  *  An object of class <code>Style</code> represents a <i>style</i>
31  *  in an OpenOffice document.  In practice subclasses of this
32  *  <code>Style</code>, such as <code>TextStyle</code>,
33  *  <code>ParaStyle</code> are used.
34  *
35  *  @author   David Proulx
36  *  @see <a href="TextStyle.html">TextStyle</a>,
37  *  <a href="ParaStyle.html">ParaStyle</a>
38  */
39 public class Style {
40 
41     /**  Name of the <code>Style</code>. */
42     protected String name = null;
43     /**  Family of the <code>Style</code>. */
44     protected String family = null;
45     /**  Parent of the <code>Style</code>. */
46     protected String parent = null;
47 
48     /**
49      *  A reference to the <code>StyleCatalog</code> to be used for
50      *  looking up ancestor <code>Style</code> objects.
51      */
52     protected StyleCatalog sc;
53 
54 
55     /**
56      *  Constructor for use when going from DOM to client device format.
57      *
58      *  @param  node  A <i>style:style</i> or <i>style:default-style</i>
59      *                <code>Node</code> from the document being parsed.
60      *                No checking of <code>Node</code> is done, so if it
61      *                is not of the proper type the results will be
62      *                unpredictable.
63      *  @param  sc    The <code>StyleCatalog</code>, which is used for
64      *                looking up ancestor <code>Style</code> objects.
65      */
Style(Node node, StyleCatalog sc)66     public Style(Node node, StyleCatalog sc) {
67 
68         this.sc = sc;
69 
70         // Run through the attributes of this node, saving
71         // the ones we're interested in.
72         if (node.getNodeName().equals("style:default-style"))
73             name = "DEFAULT_STYLE";
74         NamedNodeMap attrNodes = node.getAttributes();
75         if (attrNodes != null) {
76             int len = attrNodes.getLength();
77             for (int i = 0; i < len; i++) {
78                 Node attr = attrNodes.item(i);
79                 if (attr.getNodeName().equals("style:family"))
80                     family = attr.getNodeValue();
81                 else if (attr.getNodeName().equals("style:name")) {
82                     name = attr.getNodeValue();
83                 } else if (attr.getNodeName().equals("style:parent-style-name"))
84                     parent = attr.getNodeValue();
85 
86             }
87         }
88     }
89 
90 
91     /**
92      *  Constructor for use when going from client device format to DOM.
93      *
94      *  @param  name    Name of the <code>Style</code>.  Can be null.
95      *  @param  family  Family of the <code>Style</code> - usually
96      *                  <i>paragraph</i>, <i>text</i>, etc.  Can be null.
97      *  @param  parent  Name of the parent <code>Style</code>, or null if none.
98      *  @param  sc      The <code>StyleCatalog</code>, which is used for
99      *                  looking up ancestor <code>Style</code> objects.
100      */
Style(String name, String family, String parent, StyleCatalog sc)101     public Style(String name, String family, String parent, StyleCatalog sc) {
102         this.sc = sc;
103         this.name = name;
104         this.family = family;
105         this.parent = parent;
106     }
107 
108 
109     /**
110      *  Set the <code>StyleCatalog</code> to be used when looking up the
111      *  <code>Style</code> parent.
112      *
113      *  @param  sc  The <code>StyleCatalog</code>, which is used for
114      *              looking up ancestor <code>Style</code> objects.
115      */
setCatalog(StyleCatalog sc)116     public void setCatalog(StyleCatalog sc) {
117         this.sc = sc;
118     }
119 
120 
121     /**
122      *  Returns the name of this <code>Style</code>.
123      *
124      *  @return  The name of this <code>Style</code>.
125      */
getName()126     public String getName() {
127         return name;
128     }
129 
130 
131     /**
132      *  Sets the name of this <code>Style</code>.
133      *
134      *  @param  newName  The new name of this <code>Style</code>.
135      */
setName(String newName)136     public void setName(String newName) {
137         name = newName;
138     }
139 
140 
141     /**
142      *  Return the family of this <code>Style</code>.
143      *
144      *  @return  The family of this <code>Style</code>.
145      */
getFamily()146     public String getFamily() {
147         return family;
148     }
149 
150     /**
151      *  Return the name of the parent of this <code>Style</code>.
152      *
153      *  @return  The parent of this <code>Style</code>.
154      */
getParent()155     public String getParent() {
156         return parent;
157     }
158 
159 
160     /**
161      *  Return a <code>Style</code> object corresponding to this one, but with
162      *  all of the inherited information from parent <code>Style</code>
163      *  objects filled in.  The object returned will be a new object, not a
164      *  reference to this object, even if it does not need any information
165      *  added.
166      *
167      *  @return  A resolved <code>Style</code> object in which to look up
168      *           ancestors.
169      */
getResolved()170     public Style getResolved() {
171         return new Style(name, family, parent, sc);
172     }
173 
174 
175     /**
176      *  Write a <code>Node</code> in <code>parentDoc</code>
177      *  representing this <code>Style</code>.  Note that the
178      *  <code>Node</code> is returned unconnected.
179      *
180      *  @param  parentDoc  Document to which new <code>Node</code> will
181      *                     belong.
182      *  @param  name       Name to use for new <code>Node</code>.
183      */
createNode(org.w3c.dom.Document parentDoc, String name)184     public Node createNode(org.w3c.dom.Document parentDoc, String name) {
185         // DJP: write this!  Should call writeAttributes()
186         return null;
187     }
188 
189 
190     /**
191      *  Write this <code>Style</code> object's attributes to the given
192      *  <code>Node</code>.  This may involve writing child
193      *  <code>Node</code> objects as well.  This is similar to the
194      *  <code>writeNode</code> method, but the <code>Node</code>
195      *  already exists, and this does  <b>not</b> write the name,
196      *  family, and parent attributes, which are assumed to already
197      *  exist in the <code>Node</code>.
198      *
199      *  @param  node  The <code>Node</code> to add style attributes.
200      */
writeAttributes(Node node)201     public void writeAttributes(Node node) {
202     }
203 
204 
205     /**
206      *  Return true if <code>Style</code> is a subset of this one.  Note
207      *  that this will return true even if <code>Style</code> is less
208      *  specific than this <code>Style</code>, so long as it does not
209      *  contradict this <code>Style</code> in any way.
210      *
211      *  This always returns true since only subclasses of
212      *  <code>Style</code> contain any actual <code>Style</code>
213      *  information.
214      *
215      *  @param  style  The <code>Style</code> to check
216      *
217      *  @return  true if the <code>Style</code> is a subset, false otherwise.
218      */
isSubset(Style style)219     public boolean isSubset(Style style) {
220         return true;
221     }
222 }
223 
224