/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/
package org.olat.search.service.document;
import java.util.Date;
import java.util.Locale;
import org.apache.lucene.document.Document;
import org.jamwiki.DataHandler;
import org.jamwiki.model.Topic;
import org.jamwiki.model.WikiFile;
import org.jamwiki.parser.AbstractParser;
import org.jamwiki.parser.ParserDocument;
import org.jamwiki.parser.ParserInput;
import org.jamwiki.parser.jflex.JFlexParser;
import org.olat.basesecurity.Manager;
import org.olat.basesecurity.ManagerFactory;
import org.olat.core.commons.services.search.OlatDocument;
import org.olat.core.id.Identity;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.core.util.filter.FilterFactory;
import org.olat.modules.wiki.WikiPage;
import org.olat.search.service.SearchResourceContext;
/**
* Lucene document mapper.
* @author Christian Guretzki
*/
public class WikiPageDocument extends OlatDocument {
private static final OLog log = Tracing.createLoggerFor(WikiPageDocument.class);
private static final DummyDataHandler DUMMY_DATA_HANDLER = new DummyDataHandler();
private static Manager identityManager;
public WikiPageDocument() {
super();
identityManager = ManagerFactory.getManager();
}
public static Document createDocument(SearchResourceContext searchResourceContext, WikiPage wikiPage) {
WikiPageDocument wikiPageDocument = new WikiPageDocument();
long userId = wikiPage.getInitalAuthor();
if (userId != 0) {
Identity identity = identityManager.loadIdentityByKey(Long.valueOf(userId));
wikiPageDocument.setAuthor(identity.getName());
}
wikiPageDocument.setTitle(wikiPage.getPageName());
wikiPageDocument.setContent(getContent(wikiPage));
wikiPageDocument.setCreatedDate(new Date(wikiPage.getCreationTime()));
wikiPageDocument.setLastChange(new Date(wikiPage.getModificationTime()));
wikiPageDocument.setResourceUrl(searchResourceContext.getResourceUrl());
wikiPageDocument.setDocumentType(searchResourceContext.getDocumentType());
wikiPageDocument.setCssIcon("o_wiki_icon");
wikiPageDocument.setParentContextType(searchResourceContext.getParentContextType());
wikiPageDocument.setParentContextName(searchResourceContext.getParentContextName());
if (log.isDebug()) log.debug(wikiPageDocument.toString());
return wikiPageDocument.getLuceneDocument();
}
private static String getContent(WikiPage wikiPage) {
try {
ParserInput input = new ParserInput();
input.setWikiUser(null);
input.setAllowSectionEdit(false);
input.setDepth(2);
input.setContext("");
input.setLocale(Locale.ENGLISH);
input.setTopicName("dummy");
input.setUserIpAddress("0.0.0.0");
input.setDataHandler(DUMMY_DATA_HANDLER);
input.setVirtualWiki("/olat");
AbstractParser parser = new JFlexParser(input);
ParserDocument parsedDoc = parser.parseHTML(wikiPage.getContent());
String parsedContent = parsedDoc.getContent();
String filteredContent = FilterFactory.getHtmlTagAndDescapingFilter().filter(parsedContent);
return filteredContent;
} catch(Exception e) {
e.printStackTrace();
log.error("", e);
return wikiPage.getContent();
}
}
private static class DummyDataHandler implements DataHandler {
@Override
public boolean exists(String virtualWiki, String topic) {
return true;
}
@Override
public Topic lookupTopic(String virtualWiki, String topicName, boolean deleteOK, Object transactionObject) throws Exception {
return null;
}
@Override
public WikiFile lookupWikiFile(String virtualWiki, String topicName) throws Exception {
return null;
}
}
}