001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.lenya.cms.ac.usecase.impl;
020:
021: import java.io.InputStream;
022: import java.util.HashSet;
023: import java.util.Set;
024:
025: import org.apache.avalon.framework.service.ServiceManager;
026: import org.apache.lenya.ac.AccessController;
027: import org.apache.lenya.ac.cache.BuildException;
028: import org.apache.lenya.ac.cache.InputStreamBuilder;
029: import org.apache.lenya.cms.cocoon.source.SourceUtil;
030: import org.apache.lenya.util.Assert;
031: import org.apache.lenya.xml.DocumentHelper;
032: import org.apache.lenya.xml.NamespaceHelper;
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Element;
035:
036: /**
037: * Builder for usecase roles.
038: *
039: * @version $Id: UsecaseRolesBuilder.java 562973 2007-08-05 21:44:42Z nettings $
040: */
041: public class UsecaseRolesBuilder implements InputStreamBuilder {
042:
043: protected static final String USECASES_ELEMENT = "usecases";
044: protected static final String USECASE_ELEMENT = "usecase";
045: protected static final String ROLE_ELEMENT = "role";
046: protected static final String ID_ATTRIBUTE = "id";
047:
048: /**
049: * @see org.apache.lenya.ac.cache.InputStreamBuilder#build(java.io.InputStream)
050: */
051: public Object build(InputStream stream) throws BuildException {
052:
053: UsecaseRoles usecaseRoles = new UsecaseRoles();
054:
055: Document document;
056: try {
057: document = DocumentHelper.readDocument(stream);
058: } catch (Exception e) {
059: throw new BuildException(e);
060: }
061: Assert.isTrue("Correct usecase policies XML", document
062: .getDocumentElement().getLocalName().equals(
063: USECASES_ELEMENT));
064:
065: NamespaceHelper helper = new NamespaceHelper(
066: AccessController.NAMESPACE,
067: AccessController.DEFAULT_PREFIX, document);
068:
069: Element[] usecaseElements = helper.getChildren(document
070: .getDocumentElement(), USECASE_ELEMENT);
071: for (int i = 0; i < usecaseElements.length; i++) {
072: String usecaseId = usecaseElements[i]
073: .getAttribute(ID_ATTRIBUTE);
074:
075: // add roles only if not overridden by child publication
076: if (!usecaseRoles.hasRoles(usecaseId)) {
077: Element[] roleElements = helper.getChildren(
078: usecaseElements[i], ROLE_ELEMENT);
079: Set roleIds = new HashSet();
080: for (int j = 0; j < roleElements.length; j++) {
081: String roleId = roleElements[j]
082: .getAttribute(ID_ATTRIBUTE);
083: roleIds.add(roleId);
084: }
085: String[] roleIdArray = (String[]) roleIds
086: .toArray(new String[roleIds.size()]);
087: usecaseRoles.setRoles(usecaseId, roleIdArray);
088: }
089: }
090: return usecaseRoles;
091: }
092:
093: /**
094: * Saves the usecase roles.
095: * @param usecaseRoles The roles.
096: * @param sourceUri The source to save to.
097: * @param manager The service manager.
098: * @throws BuildException if an error occurs.
099: */
100: public void save(UsecaseRoles usecaseRoles, String sourceUri,
101: ServiceManager manager) throws BuildException {
102: try {
103: NamespaceHelper helper = new NamespaceHelper(
104: AccessController.NAMESPACE,
105: AccessController.DEFAULT_PREFIX, USECASES_ELEMENT);
106: String[] usecaseNames = usecaseRoles.getUsecaseNames();
107: for (int u = 0; u < usecaseNames.length; u++) {
108: Element usecaseElement = helper
109: .createElement(USECASE_ELEMENT);
110: helper.getDocument().getDocumentElement().appendChild(
111: usecaseElement);
112: usecaseElement.setAttribute(ID_ATTRIBUTE,
113: usecaseNames[u]);
114: String[] roles = usecaseRoles.getRoles(usecaseNames[u]);
115: for (int r = 0; r < roles.length; r++) {
116: Element roleElement = helper
117: .createElement(ROLE_ELEMENT);
118: usecaseElement.appendChild(roleElement);
119: roleElement.setAttribute(ID_ATTRIBUTE, roles[r]);
120: }
121: }
122: SourceUtil.writeDOM(helper.getDocument(), sourceUri,
123: manager);
124: } catch (Exception e) {
125: throw new BuildException(e);
126: }
127: }
128:
129: }
|