Package org.jbpm.xml

Source Code of org.jbpm.xml.Parse

/*
* JBoss, Home of Professional Open Source
*
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jbpm.xml;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Stack;
import java.util.logging.Logger;

import org.jbpm.PvmException;
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;

/**
* combines the parse result, the parse problems and other parse information
* that is related to a single parse operation, see also {@link Parser}.
*
* @author Tom Baeyens
*/
public class Parse implements Serializable, ErrorHandler {

  private static final long serialVersionUID = 1L;
  private static final Logger log = Logger.getLogger(Parse.class.getName());
  private static final String NEWLINE = System.getProperty("line.separator");
 
  protected Document document = null;
  protected List<Problem> problems = null;
  protected Object documentObject;
  protected Stack<Object> objectStack;
 
  // problems /////////////////////////////////////////////////////////////////
 
  public List<Problem> getProblems() {
    return problems;
  }

  public void addProblem(Problem problem) {
    if (problems==null) {
      problems = new ArrayList<Problem>();
    }
    problems.add(problem);
  }

  public void addProblem(String msg) {
    addProblem(msg, null);
  }

  public void addProblem(String msg, Exception e) {
    addProblem(msg, e, Problem.SEVERITY_ERROR);
  }

  public void addWarning(String msg) {
    addWarning(msg, null);
  }

  public void addWarning(String msg, Exception e) {
    addProblem(msg, e, Problem.SEVERITY_WARNING);
  }

  public void addProblem(String msg, Exception e, String severity) {
    addProblem(new Problem(msg, e, severity));
  }

  public boolean hasProblems() {
    return ((problems != null) && (problems.size() > 0));
  }

  public void setProblems(List<Problem> problems) {
    this.problems = problems;
  }

  public void error(SAXParseException e) {
    addProblem(e.getMessage(), e, Problem.SEVERITY_ERROR);
  }
  public void fatalError(SAXParseException e) {
    addProblem(e.getMessage(), e, Problem.SEVERITY_FATALERROR);
  }
  public void warning(SAXParseException e) {
    addProblem(e.getMessage(), e, Problem.SEVERITY_WARNING);
  }
 
  // contextual objects ///////////////////////////////////////////////////////

  public void pushObject(Object object) {
    if (objectStack==null) {
      objectStack = new Stack<Object>();
    }
    objectStack.push(object);
  }

  public Object popObject() {
    if (objectStack!=null) {
      return objectStack.pop();
    }
    return null;
  }

  public Object peekObject() {
    if (objectStack!=null) {
      return objectStack.peek();
    }
    return null;
  }
 
  public <T> T findObject(Class<T> clazz) {
    if ( (objectStack!=null)
         && (! objectStack.isEmpty())
       ) {
      ListIterator<Object> listIter = objectStack.listIterator(objectStack.size());
      while (listIter.hasPrevious()) {
        Object object = listIter.previous();
        if (object!=null) {
          if (clazz.isAssignableFrom(object.getClass())) {
            return (T) object;
          }
        }
      }
      return null;
    }
    return null;
  }
 
  // getters and setters //////////////////////////////////////////////////////

  public Object getDocumentObject() {
    return documentObject;
  }
  public void setDocumentObject(Object object) {
    this.documentObject = object;
  }
  public Document getDocument() {
    return document;
  }
  public void setDocument(Document document) {
    this.document = document;
  }

  /** throws if error level problems are present and logs all problems and warnings. */ 
  public void checkProblems(String description) {
    if (hasProblems()) {
      StringBuffer errorMsg = new StringBuffer();
      for (Problem p : problems) {
        if (p.getSeverity().equals(Problem.SEVERITY_ERROR) || p.getSeverity().equals(Problem.SEVERITY_FATALERROR)) {
          if (errorMsg==null) {
            errorMsg = new StringBuffer();
          }
          errorMsg.append(NEWLINE);
          errorMsg.append(p.toString());
          log.severe(p.toString());
        } else {
          log.warning(p.toString());
        }
      }
      if (errorMsg!=null) {
        throw new PvmException("Error during parsing of "+description+": "+errorMsg);
      }
    }
  }
}
TOP

Related Classes of org.jbpm.xml.Parse

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.