2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.psi
;
18 import com
.intellij
.lang
.ASTFactory
;
19 import com
.intellij
.lang
.Language
;
20 import com
.intellij
.lang
.xml
.XMLLanguage
;
21 import com
.intellij
.openapi
.project
.Project
;
22 import com
.intellij
.openapi
.fileTypes
.FileType
;
23 import com
.intellij
.openapi
.fileTypes
.StdFileTypes
;
24 import com
.intellij
.psi
.xml
.*;
25 import com
.intellij
.util
.IncorrectOperationException
;
26 import com
.intellij
.xml
.util
.XmlTagTextUtil
;
27 import org
.jetbrains
.annotations
.NotNull
;
28 import org
.jetbrains
.annotations
.NonNls
;
31 * @author Dmitry Avdeev
33 public class XmlElementFactoryImpl
extends XmlElementFactory
{
35 private final Project myProject
;
37 public XmlElementFactoryImpl(Project project
) {
42 public XmlTag
createTagFromText(@NotNull @NonNls String text
, @NotNull Language language
) throws IncorrectOperationException
{
43 assert language
instanceof XMLLanguage
:"Tag can be created only for xml language";
44 FileType type
= language
.getAssociatedFileType();
45 if (type
== null) type
= StdFileTypes
.XML
;
46 final XmlDocument document
= createXmlDocument(text
, "dummy."+ type
.getDefaultExtension());
47 final XmlTag tag
= document
.getRootTag();
48 if (tag
== null) throw new IncorrectOperationException("Incorrect tag text");
53 public XmlTag
createTagFromText(@NotNull String text
) throws IncorrectOperationException
{
54 return createTagFromText(text
, StdFileTypes
.XML
.getLanguage());
58 public XmlAttribute
createXmlAttribute(@NotNull String name
, String value
) throws IncorrectOperationException
{
59 final XmlDocument document
= createXmlDocument("<tag " + name
+ "=\"" + value
+ "\"/>", "dummy.xml");
60 XmlTag tag
= document
.getRootTag();
62 return tag
.getAttributes()[0];
66 public XmlText
createDisplayText(@NotNull String s
) throws IncorrectOperationException
{
67 final XmlTag tagFromText
= createTagFromText("<a>" + XmlTagTextUtil
.getCDATAQuote(s
) + "</a>");
68 final XmlText
[] textElements
= tagFromText
.getValue().getTextElements();
69 if (textElements
.length
== 0) return (XmlText
)ASTFactory
.composite(XmlElementType
.XML_TEXT
);
70 return textElements
[0];
74 public XmlTag
createXHTMLTagFromText(@NotNull String text
) throws IncorrectOperationException
{
75 final XmlDocument document
= createXmlDocument(text
, "dummy.xhtml");
76 final XmlTag tag
= document
.getRootTag();
81 private XmlDocument
createXmlDocument(@NonNls final String text
, @NonNls final String fileName
) {
82 final XmlDocument document
= ((XmlFile
)PsiFileFactory
.getInstance(myProject
).createFileFromText(fileName
, text
)).getDocument();
83 assert document
!= null;