KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > steadystate > css > dom > MediaListImpl


1 /*
2  * MediaListImpl.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: MediaListImpl.java,v 1.2 2005/04/26 21:14:30 waldbaer Exp $
29  */

30
31 package com.steadystate.css.dom;
32
33 import java.io.IOException JavaDoc;
34 import java.io.Serializable JavaDoc;
35 import java.io.StringReader JavaDoc;
36 import java.util.*;
37 import org.w3c.dom.*;
38 import org.w3c.dom.stylesheets.*;
39 import org.w3c.css.sac.*;
40
41 import com.steadystate.css.parser.SACParser;
42
43 /**
44  *
45  * @author David Schweinsberg
46  * @version $Release$
47  */

48 public class MediaListImpl implements MediaList, Serializable JavaDoc {
49
50     private Vector _media = new Vector();
51     
52     private void setMedia(SACMediaList mediaList)
53     {
54         for (int i = 0; i < mediaList.getLength(); i++)
55         {
56             this._media.addElement(mediaList.item(i));
57         }
58     }
59
60     public MediaListImpl(SACMediaList mediaList)
61     {
62         this.setMedia(mediaList);
63     }
64
65     public String JavaDoc getMediaText() {
66         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
67         for (int i = 0; i < _media.size(); i++) {
68             sb.append(_media.elementAt(i).toString());
69             if (i < _media.size() - 1) {
70                 sb.append( ", " );
71             }
72         }
73         return sb.toString();
74     }
75
76     public void setMediaText(String JavaDoc mediaText) throws DOMException {
77         InputSource source = new InputSource(new StringReader JavaDoc(mediaText));
78         try
79         {
80             this.setMedia(new SACParser().parseMedia(source));
81         }
82         catch (CSSParseException e)
83         {
84             throw new DOMException(DOMException.SYNTAX_ERR,
85                 e.getLocalizedMessage());
86         }
87         catch (IOException JavaDoc e)
88         {
89             throw new DOMException(DOMException.NOT_FOUND_ERR,
90                 e.getLocalizedMessage());
91         }
92     }
93
94     public int getLength() {
95         return _media.size();
96     }
97
98     public String JavaDoc item(int index) {
99         return (index < _media.size()) ? (String JavaDoc) _media.elementAt(index) : null;
100     }
101
102     public void deleteMedium(String JavaDoc oldMedium) throws DOMException {
103         for (int i = 0; i < _media.size(); i++) {
104             String JavaDoc str = (String JavaDoc) _media.elementAt(i);
105             if (str.equalsIgnoreCase(oldMedium)) {
106                 _media.removeElementAt(i);
107                 return;
108             }
109         }
110         throw new DOMExceptionImpl(
111             DOMException.NOT_FOUND_ERR,
112             DOMExceptionImpl.NOT_FOUND);
113     }
114
115     public void appendMedium(String JavaDoc newMedium) throws DOMException {
116         _media.addElement(newMedium);
117     }
118
119     public String JavaDoc toString() {
120         return getMediaText();
121     }
122 }
123
Popular Tags