From a48056ad6ac69369ff17a781737cd78be7efedf3 Mon Sep 17 00:00:00 2001 From: Keiron Liddle Date: Mon, 11 Sep 2000 03:14:05 +0000 Subject: [PATCH] added stuff to handle style sheets, selectors, styling git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@193689 13f79535-47bb-0310-9956-ffa450edef68 --- src/org/apache/fop/dom/ElementImpl.java | 181 +--------- src/org/apache/fop/dom/NodeImpl.java | 97 +----- .../apache/fop/dom/css/CSSStyleRuleImpl.java | 323 ++++++++++++++++++ .../apache/fop/dom/css/CSSStyleSheetImpl.java | 194 +++++++++++ .../dom/stylesheets/StyleSheetListImpl.java | 79 +++++ .../fop/dom/svg/SVGAnimatedStringImpl.java | 78 +++++ .../fop/dom/svg/SVGCircleElementImpl.java | 5 + .../apache/fop/dom/svg/SVGElementImpl.java | 178 +++------- .../fop/dom/svg/SVGPolygonElementImpl.java | 5 + .../fop/dom/svg/SVGRectElementImpl.java | 5 + .../apache/fop/dom/svg/SVGSVGElementImpl.java | 40 ++- .../fop/dom/svg/SVGStyleElementImpl.java | 18 +- 12 files changed, 828 insertions(+), 375 deletions(-) create mode 100644 src/org/apache/fop/dom/css/CSSStyleRuleImpl.java create mode 100644 src/org/apache/fop/dom/css/CSSStyleSheetImpl.java create mode 100644 src/org/apache/fop/dom/stylesheets/StyleSheetListImpl.java create mode 100644 src/org/apache/fop/dom/svg/SVGAnimatedStringImpl.java diff --git a/src/org/apache/fop/dom/ElementImpl.java b/src/org/apache/fop/dom/ElementImpl.java index 72f912c1a..9e10b6daa 100644 --- a/src/org/apache/fop/dom/ElementImpl.java +++ b/src/org/apache/fop/dom/ElementImpl.java @@ -61,155 +61,12 @@ import java.util.*; * */ public class ElementImpl extends NodeImpl implements Element { -// Vector childs = new Vector(); -// Node parent = null; -// Document ownerDoc; public Node replaceChild(Node n, Node no) { return null; } -/* public String getNodeName() - { - return null; - } - - public short getNodeType() - { - return 0; - } - - public Node getParentNode() - { - return parent; - } - - public NodeList getChildNodes() - { - return new NodeListImpl(childs); - } - - public Node getFirstChild() - { - return null; - } - - public Node getLastChild() - { - return null; - } - - public Node getPreviousSibling() - { - return null; - } - - public Node getNextSibling() - { - return null; - } - - public NamedNodeMap getAttributes() - { - return null; - } - - public Document getOwnerDocument() - { - return ownerDoc; - } - - void setOwnerDocument(Document doc) - { - ownerDoc = doc; - NodeList nl = getChildNodes(); - for(int count = 0; count < nl.getLength(); count++) { - Node n = nl.item(count); - if(n instanceof ElementImpl) { - ((ElementImpl)n).setOwnerDocument(ownerDoc); - } - } - } - - public Node insertBefore(Node newChild, - Node refChild) - throws DOMException - { - return null; - } - - public Node removeChild(Node oldChild) - throws DOMException - { - return null; - } - - public Node appendChild(Node newChild) - throws DOMException - { - childs.addElement(newChild); - if(newChild instanceof ElementImpl) { - ElementImpl ele = (ElementImpl)newChild; - ele.parent = this; - ele.setOwnerDocument(ownerDoc); - } - return newChild; - } - - public boolean hasChildNodes() - { - return childs.size() > 0; - } - - public Node cloneNode(boolean deep) - { - return null; - } - - public void normalize() - { - } - - public boolean supports(String feature, - String version) - { - return false; - } - - public String getNamespaceURI() - { - return SVGDocumentImpl.namespaceURI; - } - - public String getPrefix() - { - return "svg"; - } - - public void setPrefix(String prefix) throws DOMException - { - } - - public String getLocalName() - { - return null; - } - - public String getNodeValue() throws DOMException - { - return null; - } - - public void setNodeValue(String nodeValue) throws DOMException - { - } - - public String getTagName() - { - return null; - } -*/ public String getAttribute(String name) { return null; @@ -242,7 +99,23 @@ public class ElementImpl extends NodeImpl implements Element { public NodeList getElementsByTagName(String name) { - return null; + NodeList nl = getChildNodes(); + Vector eles = new Vector(); + for(int count = 0; count < nl.getLength(); count++) { + Node el = (Node)nl.item(count); + if(el instanceof Element) { + if(name.equals(((Element)el).getTagName())) { + eles.addElement(el); + } + NodeList subtags = ((Element)el).getElementsByTagName(name); + for(int c = 0; c < subtags.getLength(); c++) { + Node node = (Node)subtags.item(c); + eles.addElement(node); + } + } + } + NodeList val = new NodeListImpl(eles); + return val; } public String getAttributeNS(String namespaceURI, @@ -298,23 +171,3 @@ public class ElementImpl extends NodeImpl implements Element { return false; } } -/* -class NodeListImpl implements NodeList -{ - Vector vect = null; - - NodeListImpl(Vector v) - { - vect = v; - } - - public int getLength() - { - return vect.size(); - } - - public Node item(int i) - { - return (Node)vect.elementAt(i); - } -}*/ diff --git a/src/org/apache/fop/dom/NodeImpl.java b/src/org/apache/fop/dom/NodeImpl.java index f8fe16c97..eb20a7ecd 100644 --- a/src/org/apache/fop/dom/NodeImpl.java +++ b/src/org/apache/fop/dom/NodeImpl.java @@ -92,21 +92,35 @@ public class NodeImpl implements Node { public Node getFirstChild() { + if(childs.size() > 0) { + return (Node)childs.elementAt(0); + } return null; } public Node getLastChild() { + if(childs.size() > 0) { + return (Node)childs.elementAt(childs.size() - 1); + } return null; } public Node getPreviousSibling() { + int ind = ((NodeImpl)parent).childs.indexOf(this); + if(ind > 0) { + return (Node)((NodeImpl)parent).childs.elementAt(ind - 1); + } return null; } public Node getNextSibling() { + int ind = ((NodeImpl)parent).childs.indexOf(this); + if(ind + 1 < ((NodeImpl)parent).childs.size()) { + return (Node)((NodeImpl)parent).childs.elementAt(ind + 1); + } return null; } @@ -210,89 +224,6 @@ public class NodeImpl implements Node { return null; } -/* public String getAttribute(String name) - { - return null; - } - - public void setAttribute(String name, String value) throws DOMException - { - } - - public void removeAttribute(String name) throws DOMException - { - } - - public Attr getAttributeNode(String name) - { - return null; - } - - public Attr setAttributeNode(Attr newAttr) - throws DOMException - { - return null; - } - - public Attr removeAttributeNode(Attr oldAttr) - throws DOMException - { - return null; - } - - public NodeList getElementsByTagName(String name) - { - return null; - } - - public String getAttributeNS(String namespaceURI, - String localName) - { - return null; - } - - public void setAttributeNS(String namespaceURI, - String qualifiedName, - String value) - throws DOMException - { - } - - public void removeAttributeNS(String namespaceURI, - String localName) - throws DOMException - { - } - - public Attr getAttributeNodeNS(String namespaceURI, - String localName) - { - return null; - } - - public Attr setAttributeNodeNS(Attr newAttr) - throws DOMException - { - return null; - } - - public NodeList getElementsByTagNameNS(String namespaceURI, - String localName) - { - return null; - } - - public boolean hasAttributeNS (String namespaceURI, - String localName) - { - return false; - } - - public boolean hasAttribute (String name) - { - return false; - } -*/ public boolean hasAttributes() { return false; diff --git a/src/org/apache/fop/dom/css/CSSStyleRuleImpl.java b/src/org/apache/fop/dom/css/CSSStyleRuleImpl.java new file mode 100644 index 000000000..c035f6084 --- /dev/null +++ b/src/org/apache/fop/dom/css/CSSStyleRuleImpl.java @@ -0,0 +1,323 @@ +/*-- $Id$ -- + + ============================================================================ + The Apache Software License, Version 1.1 + ============================================================================ + + Copyright (C) 1999 The Apache Software Foundation. All rights reserved. + + Redistribution and use in source and binary forms, with or without modifica- + tion, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + 3. The end-user documentation included with the redistribution, if any, must + include the following acknowledgment: "This product includes software + developed by the Apache Software Foundation (http://www.apache.org/)." + Alternately, this acknowledgment may appear in the software itself, if + and wherever such third-party acknowledgments normally appear. + + 4. The names "FOP" and "Apache Software Foundation" must not be used to + endorse or promote products derived from this software without prior + written permission. For written permission, please contact + apache@apache.org. + + 5. Products derived from this software may not be called "Apache", nor may + "Apache" appear in their name, without prior written permission of the + Apache Software Foundation. + + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This software consists of voluntary contributions made by many individuals + on behalf of the Apache Software Foundation and was originally created by + James Tauber . For more information on the Apache + Software Foundation, please see . + + */ + +package org.apache.fop.dom.css; + +//import org.apache.fop.dom.svg.*; +import org.w3c.dom.svg.*; + +import org.w3c.dom.css.*; +import org.w3c.dom.*; +import org.w3c.dom.stylesheets.*; + +import java.util.*; + +/** + * + * + */ +public class CSSStyleRuleImpl implements CSSStyleRule { + String seltext; + String cssText; + CSSStyleDeclaration style; + CSSStyleSheet styleSheet; + Vector selectors = new Vector(); + + public CSSStyleRuleImpl(CSSStyleSheet styleSheet) + { + this.styleSheet = styleSheet; + } + + public String getSelectorText() + { + return seltext; + } + + /** + * Selectors are comma separated values (except for attributes) + * of all the selectors for the style. + */ + public void setSelectorText(String selectorText) + { + seltext = selectorText; + parseSelector(); + } + + protected void parseSelector() + { + // need to handle "," in attribute selectors + StringTokenizer st = new StringTokenizer(seltext, ","); + while(st.hasMoreTokens()) { + String sel = st.nextToken().trim(); + StringTokenizer subt = new StringTokenizer(sel, " \t\n\r"); + Selector last = null; + Selector current = null; + int compoundstate = 0; + while(subt.hasMoreTokens()) { + String str = subt.nextToken().trim(); + current = new Selector(); + boolean compounding = false; + if(str.equals("*")) { + // all + current.type = Selector.ALL; + current.value = "*"; + } else if(str.equals(">")) { + // child of + compoundstate = 1; + compounding = true; + } else if(str.equals("+")) { + // precedent of + compoundstate = 2; + compounding = true; + } else if(str.startsWith("#")) { + // id + current.type = Selector.ID; + current.value = str.substring(1); + } else if(str.startsWith(".")) { + // class + current.type = Selector.CLASS; + current.value = str.substring(1); + } else if(str.startsWith("[")) { + // attribute + current.type = Selector.ATTRIBUTE; + current.value = str.substring(1, str.length() - 1); + } else { + // tag + current.type = Selector.TAG; + if(str.indexOf(":") != -1) { + current.type = Selector.TAG_FIRST_CHILD; + int pos = str.indexOf(":"); + current.value = str.substring(0, pos); + current.subValue = str.substring(pos + 1); + } else if(str.indexOf("#") != -1) { + current.type = Selector.TAG_ID; + int pos = str.indexOf("#"); + current.value = str.substring(0, pos); + current.subValue = str.substring(pos + 1); + } else if(str.indexOf(".") != -1) { + current.type = Selector.TAG_CLASS; + int pos = str.indexOf("."); + current.value = str.substring(0, pos); + current.subValue = str.substring(pos + 1); + } else { + current.value = str; + } + } + if(!compounding) { + switch(compoundstate) { + case 0: + if(last != null) { + Selector compound = new Selector(); + compound.type = Selector.DESCENDANT; + compound.last = last; + compound.current = current; + current = compound; + compoundstate = 0; + } + break; + case 1: + { + Selector compound = new Selector(); + compound.type = Selector.CHILD; + compound.last = last; + compound.current = current; + current = compound; + compoundstate = 0; + } + break; + case 2: + { + Selector compound = new Selector(); + compound.type = Selector.PRECEDENT; + compound.last = last; + compound.current = current; + current = compound; + compoundstate = 0; + } + break; + } + last = current; + } + } + if(current != null) + selectors.add(current); + } + } + + public CSSStyleDeclaration getStyle() + { + return style; + } + + public String getCssText() + { + return cssText; + } + + public void setCssText(String cssText) + { + this.cssText = cssText; + style = new CSSStyleDeclarationImpl(); + style.setCssText(cssText); + } + + public short getType() + { + return STYLE_RULE; + } + + public CSSStyleSheet getParentStyleSheet() + { + return styleSheet; + } + + public CSSRule getParentRule() + { + return null; + } + + /** + * This should probably be elsewhere, in dom.svg + */ + public boolean matches(SVGElement el) + { + for(Enumeration e = selectors.elements(); e.hasMoreElements(); ) { + Selector sel = (Selector)e.nextElement(); + if(matches(el, sel)) + return true; + } + return false; + } + + protected boolean matches(SVGElement el, Selector sel) + { + short type = sel.type; + if(el == null) + return false; + switch(type) { + case Selector.ALL: + return true; +// break; + case Selector.ID: + return el.getId().equals(sel.value); +// break; + case Selector.CLASS: + return ((SVGStylable)el).getClassName().getBaseVal().equals(sel.value); +// break; + case Selector.ATTRIBUTE: +//System.out.println(sel.value + ":" + el.getAttribute(sel.value)); +// return el.getAttribute(sel.value); + break; + case Selector.TAG: + return sel.value.equals(el.getTagName()); +// break; + case Selector.TAG_FIRST_CHILD: + if(el.getParentNode() != null) + return (el.getParentNode().getFirstChild() == el); + break; + case Selector.TAG_ID: + return sel.value.equals(el.getTagName()) + && sel.subValue.equals(el.getId()); +// break; + case Selector.TAG_CLASS: + return sel.value.equals(el.getTagName()) + && sel.subValue.equals(((SVGStylable)el).getClassName().getBaseVal()); +// break; + case Selector.DESCENDANT: + if(el.getParentNode() instanceof SVGElement) { + if(!matches(el, sel.current)) + return false; + SVGElement parent = (SVGElement)el.getParentNode(); + while(parent != null) { + if(matches(parent, sel.last)) + return true; + if(parent.getParentNode() instanceof SVGElement) { + parent = (SVGElement)parent.getParentNode(); + } else { + return false; + } + } + } + return false; +// break; + case Selector.CHILD: + return matches(el, sel.current) + && matches((SVGElement)el.getParentNode(), sel.last); +// break; + case Selector.PRECEDENT: + return matches(el, sel.current) + && matches((SVGElement)el.getPreviousSibling(), sel.last); +// break; + } + return false; + } +} + +class Selector { + final static short NONE = -1; + final static short ALL = 0; + final static short ID = 1; + final static short CLASS = 2; + final static short ATTRIBUTE = 3; + final static short TAG = 4; + final static short DESCENDANT = 5; + final static short CHILD = 6; + final static short PRECEDENT = 7; + final static short TAG_FIRST_CHILD = 8; + final static short TAG_ID = 9; + final static short TAG_CLASS = 10; + short type = NONE; + String value = ""; + String subValue = ""; + // pre- simple selector in compound selectors + Selector last = null; + Selector current = null; +} diff --git a/src/org/apache/fop/dom/css/CSSStyleSheetImpl.java b/src/org/apache/fop/dom/css/CSSStyleSheetImpl.java new file mode 100644 index 000000000..87c347e5e --- /dev/null +++ b/src/org/apache/fop/dom/css/CSSStyleSheetImpl.java @@ -0,0 +1,194 @@ +/*-- $Id$ -- + + ============================================================================ + The Apache Software License, Version 1.1 + ============================================================================ + + Copyright (C) 1999 The Apache Software Foundation. All rights reserved. + + Redistribution and use in source and binary forms, with or without modifica- + tion, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + 3. The end-user documentation included with the redistribution, if any, must + include the following acknowledgment: "This product includes software + developed by the Apache Software Foundation (http://www.apache.org/)." + Alternately, this acknowledgment may appear in the software itself, if + and wherever such third-party acknowledgments normally appear. + + 4. The names "FOP" and "Apache Software Foundation" must not be used to + endorse or promote products derived from this software without prior + written permission. For written permission, please contact + apache@apache.org. + + 5. Products derived from this software may not be called "Apache", nor may + "Apache" appear in their name, without prior written permission of the + Apache Software Foundation. + + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This software consists of voluntary contributions made by many individuals + on behalf of the Apache Software Foundation and was originally created by + James Tauber . For more information on the Apache + Software Foundation, please see . + + */ + +package org.apache.fop.dom.css; + +import org.w3c.dom.css.*; +import org.w3c.dom.*; +import org.w3c.dom.stylesheets.*; + +import java.util.*; + +/** + * + * + */ +public class CSSStyleSheetImpl implements CSSStyleSheet { + Vector rules = new Vector(); + + public CSSStyleSheetImpl(String sheet) + { + parseSheet(sheet); + } + + protected void parseSheet(String str) + { + // get each rule and insert it + // rule ends with } + // need to ignore comments + String newStr = str; + int pos1; + int pos2; + pos1 = newStr.indexOf("/*"); + pos2 = newStr.indexOf("*/"); + while((pos1 != -1) && (pos2 != -1)) { + newStr = newStr.substring(0, pos1) + newStr.substring(pos2 + 2, newStr.length()); + pos1 = newStr.indexOf("/*"); + pos2 = newStr.indexOf("*/"); + } + StringTokenizer st = new StringTokenizer(newStr, "}"); + while(st.hasMoreTokens()) { + String rule = st.nextToken() + "}"; + insertRule(rule, rules.size()); + } + } + + public CSSRule getOwnerRule() + { + // since this is for a style element + return null; + } + + public CSSRuleList getCssRules() + { + return new CSSRuleListImpl(rules); + } + + public int insertRule(String rule, + int index) + throws DOMException + { + CSSRule r = parseRule(rule); + if(r != null) + rules.addElement(r); + return 0; + } + + public void deleteRule(int index) + throws DOMException + { + rules.remove(index); + } + + public String getType() + { + return "text/css"; + } + + public boolean getDisabled() + { + return false; + } + + public void setDisabled(boolean disabled) + { + } + + public Node getOwnerNode() + { + // return the style element + return null; + } + + public StyleSheet getParentStyleSheet() + { + return null; + } + + public String getHref() + { + return null; + } + + public String getTitle() + { + return null; + } + + public MediaList getMedia() + { + return null; + } + + protected CSSRule parseRule(String str) + { + // a rule is "selectors {style}" + // a list of selectors followed by the style statement in brackets + int pos1 = str.indexOf("{"); + int pos2 = str.indexOf("}"); + if(pos1 == -1 || pos2 == -1) + return null; + String sel = str.substring(0, pos1); + String style = str.substring(pos1 + 1, pos2); + CSSStyleRule rule = new CSSStyleRuleImpl(this); + rule.setSelectorText(sel); + rule.setCssText(style); + return rule; + } +} + +class CSSRuleListImpl implements CSSRuleList { + Vector rules; + CSSRuleListImpl(Vector v) + { + rules = v; + } + + public int getLength() + { + return rules.size(); + } + + public CSSRule item(int pos) + { + return (CSSRule)rules.elementAt(pos); + } +} diff --git a/src/org/apache/fop/dom/stylesheets/StyleSheetListImpl.java b/src/org/apache/fop/dom/stylesheets/StyleSheetListImpl.java new file mode 100644 index 000000000..ebd0ccb46 --- /dev/null +++ b/src/org/apache/fop/dom/stylesheets/StyleSheetListImpl.java @@ -0,0 +1,79 @@ +/*-- $Id$ -- + + ============================================================================ + The Apache Software License, Version 1.1 + ============================================================================ + + Copyright (C) 1999 The Apache Software Foundation. All rights reserved. + + Redistribution and use in source and binary forms, with or without modifica- + tion, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + 3. The end-user documentation included with the redistribution, if any, must + include the following acknowledgment: "This product includes software + developed by the Apache Software Foundation (http://www.apache.org/)." + Alternately, this acknowledgment may appear in the software itself, if + and wherever such third-party acknowledgments normally appear. + + 4. The names "FOP" and "Apache Software Foundation" must not be used to + endorse or promote products derived from this software without prior + written permission. For written permission, please contact + apache@apache.org. + + 5. Products derived from this software may not be called "Apache", nor may + "Apache" appear in their name, without prior written permission of the + Apache Software Foundation. + + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This software consists of voluntary contributions made by many individuals + on behalf of the Apache Software Foundation and was originally created by + James Tauber . For more information on the Apache + Software Foundation, please see . + + */ + +package org.apache.fop.dom.stylesheets; + +import org.w3c.dom.stylesheets.*; + +import java.util.*; + +/** + * + * + */ +public class StyleSheetListImpl implements StyleSheetList { + Vector sheets = new Vector(); + + public StyleSheetListImpl(Vector sheets) + { + this.sheets = sheets; + } + + public int getLength() + { + return sheets.size(); + } + + public StyleSheet item(int pos) + { + return (StyleSheet)sheets.elementAt(pos); + } +} diff --git a/src/org/apache/fop/dom/svg/SVGAnimatedStringImpl.java b/src/org/apache/fop/dom/svg/SVGAnimatedStringImpl.java new file mode 100644 index 000000000..b65ad9960 --- /dev/null +++ b/src/org/apache/fop/dom/svg/SVGAnimatedStringImpl.java @@ -0,0 +1,78 @@ +/*-- $Id$ -- + + ============================================================================ + The Apache Software License, Version 1.1 + ============================================================================ + + Copyright (C) 1999 The Apache Software Foundation. All rights reserved. + + Redistribution and use in source and binary forms, with or without modifica- + tion, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + 3. The end-user documentation included with the redistribution, if any, must + include the following acknowledgment: "This product includes software + developed by the Apache Software Foundation (http://www.apache.org/)." + Alternately, this acknowledgment may appear in the software itself, if + and wherever such third-party acknowledgments normally appear. + + 4. The names "FOP" and "Apache Software Foundation" must not be used to + endorse or promote products derived from this software without prior + written permission. For written permission, please contact + apache@apache.org. + + 5. Products derived from this software may not be called "Apache", nor may + "Apache" appear in their name, without prior written permission of the + Apache Software Foundation. + + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This software consists of voluntary contributions made by many individuals + on behalf of the Apache Software Foundation and was originally created by + James Tauber . For more information on the Apache + Software Foundation, please see . + + */ + +package org.apache.fop.dom.svg; + +import org.w3c.dom.svg.*; + +public class SVGAnimatedStringImpl implements SVGAnimatedString { + String str = null; + + public SVGAnimatedStringImpl(String l) + { + str = l; + } + + public String getBaseVal( ) + { + return str; + } + + public void setBaseVal( String baseVal ) + { + str = baseVal; + } + + public String getAnimVal( ) + { + return str; + } +} diff --git a/src/org/apache/fop/dom/svg/SVGCircleElementImpl.java b/src/org/apache/fop/dom/svg/SVGCircleElementImpl.java index 0c1194487..8a3ee7e71 100644 --- a/src/org/apache/fop/dom/svg/SVGCircleElementImpl.java +++ b/src/org/apache/fop/dom/svg/SVGCircleElementImpl.java @@ -106,4 +106,9 @@ public class SVGCircleElementImpl extends GraphicElement implements SVGCircleEle { this.r = r; } + + public String getTagName() + { + return "circle"; + } } diff --git a/src/org/apache/fop/dom/svg/SVGElementImpl.java b/src/org/apache/fop/dom/svg/SVGElementImpl.java index a1c697b19..6fe052d79 100644 --- a/src/org/apache/fop/dom/svg/SVGElementImpl.java +++ b/src/org/apache/fop/dom/svg/SVGElementImpl.java @@ -50,19 +50,23 @@ */ package org.apache.fop.dom.svg; +import org.apache.fop.dom.stylesheets.*; +import org.apache.fop.dom.css.*; import org.apache.fop.datatypes.*; import org.apache.fop.dom.ElementImpl; -import org.w3c.dom.css.CSSStyleDeclaration; -import org.w3c.dom.css.CSSValue; +import org.w3c.dom.css.*; import org.w3c.dom.svg.*; import org.w3c.dom.*; +import org.w3c.dom.stylesheets.*; import java.util.*; public abstract class SVGElementImpl extends ElementImpl implements SVGElement { String idString = ""; CSSStyleDeclaration styleDec; + SVGSVGElement ownerSvg; + SVGAnimatedString className = new SVGAnimatedStringImpl(""); public String getId() { @@ -81,7 +85,7 @@ public abstract class SVGElementImpl extends ElementImpl implements SVGElement { public SVGSVGElement getOwnerSVGElement( ) { - return null; + return ownerSvg; } public SVGElement getViewportElement( ) @@ -91,25 +95,49 @@ public abstract class SVGElementImpl extends ElementImpl implements SVGElement { public SVGAnimatedString getClassName( ) { - return null; + return className; } public void setClassName( SVGAnimatedString className ) { + this.className = className; } public CSSValue getPresentationAttribute ( String name ) { CSSStyleDeclaration style; + CSSValue val = null; + style = getStyle(); - if(style == null) { - return null; + if(style != null) { + val = style.getPropertyCSSValue(name); } - CSSValue val; - val = style.getPropertyCSSValue(name); + if(val == null) { + // this checks for the style selector matching + // everytime a property is requested, this is bad, slow + // get "style" element style for this SVGSVGElement svg = getOwnerSVGElement(); + // maybe + // val = svg.getComputedStyle(this, name); + StyleSheetList list = svg.getStyleSheets(); + for(int count = 0; count < list.getLength(); count++) { + CSSRuleList rlist = ((CSSStyleSheet)list.item(count)).getCssRules(); + for(int c = 0; c < rlist.getLength(); c++) { + CSSRule rule = rlist.item(c); + if(rule.getType() == CSSRule.STYLE_RULE) { + if(((CSSStyleRuleImpl)rule).matches(this)) { + style = ((CSSStyleRule)rule).getStyle(); + val = style.getPropertyCSSValue(name); +// break; + } + } + } +// if(val != null) { +// break; +// } + } } if(val == null) { // get element parents style @@ -136,134 +164,36 @@ public abstract class SVGElementImpl extends ElementImpl implements SVGElement { styleDec = dec; } -/* SVGElement parent = null; - public SVGElement getGraphicParent() - { - return parent; - } - - public void setParent(SVGElement graph) - { - parent = graph; - }*/ - -/* Hashtable style = null; - public void setStyle(Hashtable st) - { - style = st; - } - - public Hashtable oldgetStyle() - { - Hashtable ret = null; - if(parent != null) { - ret = parent.oldgetStyle(); - if(ret != null) - ret = (Hashtable)ret.clone(); - } - if(ret == null) { - ret = style; - } else { - if(style != null) { - for(Enumeration e = style.keys(); e.hasMoreElements(); ) { - String str = (String)e.nextElement(); - ret.put(str, style.get(str)); - } - } - } - return ret; - } - - Hashtable defs = new Hashtable(); - public void addDefs(Hashtable table) + public SVGAnimatedBoolean getExternalResourcesRequired( ) { - for(Enumeration e = table.keys(); e.hasMoreElements(); ) { - String str = (String)e.nextElement(); - defs.put(str, table.get(str)); - } + return null; } - public Hashtable getDefs() + public void setExternalResourcesRequired( SVGAnimatedBoolean externalResourcesRequired ) { - Hashtable ret = null; - if(parent != null) { - ret = parent.getDefs(); - if(ret != null) - ret = (Hashtable)ret.clone(); - } - if(ret == null) { - ret = defs; - } else { - if(defs != null) { - for(Enumeration e = defs.keys(); e.hasMoreElements(); ) { - String str = (String)e.nextElement(); - ret.put(str, defs.get(str)); - } - } - } - return ret; } - public SVGElement locateDef(String str) + public Node appendChild(Node newChild) + throws DOMException { - Object obj = null; - if(defs != null) { - obj = defs.get(str); + Node nChild = super.appendChild(newChild); + if(newChild instanceof SVGElementImpl) { + SVGElementImpl ele = (SVGElementImpl)newChild; + if(ownerSvg != null) + ele.setOwnerSVG(ownerSvg); } - if(obj == null) { - NodeList list = getChildNodes(); - for(int count = 0; count < list.getLength(); count++) { - Object o = list.item(count); - if(o instanceof SVGElement) { - String s; - s = ((SVGElement)o).getId(); - if(str.equals(s)) { - obj = o; - break; - } - } - } - } - if(obj == null && parent != null) { - obj = parent.locateDef(str); - } - return (SVGElement)obj; + return nChild; } - Vector trans = null; - public void setTransform(Vector tr) + public void setOwnerSVG(SVGSVGElement owner) { - trans = tr; - } - - public Vector oldgetTransform() - { - return trans; -/* Vector ret = null; - if(parent != null) { - ret = parent.oldgetTransform(); - if(ret != null) - ret = (Vector)ret.clone(); - } - if(ret == null) { - ret = trans; - } else { - if(trans != null) { - for(Enumeration e = trans.elements(); e.hasMoreElements(); ) { - Object o = e.nextElement(); - ret.addElement(o); - } + ownerSvg = owner; + NodeList nl = getChildNodes(); + for(int count = 0; count < nl.getLength(); count++) { + Node n = nl.item(count); + if(n instanceof SVGElementImpl) { + ((SVGElementImpl)n).setOwnerSVG(owner); } } - return ret;* - } -*/ - public SVGAnimatedBoolean getExternalResourcesRequired( ) - { - return null; - } - - public void setExternalResourcesRequired( SVGAnimatedBoolean externalResourcesRequired ) - { } } diff --git a/src/org/apache/fop/dom/svg/SVGPolygonElementImpl.java b/src/org/apache/fop/dom/svg/SVGPolygonElementImpl.java index ba0ac2af3..0c5e150c6 100644 --- a/src/org/apache/fop/dom/svg/SVGPolygonElementImpl.java +++ b/src/org/apache/fop/dom/svg/SVGPolygonElementImpl.java @@ -93,4 +93,9 @@ public class SVGPolygonElementImpl extends GraphicElement implements SVGPolygonE rect.setHeight(maxY - minY); return rect; } + + public String getTagName() + { + return "polygon"; + } } diff --git a/src/org/apache/fop/dom/svg/SVGRectElementImpl.java b/src/org/apache/fop/dom/svg/SVGRectElementImpl.java index 4f45cd533..c5ace8baa 100644 --- a/src/org/apache/fop/dom/svg/SVGRectElementImpl.java +++ b/src/org/apache/fop/dom/svg/SVGRectElementImpl.java @@ -153,4 +153,9 @@ public class SVGRectElementImpl extends GraphicElement implements SVGRectElement { this.ry = ry; } + + public String getTagName() + { + return "rect"; + } } diff --git a/src/org/apache/fop/dom/svg/SVGSVGElementImpl.java b/src/org/apache/fop/dom/svg/SVGSVGElementImpl.java index 9216c858d..fc1fb3b8f 100644 --- a/src/org/apache/fop/dom/svg/SVGSVGElementImpl.java +++ b/src/org/apache/fop/dom/svg/SVGSVGElementImpl.java @@ -50,6 +50,7 @@ */ package org.apache.fop.dom.svg; +import org.apache.fop.dom.stylesheets.StyleSheetListImpl; import org.apache.fop.fo.Property; import java.util.*; @@ -59,6 +60,7 @@ import org.w3c.dom.Element; import org.w3c.dom.css.RGBColor; import org.w3c.dom.css.CSSStyleDeclaration; import org.w3c.dom.stylesheets.StyleSheetList; +import org.w3c.dom.stylesheets.StyleSheet; import org.w3c.dom.NodeList; import org.w3c.dom.views.DocumentView; import org.w3c.dom.svg.*; @@ -134,7 +136,7 @@ public class SVGSVGElementImpl extends GraphicElement implements SVGSVGElement { { } - public CSSValue getPresentationAttribute ( String name ) +/* public CSSValue getPresentationAttribute ( String name ) { CSSStyleDeclaration style; style = getStyle(); @@ -151,7 +153,7 @@ public class SVGSVGElementImpl extends GraphicElement implements SVGSVGElement { } } return val; - } + }*/ public SVGPoint getCurrentTranslate( ) { @@ -351,7 +353,15 @@ public class SVGSVGElementImpl extends GraphicElement implements SVGSVGElement { public StyleSheetList getStyleSheets() { - return null; + NodeList nl = getElementsByTagName("style"); + Vector shs = new Vector(); + for(int count = 0; count < nl.getLength(); count++) { + Node el = (Node)nl.item(count); + SVGStyleElementImpl sse = (SVGStyleElementImpl)el; + StyleSheet sheet = sse.getStyleSheet(); + shs.addElement(sheet); + } + return new StyleSheetListImpl(shs); } public Event createEvent(String str) @@ -363,4 +373,28 @@ public class SVGSVGElementImpl extends GraphicElement implements SVGSVGElement { { return null; } + + public Node appendChild(Node newChild) + throws DOMException + { + Node nChild = super.appendChild(newChild); + if(newChild instanceof SVGElementImpl) { + SVGElementImpl ele = (SVGElementImpl)newChild; + ele.setOwnerSVG(this); + } + setOwnerSVG(this); + return nChild; + } + + public void setOwnerSVG(SVGSVGElement owner) + { + ownerSvg = owner; + NodeList nl = getChildNodes(); + for(int count = 0; count < nl.getLength(); count++) { + Node n = nl.item(count); + if(n instanceof SVGElementImpl) { + ((SVGElementImpl)n).setOwnerSVG(this); + } + } + } } diff --git a/src/org/apache/fop/dom/svg/SVGStyleElementImpl.java b/src/org/apache/fop/dom/svg/SVGStyleElementImpl.java index 03c8bbb18..3fedd5edb 100644 --- a/src/org/apache/fop/dom/svg/SVGStyleElementImpl.java +++ b/src/org/apache/fop/dom/svg/SVGStyleElementImpl.java @@ -51,16 +51,22 @@ package org.apache.fop.dom.svg; +import org.apache.fop.dom.css.*; + import org.w3c.dom.svg.*; +import org.w3c.dom.css.*; +import org.w3c.dom.stylesheets.*; /** * */ public class SVGStyleElementImpl extends GraphicElement implements SVGStyleElement { String type; + CSSStyleSheet styleSheet; - public SVGStyleElementImpl() + public SVGStyleElementImpl(String str) { + styleSheet = new CSSStyleSheetImpl(str); } public String getType() @@ -90,4 +96,14 @@ public class SVGStyleElementImpl extends GraphicElement implements SVGStyleEleme public void setTitle( String title ) { } + + public StyleSheet getStyleSheet() + { + return styleSheet; + } + + public String getTagName() + { + return "style"; + } } -- 2.39.5