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.utils;
19  
20  import org.vectomatic.dom.svg.OMCSSValue;
21  import org.vectomatic.dom.svg.OMNode;
22  import org.vectomatic.dom.svg.OMSVGDocument;
23  import org.vectomatic.dom.svg.OMSVGPaint;
24  import org.vectomatic.dom.svg.OMSVGSVGElement;
25  import org.vectomatic.dom.svg.impl.DashArrayParser;
26  import org.vectomatic.dom.svg.impl.SVGDocument;
27  import org.vectomatic.dom.svg.impl.SVGPaintParser;
28  import org.vectomatic.dom.svg.impl.SVGParserImpl;
29  import org.vectomatic.dom.svg.impl.SVGSVGElement;
30  
31  import com.google.gwt.core.client.GWT;
32  import com.google.gwt.core.client.JavaScriptException;
33  
34  /**
35   * Class to parse SVG documents and instantiate SVG documents
36   * @author laaglu
37   */
38  public class OMSVGParser {
39  	private static final SVGParserImpl impl = GWT.create(SVGParserImpl.class);
40  
41  	/**
42  	 * Creates a new empty SVG document
43  	 * @return
44  	 * a new empty SVG document
45  	 */
46  	public static final OMSVGDocument createDocument() {
47  		SVGDocument doc = DOMHelper.createDocument(SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_SVG_TAG).cast();
48      	return OMNode.convert(doc);
49  	}
50  
51  	/**
52  	 * Returns the current document, as an SVG document
53  	 * @return the current document, as an SVG document
54  	 */
55  	public static final OMSVGDocument currentDocument() {
56      	return OMNode.convert(DOMHelper.getCurrentDocument());
57  	}
58  
59  	/**
60  	 * Parses the supplied SVG text into a document. If the SVG contains embedded
61  	 * scripts, they will be enabled.
62  	 * @param rawSvg
63  	 * raw xml to be parsed
64  	 * @return
65  	 * the document resulting from the parse
66  	 * @throws ParserException
67  	 * if the document is not well-formed or is not SVG
68  	 */
69  	public static final OMSVGSVGElement parse(String rawSvg) throws ParserException {
70  		return parse(rawSvg, true);
71  	}
72  
73  	/**
74  	 * Parses the supplied SVG text into a document
75  	 * @param rawSvg
76  	 * raw xml to be parsed
77  	 * @param enableScripts
78  	 * true to enable embedded scripts, false otherwise
79  	 * @return
80  	 * the document resulting from the parse
81  	 * @throws ParserException
82  	 * if the document is not well-formed or is not SVG
83  	 */
84  	public static final OMSVGSVGElement parse(String rawSvg, boolean enableScripts) throws ParserException {
85  		SVGSVGElement elt = impl.parse(rawSvg, enableScripts);
86  		if (!SVGConstants.SVG_NAMESPACE_URI.equals(DOMHelper.getNamespaceURI(elt))) {
87  			throw new ParserException(ParserException.Type.NotSvg, "Invalid root element: {" + DOMHelper.getNamespaceURI(elt) + "}" + elt.getTagName());
88  		}
89  		return new OMSVGSVGElement(elt);
90  	}
91  
92  	/**
93  	 * Parses an SVG paint value. SVG paint value are
94  	 * used for the 'fill' and the 'stroke' SVG attributes.
95  	 * @param cssText
96  	 * The value to parse
97  	 * @return
98  	 * The resulting paint object
99  	 * @throws JavaScriptException
100 	 * If the string to parse is not a valid paint value
101 	 */
102 	public static OMSVGPaint parsePaint(String cssText) throws JavaScriptException {
103 		return SVGPaintParser.INSTANCE.parse(cssText);
104 	}
105 	
106 	/**
107 	 * Parses an SVG dasharray value
108 	 * @param cssText
109 	 * The dash array to parse
110 	 * @return
111 	 * The resulting dasharray
112 	 * @throws JavaScriptException
113 	 */
114 	public static OMCSSValue parseDashArray(String cssText) throws JavaScriptException {
115 		return DashArrayParser.INSTANCE.parse(cssText);
116 	}
117 }