22 * Copyright © Magento, Inc. All rights reserved.
33 * See COPYING.txt for license details.
44 */
5+
56package com .magento .idea .magento2plugin .actions .generation .generator ;
67
78import com .intellij .openapi .command .WriteCommandAction ;
1920import com .magento .idea .magento2plugin .actions .generation .generator .util .XmlFilePositionUtil ;
2021import com .magento .idea .magento2plugin .magento .files .ModuleDiXml ;
2122import com .magento .idea .magento2plugin .util .xml .XmlPsiTreeUtil ;
22- import org .jetbrains .annotations .NotNull ;
2323import java .io .IOException ;
24- import java .util .*;
24+ import java .util .Collection ;
25+ import java .util .Properties ;
26+ import org .jetbrains .annotations .NotNull ;
2527
2628public class PluginDiXmlGenerator extends FileGenerator {
2729 private final GetCodeTemplate getCodeTemplate ;
2830 private final FindOrCreateDiXml findOrCreateDiXml ;
2931 private final XmlFilePositionUtil positionUtil ;
30- private PluginDiXmlData pluginFileData ;
31- private Project project ;
32+ private final PluginDiXmlData pluginFileData ;
33+ private final Project project ;
3234 private boolean isTypeDeclared ;
3335
34- public PluginDiXmlGenerator (@ NotNull PluginDiXmlData pluginFileData , Project project ) {
36+ /**
37+ * Constructor.
38+ *
39+ * @param pluginFileData
40+ * @param project
41+ */
42+ public PluginDiXmlGenerator (final @ NotNull PluginDiXmlData pluginFileData , final Project project ) {
3543 super (project );
3644 this .pluginFileData = pluginFileData ;
3745 this .project = project ;
3846 this .getCodeTemplate = GetCodeTemplate .getInstance (project );
39- this .findOrCreateDiXml = FindOrCreateDiXml . getInstance (project );
47+ this .findOrCreateDiXml = new FindOrCreateDiXml (project );
4048 this .positionUtil = XmlFilePositionUtil .getInstance ();
4149 }
4250
43- public PsiFile generate (String actionName )
44- {
45- PsiFile diXmlFile = findOrCreateDiXml .execute (actionName , pluginFileData .getPluginModule (), pluginFileData .getArea ());
46- XmlAttributeValue typeAttributeValue = getTypeAttributeValue ((XmlFile ) diXmlFile );
51+ /**
52+ * Creates a module di.xml file.
53+ *
54+ * @param actionName String
55+ * @return PsiFile
56+ */
57+ @ Override
58+ public PsiFile generate (final String actionName ) {
59+ final PsiFile diXmlFile = findOrCreateDiXml .execute (actionName , pluginFileData .getPluginModule (), pluginFileData .getArea ());
60+ final XmlAttributeValue typeAttributeValue = getTypeAttributeValue ((XmlFile ) diXmlFile );
4761 boolean isPluginDeclared = false ;
4862 this .isTypeDeclared = false ;
4963 if (typeAttributeValue != null ) {
@@ -54,22 +68,21 @@ public PsiFile generate(String actionName)
5468 return null ;
5569 }
5670 WriteCommandAction .runWriteCommandAction (project , () -> {
57- StringBuffer textBuf = new StringBuffer ();
71+ final StringBuffer textBuf = new StringBuffer ();
5872 try {
5973 textBuf .append (getCodeTemplate .execute (ModuleDiXml .TEMPLATE_PLUGIN , getAttributes ()));
6074 } catch (IOException e ) {
61- e .printStackTrace ();
6275 return ;
6376 }
6477
65- int insertPos = isTypeDeclared
78+ final int insertPos = isTypeDeclared
6679 ? positionUtil .getEndPositionOfTag (PsiTreeUtil .getParentOfType (typeAttributeValue , XmlTag .class ))
6780 : positionUtil .getRootInsertPosition ((XmlFile ) diXmlFile );
6881 if (textBuf .length () > 0 && insertPos >= 0 ) {
69- PsiDocumentManager psiDocumentManager = PsiDocumentManager .getInstance (project );
70- Document document = psiDocumentManager .getDocument (diXmlFile );
82+ final PsiDocumentManager psiDocumentManager = PsiDocumentManager .getInstance (project );
83+ final Document document = psiDocumentManager .getDocument (diXmlFile );
7184 document .insertString (insertPos , textBuf );
72- int endPos = insertPos + textBuf .length () + 1 ;
85+ final int endPos = insertPos + textBuf .length () + 1 ;
7386 CodeStyleManager .getInstance (project ).reformatText (diXmlFile , insertPos , endPos );
7487 psiDocumentManager .commitDocument (document );
7588 }
@@ -78,22 +91,22 @@ public PsiFile generate(String actionName)
7891 return diXmlFile ;
7992 }
8093
81- private boolean isPluginDeclared (XmlAttributeValue typeAttributeValue ) {
82- XmlTag xmlTag = PsiTreeUtil .getParentOfType (typeAttributeValue , XmlTag .class );
83- XmlTag [] xmlTags = PsiTreeUtil .getChildrenOfType (xmlTag , XmlTag .class );
94+ private boolean isPluginDeclared (final XmlAttributeValue typeAttributeValue ) {
95+ final XmlTag xmlTag = PsiTreeUtil .getParentOfType (typeAttributeValue , XmlTag .class );
96+ final XmlTag [] xmlTags = PsiTreeUtil .getChildrenOfType (xmlTag , XmlTag .class );
8497 if (xmlTags == null ) {
8598 return false ;
8699 }
87- for (XmlTag child : xmlTags ) {
100+ for (final XmlTag child : xmlTags ) {
88101 if (!child .getName ().equals (ModuleDiXml .PLUGIN_TAG_NAME )) {
89102 continue ;
90103 }
91- XmlAttribute [] xmlAttributes = PsiTreeUtil .getChildrenOfType (child , XmlAttribute .class );
92- for (XmlAttribute xmlAttribute : xmlAttributes ) {
104+ final XmlAttribute [] xmlAttributes = PsiTreeUtil .getChildrenOfType (child , XmlAttribute .class );
105+ for (final XmlAttribute xmlAttribute : xmlAttributes ) {
93106 if (!xmlAttribute .getName ().equals (ModuleDiXml .PLUGIN_TYPE_ATTRIBUTE )) {
94107 continue ;
95108 }
96- String declaredClass = PhpLangUtil .toPresentableFQN (xmlAttribute .getValue ());
109+ final String declaredClass = PhpLangUtil .toPresentableFQN (xmlAttribute .getValue ());
97110 if (declaredClass .equals (pluginFileData .getPluginFqn ())) {
98111 return true ;
99112 }
@@ -103,20 +116,20 @@ private boolean isPluginDeclared(XmlAttributeValue typeAttributeValue) {
103116 return false ;
104117 }
105118
106- private XmlAttributeValue getTypeAttributeValue (XmlFile diXml ) {
107- Collection <XmlAttributeValue > pluginTypes = XmlPsiTreeUtil .findAttributeValueElements (diXml , ModuleDiXml .PLUGIN_TYPE_TAG , ModuleDiXml .PLUGIN_TYPE_ATTR_NAME );
108- String pluginClassFqn = pluginFileData .getTargetClass ().getPresentableFQN ();
109- for (XmlAttributeValue pluginType : pluginTypes ) {
110- if (! PhpLangUtil .toPresentableFQN (pluginType .getValue ()).equals (pluginClassFqn )) {
111- continue ;
119+ private XmlAttributeValue getTypeAttributeValue (final XmlFile diXml ) {
120+ final Collection <XmlAttributeValue > pluginTypes = XmlPsiTreeUtil .findAttributeValueElements (diXml , ModuleDiXml .PLUGIN_TYPE_TAG , ModuleDiXml .PLUGIN_TYPE_ATTR_NAME );
121+ final String pluginClassFqn = pluginFileData .getTargetClass ().getPresentableFQN ();
122+ for (final XmlAttributeValue pluginType : pluginTypes ) {
123+ if (PhpLangUtil .toPresentableFQN (pluginType .getValue ()).equals (pluginClassFqn )) {
124+ return pluginType ;
112125 }
113- return pluginType ;
114126 }
115127
116128 return null ;
117129 }
118130
119- protected void fillAttributes (Properties attributes ) {
131+ @ Override
132+ protected void fillAttributes (final Properties attributes ) {
120133 if (!isTypeDeclared ) {
121134 attributes .setProperty ("TYPE" , pluginFileData .getTargetClass ().getPresentableFQN ());
122135 }
0 commit comments