Skip to content

Commit 81f8aec

Browse files
committed
New version.
1 parent 7613bc3 commit 81f8aec

19 files changed

+436
-51
lines changed

pom.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<modelVersion>4.0.0</modelVersion>
2424
<groupId>br.usp.poli.lta.cereda</groupId>
2525
<artifactId>xml2aa</artifactId>
26-
<version>1.0</version>
26+
<version>1.1</version>
2727
<packaging>jar</packaging>
2828
<dependencies>
2929
<dependency>
@@ -32,9 +32,14 @@
3232
<version>1.4.9</version>
3333
</dependency>
3434
<dependency>
35-
<groupId>${project.groupId}</groupId>
36-
<artifactId>aa</artifactId>
37-
<version>${project.version}</version>
35+
<groupId>br.usp.poli.lta.cereda</groupId>
36+
<artifactId>aa-dot</artifactId>
37+
<version>1.0</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.apache.commons</groupId>
41+
<artifactId>commons-exec</artifactId>
42+
<version>1.3</version>
3843
</dependency>
3944
</dependencies>
4045
<properties>
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/**
2+
* ------------------------------------------------------
3+
* Laboratório de Linguagens e Técnicas Adaptativas
4+
* Escola Politécnica, Universidade São Paulo
5+
* ------------------------------------------------------
6+
*
7+
* This program is free software: you can redistribute it
8+
* and/or modify it under the terms of the GNU General
9+
* Public License as published by the Free Software
10+
* Foundation, either version 3 of the License, or (at
11+
* your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will
14+
* be useful, but WITHOUT ANY WARRANTY; without even the
15+
* implied warranty of MERCHANTABILITY or FITNESS FOR A
16+
* PARTICULAR PURPOSE. See the GNU General Public License
17+
* for more details.
18+
*
19+
*
20+
*/
21+
package br.usp.poli.lta.cereda.xml2aa;
22+
23+
import java.awt.BorderLayout;
24+
import java.awt.Color;
25+
import java.awt.Dimension;
26+
import java.awt.Image;
27+
import java.io.ByteArrayInputStream;
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.IOException;
30+
import java.io.UnsupportedEncodingException;
31+
import javax.swing.ImageIcon;
32+
import javax.swing.JFrame;
33+
import javax.swing.JLabel;
34+
import org.apache.commons.exec.CommandLine;
35+
import org.apache.commons.exec.DefaultExecutor;
36+
import org.apache.commons.exec.Executor;
37+
import org.apache.commons.exec.PumpStreamHandler;
38+
39+
/**
40+
* Exibe a imagem correspondente ao código-fonte em formato DOT.
41+
*
42+
* @author Paulo Roberto Massa Cereda
43+
* @version 1.1
44+
* @since 1.0
45+
*/
46+
public class Dot extends JFrame {
47+
48+
// rótulo que conterá a imagem
49+
// gerada pelo programa 'dot'
50+
private final JLabel lblImage;
51+
52+
// medidas máximas, em pixels,
53+
// da altura e largura da imagem
54+
private static final int MAX_HEIGHT = 700;
55+
private static final int MAX_WIDTH = 700;
56+
57+
// medida, em pixels, para o espaçamento
58+
// entre a imagem e a borda da janela
59+
private static final int PAD = 50;
60+
61+
/**
62+
* Construtor.
63+
*
64+
* @param value Código-fonte do código DOT a ser gerado.
65+
* @throws UnsupportedEncodingException Codificação não suportada.
66+
* @throws IOException Erro de entrada e saída.
67+
*/
68+
public Dot(String value) throws UnsupportedEncodingException, IOException {
69+
70+
// define o título da janela
71+
super("Visualização do autômato");
72+
73+
// define a operação padrão
74+
// quando a janela é fechada
75+
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
76+
77+
// define o layout da janela
78+
setLayout(new BorderLayout());
79+
80+
// define o plano de fundo da
81+
// janela na cor branca
82+
getContentPane().setBackground(Color.WHITE);
83+
84+
// define um novo rótulo que
85+
// conterá a imagem a ser gerada
86+
lblImage = new JLabel();
87+
88+
// define o alinhamento do rótulo
89+
// para que a imagem fique centralizada
90+
lblImage.setHorizontalAlignment(JLabel.CENTER);
91+
92+
// adiciona o componente
93+
// na janela corrente
94+
add(lblImage, BorderLayout.CENTER);
95+
96+
// define um novo comando,
97+
// referenciando o programa 'dot'
98+
CommandLine command = new CommandLine("dot");
99+
100+
// define o parâmetro do programa,
101+
// neste caso, a geração será em
102+
// formato PNG
103+
command.addArgument("-Tpng");
104+
105+
// define o formato a ser submetido
106+
// ao programa, neste caso, a representação
107+
// em bytes do código-fonte informado
108+
ByteArrayInputStream input = new ByteArrayInputStream(value.
109+
getBytes("UTF-8"));
110+
111+
// cria um fluxo de saída e tenta
112+
// obter a imagem correspondente
113+
// à execução do programa 'dot'
114+
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
115+
116+
// cria um novo executor
117+
Executor executor = new DefaultExecutor();
118+
119+
// define o manipulador de fluxos, informando
120+
// os fluxos de saída e entrada correspondentes
121+
executor.setStreamHandler(new PumpStreamHandler(output,
122+
null, input));
123+
124+
// executa o comando 'dot'
125+
// propriamente dito
126+
executor.execute(command);
127+
128+
// cria uma imagem temporária
129+
// com o fluxo de bytes obtidos
130+
// a partir da execução de 'dot'
131+
ImageIcon image = new ImageIcon(output.toByteArray());
132+
133+
// altura e largura da
134+
// imagem são obtidas
135+
int height = image.getIconHeight();
136+
int width = image.getIconWidth();
137+
138+
// novos valores para
139+
// a altura e largura
140+
int newheight;
141+
int newwidth;
142+
143+
// realiza cálculos de redimensionamento,
144+
// caso a imagem obtida seja maior do que
145+
// as dimensões máximas definidas
146+
if (height > width) {
147+
148+
// define a nova altura e calcula
149+
// a largura correspondente
150+
newheight = height > MAX_HEIGHT ? MAX_HEIGHT : height;
151+
newwidth = newheight * width / height;
152+
153+
} else {
154+
155+
// define a nova largura e calcula
156+
// a altura correspondente
157+
newwidth = width > MAX_WIDTH ? MAX_WIDTH : width;
158+
newheight = newwidth * height / width;
159+
160+
}
161+
162+
// define o novo tamanho da janela
163+
// de acordo com os novos valores
164+
// calculados anteriormente
165+
setPreferredSize(new Dimension(newwidth + PAD, newheight + PAD));
166+
167+
// define a nova imagem, devidamente
168+
// redimensionada, como exibição do
169+
// rótulo da janela corrente
170+
lblImage.setIcon(new ImageIcon(image.getImage().
171+
getScaledInstance(newwidth,
172+
newheight, Image.SCALE_SMOOTH)));
173+
174+
}
175+
176+
// empacota a janela,
177+
// ajustando os componentes
178+
pack();
179+
180+
// centraliza a posição da janela
181+
// em relação à tela do usuário
182+
setLocationRelativeTo(null);
183+
184+
}
185+
186+
/**
187+
* Verifica se o programa 'dot' existe.
188+
*
189+
* @return Valor lógico indicando se o programa 'dot' existe.
190+
*/
191+
public static boolean exists() {
192+
193+
try {
194+
195+
// cria um novo comando
196+
CommandLine command = new CommandLine("dot");
197+
198+
// define um novo executor
199+
Executor executor = new DefaultExecutor();
200+
201+
// executa o comando
202+
executor.execute(command);
203+
204+
// o comando foi executado com
205+
// sucesso, indicando que este
206+
// existe no caminho do sistema
207+
return true;
208+
209+
} catch (IOException nothandled) {
210+
211+
// no caso de uma exceção de
212+
// entrada e saída, o comando
213+
// 'dot' não existe
214+
return false;
215+
}
216+
}
217+
218+
}

0 commit comments

Comments
 (0)