// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Enigma2 {
   public static void main(String[] args) {
      try {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder db = dbf.newDocumentBuilder();
         Document doc = db.parse(new File("C:\\enigma.xml"));
         List lst = new ArrayList();
         NodeList elements = doc.getElementsByTagName("rotor");
         for (int i = 0; i < elements.getLength(); i++) {
            Node element = (Element) elements.item(i);
            Rotor r = new Rotor();
            NodeList elements2 = element.getChildNodes();
            for(int j = 0; j < elements2.getLength(); j++) {
               if(elements2.item(j).getNodeType() == Node.ELEMENT_NODE) {
                  Node element2 = (Element) elements2.item(j);
                  String key = element2.getNodeName();
                  String val = element2.getFirstChild().getNodeValue();
                  if(key.equals("text")) {
                     r.setText(val);
                  } else if(key.equals("notch")) {
                     r.setNotch(val);
                  } else if(key.equals("ring")) {
                     r.setRing(val);
                  } else if(key.equals("pos")) {
                     r.setPos(val);
                  }
               }
            }
            lst.add(r);
         }
         System.out.println(lst);
      } catch (FactoryConfigurationError e) {
         e.printStackTrace();
      } catch (ParserConfigurationException e) {
         e.printStackTrace();
      } catch (SAXException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
      return;
   }
}

class Rotor {
   private String ring;
   private String text;
   private String notch;
   private String pos;
   public String getNotch() {
      return notch;
   }
   public void setNotch(String notch) {
      this.notch = notch;
   }
   public String getPos() {
      return pos;
   }
   public void setPos(String pos) {
      this.pos = pos;
   }
   public String getRing() {
      return ring;
   }
   public void setRing(String ring) {
      this.ring = ring;
   }
   public String getText() {
      return text;
   }
   public void setText(String text) {
      this.text = text;
   }
   public String toString() {
      return (text + " " + notch + " " + ring + " " + pos);
   }
}
