Skip to content

Commit

Permalink
Tweak for reading streams so help files are processed better. Help im…
Browse files Browse the repository at this point in the history
…provements.
  • Loading branch information
[email protected] authored and [email protected] committed Apr 24, 2024
1 parent 20776c3 commit 6e95833
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 58 deletions.
4 changes: 2 additions & 2 deletions language/buildNumber.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#maven.buildNumber.plugin properties file
#Wed Mar 27 14:15:30 CDT 2024
buildNumber\\d*=12888
#Wed Apr 24 08:17:20 CDT 2024
buildNumber\\d*=12894
Original file line number Diff line number Diff line change
Expand Up @@ -764,22 +764,31 @@ private void doStar(Polyad polyad, State state) {

private void doRemap(Polyad polyad, State state) {
if (polyad.isSizeQuery()) {
polyad.setResult(new int[]{2, 3});
polyad.setResult(new int[]{1, 2, 3});
polyad.setEvaluated(true);
return;
}
if (polyad.getArgCount() < 2) {
/* if (polyad.getArgCount() < 2) {
throw new MissingArgException(REMAP + " requires at least two arguments", polyad.getArgCount() == 1 ? polyad.getArgAt(0) : polyad);
}
if (3 < polyad.getArgCount()) {
throw new ExtraArgException(REMAP + " takes at most 3 arguments", polyad.getArgAt(3));
}
}*/
Object arg1 = polyad.evalArg(0, state);
checkNull(arg1, polyad.getArgAt(0));
if (!isStem(arg1)) {
throw new BadArgException(REMAP + " requires stem as its first argument", polyad.getArgAt(0));
}
QDLStem stem = (QDLStem) arg1;
if (polyad.getArgCount() == 1) {
// reverse keys and values
QDLStem out = reverseKeysAndValues(stem);
polyad.setResult(out);
polyad.setEvaluated(true);
polyad.setResultType(STEM_TYPE);
return;
}

Object arg2 = polyad.evalArg(1, state);
checkNull(arg2, polyad.getArgAt(1));

Expand Down Expand Up @@ -808,6 +817,19 @@ private void doRemap(Polyad polyad, State state) {

}

protected QDLStem reverseKeysAndValues(QDLStem inStem) {
QDLStem out = new QDLStem();
for (Object kk : inStem.keySet()) {
Object v = inStem.get(kk);
if (isLong(v) || isString(v)) {
out.putLongOrString(v, kk);
}
if (isStem(v)) {
out.putLongOrString(kk, reverseKeysAndValues((QDLStem) v));
}
}
return out;
}
protected void doIndices(Polyad polyad, State state) {
if (polyad.isSizeQuery()) {
polyad.setResult(new int[]{1, 2});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import edu.uiuc.ncsa.qdl.parsing.QDLParserDriver;
import edu.uiuc.ncsa.qdl.parsing.QDLRunner;
import edu.uiuc.ncsa.qdl.state.State;
import edu.uiuc.ncsa.qdl.util.QDLFileUtil;
import edu.uiuc.ncsa.qdl.workspace.WorkspaceCommands;
import edu.uiuc.ncsa.security.core.configuration.XProperties;
import edu.uiuc.ncsa.security.core.util.DebugUtil;
Expand Down Expand Up @@ -101,23 +102,14 @@ protected String getGUIHelp() {

InputStream helpStream = getClass().getResourceAsStream("/editor_help.txt");
if (helpStream != null) {
InputStreamReader isr = new InputStreamReader(helpStream);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder stringBuilder = new StringBuilder();
try {
String lineIn = bufferedReader.readLine();
while (lineIn != null) {
stringBuilder.append(lineIn + "\n");
lineIn = bufferedReader.readLine();
}
guiHelp = stringBuilder.toString();
bufferedReader.close();
guiHelp = QDLFileUtil.isToString(helpStream);
helpStream.close();
} catch (IOException e) {
if (DebugUtil.isEnabled()) {
e.printStackTrace();
}
}

}
}
return guiHelp;
Expand Down Expand Up @@ -173,28 +165,28 @@ public void keyPressed(KeyEvent e) {
case KeyEvent.VK_I:
// Insert in input form
if (e.isControlDown()) {
try {
String out = (String) getClipboard().getData(DataFlavor.stringFlavor);
out = LineUtil.toInputForm(out, e.isShiftDown());
String content = input.getText();
int position = input.getCaretPosition();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(content.substring(0, position));
stringBuilder.append(out);
if (position + 1 < content.length()) {
stringBuilder.append(content.substring(position + 1));
}
input.setText(null);
input.setText(stringBuilder.toString());
input.repaint();
input.setCaretPosition(position);

} catch (UnsupportedFlavorException | IOException ex) {
if (DebugUtil.isEnabled()) {
ex.printStackTrace();
}
break;
try {
String out = (String) getClipboard().getData(DataFlavor.stringFlavor);
out = LineUtil.toInputForm(out, e.isShiftDown());
String content = input.getText();
int position = input.getCaretPosition();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(content.substring(0, position));
stringBuilder.append(out);
if (position + 1 < content.length()) {
stringBuilder.append(content.substring(position + 1));
}
input.setText(null);
input.setText(stringBuilder.toString());
input.repaint();
input.setCaretPosition(position);

} catch (UnsupportedFlavorException | IOException ex) {
if (DebugUtil.isEnabled()) {
ex.printStackTrace();
}
break;
}
}
break;
case KeyEvent.VK_H:
Expand Down Expand Up @@ -285,10 +277,10 @@ public void keyPressed(KeyEvent e) {
case KeyEvent.VK_F1:
// If no selected text, put up a generic help message. Otherwise,
// search online help.
if(e.isControlDown()&& !e.isAltDown()){
if (e.isControlDown() && !e.isAltDown()) {
String x = getHelp("keyboard");
if (x != null) {
String helpMessage = x;
String helpMessage = x;
}
showHelp("QDL keyboard layout", x);
break;
Expand Down Expand Up @@ -364,7 +356,6 @@ public void keyPressed(KeyEvent e) {
}



protected void doQuit(boolean forceQuit) {
if (forceQuit) {
System.exit(0);
Expand Down
63 changes: 50 additions & 13 deletions language/src/main/java/edu/uiuc/ncsa/qdl/util/QDLFileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,44 @@
* <li>{@link #readTextFileAsStem(State, String)} - reads a text file as a stem list. </li>
* <li>{@link #writeTextFile(State, String, String)}</li>
* <li>{@link #readAttributes(State, String)} - get the attributes for a file (length, name, path...)</li>
*
*
* </ul>
* These are a facade for several calls. Note that for all workspace programming you should use these
* <p>Created by Jeff Gaynor<br>
* on 1/29/20 at 9:52 AM
*/
public class QDLFileUtil extends FileUtil {
/**
* Reads an {@link InputStream} as a string, does <i>not</i> close it when done!
*
* @param inputStream
* @return
* @throws Throwable
*/
public static String isToString(InputStream inputStream) throws IOException {
InputStreamReader isr = new InputStreamReader(inputStream);
return readerToString(isr);
}

public static String readerToString(Reader reader) throws IOException {
if(reader == null){
return "";
}
BufferedReader bufferedReader;

if(reader instanceof BufferedReader){
bufferedReader = (BufferedReader) reader;
}else{
bufferedReader = new BufferedReader(reader);
}
StringBuilder stringBuilder = new StringBuilder();
String lineIn = bufferedReader.readLine();
while (lineIn != null) {
stringBuilder.append(lineIn + "\n");
lineIn = bufferedReader.readLine();
}
return stringBuilder.toString();
}

public static QDLStem readFileAsStem(String fileName) throws Throwable {
checkFile(fileName);
Expand Down Expand Up @@ -71,10 +102,10 @@ public static QDLStem readFileAsStem(String fileName) throws Throwable {
public static List<String> readTextFileAsLines(State state, String fullPath) throws Throwable {
if (isVFSPath(fullPath)) {
String x = readTextVFS(state, fullPath);
if(x == null){
if (x == null) {
return null;
}
return StringUtils.stringToList(x);
return StringUtils.stringToList(x);
}
if (state.isServerMode()) {
throw new QDLServerModeException("unsupported in server mode");
Expand All @@ -84,6 +115,7 @@ public static List<String> readTextFileAsLines(State state, String fullPath) thr

/**
* Main entry point for reading a text file as lines
*
* @param state
* @param fullPath
* @return
Expand All @@ -101,6 +133,7 @@ public static String readTextFile(State state, String fullPath) throws Throwable

/**
* main entry point for reading a text file as a stem.
*
* @param state
* @param fullPath
* @return
Expand All @@ -115,6 +148,7 @@ public static QDLStem readTextFileAsStem(State state, String fullPath) throws Th

/**
* Main entry point for reading a binary file.
*
* @param state
* @param fullPath
* @return
Expand All @@ -132,6 +166,7 @@ public static byte[] readBinaryFile(State state, String fullPath) throws Throwab

/**
* Main entry point for writing a binary file
*
* @param state
* @param fullPath
* @param bytes
Expand All @@ -147,7 +182,7 @@ public static void writeBinaryFile(State state, String fullPath, byte[] bytes) t
Files.write(Paths.get(fullPath), bytes);
}

public static void writeTextFile(State state, String fullPath, String contents) throws Throwable{
public static void writeTextFile(State state, String fullPath, String contents) throws Throwable {
if (isVFSPath(fullPath)) {
writeTextVFS(state, fullPath, contents);
}
Expand All @@ -158,7 +193,7 @@ public static void writeTextFile(State state, String fullPath, String contents)
}


public static void writeTextFile(State state, String fullPath, List<String> contents) throws Throwable{
public static void writeTextFile(State state, String fullPath, List<String> contents) throws Throwable {
if (isVFSPath(fullPath)) {
writeTextVFS(state, fullPath, StringUtils.listToString(contents));
}
Expand Down Expand Up @@ -220,7 +255,7 @@ public static void writeTextVFS(State state, String path, String content) throws
public static byte[] readBinaryVFS(State state, String path) throws Throwable {
VFSFileProvider vfs = getVfsFileProvider(state, path);
VFSEntry vfsEntry = vfs.get(path, AbstractEvaluator.FILE_OP_BINARY);
if(vfsEntry == null){
if (vfsEntry == null) {
throw new QDLFileNotFoundException("file '" + path + "' not found");
}
return vfsEntry.getBytes();
Expand All @@ -229,7 +264,7 @@ public static byte[] readBinaryVFS(State state, String path) throws Throwable {
public static String readTextVFS(State state, String path) throws Throwable {
VFSFileProvider vfs = getVfsFileProvider(state, path);
VFSEntry vfsEntry = vfs.get(path, AbstractEvaluator.FILE_OP_TEXT_STRING);
if(vfsEntry == null){
if (vfsEntry == null) {
throw new QDLFileNotFoundException("file '" + path + "' not found");
}
return vfsEntry.getText();
Expand Down Expand Up @@ -379,6 +414,7 @@ public static class FileAttributes {

/**
* Read off the file attributes (such as name, length etc.) from the given file
*
* @param state
* @param fullPath
* @return
Expand Down Expand Up @@ -413,19 +449,20 @@ public static FileAttributes readAttributes(State state, String fullPath) throws

}

public static InputStream readFileAsInputStream(State state, String fullPath) throws Throwable{
if(isVFSPath(fullPath)){
public static InputStream readFileAsInputStream(State state, String fullPath) throws Throwable {
if (isVFSPath(fullPath)) {
return new ByteArrayInputStream(readBinaryVFS(state, fullPath));
}
if (state.isServerMode()) {
throw new QDLServerModeException("This operation is unsupported in server mode.");
}
throw new QDLServerModeException("This operation is unsupported in server mode.");
}
return new FileInputStream(new File(fullPath));
}

public static void copy(State state, String source, String target) throws Throwable {
public static void copy(State state, String source, String target) throws Throwable {
writeBinaryFile(state, target, readBinaryFile(state, source));
}
}

public static void main(String[] args) {
System.out.println(resolvePath("/a/b/c", "p/q"));
System.out.println(resolvePath("vfs#/a/b/c", "p/q"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@
*/
public class WorkspaceCommands implements Logable, Serializable {

private InputStream helpS;

public WorkspaceCommands() {
}

Expand Down Expand Up @@ -6018,7 +6020,19 @@ public void init(InputLine inputLine) throws Throwable {
}
}
}

helpStream.close();
// now add the editor help
helpStream = getClass().getResourceAsStream("/editor_help.txt");
String x = "(missing help)";
try{
x= QDLFileUtil.isToString(helpStream);
helpStream.close();
}catch(IOException iox){
if(isDebugOn()){
iox.printStackTrace();
}
}
onlineHelp.put("editor", x);
}
if (inputLine.hasArg(CONFIG_FILE_FLAG)) {
fromConfigFile(inputLine);
Expand Down
3 changes: 2 additions & 1 deletion language/src/main/resources/editor_help.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Help for the editor.
Help for the editor. This also applies to the main QDL input
panel when using the GUI.
(To get help for a topic, select the word, hit F1).

To see the special characters, select this word in the input window:
Expand Down
8 changes: 5 additions & 3 deletions language/src/main/resources/func_help.xml
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,11 @@ See also: copy]]>
</entry>
<entry id="remap">
<body>
<![CDATA[Take the elements in arg. and create a new stem. There are two cases:
<![CDATA[Take the elements in argument(s) and create a new stem. There are three cases:
#1 remap(arg.) - reverses the keys and values. Note that this only uses the values that are integers
or strings, since those are the only indices allowed.
remap(source_list., index_list.) - returns the elements of source_list.
#2 remap(source_list., index_list.) - returns the elements of source_list.
as specified in index_list. The result is always a simple list.
The contract runs as follows. If
Expand All @@ -684,7 +686,7 @@ Note that
r.4 := source_list.9
Most general case
remap(source_list., old_indices., new_indices) - returns the elements of source_list.
#3 remap(source_list., old_indices., new_indices) - returns the elements of source_list.
as specified by (assuming out. is the returned result)
out.new_indices.i := source_list.old_indices.i
Expand Down

0 comments on commit 6e95833

Please sign in to comment.