/* Copyright 2008 The JA-SIG Collaborative. All rights reserved. * See license distributed with this file and * available online at http://www.uportal.org/license.html */ package org.jasig.portal; import java.io.IOException; import java.io.Writer; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.jasig.portal.layout.IUserLayoutManager; public class ChannelTitleFilterPrintWriter extends Writer { private Writer out; private StringBuffer buffer = new StringBuffer(); private ChannelManager cm; private IUserLayoutManager ulm; private static Pattern channelTitlePattern = Pattern.compile("\"UP:CHANNEL_TITLE-((u[0-9]+)?(l[0-9]+)?(n[0-9]+))\""); public ChannelTitleFilterPrintWriter(Writer out, ChannelManager cm, IUserLayoutManager ulm) { this.out = out; this.cm = cm; this.ulm = ulm; } private void checkBuffer() throws IOException { if (buffer.toString().contains("UP:CHANNEL_TITLE-")) { Matcher m = channelTitlePattern.matcher(buffer.toString()); List replacements = new LinkedList(); while (m.find()) { for (int i=1; i<=m.groupCount(); i++) { System.out.print(m.group(i) + " - "); } System.out.println(); String channelId = m.group(1); String title = cm.getChannelTitle(channelId); if (title == null) { title = ulm.getNode(channelId).getName(); } replacements.add(new ChannelTitleReplacement(m.start(), m.end(), title)); } ListIterator itr = replacements.listIterator(); while (itr.hasNext()) {itr.next();} while (itr.hasPrevious()) { ChannelTitleReplacement replacement = itr.previous(); buffer.replace(replacement.start+1, replacement.end-1, replacement.replacement); } out.write(buffer.toString()); buffer.setLength(0); } } public Writer append(char c) throws IOException { buffer.append(c); if (c == '"') { checkBuffer(); } return this; } public Writer append(CharSequence csq, int start, int end) throws IOException { int previousLength = buffer.length(); buffer.append(csq, start, end); if (buffer.substring(previousLength).contains("\"")) { checkBuffer(); } return this; } public Writer append(CharSequence csq) throws IOException { int previousLength = buffer.length(); buffer.append(csq); if (buffer.substring(previousLength).contains("\"")) { checkBuffer(); } return this; } public void close() throws IOException { out.close(); } public boolean equals(Object obj) { return out.equals(obj); } public void flush() throws IOException { out.write(buffer.toString()); buffer.setLength(0); out.flush(); } public int hashCode() { return out.hashCode(); } public String toString() { return out.toString(); } public void write(char[] cbuf, int off, int len) throws IOException { int previousLength = buffer.length(); buffer.append(cbuf, off, len); if (buffer.substring(previousLength).contains("\"")) { checkBuffer(); } } public void write(char[] cbuf) throws IOException { int previousLength = buffer.length(); buffer.append(cbuf); if (buffer.substring(previousLength).contains("\"")) { checkBuffer(); } } public void write(int c) throws IOException { buffer.append((char)c); if (c == '"') { checkBuffer(); } } public void write(String str, int off, int len) throws IOException { int previousLength = buffer.length(); buffer.append(str, off, len); if (buffer.substring(previousLength).contains("\"")) { checkBuffer(); } } public void write(String str) throws IOException { int previousLength = buffer.length(); buffer.append(str); if (buffer.substring(previousLength).contains("\"")) { checkBuffer(); } } private static class ChannelTitleReplacement { public int start; public int end; public String replacement; public ChannelTitleReplacement(int start, int end, String replacement) { this.start = start; this.end = end; this.replacement = replacement; } } }