Skip to content

Commit 438cf4c

Browse files
committed
Commit inicial de jaxmlDixitAll
Totalmente funcional
0 parents  commit 438cf4c

File tree

4 files changed

+308
-0
lines changed

4 files changed

+308
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Luís Vázquez Lema
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# jxmlDixitAll
2+
3+
## Resumen
4+
5+
jxmlDixitAll es una aplicación Java con interfaz gráfica de usuario diseñada para gestionar un diccionario de términos y sus definiciones. Proporciona funcionalidades para agregar, modificar, eliminar y consultar términos y sus definiciones asociadas. Es una versión con la capacidad de acceso a datos en ficheros con formato XML, de tal manera que pueda ser importado y exportado fácilmente.
6+
7+
## Características
8+
9+
- **Agregar Término y Definición:** Permite agregar fácilmente nuevos términos junto con sus definiciones.
10+
- **Modificar Término:** Modificar la definición de términos existentes.
11+
- **Eliminar Término:** Eliminar términos del diccionario.
12+
- **Consultar Término:** Buscar definiciones de términos específicos.
13+
14+
## Instalación
15+
16+
Para usar jDixitAll, necesitas tener Java instalado en tu sistema. Puedes descargar el código fuente y compilarlo utilizando cualquier IDE de Java o compilarlo directamente desde la línea de comandos.
17+
18+
1. Clona el repositorio:
19+
20+
```bash
21+
git clone https://github.com/luisvazle/jxmlDixitAll.git
22+
```
23+
2. Compila el código fuente:
24+
25+
```bash
26+
javac src/jxmlDixitAll/jxmlDixitAll.java
27+
```
28+
3. Ejecuta la aplicación:
29+
30+
```bash
31+
java src.jxmlDixitAll.jxmlDixitAll
32+
```
33+
34+
## Uso
35+
36+
Al ejecutar la aplicación, se mostrará una interfaz de usuario gráfica donde puedes realizar varias acciones:
37+
38+
- **Agregar:** Ingresa un término y su definición, luego haz clic en "Guardar" para agregarlo al diccionario.
39+
- **Modificar:** Ingresa el término cuya definición quieres modificar, actualiza la definición y haz clic en "Modificar".
40+
- **Eliminar:** Ingresa el término que deseas eliminar y haz clic en "Eliminar".
41+
- **Consultar:** Ingresa el término que deseas buscar y haz clic en "Consultar" para ver su definición.
42+
43+
La primera vez que se introduzca un término, se creará una carpeta con el archivo `datos.xml`. En cualquier uso posterior, este se actualizará.
44+
45+
## Autor
46+
47+
El autor de este programa es Luís Vázquez Lema. Puedes contactar con el autor enviando un correo electrónico a la siguiente dirección: <[email protected]>
48+
49+
## Licencia
50+
51+
Se adjunta una copia de la licencia MIT aplicable si clona el repositorio: sería el fichero `LICENSE`. Asimismo, en las primeras líneas comentadas de cada fichero `.java` también podrá visualizar el contenido de la licencia.

src/jxmlDixitAll/datos/datos.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<Lista>
3+
<Término>
4+
<Clave>Luís Vázquez Lema</Clave>
5+
<Valor>Creador de esta aplicación.</Valor>
6+
</Término>
7+
</Lista>

src/jxmlDixitAll/jxmlDixitAll.java

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2024 Luí­s Vázquez Lema
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
27+
package jxmlDixitAll;
28+
29+
import java.io.*;
30+
import javax.swing.*;
31+
import java.awt.*;
32+
import java.util.*;
33+
import javax.xml.parsers.*;
34+
import javax.xml.transform.*;
35+
import javax.xml.transform.dom.*;
36+
import javax.xml.transform.stream.*;
37+
import org.w3c.dom.*;
38+
39+
/**
40+
*
41+
* @author Luís Vázquez Lema
42+
*/
43+
44+
public class jxmlDixitAll extends JFrame {
45+
private HashMap<String, String> datos;
46+
private JTextField claveEntry, consultarEntry;
47+
private JTextArea valorEntry, salida;
48+
private JList<String> lista;
49+
private DefaultListModel<String> listModel;
50+
51+
public jxmlDixitAll() {
52+
super("jxmlDixitAll");
53+
verificarCarpetaDatos();
54+
cargarDatos();
55+
56+
setLayout(new FlowLayout());
57+
58+
add(new JLabel("Término:"));
59+
claveEntry = new JTextField(20);
60+
add(claveEntry);
61+
62+
add(new JLabel("Definición:"));
63+
valorEntry = new JTextArea(10, 50);
64+
valorEntry.setWrapStyleWord(true);
65+
valorEntry.setLineWrap(true);
66+
JScrollPane valorScroll = new JScrollPane(valorEntry);
67+
add(valorScroll);
68+
69+
JButton guardarButton = new JButton("Guardar");
70+
guardarButton.addActionListener(e -> guardar());
71+
add(guardarButton);
72+
73+
JButton modificarButton = new JButton("Modificar");
74+
modificarButton.addActionListener(e -> modificar());
75+
add(modificarButton);
76+
77+
JButton eliminarButton = new JButton("Eliminar");
78+
eliminarButton.addActionListener(e -> eliminar());
79+
add(eliminarButton);
80+
81+
add(new JLabel("Término a consultar:"));
82+
consultarEntry = new JTextField(20);
83+
add(consultarEntry);
84+
85+
JButton consultarButton = new JButton("Consultar");
86+
consultarButton.addActionListener(e -> consultar());
87+
add(consultarButton);
88+
89+
add(new JLabel("Definición:"));
90+
salida = new JTextArea(10, 50);
91+
salida.setEditable(false);
92+
salida.setWrapStyleWord(true);
93+
salida.setLineWrap(true);
94+
JScrollPane salidaScroll = new JScrollPane(salida);
95+
add(salidaScroll);
96+
97+
add(new JLabel("Términos almacenados:"));
98+
listModel = new DefaultListModel<>();
99+
lista = new JList<>(listModel);
100+
JScrollPane listaScroll = new JScrollPane(lista);
101+
listaScroll.setPreferredSize(new Dimension(500, 180));
102+
add(listaScroll);
103+
104+
actualizarLista();
105+
setSize(600, 800);
106+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
107+
setVisible(true);
108+
}
109+
110+
private void verificarCarpetaDatos() {
111+
File dir = new File("src/jxmlDixitAll/datos");
112+
if (!dir.exists()) {
113+
dir.mkdirs();
114+
}
115+
}
116+
117+
private void cargarDatos() {
118+
File xmlFile = new File("src/jxmlDixitAll/datos/datos.xml");
119+
if (!xmlFile.exists()) {
120+
datos = new HashMap<>();
121+
return;
122+
}
123+
124+
try {
125+
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
126+
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
127+
Document doc = dBuilder.parse(xmlFile);
128+
doc.getDocumentElement().normalize();
129+
130+
datos = new HashMap<>();
131+
NodeList nList = doc.getElementsByTagName("Término");
132+
133+
for (int i = 0; i < nList.getLength(); i++) {
134+
Node nNode = nList.item(i);
135+
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
136+
Element eElement = (Element) nNode;
137+
String clave = eElement.getElementsByTagName("Clave").item(0).getTextContent();
138+
String valor = eElement.getElementsByTagName("Valor").item(0).getTextContent();
139+
datos.put(clave, valor);
140+
}
141+
}
142+
} catch (Exception e) {
143+
e.printStackTrace();
144+
}
145+
}
146+
147+
private void guardar() {
148+
String clave = claveEntry.getText();
149+
String valor = valorEntry.getText();
150+
datos.put(clave, valor);
151+
guardarDatos();
152+
actualizarLista();
153+
}
154+
155+
private void modificar() {
156+
String clave = claveEntry.getText();
157+
String valor = valorEntry.getText();
158+
if (datos.containsKey(clave)) {
159+
datos.put(clave, valor);
160+
guardarDatos();
161+
actualizarLista();
162+
} else {
163+
salida.setText("El término no existe");
164+
}
165+
}
166+
167+
private void eliminar() {
168+
String clave = claveEntry.getText();
169+
if (datos.containsKey(clave)) {
170+
datos.remove(clave);
171+
guardarDatos();
172+
actualizarLista();
173+
} else {
174+
salida.setText("El término no existe");
175+
}
176+
}
177+
178+
private void consultar() {
179+
String clave = consultarEntry.getText();
180+
if (datos.containsKey(clave)) {
181+
salida.setText(datos.get(clave));
182+
} else {
183+
salida.setText("El término no existe");
184+
}
185+
}
186+
187+
private void guardarDatos() {
188+
try {
189+
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
190+
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
191+
Document doc = dBuilder.newDocument();
192+
193+
Element rootElement = doc.createElement("Lista");
194+
doc.appendChild(rootElement);
195+
196+
for (Map.Entry<String, String> entry : datos.entrySet()) {
197+
Element termino = doc.createElement("Término");
198+
rootElement.appendChild(termino);
199+
200+
Element clave = doc.createElement("Clave");
201+
clave.appendChild(doc.createTextNode(entry.getKey()));
202+
termino.appendChild(clave);
203+
204+
Element valor = doc.createElement("Valor");
205+
valor.appendChild(doc.createTextNode(entry.getValue()));
206+
termino.appendChild(valor);
207+
}
208+
209+
TransformerFactory transformerFactory = TransformerFactory.newInstance();
210+
Transformer transformer = transformerFactory.newTransformer();
211+
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
212+
DOMSource source = new DOMSource(doc);
213+
StreamResult result = new StreamResult(new File("src/jxmlDixitAll/datos/datos.xml"));
214+
215+
transformer.transform(source, result);
216+
} catch (Exception e) {
217+
e.printStackTrace();
218+
}
219+
}
220+
221+
private void actualizarLista() {
222+
listModel.clear();
223+
datos.keySet().stream().sorted().forEach(listModel::addElement);
224+
}
225+
226+
public static void main(String[] args) {
227+
new jxmlDixitAll();
228+
}
229+
}

0 commit comments

Comments
 (0)