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 org.vectomatic.dom.svg.utils.DOMHelper;
21  import org.vectomatic.dom.svg.utils.ParserException;
22  
23  import com.google.gwt.dom.client.Element;
24  import com.google.gwt.dom.client.Text;
25  
26  /**
27   * Internal class to wrap DOM parser implementations for mozilla
28   * @author laaglu
29   */
30  public class SVGParserImplMozilla extends SVGParserImpl {
31  	/**
32  	 * Parses the supplied SVG text into a document
33  	 * @param rawSvg
34  	 * raw xml to be parsed
35  	 * @param enableScripts
36  	 * true to enable embedded scripts, false otherwise
37  	 * @return
38  	 * the document resulting from the parse
39  	 */
40  	@Override
41  	public final SVGSVGElement parse(String rawSvg, boolean enableScripts) throws ParserException {
42  		if (isIE()) {
43  			return parseIE(rawSvg, enableScripts);
44  		}
45  		SVGDocument doc = parseFromString(rawSvg, "text/xml").cast();
46  		Element elt = doc.getDocumentElement();
47  		if ("parsererror".equals(DOMHelper.getLocalName(elt))) {
48  			String message = "Parsing error";
49  			if (elt.getFirstChild() != null) {
50  				message = elt.getFirstChild().<Text>cast().getData();
51  			}
52  			throw new ParserException(ParserException.Type.NotWellFormed, message);
53  		}
54  		SVGSVGElement svg = DOMHelper.importNode(DOMHelper.getCurrentDocument(), elt, true).cast();
55  		// For some reason xlink:href are not correctly evaluated in
56  		// some cases in mozilla. If one clones the node this seems
57  		// to solve the problem
58      	svg = svg.cloneNode(true).<SVGSVGElement>cast();
59  		return enableScripts ? enableScriptElements(svg) : svg;
60  	}
61  }