Wednesday, March 16, 2011

Single quotation in SQL insert statment

If you want to insert a value into the row and this value contains some sigle quotation, for exmaple the attribute value that need to be inserted may be like 'Z542.UAT.IN.CMBIZ'.

You can achieve this by using double single quotation in your insert statement which is shown as the below:

INSERT INTO ATTRIBUTES (ATTRIBUTE_ID, ATTRIBUTE_NAME, ATTRIBUTE_VALUE) VALUES('AccessTargetDir','AccessTargetDir','''Z542.UAT.IN.CMBIZ''');

ThreadLocal in Java

In Java a thread-local variable is implemented by one class called ThreadLocal rather than built-in language support. In oreder to create a thread-local variable you need to create an instance of ThreadLocal.

public class ThreadLocal {
public Object get();
public void set(Object newValue);

public void remove();
public Object initialValue();

}

ThreadLocal acts as the warppper for the variable that should be thread-local.

How ThreadLocal works? ThreadLocal maintains one table internally that holds the thread and associating object reference. When you try to get the object from ThreadLocal it will return the object reference that is stored with the current thread.


import java.util.concurrent.atomic.AtomicInteger;
public class UniqueThreadIdGenerator {
private static final AtomicInteger uniqueId = new AtomicInteger(0);

private static final ThreadLocal uniqueNum = new ThreadLocal )
@Override protected Integer initialValue() {
return uniqueId.getAndIncrement(); }
};
public static int getCurrentThreadId() {
return uniqueId.get();
}
} // UniqueThreadIdGenerator

Sunday, March 13, 2011

Nillability, empty value and omitted element

Sometimes it's bit confusing for the nill value, empty value and omitted element in XML. The following are some points I found during the use of XML which can be helpful to understand these concept.

Nillability in XSD indicates that the value of one element is not set. That is to say that there is no value for this element. XSD has a way to let you specifically define a nil value for an element in the instance document. This is xsi:nill="true" appearing in the element tag. For example:

<length xsi:nil="true"/>
Adding xsl:nil="true" gives the clear message to the document consumer or processor that the value of this element is nil, not set.

In order to have xsi:nil appear in the instance document XSD requires the schma must have xs:nillable="true" for that element,

<xs:element name="length" type="xs:decimal" nillable="true"/>

With nillable="true" added in the schema, when the value is nil for this element xsi:nil must be there to declare its nillness in the instance document otherwise the document will not be validated against the schema.
For example this is one type defined in XSD :
<xs:complexType name="size">
<xs:sequence>
<xs:element name="sizeNo" type="xs:integer" nillable="true"/>
<xs:element name="length" type="xs:decimal" nillable="true"/>
<xs:element name="standard" type="xs:string" nillable="true"/>
<xs:element name="sizeName" type="xs:string" nillable="false"/>
<xs:element name="width" type="xs:integer" nillable="false"/>
</xs:sequence>
</xs:complexType>
Here comes the instance:
<size xml:xsi="http://www.w3.org/2001/XMLSchema-intances">
<sizeNo xsi:nil="true"/> OK. nil value
<length xsi:nil="true"/> OK. nil value
<standard/> OK. not nil value but empty string value
<sizeName/> OK. Cannot benil value but empty string value
<width>20</> OK. Cannot be nil. Having one value
</size>

<size xml:xsi="http://www.w3.org/2001/XMLSchema-intances">
<sizeNo xsi:nil="true"/> OK. nil value
<length xsi:nil="true"/> OK. nil value
<standard/> OK. not nil value but empty string value
<sizeName/> OK. not nil value but empty string value
<width>20</> OK. Cannot be nil. Having one value
</size>



<size xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance%22&gt;
<sizeNo/> Not OK. Need to add xsi:nil="true"
<length/> Not OK. Need to add xsi:nil="true"
<standard xsi:nil="false">20</standard> OK.
<sizeName xsl:nil="true"/> OK. It is not empty string value.
<width/></size> Not OK. Need to add xsi:nil="true"


Different from nil value of the element the element can be omitted from the instance document this is achieved from the attribute xs:minOccurs in the element. Its default value is 1 which means that if not having xs:minOccurs is equaliant to setiing xs:minOccurs=1.
When xs:minOccurs=0 it indicates that this element might be omitted from the instance document completely.

Monday, March 7, 2011

Simple Type vs. Complex Type and Simple Content vs. Complex Content

Sometimes it is bit confusing to novices in the concepts Simple Type, Complex Type, Simple Content and Complext Content in XML schema. Here what I'd like to do is to highlight some key things that might be helpful in differentiate thses concepts.
  • XML schmea file defines the XML data types that may be used in the XML instance document. There are two types in schmea file: Simple Type and Complex Type.
  • Simple Type means that this type doesn't have attribute and elements except for the character data.

  • XSD has over 40 built-in Simple Types such as xsd:string, xsd:integer, xsd:boolean, xsd:date and etc and cutom-made data types of Simple Type as well.
    All custom-made Simple Type are the restriction, list of the built-in types or other custom-made simple types.

  • Contray to Simple Type, Complex Type must either have attribute or elements or both of them.

  • Simple Content and Complext Content are about how the content of a Complex Type look like. Thus they have NOTHING to do with Simple Type.

    Simple Content

Wednesday, March 2, 2011

JAXBContext is expensive to create

According to Java Doc

"The JAXBContext class provides the client's entry point to the JAXB API. It provides an abstraction for managing the XML/Java binding information necessary to implement the JAXB binding framework operations: unmarshal, marshal and validate. "

However it is quite slow and expensive to create. It is wise to reuse the existing instance whenever the message of the same schema is repeated for marshalling or unmarshalling.

One way is to create single JAXBContext with many JAXB objects that are used and then share this JAXBContext within the application. JAXBContxet has one factory method to instantiate JAXBContext. This factory method can take in multiple JAXB classes into the context. There are two forms of instantion of JAXBContext as shown in below.

JAXBContext jc = JAXBContext.newInstance("com.toic.order.foo:com.toic.order.bar" );

JAXBContext jc = JAXBContext.newInstance(com.toic.order.foo.class, com.toic.order.bar.class" );

Where com.toic.order.foo and com.toic.order.bar are the qualified class names of two JAXB classes that are taken into the JAXB context.