Short source code examples

As a quick note, if you need to see how to write a Scala method that returns a Future, this example may help:

I generally try to avoid this coding style these days, but, if you want to see how to use a Java BufferedReader and its readLine method in a Scala while loop, here you go:

I was looking for a good way to access XML resources (like RSS feeds) in Scala, and I currently like the idea of using ScalaJ-HTTP to access the URL and download the XML content, and then using the Scala XML library to process the XML string I download from the URL.

This example Scala program shows my current approach:

Note: The code shown below is a bit old. If you want to perform a “search and replace” operation on all instances of a given pattern, all you have to do these days is use the replaceAll method on a Java String, like this:

String s = "123 Main Street";
String result = s.replaceAll("[0-9]", "-");

That second line of code returns the string “--- Main Street”. I kept the information below here for background information.

Using Akka logging is a great thing, until you need to turn it off. In short, to disable Akka logging, you need to create a file named application.conf in your SBT src/main/resources folder, and set the loglevel to “OFF” in that file, like this:

As a quick note, if you ever want to created a dotted border that has some RGB opacity to it, I just used the following CSS code to style some hyperlinks, and I can confirm that it works:

Here’s a short Java/JDBC example program where I show how to perform SQL SELECT, INSERT, UPDATE, and DELETE statements with JDBC:

As a quick note and a little bit of source code sharing, I wrote the following Perl script to delete all of the binary files it finds in a list of files it’s given. I named this script deleteBinaryFiles.pl, and it should be called like this:

deleteBinaryFiles.pl listOfFilesToLookAt

where listOfFilesToLookAt is a file that contains a list of filenames, with one filename per line.

Given that brief introduction, here’s the source code:

If you’ve never used AppleScript, here are two iTunes AppleScript examples to get you going. First, This one tells iTunes to play the playlist named “My Favorites”:

tell application "iTunes"
    play playlist "My Favorites"
end tell

That script starts playing a random song from that playlist. If you want to start by playing the first song of that playlist, this script will do the trick:

As a quick note, if you’re looking at a Drupal form and it says you can use the "Rewrite the output of this field" replacement patterns shown (somewhere) on this page — and you can’t find those replacement patterns on that page — you can find a complete list of them at this drupal.org url.

As an example, if you’re working with a Drupal Node, you can use replacement patterns like these:

[node:author:name]
[node:content-type]
[node:content-type:name]

As a quick CSS note, if you want to achieve a “zebra striping” style with even and odd CSS row selectors, CSS styles like this will get the job done:

.path-frontpage .content-inner-right .content-type-Text:nth-child(even) {
    /* yellow */
    background-color: #fdfdf6;
}

.path-frontpage .content-inner-right .content-type-Text:nth-child(odd) {
    /* blue */
    background-color: #f3fbff;
}

I use that CSS for the front page of this website, but if you want a simpler example, here you go:

SQL FAQ: How can I select every row from a database table where a column value is not unique?

I’m working on an problem today where a Drupal article can have many revisions, and the way Drupal works is that a table named node_revisions has a nid field for “node id,” and a vid field for “revision id.” If you have five revisions of an article (i.e., a blog post), there will be five records in this table, and each record will have the same nid value and a unique vid. If an article has no revisions, this table will have one entry with a unique nid and unique vid.

If you have a Drupal 8 website and your images are not showing up on your image/photo nodes, one possible problem (which I just learned) is that your Drupal 8 theme needs to refer to {{ content }} in node.html.twig and not {{ content.body }}.

I don't know why you have to do that, but after four hours of troubleshooting the problem with my own Drupal 8 website/theme, I can confirm that if you don't do it, your images/photos won't be shown on their nodes. (All I was trying to do was to separate content.body from content.comment, and when I did that my images no longer showed up at their nodes/URLs.)

As a quick note, if you need a Drupal 8 Twig template if/else/then structure where you test to see if a string value is in an array, code like this will work:

{% if node.getType not in ['photo', 'text']  %}
    <div class="similar">
        {{ similar_by_terms }}
    </div>
{% endif %}

That code can be read as, “If the node type is NOT ‘photo’ or ‘text,’ emit the HTML/Twig code shown.”

FWIW, it appears that you can drop all of the “migrate” database tables that end up in your Drupal 8 database after a migration, such as migrating from Drupal 6 to Drupal 8. I ended up with 178 of these migration tables in my Drupal 8 database, and deleted them like this:

As a quick note to self, here is some source code for a simple example PHP script that runs a SQL SELECT query against a MySQL database:

A cool feature of the Scala REPL is that you can reset/clear a REPL session. To do so, just issue the :reset command, like this:

scala> :reset
Resetting interpreter state.
Forgetting this session history:

Assuming that you already have at least a little history in your REPL session, the :reset command will show you everything that it dumps, so the full output looks more like this:

Note: This code is currently a work in progress. I know of possible approaches, but I don’t know of a perfect working solution yet.

I’m currently trying to find the right way to find the current monitor size, when you’re writing a Java Swing application to work in a multiple-monitor configuration. I always use three monitors, so I can test this pretty easily.

If you need some source code for a Java FileFilter for image files, this code can get you started:

As a quick note, if you ever need to use a Java TimerTask, you can define one like this:

class BrightnessTimerTask extends TimerTask {
    @Override
    public void run() {
        // your custom code here ...
    }        
}

and you can then instantiate it, create a Timer, and schedule the task like this: