diff options
Diffstat (limited to 'Master/Agile Software Development/TestApp/src/testapp/data/Item.java')
| -rw-r--r-- | Master/Agile Software Development/TestApp/src/testapp/data/Item.java | 413 |
1 files changed, 413 insertions, 0 deletions
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; + } +} |
