ShowMeTheBitsServlet.java 6.27 KB
Newer Older
Matias Ferreira committed
1 2 3 4 5 6 7 8 9 10 11 12 13


package com.roshka.webprojecttest.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

14 15
@WebServlet("/show-me-the-bits")
public class ShowMeTheBitsServlet extends HttpServlet {
Matias Ferreira committed
16 17 18 19 20 21 22
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		PrintWriter pw = new PrintWriter(resp.getOutputStream());
		Boolean valido=true;
		String parametro =  req.getParameter("number");
		Integer [] vectorResultado = new Integer [16];
23 24
		if(parametro == null) {
			initForm(pw);
Matias Ferreira committed
25 26
		}
		else {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
			String tipo="";
			if(parametro.length() < 1)
				valido=false;
			else if(parametro.length() >1 ) {
				if(parametro.charAt(0) == '0') {
					if(parametro.charAt(1) == 'x')
						tipo = "hexadecimal";
					else
						tipo = "octal";;
				}else
					tipo="decimal";
			}else
				tipo= "decimal";;
				
				if(tipo.equals("decimal")) {
					for ( int i = 0 ; i< parametro.length(); i++)
						if(!Character.isDigit(parametro.charAt(i)))
							valido = false;
Matias Ferreira committed
45
				}
46 47 48 49
				else if(tipo.equals("octal")) {
					for ( int i = 1 ; i< parametro.length(); i++)
						if(!Character.isDigit(parametro.charAt(i)))
							valido = false;
Matias Ferreira committed
50
				}
51 52 53 54
				else if(tipo.equals("hexadecimal")) {
					for ( int i = 2 ; i < parametro.length(); i++ )
						if( !isHexaDigit(parametro.charAt(i) ) )
							valido = false;
Matias Ferreira committed
55
				}
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
				int validAux = 0;
				if(valido) {
					if(tipo.equals("decimal") ) 
						validAux = Integer.parseInt(parametro,10);  
					else if( tipo.equals("hexadecimal") )
						validAux = Integer.parseInt(parametro.substring(2),16);
					else
						validAux = Integer.parseInt(parametro.substring(1),8);
				}
				if(validAux > 65535)
					valido = false;
				if (!valido) {
					invalido(pw, parametro);
				}
				else {
					transformacion(tipo, parametro, vectorResultado);
					theTable(pw, vectorResultado);
Matias Ferreira committed
73 74 75 76 77
				}
			
		}
		pw.close();
	}
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	
	void initForm(PrintWriter pw) {
		pw.write("<html>"
				+ "<head>"
				+ "<title>Hola, bienvenidos al show me the bits</title>"
				+ "</head>"
				+ "<body>"
				+ "<h1>Soy un conversor sistemas de numeracion</h1>"
				+ "<div>"
				+ "<form action=\"show-me-the-bits\"  >"
				+ "Introduzca el numero: <input type=\"text\"  name=number><br>"
				+ "<input type=\"submit\">"
				+ "</form>"
				+ "</div>"
				+ "</body>"
				+ "</html>");
	}
	
	void theTable(PrintWriter pw, Integer [] vectorResultado) {
		pw.write("<html>"
				+ "<head>"
				+ "<title>Hola, bienvenidos al conversor de numeros</title>"
				+ "</head>"
				+ "<body>"
				+ "<h1>Soy un conversor de sistemas de numeracion</h1>"
				+ "<div>"
				+ "<form action=\"show-me-the-bits\"  >"
				+ "Introduzca el numero: <input type=\"text\"  name=number><br>"
				+ "<input type=\"submit\">"
				+ "</form>"
				+ "</div>"
				+ "<table>" 
				+ "<tr>");
		for(int i = 15; i >= 0 ; i--) {
			if(vectorResultado[i] == 1) {
				pw.write("<th style=\"color: gold ; background-color:darkblue;\">" + vectorResultado[i]+"</th>");
				
			}
			else {
				pw.write("<th style=\"color:fuchsia ; background-color:darkblue;\">"+vectorResultado[i]+"</th>");
			}
			if(i == 8) {
				pw.write("<th> === </th>");
			}
			else if(i == 0) {
				pw.write("</tr>");
			}
			else
				pw.write("<th> | </th>");
				
		}
		pw.write( "</table>"

				+ "</body>"
				+ "</html>");	
	}
	
	void invalido(PrintWriter pw, String parametro) {
		pw.write("<html>"
				+ "<head>"
				+ "<title>Error de parametro</title>"
				+ "</head>"
				+ "<body>"
				+ "No se puede generarla tabla de bits. Valor del parametro \"" + parametro + "\" invalido."
				+ "</body>"
				+ "</html>");
	}
Matias Ferreira committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

	public void transformacion(String tipo, String valor, Integer [] resultado) {
		if(tipo.equals("decimal")) {
			Integer numero = Integer.parseInt(valor);
			int [] vectorAux = new int[16];
			vectorAux[0] = 1;
			for(int i = 1 ; i < 16 ;i++) {
				vectorAux[i] = vectorAux[i-1]*2;
			}
			for(int i = 15 ; i > -1 ; i--) {
				if(numero >= vectorAux[i]) {
					resultado[i]=1;
					numero%=vectorAux[i];
				}
				else {
					resultado[i] = 0;
				}
			}
		}
		
		else if(tipo.equals("hexadecimal")  ) {
			String cadenaAux = "";
			int k = 0;
			for (int i = valor.length()-1 ; i > 1 ; i--){
				cadenaAux = hexaToBin(valor.charAt(i));
				for(int j = 3 ; j > -1 ; j--) {
					resultado[k++]=Integer.parseInt(""+cadenaAux.charAt(j));
				}
			}
			while(k<16)
				resultado[k++]=0;
		}
		else if( tipo.equals("octal")  ) {
			String cadenaAux = "";
			int k = 0;
			for (int i = valor.length()-1 ; i > 0 ; i--){
				cadenaAux = octalToBin(valor.charAt(i));
				for(int j = 2 ; j > -1; j--) {
					resultado[k++]=Integer.parseInt(""+cadenaAux.charAt(j));
				}
			}
			while(k<16)
				resultado[k++]=0;
		}
	}
	public String hexaToBin(char caracter) {
		if(caracter == 'F')
			return "1111";
		if(caracter == 'E')
			return "1110";
		if(caracter == 'D')
			return "1101";
		if(caracter == 'C')
			return "1100";
		if(caracter == 'B')
			return "1011";
		if(caracter == 'A')
			return "1010";
		if(caracter == '9')
			return "1001";
		if(caracter == '8')
			return "1000";
		if(caracter == '7')
			return "0111";
		if(caracter == '6')
			return "0110";
		if(caracter == '5')
			return "0101";
		if(caracter == '4')
			return "0100";
		if(caracter == '3')
			return "0011";
		if(caracter == '2')
			return "0010";
		if(caracter == '1')
			return "0001";
		if(caracter == '0')
			return "0000";
		return "";
		
	}
	public String octalToBin(char caracter) {
		if(caracter == '7')
			return "111";
		if(caracter == '6')
			return "110";
		if(caracter == '5')
			return "101";
		if(caracter == '4')
			return "100";
		if(caracter == '3')
			return "011";
		if(caracter == '2')
			return "010";
		if(caracter == '1')
			return "001";
		if(caracter == '0')
			return "000";
		return "";
	}
	
	public Boolean isHexaDigit(char caracter) {
		if( caracter == '0' ||caracter == '1' ||caracter == '2' ||caracter == '3' ||
			caracter == '4' ||caracter == '5' ||caracter == '6' ||caracter == '7' ||
			caracter == '8' ||caracter == '9' ||caracter == 'A' ||caracter == 'B' ||
			caracter == 'C' ||caracter == 'D' ||caracter == 'E' ||caracter == 'F' ) {
			return true;
		}
		else
			return false;
	}
}