Example usage for org.jdom2 Element getAttribute

List of usage examples for org.jdom2 Element getAttribute

Introduction

In this page you can find the example usage for org.jdom2 Element getAttribute.

Prototype

public Attribute getAttribute(final String attname) 

Source Link

Document

This returns the attribute for this element with the given name and within no namespace, or null if no such attribute exists.

Usage

From source file:pdf2xml.TopElementComparator.java

License:Open Source License

/**
 * Return a list of fonts on a given page.
 *//*  www  . j ava 2 s .  c o m*/
private List<Font> getFonts(Element page) {
    List<Font> fonts = new ArrayList<Font>();
    int page_number = Integer.parseInt(page.getAttribute("number").getValue());

    for (Element font : page.getChildren("fontspec")) {
        int id = Integer.parseInt(font.getAttribute("id").getValue());
        int size = Integer.parseInt(font.getAttribute("size").getValue());
        String family = font.getAttribute("family").getValue();
        String color = font.getAttribute("color").getValue();
        Font f = new Font(page_number, id, size, family, color);
        fonts.add(f);
    }
    return fonts;
}

From source file:pdf2xml.TopComparator.java

License:Open Source License

public static Text_Element getTextElement(Element text, List<Font> fonts) {
    String value = text.getValue().trim();

    int top = Integer.parseInt(text.getAttribute("top").getValue());
    int left = Integer.parseInt(text.getAttribute("left").getValue());
    int width = Integer.parseInt(text.getAttribute("width").getValue());
    int height = Integer.parseInt(text.getAttribute("height").getValue());
    int font = Integer.parseInt(text.getAttribute("font").getValue());

    Type typ = Type.NUMBER;
    try {//from   w w  w  . ja  v  a 2s . c  om
        Integer.parseInt(value);
        Float.parseFloat(value);
    } catch (NumberFormatException nfe) {
        typ = Type.TEXT;
    }

    List<Element> bold_elements = text.getChildren("b");
    List<Element> italic_elements = text.getChildren("i");

    Style style;
    if (bold_elements.size() > 0) {
        if (italic_elements.size() > 0) {
            style = Style.BOLD_ITALIC;
        } else {
            style = Style.BOLD;
        }
    } else if (italic_elements.size() > 0) {
        style = Style.ITALIC;
    } else {
        style = Style.NORMAL;
    }

    // This is a hack, but we need access to the font specs to know the size
    int font_size = fonts.get(font).size;

    return new Text_Element(value, top, left, width, height, font, font_size, style, typ);

}

From source file:qtiscoringengine.Area.java

License:Open Source License

static Area fromXML(Element node, XmlNamespaceManager nsmgr) throws InvalidCastException {
    String coords = node.getAttribute("coords").getValue();
    String shapex = node.getAttribute("shape").getValue();
    try {/*from  ww w  .j a v a  2  s. c om*/
        return Area.Create(shapex, coords);
    } catch (Exception e) {
        throw new InvalidCastException("Error in element " + node.getName() + ": " + e.getMessage());
    }
}

From source file:qtiscoringengine.AreaMapEntry.java

License:Open Source License

public static AreaMapEntry fromXML(Element node, XmlNamespaceManager nsmgr, ValidationLog log)
        throws QTIScoringException {
    String val = node.getAttribute("mappedValue").getValue();

    DataElement de = DataElement.create(val, BaseType.Float);
    if (de.getIsError())
        throw new QTIScoringException(de.getErrorMessage());
    _DEFloat floatVal = (_DEFloat) de;/* w ww.  j a  v  a 2s.  co m*/

    try {
        // this will throw an exception if anything is wrong
        Area area = Area.fromXML(node, nsmgr); //
        return new AreaMapEntry(area, floatVal, node);
    } catch (Exception e) {
        log.addMessage(node, e.getMessage());
        return null;
    }
}

From source file:qtiscoringengine.ISECustomOperator.java

License:Open Source License

public boolean supportsOperator(Element customOperatorNode) {
    Element coElement = customOperatorNode;
    Attribute classAttribute = coElement.getAttribute("class");
    if (classAttribute == null)
        return false;
    return _externalScorer.GetScorerInfo(classAttribute.getValue()) != null;
}

From source file:qtiscoringengine.VariableMapEntry.java

License:Open Source License

static VariableMapEntry FromXML(Element node, BaseType bt, ValidationLog log) {
    String key = node.getAttribute("mapKey").getValue();
    String val = node.getAttribute("mappedValue").getValue();

    DataElement de = DataElement.create(key, bt);
    DataElement def = DataElement.create(val, BaseType.Float);

    if (de == null) {
        log.addMessage(node, "mapKey attribute not specified for node " + node.getName());
        return null;
    }/*from  ww w  .  jav  a  2  s. c om*/
    if (de.getIsError()) {
        log.addMessage(node, "Error getting mapKey for node " + node.getName() + ": " + de.getErrorMessage());
        return null;
    }
    if (def == null) {
        log.addMessage(node, "mappedValue attribute not specified for node " + node.getName());
        return null;
    }
    if (def.getIsError()) {
        log.addMessage(node,
                "Error getting mappedValue for node " + node.getName() + ": " + def.getErrorMessage());
        return null;
    }

    return new VariableMapEntry(de, (DEFloat) def, node);
}

From source file:recparser.idmef.IdmefParser.java

License:Open Source License

public List<IntrusionAlert> parser(String input) {

    long initParser = System.currentTimeMillis();
    System.out.println("*** INIT PARSER IDMEF at " + initParser + " *** ");

    //Primero verificar si vienen una o mas alertas
    String[] mensajes = splitIDMEF(input);
    for (int i = 0; i < mensajes.length; i++) {
        IntrusionAlert intrusionAlert = new IntrusionAlert();
        try {//from w  w w  . j av a  2s .c  o  m
            DateToXsdDatetimeFormatter xdf = new DateToXsdDatetimeFormatter();
            String currentDate = xdf.format(new Date());
            String intrusionCount = currentDate.replace(":", "").replace("-", "");
            intrusionAlert.setIntCount(intrusionCount);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        String messageID = null;
        SAXBuilder builder = new SAXBuilder();
        StringReader inputReader = new StringReader(mensajes[i]);
        try {

            //XML parser SAX
            Document doc = builder.build(inputReader);
            Element root = doc.getRootElement();
            Namespace ns = root.getNamespace();
            root = root.getChild("Alert", ns);

            //Es una alerta
            if (root != null) {

                //Cogemos los valores relevantes
                Content content;
                // 0. ID de la alerta
                Attribute attribute = root.getAttribute("messageid");
                if (attribute != null) {
                    intrusionAlert.setIntID(attribute.getValue());
                    intrusionAlert.setMessageID(attribute.getValue());
                    messageID = attribute.getValue();
                }
                //1. Datos del analizador
                Element e = root.getChild("Analyzer", ns);
                if (e != null) {
                    attribute = e.getAttribute("analyzerid");
                    if (attribute != null) {
                        intrusionAlert.setAnalyzerID(attribute.getValue());
                        messageID = attribute.getValue() + messageID;
                        intrusionAlert.setIntID(messageID); //MOdificamos el valor de IntID = analyzerID+messageID
                    }
                }

                //2. Tiempo de creacin
                e = root.getChild("CreateTime", ns);
                if (e != null) {
                    for (int j = 0; j < e.getContentSize(); j++) {
                        content = e.getContent(j);
                        intrusionAlert.setIntAlertCreateTime(content.getValue());
                    }
                }

                //3. Severidad 
                e = root.getChild("Assessment", ns);
                if (e != null) {
                    attribute = e.getAttribute("completion");
                    if (attribute != null)
                        intrusionAlert.setIntCompletion(attribute.getValue());
                    e = e.getChild("Impact", ns);
                    if (e != null) {
                        attribute = e.getAttribute("severity");
                        if (attribute != null) {
                            String attributeValue = attribute.getValue();
                            for (int j = 0; j < 4; j++) {
                                if (attributeValue.equals(severidad[j])) {
                                    intrusionAlert.setIntSeverity(j + 1);
                                }
                            }
                        }
                    }
                }

                Namespace reclamo = Namespace.getNamespace("http://reclamo.inf.um.es/idmef");
                Element additional = null;
                additional = root.getChild("AdditionalData", ns);
                if (additional != null) {
                    //4. Porcentaje de ataque
                    additional = additional.getChild("xml", ns);
                    if (additional != null) {

                        additional = additional.getChild("IntrusionTrust", reclamo);

                        if (additional != null) {
                            e = additional.getChild("AttackPercentage", reclamo);
                            if (e != null) {
                                for (int j = 0; j < e.getContentSize(); j++)
                                    content = e.getContent(j);
                            }
                            //5. Certeza
                            additional = additional.getChild("AssessmentTrust", reclamo);
                            if (additional != null) {
                                additional = additional.getChild("Assessment", reclamo);
                                if (additional != null) {
                                    e = additional.getChild("Trust", reclamo);
                                    if (e != null) {
                                        for (int j = 0; j < e.getContentSize(); j++) {
                                            content = e.getContent(j);
                                            intrusionAlert.setAnalyzerConfidence(
                                                    Double.parseDouble(content.getValue()));
                                        }
                                    }
                                }
                            }
                        }
                    }

                }

                //6. Tiempo de deteccin
                e = root.getChild("DetectTime", ns);
                if (e != null) {
                    content = e.getContent(0);
                    intrusionAlert.setIntDetectionTime(content.getValue());

                } else if (additional != null) {//No aparece esta rama, hay que cogerlo del additionaldata
                    e = additional.getChild("Timestamp", reclamo);
                    if (e != null) {
                        for (int j = 0; j < e.getContentSize(); j++) {
                            content = e.getContent(j);
                            intrusionAlert.setIntDetectionTime(content.getValue());
                        }
                    }
                }

                //7. Recursividad en la rama Target
                List targets = root.getChildren("Target", ns);
                Iterator it = targets.iterator();
                while (it.hasNext()) {
                    IntrusionTarget intrusionTarget = new IntrusionTarget();
                    listChildren((Element) it.next(), intrusionTarget, null);
                    intrusionAlert.setIntrusionTarget(intrusionTarget);

                }

                //8. Recursividad en la rama Source
                List sources = root.getChildren("Source", ns);
                it = sources.iterator();
                while (it.hasNext()) {
                    IntrusionSource intrusionSource = new IntrusionSource();
                    listChildren((Element) it.next(), intrusionSource, null);
                    intrusionAlert.setIntrusionSource(intrusionSource);
                }

                //9. Classification

                e = root.getChild("Classification", ns);
                String tipo_alert = null;
                if (e != null) {
                    attribute = e.getAttribute("text");
                    if (attribute != null) {
                        tipo_alert = attribute.getValue();
                        intrusionAlert.setIntName(tipo_alert);
                        String path = "/"
                                + getClass().getProtectionDomain().getCodeSource().getLocation().toString();
                        String path2 = path.substring(6, path.length() - 13);
                        String classtype = this.obtainParameter(
                                path2 + props.getIdmefIntrusionClassificationFile(), tipo_alert);

                        intrusionAlert.setIntType(classtype);
                    }
                }

                //10. En caso de no tener el tipo de alerta antes:
                if (tipo_alert != null && tipo_alert.equals("unknown")) {
                    e = root.getChild("CorrelationAlert", ns);
                    if (e != null) {
                        e = e.getChild("name", ns);
                        if (e != null) {
                            for (int j = 0; j < e.getContentSize(); j++) {
                                content = e.getContent(j);
                                intrusionAlert.setIntType(content.getValue());
                            }
                        }

                    }
                }

                if (intrusionAlert.getIntSeverity() < 0) {
                    intrusionAlert.setIntSeverity(0); //Asignamos 0 al valor de la severidad en caso de que sea -1
                }
                //Insertamos la alerta leida
                intrusionAlerts.add(intrusionAlert);
            }
        }
        // indicates a well-formedness error
        catch (JDOMException e) {
            System.out.println(" is not well-formed.");
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println(e + "2");
        }

        long endParser = System.currentTimeMillis();
        System.out.println("*** END PARSER IDMEF *** Parsing time : " + (endParser - initParser) + " (ms)*** ");
    }

    return intrusionAlerts;

}

From source file:Reference.SearchThroughXML.XML_Query_SAX_1.java

public static void querySAX(String inputFileName) {
    try {//from  w  w w .  j a  va 2 s  . c o m

        //File inputFile = new File(inputFileName);
        File inputFile = new File("data/input_car_xml.xml");

        SAXBuilder saxBuilder = new SAXBuilder();
        Document document = saxBuilder.build(inputFile);

        System.out.println("Root element :" + document.getRootElement().getName());

        Element rootElement = document.getRootElement();

        System.out.println("---------------------------------------------");

        List<Element> supercarList = rootElement.getChildren("supercars");

        System.out.println("size of supercarList is " + supercarList.size());
        System.out.println("");

        for (int i = 0; i < supercarList.size(); i++) {
            Element supercarElement = supercarList.get(i);
            System.out.println("Current Element :" + supercarElement.getName());
            Attribute attribute = supercarElement.getAttribute("company");
            System.out.println("company : " + attribute.getValue());
            List<Element> carNameList = supercarElement.getChildren("carname");
            for (int j = 0; j < carNameList.size(); j++) {
                Element carElement = carNameList.get(j);
                String carName = carElement.getText();
                Attribute typeAttribute = carElement.getAttribute("type");
                String carType = (typeAttribute != null ? typeAttribute.getValue() : "null");
                System.out.println("car name : " + carName + ", " + "car type : " + carType);
            }
            //deck
            Element deckElement = supercarElement.getChild("deck");
            //Attribute nullAttribute = deckElement.getAttribute("null");
            //System.out.println("* " + nullAttribute.toString());
            //String deckString = (nullAttribute != null ? "null" : deckElement.getText());
            System.out.println("deck : " + deckElement.getText());

            Element aliasesElement = supercarElement.getChild("aliases");
            Attribute nullAttribute = aliasesElement.getAttribute("null");
            //String aliasText = (nullAttribute.getValue().equals("true") ? "null" : aliasesElement.getText());
            String aliasText = (nullAttribute == null ? aliasesElement.getText() : "null");
            System.out.println("aliasesElement : " + aliasText);

            System.out.println("");
        }

        List<Element> luxurycarList = rootElement.getChildren("luxurycars");

        System.out.println("size of luxurycarList is " + luxurycarList.size());
        System.out.println("");

        for (int i = 0; i < luxurycarList.size(); i++) {
            Element luxurycarElement = luxurycarList.get(i);
            System.out.println("Current Element :" + luxurycarElement.getName());
            List<Element> carNameList = luxurycarElement.getChildren("carname");
            for (int j = 0; j < carNameList.size(); j++) {
                Element carElement = carNameList.get(j);
                String carName = carElement.getText();
                System.out.println("car name : " + carName);

            }
        }

    } catch (JDOMException e) {
        e.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

From source file:se.miun.itm.input.export.LaTeXFileExporter.java

License:Open Source License

private void appendValueToTable(StringBuilder b, Element value, int rootDistance) {
    setId(b, value, rootDistance);//from   www.ja  va 2 s  .c  o  m
    // always get the value and print the id to it simply, check for children then!
    if (value.getAttribute(Q.VALUE_ATTR) != null) {
        if (value.getName().equals(Q.NVALUE)) {
            b.append('$');
            b.append(value.getAttributeValue(Q.VALUE_ATTR));
            b.append('$');
        } else if (value.getName().equals(Q.SVALUE)) {
            appendStructuralValue(b, value, rootDistance);
        }
    } else {
        List<Element> children = value.getChildren();
        int depth = getDepth(value);
        if (depth <= 2) {
            b.append("$\\left(");
            b.append(LINEBREAK);
            b.append("\\begin{array}{");
            appendCs(b, getAmountElements(children));
            b.append('}');
            b.append(LINEBREAK);
            appendMatrixContent(b, children, depth, rootDistance);
            b.append("\\end{array}");
            b.append(LINEBREAK);
            b.append("\\right)$");
        } else {
            b.append("Too high dimensional to visualize.");
        }
    }
    if (rootDistance == 1) {
        b.append("\\\\");
        b.append(LINEBREAK);
    }
}

From source file:se.miun.itm.input.model.mapping.Wrapper.java

License:Open Source License

private String initSetter(Element mapping, Element wrap, String localId) {
    if (wrap.getAttribute(Q.SET_ATTR) != null)
        return wrap.getAttributeValue(Q.SET_ATTR);
    return Q.SET_ATTR + localId;
}