Skip to content

Commit

Permalink
feat: Added some commented out code that dumps the decrypted parts of…
Browse files Browse the repository at this point in the history
… ETS6+ files (helps debug ETS parsing)
  • Loading branch information
chrisdutz committed Sep 13, 2023
1 parent c268ae2 commit 688c85e
Showing 1 changed file with 32 additions and 0 deletions.
Expand Up @@ -40,9 +40,15 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.*;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
Expand Down Expand Up @@ -106,17 +112,28 @@ public EtsModel parse(File knxprojFile, String password) {
File tempFile = new File(tempDir.toFile(), projectNumber + ".zip");

// Unzip the archive inside the archive.
//noinspection CommentedOutCode
try (ZipFile projectZipFile = fileHandler.getProjectFiles(tempFile, password)) {
final FileHeader compressedProjectFileHeader = projectZipFile.getFileHeader("project.xml");
if (compressedProjectFileHeader == null) {
throw new PlcRuntimeException(String.format("Error accessing project header file: Project file 'project.xml' inside '%s.zip'.", projectNumber));
}
projectHeaderDoc = builder.parse(projectZipFile.getInputStream(compressedProjectFileHeader));

// TODO: Leave this in, as it helps debug problems, wen a new ETS version comes out.
// Print the document content to the console.
//System.out.println("Project Header Doc:");
//printDocument(projectHeaderDoc);

FileHeader projectFileFileHeader = projectZipFile.getFileHeader("0.xml");
if (projectFileFileHeader == null) {
throw new PlcRuntimeException("Error accessing project file.");
}
projectDoc = builder.parse(projectZipFile.getInputStream(projectFileFileHeader));

// TODO: Leave this in, as it helps debug problems, wen a new ETS version comes out.
//System.out.println("Project Doc:");
//printDocument(projectDoc);
}
} else {
projectHeaderDoc = builder.parse(zipFile.getInputStream(projectFileHeader));
Expand Down Expand Up @@ -256,4 +273,19 @@ private String getProjectNumber(ZipFile zipFile) throws ZipException
throw new PlcRuntimeException("Error determining project number.");
}

@SuppressWarnings("All")
public void printDocument(Document doc) {
try {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
System.out.println(writer.toString());
} catch(TransformerException ex) {
ex.printStackTrace();
}
}

}

0 comments on commit 688c85e

Please sign in to comment.