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 org.vectomatic.dom.svg.utils.DOMHelper;
21  import org.vectomatic.dom.svg.utils.ParserException;
22  import org.vectomatic.dom.svg.utils.XPathPrefixResolver;
23  
24  import com.google.gwt.dom.client.Element;
25  import com.google.gwt.dom.client.Text;
26  
27  /**
28   * Internal class to wrap DOM parser implementations for webkit based browsers
29   * @author laaglu
30   */
31  public class SVGParserImplWebkit extends SVGParserImpl {
32  	/**
33  	 * Parses the supplied SVG text into a document
34  	 * @param rawSvg
35  	 * raw xml to be parsed
36  	 * @param enableScripts
37  	 * true to enable embedded scripts, false otherwise
38  	 * @return
39  	 * the document resulting from the parse
40  	 */
41  	@Override
42  	public final SVGSVGElement parse(String rawSvg, boolean enableScripts) throws ParserException {
43  		if (isIE()) {
44  			return parseIE(rawSvg, enableScripts);
45  		}
46  		SVGDocument doc = parseFromString(rawSvg, "text/xml").cast();
47  		Element elt = doc.getDocumentElement();
48  		if ("parsererror".equals(DOMHelper.getLocalName(elt))) {
49  			String message = "Parsing error";
50  			if (elt.getFirstChild() != null) {
51  				message = elt.getFirstChild().<Text>cast().getData();
52  			}
53  			throw new ParserException(ParserException.Type.NotWellFormed, message);
54  		} else if ("html".equals(DOMHelper.getLocalName(elt))) {
55  			String message = DOMHelper.evaluateStringXPath(elt, "./x:body/x:parsererror/x:div/text()", new XPathPrefixResolver() {
56  				@Override
57  				public String resolvePrefix(String prefix) {
58  					if ("x".equals(prefix)) {
59  						return "http://www.w3.org/1999/xhtml";
60  					}
61  					return null;
62  				}
63  			});
64  			throw new ParserException(ParserException.Type.NotWellFormed, message);
65  		}
66  		// Import + clone seem to be required for embedded QTWebkit (though they are not needed
67  		// for Chromium but there is no simple way to distinguish both)
68  		SVGSVGElement svg = DOMHelper.importNode(DOMHelper.getCurrentDocument(), elt, true).cast();
69      	svg = svg.cloneNode(true).<SVGSVGElement>cast();
70  		return enableScripts ? enableScriptElements(svg) : svg;
71  	}
72  
73  }