texturemanager.java

来自「Java mulitplayer strategy game. Adaptati」· Java 代码 · 共 88 行

JAVA
88
字号
/*
 * Created on 2005-10-04.
 * $Id: TextureManager.java,v 1.3 2005/10/05 11:42:07 macx2k Exp $
 */
package net.sf.jawp.j3d.texture;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.DOMBuilder;
import org.xml.sax.SAXException;

/**
 * TODO maciekm (2005-10-04) comment this type!
 * 
 * <p>
 * <b>Latest changed:</b><br/> $Date: 2005/10/05 11:42:07 $ $Author: macx2k $
 * </p>
 * 
 * @author Maciej Malecki
 * @version $Revision: 1.3 $
 */
public class TextureManager
{
	private static final DocumentBuilderFactory XML_FACTORY = DocumentBuilderFactory
			.newInstance();

	private final Map<String, TextureGroup> groups = new HashMap<String, TextureGroup>();

	public TextureManager(final InputStream configXml)
	{
		parseConfig(configXml);
	}

	public TextureGroup getGroup(final String id)
	{
		final TextureGroup tg = groups.get(id);
		if (tg == null)
		{
			throw new NoSuchElementException("Group " + id + " not found");
		}
		return tg;
	}

	private void parseConfig(final InputStream configXml)
	{
		try
		{
			final DocumentBuilder builder = XML_FACTORY.newDocumentBuilder();
			final Document document = new DOMBuilder().build(builder
					.parse(configXml));
			
			final List groups = document.getRootElement().getChildren("group");
			
			for (Object obj : groups)
			{
				final Element groupNode = (Element) obj;
				final TextureGroup tg = new TextureGroup(groupNode);
				this.groups.put(tg.getName(), tg);
			}
		}
		catch (final ParserConfigurationException e)
		{
			throw new RuntimeException(e);
		}
		catch (final IOException e)
		{
			throw new RuntimeException(
					"Error reading config file for TextureManager", e);
		}
		catch (final SAXException e)
		{
			throw new RuntimeException(
					"Malformed config file for TextureManager", e);
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?