📄 xmpmetaimpl.java
字号:
// =================================================================================================// ADOBE SYSTEMS INCORPORATED// Copyright 2006-2007 Adobe Systems Incorporated// All Rights Reserved//// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms// of the Adobe license agreement accompanying it.// =================================================================================================package com.adobe.xmp.impl;import java.util.Calendar;import java.util.Iterator;import com.adobe.xmp.XMPConst;import com.adobe.xmp.XMPDateTime;import com.adobe.xmp.XMPError;import com.adobe.xmp.XMPException;import com.adobe.xmp.XMPIterator;import com.adobe.xmp.XMPMeta;import com.adobe.xmp.XMPPathFactory;import com.adobe.xmp.XMPUtils;import com.adobe.xmp.impl.xpath.XMPPath;import com.adobe.xmp.impl.xpath.XMPPathParser;import com.adobe.xmp.options.IteratorOptions;import com.adobe.xmp.options.PropertyOptions;import com.adobe.xmp.properties.XMPProperty;/** * Implementation for {@link XMPMeta}. * * @since 17.02.2006 */public class XMPMetaImpl implements XMPMeta, XMPConst{ /** Property values are Strings by default */ private static final int VALUE_STRING = 0; /** */ private static final int VALUE_BOOLEAN = 1; /** */ private static final int VALUE_INTEGER = 2; /** */ private static final int VALUE_LONG = 3; /** */ private static final int VALUE_DOUBLE = 4; /** */ private static final int VALUE_DATE = 5; /** */ private static final int VALUE_CALENDAR = 6; /** */ private static final int VALUE_BASE64 = 7; /** root of the metadata tree */ private XMPNode tree; /** * Constructor for an empty metadata object. */ public XMPMetaImpl() { // create root node tree = new XMPNode(null, null, null); } /** * Constructor for a cloned metadata tree. * * @param tree * an prefilled metadata tree which fulfills all * <code>XMPNode</code> contracts. */ public XMPMetaImpl(XMPNode tree) { this.tree = tree; } /** * @see XMPMeta#appendArrayItem(String, String, PropertyOptions, String, * PropertyOptions) */ public void appendArrayItem(String schemaNS, String arrayName, PropertyOptions arrayOptions, String itemValue, PropertyOptions itemOptions) throws XMPException { ParameterAsserts.assertSchemaNS(schemaNS); ParameterAsserts.assertArrayName(arrayName); if (arrayOptions == null) { arrayOptions = new PropertyOptions(); } if (!arrayOptions.isOnlyArrayOptions()) { throw new XMPException("Only array form flags allowed for arrayOptions", XMPError.BADOPTIONS); } // Check if array options are set correctly. arrayOptions = XMPNodeUtils.verifySetOptions(arrayOptions, null); // Locate or create the array. If it already exists, make sure the array // form from the options // parameter is compatible with the current state. XMPPath arrayPath = XMPPathParser.expandXPath(schemaNS, arrayName); // Just lookup, don't try to create. XMPNode arrayNode = XMPNodeUtils.findNode(tree, arrayPath, false, null); if (arrayNode != null) { // The array exists, make sure the form is compatible. Zero // arrayForm means take what exists. if (!arrayNode.getOptions().isArray()) { throw new XMPException("The named property is not an array", XMPError.BADXPATH); } // if (arrayOptions != null && !arrayOptions.equalArrayTypes(arrayNode.getOptions())) // { // throw new XMPException("Mismatch of existing and specified array form", BADOPTIONS); // } } else { // The array does not exist, try to create it. if (arrayOptions.isArray()) { arrayNode = XMPNodeUtils.findNode(tree, arrayPath, true, arrayOptions); if (arrayNode == null) { throw new XMPException("Failure creating array node", XMPError.BADXPATH); } } else { // array options missing throw new XMPException("Explicit arrayOptions required to create new array", XMPError.BADOPTIONS); } } doSetArrayItem(arrayNode, ARRAY_LAST_ITEM, itemValue, itemOptions, true); } /** * @see XMPMeta#appendArrayItem(String, String, String) */ public void appendArrayItem(String schemaNS, String arrayName, String itemValue) throws XMPException { appendArrayItem(schemaNS, arrayName, null, itemValue, null); } /** * @throws XMPException * @see XMPMeta#countArrayItems(String, String) */ public int countArrayItems(String schemaNS, String arrayName) throws XMPException { ParameterAsserts.assertSchemaNS(schemaNS); ParameterAsserts.assertArrayName(arrayName); XMPPath arrayPath = XMPPathParser.expandXPath(schemaNS, arrayName); XMPNode arrayNode = XMPNodeUtils.findNode(tree, arrayPath, false, null); if (arrayNode == null) { return 0; } if (arrayNode.getOptions().isArray()) { return arrayNode.getChildrenLength(); } else { throw new XMPException("The named property is not an array", XMPError.BADXPATH); } } /** * @see XMPMeta#deleteArrayItem(String, String, int) */ public void deleteArrayItem(String schemaNS, String arrayName, int itemIndex) { try { ParameterAsserts.assertSchemaNS(schemaNS); ParameterAsserts.assertArrayName(arrayName); String itemPath = XMPPathFactory.composeArrayItemPath(arrayName, itemIndex); deleteProperty(schemaNS, itemPath); } catch (XMPException e) { // EMPTY, exceptions are ignored within delete } } /** * @see XMPMeta#deleteProperty(String, String) */ public void deleteProperty(String schemaNS, String propName) { try { ParameterAsserts.assertSchemaNS(schemaNS); ParameterAsserts.assertPropName(propName); XMPPath expPath = XMPPathParser.expandXPath(schemaNS, propName); XMPNode propNode = XMPNodeUtils.findNode(tree, expPath, false, null); if (propNode != null) { XMPNodeUtils.deleteNode(propNode); } } catch (XMPException e) { // EMPTY, exceptions are ignored within delete } } /** * @see XMPMeta#deleteQualifier(String, String, String, String) */ public void deleteQualifier(String schemaNS, String propName, String qualNS, String qualName) { try { // Note: qualNS and qualName are checked inside composeQualfierPath ParameterAsserts.assertSchemaNS(schemaNS); ParameterAsserts.assertPropName(propName); String qualPath = propName + XMPPathFactory.composeQualifierPath(qualNS, qualName); deleteProperty(schemaNS, qualPath); } catch (XMPException e) { // EMPTY, exceptions within delete are ignored } } /** * @see XMPMeta#deleteStructField(String, String, String, String) */ public void deleteStructField(String schemaNS, String structName, String fieldNS, String fieldName) { try { // fieldNS and fieldName are checked inside composeStructFieldPath ParameterAsserts.assertSchemaNS(schemaNS); ParameterAsserts.assertStructName(structName); String fieldPath = structName + XMPPathFactory.composeStructFieldPath(fieldNS, fieldName); deleteProperty(schemaNS, fieldPath); } catch (XMPException e) { // EMPTY, exceptions within delete are ignored } } /** * @see XMPMeta#doesPropertyExist(String, String) */ public boolean doesPropertyExist(String schemaNS, String propName) { try { ParameterAsserts.assertSchemaNS(schemaNS); ParameterAsserts.assertPropName(propName); XMPPath expPath = XMPPathParser.expandXPath(schemaNS, propName); final XMPNode propNode = XMPNodeUtils.findNode(tree, expPath, false, null); return propNode != null; } catch (XMPException e) { return false; } } /** * @see XMPMeta#doesArrayItemExist(String, String, int) */ public boolean doesArrayItemExist(String schemaNS, String arrayName, int itemIndex) { try { ParameterAsserts.assertSchemaNS(schemaNS); ParameterAsserts.assertArrayName(arrayName); String path = XMPPathFactory.composeArrayItemPath(arrayName, itemIndex); return doesPropertyExist(schemaNS, path); } catch (XMPException e) { return false; } } /** * @see XMPMeta#doesStructFieldExist(String, String, String, String) */ public boolean doesStructFieldExist(String schemaNS, String structName, String fieldNS, String fieldName) { try { // fieldNS and fieldName are checked inside composeStructFieldPath() ParameterAsserts.assertSchemaNS(schemaNS); ParameterAsserts.assertStructName(structName); String path = XMPPathFactory.composeStructFieldPath(fieldNS, fieldName); return doesPropertyExist(schemaNS, structName + path); } catch (XMPException e) { return false; } } /** * @see XMPMeta#doesQualifierExist(String, String, String, String) */ public boolean doesQualifierExist(String schemaNS, String propName, String qualNS, String qualName) { try { // qualNS and qualName are checked inside composeQualifierPath() ParameterAsserts.assertSchemaNS(schemaNS); ParameterAsserts.assertPropName(propName); String path = XMPPathFactory.composeQualifierPath(qualNS, qualName); return doesPropertyExist(schemaNS, propName + path); } catch (XMPException e) { return false; } } /** * @see XMPMeta#getArrayItem(String, String, int) */ public XMPProperty getArrayItem(String schemaNS, String arrayName, int itemIndex) throws XMPException { ParameterAsserts.assertSchemaNS(schemaNS); ParameterAsserts.assertArrayName(arrayName); String itemPath = XMPPathFactory.composeArrayItemPath(arrayName, itemIndex); return getProperty(schemaNS, itemPath); } /** * @throws XMPException * @see XMPMeta#getLocalizedText(String, String, String, String) */ public XMPProperty getLocalizedText(String schemaNS, String altTextName, String genericLang, String specificLang) throws XMPException { ParameterAsserts.assertSchemaNS(schemaNS); ParameterAsserts.assertArrayName(altTextName); ParameterAsserts.assertSpecificLang(specificLang); genericLang = genericLang != null ? Utils.normalizeLangValue(genericLang) : null; specificLang = Utils.normalizeLangValue(specificLang); XMPPath arrayPath = XMPPathParser.expandXPath(schemaNS, altTextName); XMPNode arrayNode = XMPNodeUtils.findNode(tree, arrayPath, false, null); if (arrayNode == null) { return null; } Object[] result = XMPNodeUtils.chooseLocalizedText(arrayNode, genericLang, specificLang); int match = ((Integer) result[0]).intValue(); final XMPNode itemNode = (XMPNode) result[1]; if (match != XMPNodeUtils.CLT_NO_VALUES) { return new XMPProperty() { public Object getValue() { return itemNode.getValue(); } public PropertyOptions getOptions() { return itemNode.getOptions(); } public String getLanguage() { return itemNode.getQualifier(1).getValue(); } public String toString() { return itemNode.getValue().toString(); } }; } else { return null; } } /** * @see XMPMeta#setLocalizedText(String, String, String, String, String, * PropertyOptions) */ public void setLocalizedText(String schemaNS, String altTextName, String genericLang, String specificLang, String itemValue, PropertyOptions options) throws XMPException { ParameterAsserts.assertSchemaNS(schemaNS); ParameterAsserts.assertArrayName(altTextName); ParameterAsserts.assertSpecificLang(specificLang); genericLang = genericLang != null ? Utils.normalizeLangValue(genericLang) : null; specificLang = Utils.normalizeLangValue(specificLang); XMPPath arrayPath = XMPPathParser.expandXPath(schemaNS, altTextName); // Find the array node and set the options if it was just created. XMPNode arrayNode = XMPNodeUtils.findNode(tree, arrayPath, true, new PropertyOptions( PropertyOptions.ARRAY | PropertyOptions.ARRAY_ORDERED | PropertyOptions.ARRAY_ALTERNATE | PropertyOptions.ARRAY_ALT_TEXT)); if (arrayNode == null) { throw new XMPException("Failed to find or create array node", XMPError.BADXPATH); } else if (!arrayNode.getOptions().isArrayAltText()) { if (!arrayNode.hasChildren() && arrayNode.getOptions().isArrayAlternate()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -