⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultdocumentprovider.java

📁 一个eclipse插件源代码。用于web开发
💻 JAVA
字号:
/*
 * $Header: /home/cvs/WEBPUMP2.0/WebPumpIDE_Src/WebPumpIDE/src/com/webpump/ui/properties/DefaultDocumentProvider.java,v 1.1.1.1 2004/07/01 09:07:52 wang_j Exp $
 * $Revision: 1.1.1.1 $
 * $Date: 2004/07/01 09:07:52 $
 *
 * ====================================================================
 *
 * The NanJing HopeRun(IT-FOREST) Software License, Version 2.0.0
 *
 * Copyright 2003-2004 by NanJing HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and
 *                        IT Forest Corporation
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
 * You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with
 * HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
 */
package com.webpump.ui.properties;

import java.io.*;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.dialogs.ContainerGenerator;
import org.eclipse.ui.editors.text.FileDocumentProvider;
import org.eclipse.ui.texteditor.*;
import com.webpump.ui.perspective.WebpumpIDEPlugin;


/**
 * Class for document provider.
 * 
 * @author luo_sa
 * @version 2.0 2004-02-26
 */
public class DefaultDocumentProvider extends FileDocumentProvider {
    
    /** char*/
    private static final char BOM = 65279;
    
    /**
     * Constructor
     *
     */
    public DefaultDocumentProvider() {
        
    }
    
    /**
     * Set document content.
     */
    protected void setDocumentContent(IDocument document, InputStream contentStream, String encoding)
        throws CoreException {
            
        Reader in = null;
        try {
            if (encoding == null)
                encoding = getDefaultEncoding();
            in = new InputStreamReader(contentStream, encoding);
            StringBuffer buffer = new StringBuffer();
            char readBuffer[] = new char[2048];
            for (int n = in.read(readBuffer); n > 0; n = in.read(readBuffer))
                buffer.append(readBuffer, 0, n);

            if (buffer.length() > 0 && buffer.charAt(0) == '\uFEFF')
                buffer.deleteCharAt(0);
            document.set(buffer.toString());
        }
        catch(IOException x) {
            WebpumpIDEPlugin.log(x);
        }
        finally {
            if (in != null)
                try {
                    in.close();
                }
                catch(IOException _ex) { 
                    WebpumpIDEPlugin.log(_ex);
                }
        }
    }
    
    
    /**
     * Save document provided for the element.
     */
    protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite)
        throws CoreException {
            
        if (!(element instanceof IFileEditorInput)) {
            super.doSaveDocument(monitor, element, document, overwrite);
            return;
        }
        IFileEditorInput input = (IFileEditorInput)element;
        try {
            String content = document.get();
            String encoding = getDeclaredEncoding(new ByteArrayInputStream(content.getBytes("ISO-8859-1")));
            if (encoding == null) {
                encoding = getEncoding(element);
                if (encoding == null || !encoding.startsWith("UTF-16"))
                    encoding = getDefaultEncoding();
            }
            if (encoding.startsWith("UTF-16"))
                content = '\uFEFF' + content;
            InputStream stream = null;
            try {
                stream = new ByteArrayInputStream(content.getBytes(encoding));
            }
            catch (UnsupportedEncodingException e) {
                WebpumpIDEPlugin.log(e);
            }
            IFile file = input.getFile();
            
            if (file.exists()) {
                org.eclipse.ui.editors.text.FileDocumentProvider.FileInfo info = (org.eclipse.ui.editors.text.FileDocumentProvider.FileInfo)getElementInfo(element);
                if(info != null && !overwrite)
                    checkSynchronizationState(info.fModificationStamp, file);
                fireElementStateChanging(element);
                
                try {
                    file.setContents(stream, overwrite, true, monitor);
                }
                catch (CoreException x) {
                    fireElementStateChangeFailed(element);
                    WebpumpIDEPlugin.log(x);
                }
                catch(RuntimeException x) {
                    fireElementStateChangeFailed(element);
                    WebpumpIDEPlugin.log(x);
                }
                if(info != null) {
                    ResourceMarkerAnnotationModel model = (ResourceMarkerAnnotationModel)((org.eclipse.ui.texteditor.AbstractDocumentProvider.ElementInfo) (info)).fModel;
                    model.updateMarkers(((org.eclipse.ui.texteditor.AbstractDocumentProvider.ElementInfo) (info)).fDocument);
                    info.fModificationStamp = computeModificationStamp(file);
                }
            } else {
                try {
                    monitor.beginTask(EditorMessages.getString("DefaultDocumentProvider.task.saving"), 2000);
                    ContainerGenerator generator = new ContainerGenerator(file.getParent().getFullPath());
                    generator.generateContainer(new SubProgressMonitor(monitor, 1000));
                    file.create(stream, false, new SubProgressMonitor(monitor, 1000));
                }
                finally {
                    monitor.done();
                }
            }
        }
        catch(IOException x) {
            WebpumpIDEPlugin.log(x);
        }
    }
    
    
    /**
     * Get encoding mode of the element .
     */
    public String getEncoding(Object element) {
        
        String encoding = super.getEncoding(element);
        if (encoding != null)
            return encoding;
        if(element instanceof IStorageEditorInput) {
            IStorageEditorInput sei = (IStorageEditorInput)element;
            try {
                InputStream in = sei.getStorage().getContents();
                try {
                    encoding = getDeclaredEncoding(in);
                }
                finally {
                    in.close();
                }
            }
            catch(CoreException _ex) { 
                WebpumpIDEPlugin.log(_ex);
            }
            catch(IOException _ex) { 
                WebpumpIDEPlugin.log(_ex);
            }
            if(encoding == null)
                encoding = getDefaultEncoding();
            setEncoding(element, encoding);
        }
        return encoding;
    }
    
    
    /**
     * Set encoding mode of the element.
     */
    public void setEncoding(Object element, String encoding) {
        
        if (encoding == null)
            encoding = getDefaultEncoding();
        super.setEncoding(element, encoding);
    }
    
    
    /**
     * Get encoding mode of input stream.
     * @param in input stream
     * @return 
     * @throws IOException
     */ 
    public String getDeclaredEncoding(InputStream in)
        throws IOException {
            
        return getBOMEncoding(in);
    }


    /**
     * Get encoding mode of input stream.
     * @param in
     * @return
     * @throws IOException
     */
    private String getBOMEncoding(InputStream in)
        throws IOException {
            
        int first = in.read();
        if(first < 0)
            return null;
        int second = in.read();
        if(second < 0)
            return null;
        if(first == 254 && second == 255)
            return "UTF-16BE";
        if(first == 255 && second == 254)
            return "UTF-16LE";
        int third = in.read();
        if(third < 0)
            return null;
        if(first == 239 && second == 187 && third == 191)
            return "UTF-8";
        else
            return null;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -