001: package com.sun.portal.rewriter.engines.xml.parser;
002:
003: import com.sun.portal.rewriter.Translator;
004: import com.sun.portal.rewriter.TranslatorHelper;
005: import com.sun.portal.rewriter.engines.PageContent;
006: import com.sun.portal.rewriter.rom.Rule;
007: import com.sun.portal.rewriter.rom.RuleSet;
008: import com.sun.portal.rewriter.rom.RuleSetHelper;
009: import com.sun.portal.rewriter.rom.common.Attribute;
010: import com.sun.portal.rewriter.rom.common.AttributeRule;
011: import com.sun.portal.rewriter.rom.common.TagText;
012: import com.sun.portal.rewriter.rom.common.TagTextRule;
013: import com.sun.portal.rewriter.util.collections.ListMap;
014: import com.sun.portal.rewriter.util.re.Pattern;
015:
016: import java.io.FileNotFoundException;
017: import java.io.IOException;
018: import java.io.Reader;
019: import java.io.StringReader;
020: import java.net.MalformedURLException;
021: import java.util.Stack;
022:
023: /**
024: * @author Noble Paul, 4th Sep 2003
025: */
026: public class XMLReaderWrapper implements XMLReader, XMLParserListener {
027:
028: private int attStartOffset = -1;
029: private int piStartOffset = -1;
030: private int startOfCDATA = -1;
031: private final StringBuffer buffer = new StringBuffer();
032: private final Stack elementStack = new Stack();
033: private final Attribute attribute = new Attribute("");
034: private final TagText tagText = new TagText("");
035: private final Stack attributesStack = new Stack();
036: private final String uri;
037:
038: private final XMLReader delegate;
039: private final Translator translator;
040:
041: private final RuleSet ruleSet;
042:
043: private final PageContent pageContent;
044:
045: public XMLReaderWrapper(final PageContent aPageContent,
046: final RuleSet aRuleSet, final Translator aTranslator) {
047: delegate = new StandardXMLReader(new StringReader(aPageContent
048: .getOriginalContent()));
049: ruleSet = aRuleSet;
050: translator = aTranslator;
051: uri = translator.getPageSpec().getPageURI().getFullFileURI();
052: pageContent = aPageContent;
053:
054: }//constructor
055:
056: public void startElement(String key) {
057: elementStack.push(key);
058: attributesStack.push(ListMap.EMPTY_LISTMAP);
059: }
060:
061: public void endElement() {
062: elementStack.pop();
063: attributesStack.pop();
064: }
065:
066: public boolean startAttribute(String key) {
067: attStartOffset = buffer.length();
068: return true;
069: }
070:
071: public int getOffset() {
072: return buffer.length();
073: }
074:
075: public void startPCData() {
076: }
077:
078: public void endPCData(int startOffset) {
079: if (!isMatchTagtextRule())
080: return;
081: String lPCData = buffer.substring(startOffset);
082: buffer.delete(startOffset, buffer.length());
083: String lResult = translator.translate(lPCData);
084: buffer.append(lResult);
085: }
086:
087: private boolean isMatchTagtextRule() {
088: String lTag = (String) elementStack.peek();
089: tagText.recycleMatchee(lTag, (ListMap) attributesStack.peek(),
090: uri);
091: TagTextRule lTextMatch = (TagTextRule) RuleSetHelper
092: .findXMLTagTextMatch(ruleSet, tagText);
093: return !(lTextMatch == null);
094: }
095:
096: public String toString() {
097: return buffer.toString();
098: }
099:
100: public void endParsing() {
101: try {
102: while (!delegate.atEOF())
103: buffer.append(delegate.read());
104: } catch (IOException e) //this should never happen
105: {
106: throw new RuntimeException("Unexpected Exception : "
107: + e.getMessage());
108:
109: }
110: pageContent.getResultBuffer().append(buffer);
111:
112: }
113:
114: public void endAttribute(String key, String value) {
115: try {
116: String tag = (String) elementStack.peek();
117: attribute.recycleMatchee(key, tag, Rule.URL, uri);
118: String lResult;
119:
120: AttributeRule lMatch = (AttributeRule) RuleSetHelper
121: .findXMLAttributeMatch(ruleSet, attribute);
122: String lValue = buffer.substring(attStartOffset + 1, buffer
123: .length() - 1);
124:
125: ListMap lAttributes = getAttributesListMap();
126: lAttributes.put(key, lValue);
127:
128: if (lMatch == null)
129: return;
130:
131: Pattern[] lList = lMatch.getParsedPatterns();
132: if (lList.length == 0) //simple URL to be translated
133: {
134: lResult = translator.translate(lValue);
135: } else //string with URL Patterns need to be translated
136: {
137: lResult = TranslatorHelper.translateSubStrings(lList,
138: lValue, translator);
139: }
140:
141: buffer.replace(attStartOffset + 1, buffer.length() - 1,
142: lResult);
143: } finally {
144: attStartOffset = -1;
145: }
146:
147: }
148:
149: private ListMap getAttributesListMap() {
150: ListMap lAttributes = (ListMap) attributesStack.peek();
151: if (!lAttributes.isEmpty())
152: return lAttributes;
153:
154: lAttributes = new ListMap();
155: attributesStack.pop();
156: attributesStack.push(lAttributes);
157: return lAttributes;
158: }
159:
160: /**
161: * Reads a character.
162: */
163: public char read() throws IOException {
164: char c = delegate.read();
165: buffer.append(c);
166: return c;
167: }
168:
169: /**
170: * Returns true if the current stream has no more characters left to be
171: * read.
172: */
173: public boolean atEOFOfCurrentStream() throws IOException {
174: return delegate.atEOFOfCurrentStream();
175: }
176:
177: /**
178: * Returns true if there are no more characters left to be read.
179: */
180: public boolean atEOF() throws IOException {
181: return delegate.atEOF();
182: }
183:
184: /**
185: * Pushes the last character read back to the stream.
186: * @param ch the character to push back.
187: */
188: public void unread(char ch) throws IOException {
189: buffer.deleteCharAt(buffer.length() - 1);
190: delegate.unread(ch);
191: }
192:
193: public int getLineNr() {
194: return delegate.getLineNr();
195: }
196:
197: /**
198: * Opens a stream from a public and system ID.
199: *
200: * @param publicID the public ID, which may be null.
201: * @param systemID the system ID, which is never null.
202: *
203: * @throws MalformedURLException
204: * If the system ID does not contain a valid URL.
205: * @throws FileNotFoundException
206: * If the system ID refers to a local file which does not exist.
207: * @throws IOException
208: * If an error occurred opening the stream.
209: */
210: public Reader openStream(String publicID, String systemID)
211: throws MalformedURLException, FileNotFoundException,
212: IOException {
213: return delegate.openStream(publicID, systemID);
214: }
215:
216: /**
217: * Starts a new stream from a Java reader. The new stream is used
218: * temporary to read data from. If that stream is exhausted, control
219: * returns to the "parent" stream.
220: *
221: * @param reader the reader to read the new data from.
222: */
223: public void startNewStream(Reader reader) {
224: delegate.startNewStream(reader);
225: }
226:
227: public void startNewStream(Reader reader, boolean isInternalEntity) {
228: }
229:
230: public void startPI() {
231: piStartOffset = buffer.length();
232:
233: }
234:
235: public void endPI(String href) {
236: if (href.startsWith("#"))
237: return;//The case of inline stylesheet
238: try {
239: attribute.recycleMatchee("href", XMLParser.STYLESHEET_PI,
240: Rule.URL, uri);
241: AttributeRule lMatch = (AttributeRule) RuleSetHelper
242: .findXMLAttributeMatch(ruleSet, attribute);
243: String lValue = buffer.substring(piStartOffset + 1, buffer
244: .length() - 1);
245:
246: if (lMatch == null)
247: return;
248:
249: Pattern[] lList = lMatch.getParsedPatterns();
250: String lResult;
251: if (lList.length == 0) //simple URL to be translated
252: {
253: lResult = translator.translate(lValue);
254: } else //string with URL Patterns need to be translated
255: {
256: lResult = TranslatorHelper.translateSubStrings(lList,
257: lValue, translator);
258: }
259: buffer.replace(piStartOffset + 1, buffer.length() - 1,
260: lResult);
261: } finally {
262: piStartOffset = -1;
263: }
264:
265: }
266:
267: public void startCDATA() {
268: startOfCDATA = getOffset();
269: }
270:
271: public void endCDATA() {
272: if (startOfCDATA == -1)
273: return;
274: if (!isMatchTagtextRule()) {
275: startOfCDATA = -1;
276: return;
277: }
278:
279: String cdata = buffer.substring(startOfCDATA);
280: buffer.delete(startOfCDATA, buffer.length());
281: String lResult = translator.translate(cdata);
282: buffer.append(lResult);
283: startOfCDATA = -1;
284: }
285:
286: public int getStreamLevel() {
287: return delegate.getStreamLevel();
288: }
289:
290: public void setSystemID(String systemID)
291: throws MalformedURLException {
292: delegate.setSystemID(systemID);
293: }
294:
295: public void setPublicID(String publicID) {
296: delegate.setPublicID(publicID);
297: }
298:
299: public String getSystemID() {
300: return delegate.getSystemID();
301: }
302: }
|