Java Code Examples for org.jdom2.Element#getNamespace()

The following examples show how to use org.jdom2.Element#getNamespace() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private String extractDescription(String xml) {
    String description = "No description provided";
    Document doc = JDOMUtil.stringToDocument(xml);
    if (doc != null) {
        Element root = doc.getRootElement();
        if (root != null) {
            Namespace ns = root.getNamespace();
            Element spec = root.getChild("specification", ns);
            if (spec != null) {
                String content = spec.getChildText("documentation", ns);
                if (content == null) {
                    Element metadata = spec.getChild("metaData", ns);
                    if (metadata != null) {
                        content = metadata.getChildText("description", ns);
                    }
                }
                if (content != null) {
                    description = content;
                }
            }
        }
    }
    return description;
}
 
Example 2
protected static RadarConfigEntry.RangeInfo readGeospatialRange(Element spElem, String defUnits) {
  if (spElem == null)
    return null;

  Namespace defNS = spElem.getNamespace();
  double start = readDouble(spElem.getChild("start", defNS));
  double size = readDouble(spElem.getChild("size", defNS));

  String units = spElem.getChildText("units", defNS);
  if (units == null)
    units = defUnits;

  RadarConfigEntry.RangeInfo ri = new RadarConfigEntry.RangeInfo();
  ri.units = units;
  ri.start = start;
  ri.size = size;
  return ri;
}
 
Example 3
private Map<String, String> getAppInfo(Element eField) {
    Namespace ns = eField.getNamespace();
    Element annotation = eField.getChild("annotation", ns);
    if (annotation != null) {
        Map<String, String> infoMap = new HashMap<>();
        Element appInfo = annotation.getChild("appinfo", ns);
        if (appInfo != null) {
            for (Element child : appInfo.getChildren()) {
                String text = child.getText();
                if (!StringUtil.isNullOrEmpty(text)) {
                    infoMap.put(child.getName(), text);
                }
            }
        }
        eField.removeChild("annotation", ns);   // done processing the annotation
        return infoMap;
    }
    return Collections.emptyMap();
}
 
Example 4
private LyricsInfo parseSearchResult(String xml) throws Exception {
    SAXBuilder builder = createSAXBuilder();
    Document document = builder.build(new StringReader(xml));

    Element root = document.getRootElement();
    Namespace ns = root.getNamespace();

    String lyric = StringUtils.trimToNull(root.getChildText("Lyric", ns));
    String song = root.getChildText("LyricSong", ns);
    String artist = root.getChildText("LyricArtist", ns);

    return new LyricsInfo(lyric, artist, song);
}
 
Example 5
Source Project: rome   File: RSS090Parser.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Parses an item element of an RSS document looking for item information.
 * <p/>
 * It reads title and link out of the 'item' element.
 * <p/>
 *
 * @param rssRoot the root element of the RSS document in case it's needed for context.
 * @param eItem the item element to parse.
 * @return the parsed RSSItem bean.
 */
protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) {

    final Item item = new Item();

    final Element title = eItem.getChild("title", getRSSNamespace());
    if (title != null) {
        item.setTitle(title.getText());
    }

    final Element link = eItem.getChild("link", getRSSNamespace());
    if (link != null) {
        item.setLink(link.getText());
        item.setUri(link.getText());
    }

    item.setModules(parseItemModules(eItem, locale));

    final List<Element> foreignMarkup = extractForeignMarkup(eItem, item, getRSSNamespace());
    // content:encoded elements are treated special, without a module, they have to be removed
    // from the foreign markup to avoid duplication in case of read/write. Note that this fix
    // will break if a content module is used
    final Iterator<Element> iterator = foreignMarkup.iterator();
    while (iterator.hasNext()) {
        final Element element = iterator.next();
        final Namespace eNamespace = element.getNamespace();
        final String eName = element.getName();
        if (getContentNamespace().equals(eNamespace) && eName.equals("encoded")) {
            iterator.remove();
        }
    }

    if (!foreignMarkup.isEmpty()) {
        item.setForeignMarkup(foreignMarkup);
    }

    return item;
}
 
Example 6
Source Project: rome   File: RSS20YahooParser.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Indicates if a JDom document is an RSS instance that can be parsed with the parser.
 * <p/>
 * It checks for RDF ("http://www.w3.org/1999/02/22-rdf-syntax-ns#") and RSS
 * ("http://purl.org/rss/1.0/") namespaces being defined in the root element.
 *
 * @param document document to check if it can be parsed with this parser implementation.
 * @return <b>true</b> if the document is RSS1., <b>false</b> otherwise.
 */
@Override
public boolean isMyType(final Document document) {
    boolean ok = false;

    final Element rssRoot = document.getRootElement();
    final Namespace defaultNS = rssRoot.getNamespace();

    ok = defaultNS != null && defaultNS.equals(getRSSNamespace());

    return ok;
}
 
Example 7
protected static RadarConfigEntry.GeoInfo readGeospatialCoverage(Element gcElem) {
  if (gcElem == null)
    return null;
  Namespace defNS = gcElem.getNamespace();

  RadarConfigEntry.GeoInfo gi = new RadarConfigEntry.GeoInfo();
  gi.northSouth = readGeospatialRange(gcElem.getChild("northsouth", defNS), CDM.LAT_UNITS);
  gi.eastWest = readGeospatialRange(gcElem.getChild("eastwest", defNS), CDM.LON_UNITS);
  gi.upDown = readGeospatialRange(gcElem.getChild("updown", defNS), "m");

  return gi;
}
 
Example 8
protected static DateRange readTimeCoverage(Element tElem) {
  if (tElem == null)
    return null;
  Namespace defNS = tElem.getNamespace();
  DateType start = readDate(tElem.getChild("start", defNS));
  DateType end = readDate(tElem.getChild("end", defNS));
  TimeDuration duration = readDuration(tElem.getChild("duration", defNS));
  TimeDuration resolution = readDuration(tElem.getChild("resolution", defNS));

  try {
    return new DateRange(start, end, duration, resolution);
  } catch (java.lang.IllegalArgumentException e) {
    return null;
  }
}
 
Example 9
/**
 * Parses the decomposition from the appropriate XML doclet.
 * @param decompElem the XML doclet containing the decomp configuration.
 * @param specificationParser a reference to parent spec parser.
 * @param version the version of XML structure
 */
YDecompositionParser(Element decompElem, YSpecificationParser specificationParser,
                     YSchemaVersion version) {
    _decompElem = decompElem;
    _yawlNS = decompElem.getNamespace();
    _specificationParser = specificationParser;
    _version = version;
    _postsetIDs = new Postset();
    _removeSetIDs = new HashMap<YTask, List<String>>();
    _decomposesToIDs = new HashMap<YTask, String>();
    _decomposition = createDecomposition(decompElem);
    _postsetIDs.addImplicitConditions();
    linkElements();
}
 
Example 10
/**
 * build a specification object from part of an XML document
 * @param specificationElem the specification part of an XMl document
 * @param version the version of the XML representation (i.e. beta2 or beta3).
 * @throws YSyntaxException
 */
public YSpecificationParser(Element specificationElem, YSchemaVersion version)
        throws YSyntaxException {
    _yawlNS = specificationElem.getNamespace();

    parseSpecification(specificationElem, version);
    linkDecompositions();
}
 
Example 11
Source Project: n2o-framework   File: PersisterJdomUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void setElementInteger(Element element, String elementName, Integer value) {
    if (element == null) return;
    if (value != null) {
        Element subElement = new Element(elementName, element.getNamespace());
        subElement.setText(value.toString());
        element.addContent(subElement);
    }
}
 
Example 12
Source Project: n2o-framework   File: PersisterJdomUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void installPrefix(Element element, Element rootElement) {
    if (element == null) return;
    if (rootElement.getNamespace() == null) return;
    if (element.getNamespacePrefix() == null) return;
    if (element.getNamespace() == null) return;
    if (element.getNamespace().getURI().equals(rootElement.getNamespace().getURI())) return;
    if (element.getNamespace().getURI().isEmpty()) return;
    if (rootElement.getAdditionalNamespaces().contains(element.getNamespace())) return;
    Namespace additional = element.getNamespace();
    rootElement.addNamespaceDeclaration(additional);
}
 
Example 13
Source Project: n2o-framework   File: PersisterJdomUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void replaceNamespace(Element element, Namespace source, Namespace target) {
    if (target == null) return;
    if (element == null) return;
    if ((element.getNamespace() != null) && (element.getNamespace().equals(target))) return;
    if ((element.getNamespace() != null) && (!element.getNamespace().equals(source))) return;
    element.setNamespace(target);
    for (Object child : element.getChildren()) {
        if (child instanceof Element)
            replaceNamespace((Element) child, source, target);
    }
}
 
Example 14
private static void addProperty(Element systemProperties, Map<String, Element> propertiesByName, String name, String value) {
    Namespace namespace = systemProperties.getNamespace();
    if (!propertiesByName.containsKey(name)) {
        systemProperties.addContent(new Text("   "));
        systemProperties.addContent(new Element("property", namespace).setAttribute("name", name).setAttribute("value", value));
        systemProperties.addContent(new Text("\n    "));
    }
}
 
Example 15
Source Project: n2o-framework   File: IOProcessorImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void hasElement(Element element, String name, Supplier<Boolean> getter, Consumer<Boolean> setter) {
    if (r) {
        Element child = element.getChild(name, element.getNamespace());
        setter.accept(child != null);
    } else {
        if (getter.get() == null || !getter.get()) return;
        Element childElement = new Element(name, element.getNamespace());
        element.addContent(childElement);
    }
}
 
Example 16
private LyricsInfo parseSearchResult(String xml) throws Exception {
    SAXBuilder builder = createSAXBuilder();
    Document document = builder.build(new StringReader(xml));

    Element root = document.getRootElement();
    Namespace ns = root.getNamespace();

    String lyric = StringUtils.trimToNull(root.getChildText("Lyric", ns));
    String song = root.getChildText("LyricSong", ns);
    String artist = root.getChildText("LyricArtist", ns);

    return new LyricsInfo(lyric, artist, song);
}
 
Example 17
private YSpecificationID getDescriptors(String specxml) {
    YSpecificationID descriptors = null;

    Document doc = JDOMUtil.stringToDocument(specxml);
    if (doc != null) {
        Element root = doc.getRootElement();
        Namespace ns = root.getNamespace();
        YSchemaVersion schemaVersion = YSchemaVersion.fromString(
                root.getAttributeValue("version"));
        Element specification = root.getChild("specification", ns);

        if (specification != null) {
            String uri = specification.getAttributeValue("uri");
            String version = "0.1";
            String uid = null;
            if (! (schemaVersion == null || schemaVersion.isBetaVersion())) {
                Element metadata = specification.getChild("metaData", ns);
                version = metadata.getChildText("version", ns);
                uid = metadata.getChildText("identifier", ns);
            }
            descriptors = new YSpecificationID(uid, version, uri);
        }
        else msgPanel.error("Malformed specification: 'specification' node not found.");
    }
    else msgPanel.error("Malformed specification: unable to parse.");
    
    return descriptors;
}
 
Example 18
/**
 * parse a netcdf JDOM Element, and add contents to the targetDS NetcdfDataset.
 * <p/>
 * This is a bit tricky, because it handles several cases
 * When targetDS == refds, we are just modifying targetDS.
 * When targetDS != refds, we keep them seperate, and copy from refds to newds.
 * <p/>
 * The user may be defining new elements or modifying old ones. The only way to tell is by seeing
 * if the elements already exist.
 *
 * @param ncmlLocation NcML URL location, or may be just a unique name for caching purposes.
 * @param targetDS add the info to this one, never null
 * @param refds the referenced dataset; may equal newds, never null
 * @param netcdfElem JDOM netcdf element
 * @param cancelTask allow user to cancel the task; may be null
 * @throws IOException on read error
 */
private void readNetcdf(String ncmlLocation, NetcdfDataset targetDS, NetcdfFile refds, Element netcdfElem,
    CancelTask cancelTask) throws IOException {
  this.location = ncmlLocation; // log messages need this

  if (debugOpen)
    System.out
        .println("NcMLReader.readNetcdf ncml= " + ncmlLocation + " referencedDatasetUri= " + refds.getLocation());

  // detect incorrect namespace
  Namespace use = netcdfElem.getNamespace();
  if (!use.equals(ncNSHttp) && !use.equals(ncNSHttps)) {
    String message = String.format("Namespace specified in NcML must be either '%s' or '%s', but was '%s'.",
        ncNSHttp.getURI(), ncNSHttps.getURI(), use.getURI());
    throw new IllegalArgumentException(message);
  }

  if (ncmlLocation != null)
    targetDS.setLocation(ncmlLocation);
  targetDS.setId(netcdfElem.getAttributeValue("id"));
  targetDS.setTitle(netcdfElem.getAttributeValue("title"));

  // aggregation first
  Element aggElem = netcdfElem.getChild("aggregation", ncNS);
  if (aggElem != null) {
    Aggregation agg = readAgg(aggElem, ncmlLocation, targetDS, cancelTask);
    if (agg == null)
      return; // cancel task
    targetDS.setAggregation(agg);
    agg.finish(cancelTask);
  }

  // the root group
  readGroup(targetDS, refds, null, null, netcdfElem);
  String errors = errlog.toString();
  if (!errors.isEmpty())
    throw new IllegalArgumentException("NcML had fatal errors:" + errors);

  // transfer from groups to global containers
  targetDS.finish();

  // enhance means do scale/offset and/or add CoordSystems
  Set<NetcdfDataset.Enhance> mode = NetcdfDataset.parseEnhanceMode(netcdfElem.getAttributeValue("enhance"));
  // if (mode == null)
  // mode = NetcdfDataset.getEnhanceDefault();
  targetDS.enhance(mode);

  // optionally add record structure to netcdf-3
  String addRecords = netcdfElem.getAttributeValue("addRecords");
  if ("true".equalsIgnoreCase(addRecords))
    targetDS.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
}
 
Example 19
static public List<RadarConfigEntry> readXML(String filename) {
  List<RadarConfigEntry> configs = new ArrayList<>();

  SAXBuilder builder = new SAXBuilder();
  File f = new File(filename);
  try {
    Document doc = builder.build(f);
    Element cat = doc.getRootElement();
    Namespace catNS = cat.getNamespace();
    Element topDS = cat.getChild("dataset", cat.getNamespace());
    for (Element dataset : topDS.getChildren("datasetScan", catNS)) {
      RadarConfigEntry conf = new RadarConfigEntry();
      configs.add(conf);

      Element collection = dataset.getChild("radarCollection", catNS);
      conf.dateParseRegex = collection.getAttributeValue("dateRegex");
      conf.dateFmt = collection.getAttributeValue("dateFormat");
      conf.layout = collection.getAttributeValue("layout");
      String crawlItems = collection.getAttributeValue("crawlItems");
      if (crawlItems != null) {
        conf.crawlItems = Integer.parseInt(crawlItems);
      } else {
        conf.crawlItems = 5;
      }

      Element meta = dataset.getChild("metadata", catNS);
      conf.name = dataset.getAttributeValue("name");
      conf.urlPath = dataset.getAttributeValue("path");
      conf.dataPath = getPath(dataset.getAttributeValue("location"));
      conf.dataFormat = meta.getChild("dataFormat", catNS).getValue();
      conf.stationFile = meta.getChild("stationFile", catNS).getAttributeValue("path");
      conf.doc = meta.getChild("documentation", catNS).getValue();

      conf.timeCoverage = readTimeCoverage(meta.getChild("timeCoverage", catNS));

      conf.spatialCoverage = readGeospatialCoverage(meta.getChild("geospatialCoverage", catNS));

      Element variables = meta.getChild("variables", catNS);
      conf.vars = new ArrayList<>();
      for (Element var : variables.getChildren("variable", catNS)) {
        RadarConfigEntry.VarInfo inf = new RadarConfigEntry.VarInfo();
        conf.vars.add(inf);

        inf.name = var.getAttributeValue("name");
        inf.vocabName = var.getAttributeValue("vocabulary_name");
        inf.units = var.getAttributeValue("units");
      }
    }
  } catch (IOException | JDOMException e) {
    e.printStackTrace();
  }

  return configs;
}
 
Example 20
Source Project: n2o-framework   File: PersisterJdomUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static Element setElement(Element parentElement, String elementName) {
    Element element = new Element(elementName, parentElement.getNamespace());
    parentElement.addContent(element);
    return element;
}