001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.cocoon.transformation;
019:
020: import java.io.IOException;
021: import java.util.Map;
022:
023: import org.apache.avalon.framework.parameters.Parameters;
024: import org.apache.cocoon.ProcessingException;
025: import org.apache.cocoon.environment.ObjectModelHelper;
026: import org.apache.cocoon.environment.Request;
027: import org.apache.cocoon.environment.SourceResolver;
028: import org.apache.lenya.cms.publication.Area;
029: import org.apache.lenya.cms.publication.DocumentFactory;
030: import org.apache.lenya.cms.publication.DocumentUtil;
031: import org.apache.lenya.cms.publication.Publication;
032: import org.apache.lenya.cms.publication.URLInformation;
033: import org.apache.lenya.cms.site.SiteException;
034: import org.apache.lenya.cms.site.SiteStructure;
035: import org.apache.lenya.util.ServletHelper;
036: import org.xml.sax.Attributes;
037: import org.xml.sax.SAXException;
038:
039: /**
040: * <p>
041: * Transform
042: * <code><search:field name="uid">a5d30250-2b7c-11db-98f0-bef7b2781cf0:en</search:field></code>
043: * into the corresponding URL.
044: * </p>
045: * <p>
046: * Parameters:
047: * </p>
048: * <ul>
049: * <li>pubId (optional, defaults to current page)</li>
050: * <li>area (optional, defaults to current page)</li>
051: * </ul>
052: */
053: public class UuidToUrlTransformer extends AbstractSAXTransformer {
054:
055: private SiteStructure site;
056:
057: protected static final String NAMESPACE = "http://apache.org/cocoon/search/1.0";
058: protected static final String ELEMENT_FIELD = "field";
059: protected static final String ATTR_NAME = "name";
060: protected static final String ATTR_VALUE_UID = "uid";
061:
062: public void setup(SourceResolver resolver, Map objectModel,
063: String src, Parameters params) throws ProcessingException,
064: SAXException, IOException {
065: super .setup(resolver, objectModel, src, params);
066:
067: Request request = ObjectModelHelper.getRequest(objectModel);
068: String url = ServletHelper.getWebappURI(request);
069: URLInformation info = new URLInformation(url);
070:
071: String pubId = params.getParameter("pubId", info
072: .getPublicationId());
073: String areaId = params.getParameter("area", info.getArea());
074:
075: DocumentFactory factory = DocumentUtil.getDocumentFactory(
076: this .manager, request);
077: try {
078: Publication pub = factory.getPublication(pubId);
079: Area area = pub.getArea(areaId);
080: this .site = area.getSite();
081: } catch (Exception e) {
082: throw new ProcessingException(e);
083: }
084: }
085:
086: private boolean insideUidElement = false;
087:
088: public void startElement(String uri, String name, String raw,
089: Attributes attr) throws SAXException {
090: super .startElement(uri, name, raw, attr);
091: if (uri.equals(NAMESPACE) && name.equals(ELEMENT_FIELD)
092: && attr.getValue(ATTR_NAME).equals(ATTR_VALUE_UID)) {
093: this .insideUidElement = true;
094: }
095: }
096:
097: public void characters(char[] ch, int start, int length)
098: throws SAXException {
099: if (this .insideUidElement) {
100: String key = new String(ch);
101: String[] steps = key.split(":");
102: String uuid = steps[0];
103: String language = steps[1];
104:
105: if (site.containsByUuid(uuid, language)) {
106: try {
107: String url = site.getByUuid(uuid, language)
108: .getDocument().getCanonicalDocumentURL();
109: char[] chars = url.toCharArray();
110: super .characters(chars, 0, chars.length);
111: } catch (SiteException e) {
112: throw new SAXException(e);
113: }
114: }
115:
116: } else {
117: super .characters(ch, start, length);
118: }
119: }
120:
121: public void endElement(String uri, String name, String raw)
122: throws SAXException {
123: this .insideUidElement = false;
124: super.endElement(uri, name, raw);
125: }
126:
127: }
|