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.wizards.toc;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.UnsupportedEncodingException;
014: import java.lang.reflect.InvocationTargetException;
015:
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IProgressMonitor;
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.Status;
021: import org.eclipse.pde.internal.ui.IPDEUIConstants;
022: import org.eclipse.pde.internal.ui.PDEPlugin;
023: import org.eclipse.ui.actions.WorkspaceModifyOperation;
024:
025: public class TocHTMLOperation extends WorkspaceModifyOperation {
026:
027: private IFile fFile;
028:
029: private static byte[] getHTMLContent() throws CoreException {
030: String indent = " "; //$NON-NLS-1$
031: String delimiter = System.getProperty("line.separator"); //$NON-NLS-1$
032:
033: StringBuffer buf = new StringBuffer();
034: buf.append("<!DOCTYPE HTML PUBLIC"); //$NON-NLS-1$
035: buf.append(" \"-//W3C//DTD HTML 4.01 Transitional//EN\""); //$NON-NLS-1$
036: buf.append(" \"http://www.w3.org/TR/html4/loose.dtd\">"); //$NON-NLS-1$
037: buf.append(delimiter);
038:
039: buf.append("<html>"); //$NON-NLS-1$
040: buf.append(delimiter);
041:
042: buf.append(indent);
043: buf.append("<head>"); //$NON-NLS-1$
044: buf.append(delimiter);
045:
046: buf.append(indent);
047: buf.append(indent);
048: buf.append("<title>Title</title>"); //$NON-NLS-1$
049: buf.append(delimiter);
050:
051: buf.append(indent);
052: buf.append("</head>"); //$NON-NLS-1$
053: buf.append(delimiter);
054:
055: buf.append(delimiter);
056:
057: buf.append(indent);
058: buf.append("<body>"); //$NON-NLS-1$
059: buf.append(delimiter);
060:
061: buf.append(indent);
062: buf.append(indent);
063: buf.append("<h2>"); //$NON-NLS-1$
064: buf.append(delimiter);
065:
066: buf.append(indent);
067: buf.append(indent);
068: buf.append(indent);
069: buf.append("Title"); //$NON-NLS-1$
070: buf.append(delimiter);
071:
072: buf.append(indent);
073: buf.append(indent);
074: buf.append("</h2>"); //$NON-NLS-1$
075: buf.append(delimiter);
076:
077: buf.append(indent);
078: buf.append(indent);
079: buf.append("<p>"); //$NON-NLS-1$
080: buf.append(delimiter);
081:
082: buf.append(indent);
083: buf.append(indent);
084: buf.append(indent);
085: buf.append("Body"); //$NON-NLS-1$
086: buf.append(delimiter);
087:
088: buf.append(indent);
089: buf.append(indent);
090: buf.append("</p>"); //$NON-NLS-1$
091: buf.append(delimiter);
092:
093: buf.append(indent);
094: buf.append("</body>"); //$NON-NLS-1$
095: buf.append(delimiter);
096:
097: buf.append("</html>"); //$NON-NLS-1$
098: buf.append(delimiter);
099:
100: try {
101: return buf.toString().getBytes("UTF8"); //$NON-NLS-1$
102: } catch (UnsupportedEncodingException e) {
103: PDEPlugin.logException(e);
104: IStatus status = new Status(IStatus.ERROR,
105: IPDEUIConstants.PLUGIN_ID, IStatus.ERROR, e
106: .getMessage(), e);
107: throw new CoreException(status);
108: }
109: }
110:
111: public TocHTMLOperation(IFile file) {
112: fFile = file;
113: }
114:
115: protected void execute(IProgressMonitor monitor)
116: throws CoreException, InvocationTargetException,
117: InterruptedException {
118:
119: ByteArrayInputStream stream = new ByteArrayInputStream(
120: getHTMLContent());
121: fFile.setContents(stream, 0, monitor);
122:
123: monitor.done();
124: }
125:
126: }
|