Java Code Examples for com.igormaznitsa.jbbp.JBBPParser#prepare()
The following examples show how to use
com.igormaznitsa.jbbp.JBBPParser#prepare() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JBBPToJavaConverterReadWriteTest.java From java-binary-block-parser with Apache License 2.0 | 6 votes |
@Test public void testReaWrite_StructMappedToInterface_Array_GettersSettersOn() throws Exception { final JBBPParser parser = JBBPParser.prepare("z { x { y [_] { byte a;}}}"); final Map<String, String> interfaceMap = new HashMap<>(); interfaceMap.put("z.x.y", ByteTestInterface.class.getCanonicalName()); final String text = JBBPToJavaConverter.makeBuilder(parser).setMainClassName(CLASS_NAME).setMainClassPackage(PACKAGE_NAME).setAddGettersSetters(true).setMapSubClassesInterfaces(interfaceMap).build().convert(); final String fullClassName = PACKAGE_NAME + '.' + CLASS_NAME; final ClassLoader classLoader = saveAndCompile(new JavaClassContent(fullClassName, text)); final Object instance = ReflectUtils.newInstance(classLoader.loadClass(fullClassName)); callRead(instance, new byte[] {0, 1, 2, 3, 4, 5}); final ByteTestInterface[] data = getFieldThroughGetters(instance, "z.x.y", ByteTestInterface[].class); for (int i = 0; i < 6; i++) { assertEquals(i, data[i].getA()); } assertArrayEquals(new byte[] {0, 1, 2, 3, 4, 5}, callWrite(instance)); }
Example 2
Source File: Bin2Json.java From openhab-core with Eclipse Public License 2.0 | 5 votes |
/** * Constructor. * * @param parserRule Binary data parser rule. * @throws ConversionException if parse rule parsing fails. */ public Bin2Json(String parserRule) throws ConversionException { try { parser = JBBPParser.prepare(parserRule); } catch (JBBPException e) { throw new ConversionException(String.format("Illegal parser rule, reason: %s", e.getMessage(), e)); } }
Example 3
Source File: JBBPToJBBPToJavaConverterCompilationTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testZ80snap1() throws Exception { final JBBPParser parser = JBBPParser.prepare("byte reg_a; byte reg_f; <short reg_bc; <short reg_hl; <short reg_pc; <short reg_sp; byte reg_ir; byte reg_r; " + "flags{ bit:1 reg_r_bit7; bit:3 bordercolor; bit:1 basic_samrom; bit:1 compressed; bit:2 nomeaning;}" + "<short reg_de; <short reg_bc_alt; <short reg_de_alt; <short reg_hl_alt; byte reg_a_alt; byte reg_f_alt; <short reg_iy; <short reg_ix; byte iff; byte iff2;" + "emulFlags{bit:2 interruptmode; bit:1 issue2emulation; bit:1 doubleintfreq; bit:2 videosync; bit:2 inputdevice;}" + "byte [_] data;"); assertCompilation(makeSources(parser, null, false, false, false)); assertCompilation(makeSources(parser, null, true, false, false)); }
Example 4
Source File: CommandFlasher.java From Flashtool with GNU General Public License v3.0 | 5 votes |
public void getUfsInfo() throws IOException,X10FlashException { logger.info("Sending Get-ufs-info"); String command = "Get-ufs-info"; USBFlash.write(command.getBytes()); CommandPacket reply = USBFlash.readCommandReply(true); logger.info(" Get-ufs-info status : "+reply.getResponse()); JBBPParser ufs_parser = JBBPParser.prepare( "byte headerlen;" + "byte[headerlen-1] ufs_header;" + "luns [_] { " + " byte lunlength; " + " byte reserved1; " + " byte lunid; " + " byte[12] reserved2; " + " int length; " + " byte[lunlength-19] lundata; " + "}" ); try { System.out.println(HexDump.toHex(reply.getDataArray())); JBBPBitInputStream stream = new JBBPBitInputStream(new ByteArrayInputStream(reply.getDataArray())); ufs_infos = ufs_parser.parse(stream).mapTo(new UfsInfos()); ufs_infos.setSectorSize(Integer.parseInt(getPhoneProperty("Sector-size"))); try { stream.close(); } catch (Exception streamclose ) {} } catch (Exception e) { System.out.println(e.getMessage()); ufs_infos=null; } }
Example 5
Source File: JBBPToJBBPToJavaConverterCompilationTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMakeInternalClassObjects_StaticClasses() throws Exception { final JBBPParser parser = JBBPParser.prepare("a { b { c [_] { byte d;}} }"); final String text = JBBPToJavaConverter .makeBuilder(parser) .setMainClassName(CLASS_NAME) .setAddGettersSetters(true) .build() .convert(); assertTrue(text.contains("public A makeA(){ this.a = new A(this); return this.a; }")); assertTrue(text.contains("public B makeB(){ this.b = new B(_Root_); return this.b; }")); assertTrue(text.contains("public C[] makeC(int _Len_){ this.c = new C[_Len_]; for(int i=0;i < _Len_;i++) this.c[i]=new C(_Root_); return this.c; }")); }
Example 6
Source File: JBBPToJBBPToJavaConverterCompilationTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testAllVariantsWithLinksToExternalStructures() throws Exception { final JBBPParser parser = JBBPParser.prepare("bit:1 bit1; bit:2 bit2; bit:3 bit3; bit:4 bit4; bit:5 bit5; bit:6 bit6; bit:7 bit7; bit:8 bit8;" + "byte alpha; ubyte beta; short gamma; ushort delta; bool epsilon; int teta; long longField; var varField;" + "floatj flt1; doublej dbl1;" + "struct1 { byte someByte; struct2 {bit:3 [34*someByte<<1+$ext] data;} }"); assertCompilation(makeSources(parser, null, false, false, false)); assertCompilation(makeSources(parser, null, true, false, false)); }
Example 7
Source File: JBBPTextWriterTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test public void testBin_StringFieldAndStringArray() throws Exception { final JBBPParser parser = JBBPParser.prepare("stringj[2];stringj;"); final byte[] testArray = new byte[] { 3, 65, 66, 67, 3, 68, 69, 70, 3, 71, 72, 73 }; final String text = makeWriter().SetMaxValuesPerLine(16).Bin(parser.parse(testArray)).Close().toString(); System.out.println(text); assertFileContent("txtwrtrjbbpobj3.txt", text); }
Example 8
Source File: JBBPFieldStructTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test public void testInterStructFieldReferences() throws Exception { final JBBPParser parser = JBBPParser.prepare("header {ubyte sections; ubyte datalen;} sections [header.sections]{byte[header.datalen] data;}"); final JBBPFieldArrayStruct sections = parser.parse(new byte[] {3, 2, 1, 2, 3, 4, 5, 6}).findFieldForNameAndType("sections", JBBPFieldArrayStruct.class); assertEquals(3, sections.size()); for (int i = 0; i < 3; i++) { JBBPFieldArrayByte data = sections.getElementAt(i).findFieldForNameAndType("data", JBBPFieldArrayByte.class); final int base = i * 2; assertArrayEquals(new byte[] {(byte) (base + 1), (byte) (base + 2)}, data.getArray()); } }
Example 9
Source File: JBBPToJBBPToJavaConverterCompilationTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testClassPackage() throws Exception { final JBBPParser parser = JBBPParser.prepare("byte a;"); assertFalse(JBBPToJavaConverter.makeBuilder(parser).setMainClassName(CLASS_NAME).build().convert().contains("package ")); assertFalse(JBBPToJavaConverter.makeBuilder(parser).setMainClassName(CLASS_NAME).setMainClassPackage("").build().convert().contains("package ")); assertTrue(JBBPToJavaConverter.makeBuilder(parser).setMainClassName(CLASS_NAME).setMainClassPackage("hello.world").build().convert().contains("package hello.world;")); }
Example 10
Source File: JBBPToJBBPToJavaConverterCompilationTest.java From java-binary-block-parser with Apache License 2.0 | 4 votes |
@Test void testStruct() throws Exception { final JBBPParser parser = JBBPParser.prepare("int;{byte;ubyte;{long;}}"); assertCompilation(makeSources(parser, null, false, false, false)); assertCompilation(makeSources(parser, null, true, true, false)); }
Example 11
Source File: JBBPToJBBPToJavaConverterCompilationTest.java From java-binary-block-parser with Apache License 2.0 | 4 votes |
@Test void testGenerateBinAnnotation() { final JBBPParser parser = JBBPParser.prepare("bit:3 someBit; bit:4 [12] bitArray; some {int a; floatj b; doublej[23] darr;}"); assertFalse(JBBPToJavaConverter.makeBuilder(parser).setMainClassName(CLASS_NAME).build().convert().contains("@Bin")); assertTrue(JBBPToJavaConverter.makeBuilder(parser).setMainClassName(CLASS_NAME).addBinAnnotations().build().convert().contains("@Bin")); }
Example 12
Source File: JBBPToJBBPToJavaConverterCompilationTest.java From java-binary-block-parser with Apache License 2.0 | 4 votes |
@Test void testStringFieldInExpression_CompilationErrorForStringFieldInArithmeticException() throws Exception { final JBBPParser parser = JBBPParser.prepare("stringj str; byte a; stringj [str+a] strarr; stringj [_] all;"); assertThrows(Exception.class, () -> assertCompilation(makeSources(parser, null, false, false, false))); assertThrows(Exception.class, () -> assertCompilation(makeSources(parser, null, true, false, false))); }
Example 13
Source File: JBBPToJBBPToJavaConverterCompilationTest.java From java-binary-block-parser with Apache License 2.0 | 4 votes |
@Test void testValFields() throws Exception { final JBBPParser parser = JBBPParser.prepare("ubyte a; ubyte b; val:(a+b*2) v; byte [v] data;"); assertCompilation(makeSources(parser, null, false, false, false)); assertCompilation(makeSources(parser, null, true, false, false)); }
Example 14
Source File: SinFile.java From Flashtool with GNU General Public License v3.0 | 4 votes |
public SinFile(File f) throws SinFileException { sinfile = f; JBBPParser sinParserV1 = JBBPParser.prepare( "byte multipleHeaders;" + "int headerLen;" + "byte payloadType;" + "short unknown;" + "byte memId;" + "byte compression;" + "int hashLen;" ); JBBPParser sinParserV2 = JBBPParser.prepare( "byte multipleHeaders;" + "int headerLen;" + "byte payloadType;" + "short unknown;" + "byte memId;" + "byte compression;" + "int hashLen;" ); JBBPParser sinParserV3 = JBBPParser.prepare( "byte[3] magic;" + "int headerLen;" + "int payloadType;" + "int hashType;" + "int reserved;" + "int hashLen;" ); JBBPParser sinParserv4 = JBBPParser.prepare( "byte[2] magic;" ); try { openStreams(); version = sinStream.readByte(); if (version==1) { sinv1 = sinParserV1.parse(sinStream).mapTo(new org.sinfile.parsers.v1.SinParser()); sinv1.setLength(sinfile.length()); sinv1.setFile(sinfile); if (sinv1.hashLen>sinv1.headerLen) throw new SinFileException("Error parsing sin file"); sinv1.parseHash(sinStream); closeStreams(); } if (version==2) { sinv2 = sinParserV2.parse(sinStream).mapTo(new org.sinfile.parsers.v2.SinParser()); sinv2.setLength(sinfile.length()); sinv2.setFile(sinfile); if (sinv2.hashLen>sinv2.headerLen) throw new SinFileException("Error parsing sin file"); sinv2.parseHash(sinStream); closeStreams(); } if (version==3) { sinv3 = sinParserV3.parse(sinStream).mapTo(new org.sinfile.parsers.v3.SinParser()); sinv3.setLength(sinfile.length()); sinv3.setFile(sinfile); if (!new String(sinv3.magic).equals("SIN")) throw new SinFileException("Error parsing sin file"); if (sinv3.hashLen>sinv3.headerLen) throw new SinFileException("Error parsing sin file"); sinv3.parseHash(sinStream); openStreams(); sinStream.skip(sinv3.headerLen); sinv3.parseDataHeader(sinStream); closeStreams(); } else { closeStreams(); version=4; sinv4 = new org.sinfile.parsers.v4.SinParser(sinfile); } } catch (Exception ioe) { closeStreams(); throw new SinFileException(ioe.getMessage()); } }
Example 15
Source File: CustomThreeByteIntegerTypeTest.java From java-binary-block-parser with Apache License 2.0 | 4 votes |
@Test public void testReadThreeByteInteger_WholeArray() throws Exception { final JBBPParser parser = JBBPParser.prepare("int24 [_] array;", new Int24CustomTypeProcessor()); assertArrayEquals(new int[] {0x010203, 0x040506, 0x070809}, parser.parse(new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}).findFieldForType(JBBPFieldArrayInt.class).getArray()); }
Example 16
Source File: JBBPToJBBPToJavaConverterCompilationTest.java From java-binary-block-parser with Apache License 2.0 | 4 votes |
@Test void testPrimitiveArrayInsideStructArray() throws Exception { final JBBPParser parser = JBBPParser.prepare("ubyte len; {ubyte[len];} ubyte [_] rest;"); assertCompilation(makeSources(parser, null, false, false, false)); assertCompilation(makeSources(parser, null, true, false, false)); }
Example 17
Source File: JBBPToJBBPToJavaConverterCompilationTest.java From java-binary-block-parser with Apache License 2.0 | 4 votes |
@Test void testStringFieldAsLength_CompilationErrorForStringFieldInArithmeticException() throws Exception { final JBBPParser parser = JBBPParser.prepare("stringj str; stringj [str] strarr; stringj [_] all;"); assertThrows(Exception.class, () -> assertCompilation(makeSources(parser, null, false, false, false))); assertThrows(Exception.class, () -> assertCompilation(makeSources(parser, null, true, false, false))); }
Example 18
Source File: JBBPToJBBPToJavaConverterCompilationTest.java From java-binary-block-parser with Apache License 2.0 | 4 votes |
@Test void testCustomText() throws Exception { final JBBPParser parser = JBBPParser.prepare("byte a;"); assertTrue(JBBPToJavaConverter.makeBuilder(parser).setMainClassName(CLASS_NAME).setMainClassCustomText("public void test(){}").build().convert().contains("public void test(){}")); }
Example 19
Source File: CustomThreeByteIntegerTypeTest.java From java-binary-block-parser with Apache License 2.0 | 4 votes |
@Test public void testReadThreeByte_NamedCustomFieldAsArrayLength() throws Exception { final JBBPParser parser = JBBPParser.prepare("int24 value; byte [value];", new Int24CustomTypeProcessor()); assertEquals(5, parser.parse(new byte[] {0, 0, 5, 1, 2, 3, 4, 5}).findFieldForType(JBBPFieldArrayByte.class).size()); }
Example 20
Source File: JavaConverter.java From java-binary-block-parser with Apache License 2.0 | 4 votes |
@Override @Nonnull public Set<File> translate(@Nonnull final Parameters parameters, final boolean dryRun) throws IOException { final String text; final String rawFileName; if (parameters.getScriptFile() == null) { rawFileName = parameters.getDestFileName() == null ? "JbbpNoName" : parameters.getDestFileName(); text = Assertions.assertNotNull("Script file is null, expected script text", parameters.getScriptText()); } else { final File scriptToProcess = parameters.getScriptFile(); rawFileName = FilenameUtils.getBaseName(scriptToProcess.getName()); text = FileUtils.readFileToString(scriptToProcess, parameters.getEncodingIn()); } final String className = CommonUtils.extractClassName(rawFileName); final String packageName = parameters.getPackageName() == null ? CommonUtils.extractPackageName(rawFileName) : parameters.getPackageName(); final Set<File> resultFiles = Collections.singleton(CommonUtils.scriptFileToJavaFile(parameters.getOutputDir(), parameters.getPackageName(), parameters.getScriptFile())); if (!dryRun) { final File resultJavaFile = resultFiles.iterator().next(); final JBBPParser parser = JBBPParser.prepare(text, JBBPBitOrder.LSB0, parameters.customFieldTypeProcessor, parameters.getParserFlags()); final String[] implementsSorted = parameters.getClassImplements().toArray(ARRAY_STRING_EMPTY); Arrays.sort(implementsSorted); final JBBPToJavaConverter.Builder builder = JBBPToJavaConverter.makeBuilder(parser) .setMapSubClassesInterfaces(parameters.getSubClassInterfaces()) .setMapSubClassesSuperclasses(parameters.getSubClassSuperclasses()) .setMainClassName(className) .setHeadComment(parameters.getHeadComment()) .setMainClassPackage(packageName) .setMainClassCustomText(parameters.getCustomText()) .setAddGettersSetters(parameters.isAddGettersSetters()) .setDoMainClassAbstract(parameters.isDoAbstract()) .setMainClassImplements(implementsSorted) .setParserFlags(parameters.getParserFlags()) .setSuperClass(parameters.getSuperClass()); if (parameters.isAddBinAnnotations()) { builder.addBinAnnotations(); } if (parameters.isAddNewInstanceMethods()) { builder.addNewInstanceMethods(); } if (parameters.isDoInternalClassesNonStatic()) { builder.doInternalClassesNonStatic(); } if (parameters.isDisableGenerateFields()) { builder.disableGenerateFields(); } FileUtils.write(resultJavaFile, builder.build().convert(), parameters.getEncodingOut()); } return resultFiles; }