summaryrefslogtreecommitdiff
path: root/ihl/utils/IHLXMLParser.java
blob: 8073542e44d2547b9cf823c39e4ae5c49d5fb861 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package ihl.utils;

import ihl.IHLMod;
import ihl.guidebook.IHLGuidebookGui;

import java.io.IOException;

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

import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class IHLXMLParser {

	public DocumentBuilderFactory dbf;
	public DocumentBuilder db;
	
	public IHLXMLParser() throws ParserConfigurationException
	{
		dbf = DocumentBuilderFactory.newInstance();
	    db = dbf.newDocumentBuilder();
	}
	
	public void visit(Node node, int level, int sectionNumber1, IHLGuidebookGui ihlGuidebookGui) 
	{ 
	  IHLMod.log.debug("Visiting node.");
	  IHLMod.log.debug("Current section="+sectionNumber1);
	  IHLMod.log.debug("Node name="+node.getNodeName());
	  int sectionNumber = sectionNumber1;
	  NodeList list = node.getChildNodes(); 
	  IHLMod.log.debug("child size="+list.getLength());
      if (node instanceof Element)
	  {
    	  IHLMod.log.debug("node instance of Element.");
    	  Element e = (Element) node; 
    	  IHLMod.log.debug("Node tagname="+e.getTagName());
    	  IHLMod.log.debug("Node text content="+e.getTextContent());
	      if(e.getTagName().equals("title"))
	      {
	        	ihlGuidebookGui.setTitle(IHLUtils.trim(e.getTextContent()));
	      }
	      else if(e.getTagName().equals("itemstack"))
	      {
	        	String[] innername = IHLUtils.trim(e.getTextContent()).split(":");
	        	ihlGuidebookGui.addItemStack(IHLUtils.getOtherModItemStackWithDamage(innername[0], innername[1], Integer.parseInt(e.getAttribute("damage")),1));
	      }
	      else if(e.getTagName().equals("text"))
	      {
	    	  ihlGuidebookGui.addTextBlock(IHLUtils.trim(e.getTextContent()));
	      }
	      else if(e.getTagName().equals("image"))
	      {
	        	ihlGuidebookGui.setPicture(IHLUtils.trim(e.getTextContent()).replace(" ", ""), Integer.parseInt(e.getAttribute("width")),Integer.parseInt(e.getAttribute("height")));
	      }
	  }
	  for (int i = 0; i < list.getLength(); i++) 
	  {
	      Node childNode = list.item(i); 
	      if(childNode instanceof Element && ((Element) childNode).getTagName().equals("section"))
	      {
	    	  	Element e = (Element) childNode;
	    	  	int id = Integer.parseInt(e.getAttribute("id"));
	    	  	ihlGuidebookGui.setMaxSectionNumber(id);
		    	if(sectionNumber==id)
		    	{
				    visit(childNode, level + 1, sectionNumber, ihlGuidebookGui); 
		    	}
		    	else
		    	{
		    		if(sectionNumber > ihlGuidebookGui.getMaxSectionNumber())
		    		{
		    		  sectionNumber=0;
		    		  ihlGuidebookGui.setSectionNumber(0);
				      visit(childNode, level + 1, sectionNumber, ihlGuidebookGui); 
		    		}
		    	}
	      }
	      else
	      {
		      visit(childNode, level + 1, sectionNumber, ihlGuidebookGui); 
	      }
	  } 
	} 
	 
	public void setupGuidebookGUI(IHLGuidebookGui ihlGuidebookGui, int sectionNumber) throws SAXException, IOException
	{
		Document doc = db.parse(IHLMod.class.getResourceAsStream("/assets/ihl/config/ihl-guidebook.xml"));
	    visit(doc, 0, sectionNumber, ihlGuidebookGui); 
	} 
}