View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 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.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.vectomatic.dom.svg.utils.DOMHelper;
25  import org.vectomatic.dom.svg.utils.ParserException;
26  import org.vectomatic.dom.svg.utils.SVGPrefixResolver;
27  
28  import com.google.gwt.core.client.JavaScriptException;
29  import com.google.gwt.dom.client.Element;
30  import com.google.gwt.dom.client.Text;
31  
32  /**
33   * Internal class to wrap DOM parser implementations for IE based browsers
34   * @author laaglu
35   */
36  public class SVGParserImplIE extends SVGParserImpl {
37  	/**
38  	 * Parses the supplied SVG text into a document
39  	 * @param rawSvg
40  	 * raw xml to be parsed
41  	 * @return
42  	 * the document resulting from the parse
43  	 */
44  	public final SVGSVGElement parse(String rawSvg) throws ParserException {
45  		SVGDocument doc = null;
46  		try {
47  			doc = parseFromString(rawSvg, "text/xml").cast();
48  			
49  		} catch(JavaScriptException e) {
50  			throw new ParserException(ParserException.Type.NotWellFormed, e.getMessage());
51  		}
52  		Element elt = doc.getDocumentElement();
53  		if ("parsererror".equals(DOMHelper.getLocalName(elt))) {
54  			String message = "Parsing error";
55  			if (elt.getFirstChild() != null) {
56  				message = elt.getFirstChild().<Text>cast().getData();
57  			}
58  			throw new ParserException(ParserException.Type.NotWellFormed, message);
59  		}
60  		SVGSVGElement svg = DOMHelper.importNode(DOMHelper.getCurrentDocument(), elt, true).cast();
61  		// IE9 bug workaround: update all SVG style elements by
62  		// adding a trailing whitespace char, otherwise IE9 will
63  		// ignore them
64  		Iterator<Text> iterator = DOMHelper.evaluateNodeListXPath(svg, ".//svg:style/text()", SVGPrefixResolver.INSTANCE);
65  		List<Text> styleTexts = new ArrayList<Text>();
66  		while(iterator.hasNext()) {
67  			Text styleText = iterator.next();
68  			styleTexts.add(styleText);
69  		}
70  		for (Text styleText : styleTexts) {
71  			styleText.<Text>cast().setData(styleText.<Text>cast().getData() + " ");
72  		}
73  		
74      	return svg;
75  	}
76  
77  }