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.ElementRemover;
026:
027: /**
028: * <p>
029: * CallbackElementRemover
030: * </p>
031: * <p>
032: * Extended version of the NekoHTML ElementRemover which provides
033: * tag stripping/removal based on Rewriter settings.
034: * </p>
035: *
036: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
037: * @version $Id: CallbackElementRemover.java 517719 2007-03-13 15:05:48Z ate $
038: *
039: */
040: public class CallbackElementRemover extends ElementRemover {
041:
042: private Rewriter rewriter;
043:
044: /**
045: * Construct with reference to the rewriter context to consult for rewriting advice
046: */
047: public CallbackElementRemover(Rewriter rewriter) {
048: super ();
049:
050: this .rewriter = rewriter;
051: }
052:
053: // Base Class Protocol
054:
055: /**
056: * <p>
057: * comment
058: * </p>
059: *
060: * @see org.apache.xerces.xni.XMLDocumentHandler#comment(org.apache.xerces.xni.XMLString text, org.apache.xerces.xni.Augmentations augs)
061: * @param text
062: * @param augs
063: * @throws org.apache.xerces.xni.XNIException
064: */
065: public void comment(XMLString text, Augmentations augs)
066: throws XNIException {
067: if (rewriter.shouldRemoveComments())
068: return;
069: super .comment(text, augs);
070: }
071:
072: /**
073: * <p>
074: * emptyElement
075: * </p>
076: *
077: * @see org.apache.xerces.xni.XMLDocumentHandler#emptyElement(org.apache.xerces.xni.QName,
078: * org.apache.xerces.xni.XMLAttributes,
079: * org.apache.xerces.xni.Augmentations)
080: * @param element
081: * @param arg1
082: * @param arg2
083: * @throws org.apache.xerces.xni.XNIException
084: */
085: public void emptyElement(QName element, XMLAttributes attrs,
086: Augmentations arg2) throws XNIException {
087: processTag(element, attrs);
088: super .emptyElement(element, attrs, arg2);
089: }
090:
091: /**
092: * <p>
093: * startElement
094: * </p>
095: *
096: * @see org.apache.xerces.xni.XMLDocumentHandler#startElement(org.apache.xerces.xni.QName,
097: * org.apache.xerces.xni.XMLAttributes,
098: * org.apache.xerces.xni.Augmentations)
099: * @param element
100: * @param arg1
101: * @param arg2
102: * @throws org.apache.xerces.xni.XNIException
103: */
104: public void startElement(QName element, XMLAttributes attrs,
105: Augmentations arg2) throws XNIException {
106: processTag(element, attrs);
107: super .startElement(element, attrs, arg2);
108: }
109:
110: // Support Methods
111:
112: /**
113: * <p>
114: * processTag
115: * </p>
116: *
117: * @param tag
118: */
119: protected void processTag(QName element, XMLAttributes attrs) {
120: String tag = element.rawname.toLowerCase();
121: if (fRemovedElements.contains(tag)) {
122: // alread removed
123: return;
124: } else if (rewriter.shouldStripTag(tag)) {
125: // first time for this tag...
126: // strip - remove tag and any text associated with it
127: removeElement(tag);
128: return;
129: } else if (rewriter.shouldRemoveTag(tag)) {
130: // BOZO - block intentially left EMPTY
131:
132: // first time for this tag...
133: // remove - no directive necessary, the default behavior of ElementRemover is to drop tags that it does not know about (but the assocated text will remain)
134: return;
135: }
136:
137: // OTHERWISE - explicitly accept (keep tag and associated text)
138: // NOTE: even if fAcceptedElements contains the tag already, we need to reset the attribute names for this invocation context
139: rewriter.enterConvertTagEvent(tag, new XMLAttributesWrapper(
140: attrs));
141: acceptElement(tag, getAttributeNames(attrs));
142: }
143:
144: protected String[] getAttributeNames(XMLAttributes attrs) {
145: int length = attrs != null ? attrs.getLength() : 0;
146: String[] names = length > 0 ? new String[length] : null;
147:
148: for (int i = 0, limit = length; i < limit; i++) {
149: names[i] = attrs.getQName(i);
150: }
151: return names;
152: }
153: }
|