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
61 * @param rawSvg
62 * raw xml to be parsed
63 * @return
64 * the document resulting from the parse
65 * @throws ParserException
66 * if the document is not well-formed or is not SVG
67 */
68 public static final OMSVGSVGElement parse(String rawSvg) throws ParserException {
69 SVGSVGElement elt = impl.parse(rawSvg);
70 if (!SVGConstants.SVG_NAMESPACE_URI.equals(DOMHelper.getNamespaceURI(elt))) {
71 throw new ParserException(ParserException.Type.NotSvg, "Invalid root element: {" + DOMHelper.getNamespaceURI(elt) + "}" + elt.getTagName());
72 }
73 return new OMSVGSVGElement(elt);
74 }
75
76 /**
77 * Parses an SVG paint value. SVG paint value are
78 * used for the 'fill' and the 'stroke' SVG attributes.
79 * @param cssText
80 * The value to parse
81 * @return
82 * The resulting paint object
83 * @throws JavaScriptException
84 * If the string to parse is not a valid paint value
85 */
86 public static OMSVGPaint parsePaint(String cssText) throws JavaScriptException {
87 return SVGPaintParser.INSTANCE.parse(cssText);
88 }
89
90 /**
91 * Parses an SVG dasharray value
92 * @param cssText
93 * The dash array to parse
94 * @return
95 * The resulting dasharray
96 * @throws JavaScriptException
97 */
98 public static OMCSSValue parseDashArray(String cssText) throws JavaScriptException {
99 return DashArrayParser.INSTANCE.parse(cssText);
100 }
101 }