isExistSecurityElement() check

This commit is contained in:
nedlir 2022-02-26 22:22:07 +02:00
parent 3a1be13443
commit 20a3ab6d73
2 changed files with 25 additions and 24 deletions

View File

@ -1,3 +1,4 @@
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
@ -37,17 +38,17 @@ public class FileManipulator {
} }
public void insertFile(String targetFile) throws Exception { public void insertFile(String targetFile) throws Exception {
Path myFilePath = Paths.get(targetFile); Path myFilePath = Paths.get(targetFile);
Path zipFilePath = Paths.get(this.fileName); Path zipFilePath = Paths.get(this.fileName);
try( FileSystem fs = FileSystems.newFileSystem(zipFilePath, null) ){ try (FileSystem fs = FileSystems.newFileSystem(zipFilePath, null)) {
Path fileInsideZipPath = fs.getPath("/ppt/" + targetFile); Path fileInsideZipPath = fs.getPath("/ppt/" + targetFile);
Files.delete(fileInsideZipPath); Files.delete(fileInsideZipPath);
Files.copy(myFilePath, fileInsideZipPath); Files.copy(myFilePath, fileInsideZipPath);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

View File

@ -1,27 +1,28 @@
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
public class XMLParser { public class XMLParser {
private final String TARGET_ELEMENT = "p:modifyVerifier"; // hashed password element private final String TARGET_ELEMENT = "p:modifyVerifier"; // hashed password element
private String filePath; private String filePath;
private Element securityElement;
private Document document; private Document document;
public XMLParser(String filePath) throws Exception { public XMLParser(String filePath) throws Exception {
this.filePath = filePath; this.filePath = filePath;
this.document = XMLFileToDocument(); 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 { public Document XMLFileToDocument() throws Exception {
@ -31,15 +32,10 @@ public class XMLParser {
} }
public void removeElementOfSecurity() { public void removeElementOfSecurity() {
// get the element TARGET_ELEMENT (the hashed password element):
Element element = (Element) document.getElementsByTagName(TARGET_ELEMENT).item(0);
// remove the node: // remove the node:
if (element != null) // if element == null it means there is no hashed pass if (this.securityElement != null) { // if element == null it means there is no hashed pass
{ this.securityElement.getParentNode().removeChild(this.securityElement);
element.getParentNode().removeChild(element);
} }
} }
public void writeToXMLFile(String newFilePath) throws Exception { public void writeToXMLFile(String newFilePath) throws Exception {
@ -47,10 +43,14 @@ public class XMLParser {
TransformerFactory transformerFactory = TransformerFactory.newInstance(); TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(); Transformer transformer = transformerFactory.newTransformer();
DOMSource dom = new DOMSource(this.document); DOMSource dom = new DOMSource(this.document);
// new file:
StreamResult streamResult = new StreamResult(new File(newFilePath)); StreamResult streamResult = new StreamResult(new File(newFilePath));
// transform the XML source to a file: // transform the XML source to file:
transformer.transform(dom, streamResult); 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);
}
} }