1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.eclipse.adt.internal.refactoring.core; 18 19 import com.android.ide.eclipse.adt.AdtPlugin; 20 import com.android.sdklib.SdkConstants; 21 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.jface.text.IDocument; 25 import org.eclipse.wst.sse.core.StructuredModelManager; 26 import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; 27 import org.w3c.dom.Attr; 28 import org.w3c.dom.NamedNodeMap; 29 import org.w3c.dom.Node; 30 31 import java.io.IOException; 32 import java.io.UnsupportedEncodingException; 33 34 /** 35 * The utility class for android refactoring 36 * 37 */ 38 @SuppressWarnings("restriction") 39 public class RefactoringUtil { 40 41 private static boolean sRefactorAppPackage = false; 42 43 /** 44 * Releases SSE read model; saves SSE model if exists edit model 45 * Called in dispose method of refactoring change classes 46 * 47 * @param model the SSE model 48 * @param document the document 49 */ 50 public static void fixModel(IStructuredModel model, IDocument document) { 51 if (model != null) { 52 model.releaseFromRead(); 53 } 54 model = null; 55 if (document == null) { 56 return; 57 } 58 try { 59 model = StructuredModelManager.getModelManager().getExistingModelForEdit(document); 60 if (model != null) { 61 model.save(); 62 } 63 } catch (UnsupportedEncodingException e1) { 64 // ignore 65 } catch (IOException e1) { 66 // ignore 67 } catch (CoreException e1) { 68 // ignore 69 } finally { 70 if (model != null) { 71 model.releaseFromEdit(); 72 } 73 } 74 } 75 76 /** 77 * Finds attribute by name in android namespace 78 * 79 * @param attributes the attributes collection 80 * @param localName the local part of the qualified name 81 * 82 * @return the first attribute with this name in android namespace 83 */ 84 public static Attr findAndroidAttributes(final NamedNodeMap attributes, 85 final String localName) { 86 Attr attribute = null; 87 for (int j = 0; j < attributes.getLength(); j++) { 88 Node attNode = attributes.item(j); 89 if (attNode instanceof Attr) { 90 Attr attr = (Attr) attNode; 91 String name = attr.getLocalName(); 92 String namespace = attr.getNamespaceURI(); 93 if (SdkConstants.NS_RESOURCES.equals(namespace) 94 && name != null 95 && name.equals(localName)) { 96 attribute = attr; 97 break; 98 } 99 } 100 } 101 return attribute; 102 } 103 104 /** 105 * Logs the error message 106 * 107 * @param message the message 108 */ 109 public static void logError(String message) { 110 AdtPlugin.log(IStatus.ERROR, AdtPlugin.PLUGIN_ID, message); 111 } 112 113 /** 114 * Logs the info message 115 * 116 * @param message the message 117 */ 118 public static void logInfo(String message) { 119 AdtPlugin.log(IStatus.INFO, AdtPlugin.PLUGIN_ID, message); 120 } 121 122 /** 123 * Logs the the exception 124 * 125 * @param e the exception 126 */ 127 public static void log(Throwable e) { 128 AdtPlugin.log(e, e.getMessage()); 129 } 130 131 /** 132 * @return true if Rename/Move package needs to change the application package 133 * default is false 134 * 135 */ 136 public static boolean isRefactorAppPackage() { 137 return sRefactorAppPackage; 138 } 139 140 /** 141 * @param refactorAppPackage true if Rename/Move package needs to change the application package 142 */ 143 public static void setRefactorAppPackage(boolean refactorAppPackage) { 144 RefactoringUtil.sRefactorAppPackage = refactorAppPackage; 145 } 146 } 147