001: package org.wings.adapter;
002:
003: import org.wings.SFrame;
004: import org.wings.STemplateLayout;
005: import org.wings.session.SessionManager;
006: import org.wings.header.Link;
007: import org.wings.header.Script;
008: import org.wings.conf.CmsDetail;
009: import au.id.jericho.lib.html.*;
010:
011: import javax.servlet.http.HttpServletRequest;
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.List;
015:
016: /**
017: * <code>JoomlaAdapter<code>.
018: * <p/>
019: * User: rrd
020: * Date: 08.08.2007
021: * Time: 08:40:54
022: *
023: * @author rrd
024: * @version $Id
025: */
026: public class Joomla10Adapter extends AbstractCmsAdapter {
027:
028: public Joomla10Adapter(SFrame frame, STemplateLayout layout,
029: CmsDetail cfg) {
030: super (frame, layout, cfg);
031: }
032:
033: /**
034: * {@inheritDoc}
035: */
036: public void parseTitle(Source source) {
037: Element titleElement = source.findNextElement(0, "title");
038:
039: if (titleElement != null) {
040: String title = titleElement.getTextExtractor().toString();
041: getFrame().setTitle("JoomlaAdapter :: " + title);
042: }
043: }
044:
045: public void parseAnchors(Source source, OutputDocument output) {
046:
047: String wingsServerPath = getPath();
048: String cmsServerPath = getConfiguration().getServerPath();
049:
050: List<StartTag> anchorTags = source.findAllStartTags(Tag.A);
051: for (StartTag anchorTag : anchorTags) {
052: Attributes attributes = anchorTag.getAttributes();
053: Attribute attribute = attributes.get("href");
054: if (attribute != null) {
055: String value = attribute.getValue();
056:
057: String replacedValue = value.replaceFirst(
058: cmsServerPath, wingsServerPath);
059: if (!replacedValue.equals(value)) {
060: output.replace(attribute.getValueSegment(),
061: replacedValue);
062: }
063: }
064: }
065:
066: List<StartTag> formTags = source.findAllStartTags(Tag.FORM);
067: for (StartTag formTag : formTags) {
068: Attributes attributes = formTag.getAttributes();
069: Attribute attribute = attributes.get("action");
070: if (attribute != null) {
071: String value = attribute.getValue();
072:
073: String replacedValue = value.replaceFirst(
074: cmsServerPath, wingsServerPath);
075: if (!replacedValue.equals(value)) {
076: output.replace(attribute.getValueSegment(),
077: replacedValue);
078: }
079: }
080: }
081: }
082:
083: public void parseImages(Source source, OutputDocument output) {
084:
085: String cmsServerPath = getConfiguration().getServerPath();
086:
087: List<StartTag> imgTags = source.findAllStartTags(Tag.IMG);
088: for (StartTag imgTag : imgTags) {
089: Attributes attributes = imgTag.getAttributes();
090: Attribute attribute = attributes.get("src");
091: if (attribute != null) {
092: String value = attribute.getValue();
093:
094: if (!value.startsWith(getConfiguration().getProtocol())) {
095: value = cmsServerPath + "/" + value;
096: }
097: output.replace(attribute.getValueSegment(), value);
098: }
099: }
100: }
101:
102: /**
103: * {@inheritDoc}
104: */
105: public void parseLinks(Source source) {
106:
107: HttpServletRequest request = SessionManager.getSession()
108: .getServletRequest();
109: CmsDetail cfg = getConfiguration();
110:
111: Collection<Link> newLinks = new ArrayList<Link>();
112: Collection<StartTag> linkTags = source.findAllStartTags("link");
113: for (StartTag linkTag : linkTags) {
114: Attributes attributes = linkTag.getAttributes();
115: String rel = attributes.getValue("rel");
116: String rev = attributes.getValue("rev");
117: String type = attributes.getValue("type");
118: String target = attributes.getValue("target");
119: String href = attributes.getValue("href");
120: if (!href.startsWith("http")) {
121: String pathInfo = request.getPathInfo();
122: if (pathInfo.contains(".php"))
123: pathInfo = pathInfo.substring(0, pathInfo
124: .lastIndexOf("/"));
125: href = cfg.getServerPath() + pathInfo + "/" + href;
126: }
127:
128: newLinks
129: .add(new Link(rel, rev, type, target, new Url(href)));
130: }
131: if (!newLinks.equals(links)) {
132: // System.out.println("links = " + links);
133: // System.out.println("newLinks = " + newLinks);
134:
135: for (Link link : links)
136: getFrame().removeHeader(link);
137: for (Link link : newLinks)
138: getFrame().addHeader(link);
139:
140: links.clear();
141: links.addAll(newLinks);
142: }
143: }
144:
145: /**
146: * {@inheritDoc}
147: */
148: public void parseScripts(Source source) {
149: Collection<Script> newScripts = new ArrayList<Script>();
150: Collection<StartTag> scriptTags = source
151: .findAllStartTags("script");
152: for (StartTag scriptTag : scriptTags) {
153: Attributes attributes = scriptTag.getAttributes();
154: String type = attributes.getValue("type");
155: String src = attributes.getValue("src");
156: if (src != null)
157: newScripts.add(new Script(type, new Url(src)));
158: }
159: if (!newScripts.equals(scripts)) {
160: // System.out.println("scripts = " + scripts);
161: // System.out.println("newScripts = " + newScripts);
162:
163: for (Script script : scripts)
164: getFrame().removeHeader(script);
165: for (Script script : newScripts)
166: getFrame().addHeader(script);
167:
168: scripts.clear();
169: scripts.addAll(newScripts);
170: }
171: }
172: }
|