1 20 21 package org.apache.directory.ldapstudio.dsmlv2; 22 23 24 import java.util.List ; 25 26 import javax.xml.transform.Transformer ; 27 import javax.xml.transform.TransformerConfigurationException ; 28 import javax.xml.transform.TransformerException ; 29 import javax.xml.transform.TransformerFactory ; 30 import javax.xml.transform.stream.StreamSource ; 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 55 public class ParserUtils 56 { 57 public static final String XML_SCHEMA_URI = "http://www.w3c.org/2001/XMLSchema"; 58 public static final String XML_SCHEMA_INSTANCE_URI = "http://www.w3c.org/2001/XMLSchema-instance"; 59 public static final String BASE64BINARY = "base64Binary"; 60 public static final String XSI = "xsi"; 61 public static final String XSD = "xsd"; 62 63 64 72 public static String getXsiTypeAttributeValue( XmlPullParser xpp ) 73 { 74 String type = null; 75 int nbAttributes = xpp.getAttributeCount(); 76 for ( int i = 0; i < nbAttributes; i++ ) 77 { 78 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 100 public static boolean isBase64BinaryValue( XmlPullParser parser, String attrValue ) 101 { 102 if ( attrValue == null ) 103 { 104 return false; 105 } 106 String [] 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 122 public static boolean needsBase64Encoding( Object value ) 123 { 124 if ( value instanceof byte[] ) 125 { 126 return true; 127 } 128 else if ( value instanceof String ) 129 { 130 return !LdifUtils.isLDIFSafe( ( String ) value ); 131 } 132 return true; 133 } 134 135 136 144 public static String base64Encode( Object value ) 145 { 146 if ( value instanceof byte[] ) 147 { 148 return new String ( Base64.encode( ( byte[] ) value ) ); 149 } 150 else if ( value instanceof String ) 151 { 152 return new String ( Base64.encode( ( ( String ) value ).getBytes() ) ); 153 } 154 155 return ""; 156 } 157 158 159 171 public static int parseAndVerifyRequestID( String 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 e ) 185 { 186 throw new XmlPullParserException( "the given requestID is not an integer", xpp, null ); 187 } 188 } 189 190 191 199 public static void addControls( Element element, List <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 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 ) value ); 237 } 238 } 239 } 240 } 241 } 242 243 244 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 274 public static Document styleDocument( Document document ) 275 { 276 TransformerFactory factory = TransformerFactory.newInstance(); 278 Transformer transformer = null; 279 try 280 { 281 transformer = factory.newTransformer( new StreamSource ( Dsmlv2Engine.class 282 .getResourceAsStream( "DSMLv2.xslt" ) ) ); 283 } 284 catch ( TransformerConfigurationException e1 ) 285 { 286 e1.printStackTrace(); 288 } 289 290 DocumentSource source = new DocumentSource( document ); 292 DocumentResult result = new DocumentResult(); 293 try 294 { 295 transformer.transform( source, result ); 296 } 297 catch ( TransformerException e ) 298 { 299 e.printStackTrace(); 301 } 302 303 Document transformedDoc = result.getDocument(); 305 return transformedDoc; 306 } 307 } 308 | Popular Tags |