@Override public String getEncoding() { return delegatee.getEncoding(); }
@Override public String getEncoding() { return delegatee.getEncoding(); }
public Reader getReader(String encoding) throws IOException { inputStream = conn.getContentInputStream(); return new InputStreamReader(inputStream, conn.getEncoding()); }
public Reader getReader(String encoding) throws IOException { inputStream = conn.getContentInputStream(); return new InputStreamReader(inputStream, conn.getEncoding()); }
/** * Populates a file with the content in the reader, but also converts the encoding to UTF-8. */ private void saveContentInUtf8File(ContentReader reader, File file) { String encoding = reader.getEncoding(); try { Reader in = new InputStreamReader(reader.getContentInputStream(), encoding); Writer out = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file)), "UTF-8"); FileCopyUtils.copy(in, out); // both streams are closed } catch (IOException e) { throw new ContentIOException("Failed to copy content to file and convert "+encoding+" to UTF-8: \n" + " file: " + file, e); } } }
/** * Populates a file with the content in the reader, but also converts the encoding to UTF-8. */ private void saveContentInUtf8File(ContentReader reader, File file) { String encoding = reader.getEncoding(); try { Reader in = new InputStreamReader(reader.getContentInputStream(), encoding); Writer out = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file)), "UTF-8"); FileCopyUtils.copy(in, out); // both streams are closed } catch (IOException e) { throw new ContentIOException("Failed to copy content to file and convert "+encoding+" to UTF-8: \n" + " file: " + file, e); } } }
/** * Populates a file with the content in the reader. */ public void saveContentInFile(String sourceMimetype, ContentReader reader, File file) throws ContentIOException { String encoding = reader.getEncoding(); if (encodeAsUtf8(sourceMimetype, encoding)) { saveContentInUtf8File(reader, file); } else { reader.getContent(file); } }
/** * Populates a file with the content in the reader. */ public void saveContentInFile(String sourceMimetype, ContentReader reader, File file) throws ContentIOException { String encoding = reader.getEncoding(); if (encodeAsUtf8(sourceMimetype, encoding)) { saveContentInUtf8File(reader, file); } else { reader.getContent(file); } }
@Override public ResourceBundle doWork() throws Exception { NodeRef rootNode = nodeService.getRootNode(storeRef); // first attempt - with locale NodeRef nodeRef = getNode(rootNode, path+"_"+locale+PROPERTIES_FILE_SUFFIX); if (nodeRef == null) { // second attempt - basename nodeRef = getNode(rootNode, path+PROPERTIES_FILE_SUFFIX); } if (nodeRef == null) { logger.debug("Could not find message resource bundle " + storeRef + "/" + path); return null; } ContentReader cr = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT); ResourceBundle resBundle = new MessagePropertyResourceBundle( new InputStreamReader(cr.getContentInputStream(), cr.getEncoding())); return resBundle; } };
@Override public ResourceBundle doWork() throws Exception { NodeRef rootNode = nodeService.getRootNode(storeRef); // first attempt - with locale NodeRef nodeRef = getNode(rootNode, path+"_"+locale+PROPERTIES_FILE_SUFFIX); if (nodeRef == null) { // second attempt - basename nodeRef = getNode(rootNode, path+PROPERTIES_FILE_SUFFIX); } if (nodeRef == null) { logger.debug("Could not find message resource bundle " + storeRef + "/" + path); return null; } ContentReader cr = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT); ResourceBundle resBundle = new MessagePropertyResourceBundle( new InputStreamReader(cr.getContentInputStream(), cr.getEncoding())); return resBundle; } };
public void transformInternal(ContentReader reader, ContentWriter writer, TransformationOptions options) throws Exception { // We can only work from a file File htmlFile = TempFileProvider.createTempFile("HtmlParserContentTransformer_", ".html"); reader.getContent(htmlFile); // Fetch the encoding of the HTML, as set in the ContentReader // This will default to 'UTF-8' if not specifically set String encoding = reader.getEncoding(); // Create the extractor EncodingAwareStringBean extractor = new EncodingAwareStringBean(); extractor.setCollapse(false); extractor.setLinks(false); extractor.setReplaceNonBreakingSpaces(false); extractor.setURL(htmlFile, encoding); // get the text String text = extractor.getStrings(); // write it to the writer writer.putContent(text); // Tidy up htmlFile.delete(); } }
public void transformInternal(ContentReader reader, ContentWriter writer, TransformationOptions options) throws Exception { // We can only work from a file File htmlFile = TempFileProvider.createTempFile("HtmlParserContentTransformer_", ".html"); reader.getContent(htmlFile); // Fetch the encoding of the HTML, as set in the ContentReader // This will default to 'UTF-8' if not specifically set String encoding = reader.getEncoding(); // Create the extractor EncodingAwareStringBean extractor = new EncodingAwareStringBean(); extractor.setCollapse(false); extractor.setLinks(false); extractor.setReplaceNonBreakingSpaces(false); extractor.setURL(htmlFile, encoding); // get the text String text = extractor.getStrings(); // write it to the writer writer.putContent(text); // Tidy up htmlFile.delete(); } }
public void testDITAFileWithoutDoctype() throws Exception { // Munge the file to skip the doctype ContentReader reader = getReader(FILE_DITA_FILE); assertTrue(reader.exists()); String contents = reader.getContentString(); String noDocType = contents.replaceAll("<!DOCTYPE.*?>", ""); File tmp = File.createTempFile("alfresco", ".xml"); tmp.deleteOnExit(); ContentWriter w = new FileContentWriter(tmp); w.setEncoding(reader.getEncoding()); w.setMimetype(reader.getMimetype()); w.putContent(noDocType); // Now test extraction doTestDITAFile(w.getReader()); } private void doTestDITAFile(ContentReader reader) throws Exception
@Override public Void execute() throws Throwable { // Now validate that the target node exists and has similar properties to the source NodeRef destNodeRef = testNodeFactory.getMappedNodeRef(testContext.contentNodeRef); ContentReader reader = contentService.getReader(destNodeRef, ContentModel.PROP_CONTENT); assertNotNull("content reader is null", reader); assertTrue("content encoding is wrong", reader.getEncoding().equalsIgnoreCase(CONTENT_ENCODING)); assertEquals("content locale is wrong", reader.getLocale(), CONTENT_LOCALE); assertTrue("content does not exist", reader.exists()); String contentStr = reader.getContentString(); assertEquals("Content is wrong", contentStr, testContext.contentString); return null; } };
public void testAlf6560MimetypeSetting() throws Exception { FileInfo fileInfo = fileFolderService.create(workingRootNodeRef, "Something.html", ContentModel.TYPE_CONTENT); NodeRef fileNodeRef = fileInfo.getNodeRef(); // Write the content but without setting the mimetype ContentWriter writer = fileFolderService.getWriter(fileNodeRef); writer.putContent("CONTENT"); ContentReader reader = fileFolderService.getReader(fileNodeRef); assertEquals("Mimetype was not automatically set", MimetypeMap.MIMETYPE_HTML, reader.getMimetype()); // Now ask for encoding too writer = fileFolderService.getWriter(fileNodeRef); writer.guessEncoding(); OutputStream out = writer.getContentOutputStream(); out.write( "<html><body>hall\u00e5 v\u00e4rlden</body></html>".getBytes("UnicodeBig") ); out.close(); reader = fileFolderService.getReader(fileNodeRef); assertEquals("Mimetype was not automatically set", MimetypeMap.MIMETYPE_HTML, reader.getMimetype()); assertEquals("Encoding was not automatically set", "UTF-16BE", reader.getEncoding()); }
@Override public Void execute() throws Throwable { NodeRef destinationNodeRef = testNodeFactory.getMappedNodeRef(testContext.contentNodeRef); assertEquals("test error destinationNodeRef not correct", testContext.savedDestinationNodeRef, destinationNodeRef); ContentReader reader = contentService.getReader(destinationNodeRef, ContentModel.PROP_CONTENT); assertNotNull("content reader is null", reader); assertTrue("content encoding is wrong", reader.getEncoding().equalsIgnoreCase(CONTENT_ENCODING)); assertEquals("content locale is wrong", reader.getLocale(), CONTENT_LOCALE); assertTrue("content does not exist", reader.exists()); String contentStr = reader.getContentString(); assertEquals("Content is wrong", contentStr, CONTENT_STRING); return null; } };
writer.setEncoding(reader.getEncoding()); // original encoding
@Test public void testMimetypAndEncodingAndLocale() throws Exception { ContentWriter writer = getWriter(); // set mimetype and encoding writer.setMimetype("text/plain"); writer.setEncoding("UTF-16"); writer.setLocale(Locale.CHINESE); // create a UTF-16 string String content = "A little bit o' this and a little bit o' that"; byte[] bytesUtf16 = content.getBytes("UTF-16"); // write the bytes directly to the writer OutputStream os = writer.getContentOutputStream(); os.write(bytesUtf16); os.close(); // now get a reader from the writer ContentReader reader = writer.getReader(); assertEquals("Writer -> Reader content URL mismatch", writer.getContentUrl(), reader.getContentUrl()); assertEquals("Writer -> Reader mimetype mismatch", writer.getMimetype(), reader.getMimetype()); assertEquals("Writer -> Reader encoding mismatch", writer.getEncoding(), reader.getEncoding()); assertEquals("Writer -> Reader locale mismatch", writer.getLocale(), reader.getLocale()); // now get the string directly from the reader String contentCheck = reader.getContentString(); // internally it should have taken care of the encoding assertEquals("Encoding and decoding of strings failed", content, contentCheck); }
assertNotNull("Content URL should not be null", reader.getContentUrl()); assertNotNull("Content mimetype should not be null", reader.getMimetype()); assertNotNull("Content encoding should not be null", reader.getEncoding()); assertNotNull("Content locale should not be null", reader.getLocale()); long length = SOME_CONTENT.getBytes(reader.getEncoding()).length; // ensures correct decoding long checkLength = reader.getSize(); assertEquals("Content length incorrect", length, checkLength);
/** * Check that a valid writer into the content store can be retrieved and used. */ public void testSimpleNonTempWriter() throws Exception { ContentWriter writer = contentService.getWriter(null, null, false); assertNotNull("Writer should not be null", writer); assertNotNull("Content URL should not be null", writer.getContentUrl()); // write some content writer.putContent(SOME_CONTENT); writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN); writer.setEncoding("UTF-16"); writer.setLocale(Locale.CHINESE); // set the content property manually nodeService.setProperty(contentNodeRef, ContentModel.PROP_CONTENT, writer.getContentData()); // get the reader ContentReader reader = contentService.getReader(contentNodeRef, ContentModel.PROP_CONTENT); assertNotNull("Reader should not be null", reader); assertNotNull("Content URL should not be null", reader.getContentUrl()); assertEquals("Content Encoding was not set", "UTF-16", reader.getEncoding()); assertEquals("Content Locale was not set", Locale.CHINESE, reader.getLocale()); }