1 23 24 package org.apache.slide.common; 25 26 import java.io.IOException ; 27 import java.io.StringReader ; 28 import java.util.Enumeration ; 29 30 import javax.xml.parsers.SAXParser ; 31 import javax.xml.parsers.SAXParserFactory ; 32 33 import org.apache.slide.content.NodeProperty; 34 import org.apache.slide.content.NodeRevisionContent; 35 import org.apache.slide.content.NodeRevisionDescriptor; 36 import org.apache.slide.content.NodeRevisionDescriptors; 37 import org.apache.slide.content.RevisionAlreadyExistException; 38 import org.apache.slide.content.RevisionContentNotFoundException; 39 import org.apache.slide.content.RevisionDescriptorNotFoundException; 40 import org.apache.slide.content.RevisionNotFoundException; 41 import org.apache.slide.event.VetoException; 42 import org.apache.slide.lock.ObjectLockedException; 43 import org.apache.slide.security.AccessDeniedException; 44 import org.apache.slide.security.NodePermission; 45 import org.apache.slide.structure.ActionNode; 46 import org.apache.slide.structure.LinkNode; 47 import org.apache.slide.structure.LinkedObjectNotFoundException; 48 import org.apache.slide.structure.ObjectAlreadyExistsException; 49 import org.apache.slide.structure.ObjectNode; 50 import org.apache.slide.structure.ObjectNotFoundException; 51 import org.apache.slide.structure.SubjectNode; 52 import org.apache.slide.util.conf.Configuration; 53 import org.apache.slide.util.conf.ConfigurationElement; 54 import org.apache.slide.util.conf.ConfigurationException; 55 import org.apache.slide.util.conf.Populate; 56 import org.apache.slide.util.logger.Logger; 57 import org.xml.sax.InputSource ; 58 import org.xml.sax.SAXException ; 59 60 61 62 67 public final class XMLUnmarshaller { 68 69 private final static String LOG_CHANNEL = XMLUnmarshaller.class.getName(); 70 71 73 74 84 public static void unmarshal(NamespaceAccessToken accessToken, 85 SlideToken token, 86 Configuration dataConfiguration) 87 throws ConfigurationException, UnknownObjectClassException, 88 ServiceAccessException { 89 90 loadObjectNode(accessToken, token, 91 dataConfiguration.getConfiguration("objectnode")); 92 93 } 94 95 96 98 99 110 private static void loadObjectNode 111 (NamespaceAccessToken accessToken, SlideToken token, 112 Configuration objectDefinition) 113 throws ServiceAccessException, ConfigurationException, 114 UnknownObjectClassException { 115 116 String className = objectDefinition.getAttribute("classname"); 117 String uri = objectDefinition.getAttribute("uri"); 118 119 accessToken.getLogger().log("Loading object " + uri, 120 LOG_CHANNEL,Logger.INFO); 121 122 try { 123 124 Class objectClass = null; 125 try { 126 objectClass = Class.forName(className); 128 } catch (ClassNotFoundException e) { 129 throw new UnknownObjectClassException(className); 132 } 133 ObjectNode object = null; 134 try { 135 object = (ObjectNode) objectClass.newInstance(); 137 } catch(InstantiationException e) { 138 throw new UnknownObjectClassException(className); 140 } catch(IllegalAccessException e) { 141 throw new UnknownObjectClassException(className); 144 } 145 146 try { 147 if (object instanceof LinkNode) { 148 String linkedUri = 149 objectDefinition.getAttribute("linkedUri"); 150 accessToken.getStructureHelper().createLink 151 (token, (LinkNode) object, uri, 152 new SubjectNode(linkedUri)); 153 } else { 154 accessToken.getStructureHelper().create 155 (token, object, uri); 156 } 157 } catch (ObjectAlreadyExistsException e) { 158 accessToken.getLogger().log 160 ("Object already exists at " + uri, 161 LOG_CHANNEL, Logger.INFO); 162 } 163 164 Enumeration permissionDefinitions = 166 objectDefinition.getConfigurations("permission"); 167 168 while (permissionDefinitions.hasMoreElements()) { 172 173 Configuration permissionDefinition = 174 (Configuration) permissionDefinitions.nextElement(); 175 177 String subjectUri = 178 permissionDefinition.getAttribute("subject"); 179 180 if (accessToken.getNamespaceConfig().getUsersPath().equals(subjectUri)) { 181 subjectUri = SubjectNode.ALL_URI; 182 } 183 184 String actionUri = 185 permissionDefinition.getAttribute("action"); 186 187 if (accessToken.getNamespaceConfig().getActionsPath().equals(actionUri)) { 188 actionUri = ActionNode.ALL_URI; 189 } 190 191 boolean inheritable = true; 192 193 try { 194 if (permissionDefinition.getAttribute("inheritable") 195 .equals("false")) { 196 inheritable = false; 197 } 198 } catch (ConfigurationException e) { 199 } 200 201 boolean negative = false; 202 203 try { 204 if (permissionDefinition.getAttribute("negative") 205 .equals("true")) { 206 negative = true; 207 } 208 } catch (ConfigurationException e) { 209 } 210 211 NodePermission permission = new NodePermission 212 (uri, subjectUri, actionUri, inheritable, negative); 213 214 accessToken.getSecurityHelper() 216 .grantPermission(token, permission); 217 218 } 219 220 Enumeration revisionDefinitions = 222 objectDefinition.getConfigurations("revision"); 223 boolean revisionDefinitionsFound = false; 224 225 while (revisionDefinitions.hasMoreElements()) { 227 228 revisionDefinitionsFound = true; 229 Configuration revisionDefinition = 230 (Configuration) revisionDefinitions.nextElement(); 231 loadObjectRevision(accessToken, token, uri, 232 revisionDefinition); 233 234 } 235 236 if (!revisionDefinitionsFound) { 237 loadDefaultObjectRevision(accessToken, token, uri); 238 } 239 240 241 } catch (ObjectNotFoundException e) { 242 accessToken.getLogger().log(e,LOG_CHANNEL,Logger.WARNING); 244 accessToken.getLogger().log 245 (e.toString(),LOG_CHANNEL,Logger.WARNING); 246 } catch (VetoException e) { 247 accessToken.getLogger().log(e,LOG_CHANNEL,Logger.WARNING); 248 accessToken.getLogger().log(e.toString(),LOG_CHANNEL,Logger.WARNING); 249 } catch (LinkedObjectNotFoundException e) { 250 accessToken.getLogger().log(e,LOG_CHANNEL,Logger.WARNING); 252 accessToken.getLogger().log 253 ("Incorrect link found while creating " + uri, 254 LOG_CHANNEL,Logger.WARNING); 255 } catch (AccessDeniedException e) { 256 accessToken.getLogger().log 258 ("Insufficient credentials to create object", 259 LOG_CHANNEL,Logger.INFO); 260 } catch (ObjectLockedException e) { 261 accessToken.getLogger().log 263 ("Lock-Token required", 264 LOG_CHANNEL,Logger.INFO); 265 } 266 267 268 Enumeration childConfigurations = 269 objectDefinition.getConfigurations("objectnode"); 270 271 while(childConfigurations.hasMoreElements()) { 272 Configuration childConfiguration = 273 (Configuration) childConfigurations.nextElement(); 274 loadObjectNode(accessToken, token, childConfiguration); 275 } 276 277 } 278 279 280 281 289 private static void loadDefaultObjectRevision(NamespaceAccessToken accessToken, 290 SlideToken token, String uri) 291 throws ServiceAccessException, ConfigurationException, 292 AccessDeniedException, ObjectNotFoundException, 293 LinkedObjectNotFoundException, VetoException { 294 295 296 try { 297 SAXParserFactory factory = SAXParserFactory.newInstance(); 298 factory.setNamespaceAware(false); 299 factory.setValidating(false); 300 SAXParser parser = factory.newSAXParser(); 301 Populate pop = new Populate(); 302 Configuration slideConfiguration = 303 new ConfigurationElement( 304 pop.load( 305 new InputSource ( 306 new StringReader ("<revision/>")), parser.getXMLReader())); 307 loadObjectRevision(accessToken, token, uri, slideConfiguration); 308 } 309 catch (IOException e) { e.printStackTrace(); } 310 catch (SAXException e) { e.printStackTrace(); } 311 catch (javax.xml.parsers.ParserConfigurationException e) {e.printStackTrace(); } 312 } 313 314 315 316 324 private static void loadObjectRevision(NamespaceAccessToken accessToken, 325 SlideToken token, String uri, 326 Configuration revisionDefinition) 327 throws ServiceAccessException, ConfigurationException, 328 AccessDeniedException, ObjectNotFoundException, 329 LinkedObjectNotFoundException, VetoException { 330 331 Enumeration propertyDefinitions = 333 revisionDefinition.getConfigurations("property"); 334 335 NodeRevisionDescriptor revisionDescriptor = null; 337 338 NodeRevisionDescriptors revisionDescriptors = null; 339 try { 340 revisionDescriptors = accessToken.getContentHelper().retrieve 341 (token, uri); 342 } catch (ObjectLockedException e) { 343 } 345 if ((revisionDescriptors != null) 346 && (revisionDescriptors.hasRevisions())) { 347 try { 348 revisionDescriptor = accessToken.getContentHelper().retrieve 349 (token, revisionDescriptors); 350 } catch (RevisionDescriptorNotFoundException e) { 351 } catch (ObjectLockedException e) { 353 } 355 } 356 if (revisionDescriptor == null) { 357 revisionDescriptor = new NodeRevisionDescriptor(0); 358 } 359 360 while (propertyDefinitions.hasMoreElements()) { 361 Configuration propertyDefinition = 362 (Configuration) propertyDefinitions.nextElement(); 363 String propertyName = propertyDefinition.getAttribute("name"); 364 String propertyValue = propertyDefinition.getValue(); 365 String propertyNamespace = propertyDefinition.getAttribute 366 ("namespace", NodeProperty.DEFAULT_NAMESPACE); 367 NodeProperty property = revisionDescriptor.getProperty 368 (propertyName, propertyNamespace); 369 if (property == null) 370 revisionDescriptor.setProperty(propertyName, propertyNamespace, 371 propertyValue); 372 } 373 374 NodeRevisionContent revisionContent = null; 375 376 if ((revisionDescriptors != null) 377 && (revisionDescriptors.hasRevisions())) { 378 379 try { 380 revisionContent = accessToken.getContentHelper() 381 .retrieve(token, uri, revisionDescriptor); 382 } catch (RevisionContentNotFoundException e) { 383 } catch (ObjectLockedException e) { 385 } catch (RevisionNotFoundException e) { 387 } 389 try { 390 accessToken.getContentHelper().store 391 (token, uri, revisionDescriptor, revisionContent); 392 } catch (RevisionDescriptorNotFoundException e) { 393 accessToken.getLogger().log(e,LOG_CHANNEL,Logger.WARNING); 395 accessToken.getLogger().log 396 (e.toString(),LOG_CHANNEL,Logger.WARNING); 397 } catch (RevisionNotFoundException e) { 398 accessToken.getLogger().log(e,LOG_CHANNEL,Logger.WARNING); 400 accessToken.getLogger().log 401 (e.toString(),LOG_CHANNEL,Logger.WARNING); 402 } catch (ObjectLockedException e) { 403 } 405 406 } else { 407 408 try { 409 accessToken.getContentHelper().create 410 (token, uri, revisionDescriptor, revisionContent); 411 } catch(ObjectLockedException e) { 412 accessToken.getLogger().log(e,LOG_CHANNEL,Logger.WARNING); 414 accessToken.getLogger().log 415 (e.toString(),LOG_CHANNEL,Logger.WARNING); 416 } catch(RevisionAlreadyExistException e) { 417 accessToken.getLogger().log(e,LOG_CHANNEL,Logger.WARNING); 419 accessToken.getLogger().log 420 (e.toString(),LOG_CHANNEL,Logger.WARNING); 421 } 422 } 423 } 424 } 425 | Popular Tags |