KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > dsmlv2 > ParserUtils


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.dsmlv2;
22
23
24 import java.util.List JavaDoc;
25
26 import javax.xml.transform.Transformer JavaDoc;
27 import javax.xml.transform.TransformerConfigurationException JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.stream.StreamSource JavaDoc;
31
32 import org.apache.directory.ldapstudio.dsmlv2.engine.Dsmlv2Engine;
33 import org.apache.directory.ldapstudio.dsmlv2.request.BatchRequest;
34 import org.apache.directory.ldapstudio.dsmlv2.request.BatchRequest.Processing;
35 import org.apache.directory.ldapstudio.dsmlv2.request.BatchRequest.ResponseOrder;
36 import org.apache.directory.shared.ldap.codec.Control;
37 import org.apache.directory.shared.ldap.ldif.LdifUtils;
38 import org.apache.directory.shared.ldap.util.Base64;
39 import org.dom4j.Document;
40 import org.dom4j.Element;
41 import org.dom4j.Namespace;
42 import org.dom4j.QName;
43 import org.dom4j.io.DocumentResult;
44 import org.dom4j.io.DocumentSource;
45 import org.xmlpull.v1.XmlPullParser;
46 import org.xmlpull.v1.XmlPullParserException;
47
48
49 /**
50  * This class is a Helper class for the DSML Parser
51  *
52  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
53  * @version $Rev$, $Date$
54  */

55 public class ParserUtils
56 {
57     public static final String JavaDoc XML_SCHEMA_URI = "http://www.w3c.org/2001/XMLSchema";
58     public static final String JavaDoc XML_SCHEMA_INSTANCE_URI = "http://www.w3c.org/2001/XMLSchema-instance";
59     public static final String JavaDoc BASE64BINARY = "base64Binary";
60     public static final String JavaDoc XSI = "xsi";
61     public static final String JavaDoc XSD = "xsd";
62
63
64     /**
65      * Returns the value of the attribute 'type' of the "XMLSchema-instance' namespace if it exists
66      *
67      * @param xpp
68      * the XPP parser to use
69      * @return
70      * the value of the attribute 'type' of the "XMLSchema-instance' namespace if it exists
71      */

72     public static String JavaDoc getXsiTypeAttributeValue( XmlPullParser xpp )
73     {
74         String JavaDoc type = null;
75         int nbAttributes = xpp.getAttributeCount();
76         for ( int i = 0; i < nbAttributes; i++ )
77         {
78             // Checking if the attribute 'type' from XML Schema Instance namespace is used.
79
if ( xpp.getAttributeName( i ).equals( "type" )
80                 && xpp.getNamespace( xpp.getAttributePrefix( i ) ).equals( XML_SCHEMA_INSTANCE_URI ) )
81             {
82                 type = xpp.getAttributeValue( i );
83                 break;
84             }
85         }
86         return type;
87     }
88
89
90     /**
91      * Tells is the given value is a Base64 binary value
92      *
93      * @param parser
94      * the XPP parser to use
95      * @param attrValue
96      * the attribute value
97      * @return
98      * true if the value of the current tag is Base64BinaryEncoded, false if not
99      */

100     public static boolean isBase64BinaryValue( XmlPullParser parser, String JavaDoc attrValue )
101     {
102         if ( attrValue == null )
103         {
104             return false;
105         }
106         // We are looking for something that should look like that: "aNameSpace:base64Binary"
107
// We split the String. The first element should be the namespace prefix and the second "base64Binary"
108
String JavaDoc[] splitedString = attrValue.split( ":" );
109         return ( splitedString.length == 2 ) && ( XML_SCHEMA_URI.equals( parser.getNamespace( splitedString[0] ) ) )
110             && ( BASE64BINARY.equals( splitedString[1] ) );
111     }
112
113
114     /**
115      * Indicates if the value needs to be encoded as Base64
116      *
117      * @param value
118      * the value to check
119      * @return
120      * true if the value needs to be encoded as Base64
121      */

122     public static boolean needsBase64Encoding( Object JavaDoc value )
123     {
124         if ( value instanceof byte[] )
125         {
126             return true;
127         }
128         else if ( value instanceof String JavaDoc )
129         {
130             return !LdifUtils.isLDIFSafe( ( String JavaDoc ) value );
131         }
132         return true;
133     }
134
135
136     /**
137      * Encodes the value as a Base64 String
138      *
139      * @param value
140      * the value to encode
141      * @return
142      * the value encoded as a Base64 String
143      */

144     public static String JavaDoc base64Encode( Object JavaDoc value )
145     {
146         if ( value instanceof byte[] )
147         {
148             return new String JavaDoc( Base64.encode( ( byte[] ) value ) );
149         }
150         else if ( value instanceof String JavaDoc )
151         {
152             return new String JavaDoc( Base64.encode( ( ( String JavaDoc ) value ).getBytes() ) );
153         }
154
155         return "";
156     }
157
158
159     /**
160      * Parses and verify the parsed value of the requestID
161      *
162      * @param attributeValue
163      * the value of the attribute
164      * @param xpp
165      * the XmlPullParser
166      * @return
167      * the int value of the resquestID
168      * @throws XmlPullParserException
169      * if RequestID isn't an Integer and if requestID equals 0
170      */

171     public static int parseAndVerifyRequestID( String JavaDoc attributeValue, XmlPullParser xpp ) throws XmlPullParserException
172     {
173         try
174         {
175             int requestID = Integer.parseInt( attributeValue );
176
177             if ( requestID == 0 )
178             {
179                 throw new XmlPullParserException( "The attribute requestID can't be equal to 0", xpp, null );
180             }
181
182             return requestID;
183         }
184         catch ( NumberFormatException JavaDoc e )
185         {
186             throw new XmlPullParserException( "the given requestID is not an integer", xpp, null );
187         }
188     }
189
190
191     /**
192      * Adds Controls to the given Element.
193      *
194      * @param element
195      * the element to add the Controls to
196      * @param controls
197      * a List of Controls
198      */

199     public static void addControls( Element element, List JavaDoc<Control> controls )
200     {
201         if ( controls != null )
202         {
203             for ( int i = 0; i < controls.size(); i++ )
204             {
205                 Control control = controls.get( i );
206
207                 Element controlElement = element.addElement( "control" );
208
209                 if ( control.getControlType() != null )
210                 {
211                     controlElement.addAttribute( "type", control.getControlType() );
212                 }
213
214                 if ( control.getCriticality() )
215                 {
216                     controlElement.addAttribute( "criticality", "true" );
217                 }
218
219                 Object JavaDoc value = control.getControlValue();
220                 if ( value != null )
221                 {
222                     if ( ParserUtils.needsBase64Encoding( value ) )
223                     {
224                         Namespace xsdNamespace = new Namespace( ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI );
225                         Namespace xsiNamespace = new Namespace( ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI );
226                         element.getDocument().getRootElement().add( xsdNamespace );
227                         element.getDocument().getRootElement().add( xsiNamespace );
228
229                         Element valueElement = controlElement.addElement( "controlValue" ).addText(
230                             ParserUtils.base64Encode( value ) );
231                         valueElement.addAttribute( new QName( "type", xsiNamespace ), ParserUtils.XSD + ":"
232                             + ParserUtils.BASE64BINARY );
233                     }
234                     else
235                     {
236                         controlElement.addElement( "controlValue" ).setText( ( String JavaDoc ) value );
237                     }
238                 }
239             }
240         }
241     }
242
243
244     /**
245      * Indicates if a request ID is needed.
246      *
247      * @param container
248      * the associated container
249      * @return
250      * true if a request ID is needed (ie Processing=Parallel and ResponseOrder=Unordered)
251      * @throws XmlPullParserException
252      * if the batch request has not been parsed yet
253      */

254     public static boolean isRequestIdNeeded( Dsmlv2Container container ) throws XmlPullParserException
255     {
256         BatchRequest batchRequest = container.getBatchRequest();
257
258         if ( batchRequest == null )
259         {
260             throw new XmlPullParserException( "unable to find the batch request", container.getParser(), null );
261         }
262
263         return ( ( batchRequest.getProcessing() == Processing.PARALLEL ) && ( batchRequest.getResponseOrder() == ResponseOrder.UNORDERED ) );
264     }
265
266
267     /**
268      * XML Pretty Printer XSLT Tranformation
269      *
270      * @param document
271      * the Dom4j Document
272      * @return
273      */

274     public static Document styleDocument( Document document )
275     {
276         // load the transformer using JAXP
277
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
278         Transformer JavaDoc transformer = null;
279         try
280         {
281             transformer = factory.newTransformer( new StreamSource JavaDoc( Dsmlv2Engine.class
282                 .getResourceAsStream( "DSMLv2.xslt" ) ) );
283         }
284         catch ( TransformerConfigurationException JavaDoc e1 )
285         {
286             // TODO Auto-generated catch block
287
e1.printStackTrace();
288         }
289
290         // now lets style the given document
291
DocumentSource source = new DocumentSource( document );
292         DocumentResult result = new DocumentResult();
293         try
294         {
295             transformer.transform( source, result );
296         }
297         catch ( TransformerException JavaDoc e )
298         {
299             // TODO Auto-generated catch block
300
e.printStackTrace();
301         }
302
303         // return the transformed document
304
Document transformedDoc = result.getDocument();
305         return transformedDoc;
306     }
307 }
308
Popular Tags