Index: src/org/opencms/jsp/CmsJspTagContentShow.java =================================================================== RCS file: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagContentShow.java,v retrieving revision 1.3 diff -u -r1.3 CmsJspTagContentShow.java --- src/org/opencms/jsp/CmsJspTagContentShow.java 17 Nov 2004 12:16:59 -0000 1.3 +++ src/org/opencms/jsp/CmsJspTagContentShow.java 18 Nov 2004 21:16:58 -0000 @@ -40,11 +40,15 @@ import org.opencms.xml.CmsXmlException; import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import java.util.Locale; import javax.servlet.ServletRequest; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspTagException; +import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.Tag; import javax.servlet.jsp.tagext.TagSupport; @@ -52,15 +56,30 @@ * Used to access and display XML content item information from the VFS.

* * @author Alexander Kandzior (a.kandzior@alkacon.com) + * @author Stephan Hartmann (hartmann@metamesh.de) * * @version $Revision: 1.3 $ * @since 5.5.0 */ public class CmsJspTagContentShow extends TagSupport { + // some constants for the scpe + public static final String C_PAGE = "page"; + public static final String C_REQUEST = "request"; + public static final String C_SESSION = "session"; + public static final String C_APPLICATION = "application"; + /** Name of the content node element to show. */ private String m_element; + /** Name of the scoped var */ + private String var; + + /** The scope where the var is to be stored */ + private int scope = PageContext.PAGE_SCOPE; + + private boolean scopeSpecified; + /** * Internal action method to show an element from a XML content document.

* @@ -69,9 +88,9 @@ * @param locale the locale of the element to show * @param req the current request * - * @return the value of the selected content element + * @return the value(s) of the selected content element */ - public static String contentShowTagAction( + public static Object contentShowTagAction( A_CmsXmlDocument content, String element, Locale locale, @@ -82,24 +101,67 @@ return null; } - if (content.hasValue(element, locale)) { - - // selected element is available in content - CmsFlexController controller = (CmsFlexController)req.getAttribute(CmsFlexController.ATTRIBUTE_NAME); - CmsObject cms = controller.getCmsObject(); - - try { - // read the element from the content - return content.getStringValue(cms, element, locale); - } catch (CmsXmlException e) { - OpenCms.getLog(CmsJspTagContentShow.class).error( - "Error processing content element '" + element, - e); - return null; - } - } else { + Object result; + CmsFlexController controller = (CmsFlexController)req.getAttribute(CmsFlexController.ATTRIBUTE_NAME); + CmsObject cms = controller.getCmsObject(); + + if (element == null) { + // iterate over all elements and return a map + List names = content.getDistinctNames(locale); + result = new HashMap(names.size()); + for (int i = 0; i < names.size(); i++) { + String name = (String)names.get(i); + ((HashMap)result).put(name, + getElements(cms, content, name, locale)); + } + return result; + + } + else { + return getElements(cms, content, element, locale); + } + } + /** + * this method either returns a String if element occurs one time + * or a list of Strings if element occurs multiple times + * + * @return Object either null or String or List + */ + private static Object getElements(CmsObject cms, + A_CmsXmlDocument content, + String element, + Locale locale) { + int size; + if (element.indexOf(("[")) > 0) size = 1; + else + size = content.getIndexCount(element, locale); + if (size == 0) return null; + if (size == 1) { + + try { + // read the element from the content + return content.getStringValue(cms, element, locale); + } catch (CmsXmlException e) { + OpenCms.getLog(CmsJspTagContentShow.class).error( + "Error processing content element '" + element, + e); return null; } + } + else { + ArrayList result = new ArrayList(size); + for (int i = 0; i < size; i++) { + try { + // read the element from the content + result.add(content.getStringValue(cms, element+"["+i+"]", locale)); + } catch (CmsXmlException e) { + OpenCms.getLog(CmsJspTagContentShow.class).error( + "Error processing content element '" + element, + e); + } + } + return result; + } } /** @@ -124,8 +186,8 @@ element = contentContainer.getXmlDocumentElement(); } - String content; - if (element.startsWith(I_CmsJspTagContentContainer.C_MAGIC_PREFIX)) { + Object content; + if ((element != null) && element.startsWith(I_CmsJspTagContentContainer.C_MAGIC_PREFIX)) { // this is a "magic" element name, resolve it content = contentContainer.resolveMagicName(element); @@ -140,13 +202,30 @@ } } - try { + // if there is a var store the content there, otherwise print it out + if (var != null) { + if (content != null) { + pageContext.setAttribute(var, content, scope); + } else { + if (scopeSpecified) + pageContext.removeAttribute(var, scope); + else + pageContext.removeAttribute(var); + } + } + else { + // print directly to the output + try { + // if we have a list only take the first element + if (content instanceof List) + content = ((List)content).get(0); pageContext.getOut().print(content); - } catch (IOException e) { + } catch (IOException e) { if (OpenCms.getLog(this).isErrorEnabled()) { OpenCms.getLog(this).error("Error in Jsp tag processing", e); } throw new JspException(e); + } } return SKIP_BODY; @@ -169,6 +248,9 @@ super.release(); m_element = null; + var = null; + scope = PageContext.PAGE_SCOPE; + scopeSpecified = false; } /** @@ -180,4 +262,35 @@ m_element = element; } + + /** + * @return Returns the name of the scoped var. + */ + public String getVar() { + return var; + } + + /** + * @param var The name of the scoped var to set. + */ + public void setVar(String var) { + this.var = var; + } + + /** + * + * @param scope String value of scope where the var is to be stored + */ + public void setScope(String scope) { + if (C_REQUEST.equalsIgnoreCase(scope)) + this.scope = PageContext.REQUEST_SCOPE; + else if (C_SESSION.equalsIgnoreCase(scope)) + this.scope = PageContext.SESSION_SCOPE; + else if (C_APPLICATION.equalsIgnoreCase(scope)) + this.scope = PageContext.APPLICATION_SCOPE; + else + this.scope = PageContext.PAGE_SCOPE; + + this.scopeSpecified = true; + } } \ No newline at end of file Index: src/org/opencms/xml/A_CmsXmlDocument.java =================================================================== RCS file: /usr/local/cvs/opencms/src/org/opencms/xml/A_CmsXmlDocument.java,v retrieving revision 1.11 diff -u -r1.11 A_CmsXmlDocument.java --- src/org/opencms/xml/A_CmsXmlDocument.java 17 Nov 2004 12:16:59 -0000 1.11 +++ src/org/opencms/xml/A_CmsXmlDocument.java 18 Nov 2004 21:17:00 -0000 @@ -57,6 +57,7 @@ * with XML documents that are stored in the OpenCms VFS.

* * @author Alexander Kandzior (a.kandzior@alkacon.com) + * @author Stephan Hartmann (hartmann@metamesh.de) * * @version $Revision: 1.11 $ * @since 5.3.5 @@ -87,6 +88,9 @@ /** Set of locales contained in this document. */ protected Set m_locales; + /** Maps locale to distinct names in this document. */ + protected Map m_distinctNames; + /** Reference for named elements in the document. */ private Map m_bookmarks; @@ -525,7 +529,7 @@ // add the locale to all locales in this dcoument m_locales.add(locale); - + // add a bookmark to the provided value m_bookmarks.put(getBookmarkName(name, locale), value); @@ -554,6 +558,30 @@ } /** + * Adds a element name to the distinct set of names for the given locale

+ * + * @param name the name of the element + * @param locale the locale to use for the element name + * @param enabled if true, the value is enabled, if false it is disabled + */ + protected void addDistinctName(String name, Locale locale, boolean enabled) { + Object o = m_distinctNames.get(locale); + if (o != null) { + ((Set)o).add(name); + } else { + Set set = new HashSet(); + set.add(name); + m_distinctNames.put(locale, set); + } + } + + public List getDistinctNames(Locale locale) { + Set set = (Set)m_distinctNames.get(locale); + if (set != null) + return (List)new ArrayList(set); + else return Collections.EMPTY_LIST; + } + /** * Returns the bookmarked value for the given name.

* * @param name the name to get the bookmark for Index: src/org/opencms/xml/content/CmsXmlContent.java =================================================================== RCS file: /usr/local/cvs/opencms/src/org/opencms/xml/content/CmsXmlContent.java,v retrieving revision 1.6 diff -u -r1.6 CmsXmlContent.java --- src/org/opencms/xml/content/CmsXmlContent.java 8 Nov 2004 15:06:43 -0000 1.6 +++ src/org/opencms/xml/content/CmsXmlContent.java 18 Nov 2004 21:17:01 -0000 @@ -197,6 +197,7 @@ m_encoding = CmsEncoder.lookupEncoding(encoding, encoding); m_elementLocales = new HashMap(); m_elementNames = new HashMap(); + m_distinctNames = new HashMap(); // initialize the bookmarks for (Iterator i = m_document.getRootElement().elementIterator(); i.hasNext();) { @@ -267,6 +268,7 @@ // directly add simple type to schema I_CmsXmlContentValue value = schemaType.createValue(element, name, count); addBookmark(path, locale, true, value); + addDistinctName(name, locale, true); } else { // recurse for cascaded schema CmsXmlCascadedContentDefinition cascadedSchema = (CmsXmlCascadedContentDefinition)schemaType; Index: webapp/WEB-INF/opencms.tld =================================================================== RCS file: /usr/local/cvs/opencms/webapp/WEB-INF/opencms.tld,v retrieving revision 1.9 diff -u -r1.9 opencms.tld --- webapp/WEB-INF/opencms.tld 17 Nov 2004 12:16:59 -0000 1.9 +++ webapp/WEB-INF/opencms.tld 18 Nov 2004 21:17:03 -0000 @@ -128,6 +128,12 @@ element + + + var + + + scope