Install Firefox Java Plugin on Linux


about-plugins

This is silly but not super obvious and not easy to find on the interweb thingie searches come up with Oracle’s solution about linking the libjavaplugin_oji lib in the Mozilla plugins dir:

<Java installation directory>/plugin/i386/ns7/libjavaplugin_oji.so

which does not work for me. The way to get this going is to create a link to the libnpjp2 library in the Mozilla plugin directory as follows:

ln -s /<Java installation directory>/jre/lib/i386/libnpjp2.so /usr/lib/mozilla/plugins/


Access and use Java Properties in Spring Context file

This seems like such a simple thing to do, and yet finding a nice simple example online was a real bear (A BEAR!!! actually). So here is my example so the next person will have an easier time of it.

Add properties to your properties file:

[my.properties]

button.icon.search=”com/example/resorces/search.gif”
button.text.search=”Search”

Load the properties file inside the context file:

<bean
class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
<property name=”locations”>
<value>classpath:path/in/cp/my.properties</value>
</property>
</bean>

Access properties from the properties file when setting properties of a bean:

<bean id=”searchButtonBean” init-method=”init” class=”javax.swing.JButton”>
<property name=”text”>
<value>${button.text.search}</value>
</property>
<property name=”buttonIcon” value=”${button.icon.search}”/>
</bean>

Apache ANT If Else Condition without ANT-Contrib

Sometimes I must make an ANT Build file that will need to work on many different machines, where I cannot be sure of the version or configuration of ANT nor the environment it is running in. This can cause some discomfort when it comes to complex builds involving conditions. The ANT-Contrib library has a convenient If/Else condition but this library is not always installed with ANT or even when it is, the build environment may not be configured to use it. So in either case, I use the following method which, though a little wordy, gets the job done without relying on anything beyond a standard ANT install. The method uses three ANT targets to do the work, one for the <if>, one for the <then> and one for the <else>. As you can see it creates a bit of extra code, but I believe the portability is worth it.

Below the explanation, the example referred to in the post will be made available. Now lets get started.

First Create a target that should do some bit of work based on a condition. Make the target depend on two other targets (here these are “setup” and “fail”), and only allow it to execute if a boolean variable has the right value, using the “if” attribute of the “target” element.

<target name=”go” depends=”setup, fail” if=”allIsWellBool”>
<!– SOME WORK HAPPENS HERE –>
</target>

Next, have one of the dependencies create a boolean property based on the condition. The process is a little convoluted, in that you need to use a <condition> construct that has an <equals> test against text to create a boolean property. For instance, in the example for this post, this dependency target, “setup”, asks for some user input, then checks to see if an empty string was entered. A new boolean property is added based on the test.

<target name=”setup”>
<input message=”Please enter something:” addproperty=”somethingProp”/>

<condition property=”allIsWellBool”>
<not>
<equals arg1=”${somethingProp}” arg2=”” trim=”true”/>
</not>
</condition>
</target>

After this target either there is a new boolean property called “allIsWellBool” or there is not. Subsequent test for this property will pass if the property is there, and true, or fail if it is not.

Finally, create a target to execute if the test fails, the else condition basically. This is done by only allowing it to execute if a boolean variable has the right value again, but this time using the “unless” attribute of the “target” element. In the case of the example for this post, this target will execute unless the “allIsWellBool” exists and is true.

<target name=”fail” depends=”setup” unless=”allIsWellBool”>
<fail message=”You must enter something”/>
</target>

See also that we are adding the dependency for the “setup” target here as well. This is because this target depends on a property that is set in the “setup” target, and “setup” should therefore happen first.

The full source of the example mentioned in this post is below.

<?xml version="1.0" encoding="UTF-8"?>
<!--
    Simple example of how to do an If/Else condition in
    your build file without using the ANT-Contrib libs

    This source code is provided free of charge and placed in the
    public domain. It is not supported by anyone.

    Use this at your own risk.
-->
<project name="Test If Else Condition without ANT-Contrib" default="go">

    <!-- ======================= -->
    <!-- THE START OF THE SCRIPT -->
    <target name="go" depends="setup, fail" if="allIsWellBool">

        <echo message="Great, passed the test, lets do something!"/>
        <!-- SHOULD DO SOMETHING -->

    </target>

    <!-- =================================================== -->
    <!-- SETUP, GETS SOME INPUT THAT WE WILL TEST WITH LATER -->
    <target name="setup">

        <input message="Please enter something: " addproperty="somethingProp"/>

        <!-- SET A BOOLEAN PROPERTY IF WE GOT SOME INPUT  -->
        <condition property="allIsWellBool">
            <not>
                <equals arg1="${somethingProp}" arg2="" trim="true"/>
            </not>
        </condition>
    </target>

    <!-- ============================================ -->
    <!-- FAIL, WE DID NOT GET ANY INPUT FROM THE USER
         THIS DEPENDSON THE SETUP TASK AND WILL
         EXECUTE ONLY IF THE PROPERTY allIsWellBool
         IS FALSE
    -->
    <target name="fail" depends="setup" unless="allIsWellBool">
        <fail message="You must enter something"/>
    </target>

</project>

Determine if a String is XML using Java and Regular Expressions

So again I am posting something I have to do every now and then and have to spend time, each time, to check the pattern or usage etc. for.

Once in a while, in an app that does not do much XML, and therefore is not already using an XML parser of some kind, will need to at the least, determine if a String is XML. With a pretty simple Regular Expression, it is possible using plain old Java and without using any specific XML technology.

I know there are other references out there for doing this, but it is here below as a code sample, for my easy reference and maybe it will help someone else out, who knows. Enjoy.

Are we XML (like) data? :

import java.util.regex.Pattern;
import java.util.regex.Matcher;


public class test {



    /**
     * return true if the String passed in is something like XML
     *
     *
     * @param inString a string that might be XML
     * @return true of the string is XML, false otherwise
     */
    public static boolean isXMLLike(String inXMLStr) {

        boolean retBool = false;
        Pattern pattern;
        Matcher matcher;

        // REGULAR EXPRESSION TO SEE IF IT AT LEAST STARTS AND ENDS
        // WITH THE SAME ELEMENT
        final String XML_PATTERN_STR = "<(\\S+?)(.*?)>(.*?)</\\1>";



        // IF WE HAVE A STRING
        if (inXMLStr != null && inXMLStr.trim().length() > 0) {

            // IF WE EVEN RESEMBLE XML
            if (inXMLStr.trim().startsWith("<")) {

                pattern = Pattern.compile(XML_PATTERN_STR,
                Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE);

                // RETURN TRUE IF IT HAS PASSED BOTH TESTS
                matcher = pattern.matcher(inXMLStr);
                retBool = matcher.matches();
            }
        // ELSE WE ARE FALSE
        }

        return retBool;
    }



}/**/

DOM Document – get or extract contained document (or Node) as XML Source

Something I have to do every once in a while, and can never remember how (especially when under some tight deadline, with people standing over my shoulder asking “is it done yet, is it done?” “how much longer?” etc.) is to extract a fragment of one DOM document to get the XML source of the nested or contained document. So I am going to add a note here, for everyone’s easy reference.

First step is to get a Node to be the Root Node of the new Document. Using methods like Document’s getElementsByTagName(String) and Node.getChildNodes(), or using XPathAPIs and CachedXPathAPI class’ selectSingleNode(Node n, String xPath).

Next we can use a StringWriter and a Transformer to covert the Node to XML Source. Better than a rambling explanation, a simple source example should be do the trick. You can use a method something like the nodeToXMLString example below.

  private String nodeToXMLString(Node node) throws TransformerException
  {
    StringWriter sw = new StringWriter();

    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.transform(new DOMSource(node), new StreamResult(sw));

    return (sw.toString());
  }

Apache XMLBeans – output XML without a namespace

This is again something that I need to know how to do but never remember how when time it tight.

Apache XMLBeans http://xmlbeans.apache.org/ is a great tool for working with XML in Java, but it requires the XML Schema being used to create the objects from XML to have a Namespace. The namespace ends up being part of the package structure for the Objects created and I guess having a unique path for these is a good idea. However, this requirement can be a bit of a pain, when working with some simple XML structures that do not have a namespace. Especially when your ready to persist the XML Bean objects to XML source. If a namespace is added to your XML Schema that the XML Bean objects are created from, XML source generated from them will by default also have the namespace. I can never remember how to output the XML source without a namespace and so I am writing it down here where I can get at it with a click, and maybe this will help someone else as well.

There are two key steps to remove the Namespace when outputting XML Source.

  1. Tell it to use the default namespace:
           xmlops.setUseDefaultNamespace();
       
  2. Tell it that you have already declared the default namespace:
           dnsMap.put("", "http://example.com/schemas/DefaultNnameSpace");
           xmlops.setSaveImplicitNamespaces(dnsMap);
      

After this, you can output as normal:

      xmlops.setSavePrettyPrint();
      xmlops.setSaveNamespacesFirst();
      retString = myXMLDoc.xmlText(xmlops);
      return retString;

Note: the research and testing to solve this was done using XMLBeans v2.4.0

Declare and fill a multi dimensional array in Java

This is one of those super simple things I wont use for ever and ever and then when it comes time to use it, I can never remember and have to look at the API again. It makes me angry that I don’t remember it and so it is here now and I (and others maybe) can remember with a click.  

Using a Java String array as an example, the declaration looks like:

 

String[][] myStr = {
{"col-0-row-0","col-1-row-0"},
{"col-0-row-0","col-1-row-0"}
};
Follow

Get every new post delivered to your Inbox.