001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.toc;
011:
012: import java.util.HashSet;
013: import java.util.Locale;
014:
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.resources.IWorkspaceRoot;
018: import org.eclipse.core.resources.ResourcesPlugin;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.pde.core.IBaseModel;
021: import org.eclipse.pde.core.IModel;
022: import org.eclipse.pde.internal.core.itoc.ITocConstants;
023: import org.eclipse.pde.internal.ui.util.XMLRootElementMatcher;
024:
025: public class TocExtensionUtil {
026: public static final String[] pageExtensions = {
027: "htm", "shtml", "html", "xhtml" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
028: public static final String tocExtension = "xml"; //$NON-NLS-1$
029: private static HashSet pageExtensionSet = new HashSet(
030: pageExtensions.length);
031:
032: private static void populateHashSet() {
033: for (int i = 0; i < pageExtensions.length; ++i) {
034: pageExtensionSet.add(pageExtensions[i]);
035: }
036: }
037:
038: public static boolean hasValidPageExtension(IPath path) {
039: String fileExtension = path.getFileExtension();
040: if (fileExtension != null) {
041: fileExtension = fileExtension.toLowerCase(Locale.ENGLISH);
042: if (pageExtensionSet.isEmpty()) {
043: populateHashSet();
044: }
045:
046: return pageExtensionSet.contains(fileExtension);
047: }
048:
049: return false;
050: }
051:
052: private static boolean hasValidTocExtension(IPath path) {
053: String fileExtension = path.getFileExtension();
054: return fileExtension != null
055: && fileExtension.equals(tocExtension);
056: }
057:
058: /**
059: * @param file
060: */
061: public static boolean isTOCFile(IPath path) {
062: if (!hasValidTocExtension(path))
063: return false;
064:
065: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
066:
067: IResource resource = root.findMember(path);
068: if (resource != null && resource instanceof IFile) {
069: return XMLRootElementMatcher.fileMatchesElement(
070: (IFile) resource, ITocConstants.ELEMENT_TOC);
071: }
072:
073: return XMLRootElementMatcher.fileMatchesElement(path.toFile(),
074: ITocConstants.ELEMENT_TOC);
075: }
076:
077: public static boolean isCurrentResource(IPath path, IBaseModel model) {
078: if (model instanceof IModel) {
079: IPath workspacePath = ResourcesPlugin.getWorkspace()
080: .getRoot().getLocation();
081: IPath fullPath;
082:
083: if (workspacePath.isPrefixOf(path)) {
084: fullPath = ((IModel) model).getUnderlyingResource()
085: .getLocation();
086: } else {
087: fullPath = ((IModel) model).getUnderlyingResource()
088: .getFullPath();
089: }
090:
091: return fullPath.equals(path);
092: }
093:
094: return false;
095: }
096:
097: public static String getPageExtensionList() {
098: StringBuffer buf = new StringBuffer();
099:
100: for (int i = 0; i < pageExtensions.length; ++i) {
101: buf.append('.');
102: buf.append(pageExtensions[i]);
103: if (i != pageExtensions.length - 1) {
104: buf.append(", "); //$NON-NLS-1$
105: }
106: }
107:
108: return buf.toString();
109: }
110: }
|