📄 propertiesdocumentprovider.java
字号:
/*
* $Header: /home/cvs/WEBPUMP2.0/WebPumpIDE_Src/WebPumpIDE/src/com/webpump/ui/properties/PropertiesDocumentProvider.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 com.webpump.ui.properties.DefaultDocumentProvider;
import com.webpump.ui.properties.EditorMessages;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.rules.IWhitespaceDetector;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.dialogs.ContainerGenerator;
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 PropertiesDocumentProvider extends DefaultDocumentProvider {
/** whitespace detector */
protected IWhitespaceDetector detector;
/**
* Constructor
*
*/
public PropertiesDocumentProvider() {
detector = new WhitespaceDetector();
}
/**
* Get default encoding mode.
*/
public String getDefaultEncoding() {
return "ISO-8859-1";
}
/**
* Get encoding mode of the input stream.
*/
public String getDeclaredEncoding(InputStream in)
throws IOException {
return getDefaultEncoding();
}
/**
* Set document content.
*
* @param document document in which input stream write
* @param contentStream input stream
* @param encoding encoding mode
*/
protected void setDocumentContent(IDocument document, InputStream contentStream, String encoding)
throws CoreException {
Reader in = null;
boolean flag = false;
try {
if(encoding == null)
encoding = getDefaultEncoding();
in = new BufferedReader(new InputStreamReader(contentStream, encoding));
StringBuffer buffer = new StringBuffer();
int ch = in.read();
label0:
while (ch >= 0) {
switch (ch) {
case 92: // '\\'
int c0 = in.read();
if (c0 < 0) {
buffer.append('\\');
break label0;
}
switch (c0) {
case 92: // '\\'
buffer.append("\\");
flag = true;
break;
case 117: // 'u'
int c1 = in.read();
int n = hex2int(c1);
if (n < 0) {
buffer.append("\\u");
if (ch < 0)
break label0;
ch = c1;
continue;
}
int sum = n << 12;
int c2 = in.read();
n = hex2int(c2);
if (n < 0) {
buffer.append("\\u").append((char)c1);
if (c2 < 0)
break label0;
ch = c2;
continue;
}
sum |= n << 8;
int c3 = in.read();
n = hex2int(c3);
if (n < 0) {
buffer.append("\\u").append((char)c1).append((char)c2);
if (c3 < 0)
break label0;
ch = c3;
continue;
}
sum |= n << 4;
int c4 = in.read();
n = hex2int(c4);
if (n < 0) {
buffer.append("\\u").append((char)c1).append((char)c2).append((char)c3);
if (c4 < 0)
break label0;
ch = c4;
continue;
}
sum |= n;
buffer.append((char)sum);
break;
default:
buffer.append('\\');
buffer.append((char)c0);
break;
}
break;
default:
buffer.append((char)ch);
break;
}
if(flag) {
ch = 92;
flag = false;
} else {
ch = in.read();
}
}
document.set(buffer.toString());
}
catch(IOException x) {
WebpumpIDEPlugin.log(x);
}
finally {
if (in != null)
try {
in.close();
}
catch(IOException ex) {
WebpumpIDEPlugin.log(ex);
}
}
}
/**
* Transform hex to int .
* @param ch
* @return
*/
private int hex2int(int ch) {
if(48 <= ch && ch <= 57)
return ch - 48;
if(97 <= ch && ch <= 102)
return (ch + 10) - 97;
if(65 <= ch && ch <= 70)
return (ch + 10) - 65;
else
return -1;
}
/**
* Transform int to hex.
* @param n
* @return
*/
private int int2hex(int n) {
return (n &= 0xf) >= 10 ? (n - 10) + 65 : n + 48;
}
/**
* Save document 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 {
Reader reader = new StringReader(document.get());
ByteArrayOutputStream buf = new ByteArrayOutputStream(document.getLength() * 2);
do {
int ch = reader.read();
if (ch < 0)
break;
if (ch > 126) {
buf.write(92);
buf.write(117);
buf.write(int2hex(ch >> 12));
buf.write(int2hex(ch >> 8));
buf.write(int2hex(ch >> 4));
buf.write(int2hex(ch));
} else {
buf.write(ch);
}
} while (true);
InputStream stream = new ByteArrayInputStream(buf.toByteArray());
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);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -