Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

String Samples

String samples
  
     System.setProperty("file.encoding","UTF-8") 
     System.setProperty("file.encoding","CP850")
     def delimiters = [:]

     //String data = new String((byte[])"é():;éééçç,.<>[]","UTF-8");

     def data =   "é():;éùùçç,.<>[]" 
     
     data = data.replace('é', 'e');
     println data
     println data.size()
     
     data.each { c ->
        // def str = Long.toHexString(c as long)
       def str = c
       //  if (!(c =~ /\w/)) {
         if (c == 'é') {
        	delimiters[c] = "Annul\u00E7e"
         }
         else if(c == 'ù') {
        	 delimiters[c] = "\u00E7"
         }         
         else
         {
        	 delimiters[c] = str
         }
     }
     println delimiters

test

// description of your code here

"def" is a replacement for a type name. In variable definitions it is used to indicate that you don't care about the type. In variable definitions it is mandatory to either provide a type name explicitly or to use "def" in replacement. This is needed to the make variable definitions detectable for the Groovy parser.

These definitions may occur for local variables in a script or for local variables and properties/fields in a class.
Rule of thumb
You can think of "def" as an alias of "Object" and you will understand it in an instant.

Future Groovy may give "def" an additional meaning in terms of static and dynamic typing. But this is post Groovy 1.0.
"def" can also replace "void" as the return type in a method definiton.

def dynamic  =  1
dynamic = "I am a String stored in a variable of dynamic type"
int typed = 2
typed = "I am a String stored in a variable of type int??"    // throws ClassCastException


The assignment of a string, to a variable of type int will fail. A variable typed with "def" allows this.

Groovy XML Schema Validator

// Create a Schema and a sample document, then validate the document against the schema

def xsd = '''
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="comics">
</xs:element>
</xs:schema>
'''

def xml = '''
<comics>
</comics>
'''

import javax.xml.XMLConstants
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.SchemaFactory

def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
def schema = factory.newSchema(new StreamSource(new StringReader(xsd)))
def validator = schema.newValidator()
validator.validate(new StreamSource(new StringReader(xml)))

Groovy XML Schema Validator

// Create a Schema and a sample document, then validate the document against the schema

def xsd = '''
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="comics">
</xs:element>
</xs:schema>
'''

def xml = '''
<comics>
</comics>
'''

import javax.xml.XMLConstants
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.SchemaFactory

def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
def schema = factory.newSchema(new StreamSource(new StringReader(xsd)))
def validator = schema.newValidator()
validator.validate(new StreamSource(new StringReader(xml)))