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

org.jbasics.parser.ParsingInfoBuilder Maven / Gradle / Ivy

/*
 * Copyright (c) 2009-2015
 * 	IT-Consulting Stephan Schloepke (http://www.schloepke.de/)
 * 	klemm software consulting Mirko Klemm (http://www.klemm-scs.com/)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.jbasics.parser;

import org.jbasics.checker.ContractCheck;
import org.jbasics.parser.invoker.Invoker;
import org.jbasics.pattern.builder.Builder;
import org.jbasics.pattern.factory.Factory;
import org.jbasics.types.tuples.Pair;

import javax.xml.namespace.QName;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

@SuppressWarnings("unchecked")
public class ParsingInfoBuilder implements Builder {
	private Class builderType;
	private Factory builderFactory;
	private Invoker qualifiedNameInvoker;
	private Map> attributeInvokers;
	private Invoker anyAttributeInvoker;
	private Map>> elementInvokers;
	private Pair> anyElementInvoker;
	private Invoker contentInvoker;
	private Invoker commentInvoker;

	public static ParsingInfoBuilder newInstance() {
		return new ParsingInfoBuilder();
	}

	public void reset() {
		this.builderType = null;
		this.builderFactory = null;
		this.qualifiedNameInvoker = null;
		if (this.attributeInvokers != null) {
			this.attributeInvokers.clear();
		}
		this.anyAttributeInvoker = null;
		if (this.elementInvokers != null) {
			this.elementInvokers.clear();
		}
		this.anyElementInvoker = null;
		this.contentInvoker = null;
		this.commentInvoker = null;
	}

	public ParsingInfo build() {
		Map> attributes;
		if (this.attributeInvokers == null) {
			attributes = Collections.emptyMap();
		} else {
			attributes = new LinkedHashMap>();
			attributes.putAll(this.attributeInvokers);
			attributes = Collections.unmodifiableMap(attributes);
		}
		Map>> elements;
		if (this.elementInvokers == null) {
			elements = Collections.emptyMap();
		} else {
			elements = new LinkedHashMap>>();
			elements.putAll(this.elementInvokers);
			elements = Collections.unmodifiableMap(elements);
		}
		return new ParsingInfo(this.builderFactory, this.qualifiedNameInvoker, attributes, this.anyAttributeInvoker,
				elements, this.anyElementInvoker, this.contentInvoker, this.commentInvoker);
	}

	public ParsingInfoBuilder setBuilderFactory(Factory builderFactory) {
		this.builderFactory = ContractCheck.mustNotBeNull(builderFactory, "builderFactory");
		return this;
	}

	public ParsingInfoBuilder addAttribute(QName name, Invoker invoker) {
		if (this.attributeInvokers == null) {
			this.attributeInvokers = new LinkedHashMap>();
		}
		if (!this.attributeInvokers.containsKey(ContractCheck.mustNotBeNull(name, "name"))) {
			this.attributeInvokers.put(name, ContractCheck.mustNotBeNull(invoker, "metthod"));
		} else {
			throw new IllegalStateException("Attribute method for " + name + " already set to "
					+ this.attributeInvokers.get(name) + ". Duplicate " + invoker);
		}
		return this;
	}

	public ParsingInfoBuilder setAnyAttribute(Invoker invoker) {
		if (this.anyAttributeInvoker != null) {
			throw new IllegalStateException("Any Attribute Invoker already set to " + this.anyAttributeInvoker
					+ ". Duplicate " + invoker);
		}
		this.anyAttributeInvoker = ContractCheck.mustNotBeNull(invoker, "invoker");
		return this;
	}

	public ParsingInfoBuilder addElement(QName name, Pair> invoker) {
		if (this.elementInvokers == null) {
			this.elementInvokers = new LinkedHashMap>>();
		}
		if (!this.elementInvokers.containsKey(ContractCheck.mustNotBeNull(name, "name"))) {
			this.elementInvokers.put(name, ContractCheck.mustNotBeNull(invoker, "metthod"));
		} else {
			throw new IllegalStateException("Invoker for " + name + " already set to " + this.elementInvokers.get(name)
					+ ". Duplicate " + invoker);
		}
		return this;
	}

	public ParsingInfoBuilder setAnyElement(Pair> invoker) {
		if (this.anyElementInvoker != null) {
			throw new IllegalStateException("Any Element Method already set to " + this.anyElementInvoker
					+ ". Duplicate " + invoker);
		}
		this.anyElementInvoker = ContractCheck.mustNotBeNull(invoker, "invoker");
		return this;
	}

	public ParsingInfoBuilder setQualifiedName(Invoker invoker) {
		if (this.qualifiedNameInvoker != null) {
			throw new IllegalStateException("Qualified Name Method already set to " + this.qualifiedNameInvoker
					+ ". Duplicate " + invoker);
		}
		this.qualifiedNameInvoker = ContractCheck.mustNotBeNull(invoker, "invoker");
		return this;
	}

	public ParsingInfoBuilder setContentInvoker(Invoker invoker) {
		if (this.contentInvoker != null) {
			throw new IllegalStateException("Content invoker already set to " + this.qualifiedNameInvoker
					+ ". Duplicate " + invoker);
		}
		this.contentInvoker = ContractCheck.mustNotBeNull(invoker, "invoker");
		return this;
	}

	public ParsingInfoBuilder setCommentInvoker(Invoker invoker) {
		if (this.commentInvoker != null) {
			throw new IllegalStateException("Comment invoker already set to " + this.qualifiedNameInvoker
					+ ". Duplicate " + invoker);
		}
		this.commentInvoker = ContractCheck.mustNotBeNull(invoker, "invoker");
		return this;
	}

	public Class getBuilderType() {
		return this.builderType;
	}

	public ParsingInfoBuilder setBuilderType(Class builderType) {
		this.builderType = builderType;
		return this;
	}
}