001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.rewriter.html;
018:
019: import java.util.Enumeration;
020:
021: import javax.swing.text.MutableAttributeSet;
022: import javax.swing.text.html.HTML;
023: import javax.swing.text.html.HTML.Attribute;
024:
025: import org.apache.jetspeed.rewriter.MutableAttributes;
026:
027: /**
028: * SwingAttributes
029: *
030: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
031: * @version $Id: SwingAttributes.java 516448 2007-03-09 16:25:47Z ate $
032: */
033: public class SwingAttributes implements MutableAttributes {
034: MutableAttributeSet swingset;
035:
036: public SwingAttributes(MutableAttributeSet swingset) {
037: this .swingset = swingset;
038: }
039:
040: /* (non-Javadoc)
041: * @see org.xml.sax.Attributes#getLength()
042: */
043: public int getLength() {
044: return swingset.getAttributeCount();
045: }
046:
047: /* (non-Javadoc)
048: * @see org.xml.sax.Attributes#getURI(int)
049: */
050: public String getURI(int index) {
051: return "";
052: }
053:
054: /* (non-Javadoc)
055: * @see org.xml.sax.Attributes#getLocalName(int)
056: */
057: public String getLocalName(int index) {
058: Enumeration e = swingset.getAttributeNames();
059: int ix = 0;
060: while (e.hasMoreElements()) {
061: Object object = e.nextElement();
062: if (ix == index) {
063: return object.toString();
064: }
065: }
066: return null;
067: }
068:
069: /* (non-Javadoc)
070: * @see org.xml.sax.Attributes#getQName(int)
071: */
072: public String getQName(int index) {
073: return getLocalName(index);
074: }
075:
076: /* (non-Javadoc)
077: * @see org.xml.sax.Attributes#getType(int)
078: */
079: public String getType(int index) {
080: return "CDATA";
081: }
082:
083: /* (non-Javadoc)
084: * @see org.xml.sax.Attributes#getValue(int)
085: */
086: public String getValue(int index) {
087: Enumeration e = swingset.getAttributeNames();
088: int ix = 0;
089: while (e.hasMoreElements()) {
090: Object object = e.nextElement();
091: if (ix == index) {
092: return (String) swingset.getAttribute(object);
093: }
094: }
095: return null;
096: }
097:
098: /* (non-Javadoc)
099: * @see org.xml.sax.Attributes#getIndex(java.lang.String, java.lang.String)
100: */
101: public int getIndex(String uri, String localPart) {
102: return getIndex(localPart);
103: }
104:
105: /* (non-Javadoc)
106: * @see org.xml.sax.Attributes#getIndex(java.lang.String)
107: */
108: public int getIndex(String qName) {
109: Enumeration e = swingset.getAttributeNames();
110: int ix = 0;
111: while (e.hasMoreElements()) {
112: String name = (String) e.nextElement();
113: if (name.equalsIgnoreCase(qName)) {
114: return ix;
115: }
116: }
117: return -1;
118: }
119:
120: /* (non-Javadoc)
121: * @see org.xml.sax.Attributes#getType(java.lang.String, java.lang.String)
122: */
123: public String getType(String uri, String localName) {
124: return "CDATA";
125: }
126:
127: /* (non-Javadoc)
128: * @see org.xml.sax.Attributes#getType(java.lang.String)
129: */
130: public String getType(String qName) {
131: return "CDATA";
132: }
133:
134: /* (non-Javadoc)
135: * @see org.xml.sax.Attributes#getValue(java.lang.String, java.lang.String)
136: */
137: public String getValue(String uri, String localName) {
138: return getValue(localName);
139: }
140:
141: /* (non-Javadoc)
142: * @see org.xml.sax.Attributes#getValue(java.lang.String)
143: */
144: public String getValue(String qName) {
145: Attribute att = HTML.getAttributeKey(qName.toLowerCase());
146: return (String) swingset.getAttribute(att);
147: }
148:
149: /* (non-Javadoc)
150: * @see org.apache.jetspeed.cps.rewriter.MutableAttributes#addAttribute(java.lang.String, java.lang.Object)
151: */
152: public void addAttribute(String name, Object value) {
153: Attribute att = HTML.getAttributeKey(name.toLowerCase());
154: swingset.addAttribute(att, value);
155: }
156:
157: }
|