001: package com.icesoft.faces.webapp.command;
002:
003: import com.icesoft.faces.util.DOMUtils;
004: import org.w3c.dom.Element;
005:
006: import java.io.IOException;
007: import java.io.StringWriter;
008: import java.io.Writer;
009: import java.util.Arrays;
010: import java.util.HashSet;
011: import java.util.Set;
012: import java.util.regex.Pattern;
013:
014: public class UpdateElements implements Command {
015: private final static Pattern START_CDATA = Pattern
016: .compile("<\\!\\[CDATA\\[");
017: private final static Pattern END_CDATA = Pattern.compile("\\]\\]>");
018: private Element[] updates;
019:
020: public UpdateElements(Element[] updates) {
021: this .updates = updates;
022: }
023:
024: public Command coalesceWith(UpdateElements updateElementsCommand) {
025: Set coallescedUpdates = new HashSet();
026: Element[] previousUpdates = updateElementsCommand.updates;
027:
028: for (int i = 0; i < previousUpdates.length; i++) {
029: Element previousUpdate = previousUpdates[i];
030: Element selectedUpdate = previousUpdate;
031: for (int j = 0; j < updates.length; j++) {
032: Element update = updates[j];
033: if (update.getAttribute("id").equals(
034: previousUpdate.getAttribute("id"))) {
035: selectedUpdate = update;
036: break;
037: }
038: }
039: coallescedUpdates.add(selectedUpdate);
040: }
041: coallescedUpdates.addAll(Arrays.asList(updates));
042:
043: return new UpdateElements((Element[]) coallescedUpdates
044: .toArray(new Element[coallescedUpdates.size()]));
045: }
046:
047: public Command coalesceWith(Command command) {
048: return command.coalesceWith(this );
049: }
050:
051: public Command coalesceWith(Macro macro) {
052: return new Macro(macro, this );
053: }
054:
055: public Command coalesceWith(Redirect redirect) {
056: return new Macro(redirect, this );
057: }
058:
059: public Command coalesceWith(SessionExpired sessionExpired) {
060: return sessionExpired;
061: }
062:
063: public Command coalesceWith(SetCookie setCookie) {
064: return new Macro(setCookie, this );
065: }
066:
067: public Command coalesceWith(Pong pong) {
068: return new Macro(pong, this );
069: }
070:
071: public Command coalesceWith(NOOP noop) {
072: return this ;
073: }
074:
075: public void serializeTo(Writer writer) throws IOException {
076: writer.write("<updates>");
077: for (int i = 0; i < updates.length; i++) {
078: Element update = updates[i];
079: if (update == null)
080: continue;
081: writer.write("<update address=\"");
082: writer.write(update.getAttribute("id"));
083: writer.write("\"><![CDATA[");
084: String content = DOMUtils.nodeToString(update);
085: content = START_CDATA.matcher(content).replaceAll(
086: "<!#cdata#");
087: content = END_CDATA.matcher(content).replaceAll("##>");
088: writer.write(content);
089: writer.write("]]></update>");
090: }
091: writer.write("</updates>");
092: }
093:
094: public String toString() {
095: try {
096: StringWriter writer = new StringWriter();
097: serializeTo(writer);
098: return writer.toString();
099: } catch (IOException e) {
100: throw new RuntimeException(e);
101: }
102: }
103: }
|