diff --git a/.idea/ClojureProjectResolveSettings.xml b/.idea/ClojureProjectResolveSettings.xml new file mode 100644 index 00000000..df470b16 --- /dev/null +++ b/.idea/ClojureProjectResolveSettings.xml @@ -0,0 +1,6 @@ + + + + IDE + + \ No newline at end of file diff --git a/.idea/CodeMetropolis-sz10.iml b/.idea/CodeMetropolis-sz10.iml new file mode 100644 index 00000000..d6ebd480 --- /dev/null +++ b/.idea/CodeMetropolis-sz10.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..919ce1f1 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..a55e7a17 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..aa345419 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..dd03e277 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000..6a0cbfe5 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1647969322929 + + + + \ No newline at end of file diff --git a/demo-sz10 b/demo-sz10 new file mode 100644 index 00000000..e69de29b diff --git a/sources/toolchain/ixmlfiltering/.project b/sources/toolchain/ixmlfiltering/.project new file mode 100644 index 00000000..1b482466 --- /dev/null +++ b/sources/toolchain/ixmlfiltering/.project @@ -0,0 +1,23 @@ + + + ixmlfiltering + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/sources/toolchain/ixmlfiltering/pom.xml b/sources/toolchain/ixmlfiltering/pom.xml new file mode 100644 index 00000000..8b85c21a --- /dev/null +++ b/sources/toolchain/ixmlfiltering/pom.xml @@ -0,0 +1,68 @@ + + 4.0.0 + + codemetropolis + toolchain + 1.4.0 + + + ixmlfiltering + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + codemetropolis.toolchain.ixmlfiltering.Main + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + + + + + + ${project.groupId} + commons + + + + args4j + args4j + 2.32 + + + org.apache.commons + commons-collections4 + 4.1 + + + + javax.xml.bind + jaxb-api + 2.1 + + + org.w3c + dom + 2.3.0-jaxb-1.0.6 + + + org.mockito + mockito-all + 1.10.19 + test + + + + \ No newline at end of file diff --git a/sources/toolchain/ixmlfiltering/src/main/java/codemetropolis/toolchain/ixmlfiltering/CommandLineOptions.java b/sources/toolchain/ixmlfiltering/src/main/java/codemetropolis/toolchain/ixmlfiltering/CommandLineOptions.java new file mode 100644 index 00000000..af5e36fc --- /dev/null +++ b/sources/toolchain/ixmlfiltering/src/main/java/codemetropolis/toolchain/ixmlfiltering/CommandLineOptions.java @@ -0,0 +1,47 @@ +package codemetropolis.toolchain.ixmlfiltering; + +import org.kohsuke.args4j.Option; + +/** + * The CommandLineOptions for the IXMLFiltering + * This class manage the command line arguments of the IXMLFilteringExecutor + * @author Adam Polnik LIYMCC h984893 + */ + +public class CommandLineOptions { + + @Option(name="-h", aliases = { "--help" }) + private boolean showHelp = false; + + @Option(name="-i", aliases = { "--input" }) + private String inputFile = null; + + @Option(name="-o", aliases = { "--output" }) + private String outputFile = ".\reducedXmlfile.xml"; + + @Option(name="-pn", aliases = { "--pnr" }) + private String propertyNameRegex = null; + + @Option(name="-pv", aliases = { "--pvr" }) + private String propertyValueRegex = null; + + public boolean showHelp() { + return showHelp; + } + + public String getInputFile() { + return inputFile; + } + + public String getOutputFile() { + return outputFile; + } + + public String getPropertyNameRegex() { + return propertyNameRegex; + } + public String getPropertyValueRegex() { + return propertyValueRegex; + } + +} diff --git a/sources/toolchain/ixmlfiltering/src/main/java/codemetropolis/toolchain/ixmlfiltering/IXMLFilteringExecutor.java b/sources/toolchain/ixmlfiltering/src/main/java/codemetropolis/toolchain/ixmlfiltering/IXMLFilteringExecutor.java new file mode 100644 index 00000000..626d2138 --- /dev/null +++ b/sources/toolchain/ixmlfiltering/src/main/java/codemetropolis/toolchain/ixmlfiltering/IXMLFilteringExecutor.java @@ -0,0 +1,113 @@ +package codemetropolis.toolchain.ixmlfiltering; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.xml.*; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import codemetropolis.toolchain.commons.cmxml.Attribute; +import codemetropolis.toolchain.commons.executor.AbstractExecutor; +import codemetropolis.toolchain.commons.executor.ExecutorArgs; + +import org.w3c.dom.*; + +/** + * The Executor for the IXMLFiltering + * This class processes an XML file and excludes certain items + * @author Narad Márton N4P26J h879805 + */ + +public class IXMLFilteringExecutor extends AbstractExecutor { + + /** + * First it unmarshalls the source .xml document, + * then calls the writeToFile function to create the new .xml document + * @param args contains the input file, output file and params from the -p switch + * @return true if sucessful, false is an error occurred + */ + @Override + public boolean execute(ExecutorArgs args){ + System.out.println("Execute started"); + IXMLFilteringExecutorArgs IXMLArgs = (IXMLFilteringExecutorArgs) args; + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + File inputFile = new File(IXMLArgs.getInputFile()); + boolean fileExists = inputFile.exists(); + if (!fileExists) try { + throw new FileNotFoundException(); + } catch (FileNotFoundException e) { + System.out.println("Input File doesn't exist!"); + return false; + } + + try { + + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + Document document = docBuilder.parse(inputFile); + Pattern propertyNameRegex=IXMLArgs.getPropertyNameRegex(); + Pattern propertyValueRegex=IXMLArgs.getPropertyValueRegex(); + Node parent= document.getFirstChild(); + System.out.println(parent); + Queue queue=new LinkedList(); + queue.add(parent); + while(!queue.isEmpty()) { + Node current=queue.poll(); + if(current.getNodeName().equals("buildable") && isRemoveable(current,propertyNameRegex,propertyValueRegex)){ + current.getParentNode().removeChild(current); + continue; + } + NodeList children=current.getChildNodes(); + for (int i = 0; i < children.getLength(); i++) { + queue.add(children.item(i)); + } + } + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + DOMSource domSource = new DOMSource(document); + StreamResult streamResult = new StreamResult(IXMLArgs.getOutputFile()); + transformer.transform(domSource, streamResult); + System.out.println("Done creating XML File"); + + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + /** + * The isRemoveable method for the IXMLFiltering + * This class processes an XML file and excludes certain items + * @author Polnik Ádám LYIMCC h984893 + */ + + private boolean isRemoveable(Node node,Pattern propertyNameRegex, Pattern propertyValueRegex) { + NamedNodeMap attributes = node.getAttributes(); + + for (int i = 0; i Root node founded"); + }catch (Exception e) { + e.printStackTrace(); + } + } + @Test()//File Not Found Exception + void FileReadFailTest() { + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + Document document = docBuilder.parse("./src/test/resources/no.xml"); + }catch (Exception e) { + assertEquals(FileNotFoundException.class,e.getClass()); + System.out.println("Test Passed> File Not Found Exception"); + } + } + @Test//File read unsuccessful and filtered IXML dies not created + void executorTestFail() { + CommandLineOptions options = new CommandLineOptions(); + IXMLFilteringExecutor filteringExecutor= new IXMLFilteringExecutor(); + filteringExecutor.execute( + new IXMLFilteringExecutorArgs( + "./src/test/resources/test1.xml", + "./src/test/resources/result.xml", + "", + "" + + )); + } + @Test()//File read successfully + void FileReadSuccessTest() { + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + Document document = docBuilder.parse("./src/test/resources/test.xml"); + assertTrue(true); + System.out.println("Test Passed> File Is Read Successfully"); + }catch (Exception e) { + e.printStackTrace(); + } + } + + @Test//File read successfully and filtered IXML created + void executorTestPass() { + CommandLineOptions options = new CommandLineOptions(); + IXMLFilteringExecutor filteringExecutor= new IXMLFilteringExecutor(); + filteringExecutor.execute( + new IXMLFilteringExecutorArgs( + "./src/test/resources/test.xml", + "./src/test/resources/result.xml", + "", + "" + )); + } + + + + + + +} diff --git a/sources/toolchain/ixmlfiltering/src/test/java/ixmlfiltering/MainTest.java b/sources/toolchain/ixmlfiltering/src/test/java/ixmlfiltering/MainTest.java new file mode 100644 index 00000000..e281c792 --- /dev/null +++ b/sources/toolchain/ixmlfiltering/src/test/java/ixmlfiltering/MainTest.java @@ -0,0 +1,80 @@ +package ixmlfiltering; + +import static org.mockito.Mockito.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.AfterAll; + +import java.io.*; + +import codemetropolis.toolchain.ixmlfiltering.Main; +import codemetropolis.toolchain.ixmlfiltering.CommandLineOptions; + +import org.kohsuke.args4j.CmdLineException; +import org.kohsuke.args4j.CmdLineParser; + +/** + * Test class for testing the Main class. + * + * @author Narad Márton N4P26J h879805 + */ + +public class MainTest { + + private String[] args = {}; + private Main instance = new Main(); + + private CommandLineOptions optionsMock = mock(CommandLineOptions.class); + private CmdLineParser parserMock = mock(CmdLineParser.class); + + private static final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private static final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private static final PrintStream originalOut = System.out; + private static final PrintStream originalErr = System.err; + + + @BeforeAll + public static void setup() { + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + } + + @Test + public void showHelpTest() throws CmdLineException { + try { + doNothing().when(parserMock).parseArgument(); + when(optionsMock.showHelp()).thenReturn(true); + when(optionsMock.getInputFile()).thenReturn("in"); + when(optionsMock.getOutputFile()).thenReturn("out"); + + instance.main(args); + } catch (Exception e) { + assertEquals(CmdLineException.class, e.getClass()); + } + + } + + @Test + public void missingInputTest() throws CmdLineException { + try { + doNothing().when(parserMock).parseArgument(); + when(optionsMock.showHelp()).thenReturn(false); + when(optionsMock.getInputFile()).thenReturn(null); + + instance.main(args); + } catch (Exception e) { + assertEquals(CmdLineException.class, e.getClass()); + } + + } + + @AfterAll + public static void restore() { + System.setOut(originalOut); + System.setErr(originalErr); + } + +} diff --git a/sources/toolchain/ixmlfiltering/src/test/resources/result.xml b/sources/toolchain/ixmlfiltering/src/test/resources/result.xml new file mode 100644 index 00000000..0ebc994f --- /dev/null +++ b/sources/toolchain/ixmlfiltering/src/test/resources/result.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/sources/toolchain/ixmlfiltering/src/test/resources/test.xml b/sources/toolchain/ixmlfiltering/src/test/resources/test.xml new file mode 100644 index 00000000..d199757d --- /dev/null +++ b/sources/toolchain/ixmlfiltering/src/test/resources/test.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sources/toolchain/pom.xml b/sources/toolchain/pom.xml index 0d9fbded..1de12eb8 100644 --- a/sources/toolchain/pom.xml +++ b/sources/toolchain/pom.xml @@ -20,6 +20,7 @@ placing rendering converter + ixmlfiltering @@ -45,6 +46,11 @@ converter ${project.version} + + ${project.groupId} + ixmlfiltering + ${project.version} + \ No newline at end of file