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

org.databene.formats.xml.ParseContext Maven / Gradle / Ivy

Go to download

'Databene Formats' is an open source software library for supporting data file and other formats like CSV, fixed width files, XLS, Properties and Regex. It is designed for multithreaded use and high performance.

The newest version!
/*
 * Copyright (C) 2011-2015 Volker Bergmann ([email protected]).
 * All rights reserved.
 *
 * 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.databene.formats.xml;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import org.databene.commons.ArrayUtil;
import org.databene.commons.Context;
import org.databene.commons.context.DefaultContext;
import org.databene.commons.xml.XMLUtil;
import org.w3c.dom.Element;

/**
 * Provides context informations and operations for XML parsing.
 * Created: 05.12.2010 12:09:54
 * @param  the type of components that constitute the path
 * @since 0.5.4
 * @author Volker Bergmann
 */
public class ParseContext implements Context {
	
	protected XMLElementParserFactory factory;
	protected Class pathComponentType;
	private Context context;
	
	public ParseContext(Class pathComponentType) {
		this(pathComponentType, new XMLElementParserFactory());
	}

	public ParseContext(Class pathComponentType, XMLElementParserFactory factory) {
		this.pathComponentType = pathComponentType;
		this.factory = factory;
		this.context = new DefaultContext();
	}

	public void addParser(XMLElementParser parser) {
		factory.addParser(parser);
	}

	public E parseElement(Element element, E[] parentPath) {
		XMLElementParser parser = factory.getParser(element, parentPath);
		return parser.parse(element, parentPath, this);
	}
	
	public List parseChildElementsOf(Element element, E[] currentPath) {
		List result = new ArrayList();
		for (Element childElement : XMLUtil.getChildElements(element))
			result.add(parseChildElement(childElement, currentPath));
		return result;
	}
	
	public E parseChildElement(Element childElement, E[] currentPath) {
		return parseElement(childElement, currentPath);
	}

	@SuppressWarnings("unchecked")
	public E[] createSubPath(E[] parentPath, E currentObject) {
		if (parentPath == null)
			return ArrayUtil.buildObjectArrayOfType(pathComponentType, currentObject);
		else
			return ArrayUtil.append(currentObject, parentPath);
	}

	@Override
	public Object get(String key) {
		return context.get(key);
	}

	@Override
	public void set(String key, Object value) {
		context.set(key, value);
	}

	@Override
	public void remove(String key) {
		context.remove(key);
	}

	@Override
	public boolean contains(String key) {
		return context.contains(key);
	}

	@Override
	public Set keySet() {
		return context.keySet();
	}

	@Override
	public Set> entrySet() {
		return context.entrySet();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy