Source code: com/steadystate/css/dom/CSSPageRuleImpl.java
1 /*
2 * CSSPageRuleImpl.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: CSSPageRuleImpl.java,v 1.1.1.1 2003/12/28 21:22:49 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 * TO DO:
43 * Implement setSelectorText()
44 *
45 * @author David Schweinsberg
46 * @version $Release$
47 */
48 public class CSSPageRuleImpl implements CSSPageRule, Serializable {
49
50 private CSSStyleSheetImpl _parentStyleSheet = null;
51 private CSSRule _parentRule = null;
52 private String _ident = null;
53 private String _pseudoPage = null;
54 private CSSStyleDeclaration _style = null;
55
56 public CSSPageRuleImpl(
57 CSSStyleSheetImpl parentStyleSheet,
58 CSSRule parentRule,
59 String ident,
60 String pseudoPage) {
61 _parentStyleSheet = parentStyleSheet;
62 _parentRule = parentRule;
63 _ident = ident;
64 _pseudoPage = pseudoPage;
65 }
66
67 public short getType() {
68 return PAGE_RULE;
69 }
70
71 public String getCssText() {
72 String sel = getSelectorText();
73 return "@page "
74 + sel + ((sel.length() > 0) ? " " : "")
75 + getStyle().getCssText();
76 }
77
78 public void setCssText(String cssText) throws DOMException {
79 if (_parentStyleSheet != null && _parentStyleSheet.isReadOnly()) {
80 throw new DOMExceptionImpl(
81 DOMException.NO_MODIFICATION_ALLOWED_ERR,
82 DOMExceptionImpl.READ_ONLY_STYLE_SHEET);
83 }
84
85 try {
86 InputSource is = new InputSource(new StringReader(cssText));
87 CSSOMParser parser = new CSSOMParser();
88 CSSRule r = parser.parseRule(is);
89
90 // The rule must be a page rule
91 if (r.getType() == CSSRule.PAGE_RULE) {
92 _ident = ((CSSPageRuleImpl)r)._ident;
93 _pseudoPage = ((CSSPageRuleImpl)r)._pseudoPage;
94 _style = ((CSSPageRuleImpl)r)._style;
95 } else {
96 throw new DOMExceptionImpl(
97 DOMException.INVALID_MODIFICATION_ERR,
98 DOMExceptionImpl.EXPECTING_PAGE_RULE);
99 }
100 } catch (CSSException e) {
101 throw new DOMExceptionImpl(
102 DOMException.SYNTAX_ERR,
103 DOMExceptionImpl.SYNTAX_ERROR,
104 e.getMessage());
105 } catch (IOException e) {
106 throw new DOMExceptionImpl(
107 DOMException.SYNTAX_ERR,
108 DOMExceptionImpl.SYNTAX_ERROR,
109 e.getMessage());
110 }
111 }
112
113 public CSSStyleSheet getParentStyleSheet() {
114 return _parentStyleSheet;
115 }
116
117 public CSSRule getParentRule() {
118 return _parentRule;
119 }
120
121 public String getSelectorText() {
122 return ((_ident != null) ? _ident : "")
123 + ((_pseudoPage != null) ? ":" + _pseudoPage : "");
124 }
125
126 public void setSelectorText(String selectorText) throws DOMException {
127 }
128
129 public CSSStyleDeclaration getStyle() {
130 return _style;
131 }
132
133 protected void setIdent(String ident) {
134 _ident = ident;
135 }
136
137 protected void setPseudoPage(String pseudoPage) {
138 _pseudoPage = pseudoPage;
139 }
140
141 public void setStyle(CSSStyleDeclarationImpl style) {
142 _style = style;
143 }
144 }