Bazel Build: Generate java classes from XSD using JAXB2 - xsd

I am new to Bazel. I want to generate Java classes from an XSD document using JaxB2.
I could not find any Java_Rule to auto-generate the classes.
Looking at this google group discussion, a genrule with explicit command seems to generate java classes for the author.
I tried using the github repository suggested in the discussion but failed with no such target '#local_jdk//:jdk-default': target 'jdk-default' not declared in package ' error.
➜ bazel-xjc-master bazel build //...
ERROR: /Users/<user>/bazel-xjc-master/BUILD.bazel:1:8: no such target '#local_jdk//:jdk-default': target 'jdk-default' not declared in package '' (did you mean 'jre-default'?) defined by /private/var/tmp/.../local_jdk/BUILD.bazel and referenced by '//:sample-messages'
ERROR: Analysis of target '//:sample-messages' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.152s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded, 6 targets configured)
I tried searchingjava_toolchain options to handle the above error but without any success.
Is there a Bazel build example file somewhere I can look at on how to generate java classes based on an XSD file?

The examples in the google group discussion were correct at the time, but Java toolchain integration with Bazel has changed over time, and indeed the target #local_jdk//:jdk-default no longer exists, and #local_jdk//:jar is deprecated.
But the bigger issue is that jaxb and xjc have been deprecated/removed from the jdk starting with jdk 9: http://openjdk.java.net/jeps/320
You'll need to get jaxb separately, something like this:
BUILD.bazel:
load("#rules_jvm_external//:defs.bzl", "artifact")
genrule(
name = "sample-messages",
srcs = [
"src/main/xsd/sample.xsd",
"src/main/xjb/sample.xjb",
],
outs = ["sample-messages.srcjar"],
cmd = """
mkdir sample-messages
$(location :xjc) -quiet -d sample-messages -b $(location src/main/xjb/sample.xjb) -verbose $(location src/main/xsd/sample.xsd)
jar cf $# sample-messages
""",
exec_tools = [
":xjc",
],
)
java_binary(
name = "xjc",
# See bin/xjc.sh in
# https://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-ri/3.0.2/jaxb-ri-3.0.2.zip
main_class = "com.sun.tools.xjc.XJCFacade",
runtime_deps = [
artifact("com.sun.xml.bind:jaxb-ri:3.0.2"),
],
)
java_library(
name = "sample-lib",
srcs = [":sample-messages"],
deps = [
artifact("jakarta.xml.bind:jakarta.xml.bind-api:3.0.1"),
]
)
WORKSPACE:
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
RULES_JVM_EXTERNAL_TAG = "4.2"
RULES_JVM_EXTERNAL_SHA = "cd1a77b7b02e8e008439ca76fd34f5b07aecb8c752961f9640dea15e9e5ba1ca"
http_archive(
name = "rules_jvm_external",
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
sha256 = RULES_JVM_EXTERNAL_SHA,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)
load("#rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")
rules_jvm_external_deps()
load("#rules_jvm_external//:setup.bzl", "rules_jvm_external_setup")
rules_jvm_external_setup()
load("#rules_jvm_external//:defs.bzl", "maven_install")
maven_install(
artifacts = [
"com.sun.xml.bind:jaxb-ri:3.0.2",
"jakarta.xml.bind:jakarta.xml.bind-api:3.0.1",
],
# rules_jvm_external doesn't handle zip dependencies correctly, see
# https://github.com/bazelbuild/rules_jvm_external/issues/671
excluded_artifacts = [
"com.sun.xml.bind:jaxb-samples",
"com.sun.xml.bind:jaxb-release-documentation",
],
repositories = [
"https://repo1.maven.org/maven2",
],
)
src/main/xjb/sample.xjb:
<?xml version='1.0' encoding='UTF-8'?>
<jxb:bindings version="3.0"
xmlns:jxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">
<jxb:bindings schemaLocation="../xsd/sample.xsd" node="/xs:schema">
<jxb:schemaBindings>
<jxb:package name="org.acme.sample"/>
</jxb:schemaBindings>
</jxb:bindings>
</jxb:bindings>
src/main/xsd/sample.xsd:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Sample">
<xs:complexType>
<xs:sequence>
<xs:element name="Date" type="xs:date"/>
<xs:element name="Content">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Note" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="color" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>

Related

Test NG Cucumber Parallel Automation with Dynamic test tags and param

I am currently looking to run multiple test in cucumber in parallel using testng and I successfully managed to do that.
Now my requirement is rather than having multiple Test TAGS in the testNG file with different parameters take it from the maven command line. So I can do automation without editing the testNg.xml file. Is there a way to achieve it? Please find my current testng.xml configuration.
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Testng Cucumber Suite" thread-count="2" parallel="tests">
<!-- In order to run test cases please copy and one test and add make sure you add relevant parameters -->
<test name="Run_Nexus_06">
<parameter name="deviceName" value="Google Nexus 6" />
<parameter name="platformVersion" value="6.0" />
<classes>
<class name="cucumber.mobile.ParallelRunner">
</class>
</classes>
</test>
<test name="Run_Google_Pixel">
<parameter name="deviceName" value="Google Pixel" />
<parameter name="platformVersion" value="7.1" />
<classes>
<class name="cucumber.mobile.ParallelRunner">
</class>
</classes>
</test>
</suite>
Runner Class :
#CucumberOptions(plugin = {"pretty", "html:target/html/", "json:target/cucumber.json", "junit:TEST-all.xml"},
features = "src/test/resources/features/SignUp.feature", glue = {"steps"}, tags = {"#Mobile"})
public class ParallelRunner extends Hook{
List<Object[]> data;
//<parameter name="deviceName" value="Google Pixel" />
// <parameter name="platformVersion" value="7.1" />
#BeforeTest
#Parameters({"deviceName","platformVersion"})
public void bb(String deviceName, String platformVersion){
Device device = new Device();
device.setDeviceName(deviceName);
device.setOsVersion(platformVersion);
DeviceFactory.setDevice(device);
System.out.println("Device" + deviceName + "Os Version" + platformVersion + " " + Thread.currentThread().getId());
}
}
I was glad that I found this post. Very useful if anyone trying to achieve the same thing. Dynamic Test ng IAlterSuiteListener
Maven command :> mvn compile test -DdeviceFlavors="Google Nexus 6","Google Pixel" -DdeviceOsFlavors="6.0","7.1" -Dsurefire.suiteXmlFiles=testng.xml

NLog default-target-parameters from Xml configuration are not applied to programmatically added file targets

I'm working on .Net Core 2.0 console application. NLog version 4.5.7.
Here is my config file:
<?xml version="1.0" encoding="utf-8" ?>
<nlog
xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="false"
internalLogFile="Logs/Internal/log.txt"
internalLogLevel="Warn"
internalLogToConsole="false"
internalLogToConsoleError="false"
internalLogToTrace="false"
internalLogIncludeTimestamp="true">
<targets>
<default-wrapper
xsi:type="AsyncWrapper"
timeToSleepBetweenBatches="0"
overflowAction="Block"/>
<default-target-parameters
xsi:type="File"
layout="${longdate}|${level:uppercase=true}|${message}
${onexception:${newline}}${exception:format=tostring}"
archiveAboveSize="10485760"
maxArchiveFiles="10"
archiveNumbering="DateAndSequence"
archiveOldFileOnStartup="true"
keepFileOpen="true"
cleanupFileName="false"/>
<target
name="driver"
xsi:type="File"
archiveFileName="Logs/Driver/Archives/log.{#####}.txt"
fileName="Logs/Driver/log.txt"/>
</targets>
<rules>
<logger
name="driver"
minlevel="Trace"
writeTo="driver"/>
</rules>
</nlog>
I'm setting this config on startup of my application:
LogManager.Configuration = new XmlLoggingConfiguration(<path>, false);
And then I'm adding file targets to this configuration programmatically:
string
deviceTag = string.Concat("Device_", _id.ToString()),
loggerName = string.Concat("logger_", deviceTag);
LogManager.Configuration.AddRuleForAllLevels(
new FileTarget(string.Concat("target_", deviceTag))
{
FileName = string.Concat("Logs/", deviceTag, "/log.txt"),
ArchiveFileName = string.Concat("Logs/", deviceTag,
"/Archives/log.{#####}.txt")
}, loggerName);
_logger = LogManager.GetLogger(loggerName);
LogManager.ReconfigExistingLoggers();
Target is working, logs are written. But default-target-parameters from xml config are ignored.
This is a bug or a feature?
Or I do something wrong?

Error processing / merging manifest

I get this error
W:\android-studio-projects\sharedid\app\build\intermediates\merged_manifests\flavor_customer1Debug\processFlavor_customer1DebugManifest\merged\AndroidManifest.xml:49:
error: resource string/MyAppName (aka
com.customer1.app:string/MyAppName) not found.
error: failed processing manifest.
...
My gradle contains this
flavor_customer1 {
java.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/java/"]
manifest.srcFile "W:/android-studio-projects/sharedid/app/src/customer1/AndroidManifest.xml"
assets.srcDirs = ["W:/android-studio-projects/sharedid/app/src/customer1/assets/"]
resources.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/res/", "W:/android-studio-projects/sharedid/app/src/customer1/res/"]
}
I have defined MyAppName in file
"W:/android-studio-projects/sharedid/app/src/customer1/res/values/strings_specific.xml"
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="MyAppName">Customer 1</string>
</resources>
In "W:/android-studio-projects/sharedid/app/src/customer1/AndroidManifest.xml" I use the string like this
<application
android:allowBackup="true"
android:icon="#drawable/app_logo__forlarge"
android:label="#string/MyAppName"
android:theme="#style/AppBaseTheme_Customer_One"
android:name="com.shared.app.MyApp"
>
...
What am I missing? I am trying to switch over to using product flavors
Apparentlt merge depends on "main" folder also containing strings.xml even though each flavor has their own

libxmljs: how to use xinclude?

I try to use libxmljs with nodejs.
In my sample I want to use the xi:include.
<ord:order xmlns:ord="http://example.org/ord"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.org/ord chapter04ord1.xsd"
xmlns:xi="http://www.w3.org/2001/XInclude">
<number>123ABBCC123</number>
<customer>
<name>Priscilla Walmsley</name>
<number>15466</number>
</customer>
<items>
<xi:include href="/Users/mar/ws_e/libxml/test/resources/chapter5prod.xml" />
</items>
</ord:order>
include File:
<ord:product xmlns:ord="http://example.org/ord"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.org/ord chapter04ord1.xsd">
<number>557</number>
<name>Short-Sleeved Linen Blouse</name>
<size system="US-DRESS">10</size>
<color value="blue"/>
</ord:product>
This is my code:
var xsdDoc = libxml.parseXml(schemaSource);
var xmlDocValid = libxml.parseXml(docSource);
var xmlDocInvalid = libxml.parseXml(xml_invalid);
xmlDocValid.validate(xsdDoc);
console.dir(xmlDocValid);
console.dir(xmlDocValid.validationErrors);
Then I get the error:
Document {
errors: [],
validationErrors:
[ { Error: Element '{http://www.w3.org/2001/XInclude}include': This element is not expected. Expected is ( product ).
So my question is how to use the x:include right at libxmljs?
Kind regards
Markus

Read TLV encoded data on JavaCard Classic

I'd like to read TLV encoded certificates on a Java Card (NXP JCOP J3D081, JCOP version 2.4.2, Java Card version 3.0.1 Classic).
The cap file is created successfully, but when I try to install it on the card I get an gpshell error:
load() returns 0x80206A80 (6A80: Wrong data / Incorrect values in command data.)
I use the jars from JCDK 3.0.3 and this usually works with stuff like elliptic curves etc. What could be different with the TLV stuff?
The applet code used (installs fine when not using the BERTLV stuff):
package org.thomas;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;
import javacardx.framework.tlv.BERTLV;
import javacardx.framework.tlv.ConstructedBERTLV;
public class TlvApplet extends Applet {
private ConstructedBERTLV certificate;
byte[] certificateLength = new byte[2];
/**
* TlvApplet constructor
*
* #constructor
*/
private TlvApplet() {
// Register with the JCRE
register();
}
/**
* Installs applet
*
* #param bArray
* the array containing installation parameters
* #param bOffset
* the starting offset in bArray
* #param bLength
* the length in bytes of the parameter data in bArray
*/
public static void install(byte[] bArray, short bOffset, byte bLength) {
new TlvApplet();
}
public void process(APDU apdu) throws ISOException {
byte buffer[] = apdu.getBuffer();
short incomingLength = apdu.setIncomingAndReceive();
certificate = (ConstructedBERTLV) BERTLV.getInstance(buffer,
ISO7816.OFFSET_CDATA, incomingLength);
certificateLength[0] = (byte) (certificate.size() & 0xff);
certificateLength[1] = (byte) ((certificate.size() >> 8) & 0xff);
Util.arrayCopyNonAtomic(certificateLength, (short) 0, buffer,
ISO7816.OFFSET_CDATA, (short) 2);
}
}
I create the cap file by using the following ant build.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<project default="convert" name="Create JC 3 applet" basedir=".">
<property environment="env" />
<!-- Include properties in the build.properties file -->
<property file="build.properties" />
<!-- Build specific properties -->
<property name="source.path" location="${basedir}/${source.folder}" />
<property name="jcdk.libs" location="${basedir}/${jcdk.basefolder}/${jcdk.subfolder.libs}" />
<property name="jcdk.apiexports" location="${basedir}/${jcdk.basefolder}/${jcdk.subfolder.apiexportfiles}" />
<property name="target.path.classes"
location="${basedir}/${target.basefolder}/${target.subfolder.classes}" />
<property name="target.path.cap"
location="${basedir}/${target.basefolder}/${target.subfolder.applet}" />
<!-- set the classpath for the tasks and the API, include all jar files -->
<path id="classpath" description="Sets the classpath to Java Card API and tools">
<fileset dir="${jcdk.libs}">
<include name="*.jar" />
</fileset>
</path>
<!-- set the export path to the Java Card export files -->
<path id="export" description="set the export file path">
<fileset dir="${jcdk.apiexports}">
<include name="**/*.exp" />
</fileset>
<pathelement path="${jcdk.apiexports}" />
<pathelement path="${target.path.classes}" />
</path>
<path id="capexport" description="set the export file for the cap path">
<fileset dir="${jcdk.apiexports}">
<include name="**/*.exp" />
</fileset>
<pathelement path="${jcdk.apiexports}" />
</path>
<!-- Definitions for tasks for Java Card tools -->
<taskdef name="apdu" classname="com.sun.javacard.ant.tasks.APDUToolTask" classpathref="classpath" />
<taskdef name="capgen" classname="com.sun.javacard.ant.tasks.CapgenTask" classpathref="classpath" />
<taskdef name="convert" classname="com.sun.javacard.ant.tasks.ConverterTask" classpathref="classpath" />
<taskdef name="verifyexport" classname="com.sun.javacard.ant.tasks.VerifyExpTask" classpathref="classpath" />
<taskdef name="verifycap" classname="com.sun.javacard.ant.tasks.VerifyCapTask" classpathref="classpath" />
<taskdef name="verifyrevision" classname="com.sun.javacard.ant.tasks.VerifyRevTask" classpathref="classpath" />
<typedef name="appletnameaid" classname="com.sun.javacard.ant.types.AppletNameAID" classpathref="classpath" />
<typedef name="jcainputfile" classname="com.sun.javacard.ant.types.JCAInputFile" classpathref="classpath" />
<typedef name="scriptgen" classname="com.sun.javacard.ant.tasks.ScriptgenTask" classpathref="classpath" />
<target name="init">
<mkdir dir="${target.path.classes}" />
</target>
<target name="clean">
<delete dir="${target.path.classes}" />
</target>
<target name="compile" depends="init" description="Compile source code to class files">
<javac debug="${javac.debug}"
optimize="${javac.optimize}"
srcdir="${source.path}"
destdir="${target.path.classes}"
source="${javac.version.source}"
target="${javac.version.target}">
<classpath refid="classpath" />
</javac>
</target>
<target name="convert" depends="compile" description="Convert class files to cap files">
<convert packagename="${package.name}"
packageaid="${package.aid}"
majorminorversion="${applet.version}"
classdir="${target.path.classes}"
outputdirectory="${target.path.cap}"
jca="${cap.creation.jca}"
exp="${cap.creation.exp}"
cap="true"
debug="${cap.creation.debug}" verbose="${cap.creation.verbose}"
noverify="${cap.creation.noverify}">
<appletnameaid aid="${applet.aid}" appletname="${applet.name}" />
<exportpath refid="capexport" />
<classpath refid="classpath" />
</convert>
</target>
<target name="all" depends="clean, convert" />
</project>
The build.properties used:
# Source folder
source.folder = src/org/thomas
# Java Card Development Kit folders
jcdk.basefolder = lib/javaCardKit303
jcdk.subfolder.apiexportfiles = api_export_files
jcdk.subfolder.libs = lib
# Target folders (will be created, no need to adapt)
target.basefolder = target
target.subfolder.classes = classes
target.subfolder.applet = applet
# Applet properties
package.aid = 0x41:0x41:0x41:0x41:0x41:0x41:0x41:0x41:0x41:0x42
package.name = org.thomas
applet.aid = 0x41:0x41:0x41:0x41:0x41:0x41:0x41:0x41:0x41:0x42:0x42
applet.name = TlvApplet
applet.version = 1.0
# Java class compiler options
javac.debug = no
javac.optimize = no
javac.version.source = 1.5
javac.version.target = 1.5
# CAP file creation options
cap.creation.verbose = false
cap.creation.noverify = false
cap.creation.debug = false
cap.creation.jca = false
cap.creation.exp = false
So suspected the cap to be compiled with the api_connected.jar, but removing the file from kit, didn't change anything.
Any help would be appreciated. Thanks in advance, Thomas
The card simply does not support the TLV classes of the java card API. The JC API is just a recommendation but the card manufacturer may choose to implement a subset of it.
In general anything in javacardx is optional (hence the x at the end). The API methods in javacard must be implemented though. Even for javacard packages and classes not all functionality may be available at runtime. For instance, cryptographic algorithms may not be present.

Resources