@SuppressWarnings("unchecked") private String getCharsetFromBase(Base base) { if (base == null) return null; if (base instanceof Document) { return ((Document)base).getCharset(); } else if (base instanceof Element) { return getCharsetFromBase(((Element)base).getDocument()); } return null; }
/** * Sets the content for this entry */ public Content setContent(InputStream in, String mediatype) { if (MimeTypeHelper.isText(mediatype)) { try { StringBuilder buf = new StringBuilder(); String charset = MimeTypeHelper.getCharset(mediatype); Document doc = this.getDocument(); charset = charset != null ? charset : doc != null ? doc.getCharset() : null; charset = charset != null ? charset : "UTF-8"; InputStreamReader isr = new InputStreamReader(in, charset); char[] data = new char[500]; int r = -1; while ((r = isr.read(data)) != -1) { buf.append(data, 0, r); } return setContent(buf.toString(), mediatype); } catch (IOException e) { throw new RuntimeException(e); } } else { InputStreamDataSource ds = new InputStreamDataSource(in, mediatype); DataHandler dh = new DataHandler(ds); return setContent(dh, mediatype); } }
public void writeTo(OutputStream out) throws IOException { Document doc = getDocument(); String charset = doc != null ? doc.getCharset() : "UTF-8"; Writer writer = this.getFactory().getAbdera().getWriter(); writeTo(writer, new OutputStreamWriter(out, charset)); }
/** * Attempt to guess the base direction using the charset encoding. This is a bit of a last resort approach */ @SuppressWarnings("unchecked") public static <T extends Element> Direction guessDirectionFromEncoding(T element, boolean ignoredir) { if (!ignoredir && hasDirection(element)) return getDirection(element); Document doc = element.getDocument(); if (doc == null) return Direction.UNSPECIFIED; return Bidi.guessDirectionFromEncoding(doc.getCharset()); }
public void writeTo(Base base, WritableByteChannel out, WriterOptions options) throws IOException { String charset = options.getCharset(); if (charset == null) { Document doc = null; if (base instanceof Document) doc = (Document)base; else if (base instanceof Element) { doc = ((Element)base).getDocument(); } charset = doc != null ? doc.getCharset() : null; } writeTo(base, Channels.newWriter(out, charset != null ? charset : "utf-8"), options); }
@SuppressWarnings("unchecked") public void writeTo(Base base, OutputStream out, WriterOptions options) throws IOException { out = getCompressedOutputStream(out, options); String charset = options.getCharset(); if (charset == null) { if (base instanceof Document) charset = ((Document)base).getCharset(); else if (base instanceof Element) { Document doc = ((Element)base).getDocument(); if (doc != null) charset = doc.getCharset(); } if (charset == null) charset = "UTF-8"; } else { Document doc = null; if (base instanceof Document) doc = (Document)base; else if (base instanceof Element) doc = ((Element)base).getDocument(); if (doc != null) doc.setCharset(charset); } base.writeTo(new OutputStreamWriter(out, charset)); finishCompressedOutputStream(out, options); if (options.getAutoClose()) out.close(); }
public void writeTo( Base base, WritableByteChannel out, WriterOptions options) throws IOException { String charset = options.getCharset(); if (charset == null) { Document doc = null; if (base instanceof Document) doc = (Document) base; else if (base instanceof Element) { doc = ((Element)base).getDocument(); } charset = doc != null ? doc.getCharset() : null; } writeTo( base, Channels.newWriter( out, charset != null ? charset : "utf-8"), options); }
public void writeTo(java.io.Writer writer) throws IOException { Writer out = getFactory().getAbdera().getWriter(); if (!(out instanceof FOMWriter)) { out.writeTo(this, writer); } else { try { OMOutputFormat outputFormat = new OMOutputFormat(); if (getDocument() != null && getDocument().getCharset() != null) outputFormat.setCharSetEncoding(getDocument().getCharset()); serialize(writer, outputFormat); } catch (XMLStreamException e) { throw new FOMException(e); } } }