<commit_before>/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * 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 com.badlogic.gdx.graphics.g2d.tiled;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.zip.DataFormatException;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.GdxRuntimeException;

/**
 * Loads a Tiled Map from a tmx file
 * @author <NAME>
 * */
public class TiledLoader extends DefaultHandler {

	// define states
	private static final int INIT = 0;
	private static final int DATA = 1;
	private static final int DONE = 2;

	/**
	 * Loads a Tiled Map from a tmx file
	 * @param tmxFile the map's tmx file
	 * */
	public static TiledMap createMap (FileHandle tmxFile) {

		final TiledMap map;

		map = new TiledMap();
		map.tmxFile = tmxFile;

		SAXParser parser = null;
		try {
			parser = SAXParserFactory.newInstance().newSAXParser();
			parser.parse(new InputSource(tmxFile.read()), new DefaultHandler() {

				int state = INIT;;
				TiledLayer currentLayer;
				TileSet currentTileSet;
				TiledObjectGroup currentObjectGroup;
				TiledObject currentObject;
				int currentTile;

				Stack<String> currentBranch = new Stack<String>();

				int firstgid, tileWidth, tileHeight, margin, spacing;
				String encoding, dataString, compression;
				byte[] data;

				int dataCounter = 0, row, col;

				@Override public void startElement (String uri, String name, String qName, Attributes attr) {										
					if("".equals(qName)){
						currentBranch.push(name);
					} else{
						currentBranch.push(qName);						
					}
					
					try {

						if ("layer".equals(qName) | "layer".equals(name)) {
							String layerName = attr.getValue("name");
							int layerWidth = Integer.parseInt(attr.getValue("width"));
							int layerHeight = Integer.parseInt(attr.getValue("height"));

							currentLayer = new TiledLayer(layerName, layerWidth, layerHeight);
							return;
						}

						if ("data".equals(qName) | "data".equals(name)) {
							encoding = attr.getValue("encoding");
							compression = attr.getValue("compression");
							dataString = ""; // clear the string for new data
							state = DATA;
							return;
						}

						if ("tileset".equals(qName) | "tileset".equals(name)) {
							firstgid = Integer.parseInt(attr.getValue("firstgid"));
							tileWidth = Integer.parseInt(attr.getValue("tilewidth"));
							tileHeight = Integer.parseInt(attr.getValue("tileheight"));
							spacing = parseIntWithDefault(attr.getValue("spacing"), 0);
							margin = parseIntWithDefault(attr.getValue("margin"), 0);
							return;
						}

						if ("objectgroup".equals(qName) | "objectgroup".equals(name)) {
							currentObjectGroup = new TiledObjectGroup();
							currentObjectGroup.name = attr.getValue("name");
							currentObjectGroup.height = Integer.parseInt(attr.getValue("height"));
							currentObjectGroup.width = Integer.parseInt(attr.getValue("width"));
							return;
						}

						if ("object".equals(qName) | "object".equals(name)) {
							currentObject = new TiledObject();
							currentObject.name = attr.getValue("name");
							currentObject.type = attr.getValue("type");
							currentObject.x = Integer.parseInt(attr.getValue("x"));
							currentObject.y = Integer.parseInt(attr.getValue("y"));
							currentObject.width = parseIntWithDefault(attr.getValue("width"), 0);
							currentObject.height = parseIntWithDefault(attr.getValue("height"), 0);
							currentObject.gid = parseIntWithDefault(attr.getValue("gid"), 0);
							return;
						}

						if ("image".equals(qName) | "image".equals(name)) {
							currentTileSet = new TileSet();
							currentTileSet.imageName = attr.getValue("source");
							currentTileSet.tileWidth = tileWidth;
							currentTileSet.tileHeight = tileHeight;
							currentTileSet.firstgid = firstgid;<commit_msg>[added] TileSet name now loaded by TiledLoader<commit_after>/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * 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 com.badlogic.gdx.graphics.g2d.tiled;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.zip.DataFormatException;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.GdxRuntimeException;

/**
 * Loads a Tiled Map from a tmx file
 * @author <NAME>
 * */
public class TiledLoader extends DefaultHandler {

	// define states
	private static final int INIT = 0;
	private static final int DATA = 1;
	private static final int DONE = 2;

	/**
	 * Loads a Tiled Map from a tmx file
	 * @param tmxFile the map's tmx file
	 * */
	public static TiledMap createMap (FileHandle tmxFile) {

		final TiledMap map;

		map = new TiledMap();
		map.tmxFile = tmxFile;

		SAXParser parser = null;
		try {
			parser = SAXParserFactory.newInstance().newSAXParser();
			parser.parse(new InputSource(tmxFile.read()), new DefaultHandler() {

				int state = INIT;;
				TiledLayer currentLayer;
				TileSet currentTileSet;
				TiledObjectGroup currentObjectGroup;
				TiledObject currentObject;
				int currentTile;

				Stack<String> currentBranch = new Stack<String>();

				int firstgid, tileWidth, tileHeight, margin, spacing;
				String tileSetName;
				String encoding, dataString, compression;
				byte[] data;

				int dataCounter = 0, row, col;

				@Override public void startElement (String uri, String name, String qName, Attributes attr) {										
					if("".equals(qName)){
						currentBranch.push(name);
					} else{
						currentBranch.push(qName);						
					}
					
					try {

						if ("layer".equals(qName) | "layer".equals(name)) {
							String layerName = attr.getValue("name");
							int layerWidth = Integer.parseInt(attr.getValue("width"));
							int layerHeight = Integer.parseInt(attr.getValue("height"));
							currentLayer = new TiledLayer(layerName, layerWidth, layerHeight);
							return;
						}

						if ("data".equals(qName) | "data".equals(name)) {
							encoding = attr.getValue("encoding");
							compression = attr.getValue("compression");
							dataString = ""; // clear the string for new data
							state = DATA;
							return;
						}

						if ("tileset".equals(qName) | "tileset".equals(name)) {
							firstgid = Integer.parseInt(attr.getValue("firstgid"));
							tileWidth = Integer.parseInt(attr.getValue("tilewidth"));
							tileHeight = Integer.parseInt(attr.getValue("tileheight"));
							tileSetName = attr.getValue("name");
							spacing = parseIntWithDefault(attr.getValue("spacing"), 0);
							margin = parseIntWithDefault(attr.getValue("margin"), 0);
							return;
						}

						if ("objectgroup".equals(qName) | "objectgroup".equals(name)) {
							currentObjectGroup = new TiledObjectGroup();
							currentObjectGroup.name = attr.getValue("name");
							currentObjectGroup.height = Integer.parseInt(attr.getValue("height"));
							currentObjectGroup.width = Integer.parseInt(attr.getValue("width"));
							return;
						}

						if ("object".equals(qName) | "object".equals(name)) {
							currentObject = new TiledObject();
							currentObject.name = attr.getValue("name");
							currentObject.type = attr.getValue("type");
							currentObject.x = Integer.parseInt(attr.getValue("x"));
							currentObject.y = Integer.parseInt(attr.getValue("y"));
							currentObject.width = parseIntWithDefault(attr.getValue("width"), 0);
							currentObject.height = parseIntWithDefault(attr.getValue("height"), 0);
							currentObject.gid = parseIntWithDefault(attr.getValue("gid"), 0);
							return;
						}

						if ("image".equals(qName) | "image".equals(name)) {
							currentTileSet = new TileSet();
							currentTileSet.imageName = attr.getValue("source");
							currentTileSet.name = tileSetName;
							currentTileSet.tileWidth = tileWidth;
							currentTileSet.tileHeight = tileHeight;
							currentTileSet.firstgid = firstgid;
{'fraction_non_alphanumeric': 0.144796828543112, 'fraction_numerical': 0.003964321110009911, 'mean_word_length': 3.663123844731978, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6034618', 'n_tokens_mistral': 3612, 'n_tokens_neox': 2812, 'n_words': 703}