private String getStringValue(CmsObject cms, CmsXmlContent content, String node) { I_CmsXmlContentValue val = content.getValue(node, Locale.ENGLISH); if (val == null) { return null; } return val.getStringValue(cms); }
/** 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; }
/** * Creates the container for a bundle descriptor. * @return the container for a bundle descriptor. */ private IndexedContainer createContainerForDescriptorEditing() { IndexedContainer container = new IndexedContainer(); // create properties container.addContainerProperty(TableProperty.KEY, String.class, ""); container.addContainerProperty(TableProperty.DESCRIPTION, String.class, ""); container.addContainerProperty(TableProperty.DEFAULT, String.class, ""); // add entries CmsXmlContentValueSequence messages = m_descContent.getValueSequence( "/" + Descriptor.N_MESSAGE, Descriptor.LOCALE); for (int i = 0; i < messages.getElementCount(); i++) { String prefix = messages.getValue(i).getPath() + "/"; Object itemId = container.addItem(); Item item = container.getItem(itemId); String key = m_descContent.getValue(prefix + Descriptor.N_KEY, Descriptor.LOCALE).getStringValue(m_cms); item.getItemProperty(TableProperty.KEY).setValue(key); item.getItemProperty(TableProperty.DESCRIPTION).setValue( m_descContent.getValue(prefix + Descriptor.N_DESCRIPTION, Descriptor.LOCALE).getStringValue(m_cms)); item.getItemProperty(TableProperty.DEFAULT).setValue( m_descContent.getValue(prefix + Descriptor.N_DEFAULT, Descriptor.LOCALE).getStringValue(m_cms)); } return container; }
/** * 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; }
/** Helper to read an optional String value. * @param path The XML path of the element to read. * @return The String value stored in the XML, or <code>null</code> if the value could not be read. */ protected String parseOptionalStringValue(final String path) { final I_CmsXmlContentValue value = m_xml.getValue(path, m_locale); if (value == null) { return null; } else { return value.getStringValue(null); } }
/** * @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; } }
/** * Reads the XML content value for a given xpath.<p> * * Returns null if no value was found at that path.<p> * * @param path the XML content xpath * * @return the value for that path as a string */ private String getStringValue(String path) { I_CmsXmlContentValue value = m_content.getValue(path, Locale.ENGLISH); if (value == null) { return null; } else { return value.getStringValue(m_cms); } }
/** * 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); }
/** * Removes the old detail page information from the XML content.<p> * * @throws CmsException if something goes wrong */ private void removeOldValues() throws CmsException { Locale locale = getLocale(); I_CmsXmlContentValue value = m_document.getValue(N_DETAIL_PAGE, locale); do { value = m_document.getValue(N_DETAIL_PAGE, locale); if (value != null) { m_document.removeValue(value.getPath(), locale, 0); } } while (value != null); }
/** * 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); }
/** Parses a single query facet item with query and label. * @param prefix path to the query facet item (with trailing '/'). * @return the query facet item. */ private I_CmsFacetQueryItem parseFacetQueryItem(final String prefix) { I_CmsXmlContentValue query = m_xml.getValue(prefix + XML_ELEMENT_QUERY_FACET_QUERY_QUERY, m_locale); if (null != query) { String queryString = query.getStringValue(null); I_CmsXmlContentValue label = m_xml.getValue(prefix + XML_ELEMENT_QUERY_FACET_QUERY_LABEL, m_locale); String labelString = null != label ? label.getStringValue(null) : null; return new CmsFacetQueryItem(queryString, labelString); } else { return null; } }
/** * Writes a single item of detail page information to the XML content.<p> * * @param type the type which the detail page should render * @param id the page id of the detail page * @param index the position at which the detail page info should be added */ private void writeValue(String type, CmsUUID id, int index) { Locale locale = CmsLocaleManager.getLocale("en"); // todo: check actual locale. m_document.addValue(m_cms, N_DETAIL_PAGE, locale, index); String typePath = N_DETAIL_PAGE + "[" + (1 + index) + "]/" + N_TYPE; I_CmsXmlContentValue typeVal = m_document.getValue(typePath, locale); String pagePath = N_DETAIL_PAGE + "[" + (1 + index) + "]/" + N_PAGE; CmsXmlVfsFileValue pageVal = (CmsXmlVfsFileValue)m_document.getValue(pagePath, locale); typeVal.setStringValue(m_cms, type); pageVal.setIdValue(m_cms, id); } }
/** Helper to read an optional Boolean value. * @param path The XML path of the element to read. * @return The Boolean value stored in the XML, or <code>null</code> if the value could not be read. */ protected Boolean parseOptionalBooleanValue(final String path) { final I_CmsXmlContentValue value = m_xml.getValue(path, m_locale); if (value == null) { return null; } else { final String stringValue = value.getStringValue(null); try { final Boolean boolValue = Boolean.valueOf(stringValue); return boolValue; } catch (final NumberFormatException e) { LOG.info(Messages.get().getBundle().key(Messages.LOG_OPTIONAL_BOOLEAN_MISSING_1, path), e); return null; } } }
/** Helper to read an optional Integer value. * @param path The XML path of the element to read. * @return The Integer value stored in the XML, or <code>null</code> if the value could not be read. */ protected Integer parseOptionalIntValue(final String path) { final I_CmsXmlContentValue value = m_xml.getValue(path, m_locale); if (value == null) { return null; } else { final String stringValue = value.getStringValue(null); try { final Integer intValue = Integer.valueOf(stringValue); return intValue; } catch (final NumberFormatException e) { LOG.info(Messages.get().getBundle().key(Messages.LOG_OPTIONAL_INTEGER_MISSING_1, path), e); return null; } } }
/** * Sets the editor values for the locale with the parameters from the request.<p> * * Called before saving the xml content, redisplaying the input form, * changing the language and adding or removing elements.<p> * * @param locale the locale of the content to save * @throws CmsXmlException if something goes wrong */ public void setEditorValues(Locale locale) throws CmsXmlException { List valueNames = getSimpleValueNames(m_content.getContentDefinition(), "", locale); Iterator i = valueNames.iterator(); while (i.hasNext()) { String valueName = (String)i.next(); I_CmsXmlContentValue value = m_content.getValue(valueName, locale); I_CmsWidget widget = value.getContentDefinition().getContentHandler().getWidget(value); widget.setEditorValue(getCms(), getJsp().getRequest().getParameterMap(), this, (I_CmsWidgetParameter)value); } }
/** * Sets the editor values for the locale with the parameters from the request.<p> * * Called before saving the xml content, redisplaying the input form, * changing the language and adding or removing elements.<p> * * @param locale the locale of the content to save * @throws CmsXmlException if something goes wrong */ public void setEditorValues(Locale locale) throws CmsXmlException { List<String> names = m_content.getNames(locale); Iterator<String> i = names.iterator(); while (i.hasNext()) { String path = i.next(); I_CmsXmlContentValue value = m_content.getValue(path, locale); if (value.isSimpleType()) { I_CmsWidget widget = value.getContentDefinition().getContentHandler().getWidget(value); widget.setEditorValue( getCms(), getJsp().getRequest().getParameterMap(), this, (I_CmsWidgetParameter)value); } } }
/** * Adds the given value to the content document.<p> * * @param content the content document * @param locale the content locale * @param path the value XPath * @param value the value */ protected void addContentValue(CmsXmlContent content, Locale locale, String path, String value) { boolean hasValue = content.hasValue(path, locale); if (!hasValue) { String[] pathElements = path.split("/"); String currentPath = pathElements[0]; for (int i = 0; i < pathElements.length; i++) { if (i > 0) { currentPath = CmsStringUtil.joinPaths(currentPath, pathElements[i]); } while (!content.hasValue(currentPath, locale)) { content.addValue(m_cms, currentPath, locale, CmsXmlUtils.getXpathIndexInt(currentPath) - 1); } } } content.getValue(path, locale).setStringValue(m_cms, value); }
/** * Deletes the given value path from the content document.<p> * * @param content the content document * @param locale the content locale * @param path the value XPath */ protected void deleteContentValue(CmsXmlContent content, Locale locale, String path) { boolean hasValue = content.hasValue(path, locale); if (hasValue) { int index = CmsXmlUtils.getXpathIndexInt(path) - 1; I_CmsXmlContentValue val = content.getValue(path, locale); if (index >= val.getMinOccurs()) { content.removeValue(path, locale, index); } else { val.setStringValue(m_cms, ""); } } }
/** * Saves the inheritance group.<p> * * @param resource the inheritance group resource * @param inheritanceContainer the inherited group container data * * @throws CmsException if something goes wrong */ private void saveInheritanceGroup(CmsResource resource, CmsInheritanceContainer inheritanceContainer) throws CmsException { CmsObject cms = getCmsObject(); CmsFile file = cms.readFile(resource); CmsXmlContent document = CmsXmlContentFactory.unmarshal(cms, file); for (Locale docLocale : document.getLocales()) { document.removeLocale(docLocale); } Locale locale = Locale.ENGLISH; document.addLocale(cms, locale); document.getValue("Title", locale).setStringValue(cms, inheritanceContainer.getTitle()); document.getValue("Description", locale).setStringValue(cms, inheritanceContainer.getDescription()); document.getValue("ConfigName", locale).setStringValue(cms, inheritanceContainer.getName()); byte[] content = document.marshal(); file.setContents(content); cms.writeFile(file); }
/** * @see org.opencms.jsp.search.config.parser.I_CmsSearchConfigurationParser#parseDidYouMean() */ public I_CmsSearchConfigurationDidYouMean parseDidYouMean() { final I_CmsXmlContentValue didYouMean = m_xml.getValue(XML_ELEMENT_DIDYOUMEAN, m_locale); if (didYouMean == null) { return null; } else { final String pathPrefix = didYouMean.getPath() + "/"; String param = parseOptionalStringValue(pathPrefix + XML_ELEMENT_DIDYOUMEAN_QUERYPARAM); if (null == param) { param = getQueryParam(); } Boolean collate = parseOptionalBooleanValue(pathPrefix + XML_ELEMENT_DIDYOUMEAN_COLLATE); Integer count = parseOptionalIntValue(pathPrefix + XML_ELEMENT_DIDYOUMEAN_COUNT); return new CmsSearchConfigurationDidYouMean(param, collate, count); } }