List of usage examples for org.apache.commons.io Document getId
public int getId()
From source file:com.github.stagirs.common.model.DocumentSerializer.java
public static void serialize(File file, Document document) throws IOException { StringBuilder sb = new StringBuilder(); serialize(sb, document);/*from w ww. j a v a2 s . c o m*/ FileUtils.writeStringToFile(new File(file, document.getId()), sb.toString(), "utf-8"); }
From source file:com.github.stagirs.common.model.DocumentParser.java
public static Document parse(File file) throws IOException { Document doc = new Document(); doc.setId(file.getName());//from w w w . j ava 2 s . com Iterator<String> list = split(FileUtils.readFileToString(file, "utf-8")).iterator(); while (list.hasNext()) { String item = list.next(); if (item.equals("<div class='section'>")) { doc.getBlocks().add(parseSection(doc.getId(), item, list)); continue; } if (item.startsWith("<p")) { doc.getBlocks().add(parsePoint(doc.getId(), item, list)); continue; } if (item.startsWith("<div class='title' ")) { item = item.split("semantic='")[1]; doc.setTitleSemantic(Double.parseDouble(item.substring(0, item.indexOf("'")))); doc.setTitle(list.next()); list.next(); continue; } if (item.equals("<div class='author'>")) { doc.setAuthor(list.next()); list.next(); continue; } if (item.equals("<div class='classifier'>")) { doc.setClassifier(list.next()); list.next(); continue; } if (item.equals("<div class='thanks'>")) { doc.setThanks(list.next()); list.next(); continue; } if (item.equals("<div class='output'>")) { doc.setOutput(list.next()); list.next(); continue; } } return doc; }
From source file:com.couchbase.lite.LiteTestCaseWithDB.java
public static Document createDocumentWithProperties(Database db, Map<String, Object> properties) { Document doc = db.createDocument(); Assert.assertNotNull(doc);/*from w ww . j ava2 s . co m*/ Assert.assertNull(doc.getCurrentRevisionId()); Assert.assertNull(doc.getCurrentRevision()); Assert.assertNotNull("Document has no ID", doc.getId()); // 'untitled' docs are no longer untitled (8/10/12) try { doc.putProperties(properties); } catch (Exception e) { Log.e(TAG, "Error creating document", e); assertTrue( "can't create new document in db:" + db.getName() + " with properties:" + properties.toString(), false); } Assert.assertNotNull(doc.getId()); Assert.assertNotNull(doc.getCurrentRevisionId()); Assert.assertNotNull(doc.getUserProperties()); // should be same doc instance, since there should only ever be a single Document // instance for a given document Assert.assertEquals(db.getDocument(doc.getId()), doc); Assert.assertEquals(db.getDocument(doc.getId()).getId(), doc.getId()); return doc; }
From source file:com.couchbase.lite.AttachmentsTest.java
/** * attempt to reproduce https://github.com/couchbase/couchbase-lite-android/issues/328 & * https://github.com/couchbase/couchbase-lite-android/issues/325 *///w w w . ja va2s . c o m public void testSetAttachmentsSequentially() throws CouchbaseLiteException, IOException { try { //Create rev1 of document with just properties Document doc = database.createDocument(); String id = doc.getId(); Map<String, Object> docProperties = new HashMap<String, Object>(); docProperties.put("Iteration", 0); doc.putProperties(docProperties); UnsavedRevision rev = null; //Create a new revision with attachment1 InputStream attachmentStream1 = getAsset("attachment.png"); doc = database.getDocument(id);//not required byte[] jsonb = Manager.getObjectMapper().writeValueAsBytes(doc.getProperties().get("_attachments")); Log.d(Database.TAG, "Doc _rev = %s", doc.getProperties().get("_rev")); Log.d(Database.TAG, "Doc properties = %s", new String(jsonb)); rev = doc.createRevision(); rev.setAttachment("attachment1", "image/png", attachmentStream1); rev.save(); attachmentStream1.close(); //Create a new revision updated properties doc = database.getDocument(id);//not required jsonb = Manager.getObjectMapper().writeValueAsBytes(doc.getProperties().get("_attachments")); Log.d(Database.TAG, "Doc _rev = %s", doc.getProperties().get("_rev")); Log.d(Database.TAG, "Doc properties = %s", new String(jsonb)); Map<String, Object> curProperties; curProperties = doc.getProperties(); docProperties = new HashMap<String, Object>(); docProperties.putAll(curProperties); docProperties.put("Iteration", 1); doc.putProperties(docProperties); //Create a new revision with attachment2 InputStream attachmentStream2 = getAsset("attachment.png"); doc = database.getDocument(id);//not required jsonb = Manager.getObjectMapper().writeValueAsBytes(doc.getProperties().get("_attachments")); Log.d(Database.TAG, "Doc _rev = %s", doc.getProperties().get("_rev")); Log.d(Database.TAG, "Doc properties = %s", new String(jsonb)); rev = doc.createRevision(); rev.setAttachment("attachment2", "image/png", attachmentStream2); rev.save(); attachmentStream2.close(); //Assert final document revision doc = database.getDocument(id); curProperties = doc.getProperties(); assertEquals(4, curProperties.size()); Map<String, Object> attachments = (Map<String, Object>) doc.getCurrentRevision() .getProperty("_attachments"); assertNotNull(attachments); assertEquals(2, attachments.size()); } catch (CouchbaseLiteException e) { Log.e(Database.TAG, "Error adding attachment: " + e.getMessage(), e); fail(); } }
From source file:com.couchbase.lite.AttachmentsTest.java
/** * attempt to reproduce https://github.com/couchbase/couchbase-lite-android/issues/328 & * https://github.com/couchbase/couchbase-lite-android/issues/325 *//* w w w .j a v a2s . co m*/ public void failingTestSetAttachmentsSequentiallyInTransaction() throws CouchbaseLiteException, IOException { boolean success = database.runInTransaction(new TransactionalTask() { public boolean run() { try { // add a doc with an attachment Document doc = database.createDocument(); String id = doc.getId(); InputStream jsonStream = getAsset("300k.json"); Map<String, Object> docProperties = null; docProperties = Manager.getObjectMapper().readValue(jsonStream, Map.class); docProperties.put("Iteration", 0); doc.putProperties(docProperties); jsonStream.close(); UnsavedRevision rev = null; for (int i = 0; i < 20; i++) { InputStream attachmentStream1 = getAsset("attachment.png"); Log.e(Database.TAG, "TEST ITERATION " + i); doc = database.getDocument(id);//not required rev = doc.createRevision(); rev.setAttachment("attachment " + i * 5, "image/png", attachmentStream1); rev.save(); attachmentStream1.close(); InputStream attachmentStream2 = getAsset("attachment.png"); doc = database.getDocument(id);//not required rev = doc.createRevision(); rev.setAttachment("attachment " + i * 5 + 1, "image/png", attachmentStream2); rev.save(); attachmentStream2.close(); InputStream attachmentStream3 = getAsset("attachment.png"); doc = database.getDocument(id);//not required rev = doc.createRevision(); rev.setAttachment("attachment " + i * 5 + 2, "image/png", attachmentStream3); rev.save(); attachmentStream3.close(); InputStream attachmentStream4 = getAsset("attachment.png"); doc = database.getDocument(id);//not required rev = doc.createRevision(); rev.setAttachment("attachment " + i * 5 + 3, "image/png", attachmentStream4); rev.save(); attachmentStream4.close(); InputStream attachmentStream5 = getAsset("attachment.png"); doc = database.getDocument(id);//not required rev = doc.createRevision(); rev.setAttachment("attachment " + i * 5 + 4, "image/png", attachmentStream5); rev.save(); attachmentStream5.close(); Map<String, Object> curProperties; doc = database.getDocument(id);//not required curProperties = doc.getProperties(); docProperties = new HashMap<String, Object>(); docProperties.putAll(curProperties); docProperties.put("Iteration", (i + 1) * 5); doc.putProperties(docProperties); } Map<String, Object> curProperties; doc = database.getDocument(id);//not required curProperties = doc.getProperties(); assertEquals(22, curProperties.size()); Map<String, Object> attachments = (Map<String, Object>) doc.getCurrentRevision() .getProperty("_attachments"); assertNotNull(attachments); assertEquals(100, attachments.size()); } catch (Exception e) { Log.e(Database.TAG, "Error deserializing properties from JSON", e); return false; } return true; } }); assertTrue("transaction with set attachments sequentially failed", success); }
From source file:ch.silviowangler.dox.DocumentServiceImpl.java
@Override @Transactional(propagation = SUPPORTS, readOnly = true) @PreAuthorize("hasRole('ROLE_USER')") public Set<DocumentReference> findDocumentReferences(Map<TranslatableKey, DescriptiveIndex> queryParams, String documentClassShortName) throws DocumentClassNotFoundException { logger.debug("Trying to find document references in document class '{}' using params '{}'", documentClassShortName, queryParams); ch.silviowangler.dox.domain.DocumentClass documentClass = findDocumentClass(documentClassShortName); List<Attribute> attributes = attributeRepository.findAttributesForDocumentClass(documentClass); List<Document> documents = documentRepository.findDocuments( toEntityMap(fixDataTypesOfIndices(queryParams, attributes)), toAttributeMap(attributes), documentClass);// w w w . ja va2s . c o m HashSet<DocumentReference> documentReferences = new HashSet<>(documents.size()); for (Document document : documents) { logger.trace("Found document with id {}", document.getId()); documentReferences.add(toDocumentReference(document, null)); } return documentReferences; }
From source file:ch.silviowangler.dox.DocumentServiceImpl.java
private List<DocumentReference> findDocumentReferencesInternal(String queryString, String username, Locale locale) {/*from w w w . ja va 2 s .com*/ logger.debug("About to find document references for query string '{}'", queryString); List<Document> documents; List<String> clients = getClients(); if (username == null) { if (containsWildcardCharacters(queryString)) { String value = replaceWildcardCharacters(queryString); documents = indexMapEntryRepository.findByValueLike(value.toUpperCase(), value, clients); } else { documents = indexMapEntryRepository.findByValue(queryString.toUpperCase(), queryString, clients); } } else { if (containsWildcardCharacters(queryString)) { String value = replaceWildcardCharacters(queryString); documents = indexMapEntryRepository.findByValueLikeAndUserReference(value.toUpperCase(), value, username, clients); } else { documents = indexMapEntryRepository.findByValueAndUserReference(queryString.toUpperCase(), queryString, username, clients); } } List<DocumentReference> documentReferences = newArrayListWithCapacity(documents.size()); logger.info("Found {} documents for query string '{}'", documents.size(), queryString); for (Document document : documents) { logger.trace("Found document with id {}", document.getId()); documentReferences.add(toDocumentReference(document, locale)); } return documentReferences; }
From source file:ch.silviowangler.dox.DocumentServiceImpl.java
private DocumentReference toDocumentReference(Document document, Locale locale) { final DocumentReference documentReference = new DocumentReference(document.getHash(), document.getId(), document.getPageCount(), document.getMimeType(), toDocumentClassApi(document.getDocumentClass()), toIndexMap(document.getIndexStore(), attributeRepository.findAttributesForDocumentClass(document.getDocumentClass()), locale), document.getOriginalFilename(), document.getUserReference(), document.getFileSize()); documentReference.setCreationDate(document.getCreationDate()); documentReference.setClient(document.getClient().getShortName()); return documentReference; }
From source file:com.couchbase.lite.DatabaseAttachmentTest.java
/** * attempt to reproduce https://github.com/couchbase/couchbase-lite-android/issues/328 & * https://github.com/couchbase/couchbase-lite-android/issues/325 *///from ww w . j a va 2s . co m public void testSetAttachmentsSequentially() throws CouchbaseLiteException, IOException { try { //Create rev1 of document with just properties Document doc = database.createDocument(); String id = doc.getId(); Map<String, Object> docProperties = new HashMap<String, Object>(); docProperties.put("Iteration", 0); doc.putProperties(docProperties); UnsavedRevision rev = null; //Create a new revision with attachment1 InputStream attachmentStream1 = getAsset("attachment.png"); doc = database.getDocument(id);//not required byte[] jsonb = Manager.getObjectMapper().writeValueAsBytes(doc.getProperties().get("_attachments")); Log.d(Database.TAG, "Doc _rev = %s", doc.getProperties().get("_rev")); Log.d(Database.TAG, "Doc properties = %s", new String(jsonb)); rev = doc.createRevision(); rev.setAttachment("attachment1", "image/png", attachmentStream1); rev.save(); attachmentStream1.close(); //Create a new revision updated properties doc = database.getDocument(id);//not required jsonb = Manager.getObjectMapper().writeValueAsBytes(doc.getProperties().get("_attachments")); Log.d(Database.TAG, "Doc _rev = %s", doc.getProperties().get("_rev")); Log.d(Database.TAG, "Doc properties = %s", new String(jsonb)); Map<String, Object> curProperties; curProperties = doc.getProperties(); docProperties = new HashMap<String, Object>(); docProperties.putAll(curProperties); docProperties.put("Iteration", 1); doc.putProperties(docProperties); //Create a new revision with attachment2 InputStream attachmentStream2 = getAsset("attachment.png"); doc = database.getDocument(id);//not required jsonb = Manager.getObjectMapper().writeValueAsBytes(doc.getProperties().get("_attachments")); Log.d(Database.TAG, "Doc _rev = %s", doc.getProperties().get("_rev")); Log.d(Database.TAG, "Doc properties = %s", new String(jsonb)); rev = doc.createRevision(); rev.setAttachment("attachment2", "image/png", attachmentStream2); rev.save(); attachmentStream2.close(); //Assert final document revision doc = database.getDocument(id); curProperties = doc.getProperties(); assertEquals(4, curProperties.size()); Map<String, Object> attachments = (Map<String, Object>) doc.getCurrentRevision() .getProperty("_attachments"); assertNotNull(attachments); assertEquals(2, attachments.size()); } catch (CouchbaseLiteException e) { Log.e(Database.TAG, "Error adding attachment: " + e.getMessage(), e); fail(); } }
From source file:com.couchbase.lite.DatabaseAttachmentTest.java
public void testGzippedAttachmentByBase64() throws Exception { String attachmentName = "attachment.png"; // 1. store attachment with doc // 1.a load attachment data from asset InputStream attachmentStream = getAsset(attachmentName); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(attachmentStream, baos); baos.close();//from ww w .ja v a 2 s . c om attachmentStream.close(); byte[] bytes = baos.toByteArray(); // 1.b apply GZIP + Base64 String attachmentBase64 = Base64.encodeBytes(bytes, Base64.GZIP); // 1.c attachment Map object Map<String, Object> attachmentMap = new HashMap<String, Object>(); attachmentMap.put("content_type", "image/png"); attachmentMap.put("data", attachmentBase64); attachmentMap.put("encoding", "gzip"); attachmentMap.put("length", bytes.length); // 1.d attachments Map object Map<String, Object> attachmentsMap = new HashMap<String, Object>(); attachmentsMap.put(attachmentName, attachmentMap); // 1.e document property Map object Map<String, Object> propsMap = new HashMap<String, Object>(); propsMap.put("_attachments", attachmentsMap); // 1.f store document into database Document putDoc = database.createDocument(); putDoc.putProperties(propsMap); String docId = putDoc.getId(); // 2. Load attachment from database and compare it with original // 2.a load doc and attachment from database Document getDoc = database.getDocument(docId); Attachment attachment = getDoc.getCurrentRevision().getAttachment(attachmentName); assertEquals(bytes.length, attachment.getLength()); assertEquals("image/png", attachment.getContentType()); assertEquals("gzip", attachment.getMetadata().get("encoding")); InputStream is = attachment.getContent(); byte[] receivedBytes = getBytesFromInputStream(is); assertEquals(bytes.length, receivedBytes.length); is.close(); assertTrue(Arrays.equals(bytes, receivedBytes)); }