a_cmsxmldocument.java
来自「找了很久才找到到源代码」· Java 代码 · 共 787 行 · 第 1/2 页
JAVA
787 行
* @see org.opencms.xml.I_CmsXmlDocument#getStringValue(CmsObject, java.lang.String, Locale, int)
*/
public String getStringValue(CmsObject cms, String path, Locale locale, int index) {
// directly calling getValueInternal() is more efficient then calling getStringValue(CmsObject, String, Locale)
// since the most costs are generated in resolving the xpath name
I_CmsXmlContentValue value = getValueInternal(CmsXmlUtils.createXpath(path, index + 1), locale);
if (value != null) {
return value.getStringValue(cms);
}
return null;
}
/**
* @see org.opencms.xml.I_CmsXmlDocument#getValue(java.lang.String, java.util.Locale)
*/
public I_CmsXmlContentValue getValue(String path, Locale locale) {
return getValueInternal(CmsXmlUtils.createXpath(path, 1), locale);
}
/**
* @see org.opencms.xml.I_CmsXmlDocument#getValue(java.lang.String, java.util.Locale, int)
*/
public I_CmsXmlContentValue getValue(String path, Locale locale, int index) {
return getValueInternal(CmsXmlUtils.createXpath(path, index + 1), locale);
}
/**
* @see org.opencms.xml.I_CmsXmlDocument#getValues(java.util.Locale)
*/
public List getValues(Locale locale) {
List result = new ArrayList();
// bookmarks are stored with the locale as first prefix
String prefix = '/' + locale.toString() + '/';
// it's better for performance to iterate through the list of bookmarks directly
Iterator i = m_bookmarks.keySet().iterator();
while (i.hasNext()) {
String key = (String)i.next();
if (key.startsWith(prefix)) {
result.add(m_bookmarks.get(key));
}
}
// sort the result
Collections.sort(result);
return result;
}
/**
* @see org.opencms.xml.I_CmsXmlDocument#getValues(java.lang.String, java.util.Locale)
*/
public List getValues(String path, Locale locale) {
List result = new ArrayList();
int count = 1;
Object o;
String xpath = CmsXmlUtils.createXpath(path, 1);
xpath = CmsXmlUtils.removeXpathIndex(xpath);
do {
String subpath = CmsXmlUtils.createXpathElement(xpath, count);
o = getBookmark(subpath, locale);
if (o != null) {
result.add(o);
count++;
}
} while (o != null);
return result;
}
/**
* @see org.opencms.xml.I_CmsXmlDocument#hasLocale(java.util.Locale)
*/
public boolean hasLocale(Locale locale) {
if (locale == null) {
throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NULL_LOCALE_0));
}
return m_locales.contains(locale);
}
/**
* @see org.opencms.xml.I_CmsXmlDocument#hasValue(java.lang.String, java.util.Locale)
*/
public boolean hasValue(String path, Locale locale) {
return null != getBookmark(CmsXmlUtils.createXpath(path, 1), locale);
}
/**
* @see org.opencms.xml.I_CmsXmlDocument#hasValue(java.lang.String, java.util.Locale, int)
*/
public boolean hasValue(String path, Locale locale, int index) {
return null != getBookmark(CmsXmlUtils.createXpath(path, index + 1), locale);
}
/**
* @see org.opencms.xml.I_CmsXmlDocument#initDocument()
*/
public void initDocument() {
initDocument(m_document, m_encoding, getContentDefinition());
}
/**
* @see org.opencms.xml.I_CmsXmlDocument#isEnabled(java.lang.String, java.util.Locale)
*/
public boolean isEnabled(String path, Locale locale) {
return hasValue(path, locale);
}
/**
* @see org.opencms.xml.I_CmsXmlDocument#isEnabled(java.lang.String, java.util.Locale, int)
*/
public boolean isEnabled(String path, Locale locale, int index) {
return hasValue(path, locale, index);
}
/**
* Marshals (writes) the content of the current XML document
* into a byte array using the selected encoding.<p>
*
* @return the content of the current XML document written into a byte array
* @throws CmsXmlException if something goes wrong
*/
public byte[] marshal() throws CmsXmlException {
return ((ByteArrayOutputStream)marshal(new ByteArrayOutputStream(), m_encoding)).toByteArray();
}
/**
* @see org.opencms.xml.I_CmsXmlDocument#moveLocale(java.util.Locale, java.util.Locale)
*/
public void moveLocale(Locale source, Locale destination) throws CmsXmlException {
copyLocale(source, destination);
removeLocale(source);
}
/**
* @see org.opencms.xml.I_CmsXmlDocument#removeLocale(java.util.Locale)
*/
public void removeLocale(Locale locale) throws CmsXmlException {
if (!hasLocale(locale)) {
throw new CmsXmlException(Messages.get().container(Messages.ERR_LOCALE_NOT_AVAILABLE_1, locale));
}
Element rootNode = m_document.getRootElement();
Iterator i = rootNode.elementIterator();
String localeStr = locale.toString();
while (i.hasNext()) {
Element element = (Element)i.next();
String language = element.attributeValue(CmsXmlContentDefinition.XSD_ATTRIBUTE_VALUE_LANGUAGE, null);
if ((language != null) && (localeStr.equals(language))) {
// detach node with the locale
element.detach();
// there can be only one node for the locale
break;
}
}
// re-initialize the document bookmarks
initDocument(m_document, m_encoding, getContentDefinition());
}
/**
* Sets the content conversion mode for this document.<p>
*
* @param conversion the conversion mode to set for this document
*/
public void setConversion(String conversion) {
m_conversion = conversion;
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
try {
return CmsXmlUtils.marshal(m_document, m_encoding);
} catch (CmsXmlException e) {
throw new CmsRuntimeException(Messages.get().container(Messages.ERR_WRITE_XML_DOC_TO_STRING_0), e);
}
}
/**
* Validates the XML structure of the document with the DTD or XML schema used by the document.<p>
*
* This is required in case someone modifies the XML structure of a
* document using the "edit control code" option.<p>
*
* @param resolver the XML entity resolver to use
* @throws CmsXmlException if the validation fails
*/
public void validateXmlStructure(EntityResolver resolver) throws CmsXmlException {
if (m_file != null) {
byte[] xmlData = null;
// file is set, use bytes from file directly
xmlData = m_file.getContents();
CmsXmlUtils.validateXmlStructure(xmlData, resolver);
} else {
CmsXmlUtils.validateXmlStructure(m_document, m_encoding, resolver);
}
}
/**
* Adds a bookmark for the given value.<p>
*
* @param path the lookup path to use for the bookmark
* @param locale the locale to use for the bookmark
* @param enabled if true, the value is enabled, if false it is disabled
* @param value the value to bookmark
*/
protected void addBookmark(String path, Locale locale, boolean enabled, Object value) {
// add the locale (since the locales are a set adding them more then once does not matter)
addLocale(locale);
// add a bookmark to the provided value
m_bookmarks.put(getBookmarkName(path, locale), value);
Object o;
// update mapping of element name to locale
if (enabled) {
// only include enabled elements
o = m_elementLocales.get(path);
if (o != null) {
((Set)o).add(locale);
} else {
Set set = new HashSet();
set.add(locale);
m_elementLocales.put(path, set);
}
}
// update mapping of locales to element names
o = m_elementNames.get(locale);
if (o != null) {
((Set)o).add(path);
} else {
Set set = new HashSet();
set.add(path);
m_elementNames.put(locale, set);
}
}
/**
* Adds a locale to the set of locales of the XML document.<p>
*
* @param locale the locale to add
*/
protected void addLocale(Locale locale) {
// add the locale to all locales in this dcoument
m_locales.add(locale);
}
/**
* Clears the XML document bookmarks.<p>
*/
protected void clearBookmarks() {
m_bookmarks.clear();
}
/**
* Returns the bookmarked value for the given bookmark,
* which must be a valid bookmark name.
*
* Use {@link #getBookmarks()} to get the list of all valid bookmark names.<p>
*
* @param bookmark the bookmark name to look up
* @return the bookmarked value for the given bookmark
*/
protected Object getBookmark(String bookmark) {
return m_bookmarks.get(bookmark);
}
/**
* Returns the bookmarked value for the given name.<p>
*
* @param path the lookup path to use for the bookmark
* @param locale the locale to get the bookmark for
* @return the bookmarked value
*/
protected Object getBookmark(String path, Locale locale) {
return m_bookmarks.get(getBookmarkName(path, locale));
}
/**
* Returns the names of all bookmarked elements.<p>
*
* @return the names of all bookmarked elements
*/
protected Set getBookmarks() {
return m_bookmarks.keySet();
}
/**
* Initializes an XML document based on the provided document, encoding and content definition.<p>
*
* @param document the base XML document to use for initializing
* @param encoding the encoding to use when marshalling the document later
* @param contentDefinition the content definition to use
*/
protected abstract void initDocument(Document document, String encoding, CmsXmlContentDefinition contentDefinition);
/**
* Returns <code>true</code> if the auto correction feature is enabled for saving this XML content.<p>
*
* @return <code>true</code> if the auto correction feature is enabled for saving this XML content
*/
protected boolean isAutoCorrectionEnabled() {
// by default, this method always returns false
return false;
}
/**
* Marshals (writes) the content of the current XML document
* into an output stream.<p>
*
* @param out the output stream to write to
* @param encoding the encoding to use
* @return the output stream with the XML content
* @throws CmsXmlException if something goes wrong
*/
protected OutputStream marshal(OutputStream out, String encoding) throws CmsXmlException {
return CmsXmlUtils.marshal(m_document, out, encoding);
}
/**
* Removes the bookmark for an element with the given name and locale.<p>
*
* @param path the lookup path to use for the bookmark
* @param locale the locale of the element
* @return the element removed from the bookmarks or null
*/
protected I_CmsXmlContentValue removeBookmark(String path, Locale locale) {
// remove mapping of element name to locale
Object o;
o = m_elementLocales.get(path);
if (o != null) {
((Set)o).remove(locale);
}
// remove mapping of locale to element name
o = m_elementNames.get(locale);
if (o != null) {
((Set)o).remove(path);
}
// remove the bookmark and return the removed element
return (I_CmsXmlContentValue)m_bookmarks.remove(getBookmarkName(path, locale));
}
/**
* Internal method to look up a value, requires that the name already has been
* "normalized" for the bookmark lookup.
*
* This is required to find names like "title/subtitle" which are stored
* internally as "title[0]/subtitle[0)" in the bookmarks.
*
* @param path the path to look up
* @param locale the locale to look up
*
* @return the value found in the bookmarks
*/
private I_CmsXmlContentValue getValueInternal(String path, Locale locale) {
Object value = getBookmark(path, locale);
if (value != null) {
return (I_CmsXmlContentValue)value;
}
return null;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?