Java 类com.intellij.psi.XmlElementFactory 实例源码

项目:mule-intellij-plugins    文件:MuleSchemaUtils.java   
public static void insertSchemaLocationLookup(XmlFile xmlFile, String namespace, String locationLookup) {
    final XmlTag rootTag = xmlFile.getRootTag();
    if (rootTag == null)
        return;
    final XmlAttribute attribute = rootTag.getAttribute("xsi:schemaLocation", HTTP_WWW_W3_ORG_2001_XMLSCHEMA);
    if (attribute != null) {
        final String value = attribute.getValue();
        attribute.setValue(value + "\n\t\t\t" + namespace + " " + locationLookup);
    } else {
        final XmlElementFactory elementFactory = XmlElementFactory.getInstance(xmlFile.getProject());
        final XmlAttribute schemaLocation = elementFactory.createXmlAttribute("xsi:schemaLocation", XmlUtil.XML_SCHEMA_INSTANCE_URI);
        schemaLocation.setValue(namespace + " " + locationLookup);
        rootTag.add(schemaLocation);
    }

}
项目:intellij-ce-playground    文件:DomRootInvocationHandler.java   
@Override
protected XmlTag setEmptyXmlTag() {
  final XmlTag[] result = new XmlTag[]{null};
  getManager().runChange(new Runnable() {
    @Override
    public void run() {
      try {
        final String namespace = getXmlElementNamespace();
        @NonNls final String nsDecl = StringUtil.isEmpty(namespace) ? "" : " xmlns=\"" + namespace + "\"";
        final XmlFile xmlFile = getFile();
        final XmlTag tag = XmlElementFactory.getInstance(xmlFile.getProject()).createTagFromText("<" + getXmlElementName() + nsDecl + "/>");
        result[0] = ((XmlDocument)xmlFile.getDocument().replace(((XmlFile)tag.getContainingFile()).getDocument())).getRootTag();
      }
      catch (IncorrectOperationException e) {
        LOG.error(e);
      }
    }
  });
  return result[0];
}
项目:intellij-ce-playground    文件:XmlTextTest.java   
public void testInsertAtOffset() throws Exception {
  new WriteCommandAction(getProject()) {

    @Override
    protected void run(@NotNull final Result result) throws Throwable {
      String xml = "<root>0123456789</root>";
      XmlFile file = (XmlFile)PsiFileFactory.getInstance(getProject())
        .createFileFromText("foo.xml", StdFileTypes.XML, xml, (long)1, true, false);
      //System.out.println(DebugUtil.psiToString(file, false));
      XmlTag root = file.getDocument().getRootTag();
      final XmlText text1 = root.getValue().getTextElements()[0];

      assertFalse(CodeEditUtil.isNodeGenerated(root.getNode()));
      final XmlText text = text1;

      final XmlElement element = text.insertAtOffset(XmlElementFactory.getInstance(getProject()).createTagFromText("<bar/>"), 5);
      assertNotNull(element);
      assertTrue(element instanceof XmlText);
      assertEquals("01234", element.getText());
      assertEquals("<root>01234<bar/>56789</root>", text.getContainingFile().getText());
    }
  }.execute();
}
项目:intellij-ce-playground    文件:XmlTagWriteTest.java   
public void test1() throws IncorrectOperationException {
  XmlElementFactory elementFactory = XmlElementFactory.getInstance(getProject());
  final XmlTag xmlTag = XmlElementFactory.getInstance(getProject()).createTagFromText("<tag1/>");
  WriteCommandAction.runWriteCommandAction(null, new Runnable() {
    @Override
    public void run() {
      xmlTag.add(xmlTag.createChildTag("tag2", XmlUtil.EMPTY_URI, null, false));
    }
  });


  assertEquals("<tag1><tag2/></tag1>", xmlTag.getText());
  XmlTag createdFromText = elementFactory.createTagFromText(xmlTag.getText());
  assertEquals("tag1", createdFromText.getName());
  assertEquals(1, createdFromText.getSubTags().length);
  assertEquals("tag2", createdFromText.getSubTags()[0].getName());
}
项目:intellij-ce-playground    文件:XmlTagWriteTest.java   
public void test2() throws IncorrectOperationException {
  final XmlTag xmlTag = XmlElementFactory.getInstance(getProject()).createTagFromText("<tag1></tag1>");
  WriteCommandAction.runWriteCommandAction(null, new Runnable(){
    @Override
    public void run() {
      xmlTag.add(xmlTag.createChildTag("tag2", XmlUtil.EMPTY_URI, null, false));
    }
  });


  assertEquals("<tag1><tag2/></tag1>", xmlTag.getText());
  XmlTag createdFromText = XmlElementFactory.getInstance(getProject()).createTagFromText(xmlTag.getText());
  assertEquals("tag1", createdFromText.getName());
  assertEquals(1, createdFromText.getSubTags().length);
  assertEquals("tag2", createdFromText.getSubTags()[0].getName());
}
项目:intellij-ce-playground    文件:GenerationNode.java   
@NotNull
private static XmlTag expandEmptyTagIfNecessary(@NotNull XmlTag tag) {
  StringBuilder builder = new StringBuilder();
  boolean flag = false;

  for (PsiElement child : tag.getChildren()) {
    if (child instanceof XmlToken && XmlTokenType.XML_EMPTY_ELEMENT_END.equals(((XmlToken)child).getTokenType())) {
      flag = true;
      break;
    }
    builder.append(child.getText());
  }

  if (flag) {
    builder.append("></").append(tag.getName()).append('>');
    return XmlElementFactory.getInstance(tag.getProject()).createTagFromText(builder.toString(), XMLLanguage.INSTANCE);
  }
  return tag;
}
项目:intellij-ce-playground    文件:LayoutUsageData.java   
private static void addNamespaceAttributes(XmlTag tag, Map<String, String> namespaces, Project project) {
  final XmlAttribute[] parentAttributes = tag.getAttributes();
  final XmlAttribute firstParentAttribute = parentAttributes.length > 0 ? parentAttributes[0] : null;
  final XmlElementFactory factory = XmlElementFactory.getInstance(project);

  for (Map.Entry<String, String> entry : namespaces.entrySet()) {
    final String prefix = entry.getKey();
    final String namespace = entry.getValue();

    if (!namespace.equals(tag.getNamespaceByPrefix(prefix))) {
      final XmlAttribute xmlnsAttr = factory.createXmlAttribute("xmlns:" + prefix, namespace);

      if (firstParentAttribute != null) {
        tag.addBefore(xmlnsAttr, firstParentAttribute);
      }
      else {
        tag.add(xmlnsAttr);
      }
    }
  }
}
项目:intellij-ce-playground    文件:JavaFxCollapseSubTagToAttributeIntention.java   
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
  if (!FileModificationService.getInstance().preparePsiElementsForWrite(element)) return;
  final XmlTag tag = (XmlTag)element.getParent();
  final String value;
  if (tag.getSubTags().length == 0) {
    value = tag.getValue().getText().trim();
  }
  else {
    value = StringUtil.join(tag.getSubTags(), new Function<XmlTag, String>() {
      @Override
      public String fun(XmlTag childTag) {
        final XmlAttribute valueAttr = childTag.getAttribute(FxmlConstants.FX_VALUE);
        if (valueAttr != null) {
          return valueAttr.getValue();
        }
        return "";
      }
    }, ", ");
  }
  final XmlAttribute attribute = XmlElementFactory.getInstance(project).createXmlAttribute(tag.getName(), value);
  final XmlTag parentTag = tag.getParentTag();
  parentTag.add(attribute);
  tag.delete();
}
项目:tools-idea    文件:DomRootInvocationHandler.java   
protected XmlTag setEmptyXmlTag() {
  final XmlTag[] result = new XmlTag[]{null};
  getManager().runChange(new Runnable() {
    public void run() {
      try {
        final String namespace = getXmlElementNamespace();
        @NonNls final String nsDecl = StringUtil.isEmpty(namespace) ? "" : " xmlns=\"" + namespace + "\"";
        final XmlFile xmlFile = getFile();
        final XmlTag tag = XmlElementFactory.getInstance(xmlFile.getProject()).createTagFromText("<" + getXmlElementName() + nsDecl + "/>");
        result[0] = ((XmlDocument)xmlFile.getDocument().replace(((XmlFile)tag.getContainingFile()).getDocument())).getRootTag();
      }
      catch (IncorrectOperationException e) {
        LOG.error(e);
      }
    }
  });
  return result[0];
}
项目:tools-idea    文件:XmlTextTest.java   
public void testInsertAtOffset() throws Exception {
  new WriteCommandAction(getProject()) {

    @Override
    protected void run(final Result result) throws Throwable {
      String xml = "<root>0123456789</root>";
      XmlFile file = (XmlFile)PsiFileFactory.getInstance(getProject())
        .createFileFromText("foo.xml", StdFileTypes.XML, xml, (long)1, true, false);
      //System.out.println(DebugUtil.psiToString(file, false));
      XmlTag root = file.getDocument().getRootTag();
      final XmlText text1 = root.getValue().getTextElements()[0];

      assertFalse(CodeEditUtil.isNodeGenerated(root.getNode()));
      final XmlText text = text1;

      final XmlElement element = text.insertAtOffset(XmlElementFactory.getInstance(getProject()).createTagFromText("<bar/>"), 5);
      assertNotNull(element);
      assertTrue(element instanceof XmlText);
      assertEquals("01234", element.getText());
      assertEquals("<root>01234<bar/>56789</root>", text.getContainingFile().getText());
    }
  }.execute();
}
项目:tools-idea    文件:XmlTagWriteTest.java   
public void test1() throws IncorrectOperationException {
  XmlElementFactory elementFactory = XmlElementFactory.getInstance(getProject());
  final XmlTag xmlTag = XmlElementFactory.getInstance(getProject()).createTagFromText("<tag1/>");
  ApplicationManager.getApplication().runWriteAction(new Runnable() {
    public void run() {
      xmlTag.add(xmlTag.createChildTag("tag2", XmlUtil.EMPTY_URI, null, false));
    }
  });


  assertEquals("<tag1><tag2/></tag1>", xmlTag.getText());
  XmlTag createdFromText = elementFactory.createTagFromText(xmlTag.getText());
  assertEquals("tag1", createdFromText.getName());
  assertEquals(1, createdFromText.getSubTags().length);
  assertEquals("tag2", createdFromText.getSubTags()[0].getName());
}
项目:tools-idea    文件:AddXsiSchemaLocationForExtResourceAction.java   
private static void doIt(final PsiFile file, final Editor editor, final String uri, final XmlTag tag, final String s) throws IncorrectOperationException {
  if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
  final XmlElementFactory elementFactory = XmlElementFactory.getInstance(file.getProject());

  if (tag.getAttributeValue(XMLNS_XSI_ATTR_NAME) == null) {
    tag.add(elementFactory.createXmlAttribute(XMLNS_XSI_ATTR_NAME, XmlUtil.XML_SCHEMA_INSTANCE_URI));
  }

  final XmlAttribute locationAttribute = tag.getAttribute(XSI_SCHEMA_LOCATION_ATTR_NAME);
  final String toInsert = uri + " " + s;
  int offset = s.length();

  if (locationAttribute == null) {
    tag.add(elementFactory.createXmlAttribute(XSI_SCHEMA_LOCATION_ATTR_NAME, toInsert));
  } else {
    final String newValue = locationAttribute.getValue() + "\n" + toInsert;
    locationAttribute.setValue(newValue);
  }

  CodeStyleManager.getInstance(file.getProject()).reformat(tag);

  @SuppressWarnings("ConstantConditions")
  final TextRange range = tag.getAttribute(XSI_SCHEMA_LOCATION_ATTR_NAME).getValueElement().getTextRange();
  final TextRange textRange = new TextRange(range.getEndOffset() - offset - 1, range.getEndOffset() - 1);
  editor.getCaretModel().moveToOffset(textRange.getStartOffset());
}
项目:tools-idea    文件:GenerationNode.java   
@NotNull
private static XmlTag expandEmptyTagIfNeccessary(@NotNull XmlTag tag) {
  StringBuilder builder = new StringBuilder();
  boolean flag = false;

  for (PsiElement child : tag.getChildren()) {
    if (child instanceof XmlToken && XmlTokenType.XML_EMPTY_ELEMENT_END.equals(((XmlToken)child).getTokenType())) {
      flag = true;
      break;
    }
    builder.append(child.getText());
  }

  if (flag) {
    builder.append("></").append(tag.getName()).append('>');
    return XmlElementFactory.getInstance(tag.getProject()).createTagFromText(builder.toString(), XMLLanguage.INSTANCE);
  }
  return tag;
}
项目:tools-idea    文件:JavaFxCollapseSubTagToAttributeIntention.java   
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
  if (!FileModificationService.getInstance().preparePsiElementsForWrite(element)) return;
  final XmlTag tag = (XmlTag)element.getParent();
  final String value;
  if (tag.getSubTags().length == 0) {
    value = tag.getValue().getText().trim();
  }
  else {
    value = StringUtil.join(tag.getSubTags(), new Function<XmlTag, String>() {
      @Override
      public String fun(XmlTag childTag) {
        final XmlAttribute valueAttr = childTag.getAttribute(FxmlConstants.FX_VALUE);
        if (valueAttr != null) {
          return valueAttr.getValue();
        }
        return "";
      }
    }, ", ");
  }
  final XmlAttribute attribute = XmlElementFactory.getInstance(project).createXmlAttribute(tag.getName(), value);
  final XmlTag parentTag = tag.getParentTag();
  parentTag.add(attribute);
  tag.delete();
}
项目:consulo-javafx    文件:JavaFxCollapseSubTagToAttributeIntention.java   
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
  if (!FileModificationService.getInstance().preparePsiElementsForWrite(element)) return;
  final XmlTag tag = (XmlTag)element.getParent();
  final String value;
  if (tag.getSubTags().length == 0) {
    value = tag.getValue().getText().trim();
  }
  else {
    value = StringUtil.join(tag.getSubTags(), new Function<XmlTag, String>() {
      @Override
      public String fun(XmlTag childTag) {
        final XmlAttribute valueAttr = childTag.getAttribute(FxmlConstants.FX_VALUE);
        if (valueAttr != null) {
          return valueAttr.getValue();
        }
        return "";
      }
    }, ", ");
  }
  final XmlAttribute attribute = XmlElementFactory.getInstance(project).createXmlAttribute(tag.getName(), value);
  final XmlTag parentTag = tag.getParentTag();
  parentTag.add(attribute);
  tag.delete();
}
项目:consulo-xml    文件:DomRootInvocationHandler.java   
protected XmlTag setEmptyXmlTag() {
  final XmlTag[] result = new XmlTag[]{null};
  getManager().runChange(new Runnable() {
    public void run() {
      try {
        final String namespace = getXmlElementNamespace();
        @NonNls final String nsDecl = StringUtil.isEmpty(namespace) ? "" : " xmlns=\"" + namespace + "\"";
        final XmlFile xmlFile = getFile();
        final XmlTag tag = XmlElementFactory.getInstance(xmlFile.getProject()).createTagFromText("<" + getXmlElementName() + nsDecl + "/>");
        result[0] = ((XmlDocument)xmlFile.getDocument().replace(((XmlFile)tag.getContainingFile()).getDocument())).getRootTag();
      }
      catch (IncorrectOperationException e) {
        LOG.error(e);
      }
    }
  });
  return result[0];
}
项目:consulo-xml    文件:XmlAttributeImpl.java   
@Override
public PsiElement setName(@NotNull final String nameText) throws IncorrectOperationException
{
    final ASTNode name = XmlChildRole.ATTRIBUTE_NAME_FINDER.findChild(this);
    final String oldName = name.getText();
    final PomModel model = PomManager.getModel(getProject());
    final XmlAttribute attribute = XmlElementFactory.getInstance(getProject()).createXmlAttribute(nameText, "");
    final ASTNode newName = XmlChildRole.ATTRIBUTE_NAME_FINDER.findChild((ASTNode) attribute);
    final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
    model.runTransaction(new PomTransactionBase(getParent(), aspect)
    {
        @Override
        public PomModelEvent runInner()
        {
            final PomModelEvent event = new PomModelEvent(model);
            PsiFile file = getContainingFile();
            XmlChangeSet xmlAspectChangeSet = new XmlAspectChangeSetImpl(model, file instanceof XmlFile ? (XmlFile) file : null);
            xmlAspectChangeSet.add(new XmlAttributeSetImpl(getParent(), oldName, null));
            xmlAspectChangeSet.add(new XmlAttributeSetImpl(getParent(), nameText, getValue()));
            event.registerChangeSet(model.getModelAspect(XmlAspect.class), xmlAspectChangeSet);
            CodeEditUtil.replaceChild(XmlAttributeImpl.this, name, newName);
            return event;
        }
    });
    return this;
}
项目:consulo-xml    文件:XmlAttributeValueImpl.java   
@Override
public PsiLanguageInjectionHost updateText(@NotNull String text) {
  try {
    final String quoteChar = getTextLength() > 0 ? getText().substring(0, 1) : "";
    String contents = StringUtil.containsAnyChar(quoteChar, "'\"") ?
            StringUtil.trimEnd(StringUtil.trimStart(text, quoteChar), quoteChar) : text;
    XmlAttribute newAttribute = XmlElementFactory.getInstance(getProject()).createXmlAttribute("q", contents);
    XmlAttributeValue newValue = newAttribute.getValueElement();

    CheckUtil.checkWritable(this);
    replaceAllChildrenToChildrenOf(newValue.getNode());
  }
  catch (IncorrectOperationException e) {
    LOG.error(e);
  }
  return this;
}
项目:consulo-xml    文件:AddXsiSchemaLocationForExtResourceAction.java   
private static void doIt(final PsiFile file, final Editor editor, final String uri, final XmlTag tag, final String s) throws IncorrectOperationException {
  if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
  final XmlElementFactory elementFactory = XmlElementFactory.getInstance(file.getProject());

  if (tag.getAttributeValue(XMLNS_XSI_ATTR_NAME) == null) {
    tag.add(elementFactory.createXmlAttribute(XMLNS_XSI_ATTR_NAME, XmlUtil.XML_SCHEMA_INSTANCE_URI));
  }

  final XmlAttribute locationAttribute = tag.getAttribute(XSI_SCHEMA_LOCATION_ATTR_NAME);
  final String toInsert = uri + " " + s;
  int offset = s.length();

  if (locationAttribute == null) {
    tag.add(elementFactory.createXmlAttribute(XSI_SCHEMA_LOCATION_ATTR_NAME, toInsert));
  } else {
    final String newValue = locationAttribute.getValue() + "\n" + toInsert;
    locationAttribute.setValue(newValue);
  }

  CodeStyleManager.getInstance(file.getProject()).reformat(tag);

  @SuppressWarnings("ConstantConditions")
  final TextRange range = tag.getAttribute(XSI_SCHEMA_LOCATION_ATTR_NAME).getValueElement().getTextRange();
  final TextRange textRange = new TextRange(range.getEndOffset() - offset - 1, range.getEndOffset() - 1);
  editor.getCaretModel().moveToOffset(textRange.getStartOffset());
}
项目:intellij-ce-playground    文件:XmlTagValueImpl.java   
private void setText(String value, boolean defaultPolicy) {
  try {
    XmlText text = null;
    if (value != null) {
      final XmlText[] texts = getTextElements();
      if (texts.length == 0) {
        text = (XmlText)myTag.add(XmlElementFactory.getInstance(myTag.getProject()).createDisplayText("x"));
      } else {
        text = texts[0];
      }
      if (StringUtil.isEmpty(value)) {
        text.delete();
      }
      else {
        if (defaultPolicy && text instanceof XmlTextImpl) {
          ((XmlTextImpl)text).doSetValue(value, new DefaultXmlPsiPolicy());
        } else {
          text.setValue(value);
        }
      }
    }

    if(myElements.length > 0){
      for (final XmlTagChild child : myElements) {
        if (child != text) {
          child.delete();
        }
      }
    }
  }
  catch (IncorrectOperationException e) {
    LOG.error(e);
  }
}
项目:intellij-ce-playground    文件:SearchingInXmlTest.java   
public void test() throws IncorrectOperationException {
  XmlTag xmlTag = XmlElementFactory.getInstance(getProject()).createTagFromText("<root>" +
                                                   "<tag1><name>name1</name><value>1</value></tag1>" +
                                                   "<tag1><name>name2</name><value>2</value></tag1>" +
                                                   "<tag1><name>name3</name><value>3</value></tag1>" +
                                                   "<tag1><name>name4</name><value>4</value></tag1>" +
                                                   "<tag1><name>name5</name><value>5</value></tag1>" +
                                                   "<tag1><name>name6</name><value>6</value></tag1>" +
                                                   "<tag1><name>name7</name><value>7</value></tag1>" +
                                                   "</root>");
  XmlTag found = XmlUtil.find("name", "name4", "tag1", xmlTag);

  assertEquals("4", found.findFirstSubTag("value").getValue().getText());
}
项目:intellij-ce-playground    文件:XmlTagWriteTest.java   
public void test3() throws Exception{
  final XmlTag tag = XmlElementFactory.getInstance(getProject()).createTagFromText("<b>\n0123456</b>");
  final XmlText text = (XmlText) tag.getValue().getChildren()[0];
  String textS = text.getText();
  ApplicationManager.getApplication().runWriteAction(new Runnable() {
    @Override
    public void run() {
      text.insertText("lala", 2);
    }
  });

  XmlText text2 = (XmlText)tag.getValue().getChildren()[0];
  assertEquals(textS.substring(0, 2) + "lala" + textS.substring(2), text2.getText());
}
项目:intellij-ce-playground    文件:AddAttributeValueIntentionFix.java   
@Override
public void invoke(@NotNull Project project,
                   @NotNull PsiFile file,
                   @Nullable("is null when called from inspection") final Editor editor,
                   @NotNull PsiElement startElement,
                   @NotNull PsiElement endElement) {
  final XmlAttribute attribute = PsiTreeUtil.getNonStrictParentOfType(startElement, XmlAttribute.class);
  if (attribute == null || attribute.getValue() != null) {
    return;
  }

  if (!FileModificationService.getInstance().prepareFileForWrite(attribute.getContainingFile())) {
    return;
  }

  new WriteCommandAction(project) {
    @Override
    protected void run(@NotNull final Result result) {
      final XmlAttribute attributeWithValue = XmlElementFactory.getInstance(getProject()).createXmlAttribute(attribute.getName(), "");
      final PsiElement newAttribute = attribute.replace(attributeWithValue);

      if (editor != null && newAttribute != null && newAttribute instanceof XmlAttribute && newAttribute.isValid()) {
        final XmlAttributeValue valueElement = ((XmlAttribute)newAttribute).getValueElement();
        if (valueElement != null) {
          editor.getCaretModel().moveToOffset(valueElement.getTextOffset());
          AutoPopupController.getInstance(newAttribute.getProject()).scheduleAutoPopup(editor);
        }
      }
    }
  }.execute();
}
项目:intellij-ce-playground    文件:AddXsiSchemaLocationForExtResourceAction.java   
private static void doIt(final PsiFile file, final Editor editor, final String uri, final XmlTag tag, final String s) throws IncorrectOperationException {
  if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
  final XmlElementFactory elementFactory = XmlElementFactory.getInstance(file.getProject());

  if (tag.getAttributeValue(XMLNS_XSI_ATTR_NAME) == null) {
    tag.add(elementFactory.createXmlAttribute(XMLNS_XSI_ATTR_NAME, XmlUtil.XML_SCHEMA_INSTANCE_URI));
  }

  final XmlAttribute locationAttribute = tag.getAttribute(XSI_SCHEMA_LOCATION_ATTR_NAME);
  final String toInsert = uri + " " + s;
  int offset = s.length();

  if (locationAttribute == null) {
    tag.add(elementFactory.createXmlAttribute(XSI_SCHEMA_LOCATION_ATTR_NAME, toInsert));
  } else {
    final String newValue = locationAttribute.getValue() + "\n" + toInsert;
    locationAttribute.setValue(newValue);
  }

  PsiDocumentManager.getInstance(file.getProject()).doPostponedOperationsAndUnblockDocument(editor.getDocument());
  CodeStyleManager.getInstance(file.getProject()).reformat(tag);

  @SuppressWarnings("ConstantConditions")
  final TextRange range = tag.getAttribute(XSI_SCHEMA_LOCATION_ATTR_NAME).getValueElement().getTextRange();
  final TextRange textRange = new TextRange(range.getEndOffset() - offset - 1, range.getEndOffset() - 1);
  editor.getCaretModel().moveToOffset(textRange.getStartOffset());
}
项目:intellij-ce-playground    文件:TemplateToken.java   
private static void addMissingAttributes(@NotNull XmlTag tag, @NotNull Map<String, String> attributes) {
  for (Map.Entry<String, String> attribute : attributes.entrySet()) {
    if (!XmlEmmetParser.DEFAULT_ATTRIBUTE_NAME.equals(attribute.getKey()) && tag.getAttribute(attribute.getKey()) == null) {
      XmlTag htmlTag = XmlElementFactory.getInstance(tag.getProject()).createHTMLTagFromText("<dummy " + attribute.getKey() + "=\"\"/>");
      final XmlAttribute newAttribute = ArrayUtil.getFirstElement(htmlTag.getAttributes());
      if (newAttribute != null) {
        tag.add(newAttribute);
      }
    }
  }
}
项目:intellij-ce-playground    文件:XmlEmmetParser.java   
@Nullable
private String parseAttributeName() {
  String name = "";
  ZenCodingToken token = getToken();
  while (token != null) {
    if ((token instanceof IdentifierToken)) {
      name += ((IdentifierToken)token).getText();
    }
    else if (token instanceof OperationToken && 
             (((OperationToken)token).getSign() == '+' || ((OperationToken)token).getSign() == '-')) {
      name += ((OperationToken)token).getSign();
    }
    else {
      break;
    }
    advance();
    token = getToken();
  }

  if (name.isEmpty()) {
    return null;
  }

  final XmlTag tag = XmlElementFactory.getInstance(myCallback.getProject()).createTagFromText("<tag " + name + "=''/>", StdLanguages.HTML);
  XmlAttribute[] attributes = tag.getAttributes();
  if (attributes.length == 1) {
    return attributes[0].getName();
  }
  else {
    return null;
  }
}
项目:intellij-ce-playground    文件:PrefixReferenceProvider.java   
@Nullable
@Override
public LocalQuickFix[] getQuickFixes() {
  final PsiElement element = getElement();
  final XmlElementFactory factory = XmlElementFactory.getInstance(element.getProject());
  final String value = ((XmlAttributeValue)element).getValue();
  final String[] name = value.split(":");
  final XmlTag tag = factory.createTagFromText("<" + (name.length > 1 ? name[1] : value) + " />", XMLLanguage.INSTANCE);

  return new LocalQuickFix[] { XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(tag, getCanonicalText(), null) };
}
项目:intellij-ce-playground    文件:RegExResponseHandler.java   
@NotNull
@Override
public Task[] parseIssues(@NotNull String response, int max) throws Exception {
  final List<String> placeholders = getPlaceholders(myTaskRegex);
  if (!placeholders.contains(ID_PLACEHOLDER) || !placeholders.contains(SUMMARY_PLACEHOLDER)) {
    throw new Exception("Incorrect Task Pattern");
  }

  final String taskPatternWithoutPlaceholders = myTaskRegex.replaceAll("\\{.+?\\}", "");
  Matcher matcher = Pattern
    .compile(taskPatternWithoutPlaceholders,
             Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL | Pattern.UNICODE_CASE | Pattern.CANON_EQ)
    .matcher(response);

  List<Task> tasks = new ArrayList<Task>();
  for (int i = 0; i < max && matcher.find(); i++) {
    String id = matcher.group(placeholders.indexOf(ID_PLACEHOLDER) + 1);
    String summary = matcher.group(placeholders.indexOf(SUMMARY_PLACEHOLDER) + 1);
    // temporary workaround to make AssemblaIntegrationTestPass
    final String finalSummary = summary;
    summary = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
      @Override
      public String compute() {
        XmlElementFactory factory = XmlElementFactory.getInstance(ProjectManager.getInstance().getDefaultProject());
        XmlTag text = factory.createTagFromText("<a>" + finalSummary + "</a>");
        String trimmedText = text.getValue().getTrimmedText();
        return XmlUtil.decode(trimmedText);
      }
    });
    tasks.add(new GenericTask(id, summary, myRepository));
  }
  return tasks.toArray(new Task[tasks.size()]);
}
项目:intellij-ce-playground    文件:XsltNamespaceContext.java   
public static IntentionAction[] getUnresolvedNamespaceFixesStatic(PsiReference reference, String localName) {
    final XmlElementFactory factory = XmlElementFactory.getInstance(reference.getElement().getProject());
    final XmlTag tag = factory.createTagFromText("<" + reference.getCanonicalText() + ":" + localName + " />", XMLLanguage.INSTANCE);

    final XmlFile xmlFile = PsiTreeUtil.getContextOfType(reference.getElement(), XmlFile.class, true);
    return new IntentionAction[]{
            new MyCreateNSDeclarationAction(tag, reference.getCanonicalText(), xmlFile)
    };
}
项目:intellij-ce-playground    文件:JavaFxWrapWithDefineIntention.java   
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
  if (!FileModificationService.getInstance().preparePsiElementsForWrite(element)) return;
  final XmlTag tagFromText = XmlElementFactory.getInstance(project).createTagFromText("<" + FxmlConstants.FX_DEFINE + "/>");
  tagFromText.addSubTag(myTag, true);
  myTag.replace(tagFromText);
}
项目:tools-idea    文件:XmlTagValueImpl.java   
private void setText(String value, boolean defaultPolicy) {
  try {
    XmlText text = null;
    if (value != null) {
      final XmlText[] texts = getTextElements();
      if (texts.length == 0) {
        text = (XmlText)myTag.add(XmlElementFactory.getInstance(myTag.getProject()).createDisplayText("x"));
      } else {
        text = texts[0];
      }
      if (StringUtil.isEmpty(value)) {
        text.delete();
      }
      else {
        if (defaultPolicy && text instanceof XmlTextImpl) {
          ((XmlTextImpl)text).doSetValue(value, new DefaultXmlPsiPolicy());
        } else {
          text.setValue(value);
        }
      }
    }

    if(myElements.length > 0){
      for (final XmlTagChild child : myElements) {
        if (child != text) {
          child.delete();
        }
      }
    }
  }
  catch (IncorrectOperationException e) {
    LOG.error(e);
  }
}
项目:tools-idea    文件:SearchingInXmlTest.java   
public void test() throws IncorrectOperationException {
  XmlTag xmlTag = XmlElementFactory.getInstance(getProject()).createTagFromText("<root>" +
                                                   "<tag1><name>name1</name><value>1</value></tag1>" +
                                                   "<tag1><name>name2</name><value>2</value></tag1>" +
                                                   "<tag1><name>name3</name><value>3</value></tag1>" +
                                                   "<tag1><name>name4</name><value>4</value></tag1>" +
                                                   "<tag1><name>name5</name><value>5</value></tag1>" +
                                                   "<tag1><name>name6</name><value>6</value></tag1>" +
                                                   "<tag1><name>name7</name><value>7</value></tag1>" +
                                                   "</root>");
  XmlTag found = XmlUtil.find("name", "name4", "tag1", xmlTag);

  assertEquals("4", found.findFirstSubTag("value").getValue().getText());
}
项目:tools-idea    文件:XmlTagWriteTest.java   
public void test2() throws IncorrectOperationException {
  final XmlTag xmlTag = XmlElementFactory.getInstance(getProject()).createTagFromText("<tag1></tag1>");
  ApplicationManager.getApplication().runWriteAction(new Runnable() {
    public void run() {
      xmlTag.add(xmlTag.createChildTag("tag2", XmlUtil.EMPTY_URI, null, false));
    }
  });


  assertEquals("<tag1><tag2/></tag1>", xmlTag.getText());
  XmlTag createdFromText = XmlElementFactory.getInstance(getProject()).createTagFromText(xmlTag.getText());
  assertEquals("tag1", createdFromText.getName());
  assertEquals(1, createdFromText.getSubTags().length);
  assertEquals("tag2", createdFromText.getSubTags()[0].getName());
}
项目:tools-idea    文件:XmlTagWriteTest.java   
public void test3() throws Exception{
  final XmlTag tag = XmlElementFactory.getInstance(getProject()).createTagFromText("<b>\n0123456</b>");
  final XmlText text = (XmlText) tag.getValue().getChildren()[0];
  String textS = text.getText();
  ApplicationManager.getApplication().runWriteAction(new Runnable() {
    public void run() {
      text.insertText("lala", 2);
    }
  });

  XmlText text2 = (XmlText)tag.getValue().getChildren()[0];
  assertEquals(textS.substring(0, 2) + "lala" + textS.substring(2), text2.getText());
}
项目:tools-idea    文件:TemplateToken.java   
private static void addAttributesBefore(XmlTag tag, List<Pair<String, String>> attr2value) {
  XmlAttribute firstAttribute = ArrayUtil.getFirstElement(tag.getAttributes());
  XmlElementFactory factory = XmlElementFactory.getInstance(tag.getProject());
  for (Pair<String, String> pair : attr2value) {
    XmlAttribute xmlAttribute = factory.createXmlAttribute(pair.first, "");
    if (firstAttribute != null) {
      tag.addBefore(xmlAttribute, firstAttribute);
    }
    else {
      tag.add(xmlAttribute);
    }
  }
}
项目:tools-idea    文件:PrefixReferenceProvider.java   
@Nullable
@Override
public LocalQuickFix[] getQuickFixes() {
  final PsiElement element = getElement();
  final XmlElementFactory factory = XmlElementFactory.getInstance(element.getProject());
  final String value = ((XmlAttributeValue)element).getValue();
  final String[] name = value.split(":");
  final XmlTag tag = factory.createTagFromText("<" + (name.length > 1 ? name[1] : value) + " />", XMLLanguage.INSTANCE);

  return new LocalQuickFix[] { CreateNSDeclarationIntentionFix.createFix(tag, getCanonicalText()) };
}
项目:tools-idea    文件:GenericRepository.java   
public Task[] parseResponse(String query, int max, String response) throws Exception {

    final List<String> placeholders = getPlaceholders(getTaskPattern());
    if (!placeholders.contains(ID_PLACEHOLDER) || !placeholders.contains(SUMMARY_PLACEHOLDER)) {
      throw new Exception("Incorrect Task Pattern");
    }

    final String taskPatternWithoutPlaceholders = getTaskPattern().replaceAll("\\{.+?\\}", "");
    Matcher matcher = Pattern
      .compile(taskPatternWithoutPlaceholders,
               Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL | Pattern.UNICODE_CASE | Pattern.CANON_EQ)
      .matcher(response);

    List<Task> tasks = new ArrayList<Task>();
    while (matcher.find()) {
      String id = matcher.group(placeholders.indexOf(ID_PLACEHOLDER) + 1);
      String summary = matcher.group(placeholders.indexOf(SUMMARY_PLACEHOLDER) + 1);
      if (myResponseType == ResponseType.XML && summary != null) {
        final String finalSummary = summary;
        summary = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
          @Override
          public String compute() {
            XmlElementFactory factory = XmlElementFactory.getInstance(ProjectManager.getInstance().getDefaultProject());
            XmlTag text = factory.createTagFromText("<a>" + finalSummary + "</a>");
            return XmlUtil.decode(text.getValue().getTrimmedText());
          }
        });
      }
      tasks.add(new GenericTask(id, summary, this));
    }

    final boolean searchSupported = getTasksListURL().contains(QUERY_PLACEHOLDER);
    if (!searchSupported) {
      tasks = TaskSearchSupport.filterTasks(query != null ? query : "", tasks);
    }

    tasks = tasks.subList(0, Math.min(max, tasks.size()));

    return tasks.toArray(new Task[tasks.size()]);
  }
项目:tools-idea    文件:GenerateAntTest.java   
private void checkBuildsEqual(String generated, String expected) throws IncorrectOperationException {
  final CodeStyleManager manager = CodeStyleManager.getInstance(myProject);
  XmlTag genTag = XmlElementFactory.getInstance(myProject).createTagFromText(StringUtil.convertLineSeparators(generated));
  XmlTag expTag = XmlElementFactory.getInstance(myProject).createTagFromText(StringUtil.convertLineSeparators(expected));
  if (!tagsEqual(genTag, expTag)) {
    genTag = (XmlTag)manager.reformat(manager.reformat(genTag));
    expTag = (XmlTag)manager.reformat(manager.reformat(expTag));
    assertEquals("Text mismatch: ", expTag.getText(), genTag.getText());
  }
}
项目:tools-idea    文件:XsltNamespaceContext.java   
public static IntentionAction[] getUnresolvedNamespaceFixesStatic(PsiReference reference, String localName) {
    final XmlElementFactory factory = XmlElementFactory.getInstance(reference.getElement().getProject());
    final XmlTag tag = factory.createTagFromText("<" + reference.getCanonicalText() + ":" + localName + " />", XMLLanguage.INSTANCE);

    final XmlFile xmlFile = PsiTreeUtil.getContextOfType(reference.getElement(), XmlFile.class, true);
    return new IntentionAction[]{
            new MyCreateNSDeclarationAction(tag, reference.getCanonicalText(), xmlFile)
    };
}
项目:tools-idea    文件:JavaFxWrapWithDefineIntention.java   
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
  if (!FileModificationService.getInstance().preparePsiElementsForWrite(element)) return;
  final XmlTag tagFromText = XmlElementFactory.getInstance(project).createTagFromText("<" + FxmlConstants.FX_DEFINE + "/>");
  tagFromText.addSubTag(myTag, true);
  myTag.replace(tagFromText);
}