📄 cmsrfsfileviewer.java
字号:
if (m_isLogfile) {
// stack is java hall of shame member... but standard
Stack inverter = new Stack();
for (int i = m_windowSize; i > 0 && read != null; i--) {
inverter.push(read);
read = reader.readLine();
}
// pop-off:
while (!inverter.isEmpty()) {
result.append(inverter.pop());
result.append('\n');
}
} else {
for (int i = m_windowSize; i > 0 && read != null; i--) {
result.append(read);
result.append('\n');
read = reader.readLine();
}
}
return CmsEncoder.escapeXml(result.toString());
} catch (IOException ioex) {
CmsRfsException ex = new CmsRfsException(Messages.get().container(
Messages.ERR_FILE_ARG_ACCESS_1,
m_filePath), ioex);
throw ex;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
LOG.error(e);
}
}
}
} else {
return Messages.get().getBundle().key(Messages.GUI_FILE_VIEW_NO_PREVIEW_0);
}
}
/**
* Internally sets the member <code>m_windowPos</code> to the last available
* window of <code>m_windowSize</code> windows to let further calls to
* <code>{@link #readFilePortion()}</code> display the end of the file. <p>
*
* This method is triggered when a new file is chosen
* (<code>{@link #setFilePath(String)}</code>) because the amount of lines changes.
* This method is also triggered when a different window size is chosen
* (<code>{@link #setWindowSize(int)}</code>) because the amount of lines to display change.
*
* @return the amount of lines in the file to view
*/
private int scrollToFileEnd() {
int lines = 0;
if (OpenCms.getRunLevel() < OpenCms.RUNLEVEL_3_SHELL_ACCESS) {
// no scrolling if system not yet fully initialized
} else {
LineNumberReader reader = null;
// shift the window position to the end of the file: this is expensive but OK for ocs logfiles as they
// are ltd. to 2 MB
try {
reader = new LineNumberReader(
new BufferedReader(new InputStreamReader(new FileInputStream(m_filePath))));
while (reader.readLine() != null) {
lines++;
}
reader.close();
// if 11.75 windows are available, we don't want to end on window nr. 10
int availWindows = (int)Math.ceil(lines / m_windowSize);
// we start with window 0
m_windowPos = availWindows - 1;
} catch (IOException ioex) {
LOG.error("Unable to scroll file " + m_filePath + " to end. Ensure that it exists. ");
} finally {
if (reader != null) {
try {
reader.close();
} catch (Throwable f) {
LOG.info("Unable to close reader of file " + m_filePath, f);
}
}
}
}
return lines;
}
/**
* Set the boolean that decides if the view to the underlying file via
* <code>{@link #readFilePortion()}</code> is enabled.<p>
*
* @param preview the boolean that decides if the view to the underlying file via
* <code>{@link #readFilePortion()}</code> is enabled
*/
public void setEnabled(boolean preview) {
m_enabled = preview;
}
/**
* Set the character encoding of the underlying file.<p>
*
* The given String has to match a valid charset name (canonical or alias)
* of one of the system's supported <code>{@link Charset}</code> instances
* (see <code>{@link Charset#forName(java.lang.String)}</code>).<p>
*
* This setting will be used for transcoding the file when portions
* of it are read via <code>{@link CmsRfsFileViewer#readFilePortion()}</code>
* to a String. This enables to correctly display files with text in various encodings
* in UIs.<p>
*
* @param fileEncoding the character encoding of the underlying file to set.
*/
public void setFileEncoding(String fileEncoding) {
checkFrozen();
try {
m_fileEncoding = Charset.forName(fileEncoding);
} catch (IllegalCharsetNameException icne) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_CHARSET_ILLEGAL_NAME_1,
fileEncoding));
} catch (UnsupportedCharsetException ucse) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_CHARSET_UNSUPPORTED_1,
fileEncoding));
}
}
/**
* Set the path in the real file system that points to the file
* that should be displayed.<p>
*
* This method will only suceed if the file specified by the <code>path</code>
* argument is valid within the file system, no folder and may be read by the
* OpenCms process on the current platform.<p>
*
* @param path the path in the real file system that points to the file that should be displayed to set
*
* @throws CmsRuntimeException if the configuration of this instance has been frozen
* @throws CmsRfsException if the given path is invalid, does not point to a file or cannot be accessed
*/
public void setFilePath(String path) throws CmsRfsException, CmsRuntimeException {
checkFrozen();
if (path != null) {
// leading whitespace from CmsComboWidget causes exception
path = path.trim();
}
if (CmsStringUtil.isEmpty(path)) {
throw new CmsRfsException(Messages.get().container(
Messages.ERR_FILE_ARG_EMPTY_1,
new Object[] {String.valueOf(path)}));
}
try {
// just for validation :
File file = new File(path);
if (file.isDirectory()) {
// if wrong configuration perform self healing:
if (OpenCms.getRunLevel() == OpenCms.RUNLEVEL_2_INITIALIZING) {
// this deletes the illegal entry and will default to the log file path
m_filePath = null;
m_isLogfile = true;
} else {
throw new CmsRfsException(Messages.get().container(
Messages.ERR_FILE_ARG_IS_FOLDER_1,
new Object[] {String.valueOf(path)}));
}
} else if (!file.isFile()) {
// if wrong configuration perform self healing:
if (OpenCms.getRunLevel() == OpenCms.RUNLEVEL_2_INITIALIZING) {
// this deletes the illegal entry and will default to the log file path
m_filePath = null;
m_isLogfile = true;
} else {
throw new CmsRfsException(Messages.get().container(
Messages.ERR_FILE_ARG_NOT_FOUND_1,
new Object[] {String.valueOf(path)}));
}
} else if (!file.canRead()) {
// if wrong configuration perform self healing:
if (OpenCms.getRunLevel() == OpenCms.RUNLEVEL_2_INITIALIZING) {
// this deletes the illegal entry and will default to the log file path
m_filePath = null;
m_isLogfile = true;
} else {
throw new CmsRfsException(Messages.get().container(
Messages.ERR_FILE_ARG_NOT_READ_1,
new Object[] {String.valueOf(path)}));
}
} else {
m_filePath = file.getCanonicalPath();
}
} catch (FileNotFoundException fnfe) {
// if wrong configuration perform self healing:
if (OpenCms.getRunLevel() == OpenCms.RUNLEVEL_2_INITIALIZING) {
// this deletes the illegal entry and will default to the log file path
m_filePath = null;
m_isLogfile = true;
} else {
throw new CmsRfsException(Messages.get().container(
Messages.ERR_FILE_ARG_NOT_FOUND_1,
new Object[] {String.valueOf(path)}), fnfe);
}
} catch (IOException ioex) {
// if wrong configuration perform self healing:
if (OpenCms.getRunLevel() == OpenCms.RUNLEVEL_2_INITIALIZING) {
// this deletes the illegal entry and will default to the log file path
m_filePath = null;
m_isLogfile = true;
} else {
throw new CmsRfsException(Messages.get().container(
Messages.ERR_FILE_ARG_ACCESS_1,
new Object[] {String.valueOf(path)}), ioex);
}
}
}
/**
* Package friendly access that allows the <code>{@link org.opencms.workplace.CmsWorkplaceManager}</code>
* to "freeze" this instance within the system-wide assignment in it's
* <code>{@link org.opencms.workplace.CmsWorkplaceManager#setFileViewSettings(org.opencms.file.CmsObject, CmsRfsFileViewer)}</code> method.<p>
*
* @param frozen if true this instance will freeze and throw <code>CmsRuntimeExceptions</code> upon setter invocations
*
* @throws CmsRuntimeException if the configuration of this instance has been frozen
* ({@link #setFrozen(boolean)})
*
*/
public void setFrozen(boolean frozen) throws CmsRuntimeException {
m_frozen = frozen;
}
/**
* Set if the internal file is in standard logfile format (true) or not (false).<p>
*
* If set to true the file might be
* treated / displayed in a more convenient format than standard files in future.
* Currently it is only inverted (last lines appear first) and only the last
* <code>windowsize</code> lines of the file are displayed.<p>
*
* Do not activate this (it is possible from the log file viewer settings in the workplace
* administration) if your selected file is no log file: The display will confuse you and
* be more expensive (imaging scrolling a 20 MB file to view the last 200 lines). <p>
*
* @param isLogfile determines if the internal file is in standard logfile format (true) or not (false)
*
* @throws CmsRuntimeException if the configuration of this instance has been frozen
* ({@link #setFrozen(boolean)})
*/
public void setIsLogfile(boolean isLogfile) throws CmsRuntimeException {
checkFrozen();
m_isLogfile = isLogfile;
}
/**
* Sets the start position of the current display.<p>
*
* This is a count of "windows" that
* consist of viewable text with "windowSize" lines of text (for a non-standard log file) or
* log-entries (for a standard log file).<p>
*
* @param windowPos the start position of the current display to set
* @throws CmsRuntimeException if the configuration of this instance has been frozen
* ({@link #setFrozen(boolean)})
*/
public void setWindowPos(int windowPos) throws CmsRuntimeException {
checkFrozen();
m_windowPos = windowPos;
}
/**
* Set the amount of lines (or entries depending on wether a standard log file is shown)
* to display per page.<p>
*
* @param windowSize the amount of lines to display per page
* @throws CmsRuntimeException if the configuration of this instance has been frozen
* ({@link #setFrozen(boolean)})
*
*/
public void setWindowSize(int windowSize) throws CmsRuntimeException {
checkFrozen();
m_windowSize = windowSize;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -