169 lines
5.3 KiB
Java
169 lines
5.3 KiB
Java
package org.lapaloma.aadd.inm;
|
|
|
|
import java.io.File;
|
|
import java.util.Scanner;
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
import org.palomafp.aadd.inm.gestores.GestorConfiguracion;
|
|
import org.w3c.dom.Document;
|
|
import org.w3c.dom.Element;
|
|
import org.w3c.dom.Node;
|
|
import org.w3c.dom.NodeList;
|
|
|
|
public class AppPrincipal {
|
|
public static void main(String[] args) {
|
|
AppPrincipal app = new AppPrincipal();
|
|
|
|
// Se carga el árbol DOM que más tarde se usará para las consultas.
|
|
Document documentoDOM = app.cargarArbolDOMFicheroXML();
|
|
|
|
Scanner sc = new Scanner(System.in);
|
|
int opcion;
|
|
|
|
do {
|
|
System.out.println("Elige una opción:");
|
|
System.out.println("1. Buscar información instituto");
|
|
System.out.println("2. Buscar información ciclos");
|
|
System.out.println("3. Buscar información módulos - filtro 1");
|
|
System.out.println("4. Buscar información módulos - filtro 2");
|
|
|
|
opcion = sc.nextInt();
|
|
|
|
if (opcion < 1 || opcion > 4) {
|
|
System.out.println("Opción no válida. Inténtalo de nuevo.");
|
|
}
|
|
switch (opcion) {
|
|
case 1:
|
|
app.buscarInformacionInstituto(documentoDOM);
|
|
break;
|
|
case 2:
|
|
app.buscarInformacionCiclos(documentoDOM);
|
|
break;
|
|
case 3:
|
|
app.buscarInformacionModulos(documentoDOM);
|
|
break;
|
|
case 4:
|
|
app.buscarInformacionModulosCiclo(documentoDOM);
|
|
break;
|
|
}
|
|
|
|
} while (opcion < 1 || opcion > 4);
|
|
|
|
System.out.println("Has elegido la opción " + opcion);
|
|
|
|
sc.close();
|
|
}
|
|
|
|
private void buscarInformacionInstituto(Document docDOM) {
|
|
Element elementoInstituto = (Element) docDOM.getElementsByTagName("instituto").item(0);
|
|
|
|
String nombre = elementoInstituto.getAttribute("nombre");
|
|
String tipo = elementoInstituto.getAttribute("tipo");
|
|
|
|
System.out.println("Instituto - Nombre: " + nombre + " - Tipo: " + tipo);
|
|
|
|
}
|
|
|
|
private void buscarInformacionCiclos(Document docDOM) {
|
|
NodeList nodosCiclo = docDOM.getElementsByTagName("ciclo");
|
|
for (int i = 0; i < nodosCiclo.getLength(); i++) {
|
|
Node nodoCiclo = nodosCiclo.item(i);
|
|
|
|
if (nodoCiclo.getNodeType() == Node.ELEMENT_NODE) {
|
|
Element elementoCiclo = (Element) nodoCiclo;
|
|
|
|
String codigo = elementoCiclo.getAttribute("codigo");
|
|
String nombre = elementoCiclo.getElementsByTagName("nombre_corto").item(0).getTextContent();
|
|
|
|
System.out.println("Ciclo - Nombre: " + nombre + " - Código: " + codigo);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void buscarInformacionModulos(Document docDOM) {
|
|
|
|
NodeList nodosModulo = docDOM.getElementsByTagName("modulo");
|
|
for (int i = 0; i < nodosModulo.getLength(); i++) {
|
|
Node nodoModulo = nodosModulo.item(i);
|
|
|
|
if (nodoModulo.getNodeType() == Node.ELEMENT_NODE) {
|
|
Element elementoModulo = (Element) nodoModulo;
|
|
|
|
String identificador = elementoModulo.getAttribute("id");
|
|
String referencia = elementoModulo.getAttribute("ref");
|
|
String titulo = elementoModulo.getElementsByTagName("titulo").item(0).getTextContent();
|
|
String numerohoras = elementoModulo.getElementsByTagName("numerohoras").item(0).getTextContent();
|
|
|
|
if (titulo != null && titulo.trim().startsWith("Programa")) {
|
|
|
|
System.out.println("Módulo - Título: " + titulo + " - Identificador: " + identificador
|
|
+ " - Referencia: " + referencia + " - Número horas: " + numerohoras);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void buscarInformacionModulosCiclo(Document docDOM) {
|
|
NodeList nodosModulo = docDOM.getElementsByTagName("modulo");
|
|
for (int i = 0; i < nodosModulo.getLength(); i++) {
|
|
Node nodoModulo = nodosModulo.item(i);
|
|
|
|
// Obtener el nodo padre del módulo --> el nodo curso
|
|
Node nodoCurso = nodoModulo.getParentNode();
|
|
String numero = null;
|
|
if (nodoCurso.getNodeType() == Node.ELEMENT_NODE) {
|
|
Element elementoCurso = (Element) nodoCurso;
|
|
numero = elementoCurso.getAttribute("numero");
|
|
}
|
|
|
|
// Saltar los módulos que no pertenecen al curso 1
|
|
if (numero == null || !"1".equals(numero)) {
|
|
continue;
|
|
}
|
|
|
|
// Mostrar info de los módulos que pertenecen al curso 1
|
|
if (nodoModulo.getNodeType() == Node.ELEMENT_NODE) {
|
|
Element elementoModulo = (Element) nodoModulo;
|
|
|
|
String identificador = elementoModulo.getAttribute("id");
|
|
String referencia = elementoModulo.getAttribute("ref");
|
|
String titulo = elementoModulo.getElementsByTagName("titulo").item(0).getTextContent();
|
|
String numerohoras = elementoModulo.getElementsByTagName("numerohoras").item(0).getTextContent();
|
|
|
|
if (titulo != null && titulo.trim().startsWith("Programa")) {
|
|
|
|
System.out.println("Módulo - Título: " + titulo + " - Identificador: " + identificador
|
|
+ " - Referencia: " + referencia + " - Número horas: " + numerohoras);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private Document cargarArbolDOMFicheroXML() {
|
|
Document document = null;
|
|
|
|
try {
|
|
// 1. Crear la fábrica
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
|
// 2. Crear el parser que se usará para obtener el árbol DOM
|
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
|
|
|
// 3. Leer el fichero XML y lo parseamos para obtener el árbol DOM
|
|
File fichero = new File(GestorConfiguracion.getRutaFicheroIES());
|
|
document = builder.parse(fichero);
|
|
|
|
// 4. Obtener el documento raíz normalizado
|
|
document.getDocumentElement().normalize();
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return document;
|
|
}
|
|
|
|
}
|