List of usage examples for org.xml.sax InputSource setEncoding
public void setEncoding(String encoding)
From source file:com.granule.json.utils.XML.java
/** * Method to do the transform from an XML input stream to a JSON stream. * Neither input nor output streams are closed. Closure is left up to the caller. * * @param XMLStream The XML stream to convert to JSON * @param JSONStream The stream to write out JSON to. The contents written to this stream are always in UTF-8 format. * @param verbose Flag to denote whether or not to render the JSON text in verbose (indented easy to read), or compact (not so easy to read, but smaller), format. * * @throws SAXException Thrown if a parse error occurs. * @throws IOException Thrown if an IO error occurs. *//* w ww .j a va2 s. c o m*/ public static void toJson(InputStream XMLStream, OutputStream JSONStream, boolean verbose) throws SAXException, IOException { if (logger.isLoggable(Level.FINER)) { logger.entering(className, "toJson(InputStream, OutputStream)"); } if (XMLStream == null) { throw new NullPointerException("XMLStream cannot be null"); } else if (JSONStream == null) { throw new NullPointerException("JSONStream cannot be null"); } else { if (logger.isLoggable(Level.FINEST)) { logger.logp(Level.FINEST, className, "transform", "Fetching a SAX parser for use with JSONSAXHandler"); } try { /** * Get a parser. */ SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); SAXParser sParser = factory.newSAXParser(); XMLReader parser = sParser.getXMLReader(); JSONSAXHandler jsonHandler = new JSONSAXHandler(JSONStream, verbose); parser.setContentHandler(jsonHandler); parser.setErrorHandler(jsonHandler); InputSource source = new InputSource(new BufferedInputStream(XMLStream)); if (logger.isLoggable(Level.FINEST)) { logger.logp(Level.FINEST, className, "transform", "Parsing the XML content to JSON"); } /** * Parse it. */ source.setEncoding("UTF-8"); parser.parse(source); jsonHandler.flushBuffer(); } catch (javax.xml.parsers.ParserConfigurationException pce) { throw new SAXException("Could not get a parser: " + pce.toString()); } } if (logger.isLoggable(Level.FINER)) { logger.exiting(className, "toJson(InputStream, OutputStream)"); } }
From source file:cz.zcu.kiv.eegdatabase.wui.core.experiments.metadata.ODMLSectionDeserializer.java
@Override public Section deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { long start = System.currentTimeMillis(); try {//from www . java2s .c o m String jsonString = jp.getCodec().readTree(jp).toString(); JSONObject jsonObject = new JSONObject(jsonString); String xmlString = XML.toString(jsonObject); Reader reader = new Reader(); ByteArrayInputStream stream = new ByteArrayInputStream(xmlString.getBytes("UTF-8")); // encoding is necessary DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder dbuilder = dbf.newDocumentBuilder(); InputSource source = new InputSource(stream); source.setEncoding("UTF-8"); // encoding is necessary Document dom = dbuilder.parse(source); Element rootElement = dom.getDocumentElement(); rootElement.setAttribute("version", "1.0"); byte[] xmlStringUpdated = getStringFromDocument(dom).getBytes("UTF-8"); stream = new ByteArrayInputStream(xmlStringUpdated); // encoding is necessary Section load = reader.load(stream); return load; } catch (JSONException e) { log.warn(e.getMessage(), e); } catch (Exception e) { log.warn(e.getMessage(), e); } finally { long end = System.currentTimeMillis(); log.trace("Deserialize time - " + (end - start) + " ms."); } return null; }
From source file:de.uzk.hki.da.metadata.XMPMetadataStructure.java
public XMPMetadataStructure(Path workPath, File metadataFile, List<de.uzk.hki.da.model.Document> documents) throws FileNotFoundException, JDOMException, IOException { super(workPath, metadataFile, documents); logger.debug("Instantiate new xmp metadata structure with metadata file " + metadataFile.getAbsolutePath() + " ... "); xmpFile = metadataFile;// w w w .j ava 2 s . co m currentDocuments = documents; SAXBuilder builder = XMLUtils.createNonvalidatingSaxBuilder(); FileInputStream fileInputStream = new FileInputStream(Path.makeFile(workPath, xmpFile.getPath())); BOMInputStream bomInputStream = new BOMInputStream(fileInputStream); Reader reader = new InputStreamReader(bomInputStream, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); rdfDoc = builder.build(is); descriptionElements = getXMPDescriptionElements(); fileInputStream.close(); bomInputStream.close(); reader.close(); }
From source file:com.predic8.membrane.core.interceptor.cbr.XPathCBRInterceptor.java
private Case findRoute(Request request) throws Exception { for (Case r : cases) { //TODO getBodyAsStream creates ByteArray each call. That could be a performance issue. Using BufferedInputStream did't work, because stream got closed. InputSource is = new InputSource(request.getBodyAsStreamDecoded()); is.setEncoding(request.getCharset()); if ((Boolean) newXPath(namespaces).evaluate(r.getxPath(), is, XPathConstants.BOOLEAN)) return r; log.debug("no match found for xpath {" + r.getxPath() + "}"); }/*from w w w .j a va 2 s .c o m*/ return null; }
From source file:de.uzk.hki.da.metadata.LidoMetadataStructure.java
public LidoMetadataStructure(Path workPath, File metadataFile, List<de.uzk.hki.da.model.Document> documents) throws FileNotFoundException, JDOMException, IOException { super(workPath, metadataFile, documents); lidoFile = metadataFile;// w w w . j a v a 2 s . co m currentDocuments = documents; SAXBuilder builder = XMLUtils.createNonvalidatingSaxBuilder(); FileInputStream fileInputStream = new FileInputStream(Path.makeFile(workPath, metadataFile.getPath())); BOMInputStream bomInputStream = new BOMInputStream(fileInputStream); Reader reader = new InputStreamReader(bomInputStream, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); doc = builder.build(is); lidoParser = new LidoParser(doc); lidoLinkResources = lidoParser.getLidoLinkResources(); fileInputStream.close(); bomInputStream.close(); }
From source file:de.uzk.hki.da.metadata.LidoMetadataStructure.java
/** * Append to each administrativeMetadata in a Lido-File one RightsResourceType-Element and save it. * //from www . j a va2 s .c o m * @param targetLidoFile * @param licenseHref * @param displayLabel * @param text * @throws IOException * @throws JDOMException */ public void appendRightsResource(File targetLidoFile, String licenseHref, String displayLabel) throws IOException, JDOMException { SAXBuilder builder = XMLUtils.createNonvalidatingSaxBuilder(); FileInputStream fileInputStream = new FileInputStream(Path.makeFile(workPath, targetLidoFile.getPath())); BOMInputStream bomInputStream = new BOMInputStream(fileInputStream); Reader reader = new InputStreamReader(bomInputStream, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); Document lidoDoc = builder.build(is); List<Element> lidoElems = lidoDoc.getRootElement().getChildren("lido", C.LIDO_NS); for (int i = 0; i < lidoElems.size(); i++) { appendRightsResourceToLido(lidoElems.get(i), licenseHref, displayLabel); } fileInputStream.close(); bomInputStream.close(); reader.close(); writeDocumentToFile(lidoDoc, Path.makeFile(workPath, targetLidoFile.getPath())); }
From source file:de.uzk.hki.da.metadata.MetsMetadataStructure.java
public MetsMetadataStructure(Path workPath, File metadataFile, List<de.uzk.hki.da.model.Document> documents) throws FileNotFoundException, JDOMException, IOException { super(workPath, metadataFile, documents); metsFile = metadataFile;/*w ww .j a v a2 s . co m*/ currentDocuments = documents; SAXBuilder builder = XMLUtils.createNonvalidatingSaxBuilder(); FileInputStream fileInputStream = new FileInputStream(Path.makeFile(workPath, metsFile.getPath())); BOMInputStream bomInputStream = new BOMInputStream(fileInputStream); Reader reader = new InputStreamReader(bomInputStream, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); metsDoc = builder.build(is); metsParser = new MetsParser(metsDoc); fileElements = metsParser.getFileElementsFromMetsDoc(metsDoc); fileInputStream.close(); bomInputStream.close(); reader.close(); }
From source file:de.uzk.hki.da.metadata.MetsMetadataStructure.java
/** * Append to each dmdSec in a Mets-File one accessCondition-Element and save it. * /*from w w w. ja v a2s.c o m*/ * @param targetMetsFile * @param licenseHref * @param displayLabel * @param text * @throws IOException * @throws JDOMException */ public void appendAccessCondition(File targetMetsFile, String licenseHref, String displayLabel, String text) throws IOException, JDOMException { SAXBuilder builder = XMLUtils.createNonvalidatingSaxBuilder(); FileInputStream fileInputStream = new FileInputStream(Path.makeFile(workPath, targetMetsFile.getPath())); BOMInputStream bomInputStream = new BOMInputStream(fileInputStream); Reader reader = new InputStreamReader(bomInputStream, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); Document metsDoc = builder.build(is); List<Element> dmdSections = metsDoc.getRootElement().getChildren("dmdSec", C.METS_NS); for (int i = 0; i < dmdSections.size(); i++) { Element newAccessConditionE = generateAccessCondition(licenseHref, displayLabel, text); logger.debug("Append to Mets new LicenseElement: " + newAccessConditionE.toString()); Element dmdSecElement = (Element) dmdSections.get(i); Element modsXmlData = MetsParser.getModsXmlData(dmdSecElement); modsXmlData.addContent(newAccessConditionE); } fileInputStream.close(); bomInputStream.close(); reader.close(); writeDocumentToFile(metsDoc, Path.makeFile(workPath, targetMetsFile.getPath())); }
From source file:de.uzk.hki.da.metadata.MetsMetadataStructure.java
public void makeReplacementsHrefInMetsFile(File targetMetsFile, String currentHref, String targetHref, String mimetype, String loctype) throws IOException, JDOMException { SAXBuilder builder = XMLUtils.createNonvalidatingSaxBuilder(); logger.debug(":::" + workPath + ":::" + targetMetsFile.getPath()); FileInputStream fileInputStream = new FileInputStream(Path.makeFile(workPath, targetMetsFile.getPath())); BOMInputStream bomInputStream = new BOMInputStream(fileInputStream); Reader reader = new InputStreamReader(bomInputStream, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); Document metsDoc = builder.build(is); List<Element> metsFileElements = metsParser.getFileElementsFromMetsDoc(metsDoc); for (int i = 0; i < metsFileElements.size(); i++) { Element fileElement = (Element) metsFileElements.get(i); if (metsParser.getHref(fileElement).equals(currentHref)) { setHref(fileElement, targetHref); setMimetype(fileElement, mimetype); setLoctype(fileElement, loctype); }//w w w. j a v a 2s.c om } fileInputStream.close(); bomInputStream.close(); reader.close(); writeDocumentToFile(metsDoc, Path.makeFile(workPath, targetMetsFile.getPath())); }
From source file:org.transdroid.search.RssFeedSearch.PretomeAdapter.java
@Override protected RssParser getRssParser(final String url) { return new RssParser(url) { @Override//from w w w. jav a 2 s . c o m public void parse() throws ParserConfigurationException, SAXException, IOException { HttpClient httpclient = initialise(); HttpResponse result = httpclient.execute(new HttpGet(url)); //FileInputStream urlInputStream = new FileInputStream("/sdcard/rsstest2.txt"); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); InputSource is = new InputSource(); // Pretome supplies UTF-8 compatible character data yet incorrectly defined a windows-1251 encode: override is.setEncoding("UTF-8"); is.setCharacterStream(new InputStreamReader(result.getEntity().getContent())); sp.parse(is, this); } }; }