List of usage examples for javax.json JsonWriter write
default void write(JsonValue value)
From source file:de.pangaea.fixo3.xml.ProcessXmlFiles.java
private void run() throws FileNotFoundException, SAXException, IOException, ParserConfigurationException, XPathExpressionException { // src/test/resources/, src/main/resources/files File folder = new File("src/main/resources/files"); File[] files = folder.listFiles(new FileFilter() { @Override//from w w w .j a v a 2 s .c o m public boolean accept(File pathname) { if (pathname.getName().endsWith(".xml")) return true; return false; } }); JsonArrayBuilder json = Json.createArrayBuilder(); Document doc; String deviceLabel, deviceComment, deviceImage; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); final Map<String, String> prefixToNS = new HashMap<>(); prefixToNS.put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI); prefixToNS.put(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI); prefixToNS.put("sml", "http://www.opengis.net/sensorml/2.0"); prefixToNS.put("gml", "http://www.opengis.net/gml/3.2"); prefixToNS.put("gmd", "http://www.isotc211.org/2005/gmd"); XPath x = XPathFactory.newInstance().newXPath(); x.setNamespaceContext(new NamespaceContext() { @Override public String getNamespaceURI(String prefix) { Objects.requireNonNull(prefix, "Namespace prefix cannot be null"); final String uri = prefixToNS.get(prefix); if (uri == null) { throw new IllegalArgumentException("Undeclared namespace prefix: " + prefix); } return uri; } @Override public String getPrefix(String namespaceURI) { throw new UnsupportedOperationException(); } @Override public Iterator<?> getPrefixes(String namespaceURI) { throw new UnsupportedOperationException(); } }); XPathExpression exGmlName = x.compile("/sml:PhysicalSystem/gml:name"); XPathExpression exGmlDescription = x.compile("/sml:PhysicalSystem/gml:description"); XPathExpression exGmdUrl = x.compile( "/sml:PhysicalSystem/sml:documentation/sml:DocumentList/sml:document/gmd:CI_OnlineResource/gmd:linkage/gmd:URL"); XPathExpression exTerm = x .compile("/sml:PhysicalSystem/sml:classification/sml:ClassifierList/sml:classifier/sml:Term"); int m = 0; int n = 0; for (File file : files) { log.info(file.getName()); doc = dbf.newDocumentBuilder().parse(new FileInputStream(file)); deviceLabel = exGmlName.evaluate(doc).trim(); deviceComment = exGmlDescription.evaluate(doc).trim(); deviceImage = exGmdUrl.evaluate(doc).trim(); NodeList terms = (NodeList) exTerm.evaluate(doc, XPathConstants.NODESET); JsonObjectBuilder jobDevice = Json.createObjectBuilder(); jobDevice.add("name", toUri(deviceLabel)); jobDevice.add("label", deviceLabel); jobDevice.add("comment", toAscii(deviceComment)); jobDevice.add("image", deviceImage); JsonArrayBuilder jabSubClasses = Json.createArrayBuilder(); for (int i = 0; i < terms.getLength(); i++) { Node term = terms.item(i); NodeList attributes = term.getChildNodes(); String attributeLabel = null; String attributeValue = null; for (int j = 0; j < attributes.getLength(); j++) { Node attribute = attributes.item(j); String attributeName = attribute.getNodeName(); if (attributeName.equals("sml:label")) { attributeLabel = attribute.getTextContent(); } else if (attributeName.equals("sml:value")) { attributeValue = attribute.getTextContent(); } } if (attributeLabel == null || attributeValue == null) { throw new RuntimeException("Attribute label or value cannot be null [attributeLabel = " + attributeLabel + "; attributeValue = " + attributeValue + "]"); } if (attributeLabel.equals("model")) { continue; } if (attributeLabel.equals("manufacturer")) { jobDevice.add("manufacturer", attributeValue); continue; } n++; Quantity quantity = getQuantity(attributeValue); if (quantity == null) { continue; } m++; JsonObjectBuilder jobSubClass = Json.createObjectBuilder(); JsonObjectBuilder jobCapability = Json.createObjectBuilder(); JsonObjectBuilder jobQuantity = Json.createObjectBuilder(); String quantityLabel = getQuantityLabel(attributeLabel); String capabilityLabel = deviceLabel + " " + quantityLabel; jobCapability.add("label", capabilityLabel); jobQuantity.add("label", quantity.getLabel()); if (quantity.getValue() != null) { jobQuantity.add("value", quantity.getValue()); } else if (quantity.getMinValue() != null && quantity.getMaxValue() != null) { jobQuantity.add("minValue", quantity.getMinValue()); jobQuantity.add("maxValue", quantity.getMaxValue()); } else { throw new RuntimeException( "Failed to determine quantity value [attributeValue = " + attributeValue + "]"); } jobQuantity.add("unitCode", quantity.getUnitCode()); jobQuantity.add("type", toUri(quantityLabel)); jobCapability.add("quantity", jobQuantity); jobSubClass.add("capability", jobCapability); jabSubClasses.add(jobSubClass); } jobDevice.add("subClasses", jabSubClasses); json.add(jobDevice); } Map<String, Object> properties = new HashMap<>(1); properties.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(properties); JsonWriter jsonWriter = jsonWriterFactory.createWriter(new FileWriter(new File(jsonFileName))); jsonWriter.write(json.build()); jsonWriter.close(); System.out.println("Fraction of characteristics included: " + m + "/" + n); }
From source file:org.jboss.set.aphrodite.stream.services.json.JsonStreamService.java
@Override public void serializeStreams(URL url, OutputStream out) throws NotFoundException { final Collection<Stream> streams = this.urlToParsedStreams.get(url); if (streams == null) { throw new NotFoundException("No matching set of streams for '" + url + "'"); }// ww w . j a va 2 s. c om JsonObject jsonObject = StreamsJsonParser.encode(streams); // JsonWriter jsonWriter = Json.createWriter(out); // jsonWriter.writeObject(jsonObject); // jsonWriter.close(); Map<String, Boolean> config = buildConfig(); JsonWriterFactory writerFactory = Json.createWriterFactory(config); JsonWriter jsonWriter = writerFactory.createWriter(out); jsonWriter.write(jsonObject); jsonWriter.close(); }