| 1 | /******************************************************************************* | 
| 2 |  * Copyright (c) 2007, 2009 IBM Corporation and others. | 
| 3 |  * All rights reserved. This program and the accompanying materials | 
| 4 |  * are made available under the terms of the Eclipse Public License v1.0 | 
| 5 |  * which accompanies this distribution, and is available at | 
| 6 |  * http://www.eclipse.org/legal/epl-v10.html | 
| 7 |  *  | 
| 8 |  * Contributors: | 
| 9 |  *     IBM Corporation - initial API and implementation | 
| 10 |  *******************************************************************************/ | 
| 11 | package org.eclipse.pde.api.tools.internal.comparator; | 
| 12 |   | 
| 13 | import org.eclipse.core.runtime.CoreException; | 
| 14 | import org.eclipse.pde.api.tools.internal.IApiXmlConstants; | 
| 15 | import org.eclipse.pde.api.tools.internal.provisional.comparator.ApiComparator; | 
| 16 | import org.eclipse.pde.api.tools.internal.provisional.comparator.DeltaProcessor; | 
| 17 | import org.eclipse.pde.api.tools.internal.provisional.comparator.DeltaVisitor; | 
| 18 | import org.eclipse.pde.api.tools.internal.provisional.comparator.IDelta; | 
| 19 | import org.eclipse.pde.api.tools.internal.util.Util; | 
| 20 | import org.w3c.dom.Document; | 
| 21 | import org.w3c.dom.Element; | 
| 22 |   | 
| 23 | /** | 
| 24 |  * Delta visitor that generates XML for the delta. | 
| 25 |  *   | 
| 26 |  * @since 1.0.0 | 
| 27 |  */ | 
| 28 | public class DeltaXmlVisitor extends DeltaVisitor { | 
| 29 |         /** | 
| 30 |          * XML doc being generated | 
| 31 |          */ | 
| 32 |         private Document fDoc; | 
| 33 |   | 
| 34 |         /** | 
| 35 |          * Top deltas element | 
| 36 |          */ | 
| 37 |         private Element fDeltas; | 
| 38 |          | 
| 39 |   | 
| 40 |         /** | 
| 41 |          * Constructs a new visitor for the given component. | 
| 42 |          *  | 
| 43 |          * @param component API component | 
| 44 |          * @throws CoreException if unable to construct the visitor | 
| 45 |          */ | 
| 46 |         public DeltaXmlVisitor() throws CoreException { | 
| 47 |                 fDoc = Util.newDocument(); | 
| 48 |                 fDeltas = fDoc.createElement(IApiXmlConstants.DELTAS_ELEMENT_NAME); | 
| 49 |                 fDoc.appendChild(fDeltas); | 
| 50 |         } | 
| 51 |   | 
| 52 |         /* (non-Javadoc) | 
| 53 |          * @see org.eclipse.pde.api.tools.internal.provisional.comparator.DeltaVisitor#visit(org.eclipse.pde.api.tools.internal.provisional.comparator.IDelta) | 
| 54 |          */ | 
| 55 |         public boolean visit(IDelta delta) { | 
| 56 |                 if (delta == ApiComparator.NO_DELTA) return false; | 
| 57 |                 if (delta.getChildren().length == 0) { | 
| 58 |                         processLeafDelta(delta); | 
| 59 |                 } | 
| 60 |                 return true; | 
| 61 |         } | 
| 62 |   | 
| 63 |         protected void processLeafDelta(IDelta delta) { | 
| 64 |                 Element deltaElement = fDoc.createElement(IApiXmlConstants.DELTA_ELEMENT_NAME); | 
| 65 |                 deltaElement.setAttribute(IApiXmlConstants.ATTR_FLAGS, Integer.toString(delta.getFlags())); | 
| 66 |                 deltaElement.setAttribute(IApiXmlConstants.ATTR_KIND, Util.getDeltaKindName(delta)); | 
| 67 |                 deltaElement.setAttribute(IApiXmlConstants.ATTR_NAME_ELEMENT_TYPE, Util.getDeltaElementType(delta)); | 
| 68 |                 deltaElement.setAttribute(IApiXmlConstants.ATTR_KEY, delta.getKey()); | 
| 69 |                 String typeName = delta.getTypeName(); | 
| 70 |                 if (typeName != null) { | 
| 71 |                         deltaElement.setAttribute(IApiXmlConstants.ATTR_NAME_TYPE_NAME, typeName); | 
| 72 |                 } | 
| 73 |                 deltaElement.setAttribute(IApiXmlConstants.ATTR_NAME_COMPATIBLE, Boolean.toString(DeltaProcessor.isCompatible(delta))); | 
| 74 |                 deltaElement.setAttribute(IApiXmlConstants.ATTR_NAME_OLD_MODIFIERS, Integer.toString(delta.getOldModifiers())); | 
| 75 |                 deltaElement.setAttribute(IApiXmlConstants.ATTR_NAME_NEW_MODIFIERS, Integer.toString(delta.getNewModifiers())); | 
| 76 |                 deltaElement.setAttribute(IApiXmlConstants.ATTR_RESTRICTIONS, Integer.toString(delta.getRestrictions())); | 
| 77 |                 String apiComponentID = delta.getComponentVersionId(); | 
| 78 |                 if (apiComponentID != null) { | 
| 79 |                         deltaElement.setAttribute(IApiXmlConstants.ATTR_NAME_COMPONENT_ID, apiComponentID); | 
| 80 |                 } | 
| 81 |                 deltaElement.setAttribute(IApiXmlConstants.ATTR_MESSAGE, delta.getMessage()); | 
| 82 |                 String[] messageArguments = delta.getArguments(); | 
| 83 |                 int length = messageArguments.length; | 
| 84 |                 if(length > 0) { | 
| 85 |                         Element messageArgumentsElement = fDoc.createElement(IApiXmlConstants.ELEMENT_DELTA_MESSAGE_ARGUMENTS); | 
| 86 |                         for (int j = 0; j < length; j++) { | 
| 87 |                                 Element messageArgumentElement = fDoc.createElement(IApiXmlConstants.ELEMENT_DELTA_MESSAGE_ARGUMENT); | 
| 88 |                                 messageArgumentElement.setAttribute(IApiXmlConstants.ATTR_VALUE, String.valueOf(messageArguments[j])); | 
| 89 |                                 messageArgumentsElement.appendChild(messageArgumentElement); | 
| 90 |                         } | 
| 91 |                         deltaElement.appendChild(messageArgumentsElement); | 
| 92 |                 } | 
| 93 |                 fDeltas.appendChild(deltaElement); | 
| 94 |         } | 
| 95 |   | 
| 96 |         /* (non-Javadoc) | 
| 97 |          * @see org.eclipse.pde.api.tools.internal.provisional.comparator.DeltaVisitor#endVisit(org.eclipse.pde.api.tools.internal.provisional.comparator.IDelta) | 
| 98 |          */ | 
| 99 |         public void endVisit(IDelta delta) { | 
| 100 |                 // nothing to do | 
| 101 |         } | 
| 102 |          | 
| 103 |         /** | 
| 104 |          * Returns the settings as a UTF-8 string containing XML. | 
| 105 |          *  | 
| 106 |          * @return XML | 
| 107 |          * @throws CoreException if something goes wrong  | 
| 108 |          */ | 
| 109 |         public String getXML() throws CoreException { | 
| 110 |                 return Util.serializeDocument(fDoc); | 
| 111 |         } | 
| 112 | } |