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:org.esa.nest.dataio.binary.BinaryDBReader.java

License:Open Source License

private void DecodeElementDebug(final BinaryFileReader reader, final Map metaMap, final Element child,
        final String suffix) {

    String name = "";
    try {/*from   w w w. j  av  a  2s . co m*/
        final Attribute nameAttrib = child.getAttribute("name");
        final Attribute typeAttrib = child.getAttribute("type");
        final Attribute numAttrib = child.getAttribute("num");
        if (nameAttrib != null && typeAttrib != null && numAttrib != null) {

            name = nameAttrib.getValue();
            if (suffix != null)
                name += suffix;
            final int type = Integer.parseInt(typeAttrib.getValue());
            final int num = Integer.parseInt(numAttrib.getValue());

            System.out.print(" " + reader.getCurrentPos() + ' ' + (reader.getCurrentPos() - startPos + 1) + ' '
                    + name + ' ' + type + ' ' + num);

            switch (type) {
            case Skip: {
                reader.skipBytes(num); // blank
                break;
            }
            case An: {

                final String tmp = reader.readAn(num);
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case In: {

                final int tmp = (int) reader.readIn(num);
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case B1: {

                final int tmp = reader.readB1();
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case B2: {

                final int tmp = reader.readB2();
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case B4: {

                final int tmp = reader.readB4();
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case B8: {

                final long tmp = reader.readB8();
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case Fn: {

                double tmp = reader.readFn(num);
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case En: {

                double tmp = reader.readEn(num);
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case Debug: {

                System.out.print(" = ");
                for (int i = 0; i < num; ++i) {
                    final String tmp = reader.readAn(1);
                    if (!tmp.isEmpty() && !tmp.equals(" "))
                        System.out.print(tmp);
                }
                System.out.println();
                break;
            }
            default: {
                throw new IllegalBinaryFormatException("Unknown type " + type, reader.getCurrentPos());
            }
            }
            System.out.println();
        }

    } catch (Exception e) {
        if (e.getCause() != null)
            System.out.println(' ' + e.toString() + ':' + e.getCause().toString() + " for " + name);
        else
            System.out.println(' ' + e.toString() + ':' + " for " + name);
        //throw new IllegalBinaryFormatException(e.toString(), reader.getCurrentPos());
    }
}

From source file:org.esa.s1tbx.dat.toolviews.Projects.ProductSet.java

License:Open Source License

boolean Load(final File file) {

    if (!file.exists())
        return false;
    Document doc;//w  ww  .  j a va 2  s.co  m
    try {
        doc = XMLSupport.LoadXML(file.getAbsolutePath());
    } catch (IOException e) {
        Dialogs.showError("Unable to load " + file.toString() + ": " + e.getMessage());
        return false;
    }

    fileList = new ArrayList<File>(10);
    Element root = doc.getRootElement();

    final List children = root.getContent();
    for (Object aChild : children) {
        if (aChild instanceof Element) {
            final Element child = (Element) aChild;
            if (child.getName().equals("product")) {
                final Attribute attrib = child.getAttribute("path");
                fileList.add(new File(attrib.getValue()));
            }
        }
    }
    return true;
}

From source file:org.esa.s1tbx.dat.toolviews.Projects.Project.java

License:Open Source License

public void LoadProject(final File file) {

    initProject(file);//from w ww  .j  a  v  a 2s .  c o m

    Document doc;
    try {
        doc = XMLSupport.LoadXML(file.getAbsolutePath());
    } catch (IOException e) {
        Dialogs.showError("Unable to load " + file.toString() + ": " + e.getMessage());
        return;
    }

    final Vector<ProjectSubFolder> folderList = new Vector<ProjectSubFolder>(30);
    final Vector<ProjectFile> prodList = new Vector<ProjectFile>(50);

    final Element root = doc.getRootElement();

    final List children = root.getContent();
    for (Object aChild : children) {
        if (aChild instanceof Element) {
            final Element child = (Element) aChild;
            if (child.getName().equals("subFolder")) {
                final Attribute attrib = child.getAttribute("name");
                final ProjectSubFolder subFolder = projectSubFolders.addSubFolder(attrib.getValue());
                subFolder.fromXML(child, folderList, prodList);
            }
        }
    }

    loadProducts(folderList, prodList);

    notifyEvent(false);
    showProjectsView();
}

From source file:org.esa.s1tbx.dataio.binary.BinaryDBReader.java

License:Open Source License

private static void DecodeElement(final BinaryFileReader reader, final Map metaMap, final Element child,
        final String suffix) {

    String name = "";
    try {/* www  . jav a 2  s. co  m*/
        final Attribute nameAttrib = child.getAttribute("name");
        final Attribute typeAttrib = child.getAttribute("type");
        final Attribute numAttrib = child.getAttribute("num");
        if (nameAttrib != null && typeAttrib != null && numAttrib != null) {

            name = nameAttrib.getValue();
            if (suffix != null)
                name += suffix;
            final int type = Integer.parseInt(typeAttrib.getValue());
            final int num = Integer.parseInt(numAttrib.getValue());

            switch (type) {
            case Skip: {
                reader.skipBytes(num); // blank
                break;
            }
            case An: {
                metaMap.put(name, reader.readAn(num));
                break;
            }
            case In: {
                metaMap.put(name, (int) reader.readIn(num));
                break;
            }
            case B1: {
                metaMap.put(name, reader.readB1());
                break;
            }
            case B2: {
                metaMap.put(name, reader.readB2());
                break;
            }
            case B4: {
                metaMap.put(name, reader.readB4());
                break;
            }
            case B8: {
                metaMap.put(name, reader.readB8());
                break;
            }
            case Fn: {
                metaMap.put(name, reader.readFn(num));
                break;
            }
            case En: {
                metaMap.put(name, reader.readEn(num));
                break;
            }
            case Debug: {
                System.out.print(" = ");
                for (int i = 0; i < num; ++i) {
                    final String tmp = reader.readAn(1);
                    if (!tmp.isEmpty() && !tmp.equals(" "))
                        System.out.print(tmp);
                }
                System.out.println();
                break;
            }
            default: {
                throw new IllegalBinaryFormatException("Unknown type " + type, reader.getCurrentPos());
            }
            }
        }

    } catch (Exception e) {
        if (e.getCause() != null)
            SystemUtils.LOG.severe(' ' + e.toString() + ':' + e.getCause().toString() + " for " + name);
        else
            SystemUtils.LOG.severe(' ' + e.toString() + ':' + " for " + name);
    }
}

From source file:org.esa.s1tbx.dataio.binary.BinaryDBReader.java

License:Open Source License

private void DecodeElementDebug(final BinaryFileReader reader, final Map metaMap, final Element child,
        final String suffix) {

    String name = "";
    try {//from  w ww . j  a  v  a2  s.  com
        final Attribute nameAttrib = child.getAttribute("name");
        final Attribute typeAttrib = child.getAttribute("type");
        final Attribute numAttrib = child.getAttribute("num");
        if (nameAttrib != null && typeAttrib != null && numAttrib != null) {

            name = nameAttrib.getValue();
            if (suffix != null)
                name += suffix;
            final int type = Integer.parseInt(typeAttrib.getValue());
            final int num = Integer.parseInt(numAttrib.getValue());

            System.out.print(" " + reader.getCurrentPos() + ' ' + (reader.getCurrentPos() - startPos + 1) + ' '
                    + name + ' ' + type + ' ' + num);

            switch (type) {
            case Skip: {
                reader.skipBytes(num); // blank
                break;
            }
            case An: {

                final String tmp = reader.readAn(num);
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case In: {

                final int tmp = (int) reader.readIn(num);
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case B1: {

                final int tmp = reader.readB1();
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case B2: {

                final int tmp = reader.readB2();
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case B4: {

                final int tmp = reader.readB4();
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case B8: {

                final long tmp = reader.readB8();
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case Fn: {

                double tmp = reader.readFn(num);
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case En: {

                double tmp = reader.readEn(num);
                System.out.print(" = " + tmp);
                metaMap.put(name, tmp);
                break;
            }
            case Debug: {

                System.out.print(" = ");
                for (int i = 0; i < num; ++i) {
                    final String tmp = reader.readAn(1);
                    if (!tmp.isEmpty() && !tmp.equals(" "))
                        System.out.print(tmp);
                }
                System.out.println();
                break;
            }
            default: {
                throw new IllegalBinaryFormatException("Unknown type " + type, reader.getCurrentPos());
            }
            }
            System.out.println();
        }

    } catch (Exception e) {
        if (e.getCause() != null)
            SystemUtils.LOG.severe(' ' + e.toString() + ':' + e.getCause().toString() + " for " + name);
        else
            SystemUtils.LOG.severe(' ' + e.toString() + ':' + " for " + name);
        //throw new IllegalBinaryFormatException(e.toString(), reader.getCurrentPos());
    }
}

From source file:org.esa.s2tbx.dataio.s2.gml.GmlFilter.java

License:Open Source License

public Pair<String, List<EopPolygon>> parse(InputStream stream) {
    SAXBuilder builder = new SAXBuilder();
    Document jdomDoc = null;//from w w w .  ja v  a  2 s.c  o  m

    try {
        jdomDoc = builder.build(stream);

        //get the root element
        Element web_app = jdomDoc.getRootElement();
        String maskEpsg = "";

        Namespace gml = Namespace.getNamespace("http://www.opengis.net/gml/3.2");
        Namespace eop = Namespace.getNamespace("http://www.opengis.net/eop/2.0");

        List<Element> targeted = web_app.getChildren("boundedBy", gml);
        if (!targeted.isEmpty()) {
            Element aEnvelope = targeted.get(0).getChild("Envelope", gml);
            if (aEnvelope != null) {
                maskEpsg = aEnvelope.getAttribute("srsName").getValue();
            }
        }

        List<EopPolygon> recoveredGeometries = new ArrayList<>();

        IteratorIterable<Content> contents = web_app.getDescendants();
        while (contents.hasNext()) {
            Content web_app_content = contents.next();
            if (!web_app_content.getCType().equals(CType.Text)
                    && !web_app_content.getCType().equals(CType.Comment)) {
                boolean withGml = (web_app_content.getNamespacesInScope().get(0).getPrefix().contains("gml"));
                if (withGml) {
                    boolean parentNotGml = !(web_app_content.getParentElement().getNamespace().getPrefix()
                            .contains("gml"));
                    if (parentNotGml) {
                        Element capturedElement = (Element) web_app_content;
                        Attribute attr = null;
                        String polygonId = "";
                        String typeId = "";
                        if (capturedElement.getName().contains("Polygon")) {
                            attr = capturedElement.getAttribute("id", gml);
                            if (attr != null) {
                                polygonId = attr.getValue();
                                if (polygonId.indexOf('.') != -1) {
                                    typeId = polygonId.substring(0, polygonId.indexOf('.'));
                                }
                            }
                        }

                        Document newDoc = new Document(capturedElement.clone().detach());

                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        xmlOutput.output(newDoc, baos);
                        String replacedContent = baos.toString().replace("/www.opengis.net/gml/3.2",
                                "/www.opengis.net/gml");

                        InputStream ois = new ByteArrayInputStream(replacedContent.getBytes());

                        List<Polygon> pols = streamParseGML3(ois);
                        for (Polygon pol : pols) {
                            recoveredGeometries.add(new EopPolygon(polygonId, typeId, pol));
                        }
                    }
                }

            }
        }

        return new Pair<String, List<EopPolygon>>(maskEpsg, recoveredGeometries);
    } catch (JDOMException e) {
        // {@report "parse xml problem !"}
    } catch (IOException e) {
        // {@report "IO problem !"}
    }

    return new Pair<String, List<EopPolygon>>("", new ArrayList<>());
}

From source file:org.esa.snap.core.dataop.downloadable.FtpDownloader.java

License:Open Source License

public static Map<String, Long> readRemoteFileList(final FtpDownloader ftp, final String server,
        final String remotePath) {

    boolean useCachedListing = true;
    final File listingFile = new File(SystemUtils.getCacheDir(), server + ".listing.xml");
    if (!listingFile.exists())
        useCachedListing = false;// www . j ava  2 s  . c  o  m

    final Map<String, Long> fileSizeMap = new HashMap<>(900);

    if (useCachedListing) {
        Document doc = null;
        try {
            doc = XMLSupport.LoadXML(listingFile.getAbsolutePath());
        } catch (IOException e) {
            useCachedListing = false;
        }

        if (useCachedListing) {
            final Element root = doc.getRootElement();
            boolean listingFound = false;

            final List<Content> children1 = root.getContent();
            for (Object c1 : children1) {
                if (!(c1 instanceof Element))
                    continue;
                final Element remotePathElem = (Element) c1;
                final Attribute pathAttrib = remotePathElem.getAttribute("path");
                if (pathAttrib != null && pathAttrib.getValue().equalsIgnoreCase(remotePath)) {
                    listingFound = true;
                    final List<Content> children2 = remotePathElem.getContent();
                    for (Object c2 : children2) {
                        if (!(c2 instanceof Element))
                            continue;
                        final Element fileElem = (Element) c2;
                        final Attribute attrib = fileElem.getAttribute("size");
                        if (attrib != null) {
                            try {
                                fileSizeMap.put(getFileName(fileElem.getName()), attrib.getLongValue());
                            } catch (Exception e) {
                                //
                            }
                        }
                    }
                }
            }
            if (!listingFound)
                useCachedListing = false;
        }
    }
    if (!useCachedListing) {
        try {
            final FTPFile[] remoteFileList = ftp.getRemoteFileList(remotePath);
            if (remoteFileList != null) {
                writeRemoteFileList(remoteFileList, server, remotePath, listingFile);

                for (FTPFile ftpFile : remoteFileList) {
                    fileSizeMap.put(ftpFile.getName(), ftpFile.getSize());
                }
            }
        } catch (Exception e) {
            SystemUtils.LOG.warning("Unable to get remote file list " + e.getMessage());
        }
    }

    return fileSizeMap;
}

From source file:org.esa.snap.core.dataop.downloadable.XMLSupport.java

License:Open Source License

private static void addAttribute(final MetadataElement root, final Element domElem) {

    final Attribute nameAttrib = domElem.getAttribute("name");
    final Attribute valueAttrib = domElem.getAttribute("value");
    final Attribute typeAttrib = domElem.getAttribute("type");
    final Attribute unitAttrib = domElem.getAttribute("unit");
    final Attribute descAttrib = domElem.getAttribute("desc");

    if (nameAttrib == null || valueAttrib == null)
        return;//from  ww  w. ja  va 2  s . c  o m

    final MetadataAttribute attribute = new MetadataAttribute(nameAttrib.getName(), ProductData.TYPE_ASCII, 1);
    attribute.getData().setElems(valueAttrib.getValue());

    if (unitAttrib != null)
        attribute.setUnit(unitAttrib.getValue());
    if (descAttrib != null)
        attribute.setDescription(descAttrib.getValue());

    root.addAttribute(attribute);
}

From source file:org.esa.snap.core.dataop.downloadable.XMLSupport.java

License:Open Source License

public static String getAttrib(final Element elem, final String tag) {
    final Attribute attrib = elem.getAttribute(tag);
    if (attrib != null)
        return attrib.getValue();
    return "";
}

From source file:org.esa.snap.datamodel.metadata.AbstractMetadataIO.java

License:Open Source License

private static void loadAttribute(final Element domElem, final MetadataElement rootElem) {

    final Attribute nameAttrib = domElem.getAttribute(NAME);
    if (nameAttrib == null)
        return;//ww  w . j a v a 2s  .  com

    final String name = nameAttrib.getValue();
    if (name == null)
        return;

    final Attribute valueAttrib = domElem.getAttribute(VALUE);
    if (valueAttrib == null)
        return;

    final Attribute typeAttrib = domElem.getAttribute(TYPE);
    Integer typeFromFile = null;
    if (typeAttrib != null) {
        typeFromFile = Integer.parseInt(typeAttrib.getValue());
    }

    MetadataAttribute metaAttrib = rootElem.getAttribute(name);
    if (metaAttrib == null) {
        if (typeFromFile != null) {
            metaAttrib = new MetadataAttribute(name, typeFromFile);
            rootElem.addAttribute(metaAttrib);
        } else {
            return;
        }
    }

    final int type = metaAttrib.getDataType();
    if (type == ProductData.TYPE_ASCII) {
        String valStr = valueAttrib.getValue();
        if (valStr == null || valStr.isEmpty())
            valStr = " ";
        metaAttrib.getData().setElems(valStr);
    } else if (type == ProductData.TYPE_UTC || typeFromFile == ProductData.TYPE_UTC) {
        metaAttrib.getData().setElems(AbstractMetadata.parseUTC(valueAttrib.getValue()).getArray());
    } else if (type == ProductData.TYPE_FLOAT64 || type == ProductData.TYPE_FLOAT32) {
        metaAttrib.getData().setElemDouble(Double.parseDouble(valueAttrib.getValue()));
    } else if (type == ProductData.TYPE_INT8 && typeFromFile != null
            && typeFromFile == ProductData.TYPE_ASCII) {
        metaAttrib.getData().setElems(valueAttrib.getValue());
    } else {
        metaAttrib.getData().setElemInt(Integer.parseInt(valueAttrib.getValue()));
    }

    final Attribute unitAttrib = domElem.getAttribute("unit");
    final Attribute descAttrib = domElem.getAttribute("desc");

    if (descAttrib != null)
        metaAttrib.setDescription(descAttrib.getValue());
    if (unitAttrib != null)
        metaAttrib.setUnit(unitAttrib.getValue());
}