1 /******************************************************************************* 2 * Copyright (c) 2000, 2015 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 * James Blackburn (Broadcom Corp.) - ongoing development 14 * Lars Vogel <Lars.Vogel@vogella.com> - Bug 473427 15 *******************************************************************************/ 16 package org.eclipse.core.internal.resources; 17 18 import java.io.IOException; 19 import java.io.InputStream; 20 import java.util.ArrayList; 21 import java.util.List; 22 import javax.xml.parsers.*; 23 import org.eclipse.core.internal.localstore.SafeFileInputStream; 24 import org.eclipse.core.internal.utils.Messages; 25 import org.eclipse.core.internal.utils.Policy; 26 import org.eclipse.core.resources.IResourceStatus; 27 import org.eclipse.core.runtime.IPath; 28 import org.eclipse.osgi.util.NLS; 29 import org.w3c.dom.*; 30 import org.xml.sax.SAXException; 31 32 /** 33 * This class contains legacy code only. It is being used to read workspace 34 * descriptions which are obsolete. 35 */ 36 public class WorkspaceDescriptionReader implements IModelObjectConstants { 37 /** constants */ 38 protected static final String[] EMPTY_STRING_ARRAY = new String[0]; 39 WorkspaceDescriptionReader()40 public WorkspaceDescriptionReader() { 41 super(); 42 } 43 getString(Node target, String tagName)44 protected String getString(Node target, String tagName) { 45 Node node = searchNode(target, tagName); 46 return node != null ? (node.getFirstChild() == null ? null : node.getFirstChild().getNodeValue()) : null; 47 } 48 getStrings(Node target)49 protected String[] getStrings(Node target) { 50 if (target == null) 51 return null; 52 NodeList list = target.getChildNodes(); 53 if (list.getLength() == 0) 54 return EMPTY_STRING_ARRAY; 55 List<Object> result = new ArrayList<>(list.getLength()); 56 for (int i = 0; i < list.getLength(); i++) { 57 Node node = list.item(i); 58 if (node.getNodeType() == Node.ELEMENT_NODE) 59 result.add(read(node.getChildNodes().item(0))); 60 } 61 return result.toArray(new String[result.size()]); 62 } 63 64 /** 65 * A value was discovered in the workspace description file that was not a number. 66 * Log the exception. 67 */ logNumberFormatException(String value, NumberFormatException e)68 private void logNumberFormatException(String value, NumberFormatException e) { 69 String msg = NLS.bind(Messages.resources_readWorkspaceMetaValue, value); 70 Policy.log(new ResourceStatus(IResourceStatus.FAILED_READ_METADATA, null, msg, e)); 71 } 72 read(InputStream input)73 public Object read(InputStream input) { 74 try { 75 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 76 Document document = parser.parse(input); 77 return read(document.getFirstChild()); 78 } catch (IOException | SAXException | ParserConfigurationException e) { 79 // ignore 80 } 81 return null; 82 } 83 read(IPath location, IPath tempLocation)84 public Object read(IPath location, IPath tempLocation) throws IOException { 85 try ( 86 SafeFileInputStream file = new SafeFileInputStream(location.toOSString(), tempLocation.toOSString()); 87 ) { 88 return read(file); 89 } 90 } 91 read(Node node)92 protected Object read(Node node) { 93 if (node == null) 94 return null; 95 switch (node.getNodeType()) { 96 case Node.ELEMENT_NODE : 97 if (node.getNodeName().equals(WORKSPACE_DESCRIPTION)) 98 return readWorkspaceDescription(node); 99 case Node.TEXT_NODE : 100 String value = node.getNodeValue(); 101 return value == null ? null : value.trim(); 102 default : 103 return node.toString(); 104 } 105 } 106 107 /** 108 * read (String, String) hashtables 109 */ readWorkspaceDescription(Node node)110 protected WorkspaceDescription readWorkspaceDescription(Node node) { 111 // get values 112 String name = getString(node, NAME); 113 String autobuild = getString(node, AUTOBUILD); 114 String snapshotInterval = getString(node, SNAPSHOT_INTERVAL); 115 String applyFileStatePolicy = getString(node, APPLY_FILE_STATE_POLICY); 116 String fileStateLongevity = getString(node, FILE_STATE_LONGEVITY); 117 String maxFileStateSize = getString(node, MAX_FILE_STATE_SIZE); 118 String maxFileStates = getString(node, MAX_FILE_STATES); 119 String[] buildOrder = getStrings(searchNode(node, BUILD_ORDER)); 120 121 // build instance 122 //invalid values are skipped and defaults are used instead 123 WorkspaceDescription description = new WorkspaceDescription(name); 124 if (autobuild != null) 125 //if in doubt (value is corrupt) we want autobuild on 126 description.setAutoBuilding(!autobuild.equals(Integer.toString(0))); 127 if (applyFileStatePolicy != null) 128 //if in doubt (value is corrupt) we want applyFileLimits on 129 description.setApplyFileStatePolicy(!applyFileStatePolicy.equals(Integer.toString(0))); 130 try { 131 if (fileStateLongevity != null) 132 description.setFileStateLongevity(Long.parseLong(fileStateLongevity)); 133 } catch (NumberFormatException e) { 134 logNumberFormatException(fileStateLongevity, e); 135 } 136 try { 137 if (maxFileStateSize != null) 138 description.setMaxFileStateSize(Long.parseLong(maxFileStateSize)); 139 } catch (NumberFormatException e) { 140 logNumberFormatException(maxFileStateSize, e); 141 } 142 try { 143 if (maxFileStates != null) 144 description.setMaxFileStates(Integer.parseInt(maxFileStates)); 145 } catch (NumberFormatException e) { 146 logNumberFormatException(maxFileStates, e); 147 } 148 if (buildOrder != null) 149 description.internalSetBuildOrder(buildOrder); 150 try { 151 if (snapshotInterval != null) 152 description.setSnapshotInterval(Long.parseLong(snapshotInterval)); 153 } catch (NumberFormatException e) { 154 logNumberFormatException(snapshotInterval, e); 155 } 156 return description; 157 } 158 searchNode(Node target, String tagName)159 protected Node searchNode(Node target, String tagName) { 160 NodeList list = target.getChildNodes(); 161 for (int i = 0; i < list.getLength(); i++) { 162 if (list.item(i).getNodeName().equals(tagName)) 163 return list.item(i); 164 } 165 return null; 166 } 167 } 168