List of usage examples for javax.xml.stream XMLStreamWriter writeStartElement
public void writeStartElement(String localName) throws XMLStreamException;
From source file:org.gluewine.trace.XMLTracer.java
@Override public void afterSuccess(Object o, Method m, Object[] params, Object result) { if (isSuppressed()) return;//from w w w .ja v a2 s . c om XMLStreamWriter writer = getWriter(); if (writer != null) { try { if (!m.getReturnType().equals(Void.TYPE)) { writer.writeStartElement("result"); if (result != null) writer.writeCharacters(result.toString()); else writer.writeCharacters("null"); writer.writeEndElement(); } } catch (Throwable e) { ErrorLogger.log(getClass(), e); } } clearSuppression(); }
From source file:org.gluewine.trace.XMLTracer.java
@Override public void afterFailure(Object o, Method m, Object[] params, Throwable e) { if (isSuppressed()) return;/*from www.j a v a2 s .c o m*/ XMLStreamWriter writer = getWriter(); if (writer != null) { try { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); writer.writeStartElement("exception"); writer.writeCharacters(sw.toString()); writer.writeEndElement(); } catch (Throwable t) { ErrorLogger.log(getClass(), t); } } clearSuppression(); }
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();// w w w.j a v a2s . c om 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); }/*w w w . ja v a 2 s .c om*/ 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 startXmlDocument(XMLStreamWriter writer, long timestamp) throws XMLStreamException { writer.writeStartDocument(FILE_ENCODING, "1.0"); writer.writeStartElement(GNOS_ROOT); writer.writeAttribute("date", formatToUtc(new Date(timestamp))); }
From source file:org.icgc.dcc.portal.manifest.writer.GNOSManifestWriter.java
private static void addDownloadUrlEntryToXml(XMLStreamWriter writer, String id, String downloadUrl, int rowCount) throws XMLStreamException { writer.writeStartElement(GNOS_RECORD); writer.writeAttribute("id", String.valueOf(rowCount)); addXmlElement(writer, GNOS_RECORD_ID, id); addXmlElement(writer, GNOS_RECORD_URI, downloadUrl); writer.writeStartElement(GNOS_FILES); }
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();/*from w ww . j a v a 2 s . c o m*/ writer.writeEndElement(); } }
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();//www. ja va 2s. c o m }
From source file:org.intermine.api.profile.ProfileManagerTest.java
public void testXMLWrite() throws Exception { setUpUserProfiles();/*from w w w .j a v a 2 s . com*/ 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//from www. j av a2 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); } }