From 20a3ab6d73503b07ea9f0c2ba6336b3ed6647f36 Mon Sep 17 00:00:00 2001 From: nedlir Date: Sat, 26 Feb 2022 22:22:07 +0200 Subject: [PATCH] isExistSecurityElement() check --- FileManipulator.java | 25 +++++++++++++------------ XMLParser.java | 24 ++++++++++++------------ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/FileManipulator.java b/FileManipulator.java index 3182794..7c4059b 100644 --- a/FileManipulator.java +++ b/FileManipulator.java @@ -1,3 +1,4 @@ + import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; @@ -37,17 +38,17 @@ public void extractFile(String targetFile) throws Exception { } public void insertFile(String targetFile) throws Exception { - Path myFilePath = Paths.get(targetFile); - - Path zipFilePath = Paths.get(this.fileName); - try( FileSystem fs = FileSystems.newFileSystem(zipFilePath, null) ){ - Path fileInsideZipPath = fs.getPath("/ppt/" + targetFile); - Files.delete(fileInsideZipPath); - Files.copy(myFilePath, fileInsideZipPath); - - } catch (IOException e) { - e.printStackTrace(); - } + Path myFilePath = Paths.get(targetFile); + + Path zipFilePath = Paths.get(this.fileName); + try (FileSystem fs = FileSystems.newFileSystem(zipFilePath, null)) { + Path fileInsideZipPath = fs.getPath("/ppt/" + targetFile); + Files.delete(fileInsideZipPath); + Files.copy(myFilePath, fileInsideZipPath); + + } catch (IOException e) { + e.printStackTrace(); + } } -} \ No newline at end of file +} diff --git a/XMLParser.java b/XMLParser.java index fcbf074..709cc79 100644 --- a/XMLParser.java +++ b/XMLParser.java @@ -1,27 +1,28 @@ + import java.io.File; import java.io.FileInputStream; - import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; - import org.w3c.dom.Document; import org.w3c.dom.Element; - public class XMLParser { private final String TARGET_ELEMENT = "p:modifyVerifier"; // hashed password element private String filePath; + private Element securityElement; private Document document; public XMLParser(String filePath) throws Exception { this.filePath = filePath; this.document = XMLFileToDocument(); + // get the element TARGET_ELEMENT (the hashed password element): + this.securityElement = (Element) document.getElementsByTagName(TARGET_ELEMENT).item(0); } public Document XMLFileToDocument() throws Exception { @@ -31,15 +32,10 @@ public Document XMLFileToDocument() throws Exception { } public void removeElementOfSecurity() { - // get the element TARGET_ELEMENT (the hashed password element): - Element element = (Element) document.getElementsByTagName(TARGET_ELEMENT).item(0); - // remove the node: - if (element != null) // if element == null it means there is no hashed pass - { - element.getParentNode().removeChild(element); + if (this.securityElement != null) { // if element == null it means there is no hashed pass + this.securityElement.getParentNode().removeChild(this.securityElement); } - } public void writeToXMLFile(String newFilePath) throws Exception { @@ -47,10 +43,14 @@ public void writeToXMLFile(String newFilePath) throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource dom = new DOMSource(this.document); - // new file: StreamResult streamResult = new StreamResult(new File(newFilePath)); - // transform the XML source to a file: + // transform the XML source to file: transformer.transform(dom, streamResult); } + + public boolean isExistSecurityElement() { + // if the element was returned as null during instantiation, it means it doesn't exist + return (this.securityElement != null); + } }