|
| 1 | +/*BEGIN_COPYRIGHT_BLOCK |
| 2 | + * |
| 3 | + * Copyright (c) 2001-2008, JavaPLT group at Rice University (drjava@rice.edu) |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the |
| 14 | + * names of its contributors may be used to endorse or promote products |
| 15 | + * derived from this software without specific prior written permission. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 21 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * |
| 29 | + * This software is Open Source Initiative approved Open Source Software. |
| 30 | + * Open Source Initative Approved is a trademark of the Open Source Initiative. |
| 31 | + * |
| 32 | + * This file is part of DrJava. Download the current version of this project |
| 33 | + * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ |
| 34 | + * |
| 35 | + * END_COPYRIGHT_BLOCK*/ |
| 36 | + |
| 37 | +package edu.rice.cs.drjava.config; |
| 38 | + |
| 39 | +import edu.rice.cs.util.Lambda; |
| 40 | +import edu.rice.cs.drjava.DrJava; |
| 41 | +import edu.rice.cs.util.StringOps; |
| 42 | +import edu.rice.cs.util.FileOps; |
| 43 | +import java.util.HashSet; |
| 44 | +import java.util.Iterator; |
| 45 | +import java.io.*; |
| 46 | + |
| 47 | +/** Class representing files that are always up-to-date and that |
| 48 | + * can be inserted as variables in external processes. |
| 49 | + * |
| 50 | + * @version $Id$ |
| 51 | + */ |
| 52 | +public class EagerFileProperty extends DrJavaProperty { |
| 53 | + protected Lambda<File,Void> _getFile; |
| 54 | + /** Create an eager file property. */ |
| 55 | + public EagerFileProperty(String name, Lambda<File,Void> getFile) { |
| 56 | + super(name); |
| 57 | + _getFile = getFile; |
| 58 | + resetAttributes(); |
| 59 | + } |
| 60 | + |
| 61 | + /** Return the value of the property. If it is not current, update first. */ |
| 62 | + public String getCurrent() { |
| 63 | + update(); |
| 64 | + if (_value==null) { throw new IllegalArgumentException("DrJavaProperty value is null"); } |
| 65 | + _isCurrent = true; |
| 66 | + return _value; |
| 67 | + } |
| 68 | + |
| 69 | + /** Return the value. */ |
| 70 | + public String toString() { |
| 71 | + return getCurrent(); |
| 72 | + } |
| 73 | + |
| 74 | + /** Return true if the value is current. */ |
| 75 | + public boolean isCurrent() { return true; } |
| 76 | + |
| 77 | + /** Mark the value as stale. */ |
| 78 | + public void invalidate() { |
| 79 | + // nothing to do, but tell those who are listening |
| 80 | + invalidateOthers(new HashSet<DrJavaProperty>()); |
| 81 | + } |
| 82 | + |
| 83 | + /** @return true if the specified property is equal to this one. */ |
| 84 | + public boolean equals(Object other) { |
| 85 | + if (other == null || other.getClass() != this.getClass()) return false; |
| 86 | + EagerProperty o = (EagerProperty)other; |
| 87 | + return _name.equals(o._name) && (_isCurrent == o._isCurrent) && _value.equals(o._value); |
| 88 | + } |
| 89 | + |
| 90 | + /** @return the hash code. */ |
| 91 | + public int hashCode() { |
| 92 | + int result; |
| 93 | + result = _name.hashCode(); |
| 94 | + result = 31 * result + (_value.hashCode()); |
| 95 | + result = 31 * result + (_isCurrent?1:0); |
| 96 | + return result; |
| 97 | + } |
| 98 | + |
| 99 | + public void update() { |
| 100 | + try { |
| 101 | + File f; |
| 102 | + if (_attributes.get("dir").equals("/")) { |
| 103 | + f = _getFile.apply(null).getAbsoluteFile(); |
| 104 | + try { |
| 105 | + f = f.getCanonicalFile(); |
| 106 | + } |
| 107 | + catch(IOException ioe) { } |
| 108 | + _value = edu.rice.cs.util.StringOps.escapeSpacesWith1bHex(f.toString()); |
| 109 | + } |
| 110 | + else { |
| 111 | + f = FileOps.makeRelativeTo(_getFile.apply(null), |
| 112 | + new File(StringOps.unescapeSpacesWith1bHex(StringOps.replaceVariables(_attributes.get("dir"), PropertyMaps.ONLY, PropertyMaps.GET_CURRENT)))); |
| 113 | + _value = edu.rice.cs.util.StringOps.escapeSpacesWith1bHex(f.toString()); |
| 114 | + } |
| 115 | + } |
| 116 | + catch(IOException e) { _value = "Error."; } |
| 117 | + catch(SecurityException e) { _value = "Error."; } |
| 118 | + } |
| 119 | + |
| 120 | + public void resetAttributes() { |
| 121 | + _attributes.clear(); |
| 122 | + _attributes.put("dir", "/"); |
| 123 | + } |
| 124 | +} |
0 commit comments