Actividad 1.11
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
||||
# Eclipse
|
||||
.classpath
|
||||
.project
|
||||
.settings/
|
||||
|
||||
# Compilación Java
|
||||
bin/
|
||||
target/
|
||||
*.class
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
|
||||
# Sistema operativo
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
@@ -0,0 +1 @@
|
||||
fichero.ruta.instituto=C:/Users/alumnoT/aadd/fic11/informacion-IES.xml
|
||||
@@ -0,0 +1,172 @@
|
||||
package org.palomafp.aadd.jx;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import org.palomafp.aadd.jx.excepciones.TecnicoException;
|
||||
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 GestorInstitutoXML {
|
||||
|
||||
private static final String PREFIJO_MODULO = "Programa";
|
||||
|
||||
public static void main(String[] args) throws TecnicoException {
|
||||
|
||||
Document documento = cargarDocumento();
|
||||
|
||||
if (documento == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Scanner teclado = new Scanner(System.in);
|
||||
String opcion = "";
|
||||
|
||||
do {
|
||||
mostrarMenu();
|
||||
opcion = teclado.nextLine().trim();
|
||||
|
||||
switch (opcion) {
|
||||
case "1":
|
||||
imprimirInstituto(documento);
|
||||
break;
|
||||
|
||||
case "2":
|
||||
imprimirCiclos(documento);
|
||||
break;
|
||||
|
||||
case "3":
|
||||
imprimirModulos(documento, PREFIJO_MODULO, null);
|
||||
break;
|
||||
|
||||
case "4":
|
||||
imprimirModulos(documento, PREFIJO_MODULO, "1");
|
||||
break;
|
||||
|
||||
case "0":
|
||||
System.out.println("Fin del programa.");
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Opción no válida.");
|
||||
}
|
||||
|
||||
} while (!opcion.equals("0"));
|
||||
|
||||
teclado.close();
|
||||
}
|
||||
|
||||
private static void mostrarMenu() {
|
||||
System.out.println();
|
||||
System.out.println("===== INFORMACIÓN IES =====");
|
||||
System.out.println("1. Imprimir instituto");
|
||||
System.out.println("2. Imprimir ciclos");
|
||||
System.out.println("3. Imprimir módulos filtro 1");
|
||||
System.out.println("4. Imprimir módulos filtro 2");
|
||||
System.out.println("0. Salir");
|
||||
System.out.print("Seleccione una opción: ");
|
||||
}
|
||||
|
||||
private static Document cargarDocumento() throws TecnicoException {
|
||||
Document documento = null;
|
||||
String ruta = GestorPropiedad.get("fichero.ruta.instituto");
|
||||
|
||||
if (ruta == null) {
|
||||
System.out.println("No se ha encontrado la propiedad fichero.ruta.instituto");
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder constructor = factory.newDocumentBuilder();
|
||||
documento = constructor.parse(new File(ruta));
|
||||
documento.getDocumentElement().normalize();
|
||||
|
||||
} catch (ParserConfigurationException | SAXException | IOException e) {
|
||||
System.out.println("Error leyendo el fichero XML: " + e.getMessage());
|
||||
}
|
||||
|
||||
return documento;
|
||||
}
|
||||
|
||||
private static void imprimirInstituto(Document documento) {
|
||||
Element instituto = documento.getDocumentElement();
|
||||
|
||||
String nombre = instituto.getAttribute("nombre");
|
||||
String tipo = instituto.getAttribute("tipo");
|
||||
|
||||
System.out.println("Instituto: " + nombre + " (Tipo: " + tipo + ")");
|
||||
}
|
||||
|
||||
private static void imprimirCiclos(Document documento) {
|
||||
NodeList listaCiclos = documento.getElementsByTagName("ciclo");
|
||||
|
||||
for (int i = 0; i < listaCiclos.getLength(); i++) {
|
||||
Node nodoCiclo = listaCiclos.item(i);
|
||||
|
||||
if (nodoCiclo.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element ciclo = (Element) nodoCiclo;
|
||||
|
||||
String codigo = ciclo.getAttribute("codigo");
|
||||
String nombreCorto = obtenerTexto(ciclo, "nombre_corto");
|
||||
|
||||
System.out.println("Ciclo: " + codigo + " - " + nombreCorto);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void imprimirModulos(Document documento, String prefijo, String numeroCurso) {
|
||||
NodeList listaCiclos = documento.getElementsByTagName("ciclo");
|
||||
|
||||
for (int i = 0; i < listaCiclos.getLength(); i++) {
|
||||
Element ciclo = (Element) listaCiclos.item(i);
|
||||
String nombreCorto = obtenerTexto(ciclo, "nombre_corto");
|
||||
|
||||
NodeList listaCursos = ciclo.getElementsByTagName("curso");
|
||||
|
||||
for (int j = 0; j < listaCursos.getLength(); j++) {
|
||||
Element curso = (Element) listaCursos.item(j);
|
||||
String numero = curso.getAttribute("numero");
|
||||
|
||||
if (numeroCurso != null && !numeroCurso.equals(numero)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
NodeList listaModulos = curso.getElementsByTagName("modulo");
|
||||
|
||||
for (int k = 0; k < listaModulos.getLength(); k++) {
|
||||
Element modulo = (Element) listaModulos.item(k);
|
||||
String titulo = obtenerTexto(modulo, "titulo");
|
||||
|
||||
if (titulo.startsWith(prefijo)) {
|
||||
String id = modulo.getAttribute("id");
|
||||
String ref = modulo.getAttribute("ref");
|
||||
String horas = obtenerTexto(modulo, "numerohoras");
|
||||
|
||||
System.out.println("[" + nombreCorto + " - Curso " + numero + "] "
|
||||
+ "Id: " + id + ", Ref: " + ref
|
||||
+ ", Título: " + titulo + ", Horas: " + horas);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String obtenerTexto(Element padre, String etiqueta) {
|
||||
NodeList nodos = padre.getElementsByTagName(etiqueta);
|
||||
|
||||
if (nodos.getLength() == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return nodos.item(0).getTextContent().trim();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.palomafp.aadd.jx;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.palomafp.aadd.jx.excepciones.TecnicoException;
|
||||
|
||||
public class GestorPropiedad {
|
||||
|
||||
private static final Properties propiedades = new Properties();
|
||||
private static boolean cargado = false;
|
||||
|
||||
private static void cargar() throws TecnicoException {
|
||||
Path ruta = Paths.get("config", "application.properties");
|
||||
|
||||
try (BufferedReader entrada = Files.newBufferedReader(ruta, StandardCharsets.ISO_8859_1)) {
|
||||
propiedades.load(entrada);
|
||||
cargado = true;
|
||||
} catch (IOException e) {
|
||||
throw new TecnicoException("Error al cargar el archivo de propiedades: " + ruta, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String get(String clave) throws TecnicoException {
|
||||
if (!cargado) {
|
||||
cargar();
|
||||
}
|
||||
String valor = propiedades.getProperty(clave);
|
||||
if (valor == null) {
|
||||
throw new TecnicoException("No existe la propiedad: " + clave);
|
||||
}
|
||||
return valor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.palomafp.aadd.jx.excepciones;
|
||||
|
||||
/**
|
||||
*
|
||||
* NegocioException: Clase que representa la excepción técnica ocurrida durante el acceso a datos del proyecto MapaMundi
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 15 oct 2026
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
public class AccesoDatosException extends TecnicoException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public AccesoDatosException(String mensaje) {
|
||||
super(mensaje);
|
||||
}
|
||||
public AccesoDatosException(String mensaje, Throwable causa) {
|
||||
super(mensaje, causa);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.palomafp.aadd.jx.excepciones;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* MapaMundoException: Clase abstracta que representa la excepción base del proyecto MapaMundi
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 15 oct 2026
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
public abstract class MapaMundiException extends Exception {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// Constructor protegido en clase abstracta
|
||||
// Conseguimos que esta clase no se puede "instanciar directamente", solo a través de sus subclases.
|
||||
protected MapaMundiException(String mensaje) {
|
||||
super(mensaje);
|
||||
}
|
||||
|
||||
// Constructor protegido en clase abstracta
|
||||
protected MapaMundiException(String mensaje, Throwable causa) {
|
||||
super(mensaje, causa);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.palomafp.aadd.jx.excepciones;
|
||||
|
||||
/**
|
||||
*
|
||||
* NegocioException: Clase que representa la excepción de negocio del proyecto MapaMundi
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 15 oct 2026
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
public class NegocioException extends MapaMundiException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// Constructor con mensaje como parámetro.
|
||||
public NegocioException(String mensaje) {
|
||||
super(mensaje);
|
||||
}
|
||||
|
||||
// No se incluye el constructor con Throwable; se asume que una excepción de negocio no tiene asociada una excepción.
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.palomafp.aadd.jx.excepciones;
|
||||
|
||||
/**
|
||||
*
|
||||
* NegocioException: Clase que representa la excepción técnica del proyecto MapaMundi
|
||||
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||
* @date 15 oct 2026
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
public class TecnicoException extends MapaMundiException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// Error de tipo "técnico" que no está asociado a una excepción concreta.
|
||||
public TecnicoException(String mensaje) {
|
||||
super(mensaje);
|
||||
}
|
||||
|
||||
public TecnicoException(String mensaje, Throwable causa) {
|
||||
super(mensaje, causa);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user