Skip to content

Commit

Permalink
Merge branch 'girr-input'
Browse files Browse the repository at this point in the history
  • Loading branch information
bengtmartensson committed Jan 11, 2024
2 parents 483cf0a + 41c1a83 commit 0205c0c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 7 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@
<include>IrpProtocols2html.xsl</include>
<include>${project.artifactId}.xml</include>
<include>irp-protocols.xsd</include>
<include>rawgirr2named.xsl</include>
</includes>
</configuration>
</plugin>
Expand Down
30 changes: 27 additions & 3 deletions src/main/java/org/harctoolbox/cmdline/CommandAnalyze.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
Expand Down Expand Up @@ -66,6 +67,10 @@
@Parameters(commandNames = {"analyze"}, commandDescription = "Analyze signal: tries to find an IRP form with parameters.")
public class CommandAnalyze extends AbstractCommand {

/**
* Path name, as found in the jar, of the XSLT style sheet taking raw girr to named input format.
*/
public static final String RAWGIRR2NAMEINPUT = "/rawgirr2named.xsl";
private static final Logger logger = Logger.getLogger(CommandAnalyze.class.getName());

@Parameter(names = {"-a", "--all"}, description = "List all decoder outcomes, instead of only the one with lowest weight.")
Expand Down Expand Up @@ -96,9 +101,12 @@ public class CommandAnalyze extends AbstractCommand {
@Parameter(names = {"-f", "--frequency"}, converter = FrequencyParser.class, description = "Modulation frequency of raw signal.")
private Double frequency = null;

@Parameter(names = {"-g", "--girr"}, description = "Generate Girr file. Inhibits all other output.")
@Parameter(names = {"-g", "--girroutput"}, description = "Generate Girr file. Inhibits all other output.")
private boolean girr = false;

@Parameter(names = {"-G", "--girrinput"}, description = "Read raw input in Girr format.")
private String girrInput = null;

@Parameter(names = {"-i", "--input"}, description = "File/URL from which to take inputs, one sequence per line.")
private String input = null;

Expand Down Expand Up @@ -241,8 +249,8 @@ private void analyze() throws IrpException, IrCoreException, NoDecoderMatchExcep
if (parameterTable && eliminateVars)
throw new UsageException("Parameter table is meaninless together with --eliminate-vars");

if (IrCoreUtils.numberTrue(input != null, namedInput != null, args != null) != 1)
throw new UsageException("Must use exactly one of --input, --namedinput, and non-empty arguments");
if (IrCoreUtils.numberTrue(input != null, namedInput != null, girrInput != null, args != null) != 1)
throw new UsageException("Must use exactly one of --input, --namedinput, --girrinput and non-empty arguments");

if (maxParameterWidth > FiniteBitField.MAXWIDTH) {
logger.log(Level.WARNING, "The given value of --maxparameterwidth ({0}) is larger than {1}. This is using unspecified behavior, and the correct execution is not guaranteed.",
Expand Down Expand Up @@ -276,6 +284,22 @@ private void analyze() throws IrpException, IrCoreException, NoDecoderMatchExcep
if (signals.isEmpty())
throw new InvalidArgumentException("No parseable sequences found.");
analyze(signals);
} else if (girrInput != null) {
if (validate)
throw new UsageException("Cannot use --validate with --girrinput.");

ThingsLineParser<ModulatedIrSequence> thingsLineParser = new ThingsLineParser<>(
(List<String> line) -> {
return (MultiParser.newIrCoreParser(line)).toModulatedIrSequence(frequency, trailingGap);
}, commandLineArgs.commentStart
);
InputStream xsltStream = CommandAnalyze.class.getResourceAsStream(RAWGIRR2NAMEINPUT);
Document xsltDoc = XmlUtils.openXmlStream(xsltStream, null, true, true);
Map<String, ModulatedIrSequence> signals = thingsLineParser.readNamedThings(girrInput, xsltDoc, commandLineArgs.inputEncoding);
if (signals.isEmpty()) {
throw new InvalidArgumentException("No parseable sequences found.");
}
analyze(signals);
} else {
MultiParser parser = MultiParser.newIrCoreParser(args);
if (introRepeatEnding) {
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/org/harctoolbox/cmdline/CommandDecode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
Expand Down Expand Up @@ -66,9 +67,12 @@ public class CommandDecode extends AbstractCommand {
@Parameter(names = {"-f", "--frequency"}, converter = FrequencyParser.class, description = "Set modulation frequency.")
private Double frequency = null;

@Parameter(names = {"-g", "--girr"}, description = "Generate output in Girr format (only)")
@Parameter(names = {"-g", "--girroutput"}, description = "Generate output in Girr format (only)")
private boolean girr = false;

@Parameter(names = {"-G", "--girrinput"}, description = "Read raw input in Girr format.")
private String girrInput = null;

@Parameter(names = {"-l", "--ignoreleadinggarbage"}, description = "Accept decodes starting with undecodable pairs.")
private boolean ignoreLeadingGarbage = false;

Expand Down Expand Up @@ -153,8 +157,8 @@ private class DecodeClass {
private void decode() throws UsageException, IrpParseException, IOException, InvalidArgumentException, SAXException, TransformerException {
CmdUtils.checkForOption("decode", args);

if (IrCoreUtils.numberTrue(input != null, namedInput != null, args != null) != 1)
throw new UsageException("Must use exactly one of --input, --namedinput, and non-empty arguments");
if (IrCoreUtils.numberTrue(input != null, namedInput != null, girrInput != null, args != null) != 1)
throw new UsageException("Must use exactly one of --input, --namedinput, --girrinput and non-empty arguments");
if (ignoreLeadingGarbage && strict)
throw new UsageException("--strict and --ignoreleadinggarbage may not be used together.");

Expand All @@ -181,6 +185,14 @@ private void decode() throws UsageException, IrpParseException, IOException, Inv
? irSignalParser.readNamedThings(namedInput, commandLineArgs.inputEncoding)
: irSignalParser.readNamedThings(namedInput, xslt, commandLineArgs.inputEncoding);
decode(signals);
} else if (girrInput != null) {
ThingsLineParser<IrSignal> irSignalParser = new ThingsLineParser<>((List<String> line) -> {
return (MultiParser.newIrCoreParser(line)).toIrSignal(frequency, trailingGap);
}, commandLineArgs.commentStart);
InputStream xsltStream = CommandAnalyze.class.getResourceAsStream(CommandAnalyze.RAWGIRR2NAMEINPUT);
Document xsltDoc = XmlUtils.openXmlStream(xsltStream, null, true, true);
Map<String, IrSignal> signals = irSignalParser.readNamedThings(girrInput, xsltDoc, commandLineArgs.inputEncoding);
decode(signals);
} else {
MultiParser prontoRawParser = MultiParser.newIrCoreParser(args);
IrSignal irSignal = prontoRawParser.toIrSignal(frequency, trailingGap);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/harctoolbox/ircore/ThingsLineParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.logging.Logger;
import javax.xml.transform.TransformerException;
import org.harctoolbox.xml.XmlUtils;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class ThingsLineParser<T> {
Expand Down Expand Up @@ -94,6 +95,13 @@ public Map<String, T> readNamedThings(String namedInput, String xslt, String enc
}
}

public Map<String, T> readNamedThings(String namedInput, Document stylesheet, String encoding) throws SAXException, IOException, TransformerException {
Objects.requireNonNull(stylesheet);
try (InputStreamReader reader = XmlUtils.mkReaderXml(namedInput, stylesheet, encoding)) {
return readNamedThings(reader);
}
}

public Map<String, T> readNamedThings(Reader reader) throws IOException {
BufferedReader in = new BufferedReader(reader);
Map<String, T> map = new LinkedHashMap<>(4);
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/harctoolbox/xml/XmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,12 @@ public static ByteArrayInputStream renderDOM(Document document, Document xslt, S
}

public static InputStreamReader mkReaderXml(String docu, String xslt, String encoding) throws SAXException, UnsupportedEncodingException, TransformerException, IOException {
Document document = XmlUtils.openXmlThing(docu, null, true, true);
Document stylesheet = XmlUtils.openXmlThing(xslt, null, true, true);
return mkReaderXml(docu, stylesheet, encoding);
}

public static InputStreamReader mkReaderXml(String docu, Document stylesheet, String encoding) throws SAXException, UnsupportedEncodingException, TransformerException, IOException {
Document document = XmlUtils.openXmlThing(docu, null, true, true);
InputStream inputStream = XmlUtils.renderDOM(document, stylesheet, encoding);
return new InputStreamReader(inputStream, encoding);
}
Expand Down
File renamed without changes.

0 comments on commit 0205c0c

Please sign in to comment.