/** Helper to read an optional String value list. * @param path The XML path of the element to read. * @return The String list stored in the XML, or <code>null</code> if the value could not be read. */ protected List<String> parseOptionalStringValues(final String path) { final List<I_CmsXmlContentValue> values = m_xml.getValues(path, m_locale); if (values == null) { return null; } else { List<String> stringValues = new ArrayList<String>(values.size()); for (I_CmsXmlContentValue value : values) { stringValues.add(value.getStringValue(null)); } return stringValues; } }
/** * Generates a new content sequence element from the given type, content and content definition.<p> * * @param path the path in the document to generate the value sequence for * @param schemaType the schema type to generate the sequence element for * @param locale the locale to get the content values from * @param content the XML content to generate the sequence element out of */ public CmsXmlContentValueSequence(String path, I_CmsXmlSchemaType schemaType, Locale locale, CmsXmlContent content) { m_schemaType = schemaType; m_locale = locale; m_content = content; m_values = m_content.getValues(path, m_locale); m_path = CmsXmlUtils.removeXpathIndex(path); }
/** * Returns all values of the given element path.<p> * * @param elementPath the element path * @param locale the content locale * * @return the values */ public List<I_CmsXmlContentValue> getValuesByPath(String elementPath, Locale locale) { String[] pathElements = elementPath.split("/"); List<I_CmsXmlContentValue> values = getValues(pathElements[0], locale); for (int i = 1; i < pathElements.length; i++) { List<I_CmsXmlContentValue> subValues = new ArrayList<I_CmsXmlContentValue>(); for (I_CmsXmlContentValue value : values) { subValues.addAll(getValues(CmsXmlUtils.concatXpath(value.getPath(), pathElements[i]), locale)); } if (subValues.isEmpty()) { values = Collections.emptyList(); break; } values = subValues; } return values; }
/** * Removes all surplus values of locale independent fields in the other locales.<p> * * @param elementPath the element path * @param valueCount the value count * @param sourceLocale the source locale */ private void removeSurplusValuesInOtherLocales(String elementPath, int valueCount, Locale sourceLocale) { for (Locale locale : getLocales()) { if (locale.equals(sourceLocale)) { continue; } List<I_CmsXmlContentValue> localeValues = getValues(elementPath, locale); for (int i = valueCount; i < localeValues.size(); i++) { removeValue(elementPath, locale, 0); } } }
/** Helper to read a mandatory String value list. * @param path The XML path of the element to read. * @return The String list stored in the XML, or <code>null</code> if the value could not be read. * @throws Exception thrown if the list of String values can not be read. */ protected List<I_CmsFacetQueryItem> parseFacetQueryItems(final String path) throws Exception { final List<I_CmsXmlContentValue> values = m_xml.getValues(path, m_locale); if (values == null) { return null; } else { List<I_CmsFacetQueryItem> parsedItems = new ArrayList<I_CmsFacetQueryItem>(values.size()); for (I_CmsXmlContentValue value : values) { I_CmsFacetQueryItem item = parseFacetQueryItem(value.getPath() + "/"); if (null != item) { parsedItems.add(item); } else { // TODO: log } } return parsedItems; } }
/** * Returns the content values of the requested locale.<p> * * @param content the content document * @param locale the content locale * * @return the values */ protected Map<String, String> getContentValues(CmsXmlContent content, Locale locale) { Map<String, String> result = new HashMap<String, String>(); List<I_CmsXmlContentValue> values = content.getValues(locale); for (I_CmsXmlContentValue value : values) { if (value.isSimpleType()) { result.put(value.getPath(), value.getStringValue(m_cms)); } } return result; }
/** Returns a map with additional request parameters, mapping the parameter names to Solr query parts. * @return A map with additional request parameters, mapping the parameter names to Solr query parts. */ private Map<String, String> getAdditionalRequestParameters() { List<I_CmsXmlContentValue> parametersToParse = m_xml.getValues(XML_ELEMENT_ADDITIONAL_PARAMETERS, m_locale); Map<String, String> result = new HashMap<String, String>(parametersToParse.size()); for (I_CmsXmlContentValue additionalParam : parametersToParse) { String param = m_xml.getValue( additionalParam.getPath() + "/" + XML_ELEMENT_ADDITIONAL_PARAMETERS_PARAM, m_locale).getStringValue(null); String solrQuery = m_xml.getValue( additionalParam.getPath() + "/" + XML_ELEMENT_ADDITIONAL_PARAMETERS_SOLRQUERY, m_locale).getStringValue(null); result.put(param, solrQuery); } return result; }
/** * Reads the referenced formatters.<p> * * @param xmlContent the XML content * * @return the referenced formatters */ private Map<String, CmsUUID> readReferencedFormatters(CmsXmlContent xmlContent) { Map<String, CmsUUID> result = new LinkedHashMap<String, CmsUUID>(); List<I_CmsXmlContentValue> formatters = xmlContent.getValues( CmsMacroFormatterResolver.N_FORMATTERS, CmsLocaleManager.MASTER_LOCALE); for (I_CmsXmlContentValue formatterValue : formatters) { CmsXmlVfsFileValue file = (CmsXmlVfsFileValue)xmlContent.getValue( formatterValue.getPath() + "/" + CmsMacroFormatterResolver.N_FORMATTER, CmsLocaleManager.MASTER_LOCALE); CmsUUID formatterId = file.getLink(m_cms).getStructureId(); String macroName = xmlContent.getStringValue( m_cms, formatterValue.getPath() + "/" + CmsMacroFormatterResolver.N_MACRO_NAME, CmsLocaleManager.MASTER_LOCALE); result.put(macroName, formatterId); } return result; }
/** * Removes the value element of the sequence type at the selected index from XML content document.<p> * * @param index the index where to remove the value element * * @see CmsXmlContent#removeValue(String, Locale, int) */ public void removeValue(int index) { m_content.removeValue(getPath(), getLocale(), index); // re-initialize the value list m_values = m_content.getValues(getPath(), getLocale()); } }
/** * Adds a value element of the type indicated by the given xpath * at the selected index to the XML content document.<p> * * @param cms the current users OpenCms context * @param xpath the path that indicates the element type in the content definition * @param index the index where to add the new value element * * @return the added XML content value element * * @see CmsXmlContent#addValue(CmsObject, String, Locale, int) * @see #addValue(CmsObject, I_CmsXmlSchemaType, int) * @see #addValue(CmsObject, int) */ public I_CmsXmlContentValue addValue(CmsObject cms, String xpath, int index) { I_CmsXmlContentValue newValue = m_content.addValue(cms, xpath, getLocale(), index); // re-initialize the value list m_values = m_content.getValues(getPath(), getLocale()); return newValue; }
/** * Removes the value element of the sequence type at the selected index from XML content document.<p> * * @param index the index where to remove the value element * * @see CmsXmlContent#removeValue(String, Locale, int) */ public void removeValue(int index) { m_content.removeValue(getPath(), getLocale(), index); // re-initialize the value list m_values = m_content.getValues(getPath(), getLocale()); } }
/** * @see org.opencms.i18n.CmsVfsResourceBundle.I_Loader#loadData(org.opencms.file.CmsObject, org.opencms.i18n.CmsVfsBundleParameters) */ public Map<Locale, Map<String, String>> loadData(CmsObject cms, CmsVfsBundleParameters params) throws Exception { CmsFile file = cms.readFile(params.getBasePath()); CmsXmlContent content = CmsXmlContentFactory.unmarshal(cms, file); Map<Locale, Map<String, String>> result = Maps.newHashMap(); for (Locale locale : content.getLocales()) { List<I_CmsXmlContentValue> messages = content.getValues(N_MESSAGE, locale); Map<String, String> currentLocale = new HashMap<String, String>(); for (I_CmsXmlContentValue messageValue : messages) { String path = messageValue.getPath(); I_CmsXmlContentValue keyValue = content.getValue(CmsXmlUtils.concatXpath(path, N_KEY), locale); String keyStr = keyValue.getStringValue(cms); // Ignore leading/trailing spaces in the key to protect from user error keyStr = keyStr.trim(); I_CmsXmlContentValue valueValue = content.getValue(CmsXmlUtils.concatXpath(path, N_VALUE), locale); String valueStr = valueValue.getStringValue(cms); currentLocale.put(keyStr, valueStr); } result.put(locale, currentLocale); } return result; } }
/** * Returns the parameters of the collector with resolved macros.<p> * * @return the parameters of the collector with resolved macros */ public String getParameter() { Locale locale = getRequestContext().getLocale(); String params = m_content.getStringValue(getCmsObject(), NODE_PARAMETER, locale); List links = m_content.getValues(NODE_LINKS, locale); CmsMacroResolver macroResolver = CmsMacroResolver.newInstance(); macroResolver.setKeepEmptyMacros(true); for (int i = 0; i < links.size(); i++) { I_CmsXmlContentValue xmlValue = (I_CmsXmlContentValue)links.get(i); String value = xmlValue.getStringValue(getCmsObject()); if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(value)) { StringBuffer macro = new StringBuffer(10); macro.append(MACRO_LINK_PREFIX).append(i + 1); macroResolver.addMacro(macro.toString(), getRequestContext().removeSiteRoot(value)); } } return macroResolver.resolveMacros(params); }
/** * Adds a value element of the sequence type at the selected index to the XML content document.<p> * * @param cms the current users OpenCms context * @param index the index where to add the new value element * * @return the added XML content value element * * @see CmsXmlContent#addValue(CmsObject, String, Locale, int) */ public I_CmsXmlContentValue addValue(CmsObject cms, int index) { I_CmsXmlContentValue newValue = m_content.addValue(cms, getPath(), getLocale(), index); // re-initialize the value list m_values = m_content.getValues(getPath(), getLocale()); return newValue; }
/** * Removes an existing XML content value of the given element name and locale at the given index position * from this XML content document.<p> * * @param name the name of the XML content value element * @param locale the locale where to remove the value * @param index the index where to remove the value (relative to all other values of this type) */ public void removeValue(String name, Locale locale, int index) { // first get the value from the selected locale and index I_CmsXmlContentValue value = getValue(name, locale, index); // check for the min / max occurs constrains List<I_CmsXmlContentValue> values = getValues(name, locale); if (values.size() <= value.getMinOccurs()) { // must not allow removing an element if min occurs would be violated throw new CmsRuntimeException(Messages.get().container( Messages.ERR_XMLCONTENT_ELEM_MINOCCURS_2, name, new Integer(value.getMinOccurs()))); } // detach the value node from the XML document value.getElement().detach(); // re-initialize this XML content initDocument(m_document, m_encoding, m_contentDefinition); }
/** * Returns the parameters of the collector with resolved macros.<p> * * @return the parameters of the collector with resolved macros */ public String getParameter() { Locale locale = getRequestContext().getLocale(); String params = m_content.getStringValue(getCmsObject(), NODE_PARAMETER, locale); List<I_CmsXmlContentValue> links = m_content.getValues(NODE_LINKS, locale); CmsMacroResolver macroResolver = CmsMacroResolver.newInstance().setCmsObject(getCmsObject()); macroResolver.setKeepEmptyMacros(true); for (int i = 0; i < links.size(); i++) { I_CmsXmlContentValue xmlValue = links.get(i); String value = xmlValue.getStringValue(getCmsObject()); if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(value)) { StringBuffer macro = new StringBuffer(10); macro.append(MACRO_LINK_PREFIX).append(i + 1); macroResolver.addMacro(macro.toString(), getRequestContext().removeSiteRoot(value)); } } return macroResolver.resolveMacros(params); }
/** * Removes an existing inheritance container entry with a given name from the configuration file.<p> * * This does nothing if no such entry actually exists.<p> * * @param cms the current CMS context * @param content the XML content * @param locale the locale from which to remove the entry * @param name the name of the entry * */ protected void removeExistingEntry(CmsObject cms, CmsXmlContent content, Locale locale, String name) { if (!content.hasLocale(locale)) { return; } String entriesXpath = N_CONFIGURATION; List<I_CmsXmlContentValue> values = content.getValues(entriesXpath, locale); int valueIndex = 0; for (I_CmsXmlContentValue value : values) { String valueXpath = value.getPath(); I_CmsXmlContentValue nameValue = content.getValue(CmsXmlUtils.concatXpath(valueXpath, N_NAME), locale); String currentName = nameValue.getStringValue(cms); if (currentName.equals(name)) { content.removeValue(valueXpath, locale, valueIndex); break; } valueIndex += 1; } }
/** * Removes an existing XML content value of the given element name and locale at the given index position * from this XML content document.<p> * * @param name the name of the XML content value element * @param locale the locale where to remove the value * @param index the index where to remove the value (relative to all other values of this type) */ public void removeValue(String name, Locale locale, int index) { // first get the value from the selected locale and index I_CmsXmlContentValue value = getValue(name, locale, index); if (!value.isChoiceOption()) { // check for the min / max occurs constrains List<I_CmsXmlContentValue> values = getValues(name, locale); if (values.size() <= value.getMinOccurs()) { // must not allow removing an element if min occurs would be violated throw new CmsRuntimeException( Messages.get().container( Messages.ERR_XMLCONTENT_ELEM_MINOCCURS_2, name, new Integer(value.getMinOccurs()))); } } // detach the value node from the XML document value.getElement().detach(); // re-initialize this XML content initDocument(m_document, m_encoding, m_contentDefinition); }
String name = content.getValue(namePath, locale).getStringValue(cms); Iterator<I_CmsXmlContentValue> itValues = content.getValues( CmsXmlContainerPage.XmlNode.Containers.name(), locale).iterator();
/** * Generates a new content sequence element from the given type, content and content definition.<p> * * @param path the path in the document to generate the value sequence for * @param locale the locale to get the content values from * @param content the XML content to generate the sequence element out of */ public CmsXmlContentValueSequence(String path, Locale locale, CmsXmlContent content) { m_locale = locale; m_content = content; m_path = CmsXmlUtils.removeXpathIndex(path); I_CmsXmlSchemaType type = m_content.getContentDefinition().getSchemaType(m_path); m_isChoiceSequence = type.isChoiceOption(); if (m_isChoiceSequence) { m_values = m_content.getSubValues(CmsXmlUtils.removeLastXpathElement(m_path), m_locale); } else { m_values = m_content.getValues(path, m_locale); } if (type.getContentDefinition().getChoiceMaxOccurs() > 1) { m_minOccurs = 0; m_maxOccurs = type.getContentDefinition().getChoiceMaxOccurs(); } else { if (m_isChoiceSequence && !m_values.isEmpty()) { type = m_values.get(0); } m_minOccurs = type.getMinOccurs(); m_maxOccurs = type.getMaxOccurs(); } }