atg.endeca.index.util
Class SearchXMLToEndecaXML

java.lang.Object
  extended by atg.endeca.index.util.SearchXMLToEndecaXML

public class SearchXMLToEndecaXML
extends java.lang.Object

A grungy class that uses DOM manipulations to convert an ATG-search style IOC to an Endeca-style IOC.

More specifically, it attempts to translate "text-properties" and "meta-properties" elements to a merged "properties" element. It also translates "store-as-*" attributes into corresponding "is-dimension" attributes.

This class also attempts to translate "store-as" attributes to "is-dimension" attributes (stripping out the "store as" values).

This class also provides a simple, properties-file based override mechanism that allows one to override the value of a given attribute. The attributes are specified via properties names of the form "{propertyPath}.{attributeName}". For example, a line in an override properties file reading "childSKUs.onSale.is-dimension=true" could be used to override the is-dimension attribute of the "childSKUs.onSale" property element. Note that an empty property value removes the corresponding attribute, so an override properties file line reading "childSKUs.onSale.is-dimension=" would remove the is-dimension attribute. One can set the echoPropertyNames to true to echo the encountered property paths to stderr (set via the "-echo" command-line flag).

Created: April 06 2012


Field Summary
static java.lang.String CLASS_VERSION
          Class version string from source code control system.
static java.lang.String[] ITEM_ATTRS
          All the item attributes we copy.
static java.lang.String[] ITEM_COMPAT_ATTRS
          The attributes we check when deciding to issue an item-compatibility warning.
static java.lang.String OUTPUT_FILE_ENCODING
           
static java.lang.String[] STORE_AS_ATTRS
          The "store-as-*" attributes we check to determine an is-dimension value.
 
Constructor Summary
SearchXMLToEndecaXML()
           
 
Method Summary
protected  void appendElementAndPrecedingComments(org.w3c.dom.Element pElement, java.lang.String pElementNameBefore, org.w3c.dom.Node pNewParent)
          Utility method to append the corresponding element to pNewParent.
protected  void applyOverrideProperties(java.lang.String pParentPropertyPath, java.util.Properties pOverrideProperties, java.util.Map<java.lang.String,org.w3c.dom.Element> pPropNameToElem)
          Apply any override properties to the map of properties.
protected  boolean arePropertiesCompatible(java.lang.String pItemName, java.lang.String pNameOne, org.w3c.dom.Element pMetaProp, java.lang.String pNameTwo, org.w3c.dom.Element pTextProp)
          Return whether the specified properties are compatable, issuing warnings if needed.
 void convertStoreAsToDimension(org.w3c.dom.Element pProp)
          Convert the various "store-as-*" attributes to an is-dimension value, if an is-dimension value doesn't already exist.
protected  int copyMissingAttributes(org.w3c.dom.Element pFrom, org.w3c.dom.Element pTo, java.lang.String[] pAttributeNames)
          Copy any attributes of pFrom that don't exist on pTo.
protected  java.util.Map<java.lang.String,org.w3c.dom.Element> createNameToPropertyMap(org.w3c.dom.Element pProperties, boolean pMarkAsText)
          Create a new name to property map.
protected  org.w3c.dom.Element createPropertiesElementWithChildren(org.w3c.dom.Element pItem, java.util.Collection<org.w3c.dom.Element> pChildren)
          Create a new properties element with the specified children.
protected  int getIndexOfChildNode(org.w3c.dom.Node pNode)
          Return the index of child node pNode in it's parent child nodes list.
protected  int getPreviousIndexOfNonCommentNonTextualSibling(org.w3c.dom.Node pParent, int pChildIndex)
          Returns the index of the previous non-comment, non-text sibling of the node at pChildIndex in pParent's child NodeList.
static java.lang.String getStringFromDocument(org.w3c.dom.Document pDocument)
          Returns string representation of the given document.
 boolean isEchoPropertyNames()
          Returns property echoPropertyNames.
protected  org.w3c.dom.Document loadDocument(java.io.File pSourceFile)
          Load the specified document.
static void main(java.lang.String[] pArgs)
          Implements the ATG Search style IOC to Endeca style IOC file as a command line tool.
protected  java.util.Map<java.lang.String,org.w3c.dom.Element> mergeNameToPropertyMaps(java.lang.String pParentPath, java.lang.String pNameOne, java.util.Map<java.lang.String,org.w3c.dom.Element> pOne, java.lang.String pNameTwo, java.util.Map<java.lang.String,org.w3c.dom.Element> pTwo, boolean pCheckCompat)
          Merge the text and meta property maps.
protected  boolean nullSafeEquals(java.lang.Object pOne, java.lang.Object pTwo)
          A simpleminded equals that works if one or both objects are null.
 java.util.Properties parsePropertiesFile(java.io.File pFile)
          Returns a Properties object loading it from the specified file.
 void setEchoPropertyNames(boolean pEchoPropertyNames)
          Sets property echoPropertyNames.
protected  org.w3c.dom.Document transformDocument(java.io.File pSourceFile, java.io.File pOverrideProperties)
          Transform the specified IOC source file from ATG Search format to a more Endeca-oriented IOC format.
protected  void transformItem(java.lang.String pParentPropPath, java.util.Properties pOverrideProperties, org.w3c.dom.Element pItem)
          Transform the specified "item" element.
protected  boolean warnIfAttributeMismatch(java.lang.String pLocation, java.lang.String pNameAttr, java.lang.String[] pAttributeNames, java.lang.String pOneDescription, org.w3c.dom.Element pOne, java.lang.String pTwoDescription, org.w3c.dom.Element pTwo)
          Warn if the correspoonding attribute of pOne and pTwo elements don't match.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS_VERSION

public static final java.lang.String CLASS_VERSION
Class version string from source code control system.

See Also:
Constant Field Values

ITEM_COMPAT_ATTRS

public static final java.lang.String[] ITEM_COMPAT_ATTRS
The attributes we check when deciding to issue an item-compatibility warning.


ITEM_ATTRS

public static final java.lang.String[] ITEM_ATTRS
All the item attributes we copy.


STORE_AS_ATTRS

public static final java.lang.String[] STORE_AS_ATTRS
The "store-as-*" attributes we check to determine an is-dimension value.


OUTPUT_FILE_ENCODING

public static final java.lang.String OUTPUT_FILE_ENCODING
See Also:
Constant Field Values
Constructor Detail

SearchXMLToEndecaXML

public SearchXMLToEndecaXML()
Method Detail

setEchoPropertyNames

public void setEchoPropertyNames(boolean pEchoPropertyNames)
Sets property echoPropertyNames. Whether to echo out property names to stderr. This is useful when trying to determine what keys to use in the overrider properties file.


isEchoPropertyNames

public boolean isEchoPropertyNames()
Returns property echoPropertyNames. Whether to echo out property names. This is useful when trying to determine what keys to use in the overrider properties file.


parsePropertiesFile

public java.util.Properties parsePropertiesFile(java.io.File pFile)
                                         throws java.io.IOException
Returns a Properties object loading it from the specified file.

Throws:
java.io.IOException

loadDocument

protected org.w3c.dom.Document loadDocument(java.io.File pSourceFile)
                                     throws org.xml.sax.SAXException,
                                            javax.xml.parsers.ParserConfigurationException,
                                            java.io.IOException
Load the specified document. Parses the specified file as XML an returns the corresponding DOM document.

Parameters:
pSourceFile - the source file.
Throws:
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationException
java.io.IOException

warnIfAttributeMismatch

protected boolean warnIfAttributeMismatch(java.lang.String pLocation,
                                          java.lang.String pNameAttr,
                                          java.lang.String[] pAttributeNames,
                                          java.lang.String pOneDescription,
                                          org.w3c.dom.Element pOne,
                                          java.lang.String pTwoDescription,
                                          org.w3c.dom.Element pTwo)
Warn if the correspoonding attribute of pOne and pTwo elements don't match.

Parameters:
pLocation - the human readable location field.
pNameAttr - the name of the attribute to use to determine the name of the element. May be null.
pOneDescription - the human readable description of the pOne element.
pOne - the first element whose attributes should be compared.
pTwoDescription - the human readable description of the pTwo element.
pTwo - the second element whose attributes should be compared.
Returns:
true if no attributes mismatched (and no warning was given) false otherwise.

arePropertiesCompatible

protected boolean arePropertiesCompatible(java.lang.String pItemName,
                                          java.lang.String pNameOne,
                                          org.w3c.dom.Element pMetaProp,
                                          java.lang.String pNameTwo,
                                          org.w3c.dom.Element pTextProp)
Return whether the specified properties are compatable, issuing warnings if needed.


nullSafeEquals

protected boolean nullSafeEquals(java.lang.Object pOne,
                                 java.lang.Object pTwo)
A simpleminded equals that works if one or both objects are null.


convertStoreAsToDimension

public void convertStoreAsToDimension(org.w3c.dom.Element pProp)
Convert the various "store-as-*" attributes to an is-dimension value, if an is-dimension value doesn't already exist. The "store-as-*" attributes will be removed by this method.

Parameters:
pProp - the "property" element whose store-as-* attributes should be converted.

copyMissingAttributes

protected int copyMissingAttributes(org.w3c.dom.Element pFrom,
                                    org.w3c.dom.Element pTo,
                                    java.lang.String[] pAttributeNames)
Copy any attributes of pFrom that don't exist on pTo. Will not overwrite existing attributes of pTo.

Parameters:
pFrom - the element to copy attributes values from
pTo - the element to copy attribute vlues to.

mergeNameToPropertyMaps

protected java.util.Map<java.lang.String,org.w3c.dom.Element> mergeNameToPropertyMaps(java.lang.String pParentPath,
                                                                                      java.lang.String pNameOne,
                                                                                      java.util.Map<java.lang.String,org.w3c.dom.Element> pOne,
                                                                                      java.lang.String pNameTwo,
                                                                                      java.util.Map<java.lang.String,org.w3c.dom.Element> pTwo,
                                                                                      boolean pCheckCompat)
Merge the text and meta property maps.

Parameters:
pParentPath - the property path of the parent
pNameOne - the name of the first map. This is typically "text-properties" or "meta-properties"
pOne - the first name to element map.
pNameTwo - the name of the second map. This is typically "text-properties" or "meta-properties"
pTwo - the decond name to element map.
pCheckCompat - whether to check for compatibility and possibly issues warnings.
Returns:
the merged map. This is a new map built by merging pTwo into pOne.

createNameToPropertyMap

protected java.util.Map<java.lang.String,org.w3c.dom.Element> createNameToPropertyMap(org.w3c.dom.Element pProperties,
                                                                                      boolean pMarkAsText)
Create a new name to property map.

Parameters:
pProperties - an element that should be a meta-properties or text-properties element.

transformItem

protected void transformItem(java.lang.String pParentPropPath,
                             java.util.Properties pOverrideProperties,
                             org.w3c.dom.Element pItem)
Transform the specified "item" element. This transforms any meta-properties and text-properties into a new properties element, as well as merging/modifying attributes. Recursively calls into on any child item elements.

Parameters:
pParentPath - the parent property path
pOverrideProperties - the override attributes properties map.
pItemName - the item to transform.

createPropertiesElementWithChildren

protected org.w3c.dom.Element createPropertiesElementWithChildren(org.w3c.dom.Element pItem,
                                                                  java.util.Collection<org.w3c.dom.Element> pChildren)
Create a new properties element with the specified children. Attempts to copy over any-preceding comments, as well.

Parameters:
pItem - the parent item to add a new "properties" element to.
pChildren - the children to add as children of the new "properties" child element.

applyOverrideProperties

protected void applyOverrideProperties(java.lang.String pParentPropertyPath,
                                       java.util.Properties pOverrideProperties,
                                       java.util.Map<java.lang.String,org.w3c.dom.Element> pPropNameToElem)
Apply any override properties to the map of properties.

Parameters:
pParentPropertyPath - the path of the parent properties.
pOverrideProperties - the attribute override properties file
pPropNameToElement - the map mapping from property name to element.

appendElementAndPrecedingComments

protected void appendElementAndPrecedingComments(org.w3c.dom.Element pElement,
                                                 java.lang.String pElementNameBefore,
                                                 org.w3c.dom.Node pNewParent)
Utility method to append the corresponding element to pNewParent. Attempts to copy over comment and whitespace elements that occurent before pElement, as well.

Parameters:
pElement - the element to copy to pNewParent
pElementNameBefore - the element name of the child element of pNewParent that pElement should preceed. pElement will be inserted just before the first element with the the name pElementNameBefore, if any. Otherwise, pElement is appended as the last element of pNewParent.

getPreviousIndexOfNonCommentNonTextualSibling

protected int getPreviousIndexOfNonCommentNonTextualSibling(org.w3c.dom.Node pParent,
                                                            int pChildIndex)
Returns the index of the previous non-comment, non-text sibling of the node at pChildIndex in pParent's child NodeList.

Returns:
the index of the previous non-comment, non-text sibling. returns -1 if no such sibling could be found.

getIndexOfChildNode

protected int getIndexOfChildNode(org.w3c.dom.Node pNode)
Return the index of child node pNode in it's parent child nodes list. Returns -1 if pNode has no parent.

Parameters:
pNode - the node whose child index to find.
Returns:
the index of pChildNode in it's parent's child node list.

transformDocument

protected org.w3c.dom.Document transformDocument(java.io.File pSourceFile,
                                                 java.io.File pOverrideProperties)
                                          throws org.xml.sax.SAXException,
                                                 javax.xml.parsers.ParserConfigurationException,
                                                 java.io.IOException
Transform the specified IOC source file from ATG Search format to a more Endeca-oriented IOC format.

Parameters:
pSourceFile - the source file IOX file to transform.
pOverrideProperties - the properties file containing attribute overrides (may be null).
Returns:
the transformed DOM Document object.
Throws:
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationException
java.io.IOException

main

public static void main(java.lang.String[] pArgs)
                 throws org.xml.sax.SAXException,
                        javax.xml.parsers.ParserConfigurationException,
                        java.io.IOException
Implements the ATG Search style IOC to Endeca style IOC file as a command line tool. Usage is "[-props propsOverrideFile] [-o outputFile] ioc-file".

Throws:
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationException
java.io.IOException

getStringFromDocument

public static java.lang.String getStringFromDocument(org.w3c.dom.Document pDocument)
Returns string representation of the given document. Outer comments are omitted. (That is, comments before and after the root element). XML declaration is present, document type is also copied if present.

Parameters:
pDocument -
Returns: