Example usage for com.badlogic.gdx.utils XmlReader XmlReader

List of usage examples for com.badlogic.gdx.utils XmlReader XmlReader

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils XmlReader XmlReader.

Prototype

XmlReader

Source Link

Usage

From source file:com.sixteencolorgames.sandbox.helpers.Loaders.java

public static void loadMaterials(GameLauncher game, FileHandle xml) throws IOException {
    XmlReader reader = new XmlReader();
    XmlReader.Element root = reader.parse(xml);
    System.out.println("Loading mats");
    Array<Element> xmlMaterials = root.getChildrenByName("material");
    for (Element mat : xmlMaterials) {
        int id = mat.getInt("id", 0);//TODO map claimed values
        String name = mat.get("name", Language.getLocal("mat.noname"));
        String texture = mat.get("texture", "brick.png");
        int variations = mat.getInt("variations", 1);
        game.materials.put(id, new Material(id, name, texture, variations));
        game.matTex.put(name, new Texture(Gdx.files.internal("materials/" + texture)));
        System.out.println("Adding material: " + name + " id of " + id);
    }/*from w w w. ja  v a 2s .  co m*/
}

From source file:com.turbogerm.helljump.dataaccess.RiseSectionMetadataReader.java

License:Open Source License

public static RiseSectionsMetadata read(FileHandle fileHandle) {

    XmlReader reader = new XmlReader();

    Element riseSectionsMetaNode = null;
    try {/*from  w w w  .ja  va2  s. c om*/
        riseSectionsMetaNode = reader.parse(fileHandle);
    } catch (IOException e) {
        Logger.error(e.getMessage());
        return null;
    }

    Element riseSectionsNode = riseSectionsMetaNode.getChildByName("risesections");

    int numRiseSections = riseSectionsNode.getChildCount();
    Array<RiseSectionMetadata> riseSectionMetadataList = new Array<RiseSectionMetadata>(true, numRiseSections);
    for (int i = 0; i < numRiseSections; i++) {
        Element riseSectionNode = riseSectionsNode.getChild(i);
        RiseSectionMetadata riseSection = getRiseSectionMetadata(riseSectionNode);
        riseSectionMetadataList.add(riseSection);
    }

    return new RiseSectionsMetadata(riseSectionMetadataList);
}

From source file:com.turbogerm.helljump.dataaccess.RiseSectionDataReader.java

License:Open Source License

public static RiseSectionData read(String type, String name, FileHandle fileHandle) {

    XmlReader reader = new XmlReader();

    Element riseSectionNode = null;
    try {//from www . j  av  a  2s  .  c  o  m
        riseSectionNode = reader.parse(fileHandle);
    } catch (IOException e) {
        Logger.error(e.getMessage());
        return null;
    }

    int stepRange = ReaderUtilities.getIntAttribute(riseSectionNode, "steprange");
    int difficulty = ReaderUtilities.getIntAttribute(riseSectionNode, "difficulty");

    NextGeneratedId nextGeneratedId = new NextGeneratedId();
    Element platformsNode = riseSectionNode.getChildByName("platforms");
    Array<PlatformData> platformDataList = getPlatformsData(nextGeneratedId, platformsNode);

    Element enemiesNode = riseSectionNode.getChildByName("enemies");
    Array<EnemyData> enemyDataList = getEnemiesData(enemiesNode);

    Element itemsNode = riseSectionNode.getChildByName("items");
    Array<ItemData> itemDataList = getItemsData(itemsNode);

    return new RiseSectionData(type, name, stepRange, difficulty, platformDataList, enemyDataList,
            itemDataList);
}

From source file:com.explatcreations.sft.loading.MapLoader.java

License:Open Source License

public static ITiledMap fromFile(FileHandle file) {
    final XmlReader xml = new XmlReader();
    try {/*from  w  ww .  j a v  a  2  s .  com*/
        final XmlReader.Element root = xml.parse(file);
        final Map<String, String> propertiesMap = new HashMap<String, String>();
        final XmlReader.Element properties = root.getChildByName("properties");
        for (int i = 0; i < properties.getChildCount(); i += 1) {
            XmlReader.Element prop = properties.getChild(i);
            final String name = prop.getAttribute("name");
            final String value = prop.getAttribute("value");
            propertiesMap.put(name, value);
        }
        final XmlReader.Element layers = root.getChildByName("layer");
        final String csv = layers.getChild(0).getText();
        final int[][] tiles = parseCsv(csv);
        return new ITiledMap() {
            @Override
            public String getProperty(String key) {
                return propertiesMap.get(key);
            }

            @Override
            public int[][] getTiles() {
                return tiles; //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public int getCols() {
                return tiles.length;
            }

            @Override
            public int getRows() {
                return tiles[0].length;
            }
        };
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

From source file:com.agateau.utils.FileUtils.java

License:Apache License

public static XmlReader.Element parseXml(FileHandle handle) {
    XmlReader reader = new XmlReader();
    XmlReader.Element root = reader.parse(handle);
    if (root == null) {
        NLog.e("Failed to parse xml file from %s. No root element.", handle.path());
        return null;
    }/* www .  j  ava  2 s  .co  m*/
    return root;
}

From source file:skyranger.game.xml.XmlParser.java

License:Apache License

public XmlParser() {
    super();/*from   w  w w .  j  a v a2 s  .co  m*/
    try {
        XmlReader xmlReader = new XmlReader();
        FileHandle file = Gdx.files.internal("assets\\map.xml");
        this.root = xmlReader.parse(file);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.spritertest.GdxSpriter.java

License:Apache License

private static SpriterData load(FileHandle file) {
    XmlReader reader = new XmlReader();
    SpriterData data = new SpriterData();
    try {//from  w w  w .j a  v a2  s  .  co m
        Element root = reader.parse(file);
        loadFolders(root.getChildrenByName("folder"), data);
        loadEntities(root.getChildrenByName("entity"), data);

    } catch (IOException e) {
        e.printStackTrace();
    }
    return data;
}

From source file:org.matheusdev.util.TmxObjectsLoader.java

License:Open Source License

public static void main(String... args) throws IOException {
    GdxNativesLoader.load();//from w ww  .  ja v a 2s  . c  om
    Physics physics = new Physics(Vector2.Zero, true);
    File mapfile = new File("data/maps/newmap/map005.tmx");
    Element mapXML = new XmlReader().parse(new FileInputStream(mapfile));
    System.out.println(mapXML);

    new TmxObjectsLoader(mapXML).loadToPhysics(physics, 32, 32, 50, 50);
}

From source file:com.algodal.gdxscreen.utils.GdxLoad.java

License:Apache License

public LoadData load() {
    XmlReader xmlReader = new XmlReader();
    Json json = new Json();
    return debug.assertNoException("No exception during loading", new Operation<LoadData>() {
        @Override/*from w  w  w.  j  a  v a  2  s . c o m*/
        public LoadData resultOf() throws Exception {
            Element element = xmlReader.parse(handle);
            final LoadData data = new LoadData();
            data.setName(element.getAttribute(GdxSave.ROOT_NAME));
            data.setTime(element.getAttribute(GdxSave.ROOT_TIME));
            data.setCount(Integer.parseInt(element.getAttribute(GdxSave.ROOT_COUNT)));
            data.setPlainOldJavaObjects(new Array<Object>());
            data.setRepresentation(element.toString());
            debug.assertEqual("root element is " + GdxSave.ROOT_ELEMENT, element.getName(),
                    GdxSave.ROOT_ELEMENT);
            for (int i = 0; i < element.getChildCount(); i++) {
                Element child = element.getChild(i);
                debug.assertEqual("child element is " + GdxSave.CHILD_ELEMENT, child.getName(),
                        GdxSave.CHILD_ELEMENT);
                Object childObject = json.fromJson(Object.class, child.getText());
                data.getPlainOldJavaObjects().add(childObject);
            }
            return data;
        }
    });
}

From source file:com.weimingtom.iteye.simplerpg.tiled.TiledLoader.java

License:Apache License

/** Loads a TiledMap from a tmx file.
 * @param tmxFile The tmx file. NULL to force load from <code>tmxData</code>.
 * @param tmxData The tmx file's content. NULL to force load from <code>tmxFile</code>. */
private static TiledMap createMap(FileHandle tmxFile, String tmxData) {
    final TiledMap map;

    map = new TiledMap();
    map.tmxFile = tmxFile;//  w w w .j a  va  2 s  . c  om

    try {
        XmlReader xmlReader = new XmlReader() {

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

            boolean awaitingData = false;
            TiledLayer currLayer;
            int currLayerWidth = 0, currLayerHeight = 0;
            TileSet currTileSet;
            TiledObjectGroup currObjectGroup;
            TiledObject currObject;
            int currTile;

            class Polyline {
                String name;
                String points;

                public Polyline(String name) {
                    this.name = name;
                }

                public Polyline() {
                }
            }

            Polyline polyline, polygon;

            class Property {
                String parentType, name, value;
            }

            Property currProperty;

            String encoding, dataString, compression;
            byte[] data;

            int dataCounter = 0, row, col;

            @Override
            protected void open(String name) {
                currBranch.push(name);

                if ("layer".equals(name)) {
                    currLayer = new TiledLayer();
                    return;
                }

                if ("tileset".equals(name)) {
                    currTileSet = new TileSet();
                    return;
                }

                if ("data".equals(name)) {
                    dataString = ""; // clear the string for new data
                    awaitingData = true;
                    return;
                }

                if ("objectgroup".equals(name)) {
                    currObjectGroup = new TiledObjectGroup();
                    return;
                }

                if ("object".equals(name)) {
                    currObject = new TiledObject();
                    return;
                }

                if ("property".equals(name)) {
                    currProperty = new Property();
                    currProperty.parentType = currBranch.get(currBranch.size() - 3);
                    return;
                }

                if ("polyline".equals(name)) {
                    polyline = new Polyline("polyline");
                    return;
                }

                if ("polygon".equals(name)) {
                    polygon = new Polyline("polygon");
                    return;
                }
            }

            @Override
            protected void attribute(String name, String value) {
                String element = currBranch.peek();

                if ("layer".equals(element)) {
                    if ("width".equals(name)) {
                        currLayerWidth = Integer.parseInt(value);
                    } else if ("height".equals(name)) {
                        currLayerHeight = Integer.parseInt(value);
                    }

                    if (currLayerWidth != 0 && currLayerHeight != 0) {
                        currLayer.tiles = new int[currLayerHeight][currLayerWidth];
                    }
                    if ("name".equals(name)) {
                        currLayer.name = value;
                    }
                    return;
                }

                if ("tileset".equals(element)) {
                    if ("firstgid".equals(name)) {
                        currTileSet.firstgid = Integer.parseInt(value);
                        return;
                    }
                    if ("tilewidth".equals(name)) {
                        currTileSet.tileWidth = Integer.parseInt(value);
                        return;
                    }
                    if ("tileheight".equals(name)) {
                        currTileSet.tileHeight = Integer.parseInt(value);
                        return;
                    }
                    if ("name".equals(name)) {
                        currTileSet.name = value;
                        return;
                    }
                    if ("spacing".equals(name)) {
                        currTileSet.spacing = Integer.parseInt(value);
                        return;
                    }
                    if ("margin".equals(name)) {
                        currTileSet.margin = Integer.parseInt(value);
                        return;
                    }
                    return;
                }

                if ("image".equals(element)) {
                    if ("source".equals(name)) {
                        currTileSet.imageName = value;
                        return;
                    }
                    return;
                }

                if ("data".equals(element)) {
                    if ("encoding".equals(name)) {
                        encoding = value;
                        return;
                    }
                    if ("compression".equals(name)) {
                        compression = value;
                        return;
                    }
                    return;
                }

                if ("objectgroup".equals(element)) {
                    if ("name".equals(name)) {
                        currObjectGroup.name = value;
                        return;
                    }
                    if ("height".equals(name)) {
                        currObjectGroup.height = Integer.parseInt(value);
                        return;
                    }
                    if ("width".equals(name)) {
                        currObjectGroup.width = Integer.parseInt(value);
                        return;
                    }
                    return;
                }

                if ("object".equals(element)) {
                    if ("name".equals(name)) {
                        currObject.name = value;
                        return;
                    }
                    if ("type".equals(name)) {
                        currObject.type = value;
                        return;
                    }
                    if ("x".equals(name)) {
                        currObject.x = Integer.parseInt(value);
                        return;
                    }
                    if ("y".equals(name)) {
                        currObject.y = Integer.parseInt(value);
                        return;
                    }
                    if ("width".equals(name)) {
                        currObject.width = Integer.parseInt(value);
                        return;
                    }
                    if ("height".equals(name)) {
                        currObject.height = Integer.parseInt(value);
                        return;
                    }
                    if ("gid".equals(name)) {
                        currObject.gid = Integer.parseInt(value);
                        return;
                    }
                    return;
                }

                if ("map".equals(element)) {
                    if ("orientation".equals(name)) {
                        map.orientation = value;
                        return;
                    }
                    if ("width".equals(name)) {
                        map.width = Integer.parseInt(value);
                        return;
                    }
                    if ("height".equals(name)) {
                        map.height = Integer.parseInt(value);
                        return;
                    }
                    if ("tilewidth".equals(name)) {
                        map.tileWidth = Integer.parseInt(value);
                        return;
                    }
                    if ("tileheight".equals(name)) {
                        map.tileHeight = Integer.parseInt(value);
                        return;
                    }
                    return;
                }

                if ("tile".equals(element)) {
                    if (awaitingData) { // Actually getting tile data
                        if ("gid".equals(name)) {
                            col = dataCounter % currLayerWidth;
                            row = dataCounter / currLayerWidth;
                            if (row < currLayerHeight) {
                                currLayer.tiles[row][col] = Integer.parseInt(value);
                            } else {
                                Gdx.app.log("TiledLoader",
                                        "Warning: extra XML gid values ignored! Your map is likely corrupt!");
                            }
                            dataCounter++;
                        }
                    } else { // Not getting tile data, must be a tile Id (for properties)
                        if ("id".equals(name)) {
                            currTile = Integer.parseInt(value);
                        }
                    }
                    return;
                }

                if ("property".equals(element)) {
                    if ("name".equals(name)) {
                        currProperty.name = value;
                        return;
                    }
                    if ("value".equals(name)) {
                        currProperty.value = value;
                        return;
                    }
                    return;
                }

                if ("polyline".equals(element)) {
                    if ("points".equals(name)) {
                        polyline.points = value;
                        return;
                    }
                    return;
                }

                if ("polygon".equals(element)) {
                    if ("points".equals(name)) {
                        polygon.points = value;
                        return;
                    }
                    return;
                }
            }

            @Override
            protected void text(String text) {
                if (awaitingData) {
                    dataString = dataString.concat(text);
                }
            }

            @Override
            protected void close() {
                String element = currBranch.pop();

                if ("layer".equals(element)) {
                    map.layers.add(currLayer);
                    currLayer = null;
                    return;
                }

                if ("tileset".equals(element)) {
                    map.tileSets.add(currTileSet);
                    currTileSet = null;
                    return;
                }

                if ("object".equals(element)) {
                    currObjectGroup.objects.add(currObject);
                    currObject = null;
                    return;
                }

                if ("objectgroup".equals(element)) {
                    map.objectGroups.add(currObjectGroup);
                    currObjectGroup = null;
                    return;
                }

                if ("property".equals(element)) {
                    putProperty(currProperty);
                    currProperty = null;
                    return;
                }

                if ("polyline".equals(element)) {
                    putPolyLine(polyline);
                    polyline = null;
                    return;
                }

                if ("polygon".equals(element)) {
                    putPolyLine(polygon);
                    polygon = null;
                    return;
                }

                if ("data".equals(element)) {

                    // decode and uncompress the data
                    if ("base64".equals(encoding)) {
                        if (dataString == null | "".equals(dataString.trim()))
                            return;

                        data = Base64Coder.decode(dataString.trim());

                        if ("gzip".equals(compression)) {
                            unGZip();
                        } else if ("zlib".equals(compression)) {
                            unZlib();
                        } else if (compression == null) {
                            arrangeData();
                        }

                    } else if ("csv".equals(encoding) && compression == null) {
                        fromCSV();

                    } else if (encoding == null && compression == null) {
                        // startElement() handles most of this
                        dataCounter = 0;// reset counter in case another layer comes through
                    } else {
                        throw new GdxRuntimeException("Unsupported encoding and/or compression format");
                    }

                    awaitingData = false;
                    return;
                }

                if ("property".equals(element)) {
                    putProperty(currProperty);
                    currProperty = null;
                }
            }

            private void putPolyLine(Polyline polyLine) {
                if (polyLine == null) {
                    return;
                }

                if ("polyline".equals(polyLine.name)) {
                    currObject.polyline = polyLine.points;
                    return;
                }

                if ("polygon".equals(polyLine.name)) {
                    currObject.polygon = polyLine.points;
                    return;
                }

                return;
            }

            private void putProperty(Property property) {
                if ("tile".equals(property.parentType)) {
                    map.setTileProperty(currTile + currTileSet.firstgid, property.name, property.value);
                    return;
                }

                if ("map".equals(property.parentType)) {
                    map.properties.put(property.name, property.value);
                    return;
                }

                if ("layer".equals(property.parentType)) {
                    currLayer.properties.put(property.name, property.value);
                    return;
                }

                if ("objectgroup".equals(property.parentType)) {
                    currObjectGroup.properties.put(property.name, property.value);
                    return;
                }

                if ("object".equals(property.parentType)) {
                    currObject.properties.put(property.name, property.value);
                    return;
                }
            }

            private void fromCSV() {
                StringTokenizer st = new StringTokenizer(dataString.trim(), ",");
                for (int row = 0; row < currLayerHeight; row++) {
                    for (int col = 0; col < currLayerWidth; col++) {
                        currLayer.tiles[row][col] = (int) Long.parseLong(st.nextToken().trim());
                    }
                }
            }

            private void arrangeData() {
                int byteCounter = 0;
                for (int row = 0; row < currLayerHeight; row++) {
                    for (int col = 0; col < currLayerWidth; col++) {
                        currLayer.tiles[row][col] = unsignedByteToInt(data[byteCounter++])
                                | unsignedByteToInt(data[byteCounter++]) << 8
                                | unsignedByteToInt(data[byteCounter++]) << 16
                                | unsignedByteToInt(data[byteCounter++]) << 24;
                    }
                }
            }

            private void unZlib() {
                Inflater zlib = new Inflater();
                byte[] readTemp = new byte[4];

                zlib.setInput(data, 0, data.length);

                for (int row = 0; row < currLayerHeight; row++) {
                    for (int col = 0; col < currLayerWidth; col++) {
                        try {
                            zlib.inflate(readTemp, 0, 4);
                            currLayer.tiles[row][col] = unsignedByteToInt(readTemp[0])
                                    | unsignedByteToInt(readTemp[1]) << 8 | unsignedByteToInt(readTemp[2]) << 16
                                    | unsignedByteToInt(readTemp[3]) << 24;
                        } catch (DataFormatException e) {
                            throw new GdxRuntimeException("Error Reading TMX Layer Data.", e);
                        }
                    }
                }
            }

            private void unGZip() {
                GZIPInputStream GZIS = null;
                try {
                    GZIS = new GZIPInputStream(new ByteArrayInputStream(data), data.length);
                } catch (IOException e) {
                    throw new GdxRuntimeException(
                            "Error Reading TMX Layer Data - IOException: " + e.getMessage());
                }

                // Read the GZIS data into an array, 4 bytes = 1 GID
                byte[] readTemp = new byte[4];
                for (int row = 0; row < currLayerHeight; row++) {
                    for (int col = 0; col < currLayerWidth; col++) {
                        try {
                            GZIS.read(readTemp, 0, 4);
                            currLayer.tiles[row][col] = unsignedByteToInt(readTemp[0])
                                    | unsignedByteToInt(readTemp[1]) << 8 | unsignedByteToInt(readTemp[2]) << 16
                                    | unsignedByteToInt(readTemp[3]) << 24;
                        } catch (IOException e) {
                            throw new GdxRuntimeException("Error Reading TMX Layer Data.", e);
                        }
                    }
                }
            }
        };
        // Is it a file?
        if (tmxFile != null) {
            xmlReader.parse(tmxFile);
        } else {
            xmlReader.parse(tmxData);
        }
    } catch (IOException e) {
        throw new GdxRuntimeException("Error Parsing TMX file", e);
    }

    return map;
}