MusicXML files in java swing - visual representation and dynamic editing - java

I'm developing a desktop application with java swing which should be able to display the visual content (notes, clefs, measures) defined in a MusicXML file in the frame. All .xml parsers that I found allow me to only create trees. I couldn't display the content with the JEditorPane, too.
Can I do it or will I need to first transform it dynamically to some other format such as .pdf? If so - how can I do it in java?

Use a JTextArea to let the user edit raw XML. Load the file in the background using SwingWorker, as shown here, to mitigate the effect of any latency.
#Override
protected Integer doInBackground() {
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("build.xml"));
String s;
try {
while ((s = in.readLine()) != null) {
publish(s);
}
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace(System.err);
} finally {
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
}
return status;
}
By visual representation, I meant that I needed a way to display this type of visual output.
You might look at the JMusicXML project, which has "some Java awt/swing graphics player." More resources may be found here.

Related

Is there any criteria to judge a website is blank and has no content using Java?

My problem is as follows. Currently I`m doing a web crawling project for my final year. I want to crawl down web pages carrying out .org domains and archive for text mining. Having said about the background.
During the crawling process number of blank .org domain carrying websites also were detected. Is there any criteria we could use to refrain from crawling web sites having no content?
Currently I am passing the URL and it reads the URLs HTML content in Java. Although websites do not carry any content, still it has HTML code. So could you please suggest me a way of doing it?
I have tried figuring out text availability, image availability but it was not possible to stop 100% blank web pages detection.
Finally I was able to detect blank web pages depends on its size (below 3 KB) and by scanning the content. Firstly I download the HTML content via URLConnection and store it in memory.
public ArrayList<String> captureHtmlTags(String inputUrl) {
ArrayList<String> readWebsiteHTMLTags = new ArrayList<String>();
try {
URL url = new URL(inputUrl);
try {
URLConnection connection = url.openConnection();
// open the stream and assign it into buffered reader..
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
if (bufferedReader.readLine() != null) {
while ((inputLine = bufferedReader.readLine()) != null) {
Document doc = Jsoup.parse(inputLine);
readWebsiteHTMLTags.add(String.valueOf(doc));
}
bufferedReader.close();
readWebsiteHTMLTags.size();
} else {
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
return readWebsiteHTMLTags;
}
Then using iText 7 library I convert read HTML into PDF and scan for text contents.
public String convertWebsiteToPdf(ArrayList<String> htmlCode) {
String generateHtmlPage = null;
String fileLocation = CONST_FILE_PATH; // path to save the PDFs
generateHtmlPage = htmlCode.toString();
FileOutputStream fileOutputStream = null;
try {
try {
fileOutputStream = new FileOutputStream(fileLocation);
HtmlConverter.convertToPdf(generateHtmlPage, fileOutputStream);
fileOutputStream.close();
} catch (Exception e) {
System.out.println("");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return fileLocation;
}
If no content found along with the low size of file, I consider it as a Blank web page.
It worked out finally.

KML Layer is not shown in Google Maps

I have a problem with displaying certain KMLs in Google Maps, it happens that after going through the method addLayerToMap, it is not rendered on the map.
Funny is that when I step it in Google MyMaps the same works normally and even if I export from there and set to display in Google Maps of the application, it displays normally.
I noticed that MyMaps greatly changes the structure of the KML and it is even smaller (in number of lines and consequently the size).
KML file (original): https://drive.google.com/file/d/1Z4AZMP1xNMgVNNXjK11-kD0gwlPLmJmR/view?usp=sharing
PS: On invalid paths of images, I changed manually and there were no results.
KML file (parsed by Google MyMaps): https://drive.google.com/file/d/1WPT3ZogzjTNa9ITeZze1cYf3ly4JFpUZ/view?usp=sharing
Method that I'm using to read KML (works with most of the KMLs I tried, including Google's own example):
private void retrieveFileFromResource() {
try {
KmlLayer kmlLayer = new KmlLayer(mMap, R.raw.teste3, getActivity());
kmlLayer.addLayerToMap();
moveCameraToKml(kmlLayer);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
I'm trying to add the components to the map manually (polylines, polygons, markers, etc) but did not succeed.
try using the below code to check if the kml file is present in the folder and if present, show them in the google maps.
private void retrieveFileFromResources()
{
try
{
int check = this.getResources().getIdentifier(teste3,"folder name", this.getPackageName());
if(check != 0)
{
InputStream inputstream = this.getResources().openRawResource(this.getResources().getIdentifier(teste3,"folder name",this.getPackageName()));
KmlLayer kmlLayer = new KmlLayer(mMap, inputStream, getApplicationContext());
kmlLayer.addLayerToMap();
}
else
{
Toast.makeText(this,"Request KML Layer not available",Toast.LENGTH_LONG).show();
}
}catch(IOException e)
{
e.printStackTrace();
}
catch(XmlPullParseException e)
{
e.printStackTrace();
}
}

Create shortcut with minimized window

Good morning, I have a Java code using JShortcut, the problem is I can not create the shortcut with the minimized window, try looking for properties as windowstyle, but find nothing.
Code.
public void createDesktopShortcut() {
try {
link.setFolder(JShellLink.getDirectory("desktop"));
link.setName("ie");
link.setPath(filePath);
//windowstyle = 7
link.save();
} catch (Exception ex) {
ex.printStackTrace();
}
}
How can I add this option?

Reading an image resource file in java

I've been searching to find out how to solve this problem for some time now, but I can't seem to find a way. I could read the image as long as it was in eclipse, but when I exported it, it was impossible. I tried with an InputStream, but that throws an IllegalArgumentException for some reason, what am I doing wrong?
InputStream iconstream = getClass().getResourceAsStream("resources/icon.png");
InputStream pigstream = getClass().getResourceAsStream("resources/pig.png");
This is for getting the resources, and here is where I read them:
try {
icon = ImageIO.read(iconstream);
pig = ImageIO.read(pigstream);
} catch(IOException e) {
e.printStackTrace();
System.err.println(e.getMessage());
} catch(IllegalArgumentException e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
change
InputStream iconstream = getClass().getResourceAsStream("resources/icon.png");
to
InputStream iconstream = getClass().getResourceAsStream("/resources/icon.png");
getClass().getResourceAsStream() looks in the package of the class. You either need to add a leading / to the name or else use getClass().getClassLoader().getResourceAsStream().

Why do I get FileNotFoundException when I create and try to write to file on Android emulator?

First off, I am not trying to write to the SDCard. I want to write some information to a file that persists between uses of the app. It is essentially a file to hold favorites of the particular user. Here is what the code looks like:
try {
File file = new File("favorites.txt");
if (file.exists()) {
Log.d(TAG, "File does exist.");
fis = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fis));
}
else {
Log.d(TAG, "File does not exist.");
return favDests;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
When running this code, we always get the "File does not exist." message in our DDMS log.
We have also tried the following code to no avail:
try {
File file = new File(GoLincoln.FAV_DEST_FILE);
fis = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fis));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
It is this second portion of code that results in the FileNotFoundException.
I have read multiple tutorials on writing and reading files on Android and I believe I am following them pretty closely, so I am not sure why this code doesn't work successfully. I appreciate any help!
You shouldn't use the File class directly. Use Activity.getCacheDir() to get the cache dir which is specific to your application. Then use new File(cachedir, "filename.tmp") to create the file.
Preferences and SQLLite will both allow you to have persistent data without managing your own files.
To use shared preferences you grab it from your context, then you edit the values like so
mySharedPreferences = context.getSharedPreferences("DatabaseNameWhateverYouWant", 0);
mySharedPreferences.getEditor().putString("MyPreferenceName", "Value").commit();
To get a preference out
mySharedPreferences.getString("MyPreferenceName", "DefaultValue");
This is really the simplest way to do basic preferences, much easier then doing a file. More then strings are supported, most basic data types are available to be added to the Preferences class.

Categories