Java Code Examples for org.jdom2.output.Format#getPrettyFormat()

The following examples show how to use org.jdom2.output.Format#getPrettyFormat() . 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
Source File: MCRMetaISO8601DateTest.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void createXML() {
    MCRMetaISO8601Date ts = new MCRMetaISO8601Date("servdate", "createdate", 0);
    String timeString = "1997-07-16T19:20:30.452300+01:00";
    ts.setDate(timeString);
    assertNotNull("Date is null", ts.getDate());
    Element export = ts.createXML();
    if (LOGGER.isDebugEnabled()) {
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        StringWriter sw = new StringWriter();
        try {
            xout.output(export, sw);
            LOGGER.debug(sw.toString());
        } catch (IOException e) {
            LOGGER.warn("Failure printing xml result", e);
        }
    }
}
 
Example 2
Source File: MCRMetaDefaultListXMLWriter.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void writeTo(List<? extends MCRMetaDefault> mcrMetaDefaults, Class<?> type, Type genericType,
    Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
    OutputStream entityStream) throws IOException, WebApplicationException {
    Optional<String> wrapper = getWrapper(annotations);
    if (!wrapper.isPresent()) {
        throw new InternalServerErrorException("Could not get XML wrapping element from annotations.");
    }
    httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_TYPE);
    Element root = new Element(wrapper.get());
    root.addContent(mcrMetaDefaults.stream()
        .map(MCRMetaDefault::createXML)
        .collect(Collectors.toList()));
    XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
    xout.output(root, entityStream);
}
 
Example 3
Source File: MCROAIDataProvider.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void doGetPost(MCRServletJob job) throws Exception {
    HttpServletRequest request = job.getRequest();
    // get base url
    if (this.myBaseURL == null) {
        this.myBaseURL = MCRFrontendUtil.getBaseURL() + request.getServletPath().substring(1);
    }
    logRequest(request);
    // create new oai request
    OAIRequest oaiRequest = new OAIRequest(fixParameterMap(request.getParameterMap()));
    // create new oai provider
    OAIProvider oaiProvider = oaiAdapterServiceLoader
        .findFirst()
        .orElseThrow(() -> new ServletException("No implementation of " + OAIProvider.class + " found."));
    oaiProvider.setAdapter(getOAIAdapter());
    // handle request
    OAIResponse oaiResponse = oaiProvider.handleRequest(oaiRequest);
    // build response
    Element xmlRespone = oaiResponse.toXML();
    // fire
    job.getResponse().setContentType("text/xml; charset=UTF-8");
    XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat(), OAI_XML_OUTPUT_PROCESSOR);
    xout.output(addXSLStyle(new Document(xmlRespone)), job.getResponse().getOutputStream());
}
 
Example 4
Source File: CodeTableGen.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static public void passTwo() throws IOException {
  org.jdom2.Document tdoc;
  try {
    SAXBuilder builder = new SAXBuilder();
    tdoc = builder.build(trans1);

    org.jdom2.Document ndoc = new org.jdom2.Document();
    Element nroot = new Element("ndoc");
    ndoc.setRootElement(nroot);

    transform2(tdoc.getRootElement(), nroot);

    XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
    Writer pw = new FileWriter(trans2);
    fmt.output(ndoc, pw);
    pw = new PrintWriter(System.out);
    fmt.output(ndoc, pw);

  } catch (JDOMException e) {
    throw new IOException(e.getMessage());
  }
}
 
Example 5
Source File: MCREditorOutValidator.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
/**
 * tries to generate a valid MCRObject as JDOM Document.
 *
 * @return MCRObject
 */
public Document generateValidMyCoReObject() throws JDOMException, SAXParseException, IOException {
    MCRObject obj;
    // load the JDOM object
    XPathFactory.instance()
        .compile("/mycoreobject/*/*/*/@editor.output", Filters.attribute())
        .evaluate(input)
        .forEach(Attribute::detach);
    try {
        byte[] xml = new MCRJDOMContent(input).asByteArray();
        obj = new MCRObject(xml, true);
    } catch (SAXParseException e) {
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        LOGGER.warn("Failure while parsing document:\n{}", xout.outputString(input));
        throw e;
    }
    Date curTime = new Date();
    obj.getService().setDate("modifydate", curTime);

    // return the XML tree
    input = obj.createXML();
    return input;
}
 
Example 6
Source File: MCRDataciteClient.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private static byte[] documentToByteArray(Document document) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
    xout.output(document, outputStream);

    return outputStream.toByteArray();
}
 
Example 7
Source File: GPXFileWriter.java    From BackPackTrackII with GNU General Public License v3.0 5 votes vote down vote up
public static void writeGeonames(List<Geonames.Geoname> names, File target, Context context) throws IOException {
    Document doc = new Document();
    Element gpx = new Element("gpx", NS);
    Namespace xsi = Namespace.getNamespace("xsi", XSI);
    gpx.addNamespaceDeclaration(xsi);
    Namespace bpt2 = Namespace.getNamespace("bpt2", BPT);
    gpx.addNamespaceDeclaration(bpt2);
    gpx.setAttribute("schemaLocation", XSD, xsi);
    gpx.setAttribute(new Attribute("version", "1.1"));
    gpx.setAttribute(new Attribute("creator", CREATOR));

    Collection<Element> wpts = new ArrayList<>();
    for (Geonames.Geoname name : names) {
        Element wpt = new Element("wpt", NS);
        wpt.setAttribute(new Attribute("lat", Double.toString(name.location.getLatitude())));
        wpt.setAttribute(new Attribute("lon", Double.toString(name.location.getLongitude())));
        wpt.addContent(new Element("name", NS).addContent(name.name));
        wpts.add(wpt);
    }
    gpx.addContent(wpts);

    doc.setRootElement(gpx);

    FileWriter fw = null;
    try {
        fw = new FileWriter(target);
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        xout.output(doc, fw);
        fw.flush();
    } finally {
        if (fw != null)
            fw.close();
    }
}
 
Example 8
Source File: Tools.java    From Zettelkasten with GNU General Public License v3.0 5 votes vote down vote up
/**
 * This method returns the XML database as string. just for testing
 * purposes.
 *
 * @param dataObj
 * @return
 */
public static String retrieveXMLFileAsString(Daten dataObj) {
    // create a new XML-outputter with the pretty output format,
    // so the xml-file looks nicer
    XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
    // return XML data base as string
    return out.outputString(dataObj.getZknData());
}
 
Example 9
Source File: TableAnalyzer.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeConfigXML(java.util.Formatter sf) {
  if (configResult != null) {
    PointConfigXML tcx = new PointConfigXML();
    tcx.writeConfigXML(configResult, getName(), sf);
    return;
  }
  XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
  sf.format("%s", fmt.outputString(makeDocument()));
}
 
Example 10
Source File: PointConfigXML.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void writeConfigXML(TableConfig tc, String tableConfigurerClass, java.util.Formatter sf) {
  this.tc = tc;
  this.tableConfigurerClass = tableConfigurerClass;

  XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
  sf.format("%s", fmt.outputString(makeDocument()));
}
 
Example 11
Source File: StationObsDatasetInfo.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Write stationObsDataset XML document
 */
public String writeStationObsDatasetXML() {
  XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
  return fmt.outputString(makeStationObsDatasetDocument());
}
 
Example 12
Source File: WireFeedOutput.java    From rome with Apache License 2.0 4 votes vote down vote up
/**
 * Writes to an Writer the XML representation for the given WireFeed.
 * <p>
 * If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. It is
 * the responsibility of the developer to ensure the Writer instance is using the same charset
 * encoding.
 * <p>
 * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
 * <p>
 *
 * @param feed Abstract feed to create XML representation from. The type of the WireFeed must
 *            match the type given to the FeedOuptut constructor.
 * @param writer Writer to write the XML representation for the given WireFeed.
 * @param prettyPrint pretty-print XML (true) oder collapsed
 * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed
 *             don't match.
 * @throws IOException thrown if there was some problem writing to the Writer.
 * @throws FeedException thrown if the XML representation for the feed could not be created.
 *
 */
public void output(final WireFeed feed, final Writer writer, final boolean prettyPrint) throws IllegalArgumentException, IOException, FeedException {
    final Document doc = outputJDom(feed);
    final String encoding = feed.getEncoding();
    Format format;
    if (prettyPrint) {
        format = Format.getPrettyFormat();
    } else {
        format = Format.getCompactFormat();
    }
    if (encoding != null) {
        format.setEncoding(encoding);
    }
    final XMLOutputter outputter = new XMLOutputter(format);
    outputter.output(doc, writer);
}
 
Example 13
Source File: FeatureDatasetCapabilitiesWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void getCapabilities(OutputStream os) throws IOException {
  XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
  fmt.output(getCapabilitiesDocument(), os);
}
 
Example 14
Source File: FeatureDatasetCapabilitiesWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void getCapabilities(OutputStream os) throws IOException {
  XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
  fmt.output(getCapabilitiesDocument(), os);
}
 
Example 15
Source File: KMLFileWriter.java    From BackPackTrackII with GNU General Public License v3.0 4 votes vote down vote up
public static void writeKMLFile(File target, String trackName, boolean extensions, Cursor cTrackPoints, Cursor cWayPoints, Context context)
        throws IOException {

    // https://developers.google.com/kml/documentation/kmlreference
    Document doc = new Document();

    Element kml = new Element("kml", NS);
    Namespace gx = Namespace.getNamespace("gx", "http://www.google.com/kml/ext/2.2");
    kml.addNamespaceDeclaration(gx);
    Namespace atom = Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom");
    kml.addNamespaceDeclaration(atom);

    Element document = new Element("Document", NS);

    Element name = new Element("name", NS);
    name.addContent(trackName);
    document.addContent(name);

    Element author = new Element("author", atom);
    Element authorName = new Element("name", atom);
    authorName.addContent("BackPackTrackII");
    author.addContent(authorName);
    document.addContent(author);

    Element style = new Element("Style", NS);
    style.setAttribute(new Attribute("id", "style"));
    Element linestyle = new Element("LineStyle", NS);
    Element color = new Element("color", NS);
    color.addContent("ffff0000");
    linestyle.addContent(color);
    Element width = new Element("width", NS);
    width.addContent("5");
    linestyle.addContent(width);
    style.addContent(linestyle);
    document.addContent(style);

    Collection<Element> placemarks = new ArrayList<>();
    placemarks.add(getTrackpoints(trackName, extensions, cTrackPoints, gx, context));
    placemarks.addAll(getWayPoints(extensions, cWayPoints, gx, context));
    document.addContent(placemarks);
    kml.addContent(document);
    doc.setRootElement(kml);

    FileWriter fw = null;
    try {
        fw = new FileWriter(target);
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        xout.output(doc, fw);
        fw.flush();
    } finally {
        if (fw != null)
            fw.close();
    }

    // https://developers.google.com/kml/schema/kml22gx.xsd
    // xmllint --noout --schema kml22gx.xsd BackPackTrack.kml
}
 
Example 16
Source File: NcMLGWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Write a NetcdfDataset as an NcML-G document to the specified stream.
 *
 * @param ncd write this dataset; should have opened with "add coordinates".
 * @param os write to this OutputStream
 * @param showCoords show 1D coordinate values
 * @param uri use this url, if null use getLocation()
 * @throws IOException on io error
 */
public void writeXML(NetcdfDataset ncd, OutputStream os, boolean showCoords, String uri) throws IOException {

  // Output the document, use standard formatter
  // XMLOutputter fmt = new XMLOutputter(" ", true);
  // fmt.setLineSeparator("\n");
  XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
  fmt.output(makeDocument(ncd, showCoords, uri), os);
}
 
Example 17
Source File: NetcdfDatasetInfo.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Write the information as an XML document
 * 
 * @return String containing netcdfDatasetInfo XML
 */
public String writeXML() {
  XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
  return fmt.outputString(makeDocument());
}
 
Example 18
Source File: GridDatasetInfo.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Write the information as an XML document
 *
 * @param doc write XML for this Document
 * @param os write to this output stream
 * @throws java.io.IOException on write error
 */
public void writeXML(Document doc, OutputStream os) throws IOException {
  XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
  fmt.output(doc, os);
}
 
Example 19
Source File: GridDatasetInfo.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Write the information as an XML document
 *
 * @param doc write XML for this Document
 * @return String output
 */
public String writeXML(Document doc) {
  XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
  return fmt.outputString(doc);
}
 
Example 20
Source File: CoverageDatasetCapabilities.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Write the information as an XML document
 *
 * @param doc write XML for this Document
 * @return String output
 */
public String writeXML(Document doc) {
  XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
  return fmt.outputString(doc);
}