Source code: com/steadystate/css/dom/CSSCharsetRuleImpl.java
1 /*
2 * CSSCharsetRule.java
3 *
4 * Steady State CSS2 Parser
5 *
6 * Copyright (C) 1999, 2002 Steady State Software Ltd. All rights reserved.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * To contact the authors of the library, write to Steady State Software Ltd.,
23 * 49 Littleworth, Wing, Buckinghamshire, LU7 0JX, England
24 *
25 * http://www.steadystate.com/css/
26 * mailto:css@steadystate.co.uk
27 *
28 * $Id: CSSCharsetRuleImpl.java,v 1.1.1.1 2003/12/28 21:22:47 davidsch Exp $
29 */
30
31 package com.steadystate.css.dom;
32
33 import java.io.IOException;
34 import java.io.Serializable;
35 import java.io.StringReader;
36 import org.w3c.dom.*;
37 import org.w3c.dom.css.*;
38 import org.w3c.css.sac.*;
39 import com.steadystate.css.parser.*;
40
41 /**
42 *
43 * @author David Schweinsberg
44 * @version $Release$
45 */
46 public class CSSCharsetRuleImpl implements CSSCharsetRule, Serializable {
47
48 CSSStyleSheetImpl _parentStyleSheet = null;
49 CSSRule _parentRule = null;
50 String _encoding = null;
51
52 public CSSCharsetRuleImpl(
53 CSSStyleSheetImpl parentStyleSheet,
54 CSSRule parentRule,
55 String encoding) {
56 _parentStyleSheet = parentStyleSheet;
57 _parentRule = parentRule;
58 _encoding = encoding;
59 }
60
61 public short getType() {
62 return CHARSET_RULE;
63 }
64
65 public String getCssText() {
66 return "@charset \"" + getEncoding() + "\";";
67 }
68
69 public void setCssText(String cssText) throws DOMException {
70 if (_parentStyleSheet != null && _parentStyleSheet.isReadOnly()) {
71 throw new DOMExceptionImpl(
72 DOMException.NO_MODIFICATION_ALLOWED_ERR,
73 DOMExceptionImpl.READ_ONLY_STYLE_SHEET);
74 }
75
76 try {
77 InputSource is = new InputSource(new StringReader(cssText));
78 CSSOMParser parser = new CSSOMParser();
79 CSSRule r = parser.parseRule(is);
80
81 // The rule must be a charset rule
82 if (r.getType() == CSSRule.CHARSET_RULE) {
83 _encoding = ((CSSCharsetRuleImpl)r)._encoding;
84 } else {
85 throw new DOMExceptionImpl(
86 DOMException.INVALID_MODIFICATION_ERR,
87 DOMExceptionImpl.EXPECTING_CHARSET_RULE);
88 }
89 } catch (CSSException e) {
90 throw new DOMExceptionImpl(
91 DOMException.SYNTAX_ERR,
92 DOMExceptionImpl.SYNTAX_ERROR,
93 e.getMessage());
94 } catch (IOException e) {
95 throw new DOMExceptionImpl(
96 DOMException.SYNTAX_ERR,
97 DOMExceptionImpl.SYNTAX_ERROR,
98 e.getMessage());
99 }
100 }
101
102 public CSSStyleSheet getParentStyleSheet() {
103 return _parentStyleSheet;
104 }
105
106 public CSSRule getParentRule() {
107 return _parentRule;
108 }
109
110 public String getEncoding() {
111 return _encoding;
112 }
113
114 public void setEncoding(String encoding) throws DOMException {
115 _encoding = encoding;
116 }
117 }