001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import org.wings.io.Device;
016: import org.wings.macro.MacroContainer;
017: import org.wings.macro.MacroContext;
018: import org.wings.macro.MacroProcessor;
019: import org.wings.plaf.CGManager;
020: import org.wings.plaf.CmsTableCG;
021: import org.wings.plaf.ComponentCG;
022: import org.wings.session.SessionManager;
023: import org.wings.template.CmsTemplateParseContext;
024: import org.wings.template.PropertyManager;
025: import org.wings.template.parser.ParseContext;
026: import org.wings.template.parser.PositionReader;
027: import org.wings.template.parser.SGMLTag;
028: import org.wings.template.parser.SpecialTagHandler;
029:
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.util.Iterator;
033: import java.util.Map;
034:
035: /**
036: * <code>CmsObjectTagHandler<code>.
037: * <p/>
038: * User: raedler
039: * Date: 08.08.2007
040: * Time: 14:35:57
041: *
042: * @author raedler
043: * @version $Id
044: */
045: public class MacroTagHandler implements SpecialTagHandler {
046: boolean close_is_missing = false;
047:
048: long startPos;
049: long endPos;
050: Map properties;
051: String name;
052:
053: public long getTagStart() {
054: return startPos;
055: }
056:
057: public long getTagLength() {
058: return endPos - startPos;
059: }
060:
061: public void executeTag(ParseContext context, InputStream input)
062: throws Exception {
063: CmsTemplateParseContext tcontext = (CmsTemplateParseContext) context;
064: Device sink = tcontext.getDevice();
065:
066: /*
067: * get the component that is associtated with this name. This has
068: * been set as Layout Manager Constraint.
069: */
070: SComponent c = tcontext.getComponent(name);
071: if (c == null) {
072: sink.print("<!-- Template: '" + name
073: + "' Component not given -->");
074: } else {
075:
076: StringBuilder sb = new StringBuilder();
077: for (long i = getTagLength(); i > 0; i--) {
078: sb.append((char) input.read());
079: }
080: // String content = sb.toString().replaceAll(System.getProperty("line.separator"), "");
081: // content = content.replaceAll("\n", "");
082: // Pattern pattern = Pattern.compile("<object[\\s]*name[\\s]*=[\\s]*\\\".*\\\"[\\s]*>\\n*(.*\\n)*\\n*<[\\s]*/object[\\s]*>");
083: // Matcher matcher = pattern.matcher(sb.toString());
084: //
085: // String macroTemplate = null;
086: // if (matcher.matches()) {
087: // macroTemplate = matcher.group(0);
088: // macroTemplate = macroTemplate.substring(macroTemplate.indexOf('>') + 1, macroTemplate.lastIndexOf('<'));
089: // }
090:
091: String macroTemplate = sb.toString();
092: macroTemplate = macroTemplate.substring(macroTemplate
093: .indexOf('>') + 1, macroTemplate.lastIndexOf('<'));
094:
095: if (macroTemplate != null
096: && !"".equals(macroTemplate.trim())) {
097: MacroProcessor macroProcessor = MacroProcessor
098: .getInstance();
099:
100: MacroContainer macroContainer = macroProcessor
101: .buildMacro(macroTemplate);
102:
103: MacroContext ctx = new MacroContext();
104: ctx.setDevice(sink);
105: ctx.setComponent(c);
106: ctx.put(name, c);
107:
108: macroContainer.setContext(ctx);
109:
110: // if (c instanceof CmsForm) {
111: // SComponent child = ((CmsForm) c).getComponent();
112: //
113: // if (child instanceof STable) {
114: // CmsTableCG cg = new CmsTableCG();
115: // cg.setMacros(macroContainer);
116: // child.setCG(cg);
117: // c.write(sink);
118: // }
119: // }
120:
121: if (c instanceof STable) {
122: CmsTableCG cg = new CmsTableCG();
123: cg.setMacros(macroContainer);
124: c.setCG(cg);
125: c.write(sink);
126: }
127:
128: //String result = macroProcessor.handle(macroTemplate, name, tcontext);
129: //sink.print(result);
130: } else {
131:
132: CGManager cgManager = SessionManager.getSession()
133: .getCGManager();
134: ComponentCG cg = cgManager.getCG(c);
135: c.setCG(cg);
136:
137: // set properties; the STemplateLayout knows how
138: if (properties.size() > 0) {
139: PropertyManager propManager = CmsLayout
140: .getPropertyManager(c.getClass());
141:
142: if (propManager != null) {
143: Iterator iter = properties.keySet().iterator();
144: while (iter.hasNext()) {
145: String key = (String) iter.next();
146: String value = (String) properties.get(key);
147: // System.out.println("set Property " + key + "=" +value + " for " + name);
148: propManager.setProperty(c, key, value);
149: }
150: }
151: }
152: c.write(sink);
153: }
154: // input.skip(getTagLength());
155: }
156: }
157:
158: public SGMLTag parseTag(ParseContext context, PositionReader input,
159: long startPosition, SGMLTag startTag) throws IOException {
160:
161: final String startTagName = startTag.getName();
162: final String endTagName = "/" + startTagName;
163:
164: /*
165: * parse the full tag to get all parameters
166: * (i.e. an optional 'format'-parameter)
167: * and to place the Reader at the position
168: * after the closing '>'
169: */
170: startTag.parse(input);
171:
172: /*
173: * The Offset is the space the reader skipped
174: * before it reached the opening '<'
175: * <!-- a comment --> some garbage <DATE>
176: * ^----- offset --------------------^
177: */
178: startPos = startPosition + startTag.getOffset();
179:
180: /*
181: * get properties
182: */
183: properties = startTag.getAttributes();
184:
185: name = startTag.value("NAME", null);
186: if (name == null)
187: return null;
188:
189: endPos = input.getPosition(); // in case </component> is missing
190:
191: while (!startTag.finished()) {
192: startTag = new SGMLTag(input, true);
193: if (startTag.isNamed(endTagName)
194: || startTag.isNamed(startTagName))
195: break;
196: }
197:
198: // Either EOF or newly opened COMPONENT (unexpectedly)
199: if (startTag.finished() || startTag.isNamed(startTagName)) {
200: close_is_missing = true;
201: } else {
202: // The current Position is after the closing '>'
203: endPos = input.getPosition();
204: }
205:
206: // remove properties, which are not necessary for the PropertyManager
207: properties.remove("NAME");
208: properties.remove("TYPE");
209:
210: return startTag;
211: }
212: }
|