use of tuwien.auto.calimero.xml.KNXMLException in project openremote by openremote.
the class KNXProtocol method discoverLinkedAssetAttributes.
@Override
public Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration, FileInfo fileInfo) throws IllegalStateException {
ZipInputStream zin = null;
try {
boolean fileFound = false;
byte[] data = CodecUtil.decodeBase64(fileInfo.getContents());
zin = new ZipInputStream(new ByteArrayInputStream(data));
ZipEntry zipEntry = zin.getNextEntry();
while (zipEntry != null) {
if (zipEntry.getName().endsWith("/0.xml")) {
fileFound = true;
break;
}
zipEntry = zin.getNextEntry();
}
if (!fileFound) {
String msg = "Failed to find '0.xml' in project file";
LOG.info(msg);
throw new IllegalStateException(msg);
}
// Create a transform factory instance.
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
TransformerFactory tfactory = TransformerFactory.newInstance();
// Create a transformer for the stylesheet.
Transformer transformer = tfactory.newTransformer(new StreamSource(this.getClass().getResourceAsStream("/org/openremote/agent/protocol/knx/ets_calimero_group_name.xsl")));
// Set the URIResolver
transformer.setURIResolver(new EtsFileUriResolver(data));
// Transform the source XML into byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
transformer.transform(new StreamSource(zin), new StreamResult(bos));
byte[] result = bos.toByteArray();
// we use a map of state-based datapoints and read from the transformed xml
final DatapointMap<StateDP> datapoints = new DatapointMap<>();
try (final XmlReader r = XmlInputFactory.newInstance().createXMLStreamReader(new ByteArrayInputStream(result))) {
datapoints.load(r);
} catch (final KNXMLException e) {
String msg = "Error loading parsed ETS file: " + e.getMessage();
LOG.warning(msg);
throw new IllegalStateException(msg, e);
}
MetaItem agentLink = AgentLink.asAgentLinkMetaItem(protocolConfiguration.getReferenceOrThrow());
Map<String, Asset> createdAssets = new HashMap<>();
for (StateDP dp : datapoints.getDatapoints()) {
if (dp.getName().endsWith("#A")) {
createAsset(dp, false, agentLink, createdAssets);
} else if (dp.getName().endsWith("#S")) {
createAsset(dp, true, agentLink, createdAssets);
} else if (dp.getName().endsWith("#SA") || dp.getName().endsWith("#AS")) {
createAsset(dp, false, agentLink, createdAssets);
createAsset(dp, true, agentLink, createdAssets);
} else {
LOG.info("Only group addresses ending on #A, #S, #AS or #SA will be imported. Ignoring: " + dp.getName());
}
}
return createdAssets.values().toArray(new Asset[createdAssets.values().size()]);
} catch (Exception e) {
throw new IllegalStateException("ETS import error", e);
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Aggregations