diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Agile Software Development/TestApp/src | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Master/Agile Software Development/TestApp/src')
26 files changed, 2017 insertions, 0 deletions
diff --git a/Master/Agile Software Development/TestApp/src/Parser/CssParser.java b/Master/Agile Software Development/TestApp/src/Parser/CssParser.java new file mode 100644 index 0000000..9b11a1e --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/Parser/CssParser.java @@ -0,0 +1,41 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package Parser; + +import testapp.data.ExternalCssItem; +import testapp.data.Item; +import java.util.logging.*; + +/** + * + * @author robb + */ +public class CssParser { + private Item rootNode; + private String srcUrl; + + public CssParser(Item rootNode) { + this.rootNode=rootNode; + } + + public ExternalCssItem parse(String url) { + // Todo: http client to recieve url and put contents into code + String code=new String(); + return parseCode(code); + } + + public ExternalCssItem parseCode(String code) { + ExternalCssItem externalCssItem=new ExternalCssItem(srcUrl,rootNode); + rootNode.add(externalCssItem); + + // check for @import and mark Suspicious + if(code.contains("@import")) { + externalCssItem.setSuspiciousCode(); + } + + return externalCssItem; + } +} diff --git a/Master/Agile Software Development/TestApp/src/Parser/HtmlParser.java b/Master/Agile Software Development/TestApp/src/Parser/HtmlParser.java new file mode 100644 index 0000000..96ad2b2 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/Parser/HtmlParser.java @@ -0,0 +1,56 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package Parser; + +import java.util.logging.Level; +import java.util.logging.Logger; +import org.htmlparser.util.NodeList; +import org.htmlparser.util.ParserException; +import testapp.data.Item; +import testapp.data.PageItem; +import java.util.logging.*; + +/** + * + * @author + */ +public class HtmlParser //extends NodeVisitor { +{ + + public HtmlParser() { + } + + public Item parse(String url) { + try { + PageItem rootNode = new PageItem(url,null); + + org.htmlparser.Parser parser = new org.htmlparser.Parser(rootNode.getUrl()); + NodeList nodeList = parser.parse(null); + + nodeList.visitAllNodesWith(new Visitor(rootNode)); + + return rootNode; + + } catch (ParserException ex) { + Logger.getLogger(HtmlParser.class.getName()).log(Level.SEVERE, null, ex); + return null; + } + } + + public Item parse(Item rootItem) { + try { + org.htmlparser.Parser parser = new org.htmlparser.Parser(rootItem.getUrl()); + NodeList nodeList = parser.parse(null); + + nodeList.visitAllNodesWith(new Visitor(rootItem)); + + return rootItem; + + } catch (ParserException ex) { + Logger.getLogger(HtmlParser.class.getName()).log(Level.SEVERE, null, ex); + return null; + } + } +} diff --git a/Master/Agile Software Development/TestApp/src/Parser/JavascriptParser.java b/Master/Agile Software Development/TestApp/src/Parser/JavascriptParser.java new file mode 100644 index 0000000..4cfe0ec --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/Parser/JavascriptParser.java @@ -0,0 +1,44 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package Parser; + +import testapp.data.ExternalJavascriptItem; +import testapp.data.Item; +import java.util.logging.*; + +public class JavascriptParser { + private Item rootNode; + + private static final Logger logger = Logger.getLogger(JavascriptParser.class.getName()); + + + public JavascriptParser(Item rootNode) { + this.rootNode=rootNode; + } + + public ExternalJavascriptItem parse(String url) { + // Todo: http client to recieve url and put contents into code + String code=new String(); + return parseCode(code, url); + } + + public ExternalJavascriptItem parseCode(String code, String url) { + // the only evil js code should be + //document.write('<script type="text/javascript" src="'+ jsFile + '"></scr' + 'ipt>'); + // => nearly impossible to parse, so we search only for "src" string + // if found, code is marked suspicious + // could be tricked by spliting the string! + // + + ExternalJavascriptItem externalJavascriptItem=new ExternalJavascriptItem(url,rootNode); + rootNode.add(externalJavascriptItem); + if(code.contains("src")) { + externalJavascriptItem.setSuspiciousCode(); + } + + return externalJavascriptItem; + } +} diff --git a/Master/Agile Software Development/TestApp/src/Parser/UrlAnalyzer.java b/Master/Agile Software Development/TestApp/src/Parser/UrlAnalyzer.java new file mode 100644 index 0000000..c543849 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/Parser/UrlAnalyzer.java @@ -0,0 +1,161 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package Parser; + +import java.util.HashMap; +import javax.swing.JOptionPane; + +/** + * + * @author sven + */ +public class UrlAnalyzer { + public static final String PROTOCOL_HTTP = "http"; + public static final String PROTOCOL_HTTPS = "https"; + public static final String PROTOCOL_FTP = "ftp"; + public static final String PROTOCOL_FILE = "file"; + public static final String NO_PROTOCOL = "NO_PROTOCOL"; + public static final String NO_HOST = "NO_HOST"; + public static final String NO_VALID_PATH = "NO_VALID_PATH"; + public static final String NO_VALID_FILE = "NO_VALID_FILE"; + public static final String PROTOCOL_SEPARATOR = "://"; + public static final String PATH_SEPARATOR = "/"; + public static final String PARAMS_INDICATOR = "?"; + public static final String ANCHOR = "#"; + public static final String PARAMS_SEPARATOR = "&"; + public static final String PARAMS_ASSIGN = "="; + public static final String[] VALID_PROTOCOLS = + {PROTOCOL_HTTP,PROTOCOL_HTTPS,PROTOCOL_FTP,PROTOCOL_FILE}; + public static final String NO_ANCHOR = "NO_ANCHOR"; + + public static HashMap<String, String> getParams(String url) { + HashMap<String,String> result = new HashMap<String,String>(); + + if(url!=null) { + String paramStr = url.substring(url.indexOf(PARAMS_INDICATOR)+1); + String[] params = paramStr.split(PARAMS_SEPARATOR); + for(String param : params) { + String[] paramKeyValue = param.split(PARAMS_ASSIGN); + if (paramKeyValue.length > 1) { + result.put(paramKeyValue[0], paramKeyValue[1]); + } else { + result.put(paramKeyValue[0],null); + } + } + } + return result; + } + + public static final String getProtocol(final String url) { + if(url==null) + return NO_PROTOCOL; + + for (String proto : VALID_PROTOCOLS) { + if (url.startsWith(proto)) { + return proto; + } + } + return new String(NO_PROTOCOL); + } + public static final String getHostname(final String url) { + if(url==null) + return NO_HOST; + + if (!url.contains(PROTOCOL_SEPARATOR)) { + return NO_HOST; + } + int hostStart = url.indexOf(PROTOCOL_SEPARATOR)+PROTOCOL_SEPARATOR.length(); + int hostEnd = url.indexOf(PATH_SEPARATOR, hostStart); + if ( hostEnd <= 1 ) { + return NO_HOST; + } + return url.substring(hostStart, hostEnd); + } + public static final String getFilename(final String url) { + if(url==null) + return ""; + + String rest = url; + if (url.contains(PROTOCOL_SEPARATOR)) { + int protoEnd = url.indexOf(PROTOCOL_SEPARATOR)+PROTOCOL_SEPARATOR.length(); + rest = url.substring(protoEnd); + if ( (rest.indexOf(PATH_SEPARATOR)) == -1) { + return NO_VALID_FILE; + } + rest = url.substring(protoEnd); + } + int fileStart = rest.lastIndexOf(PATH_SEPARATOR); + String res = (fileStart != -1) ? rest.substring(fileStart+1) : rest; + if (hasParams(res)) { + res = res.substring(0, res.indexOf(PARAMS_INDICATOR)); + } + if (hasAnchor(res)) { + res = res.substring(0, res.indexOf(ANCHOR)); + } + return res; + } + public static final String getPath(String url) { + if(url==null) + return NO_VALID_PATH; + + int pathStart = 0; + int pathEnd = url.lastIndexOf(PATH_SEPARATOR); + + if (pathEnd < 1) { + // item at root level + // e. g. foobar.html oder cool.gifv + return PATH_SEPARATOR; + } + if (url.contains(PROTOCOL_SEPARATOR)) { + // we have a full url + // e.g. http://foo.bar.com/dir/subdir/index.html + pathStart = url.indexOf(PATH_SEPARATOR,url.indexOf(PROTOCOL_SEPARATOR)+PROTOCOL_SEPARATOR.length()); + if (pathStart < 0) { + return NO_VALID_PATH; + } + } else { + // we have a relative path on site + // e. g.: img/coolimg.jpg + // or res/img/anotherimg.gif + pathStart = 0; + pathEnd = url.lastIndexOf(PATH_SEPARATOR); + } + return url.substring(pathStart,pathEnd)+PATH_SEPARATOR; + } + public static final String getAnchor(String url) { + if(url==null) + return NO_ANCHOR; + + return (hasAnchor(url)) ? url.substring(url.indexOf(ANCHOR)+1) : null; + } + public static final boolean hasParams(String url) { + if(url==null) + return false; + + return url.contains(PARAMS_INDICATOR); + } + public static final boolean hasAnchor(String url) { + if(url==null) + return false; + + return url.contains(ANCHOR); + } + public static final String stripProtocol(String url) { + return url.substring(url.indexOf(PROTOCOL_SEPARATOR)+PROTOCOL_SEPARATOR.length()); + } + + public static final boolean validateURL(String url) + { + if( url.contains(PROTOCOL_HTTP) || url.contains(PROTOCOL_HTTPS) || url.contains(PROTOCOL_FTP) || url.contains(PROTOCOL_FILE) ) + if(url.contains(PROTOCOL_SEPARATOR)) + if(url.contains(".")) + return true; + + JOptionPane.showMessageDialog(null, "Falsche Url. Bitte geben Sie eine korrekte Url ein!"); + + return false; + } +} diff --git a/Master/Agile Software Development/TestApp/src/Parser/Visitor.java b/Master/Agile Software Development/TestApp/src/Parser/Visitor.java new file mode 100644 index 0000000..e5d953c --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/Parser/Visitor.java @@ -0,0 +1,96 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package Parser; + +import org.htmlparser.Tag; +import org.htmlparser.visitors.NodeVisitor; +import testapp.data.FrameItem; +import testapp.data.IframeItem; +import testapp.data.ImageItem; +import testapp.data.Item; +import testapp.data.LinkItem; +import testapp.data.ScriptItem; +import testapp.data.StyleItem; +import java.util.logging.*; + +/** + * + * @author alex + */ +class Visitor extends NodeVisitor { + + private Item currentItem; + + public Visitor(Item initialItem) { + currentItem = initialItem; + } + + /// This methode will be called for every Tag found in the document + @Override + public void visitTag(Tag currentTag) { + + String tagName = currentTag.getTagName(); + + + /// Search for a link and create a link item + String hrefAttribute = currentTag.getAttribute("HREF"); + if (hrefAttribute != null && hrefAttribute.length() > 0) { + if (tagName.equals("A")) { + LinkItem li = new LinkItem(hrefAttribute,currentItem); + currentItem.add(li); + } + } + + /// Search for a source and create a specific item depending on the source type + String srcAttribute = currentTag.getAttribute("SRC"); + if (srcAttribute != null && srcAttribute.length() > 0) { + if (tagName.equals("IFRAME")) { + IframeItem iframeItem; + iframeItem = new IframeItem(srcAttribute,currentItem); + + //currentItem.add(iframeItem); + startNewHtmlParser(iframeItem); + } else if (tagName.equals("IMG")) { + ImageItem ii = new ImageItem(srcAttribute,currentItem); + currentItem.add(ii); + } else if (tagName.equals("FRAME")) { + FrameItem frameItem; + frameItem = new FrameItem(srcAttribute,currentItem); + currentItem.add(frameItem); + startNewHtmlParser(frameItem); + } else if (tagName.equals("SCRIPT")) { + JavascriptParser javascriptParser = new JavascriptParser(currentItem); + currentItem.add(javascriptParser.parse(srcAttribute)); + } else if (tagName.equals("STYLE")) { + CssParser cssParser = new CssParser(currentItem); + currentItem.add(cssParser.parse(srcAttribute)); + } + } else { + if (tagName.equals("STYLE")) { + StyleItem styleItem = new StyleItem("",currentItem); + currentItem.add(styleItem); + CssParser cssParser = new CssParser(styleItem); + cssParser.parseCode(currentTag.getText()); + } + + if (tagName.equals("SCRIPT")) { + ScriptItem scriptItem = new ScriptItem("",currentItem); + currentItem.add(scriptItem); + JavascriptParser javascriptParser = new JavascriptParser(scriptItem); + javascriptParser.parseCode(currentTag.getText(), this.currentItem.getUrl()); + } + } + + + } + + /// Start a new html parser for nested html pages + private void startNewHtmlParser(Item currentItem) { + ///restict the search dept of the html page to 10s external pages + HtmlParser htmlParser = new HtmlParser(); + currentItem.urlToValidUrl(false); + htmlParser.parse(currentItem); + } +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/App.java b/Master/Agile Software Development/TestApp/src/testapp/App.java new file mode 100644 index 0000000..d0e1a55 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/App.java @@ -0,0 +1,60 @@ +/* + * Main + */ + +package testapp; + +import java.io.IOException; +import javax.swing.JFrame; +import javax.swing.UIManager; +import java.util.logging.*; + +/** + * + * @author eisenhauer + */ +public class App { + + private static App theInstance = null; + private JFrame mainFrame = null; + private String appPath = ""; + private App() { + try { + this.appPath = Locate.getClassLocation(this.getClass()).getParent(); + // to run from Netbeans + if (appPath.contains("build")) { + appPath = appPath.replace("build", "dist"); + } + } catch (IOException ioe) { + ioe.printStackTrace(); + } + } + + public static App getInstance() { + if (theInstance == null) { + theInstance = new App(); + } + return theInstance; + } + private void startApplication() { + // Setup Look and Feel + try { + //UIManager.setLookAndFeel(new com.jgoodies.looks.windows.WindowsLookAndFeel()); + } catch (Exception e) { + e.printStackTrace(); + } + mainFrame = new AppFrame("Webspinne"); + mainFrame.pack(); + mainFrame.setVisible(true); + } + public String getAppPath() { + return appPath; + } + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + App.getInstance().startApplication(); + } + +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/AppFrame.java b/Master/Agile Software Development/TestApp/src/testapp/AppFrame.java new file mode 100644 index 0000000..ab025c4 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/AppFrame.java @@ -0,0 +1,172 @@ +/* + * GUI + */ +package testapp; + +import com.jgoodies.forms.layout.CellConstraints; +import com.jgoodies.forms.layout.FormLayout; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Container; +import java.awt.Dimension; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.File; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextField; +import java.util.logging.*; +import javax.swing.JProgressBar; + +/** + * + * @author eisenhauer + */ +public class AppFrame extends JFrame { + + private static final Logger logger = Logger.getLogger(AppFrame.class.getName()); + + private JPanel taskPanel; + private JTextField urlTxf; + private JButton loadBtn; + private ImageComponent imgComp; + private JScrollPane imgScroll; + private static AppFrame theInstance = null; + private JPanel progressPanel; + + public AppFrame(String title) { + super(title); + theInstance = this; + initUI(); + + + } + + public static AppFrame getInstance() { + return theInstance; + } + + private void initUI() { + this.addWindowListener(new WindowAdapter() { + + @Override + public void windowClosing(WindowEvent e) { + if (imgComp != null) { + if (imgComp.getFile() != null) { + // TODO + //imgComp.getFile().delete(); + } + } + System.exit(0); + } + }); + Container cont = this.getContentPane(); + ButtonListener bl = new ButtonListener(); + CellConstraints cc = new CellConstraints(); + setLayout(new BorderLayout()); + taskPanel = new JPanel(); + String colSpec = "5dlu,p,5dlu,200dlu,5dlu,p,5dlu"; + String rowSpec = "5dlu,p,5dlu,p,5dlu"; + taskPanel.setLayout(new FormLayout(colSpec, rowSpec)); + int row = 2; + JLabel urlLabel = new JLabel("URL:"); + taskPanel.add(urlLabel, cc.xy(2, row)); + urlTxf = new JTextField(""); + taskPanel.add(urlTxf, cc.xy(4, row)); + loadBtn = new JButton("Analyze"); + loadBtn.setActionCommand("LOADURL"); + loadBtn.addActionListener(bl); + urlTxf.addKeyListener(bl); + taskPanel.add(loadBtn, cc.xy(6, row)); + imgComp = new ImageComponent(); + imgScroll = new JScrollPane(imgComp); + imgScroll.setPreferredSize(new Dimension(200, 200)); + + JProgressBar progressBar = new JProgressBar(); + progressBar.setIndeterminate(true); + progressPanel = new JPanel(); + progressPanel.setVisible(false); + progressBar.setPreferredSize(new Dimension (500,25)); + + imgScroll.setForeground(Color.white); + + progressBar.setBackground(Color.black); + progressBar.setForeground(Color.red); + + + progressPanel.add(progressBar); + + + cont.add(progressPanel, BorderLayout.SOUTH); + cont.add(taskPanel, BorderLayout.NORTH); + cont.add(imgScroll, BorderLayout.CENTER); + } + + public void endProgressBar () + { + progressPanel.setVisible(false); + } + + private void onLoad() { + String url = urlTxf.getText(); + + if (!url.startsWith("http://")) + url = "http://" + url; + + ItemGenerator ig = new ItemGenerator(url, progressPanel); + + progressPanel.setVisible(true); + + ig.execute(); + } + + public void setImage(File img) { + if (!img.canRead()) { + logger.log(Level.SEVERE,"Cannot read File"); + return; + } + if (imgComp.getFile() != null) { + //TODO + //imgComp.getFile().delete(); + } + imgComp.setImage(img); + imgScroll.revalidate(); + progressPanel.setVisible(false); + } + + public File getImageFile() { + return imgComp.getFile(); + } + + public class ButtonListener implements ActionListener, KeyListener { + + public void actionPerformed(ActionEvent e) { + String cmd = e.getActionCommand(); + if (cmd.equals("LOADURL")) { + onLoad(); + } + } + + public void keyTyped(KeyEvent e) { + } + + public void keyPressed(KeyEvent e) { + int key = e.getKeyCode(); + if (key == KeyEvent.VK_ENTER) { + onLoad(); + } + } + + public void keyReleased(KeyEvent e) { + } + } + + +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/DOTGenerator.java b/Master/Agile Software Development/TestApp/src/testapp/DOTGenerator.java new file mode 100644 index 0000000..679c339 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/DOTGenerator.java @@ -0,0 +1,54 @@ +/* + * Class to write the .dot file + */ + +package testapp; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import javax.swing.SwingWorker; +import testapp.data.Item; +import java.util.logging.*; +/** + * + * @author sven + */ +public class DOTGenerator extends SwingWorker<File,Object>{ + private Item startItem; + + private static final Logger logger = Logger.getLogger(DOTGenerator.class.getName()); + + public DOTGenerator(Item startItem) { + this.startItem = startItem; + } + + @Override + protected File doInBackground() throws Exception { + File tmpDotFile = null; + try { + tmpDotFile = File.createTempFile("tmp", ".gv"); + FileWriter fw = new FileWriter(tmpDotFile); + logger.log(Level.INFO, "Path: "+tmpDotFile.getAbsolutePath()); + fw.write(startItem.toDOT()); + fw.close(); + } catch (IOException ioe) { + ioe.printStackTrace(); + } + return tmpDotFile; + } + + @Override + protected void done() { + try { + GraphGenerator gen = new GraphGenerator(get()); + gen.execute(); + } catch (InterruptedException ie) { + ie.printStackTrace(); + } catch (ExecutionException ee) { + ee.printStackTrace(); + } + } + +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/GraphGenerator.java b/Master/Agile Software Development/TestApp/src/testapp/GraphGenerator.java new file mode 100644 index 0000000..06645d2 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/GraphGenerator.java @@ -0,0 +1,81 @@ +/* + * Class to generate the graphic out of the .dot file + */ +package testapp; + +import java.io.File; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import javax.swing.SwingWorker; +import java.util.logging.*; +/** + * + * @author eisenhauer + */ +public class GraphGenerator extends SwingWorker<File,Object> { + int retVal=-1; + Process proc; + File dotFile = null; + public GraphGenerator(File dotFile) { + super(); + this.dotFile = dotFile; + } + + @Override + protected File doInBackground() throws Exception { + return getImageFileForGraph(); + } + + @Override + protected void done() { + try { + if (retVal == 0) { + AppFrame af = AppFrame.getInstance(); + if (af != null) { + af.setImage(get()); + //TODO + //dotFile.delete(); + } + } + return; + } catch (InterruptedException ie) { + ie.printStackTrace(); + } catch (ExecutionException ee) { + ee.printStackTrace(); + } + } + + //Convert the .dot file to a .png grafic + public File getImageFileForGraph(File dotFile) { + File tmpFile = null; + if (!dotFile.canRead()) { + return null; + } + try { + tmpFile = File.createTempFile("tmp", ".png"); + File dotPath = new File(App.getInstance().getAppPath() + "/lib/graphviz/bin/"); + File dotExe = new File(dotPath.toString()+"/dot.exe"); + String dotArgType = "-Tpng"; + String dotArgOutfile = "-o"+tmpFile.toString(); + String dotArgInfile = dotFile.toString(); + String graphvizCmd = dotExe.toString(); + String[] cmd = new String[] + {graphvizCmd,dotArgType,dotArgOutfile,dotArgInfile}; + ProcessBuilder procBuilder = new ProcessBuilder(cmd); + procBuilder.redirectErrorStream(true); + proc = procBuilder.start(); + try { + retVal = proc.waitFor(); + } catch (InterruptedException ie) { + ie.printStackTrace(); + } + } catch (IOException ioe) { + ioe.printStackTrace(); + } + return tmpFile; + } + public File getImageFileForGraph() { + //File dotFile = new File(System.getProperty("user.dir")+"/dist/resource/test.dot"); + return getImageFileForGraph(dotFile); + } +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/ImageComponent.java b/Master/Agile Software Development/TestApp/src/testapp/ImageComponent.java new file mode 100644 index 0000000..359bf10 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/ImageComponent.java @@ -0,0 +1,46 @@ +/* + * Class for image drawing. + */ +package testapp; + +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import javax.imageio.ImageIO; +import javax.swing.JComponent; +import java.util.logging.*; + +/** + * + * @author eisenhauer + */ +public class ImageComponent extends JComponent { + + private BufferedImage image; + private File file; + + public void setImage(File file) { + this.file = file; + try { + if ((image = ImageIO.read(this.file)) != null) { + setPreferredSize(new Dimension(image.getWidth(), image.getHeight())); + repaint(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + protected void paintComponent(Graphics g) { + if (image != null) { + g.drawImage(image, 0, 0, this); + } + } + + public File getFile() { + return file; + } +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/ItemGenerator.java b/Master/Agile Software Development/TestApp/src/testapp/ItemGenerator.java new file mode 100644 index 0000000..9a8855f --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/ItemGenerator.java @@ -0,0 +1,92 @@ +/* + * Class to use the html parser and generate items. + */ + +package testapp; + +import Parser.HtmlParser; +import Parser.UrlAnalyzer; +import java.util.concurrent.ExecutionException; +import javax.swing.SwingWorker; +import testapp.data.IframeItem; +import testapp.data.ImageItem; +import testapp.data.Item; +import testapp.data.ItemClassification; +import testapp.data.PageItem; +import java.util.logging.*; +import javax.swing.JPanel; +/** + * + * @author sven + */ +public class ItemGenerator extends SwingWorker<Item,Object> { + private static final Logger logger = Logger.getLogger(ItemGenerator.class.getName()); + + private String url; + private JPanel progressBar; + + public ItemGenerator(String url, JPanel ProgressBar) { + this.url = url; + this.progressBar = ProgressBar; + } + + @Override + protected Item doInBackground() throws Exception { + // Call Parser for URL + PageItem pi =null; + if(UrlAnalyzer.validateURL(url)) + { + progressBar.setVisible(true); + String rest = UrlAnalyzer.stripProtocol(url); + if (!rest.contains(UrlAnalyzer.PATH_SEPARATOR)) { + url += "/"; + } + pi = new PageItem(url,null); + HtmlParser htmlParser=new HtmlParser(); + htmlParser.parse(pi); + + /* + PageItem pi = new PageItem(url); + pi.setClassification(ItemClassification.SUSPICIOUS); + pi.add(new ImageItem(url+"image1").setClassification(ItemClassification.HARMLESS)); + pi.add(new ImageItem(url+"image2").setClassification(ItemClassification.CRITICAL)); + pi.add(new IframeItem(url+"if1") + .add(new ImageItem(url+"if1img1").setClassification(ItemClassification.SUSPICIOUS)) + .add(new ImageItem(url+"if1img2").setClassification(ItemClassification.SUSPICIOUS)) + ); + pi.add(new IframeItem("NEWIFRAME").setClassification(ItemClassification.UNKNOWN)); + */ + // + // start analyse here!!! + // + //pi.analyse(); + } + else + { + //throw new Exception("Wrong URL format."); + } + return pi; + } + + @Override + protected void done() { + try { + Item rootItem = get(); + rootItem.urlToValidUrl(true); + ItemClassification ic = rootItem.analyse(); + DOTGenerator dotGenerator = new DOTGenerator(rootItem); + dotGenerator.execute(); + } catch (InterruptedException ie) { + ie.printStackTrace(); + } catch (ExecutionException ee) { + ee.printStackTrace(); + } catch (Exception ex) + { + logger.log(Level.SEVERE, ex.toString()); + } + finally + { + progressBar.setVisible(false); + } + } +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/Locate.java b/Master/Agile Software Development/TestApp/src/testapp/Locate.java new file mode 100644 index 0000000..49f96a9 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/Locate.java @@ -0,0 +1,90 @@ +/* + * + */ + +package testapp; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.logging.*; + +/**@author McDowell*/ +public class Locate { + + /** + * Returns the URL of a given class. + * @param c a non-null class + * @return the URL for that class + */ + public static URL getUrlOfClass(Class c) { + if(c==null) { + throw new NullPointerException(); + } + String className = c.getName(); + String resourceName = className.replace('.', '/') + ".class"; + ClassLoader classLoader = c.getClassLoader(); + if(classLoader==null) { + classLoader = ClassLoader.getSystemClassLoader(); + } + URL url = classLoader.getResource(resourceName); + return url; + } + + /** + * Finds the location of a given class file on the file system. + * Throws an IOException if the class cannot be found. + * <br> + * If the class is in an archive (JAR, ZIP), then the returned object + * will point to the archive file. + * <br> + * If the class is in a directory, the base directory will be returned + * with the package directory removed. + * <br> + * The <code>File.isDirectory()</code> method can be used to + * determine which is the case. + * <br> + * @param c a given class + * @return a File object + * @throws IOException + */ + public static File getClassLocation(Class c) throws IOException, FileNotFoundException { + if(c==null) { + throw new NullPointerException(); + } + + String className = c.getName(); + String resourceName = className.replace('.', '/') + ".class"; + ClassLoader classLoader = c.getClassLoader(); + if(classLoader==null) { + classLoader = ClassLoader.getSystemClassLoader(); + } + URL url = classLoader.getResource(resourceName); + + String szUrl = url.toString(); + if(szUrl.startsWith("jar:file:")) { + try { + szUrl = szUrl.substring("jar:".length(), szUrl.lastIndexOf("!")); + URI uri = new URI(szUrl); + return new File(uri); + } catch(URISyntaxException e) { + throw new IOException(e.toString()); + } + } else if(szUrl.startsWith("file:")) { + try { + szUrl = szUrl.substring(0, szUrl.length() - resourceName.length()); + URI uri = new URI(szUrl); + return new File(uri); + } catch(URISyntaxException e) { + throw new IOException(e.toString()); + } + } + + throw new FileNotFoundException(szUrl); + } + +} + diff --git a/Master/Agile Software Development/TestApp/src/testapp/data/ExternalCssItem.java b/Master/Agile Software Development/TestApp/src/testapp/data/ExternalCssItem.java new file mode 100644 index 0000000..9f8025a --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/data/ExternalCssItem.java @@ -0,0 +1,58 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package testapp.data; + +import java.util.HashMap; +import java.util.logging.*; + +/** + * + * @author sven + */ +public class ExternalCssItem extends Item { + private Boolean suspiciousCode=false; + + @Override + public ItemClassification analyse() { + return doAnalyse(); + } + + public ExternalCssItem(String url, Item parent) { + super(url, parent); + itemType = ItemType.EXTERNALCSS; + } + +// public ExternalCssItem(String url) { +// super(url); +// itemType = ItemType.EXTERNALCSS; +// } + + public void setSuspiciousCode() { + suspiciousCode=true; + } + + public Boolean getSuspiciousCode() { + return suspiciousCode; + } + + @Override + protected HashMap<String, String> getAttributes() { + if (attributes == null) { + attributes = new HashMap<String,String>(); + } + attributes.put("shape", "septagon"); + attributes.put("style", "filled"); + attributes.put("fontname", "Verdana"); + attributes.put("fontsize", "10"); + return attributes; + } + + @Override + protected String getGraphNodetext() { + return "Ext. CSS: "+this.url; + } + +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/data/ExternalJavascriptItem.java b/Master/Agile Software Development/TestApp/src/testapp/data/ExternalJavascriptItem.java new file mode 100644 index 0000000..9c23f88 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/data/ExternalJavascriptItem.java @@ -0,0 +1,56 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package testapp.data; + +import java.util.HashMap; +import java.util.logging.*; +/** + * + * @author sven + */ +public class ExternalJavascriptItem extends Item { + + private Boolean suspiciousCode = false; + + @Override + public ItemClassification analyse() { + return doAnalyse(); + } + + public ExternalJavascriptItem(String url, Item parent) { + super(url, parent); + itemType = ItemType.EXTERNALJAVASCRIPT; + } + +// public ExternalJavascriptItem(String url) { +// super(url); +// itemType = ItemType.EXTERNALJAVASCRIPT; +// } + + public void setSuspiciousCode() { + suspiciousCode = true; + } + + public Boolean getSuspiciousCode() { + return suspiciousCode; + } + + @Override + protected HashMap<String, String> getAttributes() { + if (attributes == null) { + attributes = new HashMap<String, String>(); + } + attributes.put("shape", "ellipse"); + attributes.put("style", "filled"); + attributes.put("fontname", "Verdana"); + attributes.put("fontsize", "10"); + return attributes; + } + + @Override + protected String getGraphNodetext() { + return "Ext. SCRIPT: " + this.url; + } +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/data/FrameItem.java b/Master/Agile Software Development/TestApp/src/testapp/data/FrameItem.java new file mode 100644 index 0000000..84c982d --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/data/FrameItem.java @@ -0,0 +1,51 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package testapp.data; + +import java.util.HashMap; +import java.util.logging.*; +/** + * + * @author sven + */ +public class FrameItem extends Item { + + @Override + public ItemClassification analyse() { + return doAnalyse(); + } + + public FrameItem(String url, Item parent) { + super(url, parent); + itemType = ItemType.FRAME; + } + +// public FrameItem(String url) { +// super(url); +// itemType = ItemType.FRAME; +// } + + @Override + + protected HashMap<String, String> getAttributes() { + if (attributes == null) { + attributes = new HashMap<String,String>(); + } + attributes.put("shape", "doubleoctagon"); + attributes.put("style", "filled"); + attributes.put("fontname", "Verdana"); + attributes.put("fontsize", "10"); + return attributes; + } + + @Override + protected String getGraphNodetext() { + if (this.filename.compareTo("")!=0) + return this.filename; + else + return this.getUrl(); + } +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/data/GraphConstants.java b/Master/Agile Software Development/TestApp/src/testapp/data/GraphConstants.java new file mode 100644 index 0000000..6edf288 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/data/GraphConstants.java @@ -0,0 +1,44 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package testapp.data; +/** + * + * @author sven + */ +public class GraphConstants { + public static String getDOTStart() { +// return "digraph G {\r\n\tgraph [rankdir = \"LR\"];\r\n"; + return "digraph G {\r\n\tgraph[rankdir = \"LR\"];\r\n"; + } + public static String getDOTEnd() { + return "\r\n}"; + } + public final static String getDotLegend() { + StringBuilder res = new StringBuilder("\t/* Legend */\r\n"); + + res.append("\tsubgraph cluster_1 { \r\n"); + res.append("\t\tfontname=Verdana;\r\n"); + res.append("\t\tlabel = \"Legende\";\r\n"); + res.append("\t\tcolor=lightgrey;\r\n"); + res.append("\t\tstyle=filled;\r\n"); + res.append("\t\tHOME [style=filled,fontname=verdana,fontsize=10,color=white,shape=house,rank=0];\r\n"); + res.append("\t\tLink [style=filled,fontname=verdana,fontsize=10,color=white,shape=box,rank=0];\r\n"); + res.append("\t\tImage [style=filled,fontname=verdana,fontsize=10,color=white,shape=octagon,rank=0];\r\n"); + res.append("\t\tScript [style=filled,fontname=verdana,fontsize=10,color=white,shape=ellipse,rank=0];\r\n"); + res.append("\t\tCSS [style=filled,fontname=verdana,fontsize=10,color=white,shape=septagon,rank=0];\r\n"); + res.append("\t\tIframe [style=filled,fontname=verdana,fontsize=10,color=white,shape=doubleoctagon,rank=0];\r\n"); + + res.append("\t\tSuspicious [style=filled,fontname=verdana,fontsize=10,color=orange,shape=ellipse,rank=1];\r\n"); + res.append("\t\tCritical [style=filled,fontname=verdana,fontsize=10,color=red2,shape=ellipse,rank=1];\r\n"); + res.append("\t\tUnknown [style=filled,fontname=verdana,fontsize=10,color=slateblue1,shape=ellipse,rank=1];\r\n"); + res.append("\t}\r\n"); + + return res.toString(); + } + public static final String getEmptyMessage() { + return "\"Keine kritischen Elemente gefunden\" [style=filled,color=limegreen];\r\n"; + } +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/data/IframeItem.java b/Master/Agile Software Development/TestApp/src/testapp/data/IframeItem.java new file mode 100644 index 0000000..5a6537b --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/data/IframeItem.java @@ -0,0 +1,51 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package testapp.data; +import java.util.logging.*; +import java.util.HashMap; + +/** + * + * @author sven + */ +public class IframeItem extends Item { + + @Override + public ItemClassification analyse() { + return doAnalyse(); + } + + public IframeItem(String url, Item parent) { + super(url, parent); + itemType = ItemType.IFRAME; + attributes = getAttributes(); + } + +// public IframeItem(String url) { +// super(url); +// itemType = ItemType.IFRAME; +// attributes = getAttributes(); +// } + + @Override + protected HashMap<String, String> getAttributes() { + if (attributes == null) { + attributes = new HashMap<String,String>(); + } + attributes.put("shape", "doubleoctagon"); + attributes.put("style", "filled"); + attributes.put("fontname", "Verdana"); + attributes.put("fontsize", "10"); + return attributes; + } + @Override + protected String getGraphNodetext() { + if (this.filename.compareTo("")!=0) + return this.filename; + else + return this.getUrl(); + } +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/data/ImageItem.java b/Master/Agile Software Development/TestApp/src/testapp/data/ImageItem.java new file mode 100644 index 0000000..0014b81 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/data/ImageItem.java @@ -0,0 +1,102 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package testapp.data; + +import java.awt.Image; +import java.io.IOException; +import java.net.URL; +import java.util.HashMap; +import java.util.logging.*; +import javax.imageio.ImageIO; + + + +/** + * + * @author sven + */ +public class ImageItem extends Item{ + + private static final Logger logger = Logger.getLogger(ImageItem.class.getName()); + + public ImageItem(String url, Item parent) { + super(url, parent); + itemType = ItemType.IMAGE; + } + +// public ImageItem(String url) { +// super(url); +// itemType = ItemType.IMAGE; +// } + + @Override + public ItemClassification analyse() { + + // if (!url.startsWith("http://") && !url.startsWith("https://") && !url.startsWith("ftp://")) + // url="http://"+url; + doAnalyse(); + + try { + + // Bild auf Größe abfragen, wenn kleiner als 5x5 Pixel --> Critical + // Wenn größer und von fremden Server --> Suspicous + // Wenn lokal, dann ist auch die Größe egal --> Harmless + logger.log(Level.INFO, this.url.toString()); + URL imgUrl = new URL(this.url); + logger.log(Level.INFO, imgUrl.toString()); + Image image=null; + try { + image = ImageIO.read(imgUrl); + } catch(Exception exception) { + logger.log(Level.WARNING, "Couldn't verify image "+imgUrl); + setClassification(ItemClassification.SUSPICIOUS); + } + + //System.out.println("Höhe "+image.getHeight(null)+" Breite "+image.getWidth(null)); + if (image != null) { + if ( (image.getHeight(null) <= 5) && (image.getWidth(null) <= 5) && (itemPlace != ItemPlace.LOCAL) ) { + setClassification(ItemClassification.CRITICAL); + } + else if (itemPlace == ItemPlace.REMOTE) + { + setClassification(ItemClassification.SUSPICIOUS); + } + else if (itemPlace == ItemPlace.LOCAL) + { + setClassification(ItemClassification.HARMLESS); + } + } else { + setClassification(ItemClassification.UNKNOWN); + } + } catch (IOException ex) { + logger.log(Level.SEVERE, ex.toString()); + } + + logger.log(Level.INFO, "Place: " + this.itemPlace); + return itemClassification; + } + + @Override + protected HashMap<String, String> getAttributes() { + if (attributes == null) { + attributes = new HashMap<String,String>(); + } + attributes.put("shape", "octagon"); + attributes.put("style", "filled"); + attributes.put("fontname", "Verdana"); + attributes.put("fontsize", "10"); + return attributes; + } + + @Override + protected String getGraphNodetext() { + if (this.filename.compareTo("")!=0) + return this.filename; + else + return this.getUrl(); + } + +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/data/Item.java b/Master/Agile Software Development/TestApp/src/testapp/data/Item.java new file mode 100644 index 0000000..08f17a6 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/data/Item.java @@ -0,0 +1,413 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package testapp.data; + +import Parser.UrlAnalyzer; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.logging.*; + +/** + * + * @author sven + * Abstract which all Items implement + * This is the Component of the composite pattern + */ +public abstract class Item { + + private static final Logger logger = Logger.getLogger(ImageItem.class.getName()); + protected List<Item> childItems = new ArrayList<Item>(); + protected String url; + protected String protocol; + protected String hostname; + protected String path; + protected String filename; + protected String labelExtension; + protected HashMap<String, String> httpParams; + protected String urlAnchor; + protected Item parent; + protected ItemType itemType; + protected ItemClassification itemClassification; + protected HashMap<String, String> attributes = new HashMap(); + protected ItemPlace itemPlace; + +// public Item(String url) { +// this.url = url; +// this.itemClassification = ItemClassification.NEW; +// } + public Item(String url, Item parent) { + this.parent = parent; + this.url = url; + this.itemClassification = ItemClassification.NEW; + urlToValidUrl(false); + } + + /** + * method to let Item analyse itself + * and let all it's children analyse themselfs + * + * MUST call doAnalyse as return + * + */ + public abstract ItemClassification analyse(); + + public final ItemClassification doAnalyse() { + checkPlace(); + if (!itemType.equals(ItemType.LINK)) { + if (itemPlace == ItemPlace.REMOTE) { + setClassification(ItemClassification.CRITICAL); + } else if (itemPlace == ItemPlace.LOCAL) { + setClassification(ItemClassification.HARMLESS); + } + } + ItemClassification childClassification = ItemClassification.UNKNOWN; + ItemClassification ownClassification = itemClassification; + for (Item child : childItems) { + childClassification = child.analyse(); + if ((ownClassification.compareTo(childClassification) < 0)) { + ownClassification = childClassification; + } + } + + setClassification(ownClassification); + return ownClassification; + + } + + /** + * get DOT Graph representation of this Item and all + * of its children + * @return DOT String representation + * see DOT documentation for syntax + */ + public final String toDOT() { + StringBuilder sb = new StringBuilder(); + if (isRoot()) { + sb.append(GraphConstants.getDOTStart()); + if (!hasDrawable()) { + sb.append(GraphConstants.getEmptyMessage()); + } else { + sb.append(GraphConstants.getDotLegend()); + } + sb.append("\r\n\t/*Node definitions*/\r\n"); + sb.append(getAsDotNode()); + sb.append("\r\n\t/*Tree definition*/\r\n"); + } + + if (isLeaf()) { + sb.append("\"" + getGraphNodetext() + "\";\r\n"); + } else { + if (isDrawable()) { + sb.append(getChildrenAsDOT()); + } + } + if (isRoot()) { + sb.append(GraphConstants.getDOTEnd()); + } + return sb.toString(); + } + + /** + * is this Item a Leaf of the tree? + * @return true if it has no children, + * false if it has children + */ + public final boolean isLeaf() { + return (childItems.size() > 0) ? false : true; + } + + /** + * is this Item the root of the tree? + * @return true if it has no parent Item + * false if it has a parent Item + */ + protected final boolean isRoot() { + return (parent == null) ? true : false; + } + + /** + * Add a child Item to this Item + * @param child + * @return the Item + */ + public final Item add(Item child) { + child.setParent(this); + childItems.add(child); + return this; + } + + /** + * set the parent Item of this Item + * @param parent + */ + public final void setParent(Item parent) { + this.parent = parent; + } + + /** + * ste the URL of this Item + * @param url + */ + public final void setUrl(String url) { + this.url = url; + } + + /** + * get the URL of this Item + */ + public final String getUrl() { + return this.url; + } + + protected final String getChildrenAsDOT() { + StringBuilder sb = new StringBuilder(); + if (isDrawable()) { + for (Item child : childItems) { + if (child.isDrawable()) { + sb.append(isRoot() ? "\t" : ""); + sb.append("\"" + getGraphNodetext() + "\""); + sb.append(" -> "); + sb.append(child.toDOT()); + } + } + } + return sb.toString(); + } + + protected abstract String getGraphNodetext(); + + public final boolean isDrawable() { + if (itemClassification.compareTo(ItemClassification.HARMLESS) > 0) { + return true; + } + return false; + } + + /** + * + * @return String: DOT Node reprensetation plus addiditonal attributes + */ + protected final String getAsDotNode() { + if (!isDrawable()) { + return ""; + } + StringBuilder sb = new StringBuilder(); + sb.append("\t\""); + sb.append(getGraphNodetext()); + sb.append("\" ["); + int i = 0; + for (String keyStr : attributes.keySet()) { + if (i > 0) { + sb.append(","); + } + sb.append(keyStr + "=" + attributes.get(keyStr)); + i++; + } + + labelExtension = "<<table border=\"0\" cellborder=\"0\" cellpadding=\"3\" bgcolor=\"white\"><tr>test</tr></table>>"; + + if (this.labelExtension.compareTo("") != 0) { + // sb.append(",label="+this.labelExtension); + } + + logger.log(Level.INFO, labelExtension); + + sb.append("];\r\n"); + for (Item item : childItems) { + sb.append(item.getAsDotNode()); + } + return sb.toString(); + } + + protected abstract HashMap<String, String> getAttributes(); + + private boolean hasDrawable() { + boolean res = isDrawable(); + if (!res) { + for (Item child : childItems) { + res = child.hasDrawable(); + if (res) { + return res; + } + } + } + return res; + } + + private final void setAttribute(String key, String value) { + getAttributes().put(key, value); + } + + private final void setColor() { + switch (itemClassification) { + case CRITICAL: + setAttribute("color", "red2"); + break; + case HARMLESS: + setAttribute("color", "limegreen"); + break; + case SUSPICIOUS: + setAttribute("color", "orange"); + break; + case UNKNOWN: + setAttribute("color", "slateblue1"); + break; + case NEW: + setAttribute("color", "ivory3"); + break; + } + } + + /** + * check and validate the URL of this item and and its children + * CAUTION run after parsing completed, otherwise we can not be sure to + * have a valid parent item. This is important if we only have a relative url + */ + public final void urlToValidUrl(boolean recursive) { + /*if (!url.startsWith("http://") && !url.startsWith("https://") && !url.startsWith("ftp://")) + url="http://"+url; + + return url;*/ + if (this.url == null) { + this.url = this.parent.getFullUrl(); +// return; + } + if (isRoot()) { + if (!this.url.contains(UrlAnalyzer.PROTOCOL_SEPARATOR)) { + this.url = UrlAnalyzer.PROTOCOL_HTTP + UrlAnalyzer.PROTOCOL_SEPARATOR + this.url; + } + } + this.protocol = UrlAnalyzer.getProtocol(this.url); + if (this.protocol.equals(UrlAnalyzer.NO_PROTOCOL)) { + this.protocol = this.parent.getProtocol(); + } + this.hostname = UrlAnalyzer.getHostname(url); + if (this.hostname.equals(UrlAnalyzer.NO_HOST)) { + this.hostname = this.parent.hostname; + } + this.path = UrlAnalyzer.getPath(url); + if (this.path.equals(UrlAnalyzer.NO_VALID_PATH)) { + this.path = this.parent.path; + } else if (this.path != null && this.parent != null && !this.path.startsWith(String.valueOf(UrlAnalyzer.PATH_SEPARATOR))) { + this.path = this.parent.path + this.path; + } + this.filename = UrlAnalyzer.getFilename(this.url); + if (this.filename.equals(UrlAnalyzer.NO_VALID_FILE)) { + this.filename = this.parent.filename; + } + + + if (UrlAnalyzer.hasParams(this.url)) { + this.httpParams = UrlAnalyzer.getParams(this.url); + } + if (UrlAnalyzer.hasAnchor(this.url)) { + this.urlAnchor = UrlAnalyzer.getAnchor(this.url); + } + + this.url = getFullUrl(); + //System.out.println("Full Url: " + this.url); + if (recursive) { + for (Item child : childItems) { + child.urlToValidUrl(recursive); + } + } + } + + public final String getProtocol() { + if (this.protocol == null) { + urlToValidUrl(false); + this.protocol = this.parent.getProtocol(); + } + return this.protocol; + } + + public Item setClassification(ItemClassification it) { + itemClassification = it; + setColor(); + return this; + } + + protected Item getRoot() { + // Festellen, ob von eigener Seite + + Item parentIt = this.parent; + Item root = null; + + if (parentIt != null) { + while (parentIt.parent != null) { + parentIt = parentIt.parent; + } + root = parentIt; + } else { + return this; + } + + return root; + } + + /*protected String getHostname () + { + + try { + + String host = (new URL(url)).getHost(); + return host; + + } catch (MalformedURLException ex) { + Logger.getLogger(ImageItem.class.getName()).log(Level.SEVERE, null, ex); + return null; + } + + + }*/ + protected final void checkPlace() { + if (this.hostname != null) { + //System.out.println("Root: " + this.getRoot().getHostname() + " this: " + this.getHostname()); + //System.out.println("Root: " + this.getRoot().hostname + " this: " + this.hostname); + if (!this.hostname.equals(this.getRoot().hostname)) { + this.itemPlace = ItemPlace.REMOTE; + } else { + this.itemPlace = ItemPlace.LOCAL; + } + } else { + this.itemPlace = ItemPlace.LOCAL; + } + } + + public final String getFullUrl() { +// if (this.protocol == null) { +// urlToValidUrl(); +// } + String res = protocol + UrlAnalyzer.PROTOCOL_SEPARATOR + hostname + path + filename; + res += (urlAnchor != null) ? UrlAnalyzer.ANCHOR + urlAnchor : ""; + res += httpParamsToString(); + return res; + } + + public final String getFullpath() { + return protocol + UrlAnalyzer.PROTOCOL_SEPARATOR + hostname + path; + } + + private final String httpParamsToString() { + String res = ""; + if (this.httpParams != null) { + res += UrlAnalyzer.PARAMS_INDICATOR; + int n = 0; + for (String paramName : httpParams.keySet()) { + if (n > 0) { + res += UrlAnalyzer.PARAMS_SEPARATOR; + } + res += paramName; + String paramValue = httpParams.get(paramName); + if (paramValue != null) { + res += UrlAnalyzer.PARAMS_ASSIGN + paramValue; + } + n++; + } + } + return res; + } +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/data/ItemClassification.java b/Master/Agile Software Development/TestApp/src/testapp/data/ItemClassification.java new file mode 100644 index 0000000..217ac51 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/data/ItemClassification.java @@ -0,0 +1,14 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package testapp.data; + +/** + * + * @author sven + */ +public enum ItemClassification { + NEW,HARMLESS,UNKNOWN,SUSPICIOUS,CRITICAL; +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/data/ItemPlace.java b/Master/Agile Software Development/TestApp/src/testapp/data/ItemPlace.java new file mode 100644 index 0000000..78b762b --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/data/ItemPlace.java @@ -0,0 +1,14 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package testapp.data; + +/** + * + * @author widbstudi + */ +public enum ItemPlace { + UNKNOWN,LOCAL,REMOTE +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/data/ItemType.java b/Master/Agile Software Development/TestApp/src/testapp/data/ItemType.java new file mode 100644 index 0000000..d6901ad --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/data/ItemType.java @@ -0,0 +1,14 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package testapp.data; + +/** + * + * @author sven + */ +public enum ItemType { + IMAGE,IFRAME,FRAME,PAGE,LINK,STYLE,SCRIPT,EXTERNALJAVASCRIPT,EXTERNALCSS +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/data/LinkItem.java b/Master/Agile Software Development/TestApp/src/testapp/data/LinkItem.java new file mode 100644 index 0000000..600163d --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/data/LinkItem.java @@ -0,0 +1,50 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package testapp.data; + +import java.util.HashMap; +import java.util.logging.*; + +/** + * + * @author sven + */ +public class LinkItem extends Item { + + @Override + public ItemClassification analyse() { + return doAnalyse(); + } + + public LinkItem(String url, Item parent) { + super(url, parent); + itemType = ItemType.LINK; + } +// public LinkItem(String url) { +// super(url); +// itemType = ItemType.LINK; +// } + + @Override + protected HashMap<String, String> getAttributes() { + if (attributes == null) { + attributes = new HashMap<String,String>(); + } + attributes.put("shape", "box"); + attributes.put("style", "filled"); + attributes.put("fontname", "Verdana"); + attributes.put("fontsize", "10"); + return attributes; + } + @Override + protected String getGraphNodetext() { + //return this.filename; + if (this.filename.compareTo("")!=0) + return "link to: "+this.filename; + else + return "link to: "+this.getUrl(); + } +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/data/PageItem.java b/Master/Agile Software Development/TestApp/src/testapp/data/PageItem.java new file mode 100644 index 0000000..071539a --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/data/PageItem.java @@ -0,0 +1,48 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package testapp.data; + +import java.util.HashMap; +import java.util.logging.*; + +/** + * + * @author sven + */ +public class PageItem extends Item { + + @Override + public ItemClassification analyse() { + return doAnalyse(); + } + + public PageItem(String url, Item parent) { + super(url, parent); + itemType = ItemType.PAGE; + attributes = getAttributes(); + } + +// public PageItem(String url) { +// super(url); +// itemType = ItemType.PAGE; +// attributes = getAttributes(); +// } + @Override + protected HashMap<String, String> getAttributes() { + if (attributes == null) { + attributes = new HashMap<String, String>(); + } + attributes.put("shape", "house"); + attributes.put("style", "filled"); + attributes.put("fontname", "Verdana"); + attributes.put("fontsize", "10"); + return attributes; + } + + @Override + protected String getGraphNodetext() { + return getFullUrl(); + } +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/data/ScriptItem.java b/Master/Agile Software Development/TestApp/src/testapp/data/ScriptItem.java new file mode 100644 index 0000000..1764f3a --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/data/ScriptItem.java @@ -0,0 +1,62 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package testapp.data; + +import java.util.HashMap; +import java.util.logging.*; +/** + * + * @author sven + */ +public class ScriptItem extends Item { + + @Override + public ItemClassification analyse() { + setClassification(ItemClassification.HARMLESS); + + checkPlace(); + if (itemPlace == ItemPlace.REMOTE) { + setClassification(ItemClassification.CRITICAL); + } else if (itemPlace == ItemPlace.LOCAL) { + setClassification(ItemClassification.HARMLESS); + } +/* TODO: Take Child classifcation + for (Item child : childItems) { + childClassification = child.analyse(); + } + if ((itemClassification.compareTo(childClassification) > 0)) { + setClassification(childClassification); + } +*/ + return doAnalyse(); + } + + public ScriptItem(String url, Item parent) { + super(url, parent); + itemType = ItemType.SCRIPT; + } + +// public ScriptItem(String url) { +// super(url); +// itemType = ItemType.SCRIPT; +// } + + @Override + protected HashMap<String, String> getAttributes() { + if (attributes == null) { + attributes = new HashMap<String, String>(); + } + attributes.put("shape", "ellipse"); + attributes.put("style", "filled"); + attributes.put("fontname", "Verdana"); + attributes.put("fontsize", "10"); + return attributes; + } + + @Override + protected String getGraphNodetext() { + return "SCRIPT: "+this.url; + } +} diff --git a/Master/Agile Software Development/TestApp/src/testapp/data/StyleItem.java b/Master/Agile Software Development/TestApp/src/testapp/data/StyleItem.java new file mode 100644 index 0000000..4bb8ff3 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/data/StyleItem.java @@ -0,0 +1,47 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package testapp.data; +import java.util.logging.*; +import java.util.HashMap; + +/** + * + * @author sven + */ +public class StyleItem extends Item { + + @Override + public ItemClassification analyse() { + return doAnalyse(); + } + + public StyleItem(String url, Item parent) { + super(url, parent); + itemType = ItemType.STYLE; + } + +// public StyleItem(String url) { +// super(url); +// itemType = ItemType.STYLE; +// } + + @Override + protected HashMap<String, String> getAttributes() { + if (attributes == null) { + attributes = new HashMap<String,String>(); + } + attributes.put("shape", "septagon"); + attributes.put("style", "filled"); + attributes.put("fontname", "Verdana"); + attributes.put("fontsize", "10"); + return attributes; + } + @Override + protected String getGraphNodetext() { + //return this.filename; + return "CSS STYLE "+this.url; + } +} |
