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: */package org.apache.lenya.cms.export;
018:
019: import org.apache.avalon.framework.logger.AbstractLogEnabled;
020: import org.apache.avalon.framework.logger.Logger;
021: import org.apache.avalon.framework.service.ServiceManager;
022: import org.apache.avalon.framework.service.ServiceSelector;
023: import org.apache.excalibur.source.SourceResolver;
024: import org.apache.lenya.cms.cocoon.source.SourceUtil;
025: import org.apache.lenya.cms.linking.LinkConverter;
026: import org.apache.lenya.cms.metadata.MetaData;
027: import org.apache.lenya.cms.publication.Area;
028: import org.apache.lenya.cms.publication.Document;
029: import org.apache.lenya.cms.publication.DocumentManager;
030: import org.apache.lenya.cms.publication.Publication;
031: import org.apache.lenya.cms.publication.ResourceType;
032: import org.apache.lenya.cms.site.SiteNode;
033: import org.apache.lenya.cms.site.SiteStructure;
034: import org.apache.lenya.cms.site.tree.DefaultSiteTree;
035: import org.apache.lenya.xml.DocumentHelper;
036: import org.apache.lenya.xml.NamespaceHelper;
037: import org.w3c.dom.Element;
038:
039: /**
040: * Import content.
041: */
042: public class Importer extends AbstractLogEnabled {
043:
044: private ServiceManager manager;
045:
046: /**
047: * Ctor.
048: * @param manager The service manager.
049: * @param logger The logger.
050: */
051: public Importer(ServiceManager manager, Logger logger) {
052: this .manager = manager;
053: enableLogging(logger);
054: }
055:
056: /**
057: * Imports content into an area.
058: * @param area The area.
059: * @param path The path containing the content.
060: * @throws Exception if an error occurs.
061: */
062: public void importContent(Area area, String path) throws Exception {
063: importContent(area.getPublication(), area, path);
064: }
065:
066: /**
067: * Imports content from a different publication into an area.
068: * @param srcPub The source publication.
069: * @param area The area.
070: * @param path The path containing the content.
071: * @throws Exception if an error occurs.
072: */
073: public void importContent(Publication srcPub, Area area, String path)
074: throws Exception {
075: getLogger()
076: .info("Importing documents into area [" + area + "]");
077:
078: String baseUri = "file://" + path;
079: String sitetreeUri = baseUri + "/sitetree.xml";
080:
081: org.w3c.dom.Document xml = SourceUtil.readDOM(sitetreeUri,
082: this .manager);
083: NamespaceHelper helper = new NamespaceHelper(
084: DefaultSiteTree.NAMESPACE_URI, "", xml);
085:
086: Element siteElement = xml.getDocumentElement();
087: importChildren(area, helper, siteElement, baseUri, "");
088:
089: convertLinks(srcPub, area);
090: }
091:
092: protected void importElement(Area area, NamespaceHelper helper,
093: Element element, String baseUri, String parentPath) {
094: String path = parentPath + "/" + element.getAttribute("id");
095:
096: boolean visible = true;
097: String visibleString = element.getAttribute("visibleinnav");
098: if (visibleString != null && !visibleString.equals("")) {
099: visible = Boolean.valueOf(visibleString).booleanValue();
100: }
101:
102: Element[] labelElements = helper.getChildren(element, "label");
103: for (int i = 0; i < labelElements.length; i++) {
104: importDocument(area, labelElements[i], baseUri, path,
105: visible);
106: }
107: importChildren(area, helper, element, baseUri, path);
108: }
109:
110: protected void importDocument(Area area, Element element,
111: String baseUri, String path, boolean visibleInNav) {
112: String language = element.getAttribute("xml:lang");
113: String navigationTitle = DocumentHelper
114: .getSimpleElementText(element);
115:
116: String contentUri = baseUri + path + "/index_" + language;
117: String metaUri = contentUri + ".meta";
118:
119: DocumentManager docManager = null;
120: ServiceSelector selector = null;
121: ResourceType resourceType = null;
122: SourceResolver resolver = null;
123: try {
124:
125: org.w3c.dom.Document xml = SourceUtil.readDOM(metaUri,
126: this .manager);
127: NamespaceHelper helper = new NamespaceHelper(
128: "http://apache.org/cocoon/lenya/page-envelope/1.0",
129: "", xml);
130: Element metaElement = helper.getFirstChild(xml
131: .getDocumentElement(), "meta");
132: Element internalElement = helper.getFirstChild(metaElement,
133: "internal");
134: Element resourceTypeElement = helper.getFirstChild(
135: internalElement, "resourceType");
136: String resourceTypeName = DocumentHelper
137: .getSimpleElementText(resourceTypeElement);
138:
139: Element mimeTypeElement = helper.getFirstChild(
140: internalElement, "mimeType");
141: String mimeType = DocumentHelper
142: .getSimpleElementText(mimeTypeElement);
143:
144: selector = (ServiceSelector) this .manager
145: .lookup(ResourceType.ROLE + "Selector");
146: resourceType = (ResourceType) selector
147: .select(resourceTypeName);
148:
149: docManager = (DocumentManager) this .manager
150: .lookup(DocumentManager.ROLE);
151: Document newDoc;
152: SiteStructure site = area.getSite();
153: if (!site.contains(path)
154: || site.getNode(path).getLanguages().length == 0) {
155: newDoc = docManager.add(area.getPublication()
156: .getFactory(), resourceType, contentUri, area
157: .getPublication(), area.getName(), path,
158: language, "xml", navigationTitle, visibleInNav);
159: newDoc.setMimeType(mimeType);
160: } else {
161: SiteNode node = site.getNode(path);
162: Document doc = node.getLink(node.getLanguages()[0])
163: .getDocument();
164: newDoc = docManager.addVersion(doc, area.getName(),
165: language, true);
166: resolver = (SourceResolver) this .manager
167: .lookup(SourceResolver.ROLE);
168: SourceUtil.copy(resolver, contentUri, newDoc
169: .getOutputStream());
170: newDoc.getLink().setLabel(navigationTitle);
171: }
172:
173: String dcNamespace = "http://purl.org/dc/elements/1.1/";
174:
175: Element dcElement = helper.getFirstChild(metaElement, "dc");
176: NamespaceHelper dcHelper = new NamespaceHelper(dcNamespace,
177: "dc", xml);
178: Element[] dcElements = dcHelper.getChildren(dcElement);
179:
180: MetaData meta = newDoc.getMetaData(dcNamespace);
181: for (int i = 0; i < dcElements.length; i++) {
182: String key = dcElements[i].getLocalName();
183: String value = DocumentHelper
184: .getSimpleElementText(dcElements[i]);
185: meta.setValue(key, value);
186: }
187:
188: } catch (Exception e) {
189: throw new RuntimeException(e);
190: } finally {
191: if (docManager != null) {
192: this .manager.release(docManager);
193: }
194: if (selector != null) {
195: this .manager.release(selector);
196: }
197: if (resolver != null) {
198: this .manager.release(resolver);
199: }
200: }
201: }
202:
203: protected void importChildren(Area area, NamespaceHelper helper,
204: Element element, String baseUri, String path) {
205: Element[] elements = helper.getChildren(element, "node");
206: for (int i = 0; i < elements.length; i++) {
207: importElement(area, helper, elements[i], baseUri, path);
208: }
209: }
210:
211: protected void convertLinks(Publication srcPub, Area area) {
212: Document[] docs = area.getDocuments();
213: for (int i = 0; i < docs.length; i++) {
214: LinkConverter converter = new LinkConverter(this .manager,
215: getLogger());
216: converter.convertUrlsToUuids(srcPub, docs[i], false);
217: }
218: }
219:
220: }
|