How to Archive Document in Java project (DMS) - java

i'm developing a DMS.
i'm currently working on the document management system aspect , like Managing PDFs and Docs;
Now I want my application to be able to show all the existing PDF and
DOC files on the computer in my application. so that they can be
opened when the user clicks on them.
i'm currently just focusing on PDFs And Docs

import java.io.File;
import java.util.Collection;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
public class SearchDocFiles {
public static String[] EXTENSIONS = { "doc", "docx" };
public Collection<File> searchFilesWithExtensions(final File directory, final String[] extensions) {
return FileUtils.listFiles(directory,
extensions,
true);
}
public static void main(String... args) {
Collection<File> documents = new SearchDocFiles().searchFilesWithExtensions(
new File("/path/to/document/folder"),
SearchDocFiles.EXTENSIONS);
for (File document: documents) {
System.out.println(document.getName() + " - " + document.length());
}
}
}
this uses Apache Commons IO expectially FileUtil

Related

How do you add attachments from a generic filetype using Apose.Slides using java?

How do you add attachments from a generic filetype using Apose.Slides using java?
The manual PowerPoint operation I’m trying to do programmatically is:
Insert -> Object -> From file
Is this possible with Aspose.Slides insert an Excel file as a link using java?
The below code is working fine for attaching the Excel file using aspose slides
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.aspose.slides.IOleEmbeddedDataInfo;
import com.aspose.slides.IOleObjectFrame;
import com.aspose.slides.OleEmbeddedDataInfo;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
public class SetFileTypeForAnEmbeddingObject2 {
public static void main(String[] args) throws IOException {
Presentation pres = new Presentation();
try {
// Add known Ole objects
byte[] fileBytes = Files.readAllBytes(Paths.get("C:\\work\\Demo uploadt.xlsm"));
// Create Ole embedded file info
IOleEmbeddedDataInfo dataInfo = new OleEmbeddedDataInfo(fileBytes, "xls");
// Create OLE object
IOleObjectFrame oleFrame = pres.getSlides().get_Item(0).getShapes().addOleObjectFrame(150, 420, 250, 50,
dataInfo);
oleFrame.setObjectIcon(true);
pres.save("C:\\work\\" + "SetFileTypeForAnEmbeddingObject7.pptx", SaveFormat.Pptx);
} finally {
if (pres != null)
pres.dispose();
}
}
}

Apache POI PPT SLide Page setup option

I am wondering if by any mean, i can set the 'Slide sized for ' as On-ScreenShow (16:9). i mean is there any method in master object in apache poi hslf? I couldn't find it. I have added the image for the reference.
You can only have one page size per file.
To set the page dimension call SlideShow.setPageSize().
To find out which page dimensions 4:3, 16:9 or any other formats are, just create a PPT manually via Powerpoint and check its dimension - or use a Cross-multiplication:
import java.io.File;
import java.io.IOException;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory;
public class SlideSizes {
public static void main(String[] args) throws IOException {
String files[] = { "dim_4_3.ppt", "dim_16_9.ppt" };
for (String f : files) {
SlideShow<?,?> ppt = SlideShowFactory.create(new File(f));
System.out.println(ppt.getPageSize());
}
}
}

Text Segmentation using Gate

I am trying to write my own program using Java in order to segment set of text files into sentences. I have make a search on the available NLP tools and I found that GATE but i couldn't use it to just segment using the pipeline.
Any ideas how to limit the functionality of the pipeline
Any piece of codes that can help me to write my program
Adapted from a different answer:
import gate.*;
import gate.creole.SerialAnalyserController;
import java.io.File;
import java.util.*;
public class Segmenter {
public static void main(String[] args) throws Exception {
Gate.setGateHome(new File("C:\\Program Files\\GATE_Developer_8.0"));
Gate.init();
regiterGatePlugin("ANNIE");
SerialAnalyserController pipeline = (SerialAnalyserController) Factory.createResource("gate.creole.SerialAnalyserController");
pipeline.add((ProcessingResource) Factory.createResource("gate.creole.tokeniser.DefaultTokeniser"));
pipeline.add((ProcessingResource) Factory.createResource("gate.creole.splitter.SentenceSplitter"));
Corpus corpus = Factory.newCorpus("SegmenterCorpus");
Document document = Factory.newDocument("Text to be segmented.");
corpus.add(document);
pipeline.setCorpus(corpus);
pipeline.execute();
AnnotationSet defaultAS = document.getAnnotations();
AnnotationSet sentences = defaultAS.get("Sentence");
for (Annotation sentence : sentences) {
System.err.println(Utils.stringFor(document, sentence));
}
//Clean up
Factory.deleteResource(document);
Factory.deleteResource(corpus);
for (ProcessingResource pr : pipeline.getPRs()) {
Factory.deleteResource(pr);
}
Factory.deleteResource(pipeline);
}
public static void regiterGatePlugin(String name) throws Exception {
Gate.getCreoleRegister().registerDirectories(new File(Gate.getPluginsHome(), name).toURI().toURL());
}
}

How to copy-paste, and cut-paste file or folder in java?

I made a desktop app in java with netbeans platform. In my app I want to give separate copy-paste and cut-paste option of file or folder.
So how can I do that? I tried Files.copy(new File("D:\\Pndat").toPath(),new File("D:\\212").toPath(), REPLACE_EXISTING);. But I don't get the exact output.
If there any other option then suggest me.
In case of "cut-paste" you can use renameTo() like this:
File source = new File("////////Source path");
File destination = new File("//////////destination path");
if (!destination.exists()) {
source.renameTo(destination);
}
In case of "copy-paste" you need to read in Input and Output stream.
Use FileUtils from apache io and do FileUtils.copyDirectory(sourceDir, destDir);
You can also do the following file operations
writing to a file
reading from a file
make a directory including parent directories
copying files and directories
deleting files and directories
converting to and from a URL
listing files and directories by filter and extension
comparing file content
file last changed date
Download link for apache i/o jar.
I think this question relates to using the system clipboard for copying a file specified in a Java app and using the OS "Paste" function to copy the file to a folder. Here is a short instructional example that will show you how to add a single file to the OS clipboard for later doing an OS "Paste" function. Tweak as necessary and add error/exception checking as needed.
As a secondary, this code also places the file name on the clipboard so you can paste the file name into document editors.
package com.example.charles.clipboard;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
public class JavaToSystemClipboard {
public static void main(final String[] args) throws Exception {
final File fileOut = new File("someFileThatExists");
putFileToSystemClipboard(fileOut);
}
public static void putFileToSystemClipboard(final File fileOut) throws Exception {
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
final ClipboardOwner clipboardOwner = null;
final Transferable transferable = new Transferable() {
public boolean isDataFlavorSupported(final DataFlavor flavor) {
return false;
}
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.javaFileListFlavor, DataFlavor.stringFlavor };
}
public Object getTransferData(final DataFlavor flavor) {
if (flavor.equals(DataFlavor.javaFileListFlavor)) {
final List<String> list = new ArrayList<>();
list.add(fileOut.getAbsolutePath());
return list;
}
if (flavor.equals(DataFlavor.stringFlavor)) {
return fileOut.getAbsolutePath();
}
return null;
}
};
clipboard.setContents(transferable, clipboardOwner);
}
}
You can write things by yourself using FileOutputStream and FileInputStream or you can used Apache Camel.

How to read string from .properties file in junit-tests?

I use wicket in my webapplication. I save the Strings in some .properties files as follows:
foo.properties
page.label=dummy
In the html-file, I can acces the String page.label as follows:
index.html
<wicket:message key="page.label">Default label</wicket:message>
Now I wrote some junit test cases for my Application and would like to access the Strings saved in the properties file. My Question is, how to read the String from the properties file?
Try this
import java.io.FileInputStream;
import java.util.Properties;
public class MainClass {
public static void main(String args[]) throws IOException {
Properties p = new Properties();
p.load(new FileInputStream("foo.properties"));
Object label = p.get("page.label");
System.out.println(label);
}
}
This section allow you to read all properties files from wherever you want and load them in the Properties
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class MainClass {
private static String PROPERTIES_FILES_PATHNAME = "file:///Users/ftam/Downloads/test/";// for mac
public static void main(String args[]) throws Exception {
Properties p = new Properties();
List<File> files = getFiles();
for(File file : files) {
FileInputStream input = new FileInputStream(file);
p.load(input);
}
String label = (String) p.get("page.label");
System.out.println(label);
}
private static List<File> getFiles() throws IOException, URISyntaxException {
List<File> filesList = new ArrayList<File>();
URL[] url = { new URL(PROPERTIES_FILES_PATHNAME) };
URLClassLoader loader = new URLClassLoader(url);
URL[] urls = loader.getURLs();
File fileMetaInf = new File(urls[0].toURI());
File[] files = fileMetaInf.listFiles();
for(File file : files) {
if(!file.isDirectory() && file.getName().endsWith(".properties")) {
filesList.add(file);
}
}
return filesList;
}
}
Wicket has its own way of localizing the resource, taking into account the component tree. See the javadoc for the StringResourceLoader.
One way of loading the Resource would be:
WicketTester tester = new WicketTester(new MyApplication());
tester.startPage(MyPage.class);
Localizer localizer = tester.getApplication().getResourceSettings()
.getLocalizer();
String foo = localizer.getString("page.label",tester.getLastRenderedPage(), "")
Using Apache Commons Configuration is a pretty good choice!
You can use load and then get("page.label")
Have this field inside your class:
import java.util.ResourceBundle;
private static ResourceBundle settings = ResourceBundle.getBundle("test",Locale.getDefault());
then a test.properties file like this:
com.some.name=someValueHere
Finally you can access the property values this way:
private String fieldName = settings.getString("com.some.name");

Categories