Skip to content

Commit

Permalink
isExistSecurityElement() check
Browse files Browse the repository at this point in the history
  • Loading branch information
nedlir committed Feb 26, 2022
1 parent 3a1be13 commit 20a3ab6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
25 changes: 13 additions & 12 deletions FileManipulator.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
Expand Down Expand Up @@ -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();
}
}

}
}
24 changes: 12 additions & 12 deletions XMLParser.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -31,26 +32,25 @@ 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 {
// create transformer from document to xml file:
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);
}
}

0 comments on commit 20a3ab6

Please sign in to comment.