/* * Copyright 2000-2009 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.napile.idea.thermit.config.impl; import java.util.Comparator; import org.jdom.Element; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.Nullable; import org.napile.idea.thermit.ThermitBundle; import com.intellij.execution.CantRunException; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.InvalidDataException; import com.intellij.util.config.AbstractProperty; import com.intellij.util.config.Externalizer; public abstract class AntReference { private static final Logger LOG = Logger.getInstance("#org.napile.idea.thermit.config.impl.AntReference"); @NonNls private static final String PROJECT_DEFAULT_ATTR = "projectDefault"; @NonNls private static final String NAME_ATTR = "name"; @NonNls private static final String BUNDLED_ANT_ATTR = "bundledAnt"; public static final Externalizer<AntReference> EXTERNALIZER = new Externalizer<AntReference>() { public AntReference readValue(Element dataElement) throws InvalidDataException { if(Boolean.valueOf(dataElement.getAttributeValue(PROJECT_DEFAULT_ATTR)).booleanValue()) return PROJECT_DEFAULT; if(Boolean.valueOf(dataElement.getAttributeValue(BUNDLED_ANT_ATTR)).booleanValue()) return BUNDLED_ANT; String name = dataElement.getAttributeValue(NAME_ATTR); if(name == null) throw new InvalidDataException(); return new MissingAntReference(name); } public void writeValue(Element dataElement, AntReference antReference) { antReference.writeExternal(dataElement); } }; public static final Comparator<AntReference> COMPARATOR = new Comparator<AntReference>() { public int compare(AntReference reference, AntReference reference1) { if(reference.equals(reference1)) return 0; if(reference == BUNDLED_ANT) return -1; if(reference1 == BUNDLED_ANT) return 1; return reference.getName().compareToIgnoreCase(reference1.getName()); } }; protected abstract void writeExternal(Element dataElement); public String toString() { return getName(); } public static final AntReference PROJECT_DEFAULT = new AntReference() { protected void writeExternal(Element dataElement) { dataElement.setAttribute(PROJECT_DEFAULT_ATTR, Boolean.TRUE.toString()); } public AntInstallation find(GlobalThermitConfiguration ants) { throw new UnsupportedOperationException("Should not call"); } public AntReference bind(GlobalThermitConfiguration antConfiguration) { return this; } public String getName() { throw new UnsupportedOperationException("Should not call"); } @SuppressWarnings({"HardCodedStringLiteral"}) public String toString() { return "PROJECT_DEFAULT"; } public boolean equals(Object obj) { return obj == this; } }; public static final AntReference BUNDLED_ANT = new AntReference() { protected void writeExternal(Element dataElement) { dataElement.setAttribute(BUNDLED_ANT_ATTR, Boolean.TRUE.toString()); } public boolean equals(Object obj) { return obj == this; } public String getName() { return GlobalThermitConfiguration.BUNDLED_ANT_NAME; } public AntInstallation find(GlobalThermitConfiguration antConfiguration) { return antConfiguration.getBundledAnt(); } public AntReference bind(GlobalThermitConfiguration antConfiguration) { return this; } }; public abstract String getName(); public abstract AntInstallation find(GlobalThermitConfiguration antConfiguration); public abstract AntReference bind(GlobalThermitConfiguration antConfiguration); public int hashCode() { return getName().hashCode(); } public boolean equals(Object obj) { if(obj == PROJECT_DEFAULT) return this == PROJECT_DEFAULT; if(obj == BUNDLED_ANT) return this == BUNDLED_ANT; return obj instanceof AntReference && Comparing.equal(getName(), ((AntReference) obj).getName()); } @Nullable public static AntInstallation findAnt(AbstractProperty<AntReference> property, AbstractProperty.AbstractPropertyContainer container) { GlobalThermitConfiguration antConfiguration = GlobalThermitConfiguration.INSTANCE.get(container); LOG.assertTrue(antConfiguration != null); AntReference antReference = property.get(container); if(antReference == PROJECT_DEFAULT) { antReference = ThermitConfigurationImpl.DEFAULT_ANT.get(container); } if(antReference == null) return null; return antReference.find(antConfiguration); } public static AntInstallation findNotNullAnt(AbstractProperty<AntReference> property, AbstractProperty.AbstractPropertyContainer container, GlobalThermitConfiguration antConfiguration) throws CantRunException { AntReference antReference = property.get(container); if(antReference == PROJECT_DEFAULT) antReference = ThermitConfigurationImpl.DEFAULT_ANT.get(container); if(antReference == null) throw new CantRunException(ThermitBundle.message("cant.run.ant.no.ant.configured.error.message")); AntInstallation antInstallation = antReference.find(antConfiguration); if(antInstallation == null) { throw new CantRunException(ThermitBundle.message("cant.run.ant.ant.reference.is.not.configured.error.message", antReference.getName())); } return antInstallation; } @Nullable public static AntInstallation findAntOrBundled(AbstractProperty.AbstractPropertyContainer container) { GlobalThermitConfiguration antConfiguration = GlobalThermitConfiguration.INSTANCE.get(container); if(container.hasProperty(AntBuildFileImpl.ANT_REFERENCE)) return findAnt(AntBuildFileImpl.ANT_REFERENCE, container); return antConfiguration.getBundledAnt(); } static class MissingAntReference extends AntReference { private final String myName; public MissingAntReference(String name) { myName = name; } protected void writeExternal(Element dataElement) { dataElement.setAttribute(NAME_ATTR, myName); } public String getName() { return myName; } public AntInstallation find(GlobalThermitConfiguration antConfiguration) { return antConfiguration.getConfiguredAnts().get(this); } public AntReference bind(GlobalThermitConfiguration antConfiguration) { AntInstallation antInstallation = find(antConfiguration); if(antInstallation != null) return new BindedReference(antInstallation); return this; } } static class BindedReference extends AntReference { private final AntInstallation myAnt; public BindedReference(AntInstallation ant) { myAnt = ant; } public AntInstallation find(GlobalThermitConfiguration antConfiguration) { return myAnt; } public String getName() { return myAnt.getName(); } protected void writeExternal(Element dataElement) { dataElement.setAttribute(NAME_ATTR, getName()); } public AntReference bind(GlobalThermitConfiguration antConfiguration) { return this; } } }