blob: f2c5c66836169e2baee5b3dcac49512f7ff48ce5 [file] [log] [blame]
//------------------------------------------------------------------------------
// Copyright (c) 2005, 2006 IBM Corporation and others.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
// IBM Corporation - initial implementation
//------------------------------------------------------------------------------
package org.eclipse.epf.publishing.services;
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.edit.provider.ItemProviderAdapter;
import org.eclipse.epf.library.configuration.ConfigurationHelper;
import org.eclipse.epf.library.layout.ElementLayoutManager;
import org.eclipse.epf.library.layout.HtmlBuilder;
import org.eclipse.epf.library.layout.LayoutResources;
import org.eclipse.epf.library.services.LibraryProcessor;
import org.eclipse.epf.library.util.IconUtil;
import org.eclipse.epf.library.util.ResourceHelper;
import org.eclipse.epf.publishing.PublishingResources;
import org.eclipse.epf.publishing.layout.Bookmark;
import com.ibm.uma.ContentCategory;
import com.ibm.uma.ContentDescription;
import com.ibm.uma.DescribableElement;
import com.ibm.uma.MethodConfiguration;
import com.ibm.uma.MethodElement;
public abstract class AbstractViewBuilder {
private static final String APPLET_PATH = "applet" + File.separatorChar; //$NON-NLS-1$
private static final String NO_APPLET_PATH = "noapplet" + File.separatorChar; //$NON-NLS-1$
private static final String ICON_PATH = "images" + File.separatorChar; //$NON-NLS-1$
// instead of publishing the whole content library, we collect the elements
// show up in the view tree
// only publish those element show up in the view and it's referenced
// (including content reference) elements
protected List elementsTobePublished = new ArrayList();
protected List publishedElements = new ArrayList();
protected HtmlBuilder builder;
protected MethodConfiguration config;
protected Bookmark defaultView = null;
protected PublishOptions options = null;
protected File iconPath;
public AbstractViewBuilder(HtmlBuilder builder, PublishOptions options) {
this.builder = builder;
this.options = options;
this.config = builder.getLayoutManager().getConfiguration();
// the path for the icons
iconPath = new File(builder.getPublishDir(), APPLET_PATH + ICON_PATH);
}
public HtmlBuilder getHtmlBuilder() {
return builder;
}
public PublishOptions getOptions() {
return options;
}
public ElementLayoutManager getLayoutMgr() {
return builder.getLayoutManager();
}
public PublishingContentValidator getValidator() {
return (PublishingContentValidator) getHtmlBuilder().getValidator();
}
protected boolean canPublish(MethodElement element) {
if (getValidator().isDiscarded(element)) {
return false;
}
if (config == null) {
return true;
}
return ConfigurationHelper.canShow(element, config);
}
protected void discardElement(MethodElement element, boolean discard) {
if (discard) {
getValidator().addDiscardedElement(element);
} else if (element instanceof ContentCategory) {
getValidator().addValidCategory((ContentCategory) element);
}
}
/**
* publish the element, collect the linked elements in the published
* contents
*
* @param monitor
* @param element
* @param recursive
* @param linkedElements
*/
protected void publish(IProgressMonitor monitor, MethodElement element,
boolean recursive, List linkedElements) {
try {
if (!canPublish(element)) {
builder
.getValidator()
.logWarning(
element,
PublishingResources
.getString("Publishing.invalidElementWarning.msg")); //$NON-NLS-1$
} else if (!publishedElements.contains(element)) {
try {
if (monitor != null) {
String str; //$NON-NLS-1$
if (linkedElements != null) {
str = PublishingResources
.formatString(
"Publishing.publishingLinkedElementTask.name", Integer.toString(publishedElements.size()), Integer.toString(linkedElements.size())); //$NON-NLS-1$
} else {
str = PublishingResources
.formatString(
"Publishing.publishingElementTask.name", element.getType().getName(), element.getName()); //$NON-NLS-1$
}
monitor.subTask(str);
}
builder.generateHtml(element, linkedElements);
} catch (Exception ex) {
builder
.getValidator()
.logError(
element,
PublishingResources
.formatString(
"Publishing.publishElementError.msg", ex.getMessage()), ex); //$NON-NLS-1$
}
publishedElements.add(element);
}
if (recursive) {
// iterator all contained and referenced elements
EList children = element.eContents();
if (children != null && children.size() > 0) {
for (Iterator it = children.iterator(); it.hasNext();) {
Object e = it.next();
if (e instanceof ContentDescription) {
continue;
}
if (e instanceof MethodElement) {
publish(monitor, (MethodElement) e, true,
linkedElements);
} else {
builder
.getValidator()
.logWarning(
element,
PublishingResources
.formatString(
"Publishing.invalidMethodElementWarning.msg", e.toString())); //$NON-NLS-1$
}
}
}
}
} catch (RuntimeException e) {
builder
.getValidator()
.logError(
element,
PublishingResources
.formatString(
"Publishing.publishElementError.msg", e.getMessage()), e); //$NON-NLS-1$
}
}
private void copyNodeIcon(File source) {
String name = source.getName();
File dest = new File(iconPath, name);
if (ResourceHelper.copyFile(source, dest) == false) {
builder
.getValidator()
.logWarning(
PublishingResources
.formatString(
"Publishing.copyFileWarning.msg", source.getAbsolutePath(), dest.getAbsolutePath())); //$NON-NLS-1$
}
}
private String getNodeIconName(Object obj) {
File iconFile = null;
String iconName = null;
if (obj instanceof DescribableElement) {
URI uri = ((DescribableElement) obj).getNodeicon();
String elementName = ((DescribableElement) obj).getType().getName()
.toLowerCase();
if (DefaultElementTypeResources.useDefaultIcon(elementName))
uri = null;
if (uri != null) {
// try if this is a valid URL or not
boolean isFullPath = false;
try {
URL url = uri.toURL();
if (url != null) {
iconFile = new File(URLDecoder.decode(url.getFile(),
"UTF-8"));
isFullPath = true;
}
} catch (Exception ex) {
; // not a valid url, maybe a relative path
}
if (!isFullPath) {
iconFile = new File(LibraryProcessor.getInstance()
.getLibraryRootPath(), URLDecoder.decode(uri
.toString()));
}
}
}
if ((iconFile != null) && !iconFile.exists()) {
iconFile = null;
}
if (iconFile == null) {
// get the default icon name
if (obj instanceof MethodElement) {
String type = ((MethodElement) obj).getType().getName()
.toLowerCase();
iconFile = IconUtil.getNodeIconFile(type);
}
}
if (iconFile != null) {
// need to copy the file together and zip for the applet
if (!iconFile.exists()) {
if (obj instanceof MethodElement) {
builder
.getValidator()
.logWarning(
(MethodElement) obj,
PublishingResources
.formatString(
"Publishing.missingIconFileWarning.msg", iconFile.getAbsolutePath())); //$NON-NLS-1$
} else {
builder
.getValidator()
.logWarning(
PublishingResources
.formatString(
"Publishing.AbstractViewBuilder.MSG15", iconFile.getAbsolutePath())); //$NON-NLS-1$
}
}
copyNodeIcon(iconFile);
iconName = iconFile.getName();
}
if (iconName == null || iconName.length() == 0) {
String name;
if (obj instanceof MethodElement) {
name = ((MethodElement) obj).getName();
} else {
name = obj.toString();
}
builder.getValidator().logWarning(
PublishingResources.formatString(
"Publishing.missingIconNameWarning.msg", name)); //$NON-NLS-1$
}
return iconName;
}
private String getOpenIconName(Object obj) {
return ""; //$NON-NLS-1$
}
/**
* Get name
*
* @param obj
* @return
*/
private String getName(Object obj) {
String name = null;
if (obj instanceof MethodElement) {
MethodElement e = (MethodElement) obj;
// calculate the presentation name, for extenders, get from base if
// needed
name = ConfigurationHelper.getPresentationName(e, config);
if (name == null || name.equals("")) //$NON-NLS-1$
{
name = e.getClass().getName();
int index = name.lastIndexOf("."); //$NON-NLS-1$
if (index >= 0) {
name = name.substring(index + 1);
if (name.endsWith("Impl")) //$NON-NLS-1$
{
name = name.substring(0, name.length() - 4);
}
}
}
} else if (obj instanceof ItemProviderAdapter) {
ItemProviderAdapter provider = (ItemProviderAdapter) obj;
name = provider.getText(obj);
}
return name;
}
/**
* get guid
*
* @param obj
* @return
*/
private String getGUID(Object obj) {
if (obj instanceof MethodElement) {
return ((MethodElement) obj).getGuid();
} else
return null;
}
/**
* get url
*
* @param obj
* @return
*/
private String getURL(Object obj) {
if (obj instanceof MethodElement) {
return getLayoutMgr().getLayout((MethodElement) obj, true).getUrl();
} else {
// TODO - layout needs to be provided for uncategorized and category
// object
return "applet//empty.htm"; //$NON-NLS-1$
}
}
/**
* Create bookmark
*
* @param element
* @return Bookmark
*/
protected Bookmark createBookmark(IProgressMonitor monitor, Object element) {
// publish this element,
if (element instanceof MethodElement) {
// delay the publishing till the bookmarks are created.
if (!elementsTobePublished.contains(element)) {
elementsTobePublished.add(element);
}
// this.publish(monitor, (MethodElement)element, false,
// elementsTobePublished);
}
// create a bookmark for this element
String name = getName(element);
String guid = getGUID(element);
String url = getURL(element);
String nodeIcon = getNodeIconName(element);
String msg = PublishingResources.formatString(
"Publishing.generatingBookmarkTask.name", name); //$NON-NLS-1$
monitor.subTask(msg);
return createBookmark(name, guid, url, nodeIcon, nodeIcon);
}
protected Bookmark createBookmark(String name, String guid, String url,
String closeIcon, String openIcon) {
Bookmark b = new Bookmark(name);
b.setPresentationName(name);
b.setUniqueId(guid);
b.setClosedIconName(closeIcon);
b.setOpenIconName(openIcon);
b.setFileName(url);
b.setFromContentLibrary(true);
b.setEnabled(true);
b.setExist(true);
b.setVisible(true);
b.setTransparency(false);
b.setDefault(true);
b.setCurrent(false);
return b;
}
public Bookmark getDefaultView() {
return defaultView;
}
protected void copyIconsForNonApplet() {
try {
if (!options.useApplet) {
// also copy the icons to the no-applet folder
LayoutResources.copyDir(iconPath.getAbsolutePath(), builder
.getPublishDir()
+ NO_APPLET_PATH + ICON_PATH);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* build the views and returns a list of Boolmark objects
*
* @param pubMgr
* @return List a list of Bookmarks
*/
public abstract List buildViews(IProgressMonitor monitor);
public void dispose() {
elementsTobePublished.clear();
publishedElements.clear();
if (builder != null) {
builder.dispose();
}
config = null;
builder = null;
}
}