Example usage for javax.xml.stream XMLStreamWriter writeEndElement

List of usage examples for javax.xml.stream XMLStreamWriter writeEndElement

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamWriter writeEndElement.

Prototype

public void writeEndElement() throws XMLStreamException;

Source Link

Document

Writes an end tag to the output relying on the internal state of the writer to determine the prefix and local name of the event.

Usage

From source file:org.gluu.saml.AuthRequest.java

public String getStreamedRequest(boolean useBase64) throws XMLStreamException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = factory.createXMLStreamWriter(baos);

    writer.writeStartElement("samlp", "AuthnRequest", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");

    writer.writeAttribute("ID", id);
    writer.writeAttribute("Version", "2.0");
    writer.writeAttribute("IssueInstant", this.issueInstant);
    writer.writeAttribute("ProtocolBinding", "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST");
    writer.writeAttribute("AssertionConsumerServiceURL", this.samlSettings.getAssertionConsumerServiceUrl());

    writer.writeStartElement("saml", "Issuer", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeCharacters(this.samlSettings.getIssuer());
    writer.writeEndElement();

    writer.writeStartElement("samlp", "NameIDPolicy", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");

    writer.writeAttribute("Format", this.samlSettings.getNameIdentifierFormat());
    writer.writeAttribute("AllowCreate", "true");
    writer.writeEndElement();//from   www  .j  a va  2 s.  c  o m

    writer.writeStartElement("samlp", "RequestedAuthnContext", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");

    writer.writeAttribute("Comparison", "exact");

    writer.writeStartElement("saml", "AuthnContextClassRef", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeCharacters("urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport");
    writer.writeEndElement();

    writer.writeEndElement();

    writer.writeEndElement();
    writer.flush();

    if (log.isDebugEnabled()) {
        log.debug("Genereated Saml Request " + new String(baos.toByteArray(), "UTF-8"));
    }

    if (useBase64) {
        byte[] deflated = CompressionHelper.deflate(baos.toByteArray(), true);
        String base64 = Base64.encodeBase64String(deflated);
        String encoded = URLEncoder.encode(base64, "UTF-8");

        return encoded;
    }

    return new String(baos.toByteArray(), "UTF-8");
}

From source file:org.graphipedia.dataextract.ExtractData.java

@Override
public void run() {
    logger.info("Start extracting data...");
    long startTime = System.currentTimeMillis();

    DisambiguationPageExtractor dpExtractor = new DisambiguationPageExtractor(settings, this.language,
            dpRootCategory, checkpoint, loggerMessageSuffix);
    dpExtractor.start();/*from   w w  w . j a v  a 2 s .  co m*/
    ExtractNamespaces nsExtractor = new ExtractNamespaces(settings, language, loggerMessageSuffix,
            new File(settings.wikipediaEditionDirectory(language), ExtractNamespaces.NAMESPACE_FILE),
            checkpoint);
    nsExtractor.start();
    InfoboxTemplatesExtractor itExtractor = new InfoboxTemplatesExtractor(settings, language, itRootCategory,
            checkpoint, loggerMessageSuffix);
    itExtractor.start();
    ExtractGeoTags geotagsExtractor = new ExtractGeoTags(settings, language, loggerMessageSuffix);
    geotagsExtractor.start();
    try {
        dpExtractor.join();
        nsExtractor.join();
        this.ns = nsExtractor.namespaces();
        itExtractor.join();
        geotagsExtractor.join();
        this.geotags = geotagsExtractor.getGeoTags();
    } catch (InterruptedException e) {
        logger.severe("Problems with the threads.");
        e.printStackTrace();
        System.exit(-1);
    }
    XMLOutputFactory outputFactory = XMLOutputFactory2.newInstance();
    File outputFile = new File(settings.wikipediaEditionDirectory(language), TEMPORARY_LINK_FILE);
    if (checkpoint.isLinksExtracted(this.language)) {
        logger.info("Using pages and links from a previous computation");
        return;
    }
    try {
        FileOutputStream fout = new FileOutputStream(outputFile.getAbsolutePath());
        BufferedOutputStream bos = new BufferedOutputStream(fout);
        CompressorOutputStream output = new CompressorStreamFactory()
                .createCompressorOutputStream(CompressorStreamFactory.BZIP2, bos);
        XMLStreamWriter writer = outputFactory.createXMLStreamWriter(output, "UTF-8");
        writer.writeStartDocument();
        writer.writeStartElement("d");

        LinkExtractor linkExtractor = new LinkExtractor(writer, logger, settings, language,
                dpExtractor.disambiguationPages(), itExtractor.infoboxTemplates(), this.ns);
        linkExtractor.parse(settings.getWikipediaXmlFile(language).getAbsolutePath());
        writer.writeEndElement();
        writer.writeEndDocument();
        output.close();
        fout.close();
        bos.close();
        writer.close();
        long elapsed = System.currentTimeMillis() - startTime;
        logger.info("Data extracted in " + ReadableTime.readableTime(elapsed));
    } catch (Exception e) {
        logger.severe("Error while parsing the XML file ");
        e.printStackTrace();
        System.exit(-1);
    }
    try {
        checkpoint.addLinksExtracted(this.language, true);
    } catch (IOException e) {
        logger.severe("Error while saving the checkpoint to file");
        e.printStackTrace();
        System.exit(-1);
    }
    settings.getWikipediaXmlFile(language).delete();
}

From source file:org.gtdfree.model.GTDDataXMLTools.java

static public void store(GTDModel model, OutputStream out, ActionFilter filter)
        throws IOException, XMLStreamException, FactoryConfigurationError {

    if (filter == null) {
        filter = new DummyFilter(true);
    }//from   w w  w .  jav  a  2 s .  c o  m
    XMLStreamWriter w = XMLOutputFactory.newInstance().createXMLStreamWriter(out, "UTF-8"); //$NON-NLS-1$

    w.writeStartDocument("UTF-8", "1.0"); //$NON-NLS-1$ //$NON-NLS-2$

    w.writeCharacters(EOL);
    w.writeCharacters(EOL);

    w.writeStartElement("gtd-data"); //$NON-NLS-1$
    w.writeAttribute("version", "2.2"); //$NON-NLS-1$ //$NON-NLS-2$
    w.writeAttribute("modified", ApplicationHelper.formatLongISO(new Date())); //$NON-NLS-1$
    w.writeAttribute("lastActionID", Integer.toString(model.getLastActionID())); //$NON-NLS-1$
    w.writeCharacters(EOL);
    w.writeCharacters(EOL);

    // Write folders

    Folder[] fn = model.toFoldersArray();
    w.writeStartElement("lists"); //$NON-NLS-1$
    w.writeCharacters(EOL);

    for (int i = 0; i < fn.length; i++) {
        Folder ff = fn[i];
        if (ff.isMeta() || !filter.isAcceptable(ff, null)) {
            continue;
        }
        w.writeCharacters(SKIP);
        w.writeStartElement("list"); //$NON-NLS-1$
        w.writeAttribute("id", String.valueOf(ff.getId())); //$NON-NLS-1$
        w.writeAttribute("name", ff.getName()); //$NON-NLS-1$
        w.writeAttribute("type", ff.getType().toString()); //$NON-NLS-1$
        w.writeAttribute("closed", Boolean.toString(ff.isClosed())); //$NON-NLS-1$
        if (ff.getCreated() != null)
            w.writeAttribute("created", Long.toString(ff.getCreated().getTime())); //$NON-NLS-1$
        if (ff.getModified() != null)
            w.writeAttribute("modified", Long.toString(ff.getModified().getTime())); //$NON-NLS-1$
        if (ff.getResolved() != null)
            w.writeAttribute("resolved", Long.toString(ff.getResolved().getTime())); //$NON-NLS-1$
        if (!ff.isInBucket() && ff.getDescription() != null) {
            w.writeAttribute("description", ApplicationHelper.escapeControls(ff.getDescription())); //$NON-NLS-1$
        }
        w.writeCharacters(EOL);

        for (Action a : ff) {

            if (!filter.isAcceptable(ff, a)) {
                continue;
            }

            w.writeCharacters(SKIPSKIP);
            w.writeStartElement("action"); //$NON-NLS-1$
            w.writeAttribute("id", Integer.toString(a.getId())); //$NON-NLS-1$
            w.writeAttribute("created", Long.toString(a.getCreated().getTime())); //$NON-NLS-1$
            w.writeAttribute("resolution", a.getResolution().toString()); //$NON-NLS-1$
            if (a.getResolved() != null)
                w.writeAttribute("resolved", Long.toString(a.getResolved().getTime())); //$NON-NLS-1$
            if (a.getModified() != null)
                w.writeAttribute("modified", Long.toString(a.getModified().getTime())); //$NON-NLS-1$
            if (a.getDescription() != null)
                w.writeAttribute("description", ApplicationHelper.escapeControls(a.getDescription())); //$NON-NLS-1$
            if (a.getStart() != null)
                w.writeAttribute("start", Long.toString(a.getStart().getTime())); //$NON-NLS-1$
            if (a.getRemind() != null)
                w.writeAttribute("remind", Long.toString(a.getRemind().getTime())); //$NON-NLS-1$
            if (a.getDue() != null)
                w.writeAttribute("due", Long.toString(a.getDue().getTime())); //$NON-NLS-1$
            if (a.getType() != null)
                w.writeAttribute("type", a.getType().toString()); //$NON-NLS-1$
            if (a.getUrl() != null)
                w.writeAttribute("url", a.getUrl().toString()); //$NON-NLS-1$
            if (a.isQueued())
                w.writeAttribute("queued", Boolean.toString(a.isQueued())); //$NON-NLS-1$
            if (a.getProject() != null)
                w.writeAttribute("project", a.getProject().toString()); //$NON-NLS-1$
            if (a.getPriority() != null)
                w.writeAttribute("priority", a.getPriority().toString()); //$NON-NLS-1$
            w.writeEndElement();
            w.writeCharacters(EOL);
        }
        w.writeCharacters(SKIP);
        w.writeEndElement();
        w.writeCharacters(EOL);
    }
    w.writeEndElement();
    w.writeCharacters(EOL);

    // Write projects
    Project[] pn = model.toProjectsArray();
    w.writeStartElement("projects"); //$NON-NLS-1$
    w.writeCharacters(EOL);

    for (int i = 0; i < pn.length; i++) {
        Project ff = pn[i];

        if (!filter.isAcceptable(ff, null)) {
            continue;
        }

        w.writeCharacters(SKIP);
        w.writeStartElement("project"); //$NON-NLS-1$
        w.writeAttribute("id", String.valueOf(ff.getId())); //$NON-NLS-1$
        w.writeAttribute("name", ff.getName()); //$NON-NLS-1$
        w.writeAttribute("closed", String.valueOf(ff.isClosed())); //$NON-NLS-1$
        if (ff.getCreated() != null)
            w.writeAttribute("created", Long.toString(ff.getCreated().getTime())); //$NON-NLS-1$
        if (ff.getModified() != null)
            w.writeAttribute("modified", Long.toString(ff.getModified().getTime())); //$NON-NLS-1$
        if (ff.getResolved() != null)
            w.writeAttribute("resolved", Long.toString(ff.getResolved().getTime())); //$NON-NLS-1$

        if (ff.getDescription() != null) {
            w.writeAttribute("description", ApplicationHelper.escapeControls(ff.getDescription())); //$NON-NLS-1$
        }

        StringBuilder sb = new StringBuilder();

        for (Action a : ff) {
            if (!filter.isAcceptable(ff, a)) {
                continue;
            }
            if (sb.length() > 0) {
                sb.append(","); //$NON-NLS-1$
            }
            sb.append(a.getId());
        }
        w.writeAttribute("actions", sb.toString()); //$NON-NLS-1$
        w.writeEndElement();
        w.writeCharacters(EOL);
    }
    w.writeEndElement();
    w.writeCharacters(EOL);

    // Write queue
    Folder f = model.getQueue();

    if (filter.isAcceptable(f, null)) {
        w.writeStartElement("queue"); //$NON-NLS-1$
        w.writeAttribute("id", String.valueOf(f.getId())); //$NON-NLS-1$
        w.writeAttribute("name", f.getName()); //$NON-NLS-1$

        StringBuilder sb = new StringBuilder();
        Iterator<Action> i = f.iterator();
        if (i.hasNext()) {
            sb.append(i.next().getId());
        }
        while (i.hasNext()) {
            sb.append(","); //$NON-NLS-1$
            sb.append(i.next().getId());
        }
        w.writeAttribute("actions", sb.toString()); //$NON-NLS-1$
        w.writeEndElement();
        w.writeCharacters(EOL);
    }

    // containers
    w.writeEndElement();
    w.writeEndDocument();

    w.flush();
    w.close();

    //changed=false;

}

From source file:org.icgc.dcc.portal.manifest.writer.GNOSManifestWriter.java

private static void endXmlDocument(XMLStreamWriter writer) throws XMLStreamException {
    // Closes the root element - GNOS_ROOT
    writer.writeEndElement();
    writer.writeEndDocument();//  ww w . j av  a 2 s. co  m
}

From source file:org.icgc.dcc.portal.manifest.writer.GNOSManifestWriter.java

private static void addFileInfoEntriesToXml(XMLStreamWriter writer, Iterable<ManifestFile> info)
        throws XMLStreamException {
    for (val file : info) {
        writer.writeStartElement(GNOS_FILE);

        addXmlElement(writer, GNOS_FILE_NAME, file.getName());
        addXmlElement(writer, GNOS_FILE_SIZE, file.getSize() + "");

        writer.writeStartElement(GNOS_CHECK_SUM);
        writer.writeAttribute("type", "md5");
        writer.writeCharacters(file.getMd5sum());
        writer.writeEndElement();

        writer.writeEndElement();// w w  w  .  j a  v a 2  s.c  o  m
    }
}

From source file:org.icgc.dcc.portal.manifest.writer.GNOSManifestWriter.java

private static void closeDownloadUrlElement(@NonNull XMLStreamWriter writer) throws XMLStreamException {
    // Close off GNOS_FILES ("files") element.
    writer.writeEndElement();
    // Close off GNOS_RECORD ("Result") element.
    writer.writeEndElement();//w ww .j a v  a  2s . com
}

From source file:org.icgc.dcc.portal.manifest.writer.GNOSManifestWriter.java

private static void addXmlElement(XMLStreamWriter writer, String elementName, String elementValue)
        throws XMLStreamException {
    writer.writeStartElement(elementName);
    writer.writeCharacters(elementValue);
    writer.writeEndElement();
}

From source file:org.intermine.api.profile.ProfileManagerTest.java

public void testXMLWrite() throws Exception {
    setUpUserProfiles();/*from ww  w.jav a 2  s. c  om*/
    XMLUnit.setIgnoreWhitespace(true);
    StringWriter sw = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    TagManager tagManager = im.getTagManager();

    tagManager.addTag("test-tag", "Department.company", "reference", "bob");
    tagManager.addTag("test-tag2", "Department.name", "attribute", "bob");
    tagManager.addTag("test-tag2", "Department.company", "reference", "bob");
    tagManager.addTag("test-tag2", "Department.employees", "collection", "bob");

    tagManager.addTag("test-tag", "Department.company", "reference", "sally");

    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
        writer.writeStartElement("userprofiles");
        ProfileBinding.marshal(bobProfile, os, writer, PathQuery.USERPROFILE_VERSION, classKeys);
        ProfileBinding.marshal(sallyProfile, os, writer, PathQuery.USERPROFILE_VERSION, classKeys);
        writer.writeEndElement();
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }

    InputStream is = getClass().getClassLoader().getResourceAsStream("ProfileManagerBindingTest.xml");
    String expectedXml = IOUtils.toString(is);

    String actualXml = sw.toString().trim();

    assertEquals(normalise(expectedXml), normalise(actualXml));
}

From source file:org.intermine.api.tracker.xml.TemplateTrackBinding.java

/**
 * Convert a TemplateTrack to XML and write XML to given writer.
 * @param uos the UserObjectStore//  w  w w .java2  s .c  o m
 * @param writer the XMLStreamWriter to write to
 */
public static void marshal(ObjectStore uos, XMLStreamWriter writer) {
    Connection conn = null;
    Statement stm = null;
    ResultSet rs = null;

    try {
        writer.writeCharacters("\n");
        writer.writeStartElement(TEMPLATETRACKS);
        conn = ((ObjectStoreWriterInterMineImpl) uos).getConnection();
        String sql = "SELECT templatename, username, sessionidentifier,timestamp " + "FROM "
                + TrackerUtil.TEMPLATE_TRACKER_TABLE;
        stm = conn.createStatement();
        rs = stm.executeQuery(sql);
        while (rs.next()) {
            writer.writeCharacters("\n");
            writer.writeStartElement(TEMPLATETRACK);
            writer.writeAttribute("templatename", rs.getString(1));
            String username = rs.getString(2);
            if (!StringUtils.isBlank(username)) {
                writer.writeAttribute("username", username);
            }
            writer.writeAttribute("sessionidentifier", rs.getString(3));
            writer.writeAttribute("timestamp", rs.getTimestamp(4).toString());
            writer.writeEndElement();
        }
        writer.writeCharacters("\n");
        writer.writeEndElement();
    } catch (SQLException sqle) {
        LOG.error("The templatetrack table does't exist!", sqle);
        try {
            writer.writeEndElement();
        } catch (XMLStreamException e) {
        }
    } catch (XMLStreamException e) {
        throw new RuntimeException("exception while marshalling template tracks", e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                LOG.error("Problem closing  resources.", e);
            }
        }
        ((ObjectStoreWriterInterMineImpl) uos).releaseConnection(conn);
    }
}

From source file:org.intermine.api.xml.ProfileManagerBinding.java

/**
 * Convert the contents of a ProfileManager to XML and write the XML to the given writer.
 * @param profileManager the ProfileManager
 * @param writer the XMLStreamWriter to write to
 *///from  www  .  ja  v  a2  s .c  om
public static void marshal(ProfileManager profileManager, XMLStreamWriter writer) {
    try {
        writer.writeStartElement("userprofiles");
        String profileVersion = getProfileVersion(profileManager.getProfileObjectStoreWriter());
        writer.writeAttribute(MetadataManager.PROFILE_FORMAT_VERSION, profileVersion);
        List<?> usernames = profileManager.getProfileUserNames();

        for (Object userName : usernames) {
            Profile profile = profileManager.getProfile((String) userName);
            LOG.info("Writing profile: " + profile.getUsername());
            long startTime = System.currentTimeMillis();

            ProfileBinding.marshal(profile, profileManager.getProductionObjectStore(), writer,
                    profileManager.getVersion(), getClassKeys(profileManager.getProductionObjectStore()));

            long totalTime = System.currentTimeMillis() - startTime;
            LOG.info("Finished writing profile: " + profile.getUsername() + " took " + totalTime + "ms.");
        }

        TrackManagerBinding.marshal(profileManager.getProfileObjectStoreWriter(), writer);
        writer.writeEndElement();
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
}