All pastes #497561 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-05-19 22:56 UTC
rendered paste body
package ui.xml;



import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.io.File;

import java.io.IOException;

import java.net.URI;

import java.net.URISyntaxException;



import javax.swing.JPanel;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;



import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;



@SuppressWarnings("serial")

public class OnyxGridBag extends JPanel {

	

	// XML

	private String filename;

	private Document document;

	boolean compress = false;

	

	// SWING

	private int nbrMaxTD;

	private int nbrTD;



	

	public OnyxGridBag(String filename) {

		this.filename = filename;

		

		GridBagLayout gbMain = new GridBagLayout ();

		GridBagConstraints cMain = new GridBagConstraints ();

		this.setLayout(gbMain);

		

		// Nombre maximum de colonnes

		// C'est le nombre de <td> maximum trouvé dans une <tr> du fichier xml

		openXML ();

		

		nbrMaxTD = getTR (document.getDocumentElement(), 0);

		System.out.println (nbrMaxTD);

	}





	

	

	

	/*

	 * Tous ce qui concerne la manipulation du fichier xml... 

	 */

	

	// renvoie le nombre maximum de TD trouvé dans un des TR 

	private int getTR (Node parent, int nbrTDs) {

		int nbrMaxTD = 0;

		NodeList trNodeList = parent.getChildNodes();

		for (int i=0; i<trNodeList.getLength(); i++) {

			if (trNodeList.item(i).getNodeName().compareToIgnoreCase("tr")==0){

				int nbrTD = getTD (trNodeList.item(i), nbrTDs);

				if (nbrMaxTD<nbrTD)

					nbrMaxTD = nbrTD;

				System.out.println ("</tr>"+nbrMaxTD);

			}

		}

		return nbrMaxTD;

	}

	

	// renvoie le nombre de TD trouvé dans un TR

	private int getTD (Node parent, int nbrTD) {

		boolean haveTable = false;

		NodeList tdNodeList = parent.getChildNodes();

		for (int i=0; i<tdNodeList.getLength(); i++){

			if (tdNodeList.item(i).getNodeName().compareToIgnoreCase("td")==0){

				Node current = tdNodeList.item(i);

				for (int j=0; j<current.getChildNodes().getLength(); j++){

					if (current.getChildNodes().item(j).getNodeName().compareToIgnoreCase("table")==0){

						nbrTD = getTR (current.getChildNodes().item(j), nbrTD);

						haveTable = true;						

					}

				}

				if (haveTable)

					haveTable = false;

				else

					nbrTD++;

				System.out.println ("<td> "+nbrTD+"\t"+tdNodeList.item(i).getFirstChild().getNodeValue());

			}

		}

		

		return nbrTD; 

	}

	

	private void openXML () {

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

		

        DocumentBuilder builder = null;

		try {

			builder = factory.newDocumentBuilder();

		} catch (ParserConfigurationException e) {

			e.printStackTrace();

		}



		try {

			document = builder.parse(new File (filename));

		} catch (SAXException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		}

	}

}