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
.pom
.references
;
18 import com
.intellij
.openapi
.util
.TextRange
;
19 import com
.intellij
.psi
.ElementManipulator
;
20 import com
.intellij
.psi
.ElementManipulators
;
21 import com
.intellij
.psi
.PsiElement
;
22 import com
.intellij
.psi
.DelegatePsiTarget
;
23 import com
.intellij
.psi
.meta
.PsiMetaOwner
;
24 import com
.intellij
.psi
.meta
.PsiMetaData
;
25 import com
.intellij
.psi
.meta
.PsiMetaDataTarget
;
26 import com
.intellij
.pom
.PomTarget
;
27 import org
.jetbrains
.annotations
.NotNull
;
32 public class PomReferenceUtil
{
34 public static String
getReferenceText(PomReference reference
) {
35 return reference
.getRangeInElement().substring(reference
.getElement().getText());
38 public static TextRange
getDefaultRangeInElement(PsiElement element
) {
39 final ElementManipulator
<PsiElement
> manipulator
= ElementManipulators
.getManipulator(element
);
40 assert manipulator
!= null: "Cannot find manipulator for " + element
;
41 return manipulator
.getRangeInElement(element
);
44 public static void changeContent(PomReference reference
, String newContent
) {
45 final ElementManipulator
<PsiElement
> manipulator
= ElementManipulators
.getManipulator(reference
.getElement());
46 assert manipulator
!= null: "Cannot find manipulator for " + reference
.getElement();
47 manipulator
.handleContentChange(reference
.getElement(), reference
.getRangeInElement(), newContent
);
52 public static PomTarget
convertPsi2Target(@NotNull PsiElement element
) {
53 if (element
instanceof PsiMetaOwner
) {
54 final PsiMetaOwner metaOwner
= (PsiMetaOwner
)element
;
55 final PsiMetaData psiMetaData
= metaOwner
.getMetaData();
56 if (psiMetaData
!= null) {
57 return new PsiMetaDataTarget(psiMetaData
);
60 if (element
instanceof PomTarget
) {
61 return (PomTarget
)element
;
63 return new DelegatePsiTarget(element
);