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: package org.apache.jetspeed.tools.pamanager.rules;
018:
019: import java.util.Locale;
020:
021: import org.apache.commons.digester.Rule;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: import org.apache.jetspeed.om.common.GenericMetadata;
027: import org.apache.jetspeed.om.common.LocalizedField;
028: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
029: import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
030:
031: import org.xml.sax.Attributes;
032:
033: /**
034: * This class helps load internationalized fields
035: *
036: * @author <a href="mailto:jford@apache.org">Jeremy Ford </a>
037: * @version $Id: LocalizedFieldRule.java 516448 2007-03-09 16:25:47Z ate $
038: */
039: public class LocalizedFieldRule extends Rule {
040: protected final static Log log = LogFactory
041: .getLog(LocalizedFieldRule.class);
042:
043: /**
044: * Handle the beginning of an XML element.
045: *
046: * @param attributes
047: * The attributes of this element
048: * @exception Exception
049: * if a processing error occurs
050: */
051: public void begin(String namespace, String name,
052: Attributes attributes) throws Exception {
053:
054: if (digester.getLogger().isDebugEnabled())
055: digester.getLogger().debug(
056: "Setting localized field " + name);
057:
058: Object obj = digester.peek();
059: if (null == obj) {
060: digester.push(null);
061: return;
062: }
063: GenericMetadata metadata = null;
064: if (obj instanceof MutablePortletApplication) {
065: metadata = ((MutablePortletApplication) obj).getMetadata();
066: }
067: if (obj instanceof PortletDefinitionComposite) {
068: metadata = ((PortletDefinitionComposite) obj).getMetadata();
069: }
070: if (metadata != null) {
071: LocalizedField child = metadata.createLocalizedField();
072:
073: if (name.equals("metadata")) {
074: String nameAttr = attributes.getValue("name");
075: child.setName(nameAttr);
076: } else {
077: child.setName(name);
078: }
079: String language = attributes.getValue("xml:lang");
080: Locale locale = null;
081: if (language == null) {
082: locale = new Locale("en");
083: } else {
084: locale = new Locale(language);
085: }
086:
087: child.setLocale(locale);
088: digester.push(child);
089: } else {
090: digester.push(null);
091: }
092: }
093:
094: public void body(String namespace, String name, String text)
095: throws Exception {
096: LocalizedField child = (LocalizedField) digester.peek(0);
097: if (child != null) {
098: child.setValue(text);
099: }
100: }
101:
102: public void end(String namespace, String name) throws Exception {
103: LocalizedField child = (LocalizedField) digester.pop();
104: if (child != null) {
105: Object obj = digester.peek();
106: if (null == obj) {
107: digester.push(null);
108: return;
109: }
110: GenericMetadata metadata = null;
111: if (obj instanceof MutablePortletApplication) {
112: metadata = ((MutablePortletApplication) obj)
113: .getMetadata();
114: }
115: if (obj instanceof PortletDefinitionComposite) {
116: metadata = ((PortletDefinitionComposite) obj)
117: .getMetadata();
118: }
119: if (null != metadata) {
120: metadata.addField(child);
121: }
122: }
123: }
124: }
|