View Javadoc

1   /**********************************************
2    * Copyright (C) 2010 Lukas Laag
3    * This file is part of lib-gwt-svg.
4    * 
5    * libgwtsvg is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * libgwtsvg is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   * 
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with libgwtsvg.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.dom.svg.impl;
19  
20  import java.util.Stack;
21  
22  import org.vectomatic.dom.svg.OMSVGAnimatedString;
23  import org.vectomatic.dom.svg.OMSVGStyle;
24  import org.vectomatic.dom.svg.utils.DOMHelper;
25  import org.vectomatic.dom.svg.utils.ParserException;
26  import org.vectomatic.dom.svg.utils.SVGConstants;
27  
28  import com.google.gwt.dom.client.Element;
29  import com.google.gwt.dom.client.Node;
30  import com.google.gwt.dom.client.NodeList;
31  import com.google.gwt.dom.client.Text;
32  import com.google.gwt.regexp.shared.MatchResult;
33  import com.google.gwt.regexp.shared.RegExp;
34  
35  
36  /**
37   * Internal class to wrap DOM parser implementations for opera
38   * @author laaglu
39   */
40  public class SVGParserImplOpera extends SVGParserImpl {
41  	private static final String URL = "^url\\((['\"])?[^#\\)]*#([^'\"\\)]*)\\1\\)$";
42  	private static final RegExp URLExp = RegExp.compile(URL, "g");
43  	private static String getRef(String expr) {
44  		URLExp.setLastIndex(0);
45  		MatchResult result = URLExp.exec(expr);
46  		if (result != null && result.getGroupCount() == 3) {
47  			return result.getGroup(2);
48  		}
49  		return null;
50  	}
51  	/**
52  	 * Parses the supplied SVG text into a document
53  	 * @param rawSvg
54  	 * raw xml to be parsed
55  	 * @return
56  	 * the document resulting from the parse
57  	 */
58  	public final SVGSVGElement parse(String rawSvg) throws ParserException {
59  		SVGDocument doc = parseFromString(rawSvg, "text/xml").cast();
60  		Element elt = doc.getDocumentElement();
61  		if ("parsererror".equals(DOMHelper.getLocalName(elt))) {
62  			String message = "Parsing error";
63  			if (elt.getFirstChild() != null) {
64  				message = elt.getFirstChild().<Text>cast().getData();
65  			}
66  			throw new ParserException(ParserException.Type.NotWellFormed, message);
67  		}
68  		SVGSVGElement svg = DOMHelper.importNode(DOMHelper.getCurrentDocument(), elt, true).cast();
69  		operaFix(svg);
70      	return svg;
71  	}
72  	
73  	/**
74  	 * Fix for opera.
75  	 * SVG Objects created by the parser and imported do not seem to recognize their
76  	 * CSS attributes. Reapplying them seems to solve the issue.
77  	 * The CSS style attributes which contain hrefs to svg elements are corrupted an
78  	 * need to be fixed as well.
79  	 * @param root
80  	 */
81  	private static void operaFix(Element root) {
82  		Stack<Element> stack = new Stack<Element>();
83  		stack.push(root);
84  		while(!stack.empty()) {
85  			Element element = stack.pop();
86  			if (SVGConstants.SVG_NAMESPACE_URI.equals(DOMHelper.getNamespaceURI(element))) {
87  				OMSVGAnimatedString cn = element.<SVGElement>cast().getClassName_();
88  				if (cn != null) {
89  					String value = cn.getBaseVal();
90  					if (value != null && value.length() > 0) {
91  						cn.setBaseVal(value);
92  					}
93  				}
94  				if (element.hasAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE)) {
95  					element.setAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE, element.getAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE));
96  					OMSVGStyle style = element.getStyle().<OMSVGStyle>cast();
97  					fixProperty(style, SVGConstants.CSS_FILL_PROPERTY);
98  					fixProperty(style, SVGConstants.CSS_STROKE_PROPERTY);
99  				}
100 			}
101 			NodeList<Node> childNodes = element.getChildNodes();
102 			for (int i = 0, length = childNodes.getLength(); i < length; i++) {
103 				Node node = childNodes.getItem(i);
104 				if (node.getNodeType() == Node.ELEMENT_NODE) {
105 					stack.push(node.<Element>cast());
106 				}
107 			}
108 		}
109 	}
110 	private static void fixProperty(OMSVGStyle style, String propertyName) {
111 		String propertyValue = style.getSVGProperty(propertyName);
112 		if (propertyValue != null && propertyValue.length() > 0) {
113 			String ref = getRef(propertyValue);
114 			if (ref != null) {
115 				String url = "url('#" + ref + "')";
116 				style.setSVGProperty(propertyName, url);
117 			}
118 		}		
119 	}
120 }