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

org.ow2.bonita.parsing.XpdlParser Maven / Gradle / Ivy

/**
 * Copyright (C) 2006  Bull S. A. S.
 * Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
 * This library 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
 * version 2.1 of the License.
 * This library 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
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA  02110-1301, USA.
 **/
package org.ow2.bonita.parsing;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilderFactory;

import org.ow2.bonita.facade.def.majorElement.ProcessDefinition;
import org.ow2.bonita.parsing.binding.ActivityBinding;
import org.ow2.bonita.parsing.binding.DataFieldBinding;
import org.ow2.bonita.parsing.binding.ParticipantBinding;
import org.ow2.bonita.parsing.binding.TransitionBinding;
import org.ow2.bonita.parsing.binding.WorkflowProcessBinding;
import org.ow2.bonita.util.BonitaRuntimeException;
import org.ow2.bonita.util.ExceptionManager;
import org.ow2.bonita.util.Misc;
import org.ow2.bonita.util.XmlConstants;
import org.ow2.bonita.util.xml.Bindings;
import org.ow2.bonita.util.xml.Entity;
import org.ow2.bonita.util.xml.Parse;
import org.ow2.bonita.util.xml.Parser;
import org.ow2.bonita.util.xml.Problem;
import org.ow2.bonita.util.xml.UrlEntity;
import org.ow2.bonita.util.xml.XmlUtil;
import org.w3c.dom.Element;

/**
 * @author Marc Blachon, Guillaume Porcher, Charles Souillard, Miguel Valdes, Pierre Vigneras
 */
public class XpdlParser extends Parser {
	private static final Logger LOG = Logger.getLogger(XpdlParser.class.getName());

	public static final String CATEGORY_MAJOR_ELT = "majorElements";

	/** path to the directory containing all xsd files. */
	private static final String RESSOURCES_DIR = "";

	/** all schema resources used to parse xpdl files. */
	private static final String[] SCHEMA_RESOURCES = {
		RESSOURCES_DIR + XmlConstants.XML_SCHEMA,
		RESSOURCES_DIR + XmlConstants.XPDL_1_0_SCHEMA};

	public static final Bindings DEFAULT_BINDINGS = getDefaultBindings();

	// the default entities are initialized at the bottom of this file.
	private static Map defaultEntities = getDefaultEntities();

	/** schema document URIs as stringsF */
	private static String[] schemaSources = getSchemaSources();

	public XpdlParser() {
		super(DEFAULT_BINDINGS, defaultEntities);
	}

	@Override
	public Object parseDocumentElement(Element packageElement, Parse parse) {
		String packageNS = packageElement.getNamespaceURI();
		if (!XmlConstants.XPDL_1_0_NS.equals(packageNS)) {
			String message = ExceptionManager.getInstance().getMessage("bpx_XP_1", packageNS);
			throw new BonitaRuntimeException(message);
		}


		Element processesContainer = XmlUtil.element(packageElement, "WorkflowProcesses");
		if (processesContainer == null) {
			String message = ExceptionManager.getInstance().getMessage("bpx_XP_3");
		      throw new BonitaRuntimeException(message);
		}
		List processes = XmlUtil.elements(processesContainer, "WorkflowProcess");
		if (processes != null) {
			if (processes.size() > 1) {
				String message = ExceptionManager.getInstance().getMessage("bpx_XP_4");
			      throw new BonitaRuntimeException(message);
			}
		}
		Element processElement = processes.get(0);
		ProcessDefinition processDef = (ProcessDefinition) parseElement(processElement, parse, "majorElements");
		return processDef;
	}

	public synchronized DocumentBuilderFactory getDocumentBuilderFactory() {
		documentBuilderFactory = newDocumentBuilderFactory();
		documentBuilderFactory.setNamespaceAware(true);
		documentBuilderFactory.setValidating(true);
		// ignore white space can only be set if parser is validating
		documentBuilderFactory.setIgnoringElementContentWhitespace(false);
		// select xml schema as the schema language (a.o.t. DTD)
		documentBuilderFactory.setAttribute(
				XmlConstants.JAXP_SCHEMALANGUAGE,
				XmlConstants.XML_NS);
		// set schema sources
		documentBuilderFactory.setAttribute(XmlConstants.JAXP_SCHEMASOURCE, schemaSources);
		return documentBuilderFactory;
	}

	/** factory method for the default bindings */
	private static Bindings getDefaultBindings() {
		Bindings defaultBindings = new Bindings();

		//defaultBindings.addBinding(new PackageBinding());
		defaultBindings.addBinding(new ParticipantBinding());
		defaultBindings.addBinding(new DataFieldBinding());
		defaultBindings.addBinding(new WorkflowProcessBinding());
		defaultBindings.addBinding(new ActivityBinding());
		defaultBindings.addBinding(new TransitionBinding());

		return defaultBindings;
	}

	/** factory method for the default entities */
	private static Map getDefaultEntities() {
		Map defaultSchemaCatalog = new HashMap();
		ClassLoader resourceLoader = XpdlParser.class.getClassLoader();
		defaultSchemaCatalog.put(XmlConstants.XML_NS, new UrlEntity(
				XmlConstants.XML_SCHEMA, resourceLoader));
		defaultSchemaCatalog.put(XmlConstants.XML_NS2, new UrlEntity(
				XmlConstants.XML_SCHEMA, resourceLoader));
		defaultSchemaCatalog.put(XmlConstants.XPDL_1_0_NS, new UrlEntity(
				XmlConstants.XPDL_1_0_SCHEMA, resourceLoader));
		return defaultSchemaCatalog;
	}

	private static String[] getSchemaSources() {
		String[] tab = new String[SCHEMA_RESOURCES.length];
		ClassLoader resourceLoader = XpdlParser.class.getClassLoader();
		for (int i = 0; i < SCHEMA_RESOURCES.length; i++) {
			tab[i] = resourceLoader.getResource(SCHEMA_RESOURCES[i]).toExternalForm();
		}
		return tab;
	}

	/** throws an exception with appropriate message in case the parse contains
	 * errors or fatal errors.  This method also logs the problems with severity
	 * 'warning'. */
	public void checkProblems(String description, Parse parse) {
		if (parse.hasProblems()) {
			StringBuffer errorMsg = null;
			for (Problem p : parse.getProblems()) {
				if (p.getSeverity().equals(Problem.SEVERITY_ERROR) || p.getSeverity().equals(Problem.SEVERITY_FATALERROR)) {
					if (errorMsg == null) {
						errorMsg = new StringBuffer();
					}
					errorMsg.append(Misc.LINE_SEPARATOR).append("  ").append(p.toString());
					if (p.getCause() != null) {
						LOG.log(Level.SEVERE, p.toString(), p.getCause());
					} else {
						LOG.severe(p.toString());
					}
				} else {
					LOG.warning(p.toString());
				}
			}
			if (errorMsg != null) {
				throw new BonitaRuntimeException("errors during parsing of " + description + ": " + errorMsg);
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy