Skip to content

Commit

Permalink
init() in PrintSocketClient (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed Apr 24, 2024
1 parent 53d38a0 commit 656e1be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/qz/ws/PrintSocketClient.java
Expand Up @@ -21,6 +21,7 @@
import qz.printer.PrintServiceMatcher;
import qz.printer.status.StatusMonitor;
import qz.utils.*;
import qz.ws.substitutions.Substitutions;

import javax.usb.util.UsbUtil;
import java.awt.*;
Expand All @@ -44,6 +45,7 @@ public class PrintSocketClient {
private static final Logger log = LogManager.getLogger(PrintSocketClient.class);

private final TrayManager trayManager = PrintSocketServer.getTrayManager();
private static Substitutions substitutions = Substitutions.init();
private static final Semaphore dialogAvailable = new Semaphore(1, true);

//websocket port -> Connection
Expand Down
26 changes: 25 additions & 1 deletion src/qz/ws/substitutions/Substitutions.java
Expand Up @@ -6,10 +6,14 @@
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import qz.utils.FileUtilities;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -18,6 +22,7 @@
public class Substitutions {
protected static final Logger log = LogManager.getLogger(Substitutions.class);

private static final Path DEFAULT_SUBSTITUTIONS_PATH = FileUtilities.SHARED_DIR.resolve("substitutions.json");
// Subkeys that are restricted for writing
private static boolean restrictSubstitutions = true;
private static HashMap<String, Type> restricted = new HashMap<>();
Expand All @@ -27,6 +32,10 @@ public class Substitutions {
}
private ArrayList<JSONObject> matches, replaces;

public Substitutions(Path path) throws IOException, JSONException {
this(new FileInputStream(path.toFile()));
}

public Substitutions(InputStream in) throws IOException, JSONException {
this(IOUtils.toString(in, StandardCharsets.UTF_8));
}
Expand Down Expand Up @@ -98,8 +107,8 @@ public static void replace(JSONObject base, JSONObject replace) throws JSONExcep
// Good, let's make sure there are no exceptions
if(restrictSubstitutions) {
switch(type) {
// Special handling for arrays
case DATA:
// Special handling for arrays
JSONArray jsonArray = jsonReplace.optJSONArray(type.getKey());
removeRestrictedSubkeys(jsonArray, type);
break;
Expand Down Expand Up @@ -258,4 +267,19 @@ private static boolean find(Object base, Object match, boolean replace) throws J
public static void setRestrictSubstitutions(boolean restrictSubstitutions) {
Substitutions.restrictSubstitutions = restrictSubstitutions;
}

public static Substitutions init() {
return init(DEFAULT_SUBSTITUTIONS_PATH);
}
public static Substitutions init(Path path) {
Substitutions substitutions = null;
try {
substitutions = new Substitutions(path);
} catch(JSONException e) {
log.warn("Unable to parse substitutions file, skipping", e);
} catch(IOException e) {
log.info("Substitutions file missing, skipping: {}", e.getMessage());
}
return substitutions;
}
}

0 comments on commit 656e1be

Please sign in to comment.