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.lenya.cms.publication;
019:
020: import org.apache.avalon.framework.service.ServiceException;
021: import org.apache.avalon.framework.service.ServiceManager;
022: import org.apache.cocoon.environment.Request;
023: import org.apache.lenya.cms.repository.RepositoryException;
024: import org.apache.lenya.cms.repository.RepositoryUtil;
025: import org.apache.lenya.cms.repository.Session;
026: import org.apache.lenya.util.ServletHelper;
027:
028: /**
029: * Document utility class.
030: */
031: public final class DocumentUtil {
032:
033: private static DocumentFactoryBuilder builder;
034:
035: /**
036: * Creates a document factory.
037: * @param manager The service manager.
038: * @param session The session.
039: * @return a document factory.
040: */
041: public static final DocumentFactory createDocumentFactory(
042: ServiceManager manager, Session session) {
043: DocumentFactoryBuilder builder = getBuilder(manager);
044: return builder.createDocumentFactory(session);
045: }
046:
047: protected static DocumentFactoryBuilder getBuilder(
048: ServiceManager manager) {
049: if (DocumentUtil.builder == null) {
050: try {
051: DocumentUtil.builder = (DocumentFactoryBuilder) manager
052: .lookup(DocumentFactoryBuilder.ROLE);
053: } catch (ServiceException e) {
054: throw new RuntimeException(e);
055: }
056: }
057: return DocumentUtil.builder;
058: }
059:
060: /**
061: * Returns a document factory for the session which is attached to the
062: * request. If no session exists, it is created.
063: * @param manager The service manager.
064: * @param request The request.
065: * @return A document factory.
066: */
067: public static DocumentFactory getDocumentFactory(
068: ServiceManager manager, Request request) {
069: Session session;
070: try {
071: session = RepositoryUtil.getSession(manager, request);
072: } catch (RepositoryException e) {
073: throw new RuntimeException(e);
074: }
075: return createDocumentFactory(manager, session);
076: }
077:
078: /**
079: * Returns the currently requested document or <code>null</code> if no
080: * document is requested.
081: * @param manager The service manager.
082: * @param request The request.
083: * @return A document.
084: * @throws RepositoryException if an error occurs.
085: * @throws DocumentBuildException if an error occurs.
086: */
087: public static Document getCurrentDocument(ServiceManager manager,
088: Request request) throws RepositoryException,
089: DocumentBuildException {
090: Session session = RepositoryUtil.getSession(manager, request);
091: DocumentFactory factory = DocumentUtil.createDocumentFactory(
092: manager, session);
093: String url = ServletHelper.getWebappURI(request);
094: Document doc = null;
095: if (factory.isDocument(url)) {
096: doc = factory.getFromURL(url);
097: }
098: return doc;
099: }
100:
101: }
|