Loading

Disable DOM DTD validation

setValidating(false) will not prevent DTD validation, but you can set http://apache.org/xml/features/nonvalidating/load-external-dtd feature to false. I think is a Xerces-only feature, though, it may not be recognized by other parsers.

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        //factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); // it doesn't work
        factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        DocumentBuilder parser = factory.newDocumentBuilder();
        Document doc = parser.parse(getClass().getResourceAsStream("ennote.xml"));

Typical DTD not found exception stacktrace

java.io.FileNotFoundException: /Users/ecerulm/NetBeansProjects/en4j/NBPlatformApp/DTD/xhtml1-strict.dtd (No such file or directory)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(FileInputStream.java:106)
	at java.io.FileInputStream.<init>(FileInputStream.java:66)
	at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
	at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
	at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:653)
	at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:1315)
	at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDTDEntity(XMLEntityManager.java:1282)
	at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.setInputSource(XMLDTDScannerImpl.java:283)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1193)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1090)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1003)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
	at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:225)
	at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283)
	at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
	at com.rubenlaguna.en4j.NoteContentViewModule.NoteContentViewTopComponent.resultChanged(NoteContentViewTopComponent.java:141)
...
...

Google wave extension list

Interesting list of google wave extensions that I found in DocStoc


Google Wave Extension List

Rotate screen on Windows [Intel® 945G Express Chipset]

I just found out on that if you have a Intel® 945 or any modern Intel card and you monitor/display/screen appears rotated 90 degrees or upside down, etc you can restore it to it’s original position by pressing Ctrl-Alt-↓

Via: http://www.labnol.org/software/turn-computer-screen-upside-down/6097/

Netbeans: Compile schemas into JAXB classes with XJC task

To add a XJC task to a Netbeans build.xml you need to add a -pre-compile target to your build.xml. If it’s a netbeans module build.xml then you need to make compile depend on -pre-compile and projectized-common.compile (see example below). For regular Java Applications just define the -pre-compile target as the standard build.xml will call -pre-compile before compile. Then it’s just a matter of defining the <taskdef> with the proper classpath to the jaxb’s jars.

But it’s just simpler to take a look to an example (this one is for a Netbeans module build.xml:

<project name="com.rubenlaguna.en4j.jaxb" default="netbeans" basedir=".">
    <description>Builds, tests, and runs the project com.rubenlaguna.en4j.jaxb.</description>
    <import file="nbproject/build-impl.xml"/>
 
 
 <target name="-pre-compile">
        <path id="xjc.classpath">
 
            <fileset dir="/Applications/NetBeans/NetBeans 6.7.app/Contents/Resources/NetBeans/ide11/modules/ext/jaxb/" includes="*.jar"/>
 
            <!-- import the JAXB 2.1 library if this task is in java application build.xml. you can't access library classpath from
                     a module's build.xml
            pathelement path="${libs.JAXB21.classpath}"/>
            <pathelement path="${libs.JAXB_21.classpath}"/>
            -->
        </path>
 
        <taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
            <classpath refid="xjc.classpath"/>
        </taskdef>
 
        <xjc destdir="src" package="com.rubenlaguna.en4j.jaxb.generated">
            <schema  dir="src" includes="**/*.xsd"/>
            <produces dir="src/com/rubenlaguna/en4j/jaxb/generated" includes="* impl/*" />
        </xjc>
    </target>
    <target name="compile" depends="-pre-compile,projectized-common.compile"/>
</project>

Netbeans & woodstox: ClassCastException: com.sun.xml.bind.v2.runtime.JAXBContextImpl cannot be cast to javax.xml.bind.JAXBContext

Are you getting a ClassCastException in a Netbeans Platform Application, when you are you sure that the class can be casted? Then check http://wiki.netbeans.org/PlainView.jsp?page=DevFaqModuleCCE. This is most likely the result of the the classes being loaded by different classloaders. So JAXBContextImpl cannot be cast to JAXBContext because that JAXBContext is from Classloader “A” and JAXBContextImpl can only be cast to JAXBContext from Classloader “B”.

SEVERE [global]
java.lang.ClassCastException: com.sun.xml.bind.v2.runtime.JAXBContextImpl cannot be cast to javax.xml.bind.JAXBContext
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:145)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:277)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:337)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:244)
        at com.rubenlaguna.en4j.mainmodule.ImportEvernoteFile.actionPerformed(ImportEvernoteFile.java:65)
        at org.openide.awt.AlwaysEnabledAction.actionPerformed(AlwaysEnabledAction.java:115)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.AbstractButton.doClick(AbstractButton.java:389)
        at com.apple.laf.ScreenMenuItem.actionPerformed(ScreenMenuItem.java:95)
        at java.awt.MenuItem.processActionEvent(MenuItem.java:627)
        at java.awt.MenuItem.processEvent(MenuItem.java:586)
        at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:317)
        at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:305)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:638)
        at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:104)
[catch] at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 28 minutes 5 seconds)

So this could happen for example if you have both woodstox in a Netbeans Library Wrapper module and you make you application dependent on ide11 ⇒ JAXB 2.1 Library
jaxb21.

In my case I was able to solve by just removing JAXB 2.1 Library from the application.

ClassCastException: JAXBContextImp to JAXBContext with woodstox and NetBeans

If you are getting

java.lang.ClassCastException: com.sun.xml.bind.v2.runtime.JAXBContextImpl cannot be cast to javax.xml.bind.JAXBContext
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:145)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:277)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:337)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:244)
        at com.rubenlaguna.en4j.mainmodule.ImportEvernoteFile.actionPerformed(ImportEvernoteFile.java:65)

and you are using woodstox be sure to check the DevFaqModuleCCE. This could happen when you have Netbeans Platform Application with a module containing woodstox but also including JAXB 2.1 in the module dependencies for the application.

git: error pushing via HTTP (return code 22)

If you get a

error: Cannot access URL http://github.com/xxxxxxx/, return code 22

when trying to push changes to a git repository via HTTP.

This is probably because you are using an HTTP proxy to access the repo and that proxy doesn’t support WebDAV HTTP methods (especially PROPFIND). So when git issues a PROPFIND the http proxy answers back with a 500 Internal Server Error or something like that.

It seems that PROPFIND is absolutely required so if you can use git:// directly or use a HTTP proxy which supports PROPFIND. Then I think the only option left is try use CONNECT in the proxy.

Netbeans refuses to recognize persistence.xml (no visual editor)

If you create the persistence.xml manually in Windows the file will be created with CRLF line endings (windows style line endings) ,that will prevent Netbeans for recognizing Netbeans will not recognize it as the special file it is and won’t be able to to open it with the special/custom visual editor.

netbeans visual editor for persistence files

netbeans visual editor for persistence files

I opened an bug report netbeans issue #172538. At the beginning, I thought the problem was due to diferent line ending CRLF vs LF issues, but as pointed out in the bug report the line ending has nothing to do with it. It’s just the IDE restart what is needed, no need to change the line endings.

The workaround is easy: change the newline (also line break or end-of-line EOL) characters to Unix style (LF) with any utility. I used Cygwin’s dos2unix (dos2unix src/META-INF/persistence.xml) and then just restart the IDE. Unfortunately just closing and reopening the project containing the persistence.xml file won’t work you have to restart Netbeans.

OpenJPA: Generated SQL contains extra UPDATEs

I’m trying to use OpenJPA to insert some entries in the database and I’m getting a strange number of UPDATEs beside the INSERTs.

I isolated the problem to the following snippet of code

    private void start() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistencexmltest1PU");
        EntityManager em = emf.createEntityManager();        
        for (int i = 0; i < 10; i++) {
            em.getTransaction().begin();
            MyEntity n =new MyEntity();
            n.setValue(i);
            em.persist(n);        
            em.getTransaction().commit();
        }
    }

The generated SQL looks like this:

INSERT INTO MYTABLE (ID, VALUE, CREATED) VALUES (?, ?, ?) [params=(int) 1, (int) 0, (null) null]
INSERT INTO MYTABLE (ID, VALUE, CREATED) VALUES (?, ?, ?) [params=(int) 2, (int) 1, (null) null]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 1]
INSERT INTO MYTABLE (ID, VALUE, CREATED) VALUES (?, ?, ?) [params=(int) 3, (int) 2, (null) null]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 2]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 1]
INSERT INTO MYTABLE (ID, VALUE, CREATED) VALUES (?, ?, ?) [params=(int) 4, (int) 3, (null) null]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 3]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 2]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 1]
INSERT INTO MYTABLE (ID, VALUE, CREATED) VALUES (?, ?, ?) [params=(int) 5, (int) 4, (null) null]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 3]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 4]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 2]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 1]
INSERT INTO MYTABLE (ID, VALUE, CREATED) VALUES (?, ?, ?) [params=(int) 6, (int) 5, (null) null]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 3]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 4]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 2]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 5]
UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 1]
...

note the extra UPDATE statements after each INSERT, the number of UPDATEs grows too. From zero UPDATEs after the first INSERT, one in the second, two in the third, and so on. I don’t need to say that this is of course really inefficient. I don’t know what is ultimate cause of this but this started when I added a @Temporal(TemporalType.TIMESTAMP) annotation to the entity class. And I can make it go away by calling EntityManager.clear() after each em.getTransaction().commit()

Ok I found a bug report stating that this behaviour is only observed when the entity classes are not enhanced. So the best solution is use the enhancer. But it really doesn’t work for me, I still get the extra UPDATEs even with enhanced classes. So I’m stuck with the EntityManager.clear() for now.If the class is PROPERLY ENHANCED this problems goes away.

Check the logs (enable them with <property name="openjpa.Log" value="DefaultLevel=TRACE"/> in persistence.xml) and make sure that you don’t see any entry like

5968  persistencexmltest1PU  INFO   [main] openjpa.Enhance - Creating subclass for "[class com.rubenlaguna.MyEntity]". This means that your application will be less efficient and will consume more memory than it would if you ran the OpenJPA enhancer. Additionally, lazy loading will not be available for one-to-one and many-to-one persistent attributes in types using field access; they will be loaded eagerly instead.

If you see the Creating subclass for message means that the class wasn’t enhanced, as I read OpenJPA really need enhanced classes, un – enhanced are just for testing and trivial examples.

For reference:

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="persistencexmltest1PU" transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <class>com.rubenlaguna.MyEntity</class>
    <properties>
      <property name="openjpa.ConnectionPassword" value=""/>
      <property name="openjpa.ConnectionDriverName" value="org.hsqldb.jdbc.JDBCDriver"/>
      <property name="openjpa.ConnectionUserName" value="sa"/>
      <property name="openjpa.ConnectionURL" value="jdbc:hsqldb:file:/Users/ecerulm/everjavatest"/>
      <property name="openjpa.Log" value="SQL=TRACE"/>
      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(SchemaAction=&apos;add,deleteTableContents&apos;,ForeignKeys=true)"/>
    </properties>
  </persistence-unit>
</persistence>

MyEntity.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package com.rubenlaguna;
 
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
 
/**
 *
 * @author Ruben Laguna <ruben.laguna at gmail.com>
 */
@Entity
@Table(name = "MYTABLE")
public class MyEntity {
    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
 
 
    @Column(name = "VALUE")
    private Integer attr1;
 
 
    @Column(name = "CREATED")
    @Temporal(TemporalType.TIMESTAMP)
    private Date created;
 
 
    public Integer getId() {
        return id;
    }
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    public Integer getValue() {
        return attr1;
    }
 
    public void setValue(Integer value) {
        this.attr1 = value;
    }
 
}

References:

Adding a Google Search Web Element to a Wordpress theme

The newly released Google Web element: custom search is awesome.

csewebelement

To add it to my wordpress theme (which doesn’t have a top sidebar for widgets) I had to edit (Appearance ⇒ Editor) the header.php file and add the snippet I got from Google there, at just at the end of the header.php. So the search web element is show right after the banner and before the posts.

In my case the code is

<!-- Google search web element BEGIN-->
<div id="cse" style="width:100%;">Loading</div>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
 
<script type="text/javascript">
  google.load('search', '1');
  google.setOnLoadCallback(function(){
    new google.search.CustomSearchControl('partner-pub-6449419902780618:ybxd02gp4hx').draw('cse');
  }, true);
</script>
<!-- Google search web element END-->

If your theme has a top sidebar for widgets you can add the snippet as a text widgets instead there (to avoid fiddling editing the theme).