Source Code Cross Referenced for DocumentHelper.java in  » Content-Management-System » apache-lenya-2.0 » org » apache » lenya » cms » publication » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » apache lenya 2.0 » org.apache.lenya.cms.publication.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:
019:        /* $Id: DocumentHelper.java 473861 2006-11-12 03:51:14Z gregor $  */
020:
021:        package org.apache.lenya.cms.publication.util;
022:
023:        import java.util.Arrays;
024:        import java.util.List;
025:        import java.util.Map;
026:
027:        import org.apache.avalon.framework.service.ServiceManager;
028:        import org.apache.cocoon.ProcessingException;
029:        import org.apache.cocoon.environment.ObjectModelHelper;
030:        import org.apache.cocoon.environment.Request;
031:        import org.apache.lenya.cms.publication.Document;
032:        import org.apache.lenya.cms.publication.DocumentBuildException;
033:        import org.apache.lenya.cms.publication.DocumentException;
034:        import org.apache.lenya.cms.publication.DocumentFactory;
035:        import org.apache.lenya.cms.publication.DocumentLocator;
036:        import org.apache.lenya.cms.publication.DocumentUtil;
037:        import org.apache.lenya.cms.publication.Publication;
038:        import org.apache.lenya.cms.publication.PublicationException;
039:        import org.apache.lenya.cms.publication.PublicationUtil;
040:        import org.apache.lenya.cms.publication.URLInformation;
041:        import org.apache.lenya.cms.repository.RepositoryException;
042:        import org.apache.lenya.cms.repository.RepositoryUtil;
043:        import org.apache.lenya.cms.repository.Session;
044:        import org.apache.lenya.util.ServletHelper;
045:
046:        /**
047:         * Helper class to handle documents from XSP.
048:         */
049:        public class DocumentHelper {
050:
051:            private Map objectModel;
052:            private DocumentFactory identityMap;
053:            private Publication publication;
054:
055:            /**
056:             * Ctor.
057:             * @param manager The service manager.
058:             * @param _objectModel The Cocoon object model.
059:             */
060:            public DocumentHelper(ServiceManager manager, Map _objectModel) {
061:                this .objectModel = _objectModel;
062:                try {
063:                    this .publication = PublicationUtil.getPublication(manager,
064:                            _objectModel);
065:                } catch (PublicationException e) {
066:                    throw new RuntimeException(e);
067:                }
068:                Request request = ObjectModelHelper.getRequest(_objectModel);
069:                Session session;
070:                try {
071:                    session = RepositoryUtil.getSession(manager, request);
072:                } catch (RepositoryException e) {
073:                    throw new RuntimeException(e);
074:                }
075:                this .identityMap = DocumentUtil.createDocumentFactory(manager,
076:                        session);
077:            }
078:
079:            /**
080:             * Creates a document URL. <br/>If the document ID is null, the current document ID is used.
081:             * <br/>If the document area is null, the current area is used. <br/>If the language is null,
082:             * the current language is used.
083:             * @param uuid The target document UUID.
084:             * @param documentArea The target area.
085:             * @param language The target language.
086:             * @return A string.
087:             * @throws ProcessingException if something went wrong.
088:             */
089:            public String getDocumentUrl(String uuid, String documentArea,
090:                    String language) throws ProcessingException {
091:
092:                String url = null;
093:
094:                try {
095:                    Request request = ObjectModelHelper
096:                            .getRequest(this .objectModel);
097:                    String webappUrl = ServletHelper.getWebappURI(request);
098:                    Document envDocument = this .identityMap
099:                            .getFromURL(webappUrl);
100:                    if (uuid == null) {
101:                        uuid = envDocument.getUUID();
102:                    }
103:
104:                    if (documentArea == null) {
105:                        URLInformation info = new URLInformation(webappUrl);
106:                        String completeArea = info.getCompleteArea();
107:                        documentArea = completeArea;
108:                    }
109:
110:                    if (language == null) {
111:                        language = envDocument.getLanguage();
112:                    }
113:
114:                    Document document = this .identityMap.get(this .publication,
115:                            documentArea, uuid, language);
116:                    url = document.getCanonicalWebappURL();
117:
118:                    String contextPath = request.getContextPath();
119:                    if (contextPath == null) {
120:                        contextPath = "";
121:                    }
122:
123:                    url = contextPath + url;
124:                } catch (final DocumentBuildException e) {
125:                    throw new ProcessingException(e);
126:                }
127:
128:                return url;
129:
130:            }
131:
132:            /**
133:             * Returns the complete URL of the parent document. If the document is a top-level document, the
134:             * /index document is chosen. If the parent does not exist in the appropriate language, the
135:             * default language is chosen.
136:             * @return A string.
137:             * @throws ProcessingException when something went wrong.
138:             */
139:            public String getCompleteParentUrl() throws ProcessingException {
140:
141:                String parentUrl;
142:                String contextPath;
143:                try {
144:                    Request request = ObjectModelHelper
145:                            .getRequest(this .objectModel);
146:                    String webappUrl = ServletHelper.getWebappURI(request);
147:                    Document document = this .identityMap.getFromURL(webappUrl);
148:
149:                    contextPath = request.getContextPath();
150:
151:                    DocumentLocator parentLocator = document.getLocator()
152:                            .getParent("/index");
153:                    Document parent = this .identityMap.get(parentLocator);
154:                    parentUrl = parent.getCanonicalWebappURL();
155:                } catch (final DocumentBuildException e) {
156:                    throw new ProcessingException(e);
157:                }
158:                if (contextPath == null) {
159:                    contextPath = "";
160:                }
161:
162:                return contextPath + parentUrl;
163:            }
164:
165:            /**
166:             * Returns an existing language version of a document. If the document exists in the default
167:             * language, the default language version is returned. Otherwise, a random language version is
168:             * returned. If no language version exists, a DocumentException is thrown.
169:             * 
170:             * @param document The document.
171:             * @return A document.
172:             * @throws DocumentException when an error occurs.
173:             */
174:            public static Document getExistingLanguageVersion(Document document)
175:                    throws DocumentException {
176:                return getExistingLanguageVersion(document, document
177:                        .getPublication().getDefaultLanguage());
178:            }
179:
180:            /**
181:             * Returns an existing language version of a document. If the document exists in the preferred
182:             * language, this version is returned. Otherwise, if the document exists in the default
183:             * language, the default language version is returned. Otherwise, a random language version is
184:             * returned. If no language version exists, a DocumentException is thrown.
185:             * 
186:             * @param document The document.
187:             * @param preferredLanguage The preferred language.
188:             * @return A document.
189:             * @throws DocumentException when an error occurs.
190:             */
191:            public static Document getExistingLanguageVersion(
192:                    final Document document, String preferredLanguage)
193:                    throws DocumentException {
194:
195:                Publication publication = document.getPublication();
196:
197:                String[] languages = document.getLanguages();
198:
199:                if (languages.length == 0) {
200:                    throw new DocumentException("The document [" + document
201:                            + "] does not exist in any language!");
202:                }
203:
204:                List languageList = Arrays.asList(languages);
205:
206:                String existingLanguage = null;
207:
208:                if (languageList.contains(preferredLanguage)) {
209:                    existingLanguage = preferredLanguage;
210:                } else if (languageList.contains(publication
211:                        .getDefaultLanguage())) {
212:                    existingLanguage = publication.getDefaultLanguage();
213:                } else {
214:                    existingLanguage = languages[0];
215:                }
216:
217:                Document existingVersion = null;
218:                try {
219:                    existingVersion = document.getTranslation(existingLanguage);
220:                } catch (DocumentException e) {
221:                    throw new DocumentException(e);
222:                }
223:
224:                return existingVersion;
225:            }
226:
227:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.