/** * Exports a single sheet to a file * * @param sheet * @throws FactoryConfigurationError * @throws XMLStreamException * @throws UnsupportedEncodingException * @throws FileNotFoundException */ private void export(final XSSFSheet sheet, final XMLStreamWriter out) throws UnsupportedEncodingException, XMLStreamException, FactoryConfigurationError, FileNotFoundException { boolean isFirst = true; final Map<String, String> columns = new HashMap<String, String>(); final String sheetName = sheet.getSheetName(); System.out.print(sheetName); out.writeStartElement("sheet"); out.writeAttribute("name", sheetName); Iterator<Row> rowIterator = sheet.rowIterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); if (isFirst) { isFirst = false; this.writeFirstRow(row, out, columns); } else { this.writeRow(row, out, columns); } } out.writeEndElement(); System.out.println(".."); }
/** * Parses an inputstream containin xlsx into an outputStream containing XML * * @param inputStream * the source * @param outputStream * the result * @throws IOException * @throws XMLStreamException */ public void parse(final InputStream inputStream, final OutputStream outputStream) throws IOException, XMLStreamException { XSSFWorkbook workbook = new XSSFWorkbook(inputStream); XMLStreamWriter out = this.getXMLWriter(outputStream); out.writeStartDocument(); out.writeStartElement("workbook"); int sheetCount = workbook.getNumberOfSheets(); for (int i = 0; i < sheetCount; i++) { final XSSFSheet sheet = workbook.getSheetAt(i); try { this.export(sheet, out); } catch (UnsupportedEncodingException | FileNotFoundException | XMLStreamException | FactoryConfigurationError e) { e.printStackTrace(); } } out.writeEndElement(); out.writeEndDocument(); out.close(); workbook.close(); }
/** * @param inputStream The inputstream to read from * @return A new pull parser */ private static XMLStreamReader openPullParser(InputStream inputStream) { try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, Charsets.UTF_8)); Reader reader; int version = Version2to4TransformingReader.readVersion(bufferedReader); if (version == 2 || version == 3) { reader = new Version2to4TransformingReader(bufferedReader, version); } else { reader = bufferedReader; } if (version > 4) { throw new IllegalStateException("Unknown XML dataset format: "+version +" This dataset has been produced by a later version of Morf"); } return XMLInputFactory.newFactory().createXMLStreamReader(reader); } catch (XMLStreamException|FactoryConfigurationError e) { throw new RuntimeException(e); } }
@SuppressWarnings("unchecked") @Override public T read(HttpResponseMessage httpResponseMessage, Type expectedType) { Class<T> expectedClassType = (Class<T>) expectedType; JAXBContext context = contextOf(expectedClassType); try { Unmarshaller unmarshaller = context.createUnmarshaller(); StreamSource source = new StreamSource(httpResponseMessage.body()); XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(source); return expectedClassType.isAnnotationPresent(XmlRootElement.class) ? (T) unmarshaller.unmarshal(reader) : unmarshaller.unmarshal(reader, expectedClassType).getValue(); } catch (JAXBException | XMLStreamException | FactoryConfigurationError e) { throw new RestifyHttpMessageReadException("Error on try read xml message", e); } }
protected DataSet getDataForFile(File f, final ProgressMonitor progressMonitor) throws FileNotFoundException, IOException, XMLStreamException, FactoryConfigurationError, JAXBException { Main.debug("[DownloadIlocateTask.ZippedShpReader.getDataForFile] Calling MY getDataForFile"); if (f == null) { return null; } else if (!f.exists()) { Main.warn("File does not exist: " + f.getPath()); return null; } else { Main.info("Parsing zipped shapefile " + f.getName()); FileInputStream in = new FileInputStream(f); ProgressMonitor instance = null; if (progressMonitor != null) { instance = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false); } return ShpReader.parseDataSet(in, f, null, instance); } }
private String findDocIdFromXml(String xml) { try { XMLEventReader eventReader = XMLInputFactory.newInstance().createXMLEventReader(new StringReader(xml)); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.getEventType() == XMLEvent.START_ELEMENT) { StartElement element = event.asStartElement(); String elementName = element.getName().getLocalPart(); if (VespaDocumentOperation.Operation.valid(elementName)) { return element.getAttributeByName(QName.valueOf("documentid")).getValue(); } } } } catch (XMLStreamException | FactoryConfigurationError e) { // as json dude does return null; } return null; }
private void setUpXMLParser(ReadableByteChannel channel, byte[] lookAhead) throws IOException { try { // We use Woodstox because the StAX implementation provided by OpenJDK reports // character locations incorrectly. Note that Woodstox still currently reports *byte* // locations incorrectly when parsing documents that contain multi-byte characters. XMLInputFactory2 xmlInputFactory = (XMLInputFactory2) XMLInputFactory.newInstance(); this.parser = xmlInputFactory.createXMLStreamReader( new SequenceInputStream( new ByteArrayInputStream(lookAhead), Channels.newInputStream(channel)), getCurrentSource().configuration.getCharset()); // Current offset should be the offset before reading the record element. while (true) { int event = parser.next(); if (event == XMLStreamConstants.START_ELEMENT) { String localName = parser.getLocalName(); if (localName.equals(getCurrentSource().configuration.getRecordElement())) { break; } } } } catch (FactoryConfigurationError | XMLStreamException e) { throw new IOException(e); } }
private String _generate() throws IOException, XMLStreamException, FactoryConfigurationError { final ByteArrayOutputStream fos = new ByteArrayOutputStream(); writer = createWriter(fos); writeHead(); writeOutput(); writeJdk(); writeContent(); writeOrderEntrySourceFolder(); final Set<Path> allPaths = new HashSet<>(); final Set<Path> allModules = new HashSet<>(); if (this.dependencyResolver != null) { writeDependencies(dependencies, this.dependencyResolver, allPaths, allModules, false); } if (this.buildDependencyResolver != null) { writeDependencies(this.buildDependencies, this.buildDependencyResolver, allPaths, allModules, true); } writeBuildProjectDependencies(allModules); writeFoot(); writer.close(); return fos.toString(ENCODING); }
public static void main(String[] args) throws FactoryConfigurationError, JAXBException, XMLStreamException, IOException { List<String> xmlReports = new ArrayList<String>(); String[] extensions = {"xml"}; String xmlPath = System.getProperty("xmlPath"); String outputPath = System.getProperty("reportsOutputPath"); if (xmlPath == null || outputPath == null) { throw new Error("xmlPath or reportsOutputPath variables have not been set"); } Object[] files = FileUtils.listFiles(new File(xmlPath), extensions, false).toArray(); System.out.println("Found " + files.length + " xml files"); for (Object absFilePath : files) { System.out.println("Found an xml: " + absFilePath); xmlReports.add(((File) absFilePath).getAbsolutePath()); } TestNgReportBuilder repo = new TestNgReportBuilder(xmlReports, outputPath); repo.writeReportsOnDisk(); }
public TestNgReportBuilder(List<String> xmlReports, String targetBuildPath) throws JAXBException, XMLStreamException, FactoryConfigurationError, IOException { testOverviewPath = targetBuildPath + "/"; classesSummaryPath = targetBuildPath + "/classes-summary/"; processedTestNgReports = new ArrayList<>(); JAXBContext cntx = JAXBContext.newInstance(TestngResultsModel.class); Unmarshaller unm = cntx.createUnmarshaller(); for (String xml : xmlReports) { InputStream inputStream = new FileInputStream(xml); XMLStreamReader xmlStream = XMLInputFactory.newInstance().createXMLStreamReader(inputStream); TestngResultsModel ts = (TestngResultsModel) unm.unmarshal(xmlStream); ts.postProcess(); processedTestNgReports.add(ts); inputStream.close(); xmlStream.close(); } }
private static XMLInputFactory createXMLInputFactory() throws FactoryConfigurationError { XMLInputFactory factory = XMLInputFactory.newInstance(); if (!SUPPORT_DTD) { factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); //these next ones are somewhat redundant, we set them just in case the DTD support property is not respected factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); factory.setXMLResolver(new XMLResolver() { @Override public Object resolveEntity(String arg0, String arg1, String arg2, String arg3) throws XMLStreamException { throw new XMLStreamException("Reading external entities is disabled"); //$NON-NLS-1$ } }); } return factory; }
private static XMLOutputFactory getOrCreateOutputFactory() throws FactoryConfigurationError { if (ourOutputFactory == null) { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); if (!ourHaveLoggedStaxImplementation) { logStaxImplementation(outputFactory.getClass()); } /* * Note that these properties are Woodstox specific and they cause a crash in environments where SJSXP is * being used (e.g. glassfish) so we don't set them there. */ try { Class.forName("com.ctc.wstx.stax.WstxOutputFactory"); if (outputFactory instanceof WstxOutputFactory) { outputFactory.setProperty(XMLOutputFactory2.P_TEXT_ESCAPER, new MyEscaper()); } } catch (ClassNotFoundException e) { ourLog.debug("WstxOutputFactory (Woodstox) not found on classpath"); } ourOutputFactory = outputFactory; } return ourOutputFactory; }
/** * Test serialization of empty syndication title property. EmployeeName is set to NULL after the update (which is * allowed because EmployeeName has default Nullable behavior which is true). * Write of an empty atom title tag is allowed within RFC4287 (http://tools.ietf.org/html/rfc4287#section-4.2.14). */ @Test public void serializeEmployeeWithNullSyndicationTitleProperty() throws IOException, XpathException, SAXException, XMLStreamException, FactoryConfigurationError, ODataException { AtomEntityProvider ser = createAtomEntityProvider(); EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(BASE_URI).build(); employeeData.put("EmployeeName", null); ODataResponse response = ser.writeEntry(MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees"), employeeData, properties); String xmlString = verifyResponse(response); assertXpathExists("/a:entry/a:title", xmlString); assertXpathEvaluatesTo("", "/a:entry/a:title", xmlString); assertXpathExists("/a:entry", xmlString); assertXpathEvaluatesTo(BASE_URI.toASCIIString(), "/a:entry/@xml:base", xmlString); assertXpathExists("/a:entry/a:content", xmlString); assertXpathEvaluatesTo("Employees('1')/$value", "/a:entry/a:content/@src", xmlString); assertXpathExists("/a:entry/m:properties", xmlString); }
@Test public void serializeEmployeeAndCheckOrderOfPropertyTags() throws IOException, XpathException, SAXException, XMLStreamException, FactoryConfigurationError, ODataException { AtomEntityProvider ser = createAtomEntityProvider(); EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(BASE_URI).build(); EdmEntitySet employeeEntitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees"); ODataResponse response = ser.writeEntry(employeeEntitySet, employeeData, properties); String xmlString = verifyResponse(response); // log.debug(xmlString); assertXpathExists("/a:entry", xmlString); assertXpathExists("/a:entry/a:content", xmlString); // verify properties assertXpathExists("/a:entry/m:properties", xmlString); assertXpathEvaluatesTo("9", "count(/a:entry/m:properties/*)", xmlString); // verify order of tags List<String> expectedPropertyNamesFromEdm = employeeEntitySet.getEntityType().getPropertyNames(); verifyTagOrdering(xmlString, expectedPropertyNamesFromEdm.toArray(new String[0])); }
@Test public void serializeAtomEntry() throws IOException, XpathException, SAXException, XMLStreamException, FactoryConfigurationError, ODataException { final EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(BASE_URI).build(); AtomEntityProvider ser = createAtomEntityProvider(); ODataResponse response = ser.writeEntry(MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms"), roomData, properties); String xmlString = verifyResponse(response); assertXpathExists("/a:entry", xmlString); assertXpathEvaluatesTo(BASE_URI.toASCIIString(), "/a:entry/@xml:base", xmlString); assertXpathExists("/a:entry/a:content", xmlString); assertXpathEvaluatesTo(ContentType.APPLICATION_XML.toString(), "/a:entry/a:content/@type", xmlString); assertXpathExists("/a:entry/a:content/m:properties", xmlString); }
@Test public void serializeWithValueEncoding() throws IOException, XpathException, SAXException, XMLStreamException, FactoryConfigurationError, ODataException { photoData.put("Type", "< Ö >"); AtomEntityProvider ser = createAtomEntityProvider(); ODataResponse response = ser.writeEntry(MockFacade.getMockEdm().getEntityContainer("Container2").getEntitySet("Photos"), photoData, DEFAULT_PROPERTIES); String xmlString = verifyResponse(response); assertXpathExists("/a:entry", xmlString); assertXpathEvaluatesTo(BASE_URI.toASCIIString(), "/a:entry/@xml:base", xmlString); assertXpathExists("/a:entry/a:id", xmlString); assertXpathEvaluatesTo(BASE_URI.toASCIIString() + "Container2.Photos(Id=1,Type='%3C%20%C3%96%20%3E')", "/a:entry/a:id/text()", xmlString); assertXpathEvaluatesTo("Container2.Photos(Id=1,Type='%3C%20%C3%96%20%3E')", "/a:entry/a:link/@href", xmlString); }
@Test public void serializeETagEncoding() throws IOException, XpathException, SAXException, XMLStreamException, FactoryConfigurationError, ODataException { Edm edm = MockFacade.getMockEdm(); EdmTyped roomIdProperty = edm.getEntityType("RefScenario", "Room").getProperty("Id"); EdmFacets facets = mock(EdmFacets.class); when(facets.getConcurrencyMode()).thenReturn(EdmConcurrencyMode.Fixed); when(facets.getMaxLength()).thenReturn(3); when(((EdmProperty) roomIdProperty).getFacets()).thenReturn(facets); roomData.put("Id", "<\">"); AtomEntityProvider ser = createAtomEntityProvider(); ODataResponse response = ser.writeEntry(edm.getDefaultEntityContainer().getEntitySet("Rooms"), roomData, DEFAULT_PROPERTIES); assertNotNull(response); assertNotNull(response.getEntity()); assertNull("EntityProvider should not set content header", response.getContentHeader()); assertEquals("W/\"<\">.3\"", response.getETag()); String xmlString = StringHelper.inputStreamToString((InputStream) response.getEntity()); assertXpathExists("/a:entry", xmlString); assertXpathExists("/a:entry/@m:etag", xmlString); assertXpathEvaluatesTo("W/\"<\">.3\"", "/a:entry/@m:etag", xmlString); }
@Test public void serializeAtomMediaResourceLinks() throws IOException, XpathException, SAXException, XMLStreamException, FactoryConfigurationError, ODataException { AtomEntityProvider ser = createAtomEntityProvider(); ODataResponse response = ser.writeEntry(MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees"), employeeData, DEFAULT_PROPERTIES); String xmlString = verifyResponse(response); String rel = Edm.NAMESPACE_REL_2007_08 + "ne_Manager"; assertXpathExists("/a:entry/a:link[@href=\"Employees('1')/ne_Manager\"]", xmlString); assertXpathExists("/a:entry/a:link[@rel='" + rel + "']", xmlString); assertXpathExists("/a:entry/a:link[@type='application/atom+xml;type=entry']", xmlString); assertXpathExists("/a:entry/a:link[@title='ne_Manager']", xmlString); }
private <T> T innerCallRest(String uri, Class<T> response) throws InterruptedException, ExecutionException, TimeoutException, JAXBException, FactoryConfigurationError, XMLStreamException { Future<Response> future = client.prepareGet(uri).execute(); Response resp = future.get(REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS); // TelldusLiveHandler.logger.info("Devices" + resp.getResponseBody()); JAXBContext jc = JAXBContext.newInstance(response); XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader xsr = xif.createXMLStreamReader(resp.getResponseBodyAsStream()); // xsr = new PropertyRenamerDelegate(xsr); @SuppressWarnings("unchecked") T obj = (T) jc.createUnmarshaller().unmarshal(xsr); if (logger.isTraceEnabled()) { logger.trace("Request [{}] Response:{}", uri, resp.getResponseBody()); } return obj; }
private void aggregateDependencies(List<MavenProject> mavenProjects) { dependencySystemPathMap = new HashMap<>(); for (MavenProject mavenProject : mavenProjects) { String packaging = mavenProject.getPackaging(); // CAPP projects are ignored. if (packaging == null || !MavenConstants.CAPP_PACKAGING.equals(packaging)) { try { dependencySystemPathMap.putAll(PackagePrepareUtils.getArtifactsSystemPathMap(mavenProject)); } catch (FactoryConfigurationError | Exception e) { // Can proceed even if this is reached log.warn("Failed to retrieve dependencies from project: " + mavenProject.getGroupId() + ":" + mavenProject.getArtifactId() + ":" + mavenProject.getVersion(), e); } } } if (isDebugEnabled) { Iterator<Entry<String, String>> dependencyIterator = dependencySystemPathMap.entrySet().iterator(); while (dependencyIterator.hasNext()) { log.debug("Identified system path of: " + dependencyIterator.next().getKey()); } } }
/** * set Registry artifact version in artifact.xml file * * @param prop * properties in release.properties file * @param repoType * type of the repository * @param artifactXml * @throws FactoryConfigurationError * @throws Exception */ private void setRegArtifactVersion(Properties prop, String repoType, File artifactXml) throws FactoryConfigurationError, Exception { File pomFile = new File(artifactXml.getParent() + File.separator + POM_XML); MavenProject mavenProject=getMavenProject(pomFile); String releaseVersion = prop.getProperty(PROJECT + repoType + "."+ mavenProject.getGroupId() + ":" + mavenProject.getArtifactId()); GeneralProjectArtifact projectArtifact = new GeneralProjectArtifact(); projectArtifact.fromFile(artifactXml); for (RegistryArtifact artifact : projectArtifact.getAllESBArtifacts()) { if (artifact.getVersion() != null && artifact.getType() != null) { if (releaseVersion != null) { artifact.setVersion(releaseVersion); } } } projectArtifact.toFile(); }
/** * set ESB artifact version in artifact.xml file * * @param prop * properties in release.properties file * @param repoType * type of the repository * @param artifactXml * @throws FactoryConfigurationError * @throws Exception */ private void setESBArtifactVersion(Properties prop, String repoType, File artifactXml) throws FactoryConfigurationError, Exception { File pomFile = new File(artifactXml.getParent() + File.separator + POM_XML); MavenProject mavenProject=getMavenProject(pomFile); String releaseVersion = prop.getProperty(PROJECT + repoType + "."+mavenProject.getGroupId() + ":" + mavenProject.getArtifactId()); ESBProjectArtifact projectArtifact = new ESBProjectArtifact(); projectArtifact.fromFile(artifactXml); for (ESBArtifact artifact : projectArtifact.getAllESBArtifacts()) { if (artifact.getVersion() != null && artifact.getType() != null) { if (releaseVersion != null) { artifact.setVersion(releaseVersion); } } } projectArtifact.toFile(); }
protected BundlesDataInfo getBundlesDataInfo(File targetProjectLocation, Artifact artifact)throws FactoryConfigurationError { if (bundlesDataInfo==null) { try { bundlesDataInfo = new BundlesDataInfo(); bundlesDataInfo.setProjects(getProjectMappings()); List<String> artifactProjects = getArtifactProjects(); for (String artifactProject : artifactProjects) { String[] artifactProjectData = artifactProject.split(":"); if (artifactProjectData.length==2 && artifactProjectData[0].equals(artifact.getName())){ String[] projectNames = artifactProjectData[1].split(","); for (String projectName : projectNames) { bundlesDataInfo.addProjectToList(projectName, null); } } } } catch (Exception e) { e.printStackTrace(); } } return bundlesDataInfo; }
protected boolean isDocumentTag(InputStream dataStream, String tagName) throws FactoryConfigurationError { try { String content = FileUtils.getContentAsString(dataStream); if (content != null) { content = content.toLowerCase(); if (content.contains("<html") && content.contains("</html>")) { if (tagName.equals("html")) { return true; } } dataStream = new ByteArrayInputStream(content.getBytes()); } OMElement documentElement = getXmlDoc(dataStream); String localName = documentElement.getLocalName(); return localName.equalsIgnoreCase(tagName); } catch (Exception e) { return false; } }
private void addPoliciesToDescriptionElement(List policies, OMElement descriptionElement) throws XMLStreamException, FactoryConfigurationError { for (int i = 0; i < policies.size(); i++) { Policy policy = (Policy) policies.get(i); OMElement policyElement = PolicyUtil.getPolicyComponentAsOMElement( policy, filter); OMNode firstChild = descriptionElement.getFirstOMChild(); if (firstChild != null) { firstChild.insertSiblingBefore(policyElement); } else { descriptionElement.addChild(policyElement); } } }
public static OMElement getPolicyComponentAsOMElement( PolicyComponent policyComponent, ExternalPolicySerializer externalPolicySerializer) throws XMLStreamException, FactoryConfigurationError { if (policyComponent instanceof Policy) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); externalPolicySerializer.serialize((Policy) policyComponent, baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos .toByteArray()); return (OMElement) XMLUtils.toOM(bais); } else { OMFactory fac = OMAbstractFactory.getOMFactory(); OMElement elem = fac.createOMElement(Constants.ELEM_POLICY_REF, Constants.URI_POLICY_NS, Constants.ATTR_WSP); elem.addAttribute(Constants.ATTR_URI, ((PolicyReference) policyComponent).getURI(), null); return elem; } }
private List<Resource> createGetFeatureResourcesAllDatasets() throws JaxenException, XMLStreamException, FactoryConfigurationError, IOException { List<Resource> getFeatureResourcesAllDatasets = new ArrayList<Resource>(); List<Dataset> datasets = managerDao.getAllDatasets(); int quantityDatasetsLoadedInInspireSchema = 0; for (Iterator<Dataset> datasetIterator = datasets.iterator(); datasetIterator.hasNext();) { Dataset dataset = (Dataset) datasetIterator.next(); EtlJob job = managerDao.getLastSuccessfullImportJob(dataset.getBronhouder(), dataset.getDatasetType(), dataset.getUuid()); if(job != null){ String fileCacheDir = null;//fileCache.getFiledir(job); String file = null;//fileCache.getFilename(job); Resource cachedResource = new FileSystemResource(fileCacheDir + System.getProperty("file.separator") + file); logger.debug("Adding cachedResource(" + quantityDatasetsLoadedInInspireSchema + ") " + "\"" + cachedResource.getDescription() + "\" to the cached-resources-list."); getFeatureResourcesAllDatasets.add(cachedResource); quantityDatasetsLoadedInInspireSchema++; } } logger.debug("Quantity of datasets that resulted in features in inspire schema:" + quantityDatasetsLoadedInInspireSchema); return getFeatureResourcesAllDatasets; }
public static List<TaggedSentence> getTaggedSentences(String xmlCorpusPath, int minNumberOfTokens) throws XMLStreamException, FactoryConfigurationError, FileNotFoundException { List<TaggedSentence> taggedSentences = new ArrayList<>(); XMLEventReader xmlEventReader = XMLInputFactory.newInstance().createXMLEventReader(new FileInputStream(xmlCorpusPath)); while (xmlEventReader.hasNext()) { XMLEvent event = xmlEventReader.nextEvent(); if (event.isStartElement() && "source".equals(event.asStartElement().getName().getLocalPart())) { String sentence = extractSentence(xmlEventReader); List<StringTaggedToken> taggedTokens = extractTaggedTokens(xmlEventReader); if (!allTagsRecognized(taggedTokens) || (taggedTokens.size() < minNumberOfTokens)) { continue; } TaggedSentence taggedSentence = createTaggedSentence(sentence, taggedTokens); taggedSentences.add(taggedSentence); } } return taggedSentences; }
public static void main(String ars[]) throws XMLStreamException, FactoryConfigurationError, IOException { for (int page = 1; page <= 10; page++) { System.out.println("Fetching page " + page); URL url = new URL(MUBI_LISTS_BASE_URL + "&page=" + page); List<MubiListRef> lists = MubiListsReader.getInstance() .readMubiLists(url); for (MubiListRef list : lists) { System.out.println(" Fetching list " + list.getTitle()); List<MubiFilmRef> filmList = MubiListsReader.getInstance() .readMubiFilmList( new URL(MUBI_BASE_URL + list.getUrl())); list.addFilms(filmList); } File outfile = new File("output", "mubi-lists-page-" + String.format("%04d", page) + ".json"); System.out.println("Writing "+ outfile.getName()); mapper.writeValue(outfile, lists); } }
public static XMLStreamReader getXMLStreamReader(InputStream is) throws XMLStreamException, FactoryConfigurationError { //return XMLInputFactory.newInstance().createXMLStreamReader(is); return new StreamReaderDelegate(XMLInputFactory.newInstance().createXMLStreamReader(is)) { public int next() throws XMLStreamException { while (true) { int event = super.next(); switch (event) { case XMLStreamConstants.COMMENT: case XMLStreamConstants.PROCESSING_INSTRUCTION: continue; default: return event; } } } }; }
@Test public void readIntAttribute() throws XMLStreamException, FactoryConfigurationError, IOException { URL url = XmlHelperTest.class.getResource("/xml/kb-layout.xml"); XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(url.openStream()); reader.next(); reader.require(XMLStreamConstants.START_ELEMENT, null, XmlHelper.KEYBOARD); assertEquals(Integer.valueOf(40), XmlHelper.readIntAttribute(reader, XmlHelper.ATTR_KEY_WIDTH).orElse(-1)); assertEquals(Integer.valueOf(30), XmlHelper.readIntAttribute(reader, XmlHelper.ATTR_KEY_HEIGHT).orElse(-1)); assertEquals(Integer.valueOf(0), XmlHelper.readIntAttribute(reader, XmlHelper.ATTR_H_GAP).orElse(-1)); assertEquals(Integer.valueOf(0), XmlHelper.readIntAttribute(reader, XmlHelper.ATTR_V_GAP).orElse(-1)); assertNull(XmlHelper.readIntAttribute(reader, "verticalGapX").orElse(null)); assertEquals(40, XmlHelper.readIntAttribute(reader, XmlHelper.ATTR_KEY_WIDTH, 1)); assertEquals(30, XmlHelper.readIntAttribute(reader, XmlHelper.ATTR_KEY_HEIGHT, 1)); assertEquals(0, XmlHelper.readIntAttribute(reader, XmlHelper.ATTR_H_GAP, 1)); assertEquals(0, XmlHelper.readIntAttribute(reader, XmlHelper.ATTR_V_GAP, 1)); assertEquals(1, XmlHelper.readIntAttribute(reader, "verticalGapX", 1)); assertEquals(1, XmlHelper.readIntAttribute(reader, "", 1)); assertEquals(1, XmlHelper.readIntAttribute(reader, null, 1)); }
@Test public void readBooleanAttribute() throws XMLStreamException, FactoryConfigurationError, IOException { URL url = XmlHelperTest.class.getResource("/xml/kb-layout.xml"); XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(url.openStream()); reader.next(); reader.require(XMLStreamConstants.START_ELEMENT, null, XmlHelper.KEYBOARD); assertFalse(XmlHelper.readBooleanAttribute(reader, XmlHelper.ATTR_REPEATABLE, false)); assertFalse(XmlHelper.readBooleanAttribute(reader, "", false)); while (reader.hasNext()) { reader.next(); if (!reader.isStartElement() || !XmlHelper.KEY.equals(reader.getLocalName())) { continue; } if (32 == XmlHelper.readIntAttribute(reader, XmlHelper.ATTR_CODES, -1)) { assertTrue(XmlHelper.readBooleanAttribute(reader, XmlHelper.ATTR_REPEATABLE, false)); assertTrue(XmlHelper.readBooleanAttribute(reader, XmlHelper.ATTR_MOVABLE, false)); } if (-1 == XmlHelper.readIntAttribute(reader, XmlHelper.ATTR_CODES, 0)) { assertTrue(XmlHelper.readBooleanAttribute(reader, XmlHelper.ATTR_STICKY, false)); } } }
protected void write(Source object, XMLStreamWriter writer) throws FactoryConfigurationError, XMLStreamException, DatabindingException { if (object == null) { return; } if (object instanceof DOMSource) { DOMSource ds = (DOMSource)object; Element element = null; if (ds.getNode() instanceof Element) { element = (Element)ds.getNode(); } else if (ds.getNode() instanceof Document) { element = ((Document)ds.getNode()).getDocumentElement(); } else { throw new DatabindingException("Node type " + ds.getNode().getClass() + " was not understood."); } StaxUtils.writeElement(element, writer, false); } else { StaxUtils.copy(object, writer); } }
private void open(File file) throws XMLStreamException, FactoryConfigurationError, FileNotFoundException { // open writer FileOutputStream fos = new FileOutputStream(file); writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fos, "UTF-8"); // start document writer.setDefaultNamespace(URI_NS); writer.writeStartDocument("UTF-8", "1.0"); writer.writeCharacters("\n"); writer.writeStartElement(URI_NS, "journal"); if (URI_NS.length() > 0) { writer.writeDefaultNamespace(URI_NS); } writer.writeCharacters("\n"); writer.writeStartElement(URI_NS, "participant"); writer.writeCharacters(participantId); writer.writeEndElement(); }
private void open(File file) throws FileNotFoundException, XMLStreamException, FactoryConfigurationError, DatatypeConfigurationException { // open writer FileOutputStream fos = new FileOutputStream(file); writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fos, "UTF-8"); // start document writer.setDefaultNamespace(URI_NS); writer.writeStartDocument("UTF-8", "1.0"); writer.writeCharacters("\n"); writer.writeStartElement(URI_NS, "insights"); if (URI_NS.length() > 0) { writer.writeDefaultNamespace(URI_NS); } writer.writeNamespace("html", URI_NS_XHTML); writer.writeCharacters("\n"); // writer.writeStartElement(URI_NS, "participant"); // writer.writeCharacters(participantId); // writer.writeEndElement(); // reset index index = 0; timeConv = new XmlCalendarConverter(); }
/** * Test serialization of empty syndication title property. EmployeeName is set to NULL after the update (which is allowed because EmployeeName has default Nullable behavior which is true). * Write of an empty atom title tag is allowed within RFC4287 (http://tools.ietf.org/html/rfc4287#section-4.2.14). */ @Test public void serializeEmployeeWithNullSyndicationTitleProperty() throws IOException, XpathException, SAXException, XMLStreamException, FactoryConfigurationError, ODataException { AtomEntityProvider ser = createAtomEntityProvider(); EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(BASE_URI).build(); employeeData.put("EmployeeName", null); ODataResponse response = ser.writeEntry(MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees"), employeeData, properties); String xmlString = verifyResponse(response); assertXpathExists("/a:entry/a:title", xmlString); assertXpathEvaluatesTo("", "/a:entry/a:title", xmlString); assertXpathExists("/a:entry", xmlString); assertXpathEvaluatesTo(BASE_URI.toASCIIString(), "/a:entry/@xml:base", xmlString); assertXpathExists("/a:entry/a:content", xmlString); assertXpathEvaluatesTo("Employees('1')/$value", "/a:entry/a:content/@src", xmlString); assertXpathExists("/a:entry/m:properties", xmlString); }
@Test public void serializeEmployeeAndCheckOrderOfPropertyTags() throws IOException, XpathException, SAXException, XMLStreamException, FactoryConfigurationError, ODataException { AtomEntityProvider ser = createAtomEntityProvider(); EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(BASE_URI).mediaResourceMimeType("abc").build(); EdmEntitySet employeeEntitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees"); ODataResponse response = ser.writeEntry(employeeEntitySet, employeeData, properties); String xmlString = verifyResponse(response); // log.debug(xmlString); assertXpathExists("/a:entry", xmlString); assertXpathExists("/a:entry/a:content", xmlString); // verify properties assertXpathExists("/a:entry/m:properties", xmlString); assertXpathEvaluatesTo("9", "count(/a:entry/m:properties/*)", xmlString); // verify order of tags List<String> expectedPropertyNamesFromEdm = employeeEntitySet.getEntityType().getPropertyNames(); verifyTagOrdering(xmlString, expectedPropertyNamesFromEdm.toArray(new String[0])); }
@Test public void serializeETagEncoding() throws IOException, XpathException, SAXException, XMLStreamException, FactoryConfigurationError, ODataException { Edm edm = MockFacade.getMockEdm(); EdmTyped roomIdProperty = edm.getEntityType("RefScenario", "Room").getProperty("Id"); EdmFacets facets = mock(EdmFacets.class); when(facets.getConcurrencyMode()).thenReturn(EdmConcurrencyMode.Fixed); when(facets.getMaxLength()).thenReturn(3); when(((EdmProperty) roomIdProperty).getFacets()).thenReturn(facets); roomData.put("Id", "<\">"); AtomEntityProvider ser = createAtomEntityProvider(); ODataResponse response = ser.writeEntry(edm.getDefaultEntityContainer().getEntitySet("Rooms"), roomData, DEFAULT_PROPERTIES); assertNotNull(response); assertNotNull(response.getEntity()); assertEquals(ContentType.APPLICATION_ATOM_XML_ENTRY_CS_UTF_8.toContentTypeString(), response.getContentHeader()); assertEquals("W/\"<\">.3\"", response.getETag()); String xmlString = StringHelper.inputStreamToString((InputStream) response.getEntity()); assertXpathExists("/a:entry", xmlString); assertXpathExists("/a:entry/@m:etag", xmlString); assertXpathEvaluatesTo("W/\"<\">.3\"", "/a:entry/@m:etag", xmlString); }
public FromXMLStreamIterator() throws XMLStreamException { XMLInputFactory fac = XMLInputFactory.newInstance(); try { this.xr = fac.createXMLStreamReader(Files.newInputStream(MzMLStAXParser.this.xml, StandardOpenOption.READ)); } catch (FactoryConfigurationError | IOException e) { LOGGER.log(Level.ERROR, e.getMessage()); System.exit(-1); } if (!this.moveToNextSpectrum()){ LOGGER.log(Level.WARN, "no spectrum found in mzml file"); } }
private T getNextSpectrumFromSeekable() { FromXMLStreamBuilder<T> spectrumBuilder = null; try { InputStream is = Channels.newInputStream(this.seekable); XMLStreamReader xr = XMLInputFactory.newInstance() .createXMLStreamReader(is); while (xr.hasNext()) { xr.next(); if (spectrumBuilder != null) { spectrumBuilder.accept(xr); } if(xr.getEventType() == XMLStreamReader.START_ELEMENT){ if(xr.getLocalName().equals("spectrum")) { spectrumBuilder = this.factory.create(this.xml.toString(), xr); } else if( xr.getLocalName().equals("referenceableParamGroupRef")) { LOGGER.log(Level.WARN, "Random access to spectra will not parse referenceable params"); } } else if(xr.getEventType() == XMLStreamReader.END_ELEMENT) { if(xr.getLocalName().equals("spectrum")) { return spectrumBuilder.build(); } } } } catch (XMLStreamException | FactoryConfigurationError e) { LOGGER.log(Level.ERROR, e.toString()); } return null; }