All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.mdl.util.DNode Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

// @(#) $Id: DNode.java 2089 2007-11-28 13:56:13Z s3460 $
package at.spardat.xma.mdl.util;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Models a descriptive node which is used to accumulate debug information. It
 * may be used to gather a structural textural representation of this without fixing
 * the concrete output format. 
 * 
 * @author YSD, 02.05.2003 16:26:01
 */
public class DNode {
    
    /**
     * Constructs a root node.
     */
    public DNode () {
        shared_ = new DscShared();
    }
    
    /**
     * Constructs a root node that has no parent and sets an inital text
     */
    public DNode (String text) {
        shared_ = new DscShared();
        append(text);
    }
    
    /**
     * Sets this node to dummy which only applies to root nodes and prevent them
     * from beeing shown in the output.
     */
    public void setDummy (boolean what) {
        dummy_ = what;
    }
    
    /**
     * Constructs a node which becomes the last child of parent
     */
    public DNode (DNode parent) {
        if (parent.sons_ == null) parent.sons_ = new ArrayList();
        parent.sons_.add(this);
        shared_ = parent.shared_;
    }
    
    /**
     * Constructs a node which becomes the last child of parent and
     * whose inital text is set to text
     */
    public DNode (DNode parent, String text) {
        this (parent);
        append (text);
    }
    
    /**
     * Appends a string to the text of this.
     * 
     * @return this
     */
    public DNode app (String s) {
        append (s);
        return this;
    }
    
    /**
     * Appends a string of the form name=value.
     * @return this
     */
    public DNode app (String name, String value) {
        append (name).append("=").append(value);
        return this;
    }

    /**
     * Appends a start bracket
     */
    public DNode sb () {
        append ("[");
        return this;
    }
    
    /**
     * Appends an end bracket
     */
    public DNode eb () {
        append ("]");
        return this;
    }
    
    
    /**
     * Appends an int to the text of this.
     * 
     * @return this
     */
    public DNode app (int i) {
        append (String.valueOf(i));
        return this;
    }
    
    /**
     * Appends a string of the form name=value.
     * @return this
     */
    public DNode app (String name, int value) {
        append (name).append("=").append(String.valueOf(value));
        return this;
    }
    
    /**
     * Appends a boolean to the text of this.
     */
    public DNode app (boolean b) {
        if (b) append ("true");
        else append ("false");
        return this;
    }
    
    /**
     * Appends a string of the form name=value
     */
    public DNode app (String name, boolean value) {
        append (name).append("=").app(value);
        return this;
    }
    
    /**
     * Appends the result of calling String.valueOf with parameter value.
     * 
     * @return this
     */
    public DNode app (Object value) {
        appendObject (value);
        return this;
    }
    
    /**
     * Appends a string of the form name=value
     */
    public DNode app (String name, Object value) {
        append (name).append("="); appendObject (value);
        return this;
    }

    /**
     * If value is a Descriptive, add it to this, otherwise
     * convert value to a string via String.valueOf and 
     * add the string.
     */
    private void appendObject (Object value) {
        if (value == null) append ("null");
        else if (value instanceof Descriptive) {
            ((Descriptive)value).describe(this);
        } else {
            append (String.valueOf(value));
        }
    }
   
    
    /**
     * Appends a comma followed by a blank.
     * 
     * @return this
     */
    public DNode comma () {
        text_.append(", ");
        return this;
    }
    
    /**
     * Appends the name of the class (without package names) of the provided
     * object.
     * 
     * @return this
     */
    public DNode appShortClass (Object o) {
        String className = o.getClass().getName();
        int  lastPoint = className.lastIndexOf('.');
        if (lastPoint != -1) className = className.substring(lastPoint+1);
        text_.append(className);
        return this; 
    }
    
    /**
     * Appends text which is generated by iterating over the sons. Assume that
     * there are sons s1, s2 and s3. Then the appended text looks like
     * 
     * [s1, s2, s3]
     * 
* All sons are removed in this method. * * @return this */ public DNode appSons () { text_.append('['); if (sons_ != null) { for (int i=0; iDescriptive to a string and returns it. */ public static String toString (Descriptive d) { DNode n = new DNode(); d.describe(n); return n.toString(); } // recursively prints the tree private void toStringRec (StringBuffer b, int indent) { for (int i=indent; i>0; i--) b.append(' '); b.append(getText()); b.append("\n"); if (sons_ != null) { Iterator sons = getSons(); while (sons.hasNext()) { ((DNode)sons.next()).toStringRec(b, indent+2); } } } /** * Appends a provided String which is cut to a max length as specified in the * shared_-object. */ private DNode append (String text) { if (text == null) append ("null"); else { String toAppend; if (text.length() > shared_.maxAppendedStringLen_) { toAppend = text.substring(0, shared_.maxAppendedStringLen_); toAppend += "..."; } else { toAppend = text; } if (toAppend.indexOf('\n') != -1) toAppend = toAppend.replace('\n', ' '); if (toAppend.indexOf('\r') != -1) toAppend = toAppend.replace('\r', ' '); text_.append(toAppend); } return this; } /** * Returns the text stored in this node * * @return String never null */ public String getText () { return text_.toString(); } /** * Returns an iterator that iterates over the sons or null, if this has no sons. */ public Iterator getSons () { if (sons_ == null) return null; return sons_.iterator(); } /** * Shows this node in a modeless windows in a tree. */ public void display () { new DNodeInspector(this).show(); } /** * If the provided object o is a Descriptive, it is displayed * in a SWT shell showing its structure. */ public static void display (Object o) { if (o != null && o instanceof Descriptive) { Descriptive d = (Descriptive) o; DNode n = new DNode(); d.describe(n); n.display(); } } /** * The text of this node */ private StringBuffer text_ = new StringBuffer(); /** * Sons of DscNodes */ private ArrayList sons_; /** * The object described by this node */ private Object described_; /** * Specifies whether a root node is a dummy node. A dummy root node * will not show up in the final output */ private boolean dummy_; /** * Points to an Object that stores information all DNodes have in common */ private DscShared shared_; /** * Information that all linked DNodes have in common * * @author YSD, 02.05.2003 16:30:26 */ private static class DscShared { int maxAppendedStringLen_ = 50; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy