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.neko;
018:
019: import org.apache.jetspeed.rewriter.Rewriter;
020: import org.apache.xerces.xni.Augmentations;
021: import org.apache.xerces.xni.QName;
022: import org.apache.xerces.xni.XMLAttributes;
023: import org.apache.xerces.xni.XMLString;
024: import org.apache.xerces.xni.XNIException;
025: import org.cyberneko.html.filters.DefaultFilter;
026:
027: /**
028: * <p>
029: * URLRewriterFilter
030: * </p>
031: * <p>
032: *
033: * </p>
034: *
035: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
036: * @version $Id: URLRewriterFilter.java 517121 2007-03-12 07:45:49Z ate $
037: *
038: */
039: public class URLRewriterFilter extends DefaultFilter {
040:
041: private Rewriter rewriter;
042:
043: /**
044: *
045: */
046: public URLRewriterFilter(Rewriter rewriter) {
047: super ();
048: this .rewriter = rewriter;
049: }
050:
051: /**
052: * <p>
053: * startElement
054: * </p>
055: *
056: * @see org.apache.xerces.xni.XMLDocumentHandler#startElement(org.apache.xerces.xni.QName,
057: * org.apache.xerces.xni.XMLAttributes,
058: * org.apache.xerces.xni.Augmentations)
059: * @param element
060: * @param attrs
061: * @param augs
062: * @throws org.apache.xerces.xni.XNIException
063: */
064: public void startElement(QName element, XMLAttributes attrs,
065: Augmentations augs) throws XNIException {
066: if (false == rewriter.enterSimpleTagEvent(element.rawname,
067: new XMLAttributesWrapper(attrs))) {
068: doRewrite(element, attrs);
069: String appended = rewriter.exitSimpleTagEvent(
070: element.rawname, new XMLAttributesWrapper(attrs));
071: if (null != appended) {
072: //TODO: implement this!
073: }
074: }
075:
076: super .startElement(element, attrs, augs);
077: }
078:
079: /**
080: * <p>
081: * doRewrite
082: * </p>
083: *
084: * @param element
085: * @param attrs
086: */
087: protected void doRewrite(QName element, XMLAttributes attrs) {
088: if (element.rawname.equals("A")) {
089: rewriteAttribute("href", attrs);
090: } else if (element.rawname.equals("FORM")) {
091: rewriteAttribute("action", attrs);
092: }
093: }
094:
095: protected void rewriteAttribute(String attrName,
096: XMLAttributes attributes) {
097:
098: String uri = attributes.getValue(attrName);
099:
100: if (uri != null) {
101: // attributes.setValue(attributes.getIndex(attrName), urlGenerator.createUrl(uri));
102:
103: }
104:
105: }
106:
107: /**
108: * <p>
109: * emptyElement
110: * </p>
111: *
112: * @see org.apache.xerces.xni.XMLDocumentHandler#emptyElement(org.apache.xerces.xni.QName, org.apache.xerces.xni.XMLAttributes, org.apache.xerces.xni.Augmentations)
113: * @param arg0
114: * @param arg1
115: * @param arg2
116: * @throws org.apache.xerces.xni.XNIException
117: */
118: public void emptyElement(QName element, XMLAttributes attrs,
119: Augmentations arg2) throws XNIException {
120: doRewrite(element, attrs);
121: super .emptyElement(element, attrs, arg2);
122: }
123:
124: /**
125: * <p>
126: * comment
127: * </p>
128: *
129: * @see org.apache.xerces.xni.XMLDocumentHandler#comment(org.apache.xerces.xni.XMLString, org.apache.xerces.xni.Augmentations)
130: * @param comment
131: * @param augs
132: * @throws org.apache.xerces.xni.XNIException
133: */
134: public void comment(XMLString comment, Augmentations augs)
135: throws XNIException {
136: if (!rewriter.shouldRemoveComments()) {
137: super .comment(comment, augs);
138: }
139:
140: }
141:
142: /**
143: * <p>
144: * endElement
145: * </p>
146: *
147: * @see org.apache.xerces.xni.XMLDocumentHandler#endElement(org.apache.xerces.xni.QName, org.apache.xerces.xni.Augmentations)
148: * @param arg0
149: * @param arg1
150: * @throws org.apache.xerces.xni.XNIException
151: */
152: public void endElement(QName element, Augmentations augs)
153: throws XNIException {
154: super .endElement(element, augs);
155: }
156:
157: /**
158: * <p>
159: * characters
160: * </p>
161: *
162: * @see org.apache.xerces.xni.XMLDocumentHandler#characters(org.apache.xerces.xni.XMLString, org.apache.xerces.xni.Augmentations)
163: * @param arg0
164: * @param arg1
165: * @throws org.apache.xerces.xni.XNIException
166: */
167: public void characters(XMLString text, Augmentations arg1)
168: throws XNIException {
169: if (!(text.ch[0] == '>')
170: && !rewriter.enterText(text.ch, text.offset)) {
171: super.characters(text, arg1);
172: }
173: }
174: }
|