krysalis-update/src/java/org/krysalis/depot/common/util/note Annotation.java,NONE,1.1 AnnotationIterator.java,NONE,1.1 AnnotationScratchpad.java,NONE,1.1

Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:07:18 +0000
Newsgroups gmane.comp.krysalis.cvs
Message-ID <[email protected]>
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/common/util/note
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/common/util/note

Added Files:
	Annotation.java AnnotationIterator.java 
	AnnotationScratchpad.java 
Log Message:
Copied from the apache incubator.

--- NEW FILE: Annotation.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.common.util.note;

import org.krysalis.depot.common.util.debug.DebugUtils;
import org.krysalis.depot.common.util.dom.DOMProducer;
import org.krysalis.depot.common.util.dom.DOMUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * @version $Revision: 1.1 $
 */
public class Annotation implements DOMProducer {
	static final int UNSET = 0;
	static final int DEBUG = 1;
	static final int VERBOSE = 2;
	static final int INFORMATION = 3;
	static final int WARNING = 4;
	static final int ERROR = 5;

	private int m_qualifier = 0;
	private Object m_content = null;
	private Throwable m_throwable = null;

	public Annotation(int qualifier, Object content) {
		m_qualifier = qualifier;
		m_content = content;
		m_throwable = null;
	}

	public Annotation(int qualifier, Object content, Throwable throwable) {
		m_qualifier = qualifier;
		m_content = content;
		m_throwable = throwable;
	}

	public String getQualifierAsString() {
		String str = "unset";

		switch (m_qualifier) {
			case DEBUG :
				{
					str = "DEBUG";
					break;
				}
			case VERBOSE :
				{
					str = "VERBOSE";
					break;
				}
			case INFORMATION :
				{
					str = "INFORMATION";
					break;
				}
			case WARNING :
				{
					str = "WARNING";
					break;
				}
			case ERROR :
				{
					str = "ERROR";
					break;
				}
		}

		return str;
	}

	public String toString() {
		StringBuffer buffer = new StringBuffer();

		buffer.append(getQualifierAsString());
		buffer.append(": ");
		buffer.append(getContentString());


		return buffer.toString();

	}
	
	public String getContentString() {
		StringBuffer buffer = new StringBuffer();

		buffer.append(m_content.toString());
		
		if (null != m_throwable) {
			buffer.append(": ");
			buffer.append(m_throwable.getLocalizedMessage());
			buffer.append("\n");
			buffer.append(DebugUtils.getDump(m_throwable));
		}

		return buffer.toString();

	}

	/**
	 * Returns the qualifier.
	 * @return int
	 */
	int getQualifier() {
		return m_qualifier;
	}

	public boolean isError() {
		return m_qualifier == Annotation.ERROR;
	}
	public boolean isWarning() {
		return m_qualifier == Annotation.WARNING;
	}
	public boolean isInformation() {
		return m_qualifier == Annotation.INFORMATION;
	}

	public boolean isVerbose() {
		return m_qualifier == Annotation.VERBOSE;
	}
	public boolean isDebug() {
		return m_qualifier == Annotation.DEBUG;
	}

	/**
	 * Returns the content.
	 * @return Object
	 */
	public Object getContent() {
		return m_content;
	}

	/**
	 * @return
	 */
	public Throwable getThrowable() {
		return m_throwable;
	}

	public void produceDOM(Document document, Element element) {
		Element annotation = DOMUtils.insertElement(document, element, this);
		DOMUtils.insertAttribute(
			document,
			annotation,
			"severity",
			getQualifierAsString());
		DOMUtils.insertText(document, annotation, m_content.toString());
		if (null != m_throwable)
			DOMUtils.insertText(
				document,
				annotation,
				DebugUtils.getDump(m_throwable));
	}
}

--- NEW FILE: AnnotationScratchpad.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.common.util.note;

import java.io.PrintWriter;
import java.util.Collection;
import java.util.Iterator;

import org.krysalis.depot.common.util.collection.EntityList;
import org.krysalis.depot.common.util.debug.DebugUtils;
import org.krysalis.depot.common.util.debug.Dumpable;
import org.krysalis.depot.common.util.dom.DOMProducer;
import org.krysalis.depot.common.util.dom.DOMUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * @version $Revision: 1.1 $
 */
public class AnnotationScratchpad implements Dumpable, DOMProducer {

    private EntityList m_list = null;

    /**
     * Constructor for AnnotationScratchpad.
     * @param c
     */
    public AnnotationScratchpad(AnnotationScratchpad otherPad) {
        super();

        m_list = new EntityList("Annotations", otherPad.m_list);
    }

    /**
     * Constructor for AnnotationScratchpad.
     * @param c
     */
    public AnnotationScratchpad(int maxSize) {
        super();

        m_list = new EntityList("Annotations");
        m_list.setMaxSize(maxSize);
    }

	/**
	 * Constructor for AnnotationScratchpad.
	 * @param c
	 */
	public AnnotationScratchpad(String title) {
		super();

		m_list = new EntityList(title);
	}
	
	/**
		* Constructor for AnnotationScratchpad.
		* @param c
		*/
	   public AnnotationScratchpad() {
		   super();

		   m_list = new EntityList("Annotations");
	   }

    /**
     * Constructor for AnnotationScratchpad.
     * @param c
     */
    public AnnotationScratchpad(Collection c) {
        super();

        m_list = new EntityList("Annotations",c);
    }

    public Annotation debug(Object content) {
        Annotation note = new Annotation(Annotation.DEBUG, content);
        m_list.add(note);
        return note;
    }

    public Annotation debug(Object content, Throwable throwable) {
        Annotation note = new Annotation(Annotation.DEBUG, content, throwable);
        m_list.add(note);
        return note;
    }

    public Annotation verbose(Object content) {
        Annotation note = new Annotation(Annotation.VERBOSE, content);
        m_list.add(note);
        return note;
    }
    public Annotation verbose(Object content, Throwable throwable) {
        Annotation note =
            new Annotation(Annotation.VERBOSE, content, throwable);
        m_list.add(note);
        return note;
    }

    public Annotation warning(Object content) {
        Annotation note = new Annotation(Annotation.WARNING, content);
        m_list.add(note);
        return note;
    }
    public Annotation warning(Object content, Throwable throwable) {
        Annotation note =
            new Annotation(Annotation.WARNING, content, throwable);
        m_list.add(note);
        return note;
    }

    public Annotation info(Object content) {
        Annotation note = new Annotation(Annotation.INFORMATION, content);
        m_list.add(note);
        return note;
    }
    public Annotation info(Object content, Throwable throwable) {
        Annotation note =
            new Annotation(Annotation.INFORMATION, content, throwable);
        m_list.add(note);
        return note;
    }

    public Annotation error(Object content) {
        Annotation note = new Annotation(Annotation.ERROR, content);
        m_list.add(note);
        return note;
    }
    public Annotation error(Object content, Throwable throwable) {
        Annotation note = new Annotation(Annotation.ERROR, content, throwable);
        m_list.add(note);
        return note;
    }

    public void addPad(AnnotationScratchpad pad) {
        m_list.addAll(pad.m_list);
    }

    public Iterator iterator() {
        return m_list.iterator();
    }

    public Iterator errorIterator() {
        return new AnnotationIterator(this, Annotation.ERROR);
    }

    public Iterator infoIterator() {
        return new AnnotationIterator(this, Annotation.INFORMATION);
    }

    public Iterator verboseIterator() {
        return new AnnotationIterator(this, Annotation.VERBOSE);
    }

    public Iterator debugIterator() {
        return new AnnotationIterator(this, Annotation.DEBUG);
    }

    public boolean hasAnnotations() {
        return !m_list.isEmpty();
    }

    public boolean hasErrors() {
        return errorIterator().hasNext();
    }

    public int size() {
        return m_list.size();
    }

    public void dump(PrintWriter out, int depth, boolean verbose) {

        final String indent = DebugUtils.getIndent(depth);
        final String indent1 = DebugUtils.getIndent(depth+1);

        out.print(indent);
        out.println(m_list.getName());
        if (hasAnnotations()) {
            out.print(indent);
            out.println("(");
            for (Iterator i = m_list.iterator(); i.hasNext();) {
                Object entry = (Object) i.next();

                if (entry instanceof Dumpable) {
                    Dumpable d = (Dumpable) entry;
                    d.dump(out, depth + 1, verbose);
                }
                else {
                    out.print(indent1);
                    out.println(entry.toString());
                }
            }
            out.print(indent);
            out.println(")");
        }
    }
    
	public void produceDOM(Document document, Element element) {
		DOMUtils.produceDOM(document, element, (DOMProducer)m_list);
	}
}

--- NEW FILE: AnnotationIterator.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.common.util.note;

import java.util.Iterator;

/**
 * @version $Revision: 1.1 $
 */
public class AnnotationIterator implements Iterator {

	private int m_qualifier = Annotation.UNSET;
	private boolean m_equalOrGreater = true;
	private Iterator m_baseIterator = null;

	private Object m_nextAnnotation = null;

	/**
	 * Constructor for AnnotationIterator.
	 */
	public AnnotationIterator(AnnotationScratchpad pad, int qualifier) {
		m_baseIterator = pad.iterator();
		m_qualifier = qualifier;

		lookAhead();
	}

	/**
	 * Constructor for AnnotationIterator.
	 */
	public AnnotationIterator(
		AnnotationScratchpad pad,
		int qualifier,
		boolean equalOrGreater) {
		m_baseIterator = pad.iterator();
		m_qualifier = qualifier;
		m_equalOrGreater = equalOrGreater;

		lookAhead();
	}

	public boolean hasNext() {
		return (null != m_nextAnnotation);
	}

	public Object next() {
		Object nextAnnotation = m_nextAnnotation;

		lookAhead();

		return nextAnnotation;
	}

	public void remove() {
		lookAhead();
	}

	private void lookAhead() {
		m_nextAnnotation = null;

		for (; m_baseIterator.hasNext() && (null == m_nextAnnotation);) {
			Object next = m_baseIterator.next();

			if (next instanceof Annotation) {
				Annotation note = (Annotation) next;

				if ((note.getQualifier() == m_qualifier)
					|| (m_equalOrGreater && (note.getQualifier() > m_qualifier)))
					m_nextAnnotation = note;
			}
			else if (m_qualifier > Annotation.DEBUG) {
				// Unqualified
				m_nextAnnotation = next;
			}
		}
	}
}



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/