Example usage for java.util Vector addElement

List of usage examples for java.util Vector addElement

Introduction

In this page you can find the example usage for java.util Vector addElement.

Prototype

public synchronized void addElement(E obj) 

Source Link

Document

Adds the specified component to the end of this vector, increasing its size by one.

Usage

From source file:com.duroty.application.mail.manager.MailManager.java

/**
 * DOCUMENT ME!//w ww . j  av  a2 s .  c o m
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param folderName DOCUMENT ME!
 * @param page DOCUMENT ME!
 * @param messagesByPage DOCUMENT ME!
 * @param order DOCUMENT ME!
 * @param orderType DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws MailException DOCUMENT ME!
 */
public Vector getMessages(Session hsession, String repositoryName, String folderName, int page,
        int messagesByPage, int order, String orderType) throws MailException {
    Vector messages = new Vector();

    try {
        Users user = getUser(hsession, repositoryName);
        Locale locale = new Locale(user.getUseLanguage());
        TimeZone timeZone = TimeZone.getDefault();

        Date now = new Date();
        Calendar calendar = Calendar.getInstance(timeZone, locale);
        calendar.setTime(now);

        SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd", locale);
        SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss", locale);
        SimpleDateFormat formatter3 = new SimpleDateFormat("MM/yy", locale);

        Criteria crit = hsession.createCriteria(Message.class);
        crit.add(Restrictions.eq("users", user));

        if (folderName.equals(this.folderAll) || folderName.equals(this.folderHidden)) {
            crit.add(Restrictions.not(Restrictions.in("mesBox",
                    new String[] { this.folderTrash, this.folderSpam, this.folderChat, FOLDER_DELETE })));
        } else if (folderName.equals(this.folderImportant)) {
            crit.add(Restrictions.eq("mesFlagged", new Boolean(true)));
        } else {
            crit.add(Restrictions.eq("mesBox", folderName));
        }

        switch (order) {
        case ORDER_BY_IMPORTANT:

            if (orderType.equals("ASC")) {
                crit.addOrder(Order.asc("mesFlagged"));
            } else {
                crit.addOrder(Order.desc("mesFlagged"));
            }

            break;

        case ORDER_BY_ADDRESS:

            if (orderType.equals("ASC")) {
                crit.addOrder(Order.asc("mesFrom"));
            } else {
                crit.addOrder(Order.desc("mesFrom"));
            }

            break;

        case ORDER_BY_SIZE:

            if (orderType.equals("ASC")) {
                crit.addOrder(Order.asc("mesSize"));
            } else {
                crit.addOrder(Order.desc("mesSize"));
            }

            break;

        case ORDER_BY_SUBJECT:

            if (orderType.equals("ASC")) {
                crit.addOrder(Order.asc("mesSubject"));
            } else {
                crit.addOrder(Order.desc("mesSubject"));
            }

            break;

        case ORDER_BY_DATE:

            if (orderType.equals("ASC")) {
                crit.addOrder(Order.asc("mesDate"));
            } else {
                crit.addOrder(Order.desc("mesDate"));
            }

            break;

        case ORDER_BY_UNREAD:

            if (orderType.equals("ASC")) {
                crit.addOrder(Order.asc("mesRecent"));
            } else {
                crit.addOrder(Order.desc("mesRecent"));
            }

            break;

        default:
            crit.addOrder(Order.desc("mesDate"));

            break;
        }

        crit.setFirstResult(page * messagesByPage);
        crit.setMaxResults(messagesByPage);

        ScrollableResults scroll = crit.scroll();

        while (scroll.next()) {
            Message message = (Message) scroll.get(0);

            MessageObj obj = new MessageObj(message.getMesName());

            obj.setBox(message.getMesBox());

            obj.setFrom(message.getMesFrom());

            if ((message.getAttachments() != null) && (message.getAttachments().size() > 0)) {
                obj.setHasAttachment(true);
            } else {
                obj.setHasAttachment(false);
            }

            int size = message.getMesSize();
            size /= 1024;

            if (size > 1024) {
                size /= 1024;
                obj.setSize(size + " MB");
            } else {
                obj.setSize(((size > 0) ? (size + "") : "<1") + " kB");
            }

            if (message.getMesBox().equals(folderSent)) {
                try {
                    obj.setEmail(message.getMesTo());
                } catch (Exception e) {
                    obj.setEmail("unknown to");
                }
            } else {
                obj.setEmail(message.getMesFrom());
            }

            Date date = message.getMesDate();

            if (date != null) {
                Calendar calendar2 = Calendar.getInstance(timeZone, locale);
                calendar2.setTime(date);

                if ((calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR))
                        && (calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH))
                        && (calendar.get(Calendar.DATE) == calendar2.get(Calendar.DATE))) {
                    obj.setDateStr(formatter2.format(calendar2.getTime()));
                } else if (calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) {
                    obj.setDateStr(formatter1.format(calendar2.getTime()));
                } else {
                    obj.setDateStr(formatter3.format(calendar2.getTime()));
                }
            }

            obj.setDate(date);

            if (message.getLabMeses() != null) {
                Iterator it = message.getLabMeses().iterator();
                StringBuffer label = new StringBuffer();

                while (it.hasNext()) {
                    if (label.length() > 0) {
                        label.append(", ");
                    }

                    LabMes labMes = (LabMes) it.next();
                    label.append(labMes.getId().getLabel().getLabName());
                }

                obj.setLabel(label.toString());
            }

            try {
                if (StringUtils.isBlank(message.getMesSubject())) {
                    obj.setSubject("(no subject)");
                } else {
                    obj.setSubject(message.getMesSubject());
                }
            } catch (Exception ex) {
                obj.setSubject("(no subject)");
            }

            if (message.isMesFlagged()) {
                obj.setFlagged(true);
            } else {
                obj.setFlagged(false);
            }

            if (message.isMesRecent()) {
                obj.setRecent(true);
            } else {
                obj.setRecent(false);
            }

            String priority = "normal";

            if (MessageUtilities.isHighPriority(message.getMesHeaders())) {
                priority = "high";
            } else if (MessageUtilities.isLowPriority(message.getMesHeaders())) {
                priority = "low";
            }

            obj.setPriority(priority);

            messages.addElement(obj);
        }

        return messages;
    } catch (Exception e) {
        throw new MailException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.sonicle.webtop.mail.Service.java

private Address[] removeDestination(Address a[], String email) throws MessagingException {
    email = email.toLowerCase();// ww w  . j  av  a 2s.c  o  m
    Vector v = new Vector();
    for (int i = 0; i < a.length; ++i) {
        InternetAddress ia = (InternetAddress) a[i];
        if (!ia.getAddress().toLowerCase().equals(email)) {
            v.addElement(a[i]);
        }
    }
    Address na[] = new Address[v.size()];
    v.copyInto(na);
    return na;
}

From source file:com.sonicle.webtop.mail.Service.java

public Message reply(MailAccount account, MimeMessage orig, boolean replyToAll, boolean fromSent)
        throws MessagingException {
    MimeMessage reply = new MimeMessage(account.getMailSession());
    /*//from  w  w  w  .  j  av  a2s  .c  o  m
     * Have to manipulate the raw Subject header so that we don't lose
     * any encoding information.  This is safe because "Re:" isn't
     * internationalized and (generally) isn't encoded.  If the entire
     * Subject header is encoded, prefixing it with "Re: " still leaves
     * a valid and correct encoded header.
     */
    String subject = orig.getHeader("Subject", null);
    if (subject != null) {
        if (!subject.regionMatches(true, 0, "Re: ", 0, 4)) {
            subject = "Re: " + subject;
        }
        reply.setHeader("Subject", subject);
    }
    Address a[] = null;
    if (!fromSent)
        a = orig.getReplyTo();
    else {
        Address ax[] = orig.getRecipients(RecipientType.TO);
        if (ax != null) {
            a = new Address[1];
            a[0] = ax[0];
        }
    }
    reply.setRecipients(Message.RecipientType.TO, a);
    if (replyToAll) {
        Vector v = new Vector();
        Session session = account.getMailSession();
        // add my own address to list
        InternetAddress me = InternetAddress.getLocalAddress(session);
        if (me != null) {
            v.addElement(me);
        }
        // add any alternate names I'm known by
        String alternates = null;
        if (session != null) {
            alternates = session.getProperty("mail.alternates");
        }
        if (alternates != null) {
            eliminateDuplicates(v, InternetAddress.parse(alternates, false));
        }
        // should we Cc all other original recipients?
        String replyallccStr = null;
        boolean replyallcc = false;
        if (session != null) {
            replyallcc = PropUtil.getBooleanSessionProperty(session, "mail.replyallcc", false);
        }
        // add the recipients from the To field so far
        eliminateDuplicates(v, a);
        a = orig.getRecipients(Message.RecipientType.TO);
        a = eliminateDuplicates(v, a);
        if (a != null && a.length > 0) {
            if (replyallcc) {
                reply.addRecipients(Message.RecipientType.CC, a);
            } else {
                reply.addRecipients(Message.RecipientType.TO, a);
            }
        }
        a = orig.getRecipients(Message.RecipientType.CC);
        a = eliminateDuplicates(v, a);
        if (a != null && a.length > 0) {
            reply.addRecipients(Message.RecipientType.CC, a);
        }
        // don't eliminate duplicate newsgroups
        a = orig.getRecipients(MimeMessage.RecipientType.NEWSGROUPS);
        if (a != null && a.length > 0) {
            reply.setRecipients(MimeMessage.RecipientType.NEWSGROUPS, a);
        }
    }

    String msgId = orig.getHeader("Message-Id", null);
    if (msgId != null) {
        reply.setHeader("In-Reply-To", msgId);
    }

    /*
     * Set the References header as described in RFC 2822:
     *
     * The "References:" field will contain the contents of the parent's
     * "References:" field (if any) followed by the contents of the parent's
     * "Message-ID:" field (if any).  If the parent message does not contain
     * a "References:" field but does have an "In-Reply-To:" field
     * containing a single message identifier, then the "References:" field
     * will contain the contents of the parent's "In-Reply-To:" field
     * followed by the contents of the parent's "Message-ID:" field (if
     * any).  If the parent has none of the "References:", "In-Reply-To:",
     * or "Message-ID:" fields, then the new message will have no
     * "References:" field.
     */
    String refs = orig.getHeader("References", " ");
    if (refs == null) {
        // XXX - should only use if it contains a single message identifier
        refs = orig.getHeader("In-Reply-To", " ");
    }
    if (msgId != null) {
        if (refs != null) {
            refs = MimeUtility.unfold(refs) + " " + msgId;
        } else {
            refs = msgId;
        }
    }
    if (refs != null) {
        reply.setHeader("References", MimeUtility.fold(12, refs));
    }

    //try {
    //    setFlags(answeredFlag, true);
    //} catch (MessagingException mex) {
    //    // ignore it
    //}
    return reply;
}

From source file:com.sonicle.webtop.mail.Service.java

private Address[] eliminateDuplicates(Vector v, Address[] addrs) {
    if (addrs == null) {
        return null;
    }/*from  w  w w .  ja  v  a2s  . co m*/
    int gone = 0;
    for (int i = 0; i < addrs.length; i++) {
        boolean found = false;
        // search the vector for this address
        for (int j = 0; j < v.size(); j++) {
            if (((InternetAddress) v.elementAt(j)).equals(addrs[i])) {
                // found it; count it and remove it from the input array
                found = true;
                gone++;
                addrs[i] = null;
                break;
            }
        }
        if (!found) {
            v.addElement(addrs[i]); // add new address to vector
        }
    }
    // if we found any duplicates, squish the array
    if (gone != 0) {
        Address[] a;
        // new array should be same type as original array
        // XXX - there must be a better way, perhaps reflection?
        if (addrs instanceof InternetAddress[]) {
            a = new InternetAddress[addrs.length - gone];
        } else {
            a = new Address[addrs.length - gone];
        }
        for (int i = 0, j = 0; i < addrs.length; i++) {
            if (addrs[i] != null) {
                a[j++] = addrs[i];
            }
        }
        addrs = a;
    }
    return addrs;
}

From source file:fsi_admin.JFsiTareas.java

private void instalarActualizacion(String folderact, float versiondisp, int revisiondisp, float version,
        int revision, PrintWriter pw, MutableBoolean reiniciarServidor, PrintWriter out) throws Exception {
    if (out != null) {
        out.println("COMENZANDO LA ACTUALIZACION DE BASES DE DATOS " + versiondisp + "." + revisiondisp + ": "
                + JUtil.obtFechaTxt(new Date(), "HH:mm:ss") + "<br>");
        out.println("------------------------------------------------------------------------------<br>");
        out.flush();//from   www.j  av a  2s . com
    }
    pw.println("COMENZANDO LA ACTUALIZACION DE BASES DE DATOS " + versiondisp + "." + revisiondisp + ": "
            + JUtil.obtFechaTxt(new Date(), "HH:mm:ss"));
    pw.println("------------------------------------------------------------------------------");
    pw.flush();

    FileReader file = new FileReader(folderact + "status_log");
    BufferedReader buff = new BufferedReader(file);
    boolean eof = false;
    Map<String, String> map = new HashMap<String, String>();
    Vector<String> actbd_sql = new Vector<String>();
    Vector<String> actfsi_sql = new Vector<String>();
    while (!eof) {
        String line = buff.readLine();
        if (line == null)
            eof = true;
        else {
            try {
                StringTokenizer st = new StringTokenizer(line, "=");
                String key = st.nextToken();
                String value = st.nextToken();
                map.put(key, value);
            } catch (NoSuchElementException e) {
                continue;
            }
        }
    }
    buff.close();
    file.close();

    //ya tenemos el status_log en memoria, ahora lo utilizamos como base para la actualizacion
    JFsiScript sc = new JFsiScript();
    sc.setVerbose(true);
    String ERROR = "";
    String dirfsi = "/usr/local/forseti";
    if (map.get("STATUS").equals("ACTBD")) // significa que no ha hecho nada, o que no se completo la actulizacion anterior
    {
        JBDSSet set = new JBDSSet(null);
        set.ConCat(true);
        set.m_Where = "ID_BD >= " + map.get("BD") + " and SU = '3'";
        set.m_OrderBy = "ID_BD ASC";
        set.Open();

        for (int i = 0; i < set.getNumRows(); i++) {
            //verifica que la base de datos a actualizar, no sea mas antigua que las actuales en este servidor
            //Esto pudiera suceder porque se acaba de restaurar una base de datos que habia sido respaldada antes
            //de la ultima actualizacion del servidor
            JAdmVariablesSet var = new JAdmVariablesSet(null);
            var.setBD(set.getAbsRow(i).getNombre());
            var.ConCat(3);
            var.m_Where = "ID_Variable = 'VERSION'";
            var.Open();
            //pw.println("SQL: " + set.getAbsRow(i).getNombre() + " " + var.getSQL());
            //pw.println("TOTAL: " + var.getNumRows());
            float varversion = var.getAbsRow(0).getVDecimal();
            int varrevision = var.getAbsRow(0).getVEntero();
            if (varversion < version || (varversion == version && varrevision < revision)) {
                if (out != null) {
                    out.println(
                            "LA BASE DE DATOS ESTA DESFASADA EN VERSION O REVISION Y NO SE PUEDE ACTUALIZAR: "
                                    + set.getAbsRow(i).getNombre() + ":" + varversion + "." + varrevision
                                    + " --- " + versiondisp + "." + revisiondisp + "<br>");
                    out.println(
                            "ESTO PUEDE SER DEBIDO A QUE SE HA RESTAURADO UNA BASE DE DATOS QUE HABIA SIDO<br>");
                    out.println(
                            "RESPALDADA ANTES DE LA ULTIMA ACTUALIZACION DEL SERVIDOR... INTENTA POR ACTUALIZACION DE EMPRESAS DESFASADAS<br>");
                    out.println(
                            "-------------------------------------------------------------------------------------------------------------------<br>");
                    out.flush();
                }
                pw.println("LA BASE DE DATOS ESTA DESFASADA EN VERSION O REVISION Y NO SE PUEDE ACTUALIZAR: "
                        + set.getAbsRow(i).getNombre() + ":" + varversion + "." + varrevision + " --- "
                        + versiondisp + "." + revisiondisp);
                pw.println("ESTO PUEDE SER DEBIDO A QUE SE HA RESTAURADO UNA BASE DE DATOS QUE HABIA SIDO");
                pw.println(
                        "RESPALDADA ANTES DE LA ULTIMA ACTUALIZACION DEL SERVIDOR... INTENTA POR ACTUALIZACION DE EMPRESAS DESFASADAS");
                pw.println(
                        "-------------------------------------------------------------------------------------------------------------------");
                pw.flush();
                continue;
            }

            if (out != null) {
                out.println("Actualizando BD :" + set.getAbsRow(i).getNombre() + "<br>");
                out.flush();
            }
            pw.println("Actualizando BD :" + set.getAbsRow(i).getNombre());
            pw.flush();

            if (map.get("PUNTO").equals("NC")) // nada actalizado a esta base de datos, comienza por los archivos
            {
                File dir = new File(folderact + "emp");
                if (dir.exists()) {
                    if (out != null) {
                        out.println("Grabando los archivos del sistema para BD: " + set.getAbsRow(i).getNombre()
                                + "<br>");
                        out.flush();
                    }
                    pw.println("Grabando los archivos del sistema para BD: " + set.getAbsRow(i).getNombre());
                    pw.flush();
                    String CONTENT = "rsync -av --stats " + folderact + "emp/ " + dirfsi + "/emp/"
                            + set.getAbsRow(i).getNombre().substring(6);
                    sc.setContent(CONTENT);
                    pw.println(CONTENT);
                    String RES = sc.executeCommand();
                    ERROR += sc.getError();
                    if (!ERROR.equals("")) {
                        //System.out.println(ERROR);
                        if (out != null) {
                            out.println(ERROR + "<br>");
                            out.flush();
                        }
                        pw.println(ERROR);
                        pw.flush();
                        return;
                    } else {
                        pw.println(RES);
                        pw.flush();
                    }
                }
                map.put("PUNTO", "ARCHIVOS");
                File f = new File(folderact + "status_log");
                FileWriter fsl = new FileWriter(f);
                fsl.write("STATUS=ACTBD\nBD=" + set.getAbsRow(i).getID_BD() + "\nPUNTO=ARCHIVOS");
                fsl.close();

                pw.println("-----------------------------------------------------------------------------");
                pw.flush();
            }

            if (map.get("PUNTO").equals("ARCHIVOS")) // archivos actualizados, ahora la estructura de la bd
            {
                File sql = new File(folderact + "actbd.sql");
                if (sql.exists()) {
                    //////////////////////////////////////////////////////////////////////////////////////////////////
                    if (actbd_sql.size() == 0) {
                        FileReader filebas = new FileReader(folderact + "actbd.sql");
                        BufferedReader buffbas = new BufferedReader(filebas);
                        boolean eofbas = false;
                        String strbas = "";

                        while (!eofbas) {
                            String linebas = buffbas.readLine();
                            if (linebas == null)
                                eofbas = true;
                            else {
                                if (linebas.indexOf("--@FIN_BLOQUE") == -1)
                                    strbas += linebas + "\n";
                                else {
                                    actbd_sql.addElement(strbas);
                                    strbas = "";
                                }
                            }
                        }
                        buffbas.close();
                        filebas.close();
                    }
                    if (out != null) {
                        out.println(
                                "Executando estructura SQL para BD: " + set.getAbsRow(i).getNombre() + "<br>");
                        out.flush();
                    }
                    pw.println("Executando estructura SQL para BD: " + set.getAbsRow(i).getNombre());
                    pw.flush();
                    Connection con = JAccesoBD.getConexion(set.getAbsRow(i).getNombre());
                    con.setAutoCommit(false);
                    Statement s = con.createStatement();
                    for (int j = 0; j < actbd_sql.size(); j++) {
                        String actbdblq_sql = JUtil.replace(actbd_sql.get(j), "[[owner]]",
                                set.getAbsRow(i).getUsuario());
                        pw.println(actbdblq_sql + "\n");
                        pw.flush();
                        s.executeUpdate(actbdblq_sql);
                    }
                    String varbd_sql = "UPDATE TBL_VARIABLES\n" + "SET VDecimal = '" + versiondisp
                            + "', VEntero = '" + revisiondisp + "', VAlfanumerico = '" + versiondisp + "."
                            + revisiondisp + "'\n" + "WHERE ID_Variable = 'VERSION';";
                    //"REASSIGN OWNED BY forseti TO " + set.getAbsRow(i).getUsuario() + ";"; 

                    pw.println(varbd_sql + "\n");
                    pw.flush();
                    s.executeUpdate(varbd_sql);
                    con.commit();
                    s.close();
                    con.close();
                    ////////////////////////////////////////////////////////////////////////////////////////////////
                } else {
                    Connection con = JAccesoBD.getConexion(set.getAbsRow(i).getNombre());
                    con.setAutoCommit(false);
                    Statement s = con.createStatement();
                    String varbd_sql = "UPDATE TBL_VARIABLES\n" + "SET VDecimal = '" + versiondisp
                            + "', VEntero = '" + revisiondisp + "', VAlfanumerico = '" + versiondisp + "."
                            + revisiondisp + "'\n" + "WHERE ID_Variable = 'VERSION';";
                    pw.println(varbd_sql + "\n");
                    pw.flush();
                    s.executeUpdate(varbd_sql);
                    con.commit();
                    s.close();
                    con.close();
                }
                map.put("PUNTO", "ESTRUCTURA");
                File f = new File(folderact + "status_log");
                FileWriter fsl = new FileWriter(f);
                fsl.write("STATUS=ACTBD\nBD=" + set.getAbsRow(i).getID_BD() + "\nPUNTO=ESTRUCTURA");
                fsl.close();

            }
            //////////////////////////////////////////////////////////////////////
            if (map.get("PUNTO").equals("ESTRUCTURA")) // Ahora los mensajes de esta bd los actualiza
            {
                File msj = new File(folderact + "bin/.forseti_es");
                if (msj.exists()) {
                    FileReader filemsj = new FileReader(folderact + "bin/.forseti_es");
                    BufferedReader buffmsj = new BufferedReader(filemsj);
                    boolean eofmsj = false;
                    if (out != null) {
                        out.println("Executando mensajes para " + set.getAbsRow(i).getNombre() + "<br>");
                        out.flush();
                    }
                    pw.println("Executando mensajes para " + set.getAbsRow(i).getNombre());
                    pw.flush();
                    Connection con = JAccesoBD.getConexion(set.getAbsRow(i).getNombre());
                    Statement s = con.createStatement();
                    String varmsj_sql = "TRUNCATE TABLE TBL_MSJ;";
                    pw.println(varmsj_sql + "\n");
                    pw.flush();
                    s.executeUpdate(varmsj_sql);

                    while (!eofmsj) {
                        String line = buffmsj.readLine();
                        if (line == null)
                            eofmsj = true;
                        else {
                            if (line.equals("__INIT")) {
                                String alc = "", mod = "", sub = "", elm = "", msj1 = "", msj2 = "", msj3 = "",
                                        msj4 = "", msj5 = "";
                                for (int im = 1; im <= 9; im++) {
                                    line = buffmsj.readLine();
                                    switch (im) {
                                    case 1:
                                        msj1 = "'" + line + "'";
                                        break;
                                    case 2:
                                        msj2 = (line.equals("null") ? "null" : "'" + JUtil.p(line) + "'");
                                        break;
                                    case 3:
                                        msj3 = (line.equals("null") ? "null" : "'" + JUtil.p(line) + "'");
                                        break;
                                    case 4:
                                        msj4 = (line.equals("null") ? "null" : "'" + JUtil.p(line) + "'");
                                        break;
                                    case 5:
                                        msj5 = (line.equals("null") ? "null" : "'" + JUtil.p(line) + "'");
                                        break;
                                    case 6:
                                        alc = "'" + JUtil.p(line) + "'";
                                        break;
                                    case 7:
                                        mod = "'" + JUtil.p(line) + "'";
                                        break;
                                    case 8:
                                        sub = "'" + JUtil.p(line) + "'";
                                        break;
                                    case 9:
                                        elm = "'" + JUtil.p(line) + "'";
                                        break;
                                    }
                                }

                                varmsj_sql = "INSERT INTO tbl_msj\nVALUES(";
                                varmsj_sql += alc + "," + mod + "," + sub + "," + elm + "," + msj1 + "," + msj2
                                        + "," + msj3 + "," + msj4 + "," + msj5 + ");";

                                pw.println(varmsj_sql + "\n");
                                pw.flush();
                                s.executeUpdate(varmsj_sql);

                            }
                        }
                    }

                    s.close();
                    con.close();

                    buffmsj.close();
                    filemsj.close();
                }
                map.put("PUNTO", "MSJ");
                File f = new File(folderact + "status_log");
                FileWriter fsl = new FileWriter(f);
                fsl.write("STATUS=ACTBD\nBD=" + set.getAbsRow(i).getID_BD() + "\nPUNTO=MSJ");
                fsl.close();

            }
            ///////////////////////////////////////////////////////////////////////
            map.put("PUNTO", "NC");
        }
        map.put("STATUS", "ACTFSI");
        map.put("BD", "FSI");
        map.put("PUNTO", "NC");
        File f = new File(folderact + "status_log");
        FileWriter fsl = new FileWriter(f);
        fsl.write("STATUS=ACTFSI\nBD=FSI\nPUNTO=NC");
        fsl.close();
    }

    if (map.get("STATUS").equals("ACTFSI")) //Significa que las bases de datos ya estan actualizadas, y falta FORSETI_ADMIN
    {
        if (map.get("PUNTO").equals("NC")) // nada actalizado a esta base de datos, comienza por los archivos
        {
            if (out != null) {
                out.println(
                        "--------------------------------- FORSETI_ADMIN -------------------------------------------<br>");
                out.flush();
            }
            pw.println(
                    "--------------------------------- FORSETI_ADMIN -------------------------------------------");
            pw.flush();

            File dir = new File(folderact + "act");
            if (dir.exists()) {
                if (out != null) {
                    out.println("Grabando los archivos act...<br>");
                    out.flush();
                }
                pw.println("Grabando los archivos act...");
                pw.flush();
                String CONTENT = "rsync -av --stats " + folderact + "act/ " + dirfsi + "/act";
                sc.setContent(CONTENT);
                pw.println(CONTENT);
                String RES = sc.executeCommand();
                ERROR = sc.getError();
                if (!ERROR.equals("")) {
                    if (out != null) {
                        out.println(ERROR + "<br>");
                        out.flush();
                    }
                    pw.println(ERROR);
                    pw.flush();
                    return;
                } else {
                    pw.println(RES);
                    pw.flush();
                }
            }
            dir = new File(folderact + "bin");
            if (dir.exists()) {
                if (out != null) {
                    out.println("Grabando los archivos bin...<br>");
                    out.flush();
                }
                pw.println("Grabando los archivos bin...");
                pw.flush();
                String CONTENT = "rsync -av --stats " + folderact + "bin/ " + dirfsi + "/bin";
                sc.setContent(CONTENT);
                pw.println(CONTENT);
                String RES = sc.executeCommand();
                ERROR = sc.getError();
                if (!ERROR.equals("")) {
                    if (out != null) {
                        out.println(ERROR + "<br>");
                        out.flush();
                    }
                    pw.println(ERROR);
                    pw.flush();
                    return;
                } else {
                    pw.println(RES);
                    pw.flush();
                }
            }
            dir = new File(folderact + "pac");
            if (dir.exists()) {
                if (out != null) {
                    out.println("Grabando los archivos pac...<br>");
                    out.flush();
                }
                pw.println("Grabando los archivos pac...");
                pw.flush();
                String CONTENT = "rsync -av --stats " + folderact + "pac/ " + dirfsi + "/pac";
                sc.setContent(CONTENT);
                pw.println(CONTENT);
                String RES = sc.executeCommand();
                ERROR = sc.getError();
                if (!ERROR.equals("")) {
                    if (out != null) {
                        out.println(ERROR + "<br>");
                        out.flush();
                    }
                    pw.println(ERROR);
                    pw.flush();
                    return;
                } else {
                    pw.println(RES);
                    pw.flush();
                }
            }
            dir = new File(folderact + "rec");
            if (dir.exists()) {
                if (out != null) {
                    out.println("Grabando los archivos rec...<br>");
                    out.flush();
                }
                pw.println("Grabando los archivos rec...");
                pw.flush();
                String CONTENT = "rsync -av --stats " + folderact + "rec/ " + dirfsi + "/rec";
                sc.setContent(CONTENT);
                pw.println(CONTENT);
                String RES = sc.executeCommand();
                ERROR = sc.getError();
                if (!ERROR.equals("")) {
                    if (out != null) {
                        out.println(ERROR + "<br>");
                        out.flush();
                    }
                    pw.println(ERROR);
                    pw.flush();
                    return;
                } else {
                    pw.println(RES);
                    pw.flush();
                }
            }
            map.put("PUNTO", "ARCHIVOS");
            File f = new File(folderact + "status_log");
            FileWriter fsl = new FileWriter(f);
            fsl.write("STATUS=ACTFSI\nBD=FSI\nPUNTO=ARCHIVOS");
            fsl.close();
        }

        if (map.get("PUNTO").equals("ARCHIVOS")) // archivos actualizados, ahora la estructura de la bd
        {
            File sql = new File(folderact + "actfsi.sql");
            if (sql.exists()) {
                //////////////////////////////////////////////////////////////////////////////////////////////////
                FileReader filebas = new FileReader(folderact + "actfsi.sql");
                BufferedReader buffbas = new BufferedReader(filebas);
                boolean eofbas = false;
                String strbas = "";

                while (!eofbas) {
                    String linebas = buffbas.readLine();
                    if (linebas == null)
                        eofbas = true;
                    else {
                        if (linebas.indexOf("--@FIN_BLOQUE") == -1)
                            strbas += linebas + "\n";
                        else {
                            actfsi_sql.addElement(strbas);
                            strbas = "";
                        }
                    }
                }
                buffbas.close();
                filebas.close();

                if (out != null) {
                    out.println("Executando estructura SQL para FORSETI_ADMIN<br>");
                    out.flush();
                }
                pw.println("Executando estructura SQL para FORSETI_ADMIN");
                pw.flush();
                Connection con = JAccesoBD.getConexion();
                con.setAutoCommit(false);
                Statement s = con.createStatement();
                for (int j = 0; j < actfsi_sql.size(); j++) {
                    String actfsiblq_sql = actfsi_sql.get(j);
                    pw.println(actfsiblq_sql + "\n");
                    pw.flush();
                    s.executeUpdate(actfsiblq_sql);
                }
                String varfsi_sql = "UPDATE TBL_VARIABLES\n" + "SET VDecimal = '" + versiondisp
                        + "', VEntero = '" + revisiondisp + "', VAlfanumerico = '" + versiondisp + "."
                        + revisiondisp + "'\n" + "WHERE ID_Variable = 'VERSION';";
                pw.println(varfsi_sql + "\n");
                pw.flush();
                s.executeUpdate(varfsi_sql);
                con.commit();
                s.close();
                con.close();
            } else {
                Connection con = JAccesoBD.getConexion();
                con.setAutoCommit(false);
                Statement s = con.createStatement();
                String varfsi_sql = "UPDATE TBL_VARIABLES\n" + "SET VDecimal = '" + versiondisp
                        + "', VEntero = '" + revisiondisp + "', VAlfanumerico = '" + versiondisp + "."
                        + revisiondisp + "'\n" + "WHERE ID_Variable = 'VERSION';";
                pw.println(varfsi_sql + "\n");
                pw.flush();
                s.executeUpdate(varfsi_sql);
                con.commit();
                s.close();
                con.close();
            }
            map.put("PUNTO", "ESTRUCTURA");
            File f = new File(folderact + "status_log");
            FileWriter fsl = new FileWriter(f);
            fsl.write("STATUS=ACTFSI\nBD=FSI\nPUNTO=ESTRUCTURA");
            fsl.close();

        }

        if (map.get("PUNTO").equals("ESTRUCTURA")) // Ahora los mensajes los actualiza
        {
            File msj = new File(folderact + "bin/.forseti_es");
            if (msj.exists()) {
                FileReader filemsj = new FileReader(folderact + "bin/.forseti_es");
                BufferedReader buffmsj = new BufferedReader(filemsj);
                boolean eofmsj = false;
                if (out != null) {
                    out.println("Executando mensajes para FORSETI_ADMIN<br>");
                    out.flush();
                }
                pw.println("Executando mensajes para FORSETI_ADMIN");
                pw.flush();
                Connection con = JAccesoBD.getConexion();
                Statement s = con.createStatement();
                String varmsj_sql = "TRUNCATE TABLE TBL_MSJ;";
                pw.println(varmsj_sql + "\n");
                pw.flush();
                s.executeUpdate(varmsj_sql);

                while (!eofmsj) {
                    String line = buffmsj.readLine();
                    if (line == null)
                        eofmsj = true;
                    else {
                        if (line.equals("__INIT")) {
                            String alc = "", mod = "", sub = "", elm = "", msj1 = "", msj2 = "", msj3 = "",
                                    msj4 = "", msj5 = "";
                            for (int i = 1; i <= 9; i++) {
                                line = buffmsj.readLine();
                                switch (i) {
                                case 1:
                                    msj1 = "'" + line + "'";
                                    break;
                                case 2:
                                    msj2 = (line.equals("null") ? "null" : "'" + JUtil.p(line) + "'");
                                    break;
                                case 3:
                                    msj3 = (line.equals("null") ? "null" : "'" + JUtil.p(line) + "'");
                                    break;
                                case 4:
                                    msj4 = (line.equals("null") ? "null" : "'" + JUtil.p(line) + "'");
                                    break;
                                case 5:
                                    msj5 = (line.equals("null") ? "null" : "'" + JUtil.p(line) + "'");
                                    break;
                                case 6:
                                    alc = "'" + JUtil.p(line) + "'";
                                    break;
                                case 7:
                                    mod = "'" + JUtil.p(line) + "'";
                                    break;
                                case 8:
                                    sub = "'" + JUtil.p(line) + "'";
                                    break;
                                case 9:
                                    elm = "'" + JUtil.p(line) + "'";
                                    break;
                                }
                            }

                            varmsj_sql = "INSERT INTO tbl_msj\nVALUES(";
                            varmsj_sql += alc + "," + mod + "," + sub + "," + elm + "," + msj1 + "," + msj2
                                    + "," + msj3 + "," + msj4 + "," + msj5 + ");";

                            pw.println(varmsj_sql + "\n");
                            pw.flush();
                            s.executeUpdate(varmsj_sql);

                        }
                    }
                }

                s.close();
                con.close();

                buffmsj.close();
                filemsj.close();
            }
            File f = new File(folderact + "status_log");
            FileWriter fsl = new FileWriter(f);
            fsl.write("STATUS=ACTFSI\nBD=FSI\nPUNTO=MSJ");
            fsl.close();
        }

        if (map.get("PUNTO").equals("MSJ")) // mensajes actualizados, ahora el archivo ROOT
        {
            File root = new File(folderact + "ROOT.war");
            if (root.exists()) {
                if (out != null) {
                    out.println("Grabando el archivo ROOT para tomcat...<br>");
                    out.flush();
                }
                pw.println("Grabando el archivo ROOT para tomcat...");
                pw.flush();
                String CONTENT = "rsync -av --stats " + folderact + "ROOT.war " + tomcat + "/webapps";
                sc.setContent(CONTENT);
                pw.println(CONTENT);
                String RES = sc.executeCommand();
                ERROR = sc.getError();
                if (!ERROR.equals("")) {
                    if (out != null) {
                        out.println(ERROR + "<br>");
                        out.flush();
                    }
                    pw.println(ERROR);
                    pw.flush();
                    return;
                } else {
                    pw.println(RES);
                    pw.flush();
                }
            }
            map.put("PUNTO", "ROOT");
            File f = new File(folderact + "status_log");
            FileWriter fsl = new FileWriter(f);
            fsl.write("STATUS=OK\nBD=FSI\nPUNTO=ROOT");
            fsl.close();
            reiniciarServidor.setValue(true);
        }

    }
    if (out != null) {
        out.println("FINALIZADA LA ACTUALIZACION DE BASES DE DATOS: "
                + JUtil.obtFechaTxt(new Date(), "HH:mm:ss") + "<br>");
        out.println("------------------------------------------------------------------------------<br>");
    }
    pw.println("FINALIZADA LA ACTUALIZACION DE BASES DE DATOS: " + JUtil.obtFechaTxt(new Date(), "HH:mm:ss"));
    pw.println("------------------------------------------------------------------------------");

}

From source file:cn.jcenterhome.web.action.CpAction.java

public ActionForward cp_import(HttpServletRequest request, HttpServletResponse response) {
    Map<String, Object> sGlobal = (Map<String, Object>) request.getAttribute("sGlobal");
    Map<String, Object> sConfig = (Map<String, Object>) request.getAttribute("sConfig");
    if (!Common.checkPerm(request, response, "allowblog")) {
        MessageVO msgVO = Common.ckSpaceLog(request);
        if (msgVO != null) {
            return showMessage(request, response, msgVO);
        }/*from  w w  w . ja v a 2s .  c  om*/
        return showMessage(request, response, "no_privilege");
    }
    if (!cpService.checkRealName(request, "blog")) {
        return showMessage(request, response, "no_privilege_realname");
    }
    if (!cpService.checkVideoPhoto(request, response, "blog")) {
        return showMessage(request, response, "no_privilege_videophoto");
    }
    switch (cpService.checkNewUser(request, response)) {
    case 1:
        break;
    case 2:
        return showMessage(request, response, "no_privilege_newusertime", "", 1,
                String.valueOf(sConfig.get("newusertime")));
    case 3:
        return showMessage(request, response, "no_privilege_avatar");
    case 4:
        return showMessage(request, response, "no_privilege_friendnum", "", 1,
                String.valueOf(sConfig.get("need_friendnum")));
    case 5:
        return showMessage(request, response, "no_privilege_email");
    }
    int waitTime = Common.checkInterval(request, response, "post");
    if (waitTime > 0) {
        return showMessage(request, response, "operating_too_fast", "", 1, String.valueOf(waitTime));
    }
    try {
        String siteUrl = Common.getSiteUrl(request);
        File userFile = new File(JavaCenterHome.jchRoot + "./data/temp/" + sGlobal.get("supe_uid") + ".data");
        if (submitCheck(request, "importsubmit")) {
            Map reward = Common.getReward("blogimport", false, 0, "", true, request, response);
            Map space = (Map) request.getAttribute("space");
            int spaceExperience = (Integer) space.get("experience");
            int spaceCredit = (Integer) space.get("credit");
            int rewardExperience = (Integer) reward.get("experience");
            int rewardCredit = (Integer) reward.get("credit");
            if (spaceExperience < rewardExperience) {
                return showMessage(request, response, "experience_inadequate", "", 1,
                        new String[] { String.valueOf(spaceExperience), String.valueOf(rewardExperience) });
            }
            if (spaceCredit < rewardCredit) {
                return showMessage(request, response, "integral_inadequate", "", 1,
                        new String[] { String.valueOf(spaceCredit), String.valueOf(rewardCredit) });
            }
            String url = request.getParameter("url").trim();
            Map urls = cpService.parseUrl(url);
            if (Common.empty(url) || urls.isEmpty()) {
                return showMessage(request, response, "url_is_not_correct");
            }
            XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
            config.setServerURL(new URL(url));
            XmlRpcClient client = new XmlRpcClient();
            client.setConfig(config);
            Vector params = new Vector();
            params.addElement("blog");
            params.addElement(
                    Common.sHtmlSpecialChars(Common.siconv(request.getParameter("username"), "utf-8", "", "")));
            params.addElement(Common.sHtmlSpecialChars(request.getParameter("password")));
            params.addElement(sConfig.get("importnum"));
            Object[] results = (Object[]) client.execute("metaWeblog.getRecentPosts", params);
            if (results == null || results.length == 0) {
                return showMessage(request, response, "blog_import_no_data", null, 1,
                        "<textarea name=\"tmp[]\" style=\"width:98%;\" rows=\"4\">no data</textarea>");
            }
            HashMap last = (HashMap) results[results.length - 1];
            if (last.containsKey("postid") == false) {
                return showMessage(request, response, "blog_import_no_data", null, 1,
                        Common.implode(last, ","));
            }
            PHPSerializer phpSerializer = new PHPSerializer(JavaCenterHome.JCH_CHARSET);
            FileHelper.writeFile(userFile, phpSerializer.serialize(results));
            request.setAttribute("results", results);
            request.setAttribute("incount", 0);
        } else if (submitCheck(request, "import2submit")) {
            ArrayList results = null;
            if (userFile.exists()) {
                String result = FileHelper.readFile(userFile);
                if (Common.empty(result) == false) {
                    PHPSerializer phpSerializer = new PHPSerializer(JavaCenterHome.JCH_CHARSET);
                    results = ((AssocArray) phpSerializer.unserialize(result)).toArrayList();
                }
            }
            String[] ids = request.getParameterValues("ids[]");
            if (Common.empty(results) || Common.empty(ids)) {
                return showMessage(request, response, "choose_at_least_one_log", "cp.jsp?ac=import");
            }
            int allCount = 0;
            int inCount = 0;
            ArrayList newResults = new ArrayList();
            for (int i = 0, size = results.size(); i < size; i++) {
                int key = i;
                allCount += 1;
                Map currBlog = ((AssocArray) results.get(i)).toHashMap();
                if (currBlog.get("dateCreated") instanceof Calendar) {
                    Calendar calendar = (Calendar) currBlog.get("dateCreated");
                    int dateline = (int) (calendar.getTimeInMillis() / 1000);
                    currBlog.put("dateCreated", Common.gmdate("yyyyMMdd'T'HH:mm:ss", dateline,
                            String.valueOf(sConfig.get("timeoffset"))));
                }
                if (Common.in_array(ids, key)) {
                    Map value = (Map) Common.sAddSlashes(currBlog);
                    int dateline = Common.strToTime(value.get("dateCreated").toString(),
                            String.valueOf(sConfig.get("timeoffset")), "yyyyMMdd'T'HH:mm:ss");
                    String subject = Common.getStr(value.get("title").toString(), 80, true, true, true, 0, 0,
                            request, response);
                    String message = value.containsKey("description") ? value.get("description").toString()
                            : value.get("content").toString();
                    message = Common.getStr(message, 0, true, true, true, 0, 1, request, response);
                    message = blogService.checkHtml(request, response, message);
                    if (Common.empty(subject) || Common.empty(message)) {
                        currBlog.put("status", "--");
                        currBlog.put("blogid", 0);
                        continue;
                    }
                    Map blogarr = new HashMap();
                    blogarr.put("uid", sGlobal.get("supe_uid"));
                    blogarr.put("username", sGlobal.get("supe_username"));
                    blogarr.put("subject", subject);
                    blogarr.put("pic", blogService.getMessagePic(message));
                    blogarr.put("dateline", dateline != 0 ? dateline : sGlobal.get("timestamp"));
                    int blogId = dataBaseService.insertTable("blog", blogarr, true, false);
                    Map fieldarr = new HashMap();
                    fieldarr.put("blogid", blogId);
                    fieldarr.put("uid", sGlobal.get("supe_uid"));
                    fieldarr.put("message", message);
                    fieldarr.put("postip", Common.getOnlineIP(request));
                    fieldarr.put("related", "");
                    fieldarr.put("target_ids", "");
                    fieldarr.put("hotuser", "");
                    dataBaseService.insertTable("blogfield", fieldarr, false, false);
                    inCount += 1;
                    currBlog.put("status", "OK");
                    currBlog.put("blogid", blogId);
                } else {
                    currBlog.put("status", "--");
                    currBlog.put("blogid", 0);
                }
                newResults.add(currBlog);
            }
            if (inCount != 0) {
                Common.getReward("blogimport", true, 0, "", true, request, response);
                userFile.delete();
            }
            request.setAttribute("results", newResults);
            request.setAttribute("incount", inCount);
        } else if (submitCheck(request, "resubmit")) {
            userFile.delete();
        }
        request.setAttribute("siteurl", siteUrl);
    } catch (XmlRpcException xre) {
        return showMessage(request, response, "blog_import_no_data", null, 1,
                "<textarea name=\"tmp[]\" style=\"width:98%;\" rows=\"4\">" + xre.code + ", " + xre.getMessage()
                        + "</textarea>");
    } catch (IllegalAccessException iace) {
        iace.printStackTrace();
    } catch (IllegalArgumentException iare) {
        iare.printStackTrace();
    } catch (InvocationTargetException ite) {
        ite.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
        return showMessage(request, response, e.getMessage());
    }
    return include(request, response, sConfig, sGlobal, "cp_import.jsp");
}

From source file:alter.vitro.vgw.wsiadapter.WsiUberDustCon.java

/**
 * Test function in order to debug resource discovery even when uberdust is not accessible
 * @param givGatewayInfo/*from ww w .  jav a2s .  c  o m*/
 * @return
 */
private CGatewayWithSmartDevices DEBUG_offline_createWSIDescr(CGateway givGatewayInfo) {
    Vector<CSmartDevice> currSmartDevicesVec = new Vector<CSmartDevice>();
    CGatewayWithSmartDevices myGatewayForSmartDevs = new CGatewayWithSmartDevices(givGatewayInfo,
            currSmartDevicesVec);

    // An auxiliary structure that maps Unique Generic Capability Descriptions to Lists of SensorTypes ids.
    HashMap<String, Vector<Integer>> myAllCapabilitiesToSensorModelIds = new HashMap<String, Vector<Integer>>();

    String responseBodyFromHttpNodesGetStr = WsiUberDustCon.DEBUG_OFFLINE_STR_NODE_GETRESTSTATUS;
    String responseBodyFromHttpNodes_STATUS_Get = WsiUberDustCon.DEBUG_OFFLINE_STR_GETRESTSTATUS_RAW;
    String responseBodyFromHttpNodes_ADMINSTATUS_Get = WsiUberDustCon.DEBUG_OFFLINE_STR_BODY_ADMINSTATUS;
    try {
        //
        String[] nodeUrnsInUberdust = responseBodyFromHttpNodesGetStr.split("\\r?\\n");
        int totalNodeUrnsInUberdust = nodeUrnsInUberdust.length;

        String[] nodeAndLastCapReadingsUrnsInUberdust = responseBodyFromHttpNodes_STATUS_Get.split("\\r?\\n");
        int totalNodeWithCapsInUberdust = nodeAndLastCapReadingsUrnsInUberdust.length;

        //TODO: test this:
        Vector<String> allFaultyNodesUrns = getFaultyNodes();

        // LOOP OVER EVERY NODE (smart device), and for each node, get its capabilities from the second response (responseBody_STATUS_Str)

        logger.debug("Total nodes:" + String.valueOf(totalNodeUrnsInUberdust));
        for (String aNodeUrnsInUberdust : nodeUrnsInUberdust) {
            if (allFaultyNodesUrns.contains(aNodeUrnsInUberdust)) {
                logger.debug("Skiipping node: " + aNodeUrnsInUberdust);
                continue; //skip faulty nodes!
            }
            logger.debug("Discovering resources of node: " + aNodeUrnsInUberdust);

            Vector<Integer> sensorModels_IDs_OfSmartDevVector = new Vector<Integer>();// todo: fix this redundancy!
            Vector<CSensorModel> sensorModelsOfSmartDevVector = new Vector<CSensorModel>();
            CSmartDevice tmpSmartDev = new CSmartDevice(aNodeUrnsInUberdust, "", /* smart device type name */
                    "", /* location description e.g. room1*/
                    new GeodesicPoint(), /*  */
                    sensorModels_IDs_OfSmartDevVector);

            // TODO: Add an extra early for loop to update the fields for the attributes of the SmartDevice such as:
            //      Eventually if the SmartDev has NO other valid sensors (e.g. observation sensors or actuators) then it won't be added !
            String tmp_longitude = "";
            String tmp_latitude = "";
            String tmp_altitude = "";

            for (String aNodeAndLastCapReadingsUrnsInUberdust1 : nodeAndLastCapReadingsUrnsInUberdust) {
                //to update the device attributes!
                String[] nodeCapAndReadingRowItems = aNodeAndLastCapReadingsUrnsInUberdust1.split("\\t");
                if (nodeCapAndReadingRowItems.length > 3
                        && nodeCapAndReadingRowItems[0].compareToIgnoreCase(aNodeUrnsInUberdust) == 0) //we are at the capabilities of the current smartdevice
                {
                    logger.debug(" node id: " + nodeCapAndReadingRowItems[0]);
                    logger.debug(" capability: " + nodeCapAndReadingRowItems[1] != null
                            ? nodeCapAndReadingRowItems[1]
                            : "");
                    logger.debug(
                            " timestamp: " + nodeCapAndReadingRowItems[2] != null ? nodeCapAndReadingRowItems[2]
                                    : "");
                    logger.debug(" measurement: " + nodeCapAndReadingRowItems[3] != null
                            ? nodeCapAndReadingRowItems[3]
                            : "");
                    // [0] is mote (smart device) id
                    // [1] is capability
                    // [2] is timestamp
                    // [3] is measurement value
                    if ((nodeCapAndReadingRowItems[1] != null)
                            && !(nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase(""))) {
                        if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("room")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            {
                                tmpSmartDev.setLocationDesc(nodeCapAndReadingRowItems[3].trim());
                            }
                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("nodetype")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            tmpSmartDev.setName(nodeCapAndReadingRowItems[3].trim());
                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("description")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            //TODO: do we need this?

                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("x")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            //TODO: we need the function to derive a valid longitude from the uberdust value (pending)
                            tmp_longitude = nodeCapAndReadingRowItems[3].trim();
                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("y")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            //TODO: we need the function to derive a valid latitude)
                            tmp_latitude = nodeCapAndReadingRowItems[3].trim();
                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("z")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            //altitude is in meters (assumption)
                            tmp_altitude = nodeCapAndReadingRowItems[3].trim();

                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("phi")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            //TODO: do we need this?
                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("theta")
                                && nodeCapAndReadingRowItems[3] != null
                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                            //TODO: do we need this?
                        }
                    }

                }
            } // end of first round of for loop for attributes
            if (!tmp_latitude.equalsIgnoreCase("") && !tmp_longitude.equalsIgnoreCase("")
                    && !tmp_altitude.equalsIgnoreCase("")) {
                tmpSmartDev.setGplocation(new GeodesicPoint(tmp_latitude, tmp_longitude, tmp_altitude));
            }

            //
            // Again same loop for measurement and actuation capabilities!
            //
            for (String aNodeAndLastCapReadingsUrnsInUberdust : nodeAndLastCapReadingsUrnsInUberdust) {
                String[] nodeCapAndReadingRowItems = aNodeAndLastCapReadingsUrnsInUberdust.split("\\t");
                if (nodeCapAndReadingRowItems.length > 3
                        && nodeCapAndReadingRowItems[0].compareToIgnoreCase(aNodeUrnsInUberdust) == 0) //we are at the capabilities of the current smartdevice
                {
                    // [0] is mote (smart device) id
                    // [1] is capability
                    // [2] is measurement value
                    // [3] is timestamp
                    //                                        logger.debug(nodeCapAndReadingRowItems[1]);
                    // TODO: FILTER OUT UNSUPPORTED OR COMPLEX CAPABILITIES!!!!
                    // Since uberdust does not distinguish currenlty between sensing/actuating capabilities and properties, we need to filter out manually
                    // everything we don't consider a sensing/actuating capability.
                    // Another filtering out is done at a later stage with the SensorMLMessageAdapter, which will filter out the capabilities not supported by IDAS
                    // TODO: it could be nice to have this filtering unified.
                    if ((nodeCapAndReadingRowItems[1] != null)
                            && (nodeCapAndReadingRowItems[1].trim().compareTo("") != 0)
                            && !getSimpleCapForUberdustUrn(nodeCapAndReadingRowItems[1].trim())
                                    .equalsIgnoreCase("UnknownPhenomenon")) {

                        //todo: this is just to support actuation during the demo. The code should be improved later on:
                        // todo: replace with regex
                        //if(getSimpleCapForUberdustUrn(nodeCapAndReadingRowItems[1].trim()).equalsIgnoreCase("switchlight1")
                        //        || getSimpleCapForUberdustUrn(nodeCapAndReadingRowItems[1].trim()).equalsIgnoreCase("switchlight2")
                        //        ||  getSimpleCapForUberdustUrn(nodeCapAndReadingRowItems[1].trim()).equalsIgnoreCase("switchlight3")
                        //        ||getSimpleCapForUberdustUrn(nodeCapAndReadingRowItems[1].trim()).equalsIgnoreCase("switchlight4") )
                        //{

                        //}
                        // else
                        // {
                        //TODO: don't get light measurements from arduinos even if they advertise light as a capability

                        // The model id is set as the hashcode of the capability name appended with the model type of the device.
                        // Perhaps this should be changed to something MORE specific
                        // TODO: the units should be set here as we know them. Create a small dictionary to set them!
                        // TODO: the non-observation sensors/ non-actuation should be filtered here!! the Name for the others should be "UnknownPhenomenon"

                        String tmpGenericCapabilityForSensor = getSimpleCapForUberdustUrn(
                                nodeCapAndReadingRowItems[1].trim());
                        Integer thedigestInt = (tmpGenericCapabilityForSensor + "-" + tmpSmartDev.getName())
                                .hashCode();
                        if (thedigestInt < 0)
                            thedigestInt = thedigestInt * (-1);

                        CSensorModel tmpSensorModel = new CSensorModel(givGatewayInfo.getId(), /*Gateway Id*/
                                thedigestInt, /*Sensor Model Id */
                                (tmpGenericCapabilityForSensor + "-"
                                        + tmpSmartDev.getName()), /* Sensor Model name */
                                CSensorModel.numericDataType, /* Data type*/ // TODO: later on this should be adjustable!!!
                                CSensorModel.defaultAccuracy, /* Accuracy */
                                CSensorModel.defaultUnits) /* Units */; // TODO: this should be set when it is known!!!
                        //                                            if(!tmpGenericCapabilityForSensor.equalsIgnoreCase("UnknownPhenomenon" ))
                        //                                            {
                        sensorModelsOfSmartDevVector.add(tmpSensorModel);
                        sensorModels_IDs_OfSmartDevVector.add(tmpSensorModel.getSmid());
                        //logger.debug("HER HE R HER : Adding id: "+ Integer.toString(thedigestInt)+ " for cap: " + tmpGenericCapabilityForSensor);
                        //Integer thedigestIntAlt = (tmpGenericCapabilityForSensor).hashCode();
                        //if (thedigestIntAlt < 0) thedigestIntAlt = thedigestIntAlt * (-1);
                        //logger.debug("HER HE R HER : WHEREAS EXPERIMENT id: "+ Integer.toString(thedigestIntAlt)+ " for cap: " + tmpGenericCapabilityForSensor);

                        //                                            }

                        if (!myAllCapabilitiesToSensorModelIds.containsKey(tmpGenericCapabilityForSensor)) {
                            myAllCapabilitiesToSensorModelIds.put(tmpGenericCapabilityForSensor,
                                    new Vector<Integer>());
                            givGatewayInfo.getAllGwGenericCapabilities().put(tmpGenericCapabilityForSensor,
                                    new Vector<CSensorModel>());
                        }
                        // When we reach this part, we already have a key that corresponds to a unique sensor capability description
                        if (!myAllCapabilitiesToSensorModelIds.get(tmpGenericCapabilityForSensor)
                                .contains(Integer.valueOf(tmpSensorModel.getSmid()))) {
                            myAllCapabilitiesToSensorModelIds.get(tmpGenericCapabilityForSensor)
                                    .addElement(tmpSensorModel.getSmid());
                            givGatewayInfo.getAllGwGenericCapabilities().get(tmpGenericCapabilityForSensor)
                                    .addElement(tmpSensorModel);
                        }
                        // }
                    }
                }
            }
            if (!sensorModelsOfSmartDevVector.isEmpty()) {
                // TODO: FILTER OUT UNSUPPORTED OR COMPLEX NODES!!!!
                // For demo purposes let's keep only the first floor and iSense devices
                String isensePrefixTag = "isense";
                String arduinoTag = "arduino";
                String telosBTag = "telosb";
                String roomsOnZeroFloor_PartI_PrefixTag = "0.I.";
                String roomsOnZeroFloor_PartII_PrefixTag = "0.II.";

                if (!VitroGatewayService.getVitroGatewayService().getAssignedGatewayUniqueIdFromReg()
                        .equalsIgnoreCase("vitrogw_hai")) {
                    if ((!tmpSmartDev.getLocationDesc().isEmpty())
                            && ((tmpSmartDev.getLocationDesc().length() >= roomsOnZeroFloor_PartI_PrefixTag
                                    .length()
                                    && tmpSmartDev.getLocationDesc()
                                            .substring(0, roomsOnZeroFloor_PartI_PrefixTag.length())
                                            .equalsIgnoreCase(roomsOnZeroFloor_PartI_PrefixTag))
                                    || (tmpSmartDev.getLocationDesc()
                                            .length() >= roomsOnZeroFloor_PartII_PrefixTag.length()
                                            && tmpSmartDev.getLocationDesc()
                                                    .substring(0, roomsOnZeroFloor_PartII_PrefixTag.length())
                                                    .equalsIgnoreCase(roomsOnZeroFloor_PartII_PrefixTag)))
                            && (!tmpSmartDev.getName().isEmpty())
                            && ((tmpSmartDev.getName().length() >= isensePrefixTag.length()
                                    && tmpSmartDev.getName().substring(0, isensePrefixTag.length())
                                            .equalsIgnoreCase(isensePrefixTag))
                                    || (tmpSmartDev.getName().length() >= arduinoTag.length()
                                            && tmpSmartDev.getName().substring(0, arduinoTag.length())
                                                    .equalsIgnoreCase(arduinoTag)))) {
                        currSmartDevicesVec.addElement(tmpSmartDev);
                    }
                } else if (VitroGatewayService.getVitroGatewayService().getAssignedGatewayUniqueIdFromReg()
                        .equalsIgnoreCase("vitrogw_hai")) {
                    //logger.debug("I am hai");
                    if ((!tmpSmartDev.getLocationDesc().isEmpty())
                            && ((tmpSmartDev.getLocationDesc().length() >= roomsOnZeroFloor_PartI_PrefixTag
                                    .length()
                                    && tmpSmartDev.getLocationDesc()
                                            .substring(0, roomsOnZeroFloor_PartI_PrefixTag.length())
                                            .equalsIgnoreCase(roomsOnZeroFloor_PartI_PrefixTag))
                                    || (tmpSmartDev.getLocationDesc()
                                            .length() >= roomsOnZeroFloor_PartII_PrefixTag.length()
                                            && tmpSmartDev.getLocationDesc()
                                                    .substring(0, roomsOnZeroFloor_PartII_PrefixTag.length())
                                                    .equalsIgnoreCase(roomsOnZeroFloor_PartII_PrefixTag)))
                            && (!tmpSmartDev.getName().isEmpty())
                            && ((tmpSmartDev.getName().length() >= telosBTag.length() && tmpSmartDev.getName()
                                    .substring(0, telosBTag.length()).equalsIgnoreCase(telosBTag)))) {
                        String myoldid = tmpSmartDev.getId();
                        tmpSmartDev.setId(dictionaryUberdustUrnToHaiUrnName.get(myoldid));
                        currSmartDevicesVec.addElement(tmpSmartDev);
                    }
                }

                //#####################################
            }
        } // ends for loop over all smartdevices discovered!

    } catch (Exception ex) {
        logger.error(ex.getMessage());
    }

    return myGatewayForSmartDevs;

}

From source file:alter.vitro.vgw.wsiadapter.WsiUberDustCon.java

/**
 * TODO: IMPORTANT !!!//from  ww  w  .  ja  v a 2s. c om
 *
 * Updates the list with the controlled WSI's capabilities
 */
public synchronized CGatewayWithSmartDevices createWSIDescr(CGateway givGatewayInfo) {
    //
    //
    //
    if (DEBUG_OFFLINE_MODE) {
        return DEBUG_offline_createWSIDescr(givGatewayInfo);
    }
    //
    Vector<CSmartDevice> currSmartDevicesVec = new Vector<CSmartDevice>();
    CGatewayWithSmartDevices myGatewayForSmartDevs = new CGatewayWithSmartDevices(givGatewayInfo,
            currSmartDevicesVec);

    // An auxiliary structure that maps Unique Generic Capability Descriptions to Lists of SensorTypes ids.
    HashMap<String, Vector<Integer>> myAllCapabilitiesToSensorModelIds = new HashMap<String, Vector<Integer>>();

    //
    // ##########################################################################################################################################
    // ##########################################################################################################################################
    //
    /*
     * TODO: optimize the status/resource retrieval process for uberdust!
     * TODO: Take into account the mote status before ADDING it to the gateway description list (++++ LATER)
     *         For now we assume that the queries to the actual WSN are handled by the middleware.
     *         We search the uberdust "database" for data. (but we can still perform actions to affect the WSI!)
     *
     *         The plan is for a future service
     *         where a peer could submit queries for submission in the actual WSNs, and subsequently gather the data
     *         of the results. (e.g. administration service>reprogramming service)
     */
    HttpClient httpclient = new DefaultHttpClient();
    try {
        //
        //
        // TODO: x, y, z can be used with wisedb Coordinate.java (look code) to produce GoogleEarth Coordinates (what ISO is that? Can it be advertised in SensorML for IDAS ?)
        // TODO: make use of Description and Type and Room Fields when available ?
        // TODO: Make a summary, how many valid from those found in uberdust? How many were registered successfully? How many measurements were registered successfully?
        //
        //
        boolean gotResponseFromHttpNodesGet = false;
        boolean gotResponseFromHttpNodes_STATUS_Get = false;
        boolean gotResponseFromHttpNodes_ADMINSTATUS_Get = false;

        String responseBodyStr = "";

        HttpGet httpUberdustNodesGet = new HttpGet(uberdustNodesGetRestUri);
        HttpResponse httpUberdustNodesGetResponse = httpclient.execute(httpUberdustNodesGet);

        int httpUberdustNodesGetResponseStatusCode = httpUberdustNodesGetResponse.getStatusLine()
                .getStatusCode();
        HttpEntity httpUberdustNodesGetResponseEntity = httpUberdustNodesGetResponse.getEntity();
        if (httpUberdustNodesGetResponseEntity != null) {

            responseBodyStr = EntityUtils.toString(httpUberdustNodesGetResponseEntity);
            if (httpUberdustNodesGetResponseStatusCode != 200) {
                // responseBody will have the error response
                logger.debug("--------ERROR Response: " + httpUberdustNodesGetResponseStatusCode
                        + "------------------------------");
                logger.debug(responseBodyStr);
                logger.debug("----------------------------------------");
            } else {
                //logger.debug("--------OK Response: "+ httpUberdustNodesGetResponseStatusCode+"------------------------------");
                //
                String[] nodeUrnsInUberdust = responseBodyStr.split("\\r?\\n");
                int totalNodeUrnsInUberdust = nodeUrnsInUberdust.length;

                HttpGet httpUberdustNodes_STATUS_Get = new HttpGet(uberdustNodes_Status_GetRestUri);
                HttpResponse httpUberdustNodes_STATUS_GetResponse = httpclient
                        .execute(httpUberdustNodes_STATUS_Get);

                int httpUberdustNodes_STATUS_GetResponseStatusCode = httpUberdustNodes_STATUS_GetResponse
                        .getStatusLine().getStatusCode();
                HttpEntity httpUberdustNodes_STATUS_GetResponseEntity = httpUberdustNodes_STATUS_GetResponse
                        .getEntity();
                if (httpUberdustNodes_STATUS_GetResponseEntity != null) {
                    String responseBody_STATUS_Str = EntityUtils
                            .toString(httpUberdustNodes_STATUS_GetResponseEntity);
                    if (httpUberdustNodes_STATUS_GetResponseStatusCode != 200) {
                        // responseBody_STATUS_Str will have the error response
                        logger.debug("--------ERROR Response: " + httpUberdustNodes_STATUS_GetResponseStatusCode
                                + "------------------------------");
                        logger.debug(responseBody_STATUS_Str);
                        logger.debug("----------------------------------------");
                    } else {
                        //logger.debug("--------OK Response: "+ httpUberdustNodes_STATUS_GetResponseStatusCode+"------------------------------");

                        String[] nodeAndLastCapReadingsUrnsInUberdust = responseBody_STATUS_Str
                                .split("\\r?\\n");
                        int totalNodeWithCapsInUberdust = nodeAndLastCapReadingsUrnsInUberdust.length;

                        //TODO: test this:
                        Vector<String> allFaultyNodesUrns = getFaultyNodes();

                        // LOOP OVER EVERY NODE (smart device), and for each node, get its capabilities from the second response (responseBody_STATUS_Str)
                        for (String aNodeUrnsInUberdust : nodeUrnsInUberdust) {
                            if (allFaultyNodesUrns.contains(aNodeUrnsInUberdust)) {
                                logger.debug("Skiipping node: " + aNodeUrnsInUberdust);
                                continue; //skip faulty nodes!
                            }

                            //                                logger.debug("Iteration " + String.valueOf(k+1) + " of " + String.valueOf(totalNodeUrnsInUberdust));
                            //                                logger.debug(nodeUrnsInUberdust[k]);

                            Vector<Integer> sensorModels_IDs_OfSmartDevVector = new Vector<Integer>();// todo: fix this redundancy!
                            Vector<CSensorModel> sensorModelsOfSmartDevVector = new Vector<CSensorModel>();
                            CSmartDevice tmpSmartDev = new CSmartDevice(aNodeUrnsInUberdust,
                                    "", /* smart device type name */
                                    "", /* location description e.g. room1*/
                                    new GeodesicPoint(), /*  */
                                    sensorModels_IDs_OfSmartDevVector);

                            // TODO: Add an extra early for loop to update the fields for the attributes of the SmartDevice such as:
                            //      Eventually if the SmartDev has NO other valid sensors (e.g. observation sensors or actuators) then it won't be added !
                            String tmp_longitude = "";
                            String tmp_latitude = "";
                            String tmp_altitude = "";

                            for (String aNodeAndLastCapReadingsUrnsInUberdust1 : nodeAndLastCapReadingsUrnsInUberdust) {
                                //to update the device attributes!
                                String[] nodeCapAndReadingRowItems = aNodeAndLastCapReadingsUrnsInUberdust1
                                        .split("\\t");
                                if (nodeCapAndReadingRowItems.length > 3 && nodeCapAndReadingRowItems[0]
                                        .compareToIgnoreCase(aNodeUrnsInUberdust) == 0) {

                                    // [0] is mote (smart device) id
                                    // [1] is capability
                                    // [2] is timestamp
                                    // [3] is measurement value
                                    if ((nodeCapAndReadingRowItems[1] != null)
                                            && !(nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase(""))) {
                                        if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("room")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            {
                                                tmpSmartDev
                                                        .setLocationDesc(nodeCapAndReadingRowItems[3].trim());
                                            }
                                        } else if (nodeCapAndReadingRowItems[1].trim()
                                                .equalsIgnoreCase("nodetype")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            tmpSmartDev.setName(nodeCapAndReadingRowItems[3].trim());
                                        } else if (nodeCapAndReadingRowItems[1].trim()
                                                .equalsIgnoreCase("description")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            //TODO: do we need this?

                                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("x")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            //TODO: we need the function to derive a valid longitude from the uberdust value (pending)
                                            tmp_longitude = nodeCapAndReadingRowItems[3].trim();
                                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("y")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            //TODO: we need the function to derive a valid latitude)
                                            tmp_latitude = nodeCapAndReadingRowItems[3].trim();
                                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("z")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            //altitude is in meters (assumption)
                                            tmp_altitude = nodeCapAndReadingRowItems[3].trim();

                                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("phi")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            //TODO: do we need this?
                                        } else if (nodeCapAndReadingRowItems[1].trim().equalsIgnoreCase("theta")
                                                && nodeCapAndReadingRowItems[3] != null
                                                && !nodeCapAndReadingRowItems[3].trim().equalsIgnoreCase("")) {
                                            //TODO: do we need this?
                                        }
                                    }

                                }
                            } // end of first round of for loop for attributes
                            if (!tmp_latitude.equalsIgnoreCase("") && !tmp_longitude.equalsIgnoreCase("")
                                    && !tmp_altitude.equalsIgnoreCase("")) {
                                tmpSmartDev.setGplocation(
                                        new GeodesicPoint(tmp_latitude, tmp_longitude, tmp_altitude));
                            }

                            //
                            // Again same loop for measurement and actuation capabilities!
                            //
                            for (String aNodeAndLastCapReadingsUrnsInUberdust : nodeAndLastCapReadingsUrnsInUberdust) {
                                String[] nodeCapAndReadingRowItems = aNodeAndLastCapReadingsUrnsInUberdust
                                        .split("\\t");
                                if (nodeCapAndReadingRowItems.length > 3 && nodeCapAndReadingRowItems[0]
                                        .compareToIgnoreCase(aNodeUrnsInUberdust) == 0) //we are at the capabilities of the current smartdevice
                                {
                                    // [0] is mote (smart device) id
                                    // [1] is capability
                                    // [2] is measurement value
                                    // [3] is timestamp
                                    //                                        logger.debug(nodeCapAndReadingRowItems[1]);
                                    // TODO: FILTER OUT UNSUPPORTED OR COMPLEX CAPABILITIES!!!!
                                    // Since uberdust does not distinguish currenlty between sensing/actuating capabilities and properties, we need to filter out manually
                                    // everything we don't consider a sensing/actuating capability.
                                    // Another filtering out is done at a later stage with the SensorMLMessageAdapter, which will filter out the capabilities not supported by IDAS
                                    // TODO: it could be nice to have this filtering unified.
                                    if ((nodeCapAndReadingRowItems[1] != null)
                                            && (nodeCapAndReadingRowItems[1].trim().compareTo("") != 0)
                                            && !getSimpleCapForUberdustUrn(nodeCapAndReadingRowItems[1].trim())
                                                    .equalsIgnoreCase("UnknownPhenomenon")) {

                                        //todo: this is just to support actuation during the demo. The code should be improved later on:
                                        // todo: replace with regex
                                        //if(getSimpleCapForUberdustUrn(nodeCapAndReadingRowItems[1].trim()).equalsIgnoreCase("switchlight1")
                                        //        || getSimpleCapForUberdustUrn(nodeCapAndReadingRowItems[1].trim()).equalsIgnoreCase("switchlight2")
                                        //        ||  getSimpleCapForUberdustUrn(nodeCapAndReadingRowItems[1].trim()).equalsIgnoreCase("switchlight3")
                                        //        ||getSimpleCapForUberdustUrn(nodeCapAndReadingRowItems[1].trim()).equalsIgnoreCase("switchlight4") )
                                        //{

                                        //}
                                        // else
                                        // {
                                        //TODO: don't get light measurements from arduinos even if they advertise light as a capability

                                        // The model id is set as the hashcode of the capability name appended with the model type of the device.
                                        // Perhaps this should be changed to something MORE specific
                                        // TODO: the units should be set here as we know them. Create a small dictionary to set them!
                                        // TODO: the non-observation sensors/ non-actuation should be filtered here!! the Name for the others should be "UnknownPhenomenon"

                                        String tmpGenericCapabilityForSensor = getSimpleCapForUberdustUrn(
                                                nodeCapAndReadingRowItems[1].trim());
                                        Integer thedigestInt = (tmpGenericCapabilityForSensor + "-"
                                                + tmpSmartDev.getName()).hashCode();
                                        if (thedigestInt < 0)
                                            thedigestInt = thedigestInt * (-1);

                                        CSensorModel tmpSensorModel = new CSensorModel(
                                                givGatewayInfo.getId(), /*Gateway Id*/
                                                thedigestInt, /*Sensor Model Id */
                                                (tmpGenericCapabilityForSensor + "-"
                                                        + tmpSmartDev.getName()), /* Sensor Model name */
                                                CSensorModel.numericDataType, /* Data type*/ // TODO: later on this should be adjustable!!!
                                                CSensorModel.defaultAccuracy, /* Accuracy */
                                                CSensorModel.defaultUnits) /* Units */; // TODO: this should be set when it is known!!!
                                        //                                            if(!tmpGenericCapabilityForSensor.equalsIgnoreCase("UnknownPhenomenon" ))
                                        //                                            {
                                        sensorModelsOfSmartDevVector.add(tmpSensorModel);
                                        sensorModels_IDs_OfSmartDevVector.add(tmpSensorModel.getSmid());
                                        //                                            }

                                        if (!myAllCapabilitiesToSensorModelIds
                                                .containsKey(tmpGenericCapabilityForSensor)) {
                                            myAllCapabilitiesToSensorModelIds.put(tmpGenericCapabilityForSensor,
                                                    new Vector<Integer>());
                                            givGatewayInfo.getAllGwGenericCapabilities().put(
                                                    tmpGenericCapabilityForSensor, new Vector<CSensorModel>());
                                        }
                                        // When we reach this part, we already have a key that corresponds to a unique sensor capability description
                                        if (!myAllCapabilitiesToSensorModelIds
                                                .get(tmpGenericCapabilityForSensor)
                                                .contains(Integer.valueOf(tmpSensorModel.getSmid()))) {
                                            myAllCapabilitiesToSensorModelIds.get(tmpGenericCapabilityForSensor)
                                                    .addElement(tmpSensorModel.getSmid());
                                            givGatewayInfo.getAllGwGenericCapabilities()
                                                    .get(tmpGenericCapabilityForSensor)
                                                    .addElement(tmpSensorModel);
                                        }
                                        // }
                                    }
                                }
                            }
                            if (!sensorModelsOfSmartDevVector.isEmpty()) {
                                // TODO: FILTER OUT UNSUPPORTED OR COMPLEX NODES!!!!
                                // For demo purposes let's keep only the first floor and iSense devices
                                String isensePrefixTag = "isense";
                                String arduinoTag = "arduino";
                                String telosBTag = "telosb";
                                String roomsOnZeroFloor_PartI_PrefixTag = "0.I.";
                                String roomsOnZeroFloor_PartII_PrefixTag = "0.II.";

                                if (!VitroGatewayService.getVitroGatewayService()
                                        .getAssignedGatewayUniqueIdFromReg().equalsIgnoreCase("vitrogw_hai")) {
                                    if ((!tmpSmartDev.getLocationDesc().isEmpty())
                                            && ((tmpSmartDev.getLocationDesc()
                                                    .length() >= roomsOnZeroFloor_PartI_PrefixTag.length()
                                                    && tmpSmartDev.getLocationDesc()
                                                            .substring(0,
                                                                    roomsOnZeroFloor_PartI_PrefixTag.length())
                                                            .equalsIgnoreCase(roomsOnZeroFloor_PartI_PrefixTag))
                                                    || (tmpSmartDev.getLocationDesc()
                                                            .length() >= roomsOnZeroFloor_PartII_PrefixTag
                                                                    .length()
                                                            && tmpSmartDev.getLocationDesc()
                                                                    .substring(0,
                                                                            roomsOnZeroFloor_PartII_PrefixTag
                                                                                    .length())
                                                                    .equalsIgnoreCase(
                                                                            roomsOnZeroFloor_PartII_PrefixTag)))
                                            && (!tmpSmartDev.getName().isEmpty())
                                            && ((tmpSmartDev.getName().length() >= isensePrefixTag.length()
                                                    && tmpSmartDev.getName()
                                                            .substring(0, isensePrefixTag.length())
                                                            .equalsIgnoreCase(isensePrefixTag))
                                                    || (tmpSmartDev.getName().length() >= arduinoTag.length()
                                                            && tmpSmartDev.getName()
                                                                    .substring(0, arduinoTag.length())
                                                                    .equalsIgnoreCase(arduinoTag)))) {
                                        currSmartDevicesVec.addElement(tmpSmartDev);
                                    }
                                } else if (VitroGatewayService.getVitroGatewayService()
                                        .getAssignedGatewayUniqueIdFromReg().equalsIgnoreCase("vitrogw_hai")) {
                                    //logger.debug("I am hai");
                                    if ((!tmpSmartDev.getLocationDesc().isEmpty())
                                            && ((tmpSmartDev.getLocationDesc()
                                                    .length() >= roomsOnZeroFloor_PartI_PrefixTag.length()
                                                    && tmpSmartDev.getLocationDesc()
                                                            .substring(0,
                                                                    roomsOnZeroFloor_PartI_PrefixTag.length())
                                                            .equalsIgnoreCase(roomsOnZeroFloor_PartI_PrefixTag))
                                                    || (tmpSmartDev.getLocationDesc()
                                                            .length() >= roomsOnZeroFloor_PartII_PrefixTag
                                                                    .length()
                                                            && tmpSmartDev.getLocationDesc()
                                                                    .substring(0,
                                                                            roomsOnZeroFloor_PartII_PrefixTag
                                                                                    .length())
                                                                    .equalsIgnoreCase(
                                                                            roomsOnZeroFloor_PartII_PrefixTag)))
                                            && (!tmpSmartDev.getName().isEmpty())
                                            && ((tmpSmartDev.getName().length() >= telosBTag.length()
                                                    && tmpSmartDev.getName().substring(0, telosBTag.length())
                                                            .equalsIgnoreCase(telosBTag)))) {
                                        String myoldid = tmpSmartDev.getId();
                                        tmpSmartDev.setId(dictionaryUberdustUrnToHaiUrnName.get(myoldid));
                                        currSmartDevicesVec.addElement(tmpSmartDev);
                                    }
                                }

                                //#####################################
                            }
                        } // ends for loop over all smartdevices discovered!
                    } //if GET STATUS response code is OK!
                } // if GET STATUS response entity is NOT null
            } //if get list of nodes replied validly
        } //if get list of nodes response entity is NOT null
    } catch (Exception e) {
        logger.debug("error::" + e.getMessage());

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate de-allocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }

    // DEBUG: for debugging
    //GatewayDescriptionAdvertisement myGwAdDesc = new GatewayDescriptionAdvertisement(givGatewayInfo,
    //        currSmartDevicesVec,
    //        myAllCapabilitiesToSensorModels);
    //
    return myGatewayForSmartDevs;
}

From source file:focusedCrawler.util.parser.PaginaURL.java

protected void separadorTextoCodigo(String arquivo) { // arquivo equivale ao codigo HTML da pagina
    if (codes.size() == 0) {
        loadHashCodes();//w ww  . java 2s . c  om
    }

    //       System.out.println(arquivo);   
    boolean obj_isRDF = false;
    boolean ignorar_espacos = true;
    boolean tag_tipo_fim = false;
    boolean tag_tipo_vazia = true;
    boolean em_script = false;
    boolean ehInicioALT = true;
    boolean em_titulo = false;
    boolean em_option = false;
    boolean em_comentario = false;
    int num_comentario = 0;
    int PONTUACAO_PALAVRAS_TEXTO = 2;
    int PONTUACAO_PALAVRAS_OPTION = 1;
    int PONTUACAO_PALAVRAS_URL = 3;
    int PONTUACAO_PALAVRAS_META = 1;
    int PONTUACAO_PALAVRAS_TITULO = 7;
    int PONTUACAO_PALAVRAS_DESCRIPTION = 5;
    int PONTUACAO_PALAVRAS_ALT = 1;
    int posicao_da_palavra = 1;
    int numOfHtmlTags = 0;

    // UTILIZANDO AS PALAVRAS DA URL COMO INFORMACAO TEXTUAL
    if (pagina != null && !filterURL) {

        StringTokenizer url_pontos = new StringTokenizer(pagina.getHost(), "./:");

        while (url_pontos.hasMoreTokens()) {
            String parte_host = url_pontos.nextToken();

            if (!parte_host.equals("www") && !parte_host.equals("org") && !parte_host.equals("gov")
                    && !parte_host.equals("com") && !parte_host.equals("br")) {

                boolean adicionou = adicionaAoVetorDeTexto(parte_host);

                if (adicionou) {
                    adicionaTermoPosicao(parte_host, posicao_da_palavra); // atualiza o centroide
                    adicionaPontuacaoTermo(parte_host, PONTUACAO_PALAVRAS_URL);
                    String parte_host_sem_acento = Acentos.retirarNotacaoHTMLAcentosANSI(parte_host);
                    if (!parte_host_sem_acento.equals(parte_host)) {
                        adicionou = adicionaAoVetorDeTexto(parte_host_sem_acento);
                        if (adicionou) {
                            adicionaTermoPosicao(parte_host_sem_acento, posicao_da_palavra); // atualiza o centroide
                            adicionaPontuacaoTermo(parte_host_sem_acento, PONTUACAO_PALAVRAS_URL);
                        }
                    }
                    posicao_da_palavra++;
                }
            }
        }

    }

    boolean em_body = false;
    boolean em_meta_robots = false;
    boolean tagScript = false;
    boolean tagTitulo = false;
    boolean tagBody = false;
    boolean tagOption = false;
    int pos_caracter_especial = -1;
    char quote_char = '\0';
    URL base = pagina; // pagina = URL da pagina atual...

    Vector frames = new Vector();
    char c = '\0';
    char ant1 = '\0';
    char ant2 = '\0';
    int n = 0;
    int n_anterior = 0;
    String str = "";
    String anchor = "";
    int numOfwordsAnchor = 0;

    LinkNeighborhood ln = null;
    String tagName = "";
    String lastTag = "";
    String atributo = "";

    boolean insideATag = false;

    boolean em_meta_description = false; // thiago
    String str_da_metatag_description = null; // thiago

    final int INICIO = 1;
    final int TAG_NAME = 2;
    final int TOKEN_PALAVRA = 3;
    final int PALAVRA = 4;
    final int ATRIBUTO = 5;
    final int FECHANDO = 6;
    final int IGUAL = 7;
    final int VALOR = 8;
    final int META_TAG = 10;
    final int ALT_TAG = 11;
    int estado = INICIO;

    try {

        //          FileOutputStream fout = null;
        //          DataOutputStream dout = null;
        //System.out.println("FORM!!! : " + form.getURL());
        //            try {
        //              fout = new FileOutputStream("/home/lbarbosa/test");
        //               dout = new DataOutputStream( fout );
        //              dout.writeBytes("begin");
        //
        //            }
        //            catch (FileNotFoundException ex) {
        //              ex.printStackTrace();
        //                 }
        //            catch (IOException ex) {
        //              ex.printStackTrace();
        //         }

        while (n < arquivo.length()) {
            if (n_anterior < n) { /* we advanced a character */
                ant1 = ant2;
                ant2 = c;
            }

            n_anterior = n;
            c = arquivo.charAt(n);
            //                System.out.print(c+"");
            //                int ascii = (int) c;
            //                System.out.print(ascii);
            //                System.out.println("");
            //                dout.writeBytes(c+"");
            //                dout.flush();
            //                if(c=='\u0000'){
            //                   organizaDados();
            //                   return;
            //                }
            if (em_comentario && num_comentario > 0) {
                if ((ant1 == '-') && (ant2 == '-') || (c == '>')) {
                    num_comentario--;
                    if (num_comentario == 0)
                        em_comentario = false;
                }

                n++;
            } else if (ignorar_espacos) {
                if (Character.isWhitespace(c)) {
                    n++;
                } else {
                    ignorar_espacos = false;
                }
            } else {
                boolean fimDeString = false;

                switch (estado) {

                case INICIO:

                    /* INICIO - Esperando texto ou caracter de abertura de tag '<' */

                    // System.out.println("Entrei no inicio e caractere=" + c);
                    if (c == '<') {
                        estado = TAG_NAME;
                        tagName = "";
                        tag_tipo_fim = false;
                        em_meta_robots = false;
                        n++;
                    } else {
                        estado = TOKEN_PALAVRA;
                        pos_caracter_especial = -1;
                    }

                    quote_char = '\0';

                    break;

                case TOKEN_PALAVRA:
                    //                       if(str.contains("1044")){
                    //                          System.out.println("TEST");
                    //                       }
                    /* faz o token da string */
                    if ((caracterFazParteDePalavra(c)) || (c == ';') || (c == '&')) {
                        str += converteChar(c);
                        n++;
                        int begin = str.indexOf("&#");
                        int end = str.indexOf(";");
                        if (begin != -1 && end != -1) {
                            String specialchar = str.substring(begin + 2, end);
                            try {
                                int hex = Integer.parseInt(specialchar);
                                char uni = (char) hex;
                                String unicode = uni + "";
                                str = str.substring(0, begin) + unicode;
                                //                             System.out.println(unicode);
                                pos_caracter_especial = -1;
                                continue;
                            } catch (Exception e) {
                                // TODO: handle exception
                            }
                        }
                        if (str.toLowerCase().contains("&ntilde;")) {
                            str = str.toLowerCase().replace("&ntilde;", "n");
                            pos_caracter_especial = -1;
                            continue;
                        }
                        if (str.contains("")) {
                            str = str.replace("", "n");
                            pos_caracter_especial = -1;
                            continue;
                        }
                        if (c == '&') {
                            pos_caracter_especial = n;
                        } else
                        //                               System.out.println(str + ":" + pos_caracter_especial);
                        if (pos_caracter_especial != -1) {
                            int posicao = str.length() - (n - pos_caracter_especial) - 1;
                            char ch = caracterEspecial(str, posicao);

                            if (ch != '\0') {
                                if (caracterFazParteDePalavra(ch)) {
                                    str = str.substring(0, posicao) + converteChar(ch);
                                } else {
                                    str = str.substring(0, posicao);
                                    estado = PALAVRA;

                                    if (em_titulo) {
                                        titulo += str + ch;
                                    }
                                }
                            }
                            if ((c == ';') || (n - pos_caracter_especial) > 9) {
                                pos_caracter_especial = -1;
                            }
                        }
                    } else {
                        estado = PALAVRA;

                        if (em_titulo) {
                            titulo += str;
                        }
                        if (!(c == '<')) {
                            if (em_titulo) {
                                //                                   if(!Character.isLetterOrDigit(c)){
                                //                                      c = ' ';
                                //                                   }
                                titulo += c;
                            }

                            n++;
                        }
                    }

                    break;

                case PALAVRA:
                    //                      System.out.println("PALAVRA:"+lastTag);
                    if (insideATag) {
                        anchor = anchor + " " + str.toLowerCase();

                        numOfwordsAnchor++;
                        //                                                  insideATag = false;

                        //                          System.out.println("ANCHOR:"+anchor);
                    }
                    //                      if(anchor.indexOf("school") != -1){
                    //                        System.out.println("TEST");
                    //                      }
                    /* PALAVRA - palavra pronta */
                    if (!em_script && (str.length() > 0)) {
                        if (em_body && paragrafo.length() + str.length() < MAX_PARAGRAPH_SIZE) {
                            if (Character.isWhitespace(c)) {
                                paragrafo += str + c; // atualiza variavel paragrafo
                            } else {
                                paragrafo += str + " ";
                            }
                        }

                        if (!em_titulo) {
                            boolean adicionou = adicionaAoVetorDeTexto(str);
                            if (adicionou) {
                                around.add(str);
                                adicionaTermoPosicao(str, posicao_da_palavra); // atualiza o centroide
                                if (em_option) {
                                    adicionaPontuacaoTermo(str, PONTUACAO_PALAVRAS_OPTION);
                                } else {
                                    adicionaPontuacaoTermo(str, PONTUACAO_PALAVRAS_TEXTO);
                                }
                                String str_sem_acento = Acentos.retirarNotacaoHTMLAcentosANSI(str);
                                if (!str_sem_acento.equals(str)) {
                                    adicionou = adicionaAoVetorDeTexto(str_sem_acento);
                                    if (adicionou) {
                                        adicionaTermoPosicao(str_sem_acento, posicao_da_palavra); // atualiza o centroide
                                        if (em_option) {
                                            adicionaPontuacaoTermo(str_sem_acento, PONTUACAO_PALAVRAS_OPTION);
                                        } else {
                                            adicionaPontuacaoTermo(str_sem_acento, PONTUACAO_PALAVRAS_TEXTO);
                                        }
                                    }
                                }
                                posicao_da_palavra++;
                            }
                        } else {
                            boolean adicionou = adicionaAoVetorDeTexto(str);
                            if (adicionou) {
                                adicionaTermoPosicao(str, posicao_da_palavra); // atualiza o centroide
                                adicionaPontuacaoTermo(str, PONTUACAO_PALAVRAS_TITULO);
                                String str_sem_acento = Acentos.retirarNotacaoHTMLAcentosANSI(str);
                                if (!str_sem_acento.equals(str)) {
                                    adicionou = adicionaAoVetorDeTexto(str_sem_acento);
                                    if (adicionou) {
                                        adicionaTermoPosicao(str_sem_acento, posicao_da_palavra); // atualiza o centroide
                                        adicionaPontuacaoTermo(str_sem_acento, PONTUACAO_PALAVRAS_TITULO);
                                    }
                                }
                                posicao_da_palavra++;
                            }
                        }
                    }

                    estado = INICIO;
                    ignorar_espacos = true;
                    str = "";

                    break;

                case TAG_NAME:

                    /* TAG_NAME - terminated by space, \r, \n, >, / */
                    if (em_script) {
                        if (c != '>') {
                            if ("/script".startsWith(str + c) || "/SCRIPT".startsWith(str + c)
                                    || "/style".startsWith(str + c) || "/STYLE".startsWith(str + c)) {
                                str += c;
                            } else {
                                str = "";
                                estado = INICIO;
                            }
                            n++;
                        } else if (c == '>') {
                            if (str.equalsIgnoreCase("/script") || str.equalsIgnoreCase("/style")) {
                                fimDeString = true;
                                tag_tipo_fim = true;
                                tagScript = true;
                                estado = FECHANDO;
                            } else {
                                n++;
                            }
                        }
                    } else {
                        if (str.equals("BASE")) {
                            //                                System.out.println("EM TAG_NAME, str="+str + ", c="+c+", tagTitulo="+tagTitulo);
                            if (c == '>') {
                                estado = FECHANDO;
                            } else {
                                n++;
                            }
                        } else {
                            //                                if ((c == '"') || (c == '\'')) {
                            //                                if ((c == '\'')) {
                            //                                    organizaDados(); //new
                            //                                    return;    /* error - these are not allowed in tagname */
                            //                                } else 
                            if (c == ' ') {

                                /*
                                 * Note: Both mozilla and XML don't allow any spaces between < and tagname.
                                 * Need to check for zero-length tagname.
                                 */
                                //                                    if (str.length() == 0) {
                                //                                        organizaDados(); //new
                                //                                        return;    /* str is the buffer we're working on */
                                //                                    }

                                fimDeString = true;
                                estado = ATRIBUTO;
                                ignorar_espacos = true;
                                n++;
                            } else if (c == '/') {
                                if (tagName.length() == 0) {
                                    tag_tipo_fim = true; /* indicates end tag if no tag name read yet */
                                } else if (obj_isRDF) { /* otherwise its an empty tag (RDF only) */
                                    fimDeString = true;
                                    tag_tipo_vazia = true;
                                    estado = FECHANDO;
                                }
                                //                                    else {
                                //                                        organizaDados(); //new
                                //                                        return;
                                //                                    }

                                n++;
                            } else if (c == '>') {
                                fimDeString = true;
                                //                                tag_tipo_fim = true;
                                estado = FECHANDO;
                            } else if ((c != '\r') && (c != '\n')) {

                                // System.out.println("Estou NO CAMINHO CERTO!!!!");
                                str += c;
                                n++;
                            } else {
                                fimDeString = true;
                                estado = ATRIBUTO; /* note - mozilla allows newline after tag name */
                                ignorar_espacos = true;
                                n++;
                            }

                            if (fimDeString) {
                                //if (str.equals("!--")) {    /* html comment */
                                if (str.startsWith("!--")) { /* html comment */
                                    em_comentario = true;
                                    num_comentario++;
                                    estado = INICIO;
                                } else {
                                    str = str.toLowerCase();
                                    tagName = str;
                                    tagBody = str.equals("body");
                                    tagTitulo = str.equals("title");
                                    tagOption = str.equals("option");
                                    if (tagName.equals("html")) {
                                        if (!tag_tipo_fim) {
                                            numOfHtmlTags++;
                                        } else {
                                            numOfHtmlTags--;
                                        }
                                        //                                           System.out.println(">>>>>>>>>>>>>" + numOfHtmlTags);
                                    }

                                    //if (tagTitulo) {
                                    //    System.out.println("achot tag titulo " + str);
                                    //}
                                    tagScript = str.equals("script") || str.equals("style");

                                    if (str.equals("form")) {
                                        this.forms++;
                                    }
                                }

                                str = "";
                                fimDeString = false;
                            }

                            // System.out.println("A STRING DO ATRIBUTO EH: " + str + " estado novo "+ estado);
                        }
                    }
                    break;

                case FECHANDO:
                    /* FECHANDO - expecting a close bracket, anything else is an error */
                    //                        System.out.println("END OF TAG:"+tagName);
                    //                        if(ln!=null){
                    //                          ln.setAnchor(anchor);
                    //                          System.out.println("URL---"+ln.getLink());
                    //                         System.out.println("ANC---"+ln.getAnchor());
                    //                      }

                    if ((tag_tipo_fim && tagName.equals("a")) || tagName.equals("area")) {

                        insideATag = false;
                        if (ln != null) {
                            Vector anchorTemp = new Vector();
                            //                          System.out.println("URL---"+ln.getLink());
                            //                          System.out.println("ANC---"+anchor);
                            StringTokenizer tokenizer = new StringTokenizer(anchor, " ");
                            while (tokenizer.hasMoreTokens()) {
                                anchorTemp.add(tokenizer.nextToken());
                            }
                            String[] anchorArray = new String[anchorTemp.size()];
                            anchorTemp.toArray(anchorArray);
                            ln.setAnchor(anchorArray);
                            ln.setAroundPosition(around.size());
                            ln.setNumberOfWordsAnchor(numOfwordsAnchor);
                            linkNeigh.add(ln.clone());
                            //                            anchor = "";
                            ln = null;
                        }
                        anchor = "";
                    }
                    // System.out.println("Entrei em fechando");
                    if (c == '>') {
                        if (tagScript) {

                            /* we're inside a script tag (not RDF) */
                            em_script = !tag_tipo_fim;
                        }

                        if (tagTitulo) {
                            em_titulo = !tag_tipo_fim;
                            //System.out.println("EM tag titulo " + str + ", em_titulo"+ em_titulo);
                            //System.out.println("EM tag titulo " + str + ", tag_tipo_fim"+ tag_tipo_fim);
                            //System.out.println("EM tag titulo " + str + ", tagTitulo"+ tagTitulo);
                        }

                        if (tagBody) {
                            em_body = !tag_tipo_fim;

                            // System.out.println("Entrei no estado inicial");
                        }
                        if (tagOption) {
                            em_option = !tag_tipo_fim;

                            // System.out.println("Entrei no estado inicial");
                        }
                        //                            if(tag_tipo_fim && tagName.equals("html") && numOfHtmlTags == 0){
                        //                                organizaDados();
                        //                                return;
                        //                            }

                        tagTitulo = false;
                        tagBody = false;
                        tagScript = false;
                        tagOption = false;
                        estado = INICIO;
                        str = "";
                        tagName = "";

                        numOfwordsAnchor = 0;
                        ignorar_espacos = true;
                        n++;
                    } else {

                        organizaDados(); //new
                        return; /* error */
                    }

                    break;

                case ATRIBUTO:

                    /* ATRIBUTO - expecting an attribute name, or / (RDF only) or > indicating no more attributes */

                    /*
                     * accept attributes without values, such as <tag attr1 attr2=val2>
                     * or <tag attr2=val2 attr1>
                     */
                    if (quote_char == c) {
                        quote_char = '\0'; /* close quote */
                    } else if (((c == '"') || (c == '\'')) && (quote_char == '\0')) {

                        /* start a quote if none is already in effect */
                        quote_char = c;
                    }

                    if (quote_char == '\0') {
                        if ((((c == '/') && obj_isRDF) || (c == '>')) && (str.length() == 0)) {
                            estado = FECHANDO;
                        } else if ((c == ' ') || (c == '=') || (c == '\n') || (c == '\r')
                                || ((c == '/') && obj_isRDF) || (c == '>')) {
                            atributo = str;
                            str = "";
                            estado = IGUAL;
                            //System.out.println("[ATRIBUTO c='"+c+"', estado=IGUAL], atributo="+atributo);
                            /* if non-null attribute name */
                        } else {
                            str += c;
                            n++;
                        }
                    } else {
                        str += c;
                        n++;
                    }

                    break;

                case IGUAL:
                    atributo = atributo.toLowerCase();
                    tagName = tagName.toLowerCase();

                    // System.out.println("------------------------------------");
                    // System.out.println(" A TAG NAME EH: " + tagName);
                    // if(atributo.equals("src") && tagName.equals("img") && (c == '='))
                    // {
                    // ignorar_espacos = true;
                    // estado = IMAGEM;
                    // n++;
                    // }
                    // else
                    // {
                    /****
                                            if (atributo.equals("content")
                        && tagName.equals("meta") && (c == '=')) {
                    ignorar_espacos = true;
                    estado = META_TAG;
                    n++;
                                            } else if (atributo.equals("alt")
                                && tagName.equals("img") && (c == '=')) {
                    ignorar_espacos = true;
                    estado = ALT_TAG;
                    n++;
                                            } else {
                    ***/
                    if ((c == ' ') || (c == '\n') || (c == '\r')) {
                        ignorar_espacos = true;
                        n++;
                    } else if (c == '=') {
                        ignorar_espacos = true;
                        estado = VALOR;
                        n++;
                    } else { /* no value for the attribute - error in RDF? */
                        str = "";
                        atributo = "";

                        // estado = ATRIBUTO;
                        if (c == '>') {
                            // System.out.println("Entrei aqui no MENOR QUE");
                            tagScript = false;
                            tagBody = false;
                            tagTitulo = false;
                            estado = FECHANDO;
                        } else {
                            ignorar_espacos = true;

                            // System.out.println("Entrei PARA ANDAR NA LINHA");
                            n++;
                        }
                    }
                    //                        }

                    // }
                    break;

                case ALT_TAG: // nao usa mais, foi mudado, ver no estado VALOR 
                    if (((c == ' ') || (c == '"')) && ehInicioALT) {
                        ignorar_espacos = false;

                        boolean adicionou = adicionaAoVetorDeTexto(str);
                        if (adicionou) {
                            adicionaTermoPosicao(str, posicao_da_palavra); // atualiza o centroide
                            adicionaPontuacaoTermo(str, PONTUACAO_PALAVRAS_ALT);
                            String str_sem_acento = Acentos.retirarNotacaoHTMLAcentosANSI(str);
                            if (!str_sem_acento.equals(str)) {
                                adicionou = adicionaAoVetorDeTexto(str_sem_acento);
                                if (adicionou) {
                                    adicionaTermoPosicao(str_sem_acento, posicao_da_palavra); // atualiza o centroide
                                    adicionaPontuacaoTermo(str_sem_acento, PONTUACAO_PALAVRAS_ALT);
                                }
                            }
                            posicao_da_palavra++;
                        }

                        str = "";
                        ehInicioALT = false;
                    } else {
                        if (c == '>') {
                            //                                estado = INICIO; //nao sei se esta' ok
                            estado = VALOR;
                            ehInicioALT = true;
                        } else {
                            if (c == '.' || c == ',') {
                            } else {
                                if ((c != '\0') && (c != '\r') && (c != '\n') && (c != '"')) {
                                    str += c;
                                } else {
                                    if (c == '"') {
                                        estado = ATRIBUTO;
                                        ehInicioALT = true;
                                    }
                                }
                            }
                        }
                    }

                    n++;

                    break;

                case META_TAG: // nao usa mais, foi mudado, ver no estado VALOR [ogm]
                    if ((c == ' ') || (c == '"') || (c == '\n') || (c == ',')) {
                        ignorar_espacos = false;

                        textoMeta.addElement(str); // adiciona a palavra na variavel texto

                        for (int contadorI = 0; contadorI < PONTUACAO_PALAVRAS_META; contadorI++) {
                            adicionaTermoMetaPosicao(str, textoMeta.size());
                        }

                        str = "";
                    } else {
                        if (c == '>') {
                            estado = INICIO;
                            //                                estado = VALOR;
                        } else {
                            if (c == '.' || c == ',') {
                            } else {
                                if ((c != '\0') && (c != '\r') && (c != '\n') && (c != '"')) {
                                    str += c;
                                }
                            }
                        }
                    }

                    n++;

                    break;

                case VALOR:

                    /* expecting a value, or space, / (RDF only), or > indicating end of value. */
                    /* whether the current character should be included in value */
                    boolean include = true;
                    //                        System.out.println("LENGTH:"+str.length());
                    //                        if(str.length() > 300){
                    //                          System.out.println("TEST");
                    //                        }
                    if (quote_char == c || str.length() > 10000) {
                        quote_char = '\0'; /* close quote */
                        include = false;
                    } else if (((c == '"') || (c == '\'')) && (quote_char == '\0')) {

                        /* start a quote if none is already in effect */
                        quote_char = c;
                        include = false;

                    }

                    if (quote_char == '\0') {
                        if ((c == '/') && obj_isRDF) {
                            fimDeString = true;
                            estado = FECHANDO;
                            n++;
                            //                            } else if (c == '>' || str.length() > 10000) {
                        } else if (c == '>' || str.length() > 100000) {
                            fimDeString = true;
                            estado = FECHANDO;
                        } else if ((c == ' ') || (c == '\r') || (c == '\n')) {
                            fimDeString = true;
                            ignorar_espacos = true;
                            estado = ATRIBUTO; /* if non-null value name */
                            n++;
                        } else if (include) {
                            str += c;
                            n++;
                        } else {
                            n++;
                        }
                    } else if (include) {
                        str += c;
                        n++;
                    } else {
                        n++;
                    }

                    if (fimDeString) {
                        tagName = tagName.toLowerCase();

                        //                            System.out.println("TAG:"+tagName);
                        atributo = atributo.toLowerCase();

                        //                            System.out.println("[VALOR, estado='"+estado+"', c="+c+"] "+tagName+"."+atributo+"="+str);

                        if (tagName.equals("a") && atributo.equals("href")) {
                            insideATag = true;
                            String urlTemp = adicionaLink(str, base);
                            //                                  System.out.println("----URL:"+urlTemp);
                            if (urlTemp != null && urlTemp.startsWith("http")) {
                                if (ln != null) {
                                    Vector anchorTemp = new Vector();
                                    StringTokenizer tokenizer = new StringTokenizer(anchor, " ");
                                    while (tokenizer.hasMoreTokens()) {
                                        anchorTemp.add(tokenizer.nextToken());
                                    }
                                    String[] anchorArray = new String[anchorTemp.size()];
                                    anchorTemp.toArray(anchorArray);
                                    ln.setAnchor(anchorArray);
                                    ln.setAroundPosition(around.size());
                                    ln.setNumberOfWordsAnchor(numOfwordsAnchor);
                                    linkNeigh.add(ln.clone());
                                    anchor = "";
                                    ln = null;
                                }
                                ln = new LinkNeighborhood(new URL(urlTemp));
                            }
                            //                                System.out.println("CREATE LINK:"  + urlTemp);
                        } else if (tagName.equals("link") && atributo.equals("href")) {
                            String urlTemp = adicionaLink(str, base);
                            if (urlTemp != null && urlTemp.startsWith("http")) {
                                ln = new LinkNeighborhood(new URL(urlTemp));
                            }
                            //                                System.out.println("CREATE LINK:"  + urlTemp);
                        } else if (tagName.equals("area") && atributo.equals("href")) {
                            adicionaLink(str, base);
                            String urlTemp = adicionaLink(str, base);
                            if (urlTemp != null && urlTemp.startsWith("http")) {
                                ln = new LinkNeighborhood(new URL(urlTemp));
                            }
                        } else if (tagName.equals("img") && atributo.equals("src")) {
                            if (ln != null) {
                                ln.setImgSource(str);
                            }
                            try {
                                imagens.addElement(parseLink(base, str).toString());
                            } catch (Exception e) {
                                // TODO: handle exception
                            }

                        }
                        //                            else if((tagName.equals("area") || tagName.equals("a"))&& atributo.equals("alt")){
                        //                               anchor = anchor + " " + str.toLowerCase();
                        //                            } 
                        else if (tagName.equals("frame") && atributo.equals("src")) {
                            frames.addElement(str);
                            adicionaLink(str, base);
                        } else if (tagName.equals("img") && (atributo.equals("alt") || atributo.equals("title")
                                || atributo.equals("id"))) {
                            //                                System.out.println("img.alt.str="+str);
                            Vector<String> altWords = new Vector<String>();
                            StringTokenizer st = new StringTokenizer(str);
                            while (st.hasMoreTokens()) {
                                String token = st.nextToken();
                                if (token.contains("")) {
                                    token = token.replace("", "n");
                                }
                                token = token.toLowerCase();
                                if (token.contains("&#241;")) {
                                    token = token.replace("&#241;", "n");
                                }
                                if (token.contains("&ntilde;")) {
                                    token = token.replace("&ntilde;", "n");
                                }
                                if (token.contains("")) {
                                    token = token.replace("", "n");
                                }

                                altWords.add(token);
                                if (!caracterFazParteDePalavra(token.charAt(0))) {
                                    token = token.substring(1);
                                }
                                if (token.equals("")) {
                                    break;
                                }
                                if (!caracterFazParteDePalavra(token.charAt(token.length() - 1))) {
                                    token = token.substring(0, token.length() - 1);
                                }
                                if (token.equals("")) {
                                    break;
                                }
                                boolean adicionou = adicionaAoVetorDeTexto(token);
                                if (adicionou) {
                                    adicionaTermoPosicao(token, posicao_da_palavra); // atualbejiza o centroide
                                    adicionaPontuacaoTermo(token, PONTUACAO_PALAVRAS_ALT);
                                    String token_sem_acento = Acentos.retirarNotacaoHTMLAcentosANSI(token);
                                    if (!token_sem_acento.equals(token)) {
                                        adicionou = adicionaAoVetorDeTexto(token_sem_acento);
                                        if (adicionou) {
                                            adicionaTermoPosicao(token_sem_acento, posicao_da_palavra); // atualiza o centroide
                                            adicionaPontuacaoTermo(token_sem_acento, PONTUACAO_PALAVRAS_ALT);
                                        }
                                    }
                                    posicao_da_palavra++;
                                }
                            }
                            if (ln != null) {
                                String[] current = ln.getImgAlt();
                                if (current == null) {
                                    String[] terms = new String[altWords.size()];
                                    altWords.toArray(terms);
                                    ln.setImgAlt(terms);
                                } else {
                                    String[] terms = new String[altWords.size() + current.length];
                                    int indexTerms = 0;
                                    for (int i = 0; i < current.length; i++, indexTerms++) {
                                        terms[indexTerms] = current[i];
                                    }
                                    for (int i = 0; i < altWords.size(); i++, indexTerms++) {
                                        terms[indexTerms] = altWords.elementAt(i);
                                    }
                                    ln.setImgAlt(terms);
                                }
                            }
                        } else if (tagName.equals("meta") && atributo.equals("content")) {
                            if (em_meta_description) {
                                str_da_metatag_description = str;
                                em_meta_description = false;
                                if (USAR_DESCRIPTION) {
                                    StringTokenizer st = new StringTokenizer(str);
                                    while (st.hasMoreTokens()) {
                                        String token = st.nextToken();
                                        int posicao = texto.size();
                                        boolean adicionou = adicionaAoVetorDeTexto(token);
                                        if (adicionou) {
                                            adicionaTermoPosicao(token, posicao_da_palavra); // atualiza o centroide
                                            adicionaPontuacaoTermo(token, PONTUACAO_PALAVRAS_DESCRIPTION);
                                            String token_sem_acento = Acentos
                                                    .retirarNotacaoHTMLAcentosANSI(token);
                                            if (!token_sem_acento.equals(token)) {
                                                adicionou = adicionaAoVetorDeTexto(token_sem_acento);
                                                if (adicionou) {
                                                    adicionaTermoPosicao(token_sem_acento, posicao_da_palavra); // atualiza o centroide
                                                    adicionaPontuacaoTermo(token_sem_acento,
                                                            PONTUACAO_PALAVRAS_DESCRIPTION);
                                                }
                                            }
                                            posicao_da_palavra++;
                                        }
                                    }
                                }
                            }

                            //                                System.out.println("meta.content.str="+str);
                            StringTokenizer st = new StringTokenizer(str);
                            while (st.hasMoreTokens()) {
                                String token = st.nextToken();
                                textoMeta.addElement(token); // adiciona a palavra na variavel texto
                                for (int contadorI = 0; contadorI < PONTUACAO_PALAVRAS_META; contadorI++) {
                                    adicionaTermoMetaPosicao(token, textoMeta.size());
                                }
                            }
                        } else if (tagName.equals("meta") && atributo.equals("name")) {
                            if (str.toLowerCase().equals("robot")) {
                                em_meta_robots = true;
                            }
                            if (str.toLowerCase().equals("description")
                                    || str.toLowerCase().equals("descricao")) {
                                //System.out.println("meta.description.str="+str);
                                em_meta_description = true;
                            }
                        } else if (em_meta_robots && atributo.equals("content")) {
                            if (str.toLowerCase().indexOf("noindex") != -1) {
                                noindex = true;
                            }

                            if (str.toLowerCase().indexOf("nofollow") != -1) {
                                nofollow = true;
                            }
                        } else if (tagName.equals("base") && atributo.equals("href")) {
                            try {
                                base = parseLink(pagina, str);
                            } catch (Exception e) {
                            } // ignora
                        }

                        str = "";
                        atributo = "";
                        fimDeString = false;
                    }

                    break;
                default:
                    break;
                }
            }
        }
        if (USAR_DESCRIPTION) {
            if (str_da_metatag_description != null) {
                paragrafo = str_da_metatag_description;
            }
        }
        if (estado == PALAVRA && str != null && !"".equals(str)) {
            boolean adicionou = adicionaAoVetorDeTexto(str);
            if (adicionou) {
                adicionaTermoPosicao(str, posicao_da_palavra); // atualiza o centroide
                adicionaPontuacaoTermo(str, PONTUACAO_PALAVRAS_TEXTO);
                String str_sem_acento = Acentos.retirarNotacaoHTMLAcentosANSI(str);
                if (!str_sem_acento.equals(str)) {
                    adicionou = adicionaAoVetorDeTexto(str_sem_acento);
                    if (adicionou) {
                        adicionaTermoPosicao(str_sem_acento, posicao_da_palavra); // atualiza o centroide
                        adicionaPontuacaoTermo(str_sem_acento, PONTUACAO_PALAVRAS_TEXTO);
                    }
                }
                posicao_da_palavra++;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    this.frames = frames.size();
    this.images = imagens.size();
    organizaDados();

}