Java tutorial
// Description: Java 7 XML SAX Element Handler for Service /* * CF Customer Relations Management model * * Copyright (c) 2014 Mark Sobkow * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * *********************************************************************** * * Code manufactured by MSS Code Factory */ package net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrmSaxLoader; import java.math.*; import java.sql.*; import java.text.*; import java.util.*; import net.sourceforge.msscodefactory.cflib.v2_1.CFLib.*; import org.apache.commons.codec.binary.Base64; import org.xml.sax.*; import net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrm.*; import net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrmObj.*; /* * CFCrmSaxLoaderServiceParse XML SAX Element Handler implementation * for Service. */ public class CFCrmSaxLoaderServiceHandler extends CFLibXmlCoreElementHandler { public CFCrmSaxLoaderServiceHandler(CFCrmSaxLoader saxLoader) { super(saxLoader); } public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { try { // Common XML Attributes String attrId = null; // Service Attributes String attrHostPort = null; String attrServiceType = null; // Service References ICFCrmClusterObj refCluster = null; ICFCrmHostNodeObj refHost = null; ICFCrmServiceTypeObj refServiceType = null; // Attribute Extraction String attrLocalName; int numAttrs; int idxAttr; final String S_ProcName = "startElement"; final String S_LocalName = "LocalName"; assert qName.equals("Service"); CFCrmSaxLoader saxLoader = (CFCrmSaxLoader) getParser(); if (saxLoader == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "getParser()"); } ICFCrmSchemaObj schemaObj = saxLoader.getSchemaObj(); if (schemaObj == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "getParser().getSchemaObj()"); } // Instantiate an edit buffer for the parsed information ICFCrmServiceEditObj editBuff = (ICFCrmServiceEditObj) schemaObj.getServiceTableObj().newInstance() .beginEdit(); // Extract Attributes numAttrs = attrs.getLength(); for (idxAttr = 0; idxAttr < numAttrs; idxAttr++) { attrLocalName = attrs.getLocalName(idxAttr); if (attrLocalName.equals("Id")) { if (attrId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("HostPort")) { if (attrHostPort != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrHostPort = attrs.getValue(idxAttr); } else if (attrLocalName.equals("ServiceType")) { if (attrServiceType != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrServiceType = attrs.getValue(idxAttr); } else if (attrLocalName.equals("schemaLocation")) { // ignored } else { throw CFLib.getDefaultExceptionFactory().newUnrecognizedAttributeException(getClass(), S_ProcName, getParser().getLocationInfo(), attrLocalName); } } // Ensure that required attributes have values if ((attrHostPort == null) || (attrHostPort.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "HostPort"); } // Save named attributes to context CFLibXmlCoreContext curContext = getParser().getCurContext(); curContext.putNamedValue("Id", attrId); curContext.putNamedValue("HostPort", attrHostPort); curContext.putNamedValue("ServiceType", attrServiceType); // Convert string attributes to native Java types // and apply the converted attributes to the editBuff. Integer natId; if ((attrId != null) && (attrId.length() > 0)) { natId = new Integer(Integer.parseInt(attrId)); } else { natId = null; } short natHostPort = Short.parseShort(attrHostPort); editBuff.setRequiredHostPort(natHostPort); // Get the scope/container object CFLibXmlCoreContext parentContext = curContext.getPrevContext(); Object scopeObj; if (parentContext != null) { scopeObj = parentContext.getNamedValue("Object"); } else { scopeObj = null; } // Resolve and apply optional Container reference if (scopeObj == null) { refHost = null; editBuff.setOptionalContainerHost(refHost); refCluster = editBuff.getRequiredOwnerCluster(); } else if (scopeObj instanceof ICFCrmHostNodeObj) { refHost = (ICFCrmHostNodeObj) scopeObj; editBuff.setOptionalContainerHost(refHost); refCluster = editBuff.getRequiredOwnerCluster(); } else { throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName, "scopeObj", scopeObj, "ICFCrmHostNodeObj"); } // Resolve and apply Owner reference if (refCluster == null) { if (scopeObj instanceof ICFCrmClusterObj) { refCluster = (ICFCrmClusterObj) scopeObj; editBuff.setRequiredOwnerCluster(refCluster); } else { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "Owner<Cluster>"); } } // Lookup refServiceType by key name value attr if ((attrServiceType != null) && (attrServiceType.length() > 0)) { refServiceType = schemaObj.getServiceTypeTableObj().readServiceTypeByUDescrIdx(attrServiceType); if (refServiceType == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "Resolve ServiceType reference named \"" + attrServiceType + "\" to table ServiceType"); } } else { refServiceType = null; } editBuff.setOptionalParentServiceType(refServiceType); ICFCrmServiceObj origService; ICFCrmServiceEditObj editService = editBuff; origService = (ICFCrmServiceObj) editService.create(); editService.endEdit(); curContext.putNamedValue("Object", origService); } catch (RuntimeException e) { throw new RuntimeException("Near " + getParser().getLocationInfo() + ": Caught and rethrew " + e.getClass().getName() + " - " + e.getMessage(), e); } catch (Error e) { throw new Error("Near " + getParser().getLocationInfo() + ": Caught and rethrew " + e.getClass().getName() + " - " + e.getMessage(), e); } } public void endElement(String uri, String localName, String qName) throws SAXException { } }