1 11 package org.eclipse.core.internal.resources; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 import javax.xml.parsers.*; 18 import org.eclipse.core.internal.localstore.SafeFileInputStream; 19 import org.eclipse.core.internal.utils.Messages; 20 import org.eclipse.core.internal.utils.Policy; 21 import org.eclipse.core.resources.IResourceStatus; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.osgi.util.NLS; 24 import org.w3c.dom.*; 25 import org.xml.sax.SAXException ; 26 27 31 public class WorkspaceDescriptionReader implements IModelObjectConstants { 32 33 protected static final String [] EMPTY_STRING_ARRAY = new String [0]; 34 35 public WorkspaceDescriptionReader() { 36 super(); 37 } 38 39 protected String getString(Node target, String tagName) { 40 Node node = searchNode(target, tagName); 41 return node != null ? (node.getFirstChild() == null ? null : node.getFirstChild().getNodeValue()) : null; 42 } 43 44 protected String [] getStrings(Node target) { 45 if (target == null) 46 return null; 47 NodeList list = target.getChildNodes(); 48 if (list.getLength() == 0) 49 return EMPTY_STRING_ARRAY; 50 List result = new ArrayList (list.getLength()); 51 for (int i = 0; i < list.getLength(); i++) { 52 Node node = list.item(i); 53 if (node.getNodeType() == Node.ELEMENT_NODE) 54 result.add(read(node.getChildNodes().item(0))); 55 } 56 return (String []) result.toArray(new String [result.size()]); 57 } 58 59 63 private void logNumberFormatException(String value, NumberFormatException e) { 64 String msg = NLS.bind(Messages.resources_readWorkspaceMetaValue, value); 65 Policy.log(new ResourceStatus(IResourceStatus.FAILED_READ_METADATA, null, msg, e)); 66 } 67 68 public Object read(InputStream input) { 69 try { 70 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 71 Document document = parser.parse(input); 72 return read(document.getFirstChild()); 73 } catch (IOException e) { 74 } catch (SAXException e) { 76 } catch (ParserConfigurationException e) { 78 } 80 return null; 81 } 82 83 public Object read(IPath location, IPath tempLocation) throws IOException { 84 SafeFileInputStream file = new SafeFileInputStream(location.toOSString(), tempLocation.toOSString()); 85 try { 86 return read(file); 87 } finally { 88 file.close(); 89 } 90 } 91 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 110 protected WorkspaceDescription readWorkspaceDescription(Node node) { 111 String name = getString(node, NAME); 113 String autobuild = getString(node, AUTOBUILD); 114 String snapshotInterval = getString(node, SNAPSHOT_INTERVAL); 115 String fileStateLongevity = getString(node, FILE_STATE_LONGEVITY); 116 String maxFileStateSize = getString(node, MAX_FILE_STATE_SIZE); 117 String maxFileStates = getString(node, MAX_FILE_STATES); 118 String [] buildOrder = getStrings(searchNode(node, BUILD_ORDER)); 119 120 WorkspaceDescription description = new WorkspaceDescription(name); 123 if (autobuild != null) 124 description.setAutoBuilding(!autobuild.equals(Integer.toString(0))); 126 try { 127 if (fileStateLongevity != null) 128 description.setFileStateLongevity(Long.parseLong(fileStateLongevity)); 129 } catch (NumberFormatException e) { 130 logNumberFormatException(fileStateLongevity, e); 131 } 132 try { 133 if (maxFileStateSize != null) 134 description.setMaxFileStateSize(Long.parseLong(maxFileStateSize)); 135 } catch (NumberFormatException e) { 136 logNumberFormatException(maxFileStateSize, e); 137 } 138 try { 139 if (maxFileStates != null) 140 description.setMaxFileStates(Integer.parseInt(maxFileStates)); 141 } catch (NumberFormatException e) { 142 logNumberFormatException(maxFileStates, e); 143 } 144 if (buildOrder != null) 145 description.internalSetBuildOrder(buildOrder); 146 try { 147 if (snapshotInterval != null) 148 description.setSnapshotInterval(Long.parseLong(snapshotInterval)); 149 } catch (NumberFormatException e) { 150 logNumberFormatException(snapshotInterval, e); 151 } 152 return description; 153 } 154 155 protected Node searchNode(Node target, String tagName) { 156 NodeList list = target.getChildNodes(); 157 for (int i = 0; i < list.getLength(); i++) { 158 if (list.item(i).getNodeName().equals(tagName)) 159 return list.item(i); 160 } 161 return null; 162 } 163 } 164 | Popular Tags |