Java Code Examples for com.intellij.util.xmlb.XmlSerializer#serialize()

The following examples show how to use com.intellij.util.xmlb.XmlSerializer#serialize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PersistentRootConfigComponent.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Element getState() {
    Element ret = new Element("all-root-configs");
    for (Map.Entry<VirtualFile, List<ConfigPart>> entry : rootPartMap.entrySet()) {
        if (!isValidRoot(entry.getKey())) {
            LOG.info("Skipped persisting VCS root " + entry.getKey() +
                    " because it does not appear to be a valid Perforce root");
            continue;
        }
        VcsRootCacheStore store = new VcsRootCacheStore(entry.getKey());
        store.setConfigParts(entry.getValue());
        Element rootElement = XmlSerializer.serialize(store.getState());
        Element configElement = new Element("root");
        configElement.addContent(rootElement);
        ret.addContent(configElement);
    }
    return ret;
}
 
Example 2
Source File: BackgroundTaskByVfsChangeManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Element getState() {
  Element element = new Element("state");
  for (BackgroundTaskByVfsChangeTaskImpl task : myTasks) {
    Element taskElement = new Element("task");
    element.addContent(taskElement);

    taskElement.setAttribute("url", task.getVirtualFilePointer().getUrl());
    taskElement.setAttribute("provider-name", task.getProviderName());
    taskElement.setAttribute("name", task.getName());
    taskElement.setAttribute("enabled", String.valueOf(task.isEnabled()));

    Element serialize = XmlSerializer.serialize(task.getParameters());

    taskElement.addContent(serialize);

    ExpandMacroToPathMap expandMacroToPathMap = task.createExpandMacroToPathMap();

    expandMacroToPathMap.substitute(serialize, false, true);
  }
  return element;
}
 
Example 3
Source File: XDebuggerSettingsTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testSerialize() throws Exception {
  XDebuggerSettingManagerImpl settingsManager = XDebuggerSettingManagerImpl.getInstanceImpl();

  MyDebuggerSettings settings = MyDebuggerSettings.getInstance();
  assertNotNull(settings);
  settings.myOption = "239";

  Element element = XmlSerializer.serialize(settingsManager.getState());
  //System.out.println(JDOMUtil.writeElement(element, SystemProperties.getLineSeparator()));

  settings.myOption = "42";
  assertSame(settings, MyDebuggerSettings.getInstance());

  settingsManager.loadState(XmlSerializer.deserialize(element, XDebuggerSettingManagerImpl.SettingsState.class));
  assertSame(settings, MyDebuggerSettings.getInstance());
  assertEquals("239", settings.myOption);
}
 
Example 4
Source File: PersistableCodeStyleSchemes.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Element getState() {
  CodeStyleScheme currentScheme = getCurrentScheme();
  CURRENT_SCHEME_NAME = currentScheme == null ? null : currentScheme.getName();
  return XmlSerializer.serialize(this, new SerializationFilter() {
    @Override
    public boolean accepts(@Nonnull Accessor accessor, @Nonnull Object bean) {
      if ("CURRENT_SCHEME_NAME".equals(accessor.getName())) {
        return !CodeStyleScheme.DEFAULT_SCHEME_NAME.equals(accessor.read(bean));
      }
      else {
        return accessor.getValueClass().equals(String.class);
      }
    }
  });
}
 
Example 5
Source File: VcsRootCacheStoreTest.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
@Test
void serializeRestore_none() {
    MockVirtualFile localDir = MockVirtualFileSystem.createTree("/my/local/file", "data").get("/my/local/file");
    when(idea.getMockLocalFilesystem().findFileByPath("/my/local/file")).thenReturn(localDir);

    VcsRootCacheStore store = new VcsRootCacheStore(localDir);

    VcsRootCacheStore.State originalState = store.getState();
    Element serialized = XmlSerializer.serialize(originalState);
    assertNotNull(serialized);

    VcsRootCacheStore.State restored = XmlSerializer.deserialize(serialized, VcsRootCacheStore.State.class);
    assertNotNull(restored);
    assertEquals("/my/local/file", restored.rootDirectory);
    assertSize(0, restored.configParts);

    VcsRootCacheStore loaded = new VcsRootCacheStore(restored, getClass().getClassLoader());
    assertEquals(localDir, loaded.getRootDirectory());
    assertSize(0, loaded.getConfigParts());
}
 
Example 6
Source File: ProjectCacheStoreTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@ExtendWith(TemporaryFolderExtension.class)
@Test
void getSetState(TemporaryFolder tmpDir)
        throws PrimitiveMap.UnmarshalException {
    ProjectCacheStore.State state = new ProjectCacheStore.State();
    ClientServerRef ref = new ClientServerRef(
            P4ServerName.forPortNotNull("test:1234"),
            "client1"
    );
    state.clientState = Collections.singletonList(new ClientQueryCacheStore(ref).getState());
    state.serverState = Collections.singletonList(new ServerQueryCacheStore(ref.getServerName()).getState());
    MockFilePath fp = new MockFilePath(tmpDir.newFile("test-file.txt"));
    ActionStore.State actionState = ActionStore.getState(
            ActionStore.getSourceId(ref),
            new MoveFilesToChangelistAction(new P4ChangelistIdImpl(1, ref),
                    Collections.singletonList(fp)));
    state.pendingActions = Collections.singletonList(actionState);

    Element serialized = XmlSerializer.serialize(state);

    ProjectCacheStore.State unmarshalled = XmlSerializer.deserialize(serialized, ProjectCacheStore.State.class);
    assertNotNull(unmarshalled);
    assertSize(1, unmarshalled.clientState);
    assertSize(1, unmarshalled.serverState);
    assertSize(1, unmarshalled.pendingActions);

    assertNotNull(unmarshalled.pendingActions.get(0));
    ActionStore.PendingAction moveAction = ActionStore.read(unmarshalled.pendingActions.get(0));
    assertThat(moveAction.clientAction, instanceOf(MoveFilesToChangelistAction.class));
}
 
Example 7
Source File: VariablesAccessor.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
public void writeVariables(XQueryRunConfiguration configuration, Element element) {
    XQueryRunVariables variables = configuration.getVariables();
    if (variables != null) {
        Element variablesElement = XmlSerializer.serialize(variables.getState());
        element.addContent(variablesElement);
    }
}
 
Example 8
Source File: DataSourceAccessor.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
public void writeDataSourceConfiguration(XQueryRunConfiguration configuration, Element xmlRootElement) {
    String dataSourceName = configuration.getDataSourceName();
    if (dataSourceName != null) {
        XQueryDataSourceConfiguration dataSourceConfiguration = getDataSourcesSettings()
                .getDataSourceConfigurationForName(dataSourceName);
        if (dataSourceConfiguration != null) {
            Element dataSourceElement = XmlSerializer.serialize(dataSourceConfiguration);
            xmlRootElement.addContent(dataSourceElement);
        }
    }
}
 
Example 9
Source File: LibraryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void writeExternal(Element rootElement) {
  checkDisposed();

  Element element = new Element(ELEMENT);
  if (myName != null) {
    element.setAttribute(LIBRARY_NAME_ATTR, myName);
  }
  if (myKind != null) {
    element.setAttribute(LIBRARY_TYPE_ATTR, myKind.getKindId());
    LOG.assertTrue(myProperties != null, "Properties is 'null' in library with kind " + myKind);
    final Object state = myProperties.getState();
    if (state != null) {
      final Element propertiesElement = XmlSerializer.serialize(state);
      if (propertiesElement != null) {
        element.addContent(propertiesElement.setName(PROPERTIES_ELEMENT));
      }
    }
  }
  for (OrderRootType rootType : OrderRootType.getSortedRootTypes()) {
    VirtualFilePointerContainer roots = myRoots.get(rootType);
    if (roots == null || roots.isEmpty()) {
      continue;
    }

    final Element rootTypeElement = new Element(rootType.name());
    if (roots != null) {
      roots.writeExternal(rootTypeElement, ROOT_PATH_ELEMENT, false);
    }
    element.addContent(rootTypeElement);
  }
  if (myExcludedRoots != null && !myExcludedRoots.isEmpty()) {
    Element excluded = new Element(EXCLUDED_ROOTS_TAG);
    myExcludedRoots.writeExternal(excluded, ROOT_PATH_ELEMENT, false);
    element.addContent(excluded);
  }
  writeJarDirectories(element);
  rootElement.addContent(element);
}
 
Example 10
Source File: RemoteServersManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
@Override
public RemoteServersManagerState getState() {
  RemoteServersManagerState state = new RemoteServersManagerState();
  for (RemoteServer<?> server : myServers) {
    RemoteServerState serverState = new RemoteServerState();
    serverState.myName = server.getName();
    serverState.myTypeId = server.getType().getId();
    serverState.myConfiguration = XmlSerializer.serialize(server.getConfiguration().getSerializer().getState(), SERIALIZATION_FILTERS);
    state.myServers.add(serverState);
  }
  state.myServers.addAll(myUnknownServers);
  return state;
}
 
Example 11
Source File: MasterDetailsStateService.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void setComponentState(@Nonnull @NonNls String key, @Nonnull MasterDetailsState state) {
  final Element element = XmlSerializer.serialize(state, mySerializationFilter);
  if (element == null) {
    myStates.remove(key);
  }
  else {
    final ComponentState componentState = new ComponentState();
    componentState.myKey = key;
    componentState.mySettings = element;
    myStates.put(key, componentState);
  }
}
 
Example 12
Source File: VcsRootCacheStoreTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Test
void serializeRestore_two() {
    MockVirtualFile localDir = MockVirtualFileSystem.createTree("/my/local/file", "data").get("/my/local/file");
    when(idea.getMockLocalFilesystem().findFileByPath("/my/local/file")).thenReturn(localDir);

    VcsRootCacheStore store = new VcsRootCacheStore(localDir);
    ClientNameConfigPart part1 = new ClientNameConfigPart("source");
    part1.setClientname("client-name-1");
    ServerFingerprintDataPart part2 = new ServerFingerprintDataPart("source1");
    part2.setServerFingerprint("fingerprint-1");
    store.setConfigParts(Arrays.asList(part1, part2));

    VcsRootCacheStore.State originalState = store.getState();
    Element serialized = XmlSerializer.serialize(originalState);
    assertNotNull(serialized);

    VcsRootCacheStore.State restored = XmlSerializer.deserialize(serialized, VcsRootCacheStore.State.class);
    assertNotNull(restored);
    assertEquals("/my/local/file", restored.rootDirectory);
    assertSize(2, restored.configParts);
    assertNotNull(restored.configParts.get(0));
    assertEquals(restored.configParts.get(0).className, ClientNameConfigPart.class.getName());
    assertNull(restored.configParts.get(0).children);
    assertNotNull(restored.configParts.get(1));
    assertEquals(restored.configParts.get(1).className, ServerFingerprintDataPart.class.getName());
    assertNull(restored.configParts.get(1).children);

    VcsRootCacheStore loaded = new VcsRootCacheStore(restored, getClass().getClassLoader());
    assertEquals(localDir, loaded.getRootDirectory());
    assertSize(2, loaded.getConfigParts());

    assertThat(loaded.getConfigParts().get(0), instanceOf(ClientNameConfigPart.class));
    ClientNameConfigPart part1Restored = (ClientNameConfigPart) loaded.getConfigParts().get(0);
    assertEquals("client-name-1", part1Restored.getClientname());

    assertThat(loaded.getConfigParts().get(1), instanceOf(ServerFingerprintDataPart.class));
    ServerFingerprintDataPart part2Restored = (ServerFingerprintDataPart) loaded.getConfigParts().get(1);
    assertEquals("fingerprint-1", part2Restored.getServerFingerprint());
}
 
Example 13
Source File: TableModelEditor.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static <T> T cloneUsingXmlSerialization(@Nonnull T oldItem, @Nonnull T newItem) {
  Element serialized = XmlSerializer.serialize(oldItem, new SkipDefaultValuesSerializationFilters());
  if (!JDOMUtil.isEmpty(serialized)) {
    XmlSerializer.deserializeInto(newItem, serialized);
  }
  return newItem;
}
 
Example 14
Source File: XBreakpointBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
public S getState() {
  Element propertiesElement = myProperties != null ? XmlSerializer.serialize(myProperties.getState(), SERIALIZATION_FILTERS) : null;
  myState.setCondition(BreakpointState.Condition.create(!myConditionEnabled, myCondition));
  myState.setLogExpression(BreakpointState.LogExpression.create(!myLogExpressionEnabled, myLogExpression));
  myState.setPropertiesElement(propertiesElement);
  return myState;
}
 
Example 15
Source File: DarkModeSyncThemesTest.java    From dark-mode-sync-plugin with MIT License 5 votes vote down vote up
/**
 * Verifies that the {@link DarkModeSyncThemes.State} class may be serialized and de-serialized
 * using the {@link XmlSerializer}.
 */
@Test
void state_serialization_test() {
  final State expected = new State();
  final Element element = XmlSerializer.serialize(expected);
  final State actual = XmlSerializer.deserialize(element, State.class);

  assertEquals(expected, actual);
}
 
Example 16
Source File: VcsRootCacheStoreTest.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
@Test
void serializeRestore_complex() {
    Map<String, MockVirtualFile> tree = MockVirtualFileSystem.createTree(
            "/my/root/file", "data",
            "/my/root/config", "config data");
    VirtualFile root = tree.get("/my/root/file").getParent();
    MockVirtualFile configFile = tree.get("/my/root/config");
    when(idea.getMockLocalFilesystem().findFileByPath("/my/root")).thenReturn(root);
    when(idea.getMockLocalFilesystem().findFileByPath("/my/root/config")).thenReturn(configFile);

    ClientNameConfigPart part1 = new ClientNameConfigPart("s1");
    part1.setClientname("c1");
    EnvCompositePart part2 = new EnvCompositePart(root);
    FileConfigPart part3 = new FileConfigPart(root, configFile);
    RequirePasswordDataPart part4 = new RequirePasswordDataPart();
    ServerFingerprintDataPart part5 = new ServerFingerprintDataPart("s5");
    part5.setServerFingerprint("f-print");
    Map<String, String> part6Props = new HashMap<>();
    part6Props.put("port", "my-port:1234");
    SimpleDataPart part6 = new SimpleDataPart(root, "s6", part6Props);
    MultipleConfigPart part7 = new MultipleConfigPart("S7", Arrays.asList(part5, part6));
    VcsRootCacheStore original = new VcsRootCacheStore(root);
    original.setConfigParts(Arrays.asList(part1, part2, part3, part4, part7));

    VcsRootCacheStore.State originalState = original.getState();
    Element serialized = XmlSerializer.serialize(originalState);
    assertNotNull(serialized);

    VcsRootCacheStore.State restored = XmlSerializer.deserialize(serialized, VcsRootCacheStore.State.class);
    assertNotNull(restored);
    assertEquals("/my/root", restored.rootDirectory);
    assertSize(5, restored.configParts);


    VcsRootCacheStore loaded = new VcsRootCacheStore(restored, getClass().getClassLoader());
    assertEquals(root, loaded.getRootDirectory());
    assertSize(5, loaded.getConfigParts());

    assertThat(loaded.getConfigParts().get(0), instanceOf(ClientNameConfigPart.class));
    assertEquals("c1", loaded.getConfigParts().get(0).getClientname());

    assertThat(loaded.getConfigParts().get(1), instanceOf(EnvCompositePart.class));
    // Nothing to check in the env.

    assertThat(loaded.getConfigParts().get(2), instanceOf(FileConfigPart.class));
    FileConfigPart part3Restored = (FileConfigPart) loaded.getConfigParts().get(2);
    // Need to construct the file object so that Windows paths are created in the same way.
    assertEquals(configFile, part3Restored.getConfigFile());

    assertThat(loaded.getConfigParts().get(3), instanceOf(RequirePasswordDataPart.class));
    // Nothing to check in require password

    assertThat(loaded.getConfigParts().get(4), instanceOf(MultipleConfigPart.class));
    MultipleConfigPart part7Restored = (MultipleConfigPart) loaded.getConfigParts().get(4);
    assertSize(2, part7Restored.getChildren());

    assertThat(part7Restored.getChildren().get(0), instanceOf(ServerFingerprintDataPart.class));
    assertEquals("f-print", part7Restored.getChildren().get(0).getServerFingerprint());

    assertThat(part7Restored.getChildren().get(1), instanceOf(SimpleDataPart.class));
    SimpleDataPart part6Restored = (SimpleDataPart) part7Restored.getChildren().get(1);
    assertEquals("my-port:1234", part6Restored.getRawServerName());
}
 
Example 17
Source File: DateTimeFormatManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public Element getState() {
  return XmlSerializer.serialize(this);
}
 
Example 18
Source File: XBreakpointManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean statesAreDifferent(BreakpointState state1, BreakpointState state2) {
  Element elem1 = XmlSerializer.serialize(state1, SERIALIZATION_FILTER);
  Element elem2 = XmlSerializer.serialize(state2, SERIALIZATION_FILTER);
  return !JDOMUtil.areElementsEqual(elem1, elem2);
}
 
Example 19
Source File: XBreakpointsTestCase.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected Element save() {
  return XmlSerializer.serialize(myBreakpointManager.getState(), new SkipDefaultValuesSerializationFilters());
}
 
Example 20
Source File: PostfixTemplatesSettings.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public Element getState() {
  return XmlSerializer.serialize(this, new SkipDefaultValuesSerializationFilters());
}