void setRootElement(@NotNull JpsCompositePackagingElement rootElement);
+ boolean isBuildOnMake();
+
@NotNull
@Override
JpsArtifactReference createReference();
+
+ void setBuildOnMake(boolean buildOnMake);
}
ROOT_ELEMENT_CHILD_ROLE = JpsElementChildRoleBase.create("root element");
private static final JpsTypedDataRole<JpsArtifactType> TYPED_DATA_ROLE = new JpsTypedDataRole<JpsArtifactType>();
private String myOutputPath;
+ private boolean myBuildOnMake;
+
public JpsArtifactImpl(@NotNull String name, @NotNull JpsCompositePackagingElement rootElement, @NotNull JpsArtifactType type) {
super(name);
public void setRootElement(@NotNull JpsCompositePackagingElement rootElement) {
myContainer.setChild(ROOT_ELEMENT_CHILD_ROLE, rootElement);
}
+
+ @Override
+ public boolean isBuildOnMake() {
+ return myBuildOnMake;
+ }
+
+ @Override
+ public void setBuildOnMake(boolean buildOnMake) {
+ if (myBuildOnMake != buildOnMake) {
+ myBuildOnMake = buildOnMake;
+ fireElementChanged();
+ }
+ }
}
public abstract class JpsNamedElementReferenceBase<T extends JpsNamedElement, Self extends JpsNamedElementReferenceBase<T, Self>>
extends JpsCompositeElementBase<Self> implements JpsElementReference<T> {
private static final JpsElementChildRole<JpsElementReference<? extends JpsCompositeElement>> PARENT_REFERENCE_ROLE = JpsElementChildRoleBase.create("parent");
- private final JpsElementCollectionRole<? extends T> myCollectionRole;
+ protected final JpsElementCollectionRole<? extends T> myCollectionRole;
protected final String myElementName;
protected JpsNamedElementReferenceBase(@NotNull JpsElementCollectionRole<? extends T> role,
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-import org.jetbrains.jps.model.DummyJpsElementProperties;
-import org.jetbrains.jps.model.JpsElementFactory;
-import org.jetbrains.jps.model.JpsElementProperties;
+import org.jetbrains.jps.model.*;
import org.jetbrains.jps.model.java.JpsJavaLibraryType;
import org.jetbrains.jps.model.library.*;
+import org.jetbrains.jps.model.module.JpsModuleReference;
import java.util.*;
public void saveProperties(DummyJpsElementProperties properties, Element element) {
}
};
+ private static final String MODULE_LEVEL = "module";
+ private static final String PROJECT_LEVEL = "project";
+ private static final String APPLICATION_LEVEL = "application";
public static void loadLibraries(Element libraryTableElement, JpsLibraryCollection result) {
for (Element libraryElement : JDOMUtil.getChildren(libraryTableElement, LIBRARY_TAG)) {
}
throw new IllegalArgumentException("unknown type library:" + type);
}
+
+ public static JpsElementReference<? extends JpsCompositeElement> createLibraryTableReference(String level) {
+ JpsElementFactory elementFactory = JpsElementFactory.getInstance();
+ if (level.equals(PROJECT_LEVEL)) {
+ return elementFactory.createProjectReference();
+ }
+ if (level.equals(APPLICATION_LEVEL)) {
+ return elementFactory.createGlobalReference();
+ }
+ for (JpsModelSerializerExtension extension : JpsModelSerializerExtension.getExtensions()) {
+ final JpsElementReference<? extends JpsCompositeElement> reference = extension.createLibraryTableReference(level);
+ if (reference != null) {
+ return reference;
+ }
+ }
+ throw new UnsupportedOperationException();
+ }
+
+ public static String getLevelId(JpsElementReference<? extends JpsCompositeElement> reference) {
+ if (reference instanceof JpsModuleReference) {
+ return MODULE_LEVEL;
+ }
+ JpsCompositeElement element = reference.resolve();
+ if (element instanceof JpsProject) {
+ return PROJECT_LEVEL;
+ }
+ else if (element instanceof JpsGlobal) {
+ return APPLICATION_LEVEL;
+ }
+ for (JpsModelSerializerExtension extension : JpsModelSerializerExtension.getExtensions()) {
+ String levelId = extension.getLibraryTableLevelId(reference);
+ if (levelId != null) {
+ return levelId;
+ }
+ }
+ return null;
+ }
}
import org.jetbrains.jps.model.JpsCompositeElement;
import org.jetbrains.jps.model.JpsElementReference;
import org.jetbrains.jps.model.JpsProject;
-import org.jetbrains.jps.model.artifact.JpsArtifactType;
import org.jetbrains.jps.model.module.JpsDependencyElement;
import org.jetbrains.jps.model.module.JpsModule;
-import org.jetbrains.jps.model.serialization.artifact.JpsPackagingElementLoader;
+import org.jetbrains.jps.model.serialization.artifact.JpsArtifactTypeSerializer;
+import org.jetbrains.jps.model.serialization.artifact.JpsPackagingElementSerializer;
import org.jetbrains.jps.model.serialization.facet.JpsModuleExtensionLoader;
import org.jetbrains.jps.service.JpsServiceManager;
return Collections.emptyList();
}
- public List<? extends JpsPackagingElementLoader<?>> getPackagingElementLoaders() {
+ public List<? extends JpsPackagingElementSerializer<?>> getPackagingElementSerializers() {
return Collections.emptyList();
}
- @Nullable
- public JpsArtifactType getArtifactType(@NotNull String typeId) {
- return null;
+ public List<JpsArtifactTypeSerializer> getArtifactTypeSerializers() {
+ return Collections.emptyList();
}
}
private static final String MODULE_LIBRARY_TYPE = "module-library";
private static final String MODULE_TYPE = "module";
private static final String MODULE_NAME_ATTRIBUTE = "module-name";
- private static final String PROJECT_LEVEL = "project";
- private static final String APPLICATION_LEVEL = "application";
private static final String GENERATED_LIBRARY_NAME_PREFIX = "#";
public static void loadRootModel(JpsModule module, Element rootModelComponent, JpsSdkType<?> projectSdkType) {
String name = orderEntry.getAttributeValue(NAME_ATTRIBUTE);
String level = orderEntry.getAttributeValue(LEVEL_ATTRIBUTE);
final JpsLibraryDependency dependency =
- dependenciesList.addLibraryDependency(elementFactory.createLibraryReference(name, createLibraryTableReference(level)));
+ dependenciesList.addLibraryDependency(elementFactory.createLibraryReference(name, JpsLibraryTableSerializer
+ .createLibraryTableReference(level)));
loadModuleDependencyProperties(dependency, orderEntry);
}
else if (MODULE_LIBRARY_TYPE.equals(type)) {
JpsLibrary library = reference.resolve();
String libraryName = library.getName();
JpsLibraryTableSerializer
- .saveLibrary(library, libraryElement, libraryName.startsWith(GENERATED_LIBRARY_NAME_PREFIX) ? null : libraryName);
+ .saveLibrary(library, libraryElement, isGeneratedName(libraryName) ? null : libraryName);
element.addContent(libraryElement);
}
else {
element = createDependencyElement(LIBRARY_TYPE);
saveModuleDependencyProperties(dependency, element);
element.setAttribute(NAME_ATTRIBUTE, reference.getLibraryName());
- element.setAttribute(LEVEL_ATTRIBUTE, getLevelId(parentReference));
+ element.setAttribute(LEVEL_ATTRIBUTE, JpsLibraryTableSerializer.getLevelId(parentReference));
}
rootModelElement.addContent(element);
}
}
}
+ private static boolean isGeneratedName(String libraryName) {
+ return libraryName.startsWith(GENERATED_LIBRARY_NAME_PREFIX);
+ }
+
private static Element createDependencyElement(final String type) {
return new Element(ORDER_ENTRY_TAG).setAttribute(TYPE_ATTRIBUTE, type);
}
extension.saveModuleDependencyProperties(dependency, orderEntry);
}
}
-
- public static JpsElementReference<? extends JpsCompositeElement> createLibraryTableReference(String level) {
- JpsElementFactory elementFactory = JpsElementFactory.getInstance();
- if (level.equals(PROJECT_LEVEL)) {
- return elementFactory.createProjectReference();
- }
- if (level.equals(APPLICATION_LEVEL)) {
- return elementFactory.createGlobalReference();
- }
- for (JpsModelSerializerExtension extension : JpsModelSerializerExtension.getExtensions()) {
- final JpsElementReference<? extends JpsCompositeElement> reference = extension.createLibraryTableReference(level);
- if (reference != null) {
- return reference;
- }
- }
- throw new UnsupportedOperationException();
- }
-
- private static String getLevelId(JpsElementReference<? extends JpsCompositeElement> reference) {
- JpsCompositeElement element = reference.resolve();
- if (element instanceof JpsProject) {
- return PROJECT_LEVEL;
- }
- else if (element instanceof JpsGlobal) {
- return APPLICATION_LEVEL;
- }
- for (JpsModelSerializerExtension extension : JpsModelSerializerExtension.getExtensions()) {
- String levelId = extension.getLibraryTableLevelId(reference);
- if (levelId != null) {
- return levelId;
- }
- }
- return null;
- }
}
import org.jetbrains.jps.model.java.JpsJavaModuleType;
import org.jetbrains.jps.model.library.JpsSdkType;
import org.jetbrains.jps.model.module.JpsModule;
-import org.jetbrains.jps.model.serialization.artifact.JpsArtifactLoader;
+import org.jetbrains.jps.model.serialization.artifact.JpsArtifactSerializer;
import org.jetbrains.jps.model.serialization.facet.JpsFacetLoader;
import java.io.File;
}
private void loadArtifacts(Element artifactManagerComponent) {
- JpsArtifactLoader.loadArtifacts(myProject, artifactManagerComponent);
+ JpsArtifactSerializer.loadArtifacts(myProject, artifactManagerComponent);
}
@Nullable
+++ /dev/null
-package org.jetbrains.jps.model.serialization.artifact;
-
-import com.intellij.openapi.util.JDOMUtil;
-import com.intellij.util.xmlb.XmlSerializer;
-import org.jdom.Element;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-import org.jetbrains.jps.model.JpsCompositeElement;
-import org.jetbrains.jps.model.JpsElementFactory;
-import org.jetbrains.jps.model.JpsElementReference;
-import org.jetbrains.jps.model.JpsProject;
-import org.jetbrains.jps.model.artifact.*;
-import org.jetbrains.jps.model.artifact.elements.JpsCompositePackagingElement;
-import org.jetbrains.jps.model.artifact.elements.JpsPackagingElement;
-import org.jetbrains.jps.model.artifact.elements.JpsPackagingElementFactory;
-import org.jetbrains.jps.model.serialization.JpsModelSerializerExtension;
-import org.jetbrains.jps.model.serialization.JpsModuleSerializer;
-
-import java.util.Arrays;
-import java.util.List;
-
-/**
- * @author nik
- */
-public class JpsArtifactLoader {
- private static List<? extends JpsPackagingElementLoader<?>> STANDARD_LOADERS = Arrays.asList();
-
- public static void loadArtifacts(@NotNull JpsProject project, @Nullable Element componentElement) {
- JpsArtifactService service = JpsArtifactService.getInstance();
- for (Element artifactElement : JDOMUtil.getChildren(componentElement, "artifact")) {
- ArtifactState state = XmlSerializer.deserialize(artifactElement, ArtifactState.class);
- if (state == null) continue;
- JpsArtifactType artifactType = getArtifactType(state.getArtifactType());
- JpsPackagingElement rootElement = loadPackagingElement(state.getRootElement());
- if (rootElement != null) {
- JpsArtifact artifact = service.addArtifact(project, state.getName(), (JpsCompositePackagingElement)rootElement, artifactType);
- artifact.setOutputPath(state.getOutputPath());
- }
- }
- }
-
- @Nullable
- private static JpsPackagingElement loadPackagingElement(Element element) {
- JpsPackagingElement packagingElement = createPackagingElement(element);
- if (packagingElement instanceof JpsCompositePackagingElement) {
- for (Element childElement : JDOMUtil.getChildren(element, "element")) {
- JpsPackagingElement child = loadPackagingElement(childElement);
- if (child != null) {
- ((JpsCompositePackagingElement)packagingElement).addChild(child);
- }
- }
- }
- return packagingElement;
- }
-
- @Nullable
- private static JpsPackagingElement createPackagingElement(Element element) {
- String typeId = element.getAttributeValue("id");
- JpsPackagingElementFactory factory = JpsPackagingElementFactory.getInstance();
- if (typeId.equals("root")) {
- return factory.createArtifactRoot();
- }
- if (typeId.equals("directory")) {
- return factory.createDirectory(element.getAttributeValue("name"));
- }
- if (typeId.equals("archive")) {
- return factory.createArchive(element.getAttributeValue("name"));
- }
- if (typeId.equals("file-copy")) {
- return factory.createFileCopy(element.getAttributeValue("path"),
- element.getAttributeValue("output-file-name"));
- }
- if (typeId.equals("dir-copy")) {
- return factory.createDirectoryCopy(element.getAttributeValue("path"));
- }
- if (typeId.equals("extracted-dir")) {
- return factory.createExtractedDirectory(element.getAttributeValue("path"),
- element.getAttributeValue("path-in-jar"));
- }
- if (typeId.equals("library")) {
- String level = element.getAttributeValue("level");
- String libraryName = element.getAttributeValue("name");
- String moduleName = element.getAttributeValue("module-name");
- JpsElementReference<? extends JpsCompositeElement> parentReference;
- if (moduleName != null) {
- parentReference = JpsElementFactory.getInstance().createModuleReference(moduleName);
- }
- else {
- parentReference = JpsModuleSerializer.createLibraryTableReference(level);
- }
- return factory.createLibraryElement(JpsElementFactory.getInstance().createLibraryReference(libraryName, parentReference));
- }
- if (typeId.equals("artifact")) {
- return factory.createArtifactOutput(JpsArtifactService.getInstance().createReference(element.getAttributeValue("artifact-name")));
- }
- JpsPackagingElementLoader<?> loader = findElementLoader(typeId);
- if (loader != null) {
- return loader.load(element);
- }
- return null;
- }
-
- private static JpsArtifactType getArtifactType(@Nullable String typeId) {
- if (typeId == null || "plain".equals(typeId)) {
- return DirectoryArtifactType.INSTANCE;
- }
- if (typeId.equals("jar")) {
- return JarArtifactType.INSTANCE;
- }
- for (JpsModelSerializerExtension extension : JpsModelSerializerExtension.getExtensions()) {
- JpsArtifactType type = extension.getArtifactType(typeId);
- if (type != null) {
- return type;
- }
- }
- return DirectoryArtifactType.INSTANCE;
- }
-
- @Nullable
- private static JpsPackagingElementLoader<?> findElementLoader(@NotNull String typeId) {
- for (JpsPackagingElementLoader<?> loader : STANDARD_LOADERS) {
- if (loader.getTypeId().equals(typeId)) {
- return loader;
- }
- }
- for (JpsModelSerializerExtension extension : JpsModelSerializerExtension.getExtensions()) {
- for (JpsPackagingElementLoader<?> loader : extension.getPackagingElementLoaders()) {
- if (loader.getTypeId().equals(typeId)) {
- return loader;
- }
- }
- }
- return null;
- }
-}
--- /dev/null
+package org.jetbrains.jps.model.serialization.artifact;
+
+import com.intellij.openapi.util.JDOMUtil;
+import com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters;
+import com.intellij.util.xmlb.XmlSerializer;
+import org.jdom.Element;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jps.model.JpsCompositeElement;
+import org.jetbrains.jps.model.JpsElementFactory;
+import org.jetbrains.jps.model.JpsElementReference;
+import org.jetbrains.jps.model.JpsProject;
+import org.jetbrains.jps.model.artifact.*;
+import org.jetbrains.jps.model.artifact.elements.*;
+import org.jetbrains.jps.model.library.JpsLibraryReference;
+import org.jetbrains.jps.model.module.JpsModuleReference;
+import org.jetbrains.jps.model.serialization.JpsLibraryTableSerializer;
+import org.jetbrains.jps.model.serialization.JpsModelSerializerExtension;
+
+/**
+ * @author nik
+ */
+public class JpsArtifactSerializer {
+ private static JpsPackagingElementSerializer<?>[] STANDARD_SERIALIZERS = {
+ new ArtifactRootElementSerializer(),
+ new DirectoryElementSerializer(),
+ new ArchiveElementSerializer(),
+ new FileCopyElementSerializer(),
+ new DirectoryCopyElementSerializer(),
+ new ExtractedDirectoryElementSerializer(),
+ new LibraryFilesElementSerializer(),
+ new ArtifactOutputElementSerializer()
+ };
+ private static final JpsArtifactTypeSerializer[] STANDARD_TYPE_SERIALIZERS = {
+ new JpsArtifactTypeSerializer("plain", DirectoryArtifactType.INSTANCE),
+ new JpsArtifactTypeSerializer("jar", JarArtifactType.INSTANCE)
+ };
+ private static final String ELEMENT_TAG = "element";
+ private static final String ID_ATTRIBUTE = "id";
+ private static final SkipDefaultValuesSerializationFilters SERIALIZATION_FILTERS = new SkipDefaultValuesSerializationFilters();
+
+
+ public static void loadArtifacts(@NotNull JpsProject project, @Nullable Element componentElement) {
+ JpsArtifactService service = JpsArtifactService.getInstance();
+ for (Element artifactElement : JDOMUtil.getChildren(componentElement, "artifact")) {
+ ArtifactState state = XmlSerializer.deserialize(artifactElement, ArtifactState.class);
+ if (state == null) continue;
+ JpsArtifactType artifactType = getTypeSerializer(state.getArtifactType()).getType();
+ JpsPackagingElement rootElement = loadPackagingElement(state.getRootElement());
+ if (rootElement != null) {
+ JpsArtifact artifact = service.addArtifact(project, state.getName(), (JpsCompositePackagingElement)rootElement, artifactType);
+ artifact.setBuildOnMake(state.isBuildOnMake());
+ artifact.setOutputPath(state.getOutputPath());
+ }
+ }
+ }
+
+ public static void saveArtifact(@NotNull JpsArtifact artifact, Element componentElement) {
+ ArtifactState state = new ArtifactState();
+ state.setName(artifact.getName());
+ state.setBuildOnMake(artifact.isBuildOnMake());
+ state.setOutputPath(artifact.getOutputPath());
+ state.setArtifactType(getTypeSerializer(artifact.getArtifactType()).getTypeId());
+ state.setRootElement(savePackagingElement(artifact.getRootElement()));
+ componentElement.addContent(XmlSerializer.serialize(state, SERIALIZATION_FILTERS));
+ }
+
+ private static <P extends JpsPackagingElement> Element savePackagingElement(P element) {
+ //noinspection unchecked
+ JpsPackagingElementSerializer<P> serializer = findElementSerializer((Class<P>)element.getClass());
+ Element tag = new Element(ELEMENT_TAG).setAttribute(ID_ATTRIBUTE, serializer.getTypeId());
+ serializer.save(element, tag);
+ if (element instanceof JpsCompositePackagingElement) {
+ for (JpsPackagingElement child : ((JpsCompositePackagingElement)element).getChildren()) {
+ tag.addContent(savePackagingElement(child));
+ }
+ }
+ return tag;
+ }
+
+ @Nullable
+ private static JpsPackagingElement loadPackagingElement(Element element) {
+ JpsPackagingElement packagingElement = createPackagingElement(element);
+ if (packagingElement instanceof JpsCompositePackagingElement) {
+ for (Element childElement : JDOMUtil.getChildren(element, ELEMENT_TAG)) {
+ JpsPackagingElement child = loadPackagingElement(childElement);
+ if (child != null) {
+ ((JpsCompositePackagingElement)packagingElement).addChild(child);
+ }
+ }
+ }
+ return packagingElement;
+ }
+
+ @Nullable
+ private static JpsPackagingElement createPackagingElement(Element element) {
+ String typeId = element.getAttributeValue(ID_ATTRIBUTE);
+ JpsPackagingElementSerializer<?> serializer = findElementSerializer(typeId);
+ if (serializer != null) {
+ return serializer.load(element);
+ }
+ return null;
+ }
+
+ @Nullable
+ private static JpsPackagingElementSerializer<?> findElementSerializer(@NotNull String typeId) {
+ for (JpsPackagingElementSerializer<?> serializer : STANDARD_SERIALIZERS) {
+ if (serializer.getTypeId().equals(typeId)) {
+ return serializer;
+ }
+ }
+ for (JpsModelSerializerExtension extension : JpsModelSerializerExtension.getExtensions()) {
+ for (JpsPackagingElementSerializer<?> serializer : extension.getPackagingElementSerializers()) {
+ if (serializer.getTypeId().equals(typeId)) {
+ return serializer;
+ }
+ }
+ }
+ return null;
+ }
+
+ @NotNull
+ private static <E extends JpsPackagingElement> JpsPackagingElementSerializer<E> findElementSerializer(@NotNull Class<E> elementClass) {
+ for (JpsPackagingElementSerializer<?> serializer : STANDARD_SERIALIZERS) {
+ if (serializer.getElementClass().isAssignableFrom(elementClass)) {
+ //noinspection unchecked
+ return (JpsPackagingElementSerializer<E>)serializer;
+ }
+ }
+ for (JpsModelSerializerExtension extension : JpsModelSerializerExtension.getExtensions()) {
+ for (JpsPackagingElementSerializer<?> serializer : extension.getPackagingElementSerializers()) {
+ if (serializer.getElementClass().isAssignableFrom(elementClass)) {
+ //noinspection unchecked
+ return (JpsPackagingElementSerializer<E>)serializer;
+ }
+ }
+ }
+ throw new IllegalArgumentException("Serializer not found for " + elementClass);
+ }
+
+ private static JpsArtifactTypeSerializer getTypeSerializer(String typeId) {
+ for (JpsArtifactTypeSerializer serializer : STANDARD_TYPE_SERIALIZERS) {
+ if (serializer.getTypeId().equals(typeId)) {
+ return serializer;
+ }
+ }
+ for (JpsModelSerializerExtension extension : JpsModelSerializerExtension.getExtensions()) {
+ for (JpsArtifactTypeSerializer serializer : extension.getArtifactTypeSerializers()) {
+ if (serializer.getTypeId().equals(typeId)) {
+ return serializer;
+ }
+ }
+ }
+ return null;
+ }
+
+ private static JpsArtifactTypeSerializer getTypeSerializer(JpsArtifactType type) {
+ for (JpsArtifactTypeSerializer serializer : STANDARD_TYPE_SERIALIZERS) {
+ if (serializer.getType().equals(type)) {
+ return serializer;
+ }
+ }
+ for (JpsModelSerializerExtension extension : JpsModelSerializerExtension.getExtensions()) {
+ for (JpsArtifactTypeSerializer serializer : extension.getArtifactTypeSerializers()) {
+ if (serializer.getType().equals(type)) {
+ return serializer;
+ }
+ }
+ }
+ return null;
+ }
+
+ private static class ArtifactRootElementSerializer extends JpsPackagingElementSerializer<JpsArtifactRootElement> {
+ public ArtifactRootElementSerializer() {
+ super("root", JpsArtifactRootElement.class);
+ }
+
+ @Override
+ public JpsArtifactRootElement load(Element element) {
+ return JpsPackagingElementFactory.getInstance().createArtifactRoot();
+ }
+
+ @Override
+ public void save(JpsArtifactRootElement element, Element tag) {
+ }
+ }
+
+ private static class DirectoryElementSerializer extends JpsPackagingElementSerializer<JpsDirectoryPackagingElement> {
+ public DirectoryElementSerializer() {
+ super("directory", JpsDirectoryPackagingElement.class);
+ }
+
+ @Override
+ public JpsDirectoryPackagingElement load(Element element) {
+ return JpsPackagingElementFactory.getInstance().createDirectory(element.getAttributeValue("name"));
+ }
+
+ @Override
+ public void save(JpsDirectoryPackagingElement element, Element tag) {
+ tag.setAttribute("name", element.getDirectoryName());
+ }
+ }
+
+ private static class ArchiveElementSerializer extends JpsPackagingElementSerializer<JpsArchivePackagingElement> {
+ public ArchiveElementSerializer() {
+ super("archive", JpsArchivePackagingElement.class);
+ }
+
+ @Override
+ public JpsArchivePackagingElement load(Element element) {
+ return JpsPackagingElementFactory.getInstance().createArchive(element.getAttributeValue("name"));
+ }
+
+ @Override
+ public void save(JpsArchivePackagingElement element, Element tag) {
+ tag.setAttribute("name", element.getArchiveName());
+ }
+ }
+
+ private static class FileCopyElementSerializer extends JpsPackagingElementSerializer<JpsFileCopyPackagingElement> {
+ public FileCopyElementSerializer() {
+ super("file-copy", JpsFileCopyPackagingElement.class);
+ }
+
+ @Override
+ public JpsFileCopyPackagingElement load(Element element) {
+ return JpsPackagingElementFactory.getInstance().createFileCopy(element.getAttributeValue("path"),
+ element.getAttributeValue("output-file-name"));
+ }
+
+ @Override
+ public void save(JpsFileCopyPackagingElement element, Element tag) {
+ tag.setAttribute("path", element.getFilePath());
+ String outputFileName = element.getRenamedOutputFileName();
+ if (outputFileName != null) {
+ tag.setAttribute("output-path-name", outputFileName);
+ }
+ }
+ }
+
+ private static class DirectoryCopyElementSerializer extends JpsPackagingElementSerializer<JpsDirectoryCopyPackagingElement> {
+ public DirectoryCopyElementSerializer() {
+ super("dir-copy", JpsDirectoryCopyPackagingElement.class);
+ }
+
+ @Override
+ public JpsDirectoryCopyPackagingElement load(Element element) {
+ return JpsPackagingElementFactory.getInstance().createDirectoryCopy(element.getAttributeValue("path"));
+ }
+
+ @Override
+ public void save(JpsDirectoryCopyPackagingElement element, Element tag) {
+ tag.setAttribute("path", element.getDirectoryPath());
+ }
+ }
+
+ private static class ExtractedDirectoryElementSerializer
+ extends JpsPackagingElementSerializer<JpsExtractedDirectoryPackagingElement> {
+ public ExtractedDirectoryElementSerializer() {
+ super("extracted-dir", JpsExtractedDirectoryPackagingElement.class);
+ }
+
+ @Override
+ public JpsExtractedDirectoryPackagingElement load(Element element) {
+ return JpsPackagingElementFactory.getInstance().createExtractedDirectory(element.getAttributeValue("path"),
+ element.getAttributeValue("path-in-jar"));
+ }
+
+ @Override
+ public void save(JpsExtractedDirectoryPackagingElement element, Element tag) {
+ tag.setAttribute("path", element.getFilePath());
+ tag.setAttribute("path-in-jar", element.getPathInJar());
+ }
+ }
+
+ private static class LibraryFilesElementSerializer extends JpsPackagingElementSerializer<JpsLibraryFilesPackagingElement> {
+ public LibraryFilesElementSerializer() {
+ super("library", JpsLibraryFilesPackagingElement.class);
+ }
+
+ @Override
+ public JpsLibraryFilesPackagingElement load(Element element) {
+ String level = element.getAttributeValue("level");
+ String libraryName = element.getAttributeValue("name");
+ String moduleName = element.getAttributeValue("module-name");
+ JpsElementReference<? extends JpsCompositeElement> parentReference;
+ if (moduleName != null) {
+ parentReference = JpsElementFactory.getInstance().createModuleReference(moduleName);
+ }
+ else {
+ parentReference = JpsLibraryTableSerializer.createLibraryTableReference(level);
+ }
+ return JpsPackagingElementFactory.getInstance()
+ .createLibraryElement(JpsElementFactory.getInstance().createLibraryReference(libraryName, parentReference));
+ }
+
+ @Override
+ public void save(JpsLibraryFilesPackagingElement element, Element tag) {
+ JpsLibraryReference reference = element.getLibraryReference();
+ JpsElementReference<? extends JpsCompositeElement> parentReference = reference.getParentReference();
+ tag.setAttribute("level", JpsLibraryTableSerializer.getLevelId(parentReference));
+ tag.setAttribute("name", reference.getLibraryName());
+ if (parentReference instanceof JpsModuleReference) {
+ tag.setAttribute("module-name", ((JpsModuleReference)parentReference).getModuleName());
+ }
+ }
+ }
+
+ private static class ArtifactOutputElementSerializer extends JpsPackagingElementSerializer<JpsArtifactOutputPackagingElement> {
+ public ArtifactOutputElementSerializer() {
+ super("artifact", JpsArtifactOutputPackagingElement.class);
+ }
+
+ @Override
+ public JpsArtifactOutputPackagingElement load(Element element) {
+ return JpsPackagingElementFactory.getInstance()
+ .createArtifactOutput(JpsArtifactService.getInstance().createReference(element.getAttributeValue("artifact-name")));
+ }
+
+ @Override
+ public void save(JpsArtifactOutputPackagingElement element, Element tag) {
+ tag.setAttribute("artifact-name", element.getArtifactReference().getArtifactName());
+ }
+ }
+}
--- /dev/null
+package org.jetbrains.jps.model.serialization.artifact;
+
+import org.jetbrains.jps.model.artifact.JpsArtifactType;
+
+/**
+ * @author nik
+ */
+public class JpsArtifactTypeSerializer {
+ private final String myTypeId;
+ private final JpsArtifactType myType;
+
+ public JpsArtifactTypeSerializer(String typeId, JpsArtifactType type) {
+ myTypeId = typeId;
+ myType = type;
+ }
+
+ public String getTypeId() {
+ return myTypeId;
+ }
+
+ public JpsArtifactType getType() {
+ return myType;
+ }
+}
+++ /dev/null
-package org.jetbrains.jps.model.serialization.artifact;
-
-import org.jdom.Element;
-import org.jetbrains.jps.model.artifact.elements.JpsPackagingElement;
-
-/**
- * @author nik
- */
-public abstract class JpsPackagingElementLoader<E extends JpsPackagingElement> {
- private final String myTypeId;
-
- protected JpsPackagingElementLoader(String typeId) {
- myTypeId = typeId;
- }
-
- public String getTypeId() {
- return myTypeId;
- }
-
- public abstract E load(Element element);
-}
--- /dev/null
+package org.jetbrains.jps.model.serialization.artifact;
+
+import org.jdom.Element;
+import org.jetbrains.jps.model.artifact.elements.JpsPackagingElement;
+
+/**
+ * @author nik
+ */
+public abstract class JpsPackagingElementSerializer<E extends JpsPackagingElement> {
+ private final String myTypeId;
+ private final Class<? extends E> myElementClass;
+
+ protected JpsPackagingElementSerializer(String typeId, Class<? extends E> elementClass) {
+ myTypeId = typeId;
+ myElementClass = elementClass;
+ }
+
+ public String getTypeId() {
+ return myTypeId;
+ }
+
+ public Class<? extends E> getElementClass() {
+ return myElementClass;
+ }
+
+ public abstract E load(Element element);
+
+ public abstract void save(E element, Element tag);
+}
import org.jetbrains.jps.model.module.JpsModuleReference;
import org.jetbrains.jps.model.serialization.JpsLibraryRootTypeSerializer;
import org.jetbrains.jps.model.serialization.JpsModelSerializerExtension;
-import org.jetbrains.jps.model.serialization.artifact.JpsPackagingElementLoader;
+import org.jetbrains.jps.model.serialization.artifact.JpsPackagingElementSerializer;
import java.util.Arrays;
import java.util.List;
}
@Override
- public List<? extends JpsPackagingElementLoader<?>> getPackagingElementLoaders() {
- return Arrays.asList(new JpsModuleOutputPackagingElementLoader(), new JpsTestModuleOutputPackagingElementLoader());
+ public List<? extends JpsPackagingElementSerializer<?>> getPackagingElementSerializers() {
+ return Arrays.asList(new JpsModuleOutputPackagingElementSerializer(), new JpsTestModuleOutputPackagingElementSerializer());
}
private static void loadExplodedDirectoryExtension(JpsModule module, Element rootModelComponent) {
return JpsJavaExtensionService.getInstance();
}
- private static class JpsModuleOutputPackagingElementLoader extends JpsPackagingElementLoader<JpsProductionModuleOutputPackagingElement> {
- private JpsModuleOutputPackagingElementLoader() {
- super("module-output");
+ private static class JpsModuleOutputPackagingElementSerializer
+ extends JpsPackagingElementSerializer<JpsProductionModuleOutputPackagingElement> {
+ private JpsModuleOutputPackagingElementSerializer() {
+ super("module-output", JpsProductionModuleOutputPackagingElement.class);
}
@Override
JpsModuleReference reference = JpsElementFactory.getInstance().createModuleReference(element.getAttributeValue("name"));
return getService().createProductionModuleOutput(reference);
}
+
+ @Override
+ public void save(JpsProductionModuleOutputPackagingElement element, Element tag) {
+ tag.setAttribute("name", element.getModuleReference().getModuleName());
+ }
}
- private static class JpsTestModuleOutputPackagingElementLoader extends JpsPackagingElementLoader<JpsTestModuleOutputPackagingElement> {
- private JpsTestModuleOutputPackagingElementLoader() {
- super("module-test-output");
+ private static class JpsTestModuleOutputPackagingElementSerializer extends JpsPackagingElementSerializer<JpsTestModuleOutputPackagingElement> {
+ private JpsTestModuleOutputPackagingElementSerializer() {
+ super("module-test-output", JpsTestModuleOutputPackagingElement.class);
}
@Override
JpsModuleReference reference = JpsElementFactory.getInstance().createModuleReference(element.getAttributeValue("name"));
return getService().createTestModuleOutput(reference);
}
+
+ @Override
+ public void save(JpsTestModuleOutputPackagingElement element, Element tag) {
+ tag.setAttribute("name", element.getModuleReference().getModuleName());
+ }
}
}
--- /dev/null
+<component name="ArtifactManager">
+ <artifact build-on-make="true" name="dir">
+ <output-path>$PROJECT_DIR$/out/artifacts/dir</output-path>
+ <root id="root">
+ <element id="archive" name="x.jar">
+ <element id="module-output" name="util" />
+ </element>
+ <element id="directory" name="lib">
+ <element id="library" level="module" name="log4j" module-name="util" />
+ <element id="library" level="project" name="junit" />
+ <element id="file-copy" path="$PROJECT_DIR$/main.iml" />
+ <element id="dir-copy" path="$PROJECT_DIR$/lib/junit-anno" />
+ <element id="extracted-dir" path="$PROJECT_DIR$/lib/junit.jar" path-in-jar="/junit/" />
+ </element>
+ </root>
+ </artifact>
+</component>
\ No newline at end of file
--- /dev/null
+<component name="ArtifactManager">
+ <artifact type="jar" name="jar">
+ <output-path>$PROJECT_DIR$/out/artifacts/jar</output-path>
+ <root id="archive" name="jar.jar">
+ <element id="module-output" name="main" />
+ <element id="module-output" name="util" />
+ <element id="artifact" artifact-name="dir" />
+ </root>
+ </artifact>
+</component>
\ No newline at end of file
--- /dev/null
+package org.jetbrains.jps.model.serialization;
+
+import com.intellij.openapi.util.io.FileUtil;
+import com.intellij.testFramework.PlatformTestUtil;
+import org.jdom.Element;
+import org.jetbrains.jps.model.artifact.JpsArtifact;
+import org.jetbrains.jps.model.artifact.JpsArtifactService;
+import org.jetbrains.jps.model.serialization.artifact.JpsArtifactSerializer;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+
+import static org.jetbrains.jps.model.serialization.JpsProjectSerializationTest.SAMPLE_PROJECT_PATH;
+import static org.jetbrains.jps.model.serialization.JpsProjectSerializationTest.getFileInSampleProject;
+
+/**
+ * @author nik
+ */
+public class JpsArtifactSerializationTest extends JpsSerializationTestCase {
+ public void testLoadProject() {
+ loadProject(SAMPLE_PROJECT_PATH);
+ List<JpsArtifact> artifacts = getService().getArtifacts(myModel.getProject());
+ assertEquals(2, artifacts.size());
+ assertEquals("dir", artifacts.get(0).getName());
+ assertEquals("jar", artifacts.get(1).getName());
+ }
+
+ public void testSaveProject() {
+ loadProject(SAMPLE_PROJECT_PATH);
+ File[] artifactFiles = getFileInSampleProject(".idea/artifacts").listFiles();
+ assertNotNull(artifactFiles);
+ for (File file : artifactFiles) {
+ JpsArtifact artifact = getService().createReference(FileUtil.getNameWithoutExtension(file)).asExternal(myModel).resolve();
+ assertNotNull(artifact);
+ doTestSaveArtifact(artifact, file);
+ }
+ }
+
+ private static void doTestSaveArtifact(JpsArtifact artifact, File expectedFile) {
+ try {
+ Element actual = new Element("component").setAttribute("name", "ArtifactManager");
+ JpsArtifactSerializer.saveArtifact(artifact, actual);
+ JpsMacroExpander
+ expander = JpsProjectLoader.createProjectMacroExpander(Collections.<String, String>emptyMap(), getFileInSampleProject(""));
+ Element expected = JpsLoaderBase.loadRootElement(expectedFile, expander);
+ PlatformTestUtil.assertElementsEqual(expected, actual);
+ }
+ catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private static JpsArtifactService getService() {
+ return JpsArtifactService.getInstance();
+ }
+}
* @author nik
*/
public class JpsProjectSerializationTest extends JpsSerializationTestCase {
- private static final String SAMPLE_PROJECT_PATH = "/jps/model-serialization/testData/sampleProject";
+ public static final String SAMPLE_PROJECT_PATH = "/jps/model-serialization/testData/sampleProject";
public void testLoadProject() {
loadProject(SAMPLE_PROJECT_PATH);
}
}
- private static File getFileInSampleProject(String moduleFilePath) {
- return new File(getTestDataFileAbsolutePath(SAMPLE_PROJECT_PATH + "/" + moduleFilePath));
+ public static File getFileInSampleProject(String relativePath) {
+ return new File(getTestDataFileAbsolutePath(SAMPLE_PROJECT_PATH + "/" + relativePath));
}
public void _testLoadIdeaProject() {